epyt-flow 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epyt_flow/EPANET/EPANET/SRC_engines/AUTHORS +28 -0
- epyt_flow/EPANET/EPANET/SRC_engines/LICENSE +21 -0
- epyt_flow/EPANET/EPANET/SRC_engines/Readme_SRC_Engines.txt +18 -0
- epyt_flow/EPANET/EPANET/SRC_engines/enumstxt.h +134 -0
- epyt_flow/EPANET/EPANET/SRC_engines/epanet.c +5578 -0
- epyt_flow/EPANET/EPANET/SRC_engines/epanet2.c +865 -0
- epyt_flow/EPANET/EPANET/SRC_engines/epanet2.def +131 -0
- epyt_flow/EPANET/EPANET/SRC_engines/errors.dat +73 -0
- epyt_flow/EPANET/EPANET/SRC_engines/funcs.h +193 -0
- epyt_flow/EPANET/EPANET/SRC_engines/genmmd.c +1000 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hash.c +177 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hash.h +28 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hydcoeffs.c +1151 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hydraul.c +1117 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hydsolver.c +720 -0
- epyt_flow/EPANET/EPANET/SRC_engines/hydstatus.c +476 -0
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2.h +431 -0
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_2.h +1786 -0
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_enums.h +468 -0
- epyt_flow/EPANET/EPANET/SRC_engines/inpfile.c +810 -0
- epyt_flow/EPANET/EPANET/SRC_engines/input1.c +707 -0
- epyt_flow/EPANET/EPANET/SRC_engines/input2.c +864 -0
- epyt_flow/EPANET/EPANET/SRC_engines/input3.c +2170 -0
- epyt_flow/EPANET/EPANET/SRC_engines/main.c +93 -0
- epyt_flow/EPANET/EPANET/SRC_engines/mempool.c +142 -0
- epyt_flow/EPANET/EPANET/SRC_engines/mempool.h +24 -0
- epyt_flow/EPANET/EPANET/SRC_engines/output.c +852 -0
- epyt_flow/EPANET/EPANET/SRC_engines/project.c +1359 -0
- epyt_flow/EPANET/EPANET/SRC_engines/quality.c +685 -0
- epyt_flow/EPANET/EPANET/SRC_engines/qualreact.c +743 -0
- epyt_flow/EPANET/EPANET/SRC_engines/qualroute.c +694 -0
- epyt_flow/EPANET/EPANET/SRC_engines/report.c +1489 -0
- epyt_flow/EPANET/EPANET/SRC_engines/rules.c +1362 -0
- epyt_flow/EPANET/EPANET/SRC_engines/smatrix.c +871 -0
- epyt_flow/EPANET/EPANET/SRC_engines/text.h +497 -0
- epyt_flow/EPANET/EPANET/SRC_engines/types.h +874 -0
- epyt_flow/EPANET/EPANET-MSX/MSX_Updates.txt +53 -0
- epyt_flow/EPANET/EPANET-MSX/Src/dispersion.h +27 -0
- epyt_flow/EPANET/EPANET-MSX/Src/hash.c +107 -0
- epyt_flow/EPANET/EPANET-MSX/Src/hash.h +28 -0
- epyt_flow/EPANET/EPANET-MSX/Src/include/epanetmsx.h +102 -0
- epyt_flow/EPANET/EPANET-MSX/Src/include/epanetmsx_export.h +42 -0
- epyt_flow/EPANET/EPANET-MSX/Src/mathexpr.c +937 -0
- epyt_flow/EPANET/EPANET-MSX/Src/mathexpr.h +39 -0
- epyt_flow/EPANET/EPANET-MSX/Src/mempool.c +204 -0
- epyt_flow/EPANET/EPANET-MSX/Src/mempool.h +24 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxchem.c +1285 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxcompiler.c +368 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxdict.h +42 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxdispersion.c +586 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxerr.c +116 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxfile.c +260 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxfuncs.c +175 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxfuncs.h +35 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxinp.c +1504 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxout.c +401 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxproj.c +791 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxqual.c +2010 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxrpt.c +400 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxtank.c +422 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxtoolkit.c +1164 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxtypes.h +551 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxutils.c +524 -0
- epyt_flow/EPANET/EPANET-MSX/Src/msxutils.h +56 -0
- epyt_flow/EPANET/EPANET-MSX/Src/newton.c +158 -0
- epyt_flow/EPANET/EPANET-MSX/Src/newton.h +34 -0
- epyt_flow/EPANET/EPANET-MSX/Src/rk5.c +287 -0
- epyt_flow/EPANET/EPANET-MSX/Src/rk5.h +39 -0
- epyt_flow/EPANET/EPANET-MSX/Src/ros2.c +293 -0
- epyt_flow/EPANET/EPANET-MSX/Src/ros2.h +35 -0
- epyt_flow/EPANET/EPANET-MSX/Src/smatrix.c +816 -0
- epyt_flow/EPANET/EPANET-MSX/Src/smatrix.h +29 -0
- epyt_flow/EPANET/EPANET-MSX/readme.txt +14 -0
- epyt_flow/EPANET/compile.sh +4 -0
- epyt_flow/VERSION +1 -0
- epyt_flow/__init__.py +24 -0
- epyt_flow/data/__init__.py +0 -0
- epyt_flow/data/benchmarks/__init__.py +11 -0
- epyt_flow/data/benchmarks/batadal.py +257 -0
- epyt_flow/data/benchmarks/batadal_data.py +28 -0
- epyt_flow/data/benchmarks/battledim.py +473 -0
- epyt_flow/data/benchmarks/battledim_data.py +51 -0
- epyt_flow/data/benchmarks/gecco_water_quality.py +267 -0
- epyt_flow/data/benchmarks/leakdb.py +592 -0
- epyt_flow/data/benchmarks/leakdb_data.py +18923 -0
- epyt_flow/data/benchmarks/water_usage.py +123 -0
- epyt_flow/data/networks.py +650 -0
- epyt_flow/gym/__init__.py +4 -0
- epyt_flow/gym/control_gyms.py +47 -0
- epyt_flow/gym/scenario_control_env.py +101 -0
- epyt_flow/metrics.py +404 -0
- epyt_flow/models/__init__.py +2 -0
- epyt_flow/models/event_detector.py +31 -0
- epyt_flow/models/sensor_interpolation_detector.py +118 -0
- epyt_flow/rest_api/__init__.py +4 -0
- epyt_flow/rest_api/base_handler.py +70 -0
- epyt_flow/rest_api/res_manager.py +95 -0
- epyt_flow/rest_api/scada_data_handler.py +476 -0
- epyt_flow/rest_api/scenario_handler.py +352 -0
- epyt_flow/rest_api/server.py +106 -0
- epyt_flow/serialization.py +438 -0
- epyt_flow/simulation/__init__.py +5 -0
- epyt_flow/simulation/events/__init__.py +6 -0
- epyt_flow/simulation/events/actuator_events.py +259 -0
- epyt_flow/simulation/events/event.py +81 -0
- epyt_flow/simulation/events/leakages.py +404 -0
- epyt_flow/simulation/events/sensor_faults.py +267 -0
- epyt_flow/simulation/events/sensor_reading_attack.py +185 -0
- epyt_flow/simulation/events/sensor_reading_event.py +170 -0
- epyt_flow/simulation/events/system_event.py +88 -0
- epyt_flow/simulation/parallel_simulation.py +147 -0
- epyt_flow/simulation/scada/__init__.py +3 -0
- epyt_flow/simulation/scada/advanced_control.py +134 -0
- epyt_flow/simulation/scada/scada_data.py +1589 -0
- epyt_flow/simulation/scada/scada_data_export.py +255 -0
- epyt_flow/simulation/scenario_config.py +608 -0
- epyt_flow/simulation/scenario_simulator.py +1897 -0
- epyt_flow/simulation/scenario_visualizer.py +61 -0
- epyt_flow/simulation/sensor_config.py +1289 -0
- epyt_flow/topology.py +290 -0
- epyt_flow/uncertainty/__init__.py +3 -0
- epyt_flow/uncertainty/model_uncertainty.py +302 -0
- epyt_flow/uncertainty/sensor_noise.py +73 -0
- epyt_flow/uncertainty/uncertainties.py +555 -0
- epyt_flow/uncertainty/utils.py +206 -0
- epyt_flow/utils.py +306 -0
- epyt_flow-0.1.0.dist-info/LICENSE +21 -0
- epyt_flow-0.1.0.dist-info/METADATA +139 -0
- epyt_flow-0.1.0.dist-info/RECORD +131 -0
- epyt_flow-0.1.0.dist-info/WHEEL +5 -0
- epyt_flow-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1359 @@
|
|
|
1
|
+
/*
|
|
2
|
+
******************************************************************************
|
|
3
|
+
Project: OWA EPANET
|
|
4
|
+
Version: 2.2
|
|
5
|
+
Module: project.c
|
|
6
|
+
Description: project data management routines
|
|
7
|
+
Authors: see AUTHORS
|
|
8
|
+
Copyright: see AUTHORS
|
|
9
|
+
License: see LICENSE
|
|
10
|
+
Last Updated: 11/15/2019
|
|
11
|
+
******************************************************************************
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
#include <stdio.h>
|
|
16
|
+
#include <string.h>
|
|
17
|
+
#include <math.h>
|
|
18
|
+
|
|
19
|
+
//*** For the Windows SDK _tempnam function ***//
|
|
20
|
+
#ifdef _WIN32
|
|
21
|
+
#include <windows.h>
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
#include "types.h"
|
|
25
|
+
#include "funcs.h"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
int openfiles(Project *pr, const char *f1, const char *f2, const char *f3)
|
|
29
|
+
/*----------------------------------------------------------------
|
|
30
|
+
** Input: f1 = pointer to name of input file
|
|
31
|
+
** f2 = pointer to name of report file
|
|
32
|
+
** f3 = pointer to name of binary output file
|
|
33
|
+
** Output: none
|
|
34
|
+
** Returns: error code
|
|
35
|
+
** Purpose: opens input & report files
|
|
36
|
+
**----------------------------------------------------------------
|
|
37
|
+
*/
|
|
38
|
+
{
|
|
39
|
+
// Initialize file pointers to NULL
|
|
40
|
+
pr->parser.InFile = NULL;
|
|
41
|
+
pr->report.RptFile = NULL;
|
|
42
|
+
pr->outfile.OutFile = NULL;
|
|
43
|
+
pr->outfile.HydFile = NULL;
|
|
44
|
+
pr->outfile.TmpOutFile = NULL;
|
|
45
|
+
|
|
46
|
+
// Save file names
|
|
47
|
+
strncpy(pr->parser.InpFname, f1, MAXFNAME);
|
|
48
|
+
strncpy(pr->report.Rpt1Fname, f2, MAXFNAME);
|
|
49
|
+
strncpy(pr->outfile.OutFname, f3, MAXFNAME);
|
|
50
|
+
if (strlen(f3) > 0) pr->outfile.Outflag = SAVE;
|
|
51
|
+
else
|
|
52
|
+
{
|
|
53
|
+
pr->outfile.Outflag = SCRATCH;
|
|
54
|
+
strcpy(pr->outfile.OutFname, pr->TmpOutFname);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Check that file names are not identical
|
|
58
|
+
if (strlen(f1) > 0 && (strcomp(f1, f2) || strcomp(f1, f3))) return 301;
|
|
59
|
+
if (strlen(f3) > 0 && strcomp(f2, f3)) return 301;
|
|
60
|
+
|
|
61
|
+
// Attempt to open input and report files
|
|
62
|
+
if (strlen(f1) > 0)
|
|
63
|
+
{
|
|
64
|
+
if ((pr->parser.InFile = fopen(f1, "rt")) == NULL) return 302;
|
|
65
|
+
}
|
|
66
|
+
if (strlen(f2) == 0) pr->report.RptFile = stdout;
|
|
67
|
+
else
|
|
68
|
+
{
|
|
69
|
+
pr->report.RptFile = fopen(f2, "wt");
|
|
70
|
+
if (pr->report.RptFile == NULL) return 303;
|
|
71
|
+
}
|
|
72
|
+
writelogo(pr);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
int openhydfile(Project *pr)
|
|
77
|
+
/*----------------------------------------------------------------
|
|
78
|
+
** Input: none
|
|
79
|
+
** Output: none
|
|
80
|
+
** Returns: error code
|
|
81
|
+
** Purpose: opens file that saves hydraulics solution
|
|
82
|
+
**----------------------------------------------------------------
|
|
83
|
+
*/
|
|
84
|
+
{
|
|
85
|
+
const int Nnodes = pr->network.Nnodes;
|
|
86
|
+
const int Ntanks = pr->network.Ntanks;
|
|
87
|
+
const int Nlinks = pr->network.Nlinks;
|
|
88
|
+
const int Nvalves = pr->network.Nvalves;
|
|
89
|
+
const int Npumps = pr->network.Npumps;
|
|
90
|
+
|
|
91
|
+
INT4 nsize[6]; // Temporary array
|
|
92
|
+
INT4 magic;
|
|
93
|
+
INT4 version;
|
|
94
|
+
int errcode = 0;
|
|
95
|
+
|
|
96
|
+
// If HydFile currently open, then close it if its not a scratch file
|
|
97
|
+
if (pr->outfile.HydFile != NULL)
|
|
98
|
+
{
|
|
99
|
+
if (pr->outfile.Hydflag == SCRATCH) return 0;
|
|
100
|
+
fclose(pr->outfile.HydFile);
|
|
101
|
+
pr->outfile.HydFile = NULL;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Use Hydflag to determine the type of hydraulics file to use.
|
|
105
|
+
// Write error message if the file cannot be opened.
|
|
106
|
+
pr->outfile.HydFile = NULL;
|
|
107
|
+
switch (pr->outfile.Hydflag)
|
|
108
|
+
{
|
|
109
|
+
case SCRATCH:
|
|
110
|
+
strcpy(pr->outfile.HydFname, pr->TmpHydFname);
|
|
111
|
+
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "w+b");
|
|
112
|
+
break;
|
|
113
|
+
case SAVE:
|
|
114
|
+
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "w+b");
|
|
115
|
+
break;
|
|
116
|
+
case USE:
|
|
117
|
+
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "rb");
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
if (pr->outfile.HydFile == NULL) return 305;
|
|
121
|
+
|
|
122
|
+
// If a previous hydraulics solution is not being used, then
|
|
123
|
+
// save the current network size parameters to the file.
|
|
124
|
+
if (pr->outfile.Hydflag != USE)
|
|
125
|
+
{
|
|
126
|
+
magic = MAGICNUMBER;
|
|
127
|
+
version = ENGINE_VERSION;
|
|
128
|
+
nsize[0] = Nnodes;
|
|
129
|
+
nsize[1] = Nlinks;
|
|
130
|
+
nsize[2] = Ntanks;
|
|
131
|
+
nsize[3] = Npumps;
|
|
132
|
+
nsize[4] = Nvalves;
|
|
133
|
+
nsize[5] = (int)pr->times.Dur;
|
|
134
|
+
fwrite(&magic, sizeof(INT4), 1, pr->outfile.HydFile);
|
|
135
|
+
fwrite(&version, sizeof(INT4), 1, pr->outfile.HydFile);
|
|
136
|
+
fwrite(nsize, sizeof(INT4), 6, pr->outfile.HydFile);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// If a previous hydraulics solution is being used, then
|
|
140
|
+
// make sure its network size parameters match those of
|
|
141
|
+
// the current network
|
|
142
|
+
if (pr->outfile.Hydflag == USE)
|
|
143
|
+
{
|
|
144
|
+
fread(&magic, sizeof(INT4), 1, pr->outfile.HydFile);
|
|
145
|
+
if (magic != MAGICNUMBER) return 306;
|
|
146
|
+
fread(&version, sizeof(INT4), 1, pr->outfile.HydFile);
|
|
147
|
+
if (version != ENGINE_VERSION) return 306;
|
|
148
|
+
if (fread(nsize, sizeof(INT4), 6, pr->outfile.HydFile) < 6) return 306;
|
|
149
|
+
if (nsize[0] != Nnodes || nsize[1] != Nlinks || nsize[2] != Ntanks ||
|
|
150
|
+
nsize[3] != Npumps || nsize[4] != Nvalves ||
|
|
151
|
+
nsize[5] != pr->times.Dur
|
|
152
|
+
) return 306;
|
|
153
|
+
pr->outfile.SaveHflag = TRUE;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Save current position in hydraulics file
|
|
157
|
+
// where storage of hydraulic results begins
|
|
158
|
+
pr->outfile.HydOffset = ftell(pr->outfile.HydFile);
|
|
159
|
+
return errcode;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
int openoutfile(Project *pr)
|
|
163
|
+
/*----------------------------------------------------------------
|
|
164
|
+
** Input: none
|
|
165
|
+
** Output: none
|
|
166
|
+
** Returns: error code
|
|
167
|
+
** Purpose: opens binary output file.
|
|
168
|
+
**----------------------------------------------------------------
|
|
169
|
+
*/
|
|
170
|
+
{
|
|
171
|
+
int errcode = 0;
|
|
172
|
+
|
|
173
|
+
// Close output file if already opened
|
|
174
|
+
closeoutfile(pr);
|
|
175
|
+
|
|
176
|
+
// Try to open binary output file
|
|
177
|
+
pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b");
|
|
178
|
+
if (pr->outfile.OutFile == NULL) return 304;
|
|
179
|
+
|
|
180
|
+
// Save basic network data & energy usage results
|
|
181
|
+
ERRCODE(savenetdata(pr));
|
|
182
|
+
pr->outfile.OutOffset1 = ftell(pr->outfile.OutFile);
|
|
183
|
+
ERRCODE(saveenergy(pr));
|
|
184
|
+
pr->outfile.OutOffset2 = ftell(pr->outfile.OutFile);
|
|
185
|
+
|
|
186
|
+
// Open temporary file if computing time series statistic
|
|
187
|
+
if (!errcode)
|
|
188
|
+
{
|
|
189
|
+
if (pr->report.Tstatflag != SERIES)
|
|
190
|
+
{
|
|
191
|
+
pr->outfile.TmpOutFile = fopen(pr->TmpStatFname, "w+b");
|
|
192
|
+
if (pr->outfile.TmpOutFile == NULL) errcode = 304;
|
|
193
|
+
}
|
|
194
|
+
else pr->outfile.TmpOutFile = pr->outfile.OutFile;
|
|
195
|
+
}
|
|
196
|
+
return errcode;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
void closeoutfile(Project *pr)
|
|
200
|
+
/*----------------------------------------------------------------
|
|
201
|
+
** Input: none
|
|
202
|
+
** Output: none
|
|
203
|
+
** Purpose: closes binary output file.
|
|
204
|
+
**----------------------------------------------------------------
|
|
205
|
+
*/
|
|
206
|
+
{
|
|
207
|
+
if (pr->outfile.TmpOutFile != pr->outfile.OutFile)
|
|
208
|
+
{
|
|
209
|
+
if (pr->outfile.TmpOutFile != NULL)
|
|
210
|
+
{
|
|
211
|
+
fclose(pr->outfile.TmpOutFile);
|
|
212
|
+
pr->outfile.TmpOutFile = NULL;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (pr->outfile.OutFile != NULL)
|
|
216
|
+
{
|
|
217
|
+
if (pr->outfile.OutFile == pr->outfile.TmpOutFile)
|
|
218
|
+
{
|
|
219
|
+
pr->outfile.TmpOutFile = NULL;
|
|
220
|
+
}
|
|
221
|
+
fclose(pr->outfile.OutFile);
|
|
222
|
+
pr->outfile.OutFile = NULL;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void initpointers(Project *pr)
|
|
227
|
+
/*----------------------------------------------------------------
|
|
228
|
+
** Input: none
|
|
229
|
+
** Output: none
|
|
230
|
+
** Purpose: initializes data array pointers to NULL
|
|
231
|
+
**----------------------------------------------------------------
|
|
232
|
+
*/
|
|
233
|
+
{
|
|
234
|
+
Network* nw = &pr->network;
|
|
235
|
+
nw->Nnodes = 0;
|
|
236
|
+
nw->Ntanks = 0;
|
|
237
|
+
nw->Njuncs = 0;
|
|
238
|
+
nw->Nlinks = 0;
|
|
239
|
+
nw->Npipes = 0;
|
|
240
|
+
nw->Npumps = 0;
|
|
241
|
+
nw->Nvalves = 0;
|
|
242
|
+
nw->Ncontrols = 0;
|
|
243
|
+
nw->Nrules = 0;
|
|
244
|
+
nw->Npats = 0;
|
|
245
|
+
nw->Ncurves = 0;
|
|
246
|
+
|
|
247
|
+
pr->hydraul.NodeDemand = NULL;
|
|
248
|
+
pr->hydraul.NodeHead = NULL;
|
|
249
|
+
pr->hydraul.LinkFlow = NULL;
|
|
250
|
+
pr->hydraul.LinkStatus = NULL;
|
|
251
|
+
pr->hydraul.LinkSetting = NULL;
|
|
252
|
+
pr->hydraul.OldStatus = NULL;
|
|
253
|
+
pr->hydraul.P = NULL;
|
|
254
|
+
pr->hydraul.Y = NULL;
|
|
255
|
+
pr->hydraul.Xflow = NULL;
|
|
256
|
+
|
|
257
|
+
pr->quality.NodeQual = NULL;
|
|
258
|
+
pr->quality.PipeRateCoeff = NULL;
|
|
259
|
+
|
|
260
|
+
pr->network.Node = NULL;
|
|
261
|
+
pr->network.Link = NULL;
|
|
262
|
+
pr->network.Tank = NULL;
|
|
263
|
+
pr->network.Pump = NULL;
|
|
264
|
+
pr->network.Valve = NULL;
|
|
265
|
+
pr->network.Pattern = NULL;
|
|
266
|
+
pr->network.Curve = NULL;
|
|
267
|
+
pr->network.Control = NULL;
|
|
268
|
+
pr->network.Adjlist = NULL;
|
|
269
|
+
pr->network.NodeHashTable = NULL;
|
|
270
|
+
pr->network.LinkHashTable = NULL;
|
|
271
|
+
|
|
272
|
+
pr->hydraul.smatrix.Aii = NULL;
|
|
273
|
+
pr->hydraul.smatrix.Aij = NULL;
|
|
274
|
+
pr->hydraul.smatrix.F = NULL;
|
|
275
|
+
pr->hydraul.smatrix.Order = NULL;
|
|
276
|
+
pr->hydraul.smatrix.Row = NULL;
|
|
277
|
+
pr->hydraul.smatrix.Ndx = NULL;
|
|
278
|
+
pr->hydraul.smatrix.XLNZ = NULL;
|
|
279
|
+
pr->hydraul.smatrix.NZSUB = NULL;
|
|
280
|
+
pr->hydraul.smatrix.LNZ = NULL;
|
|
281
|
+
|
|
282
|
+
initrules(pr);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
int allocdata(Project *pr)
|
|
286
|
+
/*----------------------------------------------------------------
|
|
287
|
+
** Input: none
|
|
288
|
+
** Output: none
|
|
289
|
+
** Returns: error code
|
|
290
|
+
** Purpose: allocates memory for network data structures
|
|
291
|
+
**----------------------------------------------------------------
|
|
292
|
+
*/
|
|
293
|
+
{
|
|
294
|
+
int n;
|
|
295
|
+
int errcode = 0;
|
|
296
|
+
|
|
297
|
+
// Allocate node & link ID hash tables
|
|
298
|
+
pr->network.NodeHashTable = hashtable_create();
|
|
299
|
+
pr->network.LinkHashTable = hashtable_create();
|
|
300
|
+
ERRCODE(MEMCHECK(pr->network.NodeHashTable));
|
|
301
|
+
ERRCODE(MEMCHECK(pr->network.LinkHashTable));
|
|
302
|
+
|
|
303
|
+
// Allocate memory for network nodes
|
|
304
|
+
//*************************************************************
|
|
305
|
+
// NOTE: Because network components of a given type are indexed
|
|
306
|
+
// starting from 1, their arrays must be sized 1
|
|
307
|
+
// element larger than the number of components.
|
|
308
|
+
//*************************************************************
|
|
309
|
+
if (!errcode)
|
|
310
|
+
{
|
|
311
|
+
n = pr->parser.MaxNodes + 1;
|
|
312
|
+
pr->network.Node = (Snode *)calloc(n, sizeof(Snode));
|
|
313
|
+
pr->hydraul.NodeDemand = (double *)calloc(n, sizeof(double));
|
|
314
|
+
pr->hydraul.NodeHead = (double *)calloc(n, sizeof(double));
|
|
315
|
+
pr->quality.NodeQual = (double *)calloc(n, sizeof(double));
|
|
316
|
+
ERRCODE(MEMCHECK(pr->network.Node));
|
|
317
|
+
ERRCODE(MEMCHECK(pr->hydraul.NodeDemand));
|
|
318
|
+
ERRCODE(MEMCHECK(pr->hydraul.NodeHead));
|
|
319
|
+
ERRCODE(MEMCHECK(pr->quality.NodeQual));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Allocate memory for network links
|
|
323
|
+
if (!errcode)
|
|
324
|
+
{
|
|
325
|
+
n = pr->parser.MaxLinks + 1;
|
|
326
|
+
pr->network.Link = (Slink *)calloc(n, sizeof(Slink));
|
|
327
|
+
pr->hydraul.LinkFlow = (double *)calloc(n, sizeof(double));
|
|
328
|
+
pr->hydraul.LinkSetting = (double *)calloc(n, sizeof(double));
|
|
329
|
+
pr->hydraul.LinkStatus = (StatusType *)calloc(n, sizeof(StatusType));
|
|
330
|
+
ERRCODE(MEMCHECK(pr->network.Link));
|
|
331
|
+
ERRCODE(MEMCHECK(pr->hydraul.LinkFlow));
|
|
332
|
+
ERRCODE(MEMCHECK(pr->hydraul.LinkSetting));
|
|
333
|
+
ERRCODE(MEMCHECK(pr->hydraul.LinkStatus));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Allocate memory for tanks, sources, pumps, valves, and controls
|
|
337
|
+
// (memory for Patterns and Curves arrays expanded as each is added)
|
|
338
|
+
if (!errcode)
|
|
339
|
+
{
|
|
340
|
+
pr->network.Tank =
|
|
341
|
+
(Stank *)calloc(pr->parser.MaxTanks + 1, sizeof(Stank));
|
|
342
|
+
pr->network.Pump =
|
|
343
|
+
(Spump *)calloc(pr->parser.MaxPumps + 1, sizeof(Spump));
|
|
344
|
+
pr->network.Valve =
|
|
345
|
+
(Svalve *)calloc(pr->parser.MaxValves + 1, sizeof(Svalve));
|
|
346
|
+
pr->network.Control =
|
|
347
|
+
(Scontrol *)calloc(pr->parser.MaxControls + 1, sizeof(Scontrol));
|
|
348
|
+
ERRCODE(MEMCHECK(pr->network.Tank));
|
|
349
|
+
ERRCODE(MEMCHECK(pr->network.Pump));
|
|
350
|
+
ERRCODE(MEMCHECK(pr->network.Valve));
|
|
351
|
+
ERRCODE(MEMCHECK(pr->network.Control));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Initialize pointers used in nodes and links
|
|
355
|
+
if (!errcode)
|
|
356
|
+
{
|
|
357
|
+
for (n = 0; n <= pr->parser.MaxNodes; n++)
|
|
358
|
+
{
|
|
359
|
+
pr->network.Node[n].D = NULL; // node demand
|
|
360
|
+
pr->network.Node[n].S = NULL; // node source
|
|
361
|
+
pr->network.Node[n].Comment = NULL;
|
|
362
|
+
}
|
|
363
|
+
for (n = 0; n <= pr->parser.MaxLinks; n++)
|
|
364
|
+
{
|
|
365
|
+
pr->network.Link[n].Vertices = NULL;
|
|
366
|
+
pr->network.Link[n].Comment = NULL;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Allocate memory for rule base (see RULES.C)
|
|
371
|
+
if (!errcode) errcode = allocrules(pr);
|
|
372
|
+
return errcode;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
void freedata(Project *pr)
|
|
376
|
+
/*----------------------------------------------------------------
|
|
377
|
+
** Input: none
|
|
378
|
+
** Output: none
|
|
379
|
+
** Purpose: frees memory allocated for network data structures.
|
|
380
|
+
**----------------------------------------------------------------
|
|
381
|
+
*/
|
|
382
|
+
{
|
|
383
|
+
int j;
|
|
384
|
+
|
|
385
|
+
// Free memory for computed results
|
|
386
|
+
free(pr->hydraul.NodeDemand);
|
|
387
|
+
free(pr->hydraul.NodeHead);
|
|
388
|
+
free(pr->hydraul.LinkFlow);
|
|
389
|
+
free(pr->hydraul.LinkSetting);
|
|
390
|
+
free(pr->hydraul.LinkStatus);
|
|
391
|
+
free(pr->quality.NodeQual);
|
|
392
|
+
|
|
393
|
+
// Free memory used for nodal adjacency lists
|
|
394
|
+
freeadjlists(&pr->network);
|
|
395
|
+
|
|
396
|
+
// Free memory for node data
|
|
397
|
+
if (pr->network.Node != NULL)
|
|
398
|
+
{
|
|
399
|
+
for (j = 1; j <= pr->network.Nnodes; j++)
|
|
400
|
+
{
|
|
401
|
+
// Free memory used for demands and WQ source data
|
|
402
|
+
freedemands(&(pr->network.Node[j]));
|
|
403
|
+
free(pr->network.Node[j].S);
|
|
404
|
+
free(pr->network.Node[j].Comment);
|
|
405
|
+
}
|
|
406
|
+
free(pr->network.Node);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Free memory for link data
|
|
410
|
+
if (pr->network.Link != NULL)
|
|
411
|
+
{
|
|
412
|
+
for (j = 1; j <= pr->network.Nlinks; j++)
|
|
413
|
+
{
|
|
414
|
+
freelinkvertices(&pr->network.Link[j]);
|
|
415
|
+
free(pr->network.Link[j].Comment);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
free(pr->network.Link);
|
|
419
|
+
|
|
420
|
+
// Free memory for other network objects
|
|
421
|
+
free(pr->network.Tank);
|
|
422
|
+
free(pr->network.Pump);
|
|
423
|
+
free(pr->network.Valve);
|
|
424
|
+
free(pr->network.Control);
|
|
425
|
+
|
|
426
|
+
// Free memory for time patterns
|
|
427
|
+
if (pr->network.Pattern != NULL)
|
|
428
|
+
{
|
|
429
|
+
for (j = 0; j <= pr->network.Npats; j++)
|
|
430
|
+
{
|
|
431
|
+
free(pr->network.Pattern[j].F);
|
|
432
|
+
free(pr->network.Pattern[j].Comment);
|
|
433
|
+
}
|
|
434
|
+
free(pr->network.Pattern);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Free memory for curves
|
|
438
|
+
if (pr->network.Curve != NULL)
|
|
439
|
+
{
|
|
440
|
+
// There is no Curve[0]
|
|
441
|
+
for (j = 1; j <= pr->network.Ncurves; j++)
|
|
442
|
+
{
|
|
443
|
+
free(pr->network.Curve[j].X);
|
|
444
|
+
free(pr->network.Curve[j].Y);
|
|
445
|
+
free(pr->network.Curve[j].Comment);
|
|
446
|
+
}
|
|
447
|
+
free(pr->network.Curve);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Free memory for rule base (see RULES.C)
|
|
451
|
+
freerules(pr);
|
|
452
|
+
|
|
453
|
+
// Free hash table memory
|
|
454
|
+
if (pr->network.NodeHashTable != NULL)
|
|
455
|
+
{
|
|
456
|
+
hashtable_free(pr->network.NodeHashTable);
|
|
457
|
+
}
|
|
458
|
+
if (pr->network.LinkHashTable != NULL)
|
|
459
|
+
{
|
|
460
|
+
hashtable_free(pr->network.LinkHashTable);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
Pdemand finddemand(Pdemand d, int index)
|
|
465
|
+
/*----------------------------------------------------------------
|
|
466
|
+
** Input: d = pointer to start of a list of demands
|
|
467
|
+
** index = the position of the demand to retrieve
|
|
468
|
+
** Output: none
|
|
469
|
+
** Returns: the demand at the requested position
|
|
470
|
+
** Purpose: finds the demand at a given position in a demand list
|
|
471
|
+
**----------------------------------------------------------------
|
|
472
|
+
*/
|
|
473
|
+
{
|
|
474
|
+
int n = 1;
|
|
475
|
+
if (index <= 0)return NULL;
|
|
476
|
+
while (d)
|
|
477
|
+
{
|
|
478
|
+
if (n == index) break;
|
|
479
|
+
n++;
|
|
480
|
+
d = d->next;
|
|
481
|
+
}
|
|
482
|
+
return d;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
int adddemand(Snode *node, double dbase, int dpat, char *dname)
|
|
486
|
+
/*----------------------------------------------------------------
|
|
487
|
+
** Input: node = a network junction node
|
|
488
|
+
** dbase = base demand value
|
|
489
|
+
** dpat = demand pattern index
|
|
490
|
+
** dname = name of demand category
|
|
491
|
+
** Output: returns TRUE if successful, FALSE if not
|
|
492
|
+
** Purpose: adds a new demand category to a node.
|
|
493
|
+
**----------------------------------------------------------------
|
|
494
|
+
*/
|
|
495
|
+
{
|
|
496
|
+
Pdemand demand, lastdemand;
|
|
497
|
+
|
|
498
|
+
// Create a new demand struct
|
|
499
|
+
demand = (struct Sdemand *)malloc(sizeof(struct Sdemand));
|
|
500
|
+
if (demand == NULL) return FALSE;
|
|
501
|
+
|
|
502
|
+
// Assign it the designated properties
|
|
503
|
+
demand->Base = dbase;
|
|
504
|
+
demand->Pat = dpat;
|
|
505
|
+
demand->Name = NULL;
|
|
506
|
+
if (dname && strlen(dname) > 0) xstrcpy(&demand->Name, dname, MAXID);
|
|
507
|
+
demand->next = NULL;
|
|
508
|
+
|
|
509
|
+
// If node has no demands make this its first demand category
|
|
510
|
+
if (node->D == NULL) node->D = demand;
|
|
511
|
+
|
|
512
|
+
// Otherwise append this demand to the end of the node's demands list
|
|
513
|
+
else
|
|
514
|
+
{
|
|
515
|
+
lastdemand = node->D;
|
|
516
|
+
while (lastdemand->next) lastdemand = lastdemand->next;
|
|
517
|
+
lastdemand->next = demand;
|
|
518
|
+
}
|
|
519
|
+
return TRUE;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
void freedemands(Snode *node)
|
|
523
|
+
/*----------------------------------------------------------------
|
|
524
|
+
** Input: node = a network junction node
|
|
525
|
+
** Output: node
|
|
526
|
+
** Purpose: frees the memory used for a node's list of demands.
|
|
527
|
+
**----------------------------------------------------------------
|
|
528
|
+
*/
|
|
529
|
+
{
|
|
530
|
+
Pdemand nextdemand;
|
|
531
|
+
Pdemand demand = node->D;
|
|
532
|
+
while (demand != NULL)
|
|
533
|
+
{
|
|
534
|
+
nextdemand = demand->next;
|
|
535
|
+
free(demand->Name);
|
|
536
|
+
free(demand);
|
|
537
|
+
demand = nextdemand;
|
|
538
|
+
}
|
|
539
|
+
node->D = NULL;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
int addlinkvertex(Slink *link, double x, double y)
|
|
543
|
+
/*----------------------------------------------------------------
|
|
544
|
+
** Input: link = pointer to a network link
|
|
545
|
+
** x = x-coordinate of a new vertex
|
|
546
|
+
** y = y-coordiante of a new vertex
|
|
547
|
+
** Returns: an error code
|
|
548
|
+
** Purpose: adds to a link's collection of vertex points.
|
|
549
|
+
**----------------------------------------------------------------
|
|
550
|
+
*/
|
|
551
|
+
{
|
|
552
|
+
static int CHUNKSIZE = 5;
|
|
553
|
+
int n;
|
|
554
|
+
Pvertices vertices;
|
|
555
|
+
if (link->Vertices == NULL)
|
|
556
|
+
{
|
|
557
|
+
vertices = (struct Svertices *) malloc(sizeof(struct Svertices));
|
|
558
|
+
if (vertices == NULL) return 101;
|
|
559
|
+
vertices->Npts = 0;
|
|
560
|
+
vertices->Capacity = CHUNKSIZE;
|
|
561
|
+
vertices->X = (double *) calloc(vertices->Capacity, sizeof(double));
|
|
562
|
+
vertices->Y = (double *) calloc(vertices->Capacity, sizeof(double));
|
|
563
|
+
link->Vertices = vertices;
|
|
564
|
+
}
|
|
565
|
+
vertices = link->Vertices;
|
|
566
|
+
if (vertices->Npts >= vertices->Capacity)
|
|
567
|
+
{
|
|
568
|
+
vertices->Capacity += CHUNKSIZE;
|
|
569
|
+
vertices->X = realloc(vertices->X, vertices->Capacity * sizeof(double));
|
|
570
|
+
vertices->Y = realloc(vertices->Y, vertices->Capacity * sizeof(double));
|
|
571
|
+
}
|
|
572
|
+
if (vertices->X == NULL || vertices->Y == NULL) return 101;
|
|
573
|
+
n = vertices->Npts;
|
|
574
|
+
vertices->X[n] = x;
|
|
575
|
+
vertices->Y[n] = y;
|
|
576
|
+
vertices->Npts++;
|
|
577
|
+
return 0;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
void freelinkvertices(Slink *link)
|
|
581
|
+
/*----------------------------------------------------------------
|
|
582
|
+
** Input: vertices = list of link vertex points
|
|
583
|
+
** Output: none
|
|
584
|
+
** Purpose: frees the memory used for a link's list of vertices.
|
|
585
|
+
**----------------------------------------------------------------
|
|
586
|
+
*/
|
|
587
|
+
{
|
|
588
|
+
if (link->Vertices)
|
|
589
|
+
{
|
|
590
|
+
free(link->Vertices->X);
|
|
591
|
+
free(link->Vertices->Y);
|
|
592
|
+
free(link->Vertices);
|
|
593
|
+
link->Vertices = NULL;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
int buildadjlists(Network *net)
|
|
598
|
+
/*
|
|
599
|
+
**--------------------------------------------------------------
|
|
600
|
+
** Input: none
|
|
601
|
+
** Output: returns error code
|
|
602
|
+
** Purpose: builds linked list of links adjacent to each node
|
|
603
|
+
**--------------------------------------------------------------
|
|
604
|
+
*/
|
|
605
|
+
{
|
|
606
|
+
int i, j, k;
|
|
607
|
+
int errcode = 0;
|
|
608
|
+
Padjlist alink;
|
|
609
|
+
|
|
610
|
+
// Create an array of adjacency lists
|
|
611
|
+
freeadjlists(net);
|
|
612
|
+
net->Adjlist = (Padjlist *)calloc(net->Nnodes + 1, sizeof(Padjlist));
|
|
613
|
+
if (net->Adjlist == NULL) return 101;
|
|
614
|
+
for (i = 0; i <= net->Nnodes; i++) net->Adjlist[i] = NULL;
|
|
615
|
+
|
|
616
|
+
// For each link, update adjacency lists of its end nodes
|
|
617
|
+
for (k = 1; k <= net->Nlinks; k++)
|
|
618
|
+
{
|
|
619
|
+
i = net->Link[k].N1;
|
|
620
|
+
j = net->Link[k].N2;
|
|
621
|
+
|
|
622
|
+
// Include link in start node i's list
|
|
623
|
+
alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
|
|
624
|
+
if (alink == NULL)
|
|
625
|
+
{
|
|
626
|
+
errcode = 101;
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
alink->node = j;
|
|
630
|
+
alink->link = k;
|
|
631
|
+
alink->next = net->Adjlist[i];
|
|
632
|
+
net->Adjlist[i] = alink;
|
|
633
|
+
|
|
634
|
+
// Include link in end node j's list
|
|
635
|
+
alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
|
|
636
|
+
if (alink == NULL)
|
|
637
|
+
{
|
|
638
|
+
errcode = 101;
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
alink->node = i;
|
|
642
|
+
alink->link = k;
|
|
643
|
+
alink->next = net->Adjlist[j];
|
|
644
|
+
net->Adjlist[j] = alink;
|
|
645
|
+
}
|
|
646
|
+
if (errcode) freeadjlists(net);
|
|
647
|
+
return errcode;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
void freeadjlists(Network *net)
|
|
652
|
+
/*
|
|
653
|
+
**--------------------------------------------------------------
|
|
654
|
+
** Input: none
|
|
655
|
+
** Output: none
|
|
656
|
+
** Purpose: frees memory used for nodal adjacency lists
|
|
657
|
+
**--------------------------------------------------------------
|
|
658
|
+
*/
|
|
659
|
+
{
|
|
660
|
+
int i;
|
|
661
|
+
Padjlist alink;
|
|
662
|
+
|
|
663
|
+
if (net->Adjlist == NULL) return;
|
|
664
|
+
for (i = 0; i <= net->Nnodes; i++)
|
|
665
|
+
{
|
|
666
|
+
for (alink = net->Adjlist[i]; alink != NULL; alink = net->Adjlist[i])
|
|
667
|
+
{
|
|
668
|
+
net->Adjlist[i] = alink->next;
|
|
669
|
+
free(alink);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
FREE(net->Adjlist);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
int incontrols(Project *pr, int objType, int index)
|
|
676
|
+
/*----------------------------------------------------------------
|
|
677
|
+
** Input: objType = type of object (either NODE or LINK)
|
|
678
|
+
** index = the object's index
|
|
679
|
+
** Output: none
|
|
680
|
+
** Returns: 1 if any controls contain the object; 0 if not
|
|
681
|
+
** Purpose: determines if any simple or rule-based controls
|
|
682
|
+
** contain a particular node or link.
|
|
683
|
+
**----------------------------------------------------------------
|
|
684
|
+
*/
|
|
685
|
+
{
|
|
686
|
+
Network *net = &pr->network;
|
|
687
|
+
|
|
688
|
+
int i, ruleObject;
|
|
689
|
+
Spremise *premise;
|
|
690
|
+
Saction *action;
|
|
691
|
+
|
|
692
|
+
// Check simple controls
|
|
693
|
+
for (i = 1; i <= net->Ncontrols; i++)
|
|
694
|
+
{
|
|
695
|
+
if (objType == NODE && net->Control[i].Node == index) return 1;
|
|
696
|
+
if (objType == LINK && net->Control[i].Link == index) return 1;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// Check rule-based controls
|
|
700
|
+
for (i = 1; i <= net->Nrules; i++)
|
|
701
|
+
{
|
|
702
|
+
// Convert objType to a rule object type
|
|
703
|
+
if (objType == NODE) ruleObject = 6;
|
|
704
|
+
else ruleObject = 7;
|
|
705
|
+
|
|
706
|
+
// Check rule's premises
|
|
707
|
+
premise = net->Rule[i].Premises;
|
|
708
|
+
while (premise != NULL)
|
|
709
|
+
{
|
|
710
|
+
if (ruleObject == premise->object && premise->index == index) return 1;
|
|
711
|
+
premise = premise->next;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Rule actions only need to be checked for link objects
|
|
715
|
+
if (objType == LINK)
|
|
716
|
+
{
|
|
717
|
+
// Check rule's THEN actions
|
|
718
|
+
action = net->Rule[i].ThenActions;
|
|
719
|
+
while (action != NULL)
|
|
720
|
+
{
|
|
721
|
+
if (action->link == index) return 1;
|
|
722
|
+
action = action->next;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// Check rule's ELSE actions
|
|
726
|
+
action = net->Rule[i].ElseActions;
|
|
727
|
+
while (action != NULL)
|
|
728
|
+
{
|
|
729
|
+
if (action->link == index) return 1;
|
|
730
|
+
action = action->next;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
return 0;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
int valvecheck(Project *pr, int index, int type, int j1, int j2)
|
|
738
|
+
/*
|
|
739
|
+
**--------------------------------------------------------------
|
|
740
|
+
** Input: index = link index
|
|
741
|
+
** type = valve type
|
|
742
|
+
** j1 = index of upstream node
|
|
743
|
+
** j2 = index of downstream node
|
|
744
|
+
** Output: returns an error code
|
|
745
|
+
** Purpose: checks for illegal connections between valves
|
|
746
|
+
**--------------------------------------------------------------
|
|
747
|
+
*/
|
|
748
|
+
{
|
|
749
|
+
Network *net = &pr->network;
|
|
750
|
+
|
|
751
|
+
int k, vj1, vj2;
|
|
752
|
+
LinkType vtype;
|
|
753
|
+
Slink *link;
|
|
754
|
+
Svalve *valve;
|
|
755
|
+
|
|
756
|
+
if (type == PRV || type == PSV || type == FCV)
|
|
757
|
+
{
|
|
758
|
+
// Can't be connected to a fixed grade node
|
|
759
|
+
if (j1 > net->Njuncs || j2 > net->Njuncs) return 219;
|
|
760
|
+
|
|
761
|
+
// Examine each existing valve
|
|
762
|
+
for (k = 1; k <= net->Nvalves; k++)
|
|
763
|
+
{
|
|
764
|
+
valve = &net->Valve[k];
|
|
765
|
+
if (valve->Link == index) continue;
|
|
766
|
+
link = &net->Link[valve->Link];
|
|
767
|
+
vj1 = link->N1;
|
|
768
|
+
vj2 = link->N2;
|
|
769
|
+
vtype = link->Type;
|
|
770
|
+
|
|
771
|
+
// Cannot have two PRVs sharing downstream nodes or in series
|
|
772
|
+
if (vtype == PRV && type == PRV)
|
|
773
|
+
{
|
|
774
|
+
if (vj2 == j2 || vj2 == j1 || vj1 == j2) return 220;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Cannot have two PSVs sharing upstream nodes or in series
|
|
778
|
+
if (vtype == PSV && type == PSV)
|
|
779
|
+
{
|
|
780
|
+
if (vj1 == j1 || vj1 == j2 || vj2 == j1) return 220;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// Cannot have PSV connected to downstream node of PRV
|
|
784
|
+
if (vtype == PSV && type == PRV && vj1 == j2) return 220;
|
|
785
|
+
if (vtype == PRV && type == PSV && vj2 == j1) return 220;
|
|
786
|
+
|
|
787
|
+
// Cannot have PSV connected to downstream node of FCV
|
|
788
|
+
// nor have PRV connected to upstream node of FCV
|
|
789
|
+
if (vtype == FCV && type == PSV && vj2 == j1) return 220;
|
|
790
|
+
if (vtype == FCV && type == PRV && vj1 == j2) return 220;
|
|
791
|
+
if (vtype == PSV && type == FCV && vj1 == j2) return 220;
|
|
792
|
+
if (vtype == PRV && type == FCV && vj2 == j1) return 220;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
return 0;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
int findnode(Network *network, char *id)
|
|
799
|
+
/*----------------------------------------------------------------
|
|
800
|
+
** Input: id = node ID
|
|
801
|
+
** Output: none
|
|
802
|
+
** Returns: index of node with given ID, or 0 if ID not found
|
|
803
|
+
** Purpose: uses hash table to find index of node with given ID
|
|
804
|
+
**----------------------------------------------------------------
|
|
805
|
+
*/
|
|
806
|
+
{
|
|
807
|
+
return (hashtable_find(network->NodeHashTable, id));
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
int findlink(Network *network, char *id)
|
|
811
|
+
/*----------------------------------------------------------------
|
|
812
|
+
** Input: id = link ID
|
|
813
|
+
** Output: none
|
|
814
|
+
** Returns: index of link with given ID, or 0 if ID not found
|
|
815
|
+
** Purpose: uses hash table to find index of link with given ID
|
|
816
|
+
**----------------------------------------------------------------
|
|
817
|
+
*/
|
|
818
|
+
{
|
|
819
|
+
return (hashtable_find(network->LinkHashTable, id));
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
int findtank(Network *network, int index)
|
|
823
|
+
/*----------------------------------------------------------------
|
|
824
|
+
** Input: index = node index
|
|
825
|
+
** Output: none
|
|
826
|
+
** Returns: index of tank with given node id, or NOTFOUND if tank not found
|
|
827
|
+
** Purpose: for use in the deletenode function
|
|
828
|
+
**----------------------------------------------------------------
|
|
829
|
+
*/
|
|
830
|
+
{
|
|
831
|
+
int i;
|
|
832
|
+
for (i = 1; i <= network->Ntanks; i++)
|
|
833
|
+
{
|
|
834
|
+
if (network->Tank[i].Node == index) return i;
|
|
835
|
+
}
|
|
836
|
+
return NOTFOUND;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
int findpump(Network *network, int index)
|
|
840
|
+
/*----------------------------------------------------------------
|
|
841
|
+
** Input: index = link ID
|
|
842
|
+
** Output: none
|
|
843
|
+
** Returns: index of pump with given link id, or NOTFOUND if pump not found
|
|
844
|
+
** Purpose: for use in the deletelink function
|
|
845
|
+
**----------------------------------------------------------------
|
|
846
|
+
*/
|
|
847
|
+
{
|
|
848
|
+
int i;
|
|
849
|
+
for (i = 1; i <= network->Npumps; i++)
|
|
850
|
+
{
|
|
851
|
+
if (network->Pump[i].Link == index) return i;
|
|
852
|
+
}
|
|
853
|
+
return NOTFOUND;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
int findvalve(Network *network, int index)
|
|
857
|
+
/*----------------------------------------------------------------
|
|
858
|
+
** Input: index = link ID
|
|
859
|
+
** Output: none
|
|
860
|
+
** Returns: index of valve with given link id, or NOTFOUND if valve not found
|
|
861
|
+
** Purpose: for use in the deletelink function
|
|
862
|
+
**----------------------------------------------------------------
|
|
863
|
+
*/
|
|
864
|
+
{
|
|
865
|
+
int i;
|
|
866
|
+
for (i = 1; i <= network->Nvalves; i++)
|
|
867
|
+
{
|
|
868
|
+
if (network->Valve[i].Link == index) return i;
|
|
869
|
+
}
|
|
870
|
+
return NOTFOUND;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
int findpattern(Network *network, char *id)
|
|
874
|
+
/*----------------------------------------------------------------
|
|
875
|
+
** Input: id = time pattern ID
|
|
876
|
+
** Output: none
|
|
877
|
+
** Returns: time pattern index, or -1 if pattern not found
|
|
878
|
+
** Purpose: finds index of time pattern given its ID
|
|
879
|
+
**----------------------------------------------------------------
|
|
880
|
+
*/
|
|
881
|
+
{
|
|
882
|
+
int i;
|
|
883
|
+
|
|
884
|
+
// Don't forget to include the "dummy" pattern 0 in the search
|
|
885
|
+
for (i = 0; i <= network->Npats; i++)
|
|
886
|
+
{
|
|
887
|
+
if (strcmp(id, network->Pattern[i].ID) == 0) return i;
|
|
888
|
+
}
|
|
889
|
+
return -1;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
int findcurve(Network *network, char *id)
|
|
893
|
+
/*----------------------------------------------------------------
|
|
894
|
+
** Input: id = data curve ID
|
|
895
|
+
** Output: none
|
|
896
|
+
** Returns: data curve index, or 0 if curve not found
|
|
897
|
+
** Purpose: finds index of data curve given its ID
|
|
898
|
+
**----------------------------------------------------------------
|
|
899
|
+
*/
|
|
900
|
+
{
|
|
901
|
+
int i;
|
|
902
|
+
for (i = 1; i <= network->Ncurves; i++)
|
|
903
|
+
{
|
|
904
|
+
if (strcmp(id, network->Curve[i].ID) == 0) return i;
|
|
905
|
+
}
|
|
906
|
+
return 0;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
void adjustpattern(int *pat, int index)
|
|
910
|
+
/*----------------------------------------------------------------
|
|
911
|
+
** Local function that modifies a reference to a deleted time pattern
|
|
912
|
+
**----------------------------------------------------------------
|
|
913
|
+
*/
|
|
914
|
+
{
|
|
915
|
+
if (*pat == index) *pat = 0;
|
|
916
|
+
else if (*pat > index) (*pat)--;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
void adjustpatterns(Network *network, int index)
|
|
920
|
+
/*----------------------------------------------------------------
|
|
921
|
+
** Input: index = index of time pattern being deleted
|
|
922
|
+
** Output: none
|
|
923
|
+
** Purpose: modifies references made to a deleted time pattern
|
|
924
|
+
**----------------------------------------------------------------
|
|
925
|
+
*/
|
|
926
|
+
{
|
|
927
|
+
int j;
|
|
928
|
+
Pdemand demand;
|
|
929
|
+
Psource source;
|
|
930
|
+
|
|
931
|
+
// Adjust patterns used by junctions
|
|
932
|
+
for (j = 1; j <= network->Nnodes; j++)
|
|
933
|
+
{
|
|
934
|
+
// Adjust demand patterns
|
|
935
|
+
for (demand = network->Node[j].D; demand != NULL; demand = demand->next)
|
|
936
|
+
{
|
|
937
|
+
adjustpattern(&demand->Pat, index);
|
|
938
|
+
}
|
|
939
|
+
// Adjust WQ source patterns
|
|
940
|
+
source = network->Node[j].S;
|
|
941
|
+
if (source) adjustpattern(&source->Pat, index);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// Adjust patterns used by reservoir tanks
|
|
945
|
+
for (j = 1; j <= network->Ntanks; j++)
|
|
946
|
+
{
|
|
947
|
+
adjustpattern(&network->Tank[j].Pat, index);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// Adjust patterns used by pumps
|
|
951
|
+
for (j = 1; j <= network->Npumps; j++)
|
|
952
|
+
{
|
|
953
|
+
adjustpattern(&network->Pump[j].Upat, index);
|
|
954
|
+
adjustpattern(&network->Pump[j].Epat, index);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
void adjustcurve(int *curve, int index)
|
|
959
|
+
/*----------------------------------------------------------------
|
|
960
|
+
** Local function that modifies a reference to a deleted data curve
|
|
961
|
+
**----------------------------------------------------------------
|
|
962
|
+
*/
|
|
963
|
+
{
|
|
964
|
+
if (*curve == index) *curve = 0;
|
|
965
|
+
else if (*curve > index) (*curve)--;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
void adjustcurves(Network *network, int index)
|
|
969
|
+
/*----------------------------------------------------------------
|
|
970
|
+
** Input: index = index of data curve being deleted
|
|
971
|
+
** Output: none
|
|
972
|
+
** Purpose: modifies references made to a deleted data curve
|
|
973
|
+
**----------------------------------------------------------------
|
|
974
|
+
*/
|
|
975
|
+
{
|
|
976
|
+
int j, k, setting;
|
|
977
|
+
|
|
978
|
+
// Adjust tank volume curves
|
|
979
|
+
for (j = 1; j <= network->Ntanks; j++)
|
|
980
|
+
{
|
|
981
|
+
adjustcurve(&network->Tank[j].Vcurve, index);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// Adjust pump curves
|
|
985
|
+
for (j = 1; j <= network->Npumps; j++)
|
|
986
|
+
{
|
|
987
|
+
adjustcurve(&network->Pump[j].Hcurve, index);
|
|
988
|
+
adjustcurve(&network->Pump[j].Ecurve, index);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// Adjust GPV curves
|
|
992
|
+
for (j = 1; j <= network->Nvalves; j++)
|
|
993
|
+
{
|
|
994
|
+
k = network->Valve[j].Link;
|
|
995
|
+
if (network->Link[k].Type == GPV)
|
|
996
|
+
{
|
|
997
|
+
setting = INT(network->Link[k].Kc);
|
|
998
|
+
adjustcurve(&setting, index);
|
|
999
|
+
network->Link[k].Kc = setting;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
int adjustpumpparams(Project *pr, int curveIndex)
|
|
1005
|
+
/*----------------------------------------------------------------
|
|
1006
|
+
** Input: curveIndex = index of a data curve
|
|
1007
|
+
** Output: returns an error code
|
|
1008
|
+
** Purpose: updates head curve parameters for pumps using a
|
|
1009
|
+
** curve whose data have been modified.
|
|
1010
|
+
**----------------------------------------------------------------
|
|
1011
|
+
*/
|
|
1012
|
+
{
|
|
1013
|
+
Network *network = &pr->network;
|
|
1014
|
+
|
|
1015
|
+
double *Ucf = pr->Ucf;
|
|
1016
|
+
int j, err = 0;
|
|
1017
|
+
Spump *pump;
|
|
1018
|
+
|
|
1019
|
+
// Check each pump
|
|
1020
|
+
for (j = 1; j <= network->Npumps; j++)
|
|
1021
|
+
{
|
|
1022
|
+
// Pump uses curve as head curve
|
|
1023
|
+
pump = &network->Pump[j];
|
|
1024
|
+
if ( curveIndex == pump->Hcurve)
|
|
1025
|
+
{
|
|
1026
|
+
// Update its head curve parameters
|
|
1027
|
+
pump->Ptype = NOCURVE;
|
|
1028
|
+
err = updatepumpparams(pr, curveIndex);
|
|
1029
|
+
if (err > 0) break;
|
|
1030
|
+
|
|
1031
|
+
// Convert parameters to internal units
|
|
1032
|
+
if (pump->Ptype == POWER_FUNC)
|
|
1033
|
+
{
|
|
1034
|
+
pump->H0 /= Ucf[HEAD];
|
|
1035
|
+
pump->R *= (pow(Ucf[FLOW], pump->N) / Ucf[HEAD]);
|
|
1036
|
+
}
|
|
1037
|
+
pump->Q0 /= Ucf[FLOW];
|
|
1038
|
+
pump->Qmax /= Ucf[FLOW];
|
|
1039
|
+
pump->Hmax /= Ucf[HEAD];
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
return err;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
int resizecurve(Scurve *curve, int size)
|
|
1047
|
+
/*----------------------------------------------------------------
|
|
1048
|
+
** Input: curve = a data curve object
|
|
1049
|
+
** size = desired number of curve data points
|
|
1050
|
+
** Output: error code
|
|
1051
|
+
** Purpose: resizes a data curve to a desired size
|
|
1052
|
+
**----------------------------------------------------------------
|
|
1053
|
+
*/
|
|
1054
|
+
{
|
|
1055
|
+
double *x;
|
|
1056
|
+
double *y;
|
|
1057
|
+
|
|
1058
|
+
if (curve->Capacity < size)
|
|
1059
|
+
{
|
|
1060
|
+
x = (double *)realloc(curve->X, size * sizeof(double));
|
|
1061
|
+
if (x == NULL) return 101;
|
|
1062
|
+
y = (double *)realloc(curve->Y, size * sizeof(double));
|
|
1063
|
+
if (y == NULL)
|
|
1064
|
+
{
|
|
1065
|
+
free(x);
|
|
1066
|
+
return 101;
|
|
1067
|
+
}
|
|
1068
|
+
curve->X = x;
|
|
1069
|
+
curve->Y = y;
|
|
1070
|
+
curve->Capacity = size;
|
|
1071
|
+
}
|
|
1072
|
+
return 0;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
int getcomment(Network *network, int object, int index, char *comment)
|
|
1076
|
+
//----------------------------------------------------------------
|
|
1077
|
+
// Input: object = a type of network object
|
|
1078
|
+
// index = index of the specified object
|
|
1079
|
+
// comment = the object's comment string
|
|
1080
|
+
// Output: error code
|
|
1081
|
+
// Purpose: gets the comment string assigned to an object.
|
|
1082
|
+
//----------------------------------------------------------------
|
|
1083
|
+
{
|
|
1084
|
+
char *currentcomment;
|
|
1085
|
+
|
|
1086
|
+
// Get pointer to specified object's comment
|
|
1087
|
+
switch (object)
|
|
1088
|
+
{
|
|
1089
|
+
case NODE:
|
|
1090
|
+
if (index < 1 || index > network->Nnodes) return 251;
|
|
1091
|
+
currentcomment = network->Node[index].Comment;
|
|
1092
|
+
break;
|
|
1093
|
+
case LINK:
|
|
1094
|
+
if (index < 1 || index > network->Nlinks) return 251;
|
|
1095
|
+
currentcomment = network->Link[index].Comment;
|
|
1096
|
+
break;
|
|
1097
|
+
case TIMEPAT:
|
|
1098
|
+
if (index < 1 || index > network->Npats) return 251;
|
|
1099
|
+
currentcomment = network->Pattern[index].Comment;
|
|
1100
|
+
break;
|
|
1101
|
+
case CURVE:
|
|
1102
|
+
if (index < 1 || index > network->Ncurves) return 251;
|
|
1103
|
+
currentcomment = network->Curve[index].Comment;
|
|
1104
|
+
break;
|
|
1105
|
+
default:
|
|
1106
|
+
strcpy(comment, "");
|
|
1107
|
+
return 251;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// Copy the object's comment to the returned string
|
|
1111
|
+
if (currentcomment) strcpy(comment, currentcomment);
|
|
1112
|
+
else comment[0] = '\0';
|
|
1113
|
+
return 0;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
int setcomment(Network *network, int object, int index, const char *newcomment)
|
|
1117
|
+
//----------------------------------------------------------------
|
|
1118
|
+
// Input: object = a type of network object
|
|
1119
|
+
// index = index of the specified object
|
|
1120
|
+
// newcomment = new comment string
|
|
1121
|
+
// Output: error code
|
|
1122
|
+
// Purpose: sets the comment string of an object.
|
|
1123
|
+
//----------------------------------------------------------------
|
|
1124
|
+
{
|
|
1125
|
+
char *comment;
|
|
1126
|
+
|
|
1127
|
+
switch (object)
|
|
1128
|
+
{
|
|
1129
|
+
case NODE:
|
|
1130
|
+
if (index < 1 || index > network->Nnodes) return 251;
|
|
1131
|
+
comment = network->Node[index].Comment;
|
|
1132
|
+
network->Node[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
|
|
1133
|
+
return 0;
|
|
1134
|
+
|
|
1135
|
+
case LINK:
|
|
1136
|
+
if (index < 1 || index > network->Nlinks) return 251;
|
|
1137
|
+
comment = network->Link[index].Comment;
|
|
1138
|
+
network->Link[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
|
|
1139
|
+
return 0;
|
|
1140
|
+
|
|
1141
|
+
case TIMEPAT:
|
|
1142
|
+
if (index < 1 || index > network->Npats) return 251;
|
|
1143
|
+
comment = network->Pattern[index].Comment;
|
|
1144
|
+
network->Pattern[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
|
|
1145
|
+
return 0;
|
|
1146
|
+
|
|
1147
|
+
case CURVE:
|
|
1148
|
+
if (index < 1 || index > network->Ncurves) return 251;
|
|
1149
|
+
comment = network->Curve[index].Comment;
|
|
1150
|
+
network->Curve[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
|
|
1151
|
+
return 0;
|
|
1152
|
+
|
|
1153
|
+
default: return 251;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
int namevalid(const char *name)
|
|
1158
|
+
//----------------------------------------------------------------
|
|
1159
|
+
// Input: name = name used to ID an object
|
|
1160
|
+
// Output: returns TRUE if name is valid, FALSE if not
|
|
1161
|
+
// Purpose: checks that an object's ID name is valid.
|
|
1162
|
+
//----------------------------------------------------------------
|
|
1163
|
+
{
|
|
1164
|
+
size_t n = strlen(name);
|
|
1165
|
+
if (n < 1 || n > MAXID || strpbrk(name, " ;") || name[0] == '"') return FALSE;
|
|
1166
|
+
return TRUE;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
void getTmpName(char *fname)
|
|
1170
|
+
//----------------------------------------------------------------
|
|
1171
|
+
// Input: fname = file name string
|
|
1172
|
+
// Output: an unused file name
|
|
1173
|
+
// Purpose: creates a temporary file name with an "en" prefix
|
|
1174
|
+
// or a blank name if an error occurs.
|
|
1175
|
+
//----------------------------------------------------------------
|
|
1176
|
+
{
|
|
1177
|
+
#ifdef _WIN32
|
|
1178
|
+
|
|
1179
|
+
char* name = NULL;
|
|
1180
|
+
|
|
1181
|
+
// --- use Windows _tempnam function to get a pointer to an
|
|
1182
|
+
// unused file name that begins with "en"
|
|
1183
|
+
strcpy(fname, "");
|
|
1184
|
+
name = _tempnam(NULL, "en");
|
|
1185
|
+
if (name)
|
|
1186
|
+
{
|
|
1187
|
+
// --- copy the file name to fname
|
|
1188
|
+
if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
|
|
1189
|
+
|
|
1190
|
+
// --- free the pointer returned by _tempnam
|
|
1191
|
+
free(name);
|
|
1192
|
+
}
|
|
1193
|
+
// --- for non-Windows systems:
|
|
1194
|
+
#else
|
|
1195
|
+
// --- use system function mkstemp() to create a temporary file name
|
|
1196
|
+
/*
|
|
1197
|
+
int f = -1;
|
|
1198
|
+
strcpy(fname, "enXXXXXX");
|
|
1199
|
+
f = mkstemp(fname);
|
|
1200
|
+
close(f);
|
|
1201
|
+
remove(fname);
|
|
1202
|
+
*/
|
|
1203
|
+
strcpy(fname, "enXXXXXX");
|
|
1204
|
+
FILE *f = fdopen(mkstemp(fname), "r");
|
|
1205
|
+
if (f == NULL) strcpy(fname, "");
|
|
1206
|
+
else fclose(f);
|
|
1207
|
+
remove(fname);
|
|
1208
|
+
#endif
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
char *xstrcpy(char **s1, const char *s2, const size_t n)
|
|
1212
|
+
//----------------------------------------------------------------
|
|
1213
|
+
// Input: s1 = destination string
|
|
1214
|
+
// s2 = source string
|
|
1215
|
+
// n = maximum size of strings
|
|
1216
|
+
// Output: none
|
|
1217
|
+
// Purpose: like strcpy except for dynamic strings.
|
|
1218
|
+
// Note: The calling program is responsible for ensuring that
|
|
1219
|
+
// s1 points to a valid memory location or is NULL. E.g.,
|
|
1220
|
+
// the following code will likely cause a segment fault:
|
|
1221
|
+
// char *s;
|
|
1222
|
+
// s = xstrcpy(s, "Some text");
|
|
1223
|
+
// while this would work correctly:
|
|
1224
|
+
// char *s = NULL;
|
|
1225
|
+
// s = xstrcpy(s, "Some text");
|
|
1226
|
+
//----------------------------------------------------------------
|
|
1227
|
+
{
|
|
1228
|
+
size_t n1 = 0, n2 = 0;
|
|
1229
|
+
|
|
1230
|
+
// Find size of source string
|
|
1231
|
+
if (s2) n2 = strlen(s2);
|
|
1232
|
+
if (n2 > n) n2 = n;
|
|
1233
|
+
|
|
1234
|
+
// Source string is empty -- free destination string
|
|
1235
|
+
if (n2 == 0)
|
|
1236
|
+
{
|
|
1237
|
+
free(*s1);
|
|
1238
|
+
*s1 = NULL;
|
|
1239
|
+
return NULL;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
// See if size of destination string needs to grow
|
|
1243
|
+
if (*s1) n1 = strlen(*s1);
|
|
1244
|
+
if (n2 > n1) *s1 = realloc(*s1, (n2 + 1) * sizeof(char));
|
|
1245
|
+
|
|
1246
|
+
// Copy the source string into the destination string
|
|
1247
|
+
strncpy(*s1, s2, n2+1);
|
|
1248
|
+
return *s1;
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
int strcomp(const char *s1, const char *s2)
|
|
1252
|
+
/*---------------------------------------------------------------
|
|
1253
|
+
** Input: s1 = character string
|
|
1254
|
+
** s2 = character string
|
|
1255
|
+
** Output: none
|
|
1256
|
+
** Returns: 1 if s1 is same as s2, 0 otherwise
|
|
1257
|
+
** Purpose: case insensitive comparison of strings s1 & s2
|
|
1258
|
+
**---------------------------------------------------------------
|
|
1259
|
+
*/
|
|
1260
|
+
{
|
|
1261
|
+
int i;
|
|
1262
|
+
for (i = 0; UCHAR(s1[i]) == UCHAR(s2[i]); i++)
|
|
1263
|
+
{
|
|
1264
|
+
if (!s1[i + 1] && !s2[i + 1]) return 1;
|
|
1265
|
+
}
|
|
1266
|
+
return 0;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
double interp(int n, double x[], double y[], double xx)
|
|
1270
|
+
/*----------------------------------------------------------------
|
|
1271
|
+
** Input: n = number of data pairs defining a curve
|
|
1272
|
+
** x = x-data values of curve
|
|
1273
|
+
** y = y-data values of curve
|
|
1274
|
+
** xx = specified x-value
|
|
1275
|
+
** Output: none
|
|
1276
|
+
** Returns: y-value on curve at x = xx
|
|
1277
|
+
** Purpose: uses linear interpolation to find y-value on a
|
|
1278
|
+
** data curve corresponding to specified x-value.
|
|
1279
|
+
** NOTE: does not extrapolate beyond endpoints of curve.
|
|
1280
|
+
**----------------------------------------------------------------
|
|
1281
|
+
*/
|
|
1282
|
+
{
|
|
1283
|
+
int k, m;
|
|
1284
|
+
double dx, dy;
|
|
1285
|
+
|
|
1286
|
+
m = n - 1; // Highest data index
|
|
1287
|
+
if (xx <= x[0]) return (y[0]); // xx off low end of curve
|
|
1288
|
+
for (k = 1; k <= m; k++) // Bracket xx on curve
|
|
1289
|
+
{
|
|
1290
|
+
if (x[k] >= xx) // Interp. over interval
|
|
1291
|
+
{
|
|
1292
|
+
dx = x[k] - x[k - 1];
|
|
1293
|
+
dy = y[k] - y[k - 1];
|
|
1294
|
+
if (ABS(dx) < TINY) return (y[k]);
|
|
1295
|
+
else return (y[k] - (x[k] - xx) * dy / dx);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
return (y[m]); // xx off high end of curve
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
char *geterrmsg(int errcode, char *msg)
|
|
1302
|
+
/*----------------------------------------------------------------
|
|
1303
|
+
** Input: errcode = error code
|
|
1304
|
+
** Output: none
|
|
1305
|
+
** Returns: pointer to string with error message
|
|
1306
|
+
** Purpose: retrieves text of error message
|
|
1307
|
+
**----------------------------------------------------------------
|
|
1308
|
+
*/
|
|
1309
|
+
{
|
|
1310
|
+
switch (errcode)
|
|
1311
|
+
{
|
|
1312
|
+
|
|
1313
|
+
//#define DAT(code,string) case code: sprintf(msg, "%s", string); break;
|
|
1314
|
+
#define DAT(code,string) case code: strcpy(msg, string); break;
|
|
1315
|
+
#include "errors.dat"
|
|
1316
|
+
#undef DAT
|
|
1317
|
+
|
|
1318
|
+
default:
|
|
1319
|
+
strcpy(msg, "");
|
|
1320
|
+
}
|
|
1321
|
+
return (msg);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
void errmsg(Project *pr, int errcode)
|
|
1325
|
+
/*----------------------------------------------------------------
|
|
1326
|
+
** Input: errcode = error code
|
|
1327
|
+
** Output: none
|
|
1328
|
+
** Purpose: writes error message to report file
|
|
1329
|
+
**----------------------------------------------------------------
|
|
1330
|
+
*/
|
|
1331
|
+
{
|
|
1332
|
+
char errmsg[MAXMSG + 1] = "";
|
|
1333
|
+
if (errcode == 309) /* Report file write error - */
|
|
1334
|
+
{ /* Do not write msg to file. */
|
|
1335
|
+
|
|
1336
|
+
}
|
|
1337
|
+
else if (pr->report.RptFile != NULL && pr->report.Messageflag && errcode > 100)
|
|
1338
|
+
{
|
|
1339
|
+
sprintf(pr->Msg, "Error %d: %s", errcode, geterrmsg(errcode, errmsg));
|
|
1340
|
+
writeline(pr, pr->Msg);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
void writewin(void(*vp)(char *), char *s)
|
|
1345
|
+
/*----------------------------------------------------------------
|
|
1346
|
+
** Input: text string
|
|
1347
|
+
** Output: none
|
|
1348
|
+
** Purpose: passes character string to viewprog() in
|
|
1349
|
+
** application which calls the EPANET DLL
|
|
1350
|
+
**----------------------------------------------------------------
|
|
1351
|
+
*/
|
|
1352
|
+
{
|
|
1353
|
+
char progmsg[MAXMSG + 1];
|
|
1354
|
+
if (vp != NULL)
|
|
1355
|
+
{
|
|
1356
|
+
strncpy(progmsg, s, MAXMSG);
|
|
1357
|
+
vp(progmsg);
|
|
1358
|
+
}
|
|
1359
|
+
}
|