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.
Files changed (131) hide show
  1. epyt_flow/EPANET/EPANET/SRC_engines/AUTHORS +28 -0
  2. epyt_flow/EPANET/EPANET/SRC_engines/LICENSE +21 -0
  3. epyt_flow/EPANET/EPANET/SRC_engines/Readme_SRC_Engines.txt +18 -0
  4. epyt_flow/EPANET/EPANET/SRC_engines/enumstxt.h +134 -0
  5. epyt_flow/EPANET/EPANET/SRC_engines/epanet.c +5578 -0
  6. epyt_flow/EPANET/EPANET/SRC_engines/epanet2.c +865 -0
  7. epyt_flow/EPANET/EPANET/SRC_engines/epanet2.def +131 -0
  8. epyt_flow/EPANET/EPANET/SRC_engines/errors.dat +73 -0
  9. epyt_flow/EPANET/EPANET/SRC_engines/funcs.h +193 -0
  10. epyt_flow/EPANET/EPANET/SRC_engines/genmmd.c +1000 -0
  11. epyt_flow/EPANET/EPANET/SRC_engines/hash.c +177 -0
  12. epyt_flow/EPANET/EPANET/SRC_engines/hash.h +28 -0
  13. epyt_flow/EPANET/EPANET/SRC_engines/hydcoeffs.c +1151 -0
  14. epyt_flow/EPANET/EPANET/SRC_engines/hydraul.c +1117 -0
  15. epyt_flow/EPANET/EPANET/SRC_engines/hydsolver.c +720 -0
  16. epyt_flow/EPANET/EPANET/SRC_engines/hydstatus.c +476 -0
  17. epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2.h +431 -0
  18. epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_2.h +1786 -0
  19. epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_enums.h +468 -0
  20. epyt_flow/EPANET/EPANET/SRC_engines/inpfile.c +810 -0
  21. epyt_flow/EPANET/EPANET/SRC_engines/input1.c +707 -0
  22. epyt_flow/EPANET/EPANET/SRC_engines/input2.c +864 -0
  23. epyt_flow/EPANET/EPANET/SRC_engines/input3.c +2170 -0
  24. epyt_flow/EPANET/EPANET/SRC_engines/main.c +93 -0
  25. epyt_flow/EPANET/EPANET/SRC_engines/mempool.c +142 -0
  26. epyt_flow/EPANET/EPANET/SRC_engines/mempool.h +24 -0
  27. epyt_flow/EPANET/EPANET/SRC_engines/output.c +852 -0
  28. epyt_flow/EPANET/EPANET/SRC_engines/project.c +1359 -0
  29. epyt_flow/EPANET/EPANET/SRC_engines/quality.c +685 -0
  30. epyt_flow/EPANET/EPANET/SRC_engines/qualreact.c +743 -0
  31. epyt_flow/EPANET/EPANET/SRC_engines/qualroute.c +694 -0
  32. epyt_flow/EPANET/EPANET/SRC_engines/report.c +1489 -0
  33. epyt_flow/EPANET/EPANET/SRC_engines/rules.c +1362 -0
  34. epyt_flow/EPANET/EPANET/SRC_engines/smatrix.c +871 -0
  35. epyt_flow/EPANET/EPANET/SRC_engines/text.h +497 -0
  36. epyt_flow/EPANET/EPANET/SRC_engines/types.h +874 -0
  37. epyt_flow/EPANET/EPANET-MSX/MSX_Updates.txt +53 -0
  38. epyt_flow/EPANET/EPANET-MSX/Src/dispersion.h +27 -0
  39. epyt_flow/EPANET/EPANET-MSX/Src/hash.c +107 -0
  40. epyt_flow/EPANET/EPANET-MSX/Src/hash.h +28 -0
  41. epyt_flow/EPANET/EPANET-MSX/Src/include/epanetmsx.h +102 -0
  42. epyt_flow/EPANET/EPANET-MSX/Src/include/epanetmsx_export.h +42 -0
  43. epyt_flow/EPANET/EPANET-MSX/Src/mathexpr.c +937 -0
  44. epyt_flow/EPANET/EPANET-MSX/Src/mathexpr.h +39 -0
  45. epyt_flow/EPANET/EPANET-MSX/Src/mempool.c +204 -0
  46. epyt_flow/EPANET/EPANET-MSX/Src/mempool.h +24 -0
  47. epyt_flow/EPANET/EPANET-MSX/Src/msxchem.c +1285 -0
  48. epyt_flow/EPANET/EPANET-MSX/Src/msxcompiler.c +368 -0
  49. epyt_flow/EPANET/EPANET-MSX/Src/msxdict.h +42 -0
  50. epyt_flow/EPANET/EPANET-MSX/Src/msxdispersion.c +586 -0
  51. epyt_flow/EPANET/EPANET-MSX/Src/msxerr.c +116 -0
  52. epyt_flow/EPANET/EPANET-MSX/Src/msxfile.c +260 -0
  53. epyt_flow/EPANET/EPANET-MSX/Src/msxfuncs.c +175 -0
  54. epyt_flow/EPANET/EPANET-MSX/Src/msxfuncs.h +35 -0
  55. epyt_flow/EPANET/EPANET-MSX/Src/msxinp.c +1504 -0
  56. epyt_flow/EPANET/EPANET-MSX/Src/msxout.c +401 -0
  57. epyt_flow/EPANET/EPANET-MSX/Src/msxproj.c +791 -0
  58. epyt_flow/EPANET/EPANET-MSX/Src/msxqual.c +2010 -0
  59. epyt_flow/EPANET/EPANET-MSX/Src/msxrpt.c +400 -0
  60. epyt_flow/EPANET/EPANET-MSX/Src/msxtank.c +422 -0
  61. epyt_flow/EPANET/EPANET-MSX/Src/msxtoolkit.c +1164 -0
  62. epyt_flow/EPANET/EPANET-MSX/Src/msxtypes.h +551 -0
  63. epyt_flow/EPANET/EPANET-MSX/Src/msxutils.c +524 -0
  64. epyt_flow/EPANET/EPANET-MSX/Src/msxutils.h +56 -0
  65. epyt_flow/EPANET/EPANET-MSX/Src/newton.c +158 -0
  66. epyt_flow/EPANET/EPANET-MSX/Src/newton.h +34 -0
  67. epyt_flow/EPANET/EPANET-MSX/Src/rk5.c +287 -0
  68. epyt_flow/EPANET/EPANET-MSX/Src/rk5.h +39 -0
  69. epyt_flow/EPANET/EPANET-MSX/Src/ros2.c +293 -0
  70. epyt_flow/EPANET/EPANET-MSX/Src/ros2.h +35 -0
  71. epyt_flow/EPANET/EPANET-MSX/Src/smatrix.c +816 -0
  72. epyt_flow/EPANET/EPANET-MSX/Src/smatrix.h +29 -0
  73. epyt_flow/EPANET/EPANET-MSX/readme.txt +14 -0
  74. epyt_flow/EPANET/compile.sh +4 -0
  75. epyt_flow/VERSION +1 -0
  76. epyt_flow/__init__.py +24 -0
  77. epyt_flow/data/__init__.py +0 -0
  78. epyt_flow/data/benchmarks/__init__.py +11 -0
  79. epyt_flow/data/benchmarks/batadal.py +257 -0
  80. epyt_flow/data/benchmarks/batadal_data.py +28 -0
  81. epyt_flow/data/benchmarks/battledim.py +473 -0
  82. epyt_flow/data/benchmarks/battledim_data.py +51 -0
  83. epyt_flow/data/benchmarks/gecco_water_quality.py +267 -0
  84. epyt_flow/data/benchmarks/leakdb.py +592 -0
  85. epyt_flow/data/benchmarks/leakdb_data.py +18923 -0
  86. epyt_flow/data/benchmarks/water_usage.py +123 -0
  87. epyt_flow/data/networks.py +650 -0
  88. epyt_flow/gym/__init__.py +4 -0
  89. epyt_flow/gym/control_gyms.py +47 -0
  90. epyt_flow/gym/scenario_control_env.py +101 -0
  91. epyt_flow/metrics.py +404 -0
  92. epyt_flow/models/__init__.py +2 -0
  93. epyt_flow/models/event_detector.py +31 -0
  94. epyt_flow/models/sensor_interpolation_detector.py +118 -0
  95. epyt_flow/rest_api/__init__.py +4 -0
  96. epyt_flow/rest_api/base_handler.py +70 -0
  97. epyt_flow/rest_api/res_manager.py +95 -0
  98. epyt_flow/rest_api/scada_data_handler.py +476 -0
  99. epyt_flow/rest_api/scenario_handler.py +352 -0
  100. epyt_flow/rest_api/server.py +106 -0
  101. epyt_flow/serialization.py +438 -0
  102. epyt_flow/simulation/__init__.py +5 -0
  103. epyt_flow/simulation/events/__init__.py +6 -0
  104. epyt_flow/simulation/events/actuator_events.py +259 -0
  105. epyt_flow/simulation/events/event.py +81 -0
  106. epyt_flow/simulation/events/leakages.py +404 -0
  107. epyt_flow/simulation/events/sensor_faults.py +267 -0
  108. epyt_flow/simulation/events/sensor_reading_attack.py +185 -0
  109. epyt_flow/simulation/events/sensor_reading_event.py +170 -0
  110. epyt_flow/simulation/events/system_event.py +88 -0
  111. epyt_flow/simulation/parallel_simulation.py +147 -0
  112. epyt_flow/simulation/scada/__init__.py +3 -0
  113. epyt_flow/simulation/scada/advanced_control.py +134 -0
  114. epyt_flow/simulation/scada/scada_data.py +1589 -0
  115. epyt_flow/simulation/scada/scada_data_export.py +255 -0
  116. epyt_flow/simulation/scenario_config.py +608 -0
  117. epyt_flow/simulation/scenario_simulator.py +1897 -0
  118. epyt_flow/simulation/scenario_visualizer.py +61 -0
  119. epyt_flow/simulation/sensor_config.py +1289 -0
  120. epyt_flow/topology.py +290 -0
  121. epyt_flow/uncertainty/__init__.py +3 -0
  122. epyt_flow/uncertainty/model_uncertainty.py +302 -0
  123. epyt_flow/uncertainty/sensor_noise.py +73 -0
  124. epyt_flow/uncertainty/uncertainties.py +555 -0
  125. epyt_flow/uncertainty/utils.py +206 -0
  126. epyt_flow/utils.py +306 -0
  127. epyt_flow-0.1.0.dist-info/LICENSE +21 -0
  128. epyt_flow-0.1.0.dist-info/METADATA +139 -0
  129. epyt_flow-0.1.0.dist-info/RECORD +131 -0
  130. epyt_flow-0.1.0.dist-info/WHEEL +5 -0
  131. epyt_flow-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,816 @@
1
+ /*
2
+ *******************************************************************
3
+ Modified from:
4
+
5
+ SMATRIX.C -- Sparse matrix routines for EPANET program, VERSION: 2.0.0.
6
+
7
+
8
+ DATE: 5/8/00
9
+ AUTHOR: L. Rossman
10
+ US EPA - NRMRL
11
+
12
+ This module contains the sparse matrix routines used to solve
13
+ a network's hydraulic equations. The entry points into this
14
+ module are:
15
+ msx_createsparse() -- called from openhyd() in HYDRAUL.C
16
+ msx_freesparse() -- called from closehyd() in HYDRAUL.C
17
+ msx_linsolve() -- called from netsolve() in HYDRAUL.C
18
+
19
+ Createsparse() does the following:
20
+ 1. for each node, builds an adjacency list that identifies
21
+ all links connected to the node (see buildlists())
22
+ 2. re-orders the network's nodes to minimize the number
23
+ of non-zero entries in the hydraulic solution matrix
24
+ (see reorder())
25
+ 3. converts the adjacency lists into a compact scheme
26
+ for storing the non-zero coeffs. in the lower diagonal
27
+ portion of the solution matrix (see storesparse())
28
+ Freesparse() frees the memory used for the sparse matrix.
29
+ Linsolve() solves the linearized system of hydraulic equations.
30
+
31
+ ********************************************************************
32
+ */
33
+
34
+ #include <stdio.h>
35
+ #include <string.h>
36
+ #include <stdlib.h>
37
+ #include <math.h>
38
+
39
+ #include "hash.h"
40
+ #include "msxtypes.h"
41
+ #include "smatrix.h"
42
+ #include "dispersion.h"
43
+ #define EXTERN extern
44
+
45
+
46
+
47
+ // External variables
48
+ //--------------------
49
+ extern MSXproject MSX; // MSX project data
50
+ #define ERRCODE(x) (errcode = ((errcode>100) ? (errcode) : (x)))
51
+
52
+
53
+
54
+
55
+ int msx_createsparse()
56
+ /*
57
+ **--------------------------------------------------------------
58
+ ** Input: none
59
+ ** Output: returns error code
60
+ ** Purpose: creates sparse representation of coeff. matrix
61
+ **--------------------------------------------------------------
62
+ */
63
+ {
64
+ int errcode = 0;
65
+ int njuncs = MSX.Nobjects[NODE] - MSX.Nobjects[TANK];
66
+
67
+ /* Allocate data structures */
68
+ ERRCODE(allocsparse());
69
+ if (errcode) return(errcode);
70
+
71
+ /* Build node-link adjacency lists with parallel links removed. */
72
+
73
+ ERRCODE(buildlists(TRUE)); //TRUE localadjlists in 2.2
74
+ if (!errcode)
75
+ {
76
+ xparalinks(); /* Remove parallel links */
77
+ countdegree(); /* Find degree of each junction */
78
+ } /* (= # of adjacent links) */
79
+
80
+ /* Re-order nodes to minimize number of non-zero coeffs. */
81
+ /* in factorized solution matrix. At same time, adjacency */
82
+ /* list is updated with links representing non-zero coeffs. */
83
+ MSX.Dispersion.Ncoeffs = MSX.Nobjects[LINK];
84
+ ERRCODE(reordernodes());
85
+
86
+ /* Allocate memory for sparse storage of positions of non-zero */
87
+ /* coeffs. and store these positions in vector NZSUB. */
88
+ ERRCODE(storesparse(njuncs));
89
+
90
+ /* Free memory used for adjacency lists and sort */
91
+ /* row indexes in NZSUB to optimize msx_linsolve(). */
92
+ if (!errcode) freelists();
93
+ ERRCODE(ordersparse(njuncs));
94
+
95
+ MSX.Dispersion.Aij = (double*)calloc(MSX.Dispersion.Ncoeffs + 1, sizeof(double));
96
+ MSX.Dispersion.Aii = (double*)calloc(MSX.Nobjects[NODE]+1, sizeof(double));
97
+ MSX.Dispersion.F = (double*)calloc(MSX.Nobjects[NODE]+1, sizeof(double));
98
+ MSX.Dispersion.temp = (double*)calloc(MSX.Nobjects[NODE] + 1, sizeof(double));
99
+ MSX.Dispersion.link = (int*)calloc(MSX.Nobjects[NODE] + 1, sizeof(int));
100
+ MSX.Dispersion.first = (int*)calloc(MSX.Nobjects[NODE] + 1, sizeof(int));
101
+ ERRCODE(MEMCHECK(MSX.Dispersion.Aij));
102
+ ERRCODE(MEMCHECK(MSX.Dispersion.Aii));
103
+ ERRCODE(MEMCHECK(MSX.Dispersion.F));
104
+ ERRCODE(MEMCHECK(MSX.Dispersion.temp));
105
+ ERRCODE(MEMCHECK(MSX.Dispersion.link));
106
+ ERRCODE(MEMCHECK(MSX.Dispersion.first));
107
+
108
+ // MSX.Dispersion.md = (double*)calloc(MSX.Nobjects[SPECIES] + 1, sizeof(double));
109
+
110
+ MSX.Dispersion.pipeDispersionCoeff = (double*)calloc(MSX.Nobjects[LINK] + 1, sizeof(double));
111
+
112
+
113
+
114
+ ERRCODE(MEMCHECK(MSX.Dispersion.ld));
115
+ ERRCODE(MEMCHECK(MSX.Dispersion.md));
116
+
117
+
118
+ dispersion_open();
119
+
120
+ /* Re-build adjacency lists without removing parallel */
121
+ /* links for use in future connectivity checking. */
122
+ ERRCODE(buildlists(FALSE)); //FALSE buildadjlists in 2.2
123
+
124
+ /* Free allocated memory */
125
+ free(MSX.Dispersion.Degree);
126
+ return(errcode);
127
+ } /* End of msx_createsparse */
128
+
129
+
130
+ int allocsparse()
131
+ /*
132
+ **--------------------------------------------------------------
133
+ ** Input: none
134
+ ** Output: returns error code
135
+ ** Purpose: allocates memory for indexing the solution matrix
136
+ **--------------------------------------------------------------
137
+ */
138
+ {
139
+ int errcode = 0;
140
+ int nnodes = MSX.Nobjects[NODE];
141
+ int nlinks = MSX.Nobjects[LINK];
142
+ MSX.Dispersion.Aii = NULL;
143
+ MSX.Dispersion.Aij = NULL;
144
+ MSX.Dispersion.F = NULL;
145
+ MSX.Dispersion.temp = NULL;
146
+ MSX.Dispersion.link = NULL;
147
+ MSX.Dispersion.first = NULL;
148
+
149
+ MSX.Dispersion.Adjlist = (Padjlist *) calloc(nnodes+1, sizeof(Padjlist));
150
+ MSX.Dispersion.Order = (int *) calloc(nnodes+1, sizeof(int));
151
+ MSX.Dispersion.Row = (int *) calloc(nnodes+1, sizeof(int));
152
+ MSX.Dispersion.Ndx = (int *) calloc(nlinks+1, sizeof(int));
153
+ ERRCODE(MEMCHECK(MSX.Dispersion.Adjlist));
154
+ ERRCODE(MEMCHECK(MSX.Dispersion.Order));
155
+ ERRCODE(MEMCHECK(MSX.Dispersion.Row));
156
+ ERRCODE(MEMCHECK(MSX.Dispersion.Ndx));
157
+ return(errcode);
158
+ }
159
+
160
+
161
+ void msx_freesparse()
162
+ /*
163
+ **----------------------------------------------------------------
164
+ ** Input: None
165
+ ** Output: None
166
+ ** Purpose: Frees memory used for sparse matrix storage
167
+ **----------------------------------------------------------------
168
+ */
169
+ {
170
+ freelists();
171
+ FREE(MSX.Dispersion.Adjlist);
172
+ FREE(MSX.Dispersion.Order);
173
+ FREE(MSX.Dispersion.Row);
174
+ FREE(MSX.Dispersion.Ndx);
175
+ FREE(MSX.Dispersion.XLNZ);
176
+ FREE(MSX.Dispersion.NZSUB);
177
+ FREE(MSX.Dispersion.LNZ);
178
+ FREE(MSX.Dispersion.Aii);
179
+ FREE(MSX.Dispersion.Aij);
180
+ FREE(MSX.Dispersion.F);
181
+ FREE(MSX.Dispersion.link);
182
+ FREE(MSX.Dispersion.first);
183
+ FREE(MSX.Dispersion.temp);
184
+
185
+ FREE(MSX.Dispersion.md);
186
+
187
+ FREE(MSX.Dispersion.pipeDispersionCoeff);
188
+ dispersion_close();
189
+
190
+
191
+
192
+
193
+ } /* End of msx_freesparse */
194
+
195
+
196
+ int buildlists(int paraflag)
197
+ /*
198
+ **--------------------------------------------------------------
199
+ ** Input: paraflag = TRUE if list marks parallel links
200
+ ** Output: returns error code
201
+ ** Purpose: builds linked list of links adjacent to each node
202
+ **--------------------------------------------------------------
203
+ */
204
+ {
205
+ int i,j,k;
206
+ int pmark = 0;
207
+ int errcode = 0;
208
+ int nlinks = MSX.Nobjects[LINK];
209
+ Padjlist alink;
210
+
211
+ freelists();
212
+ MSX.Dispersion.Adjlist = (Padjlist*)calloc(MSX.Nobjects[NODE] + 1, sizeof(Padjlist));
213
+ if (MSX.Dispersion.Adjlist == NULL) return 101;
214
+ for (i = 0; i <= MSX.Nobjects[NODE]; i++)
215
+ MSX.Dispersion.Adjlist[i] = NULL;
216
+
217
+ /* For each link, update adjacency lists of its end nodes */
218
+ for (k=1; k<=nlinks; k++)
219
+ {
220
+ i = MSX.Link[k].n1;
221
+ j = MSX.Link[k].n2;
222
+ if (paraflag) pmark = paralink(i,j,k); /* Parallel link check */
223
+
224
+ /* Include link in start node i's list */
225
+ alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
226
+ if (alink == NULL) return(101);
227
+ if (!pmark) alink->node = j;
228
+ else alink->node = 0; /* Parallel link marker */
229
+ alink->link = k;
230
+ alink->next = MSX.Dispersion.Adjlist[i];
231
+ MSX.Dispersion.Adjlist[i] = alink;
232
+
233
+ /* Include link in end node j's list */
234
+ alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
235
+ if (alink == NULL) return(101);
236
+ if (!pmark) alink->node = i;
237
+ else alink->node = 0; /* Parallel link marker */
238
+ alink->link = k;
239
+ alink->next = MSX.Dispersion.Adjlist[j];
240
+ MSX.Dispersion.Adjlist[j] = alink;
241
+ }
242
+ if (errcode)freelists();
243
+ return(errcode);
244
+ } /* End of buildlists */
245
+
246
+
247
+ int paralink(int i, int j, int k)
248
+ /*
249
+ **--------------------------------------------------------------
250
+ ** Input: i = index of start node of link
251
+ ** j = index of end node of link
252
+ ** k = link index
253
+ ** Output: returns 1 if link k parallels another link, else 0
254
+ ** Purpose: checks for parallel links between nodes i and j
255
+ **
256
+ **--------------------------------------------------------------
257
+ */
258
+ {
259
+ Padjlist alink;
260
+ for (alink = MSX.Dispersion.Adjlist[i]; alink != NULL; alink = alink->next)
261
+ {
262
+ if (alink->node == j) /* Link || to k (same end nodes) */
263
+ {
264
+ MSX.Dispersion.Ndx[k] = alink->link; /* Assign Ndx entry to this link */
265
+ return(1);
266
+ }
267
+ }
268
+ MSX.Dispersion.Ndx[k] = k; /* Ndx entry if link not parallel */
269
+ return(0);
270
+ } /* End of paralink */
271
+
272
+
273
+ void xparalinks()
274
+ /*
275
+ **--------------------------------------------------------------
276
+ ** Input: none
277
+ ** Output: none
278
+ ** Purpose: removes parallel links from nodal adjacency lists
279
+ **--------------------------------------------------------------
280
+ */
281
+ {
282
+ int i;
283
+ Padjlist alink, /* Current item in adjacency list */
284
+ blink; /* Previous item in adjacency list */
285
+ int nnodes = MSX.Nobjects[NODE];
286
+
287
+ /* Scan adjacency list of each node */
288
+ for (i=1; i<=nnodes; i++)
289
+ {
290
+ alink = MSX.Dispersion.Adjlist[i]; /* First item in list */
291
+ blink = NULL;
292
+ while (alink != NULL)
293
+ {
294
+ if (alink->node == 0) /* Parallel link marker found */
295
+ {
296
+ if (blink == NULL) /* This holds at start of list */
297
+ {
298
+ MSX.Dispersion.Adjlist[i] = alink->next;
299
+ free(alink); /* Remove item from list */
300
+ alink = MSX.Dispersion.Adjlist[i];
301
+ }
302
+ else /* This holds for interior of list */
303
+ {
304
+ blink->next = alink->next;
305
+ free(alink); /* Remove item from list */
306
+ alink = blink->next;
307
+ }
308
+ }
309
+ else
310
+ {
311
+ blink = alink; /* Move to next item in list */
312
+ alink = alink->next;
313
+ }
314
+ }
315
+ }
316
+ } /* End of xparalinks */
317
+
318
+
319
+ void freelists()
320
+ /*
321
+ **--------------------------------------------------------------
322
+ ** Input: none
323
+ ** Output: none
324
+ ** Purpose: frees memory used for nodal adjacency lists
325
+ **--------------------------------------------------------------
326
+ */
327
+ {
328
+ int i;
329
+ Padjlist alink;
330
+ int nnodes = MSX.Nobjects[NODE];
331
+
332
+ if (MSX.Dispersion.Adjlist == NULL)
333
+ return;
334
+ for (i=0; i<=nnodes; i++)
335
+ {
336
+ for (alink = MSX.Dispersion.Adjlist[i]; alink != NULL; alink = MSX.Dispersion.Adjlist[i])
337
+ {
338
+ MSX.Dispersion.Adjlist[i] = alink->next;
339
+ free(alink);
340
+ }
341
+ }
342
+ free(MSX.Dispersion.Adjlist);
343
+ MSX.Dispersion.Adjlist = NULL;
344
+ } /* End of freelists */
345
+
346
+
347
+ void countdegree()
348
+ /*
349
+ **----------------------------------------------------------------
350
+ ** Input: none
351
+ ** Output: none
352
+ ** Purpose: counts number of nodes directly connected to each node
353
+ **----------------------------------------------------------------
354
+ */
355
+ {
356
+ int i;
357
+ Padjlist alink;
358
+
359
+ int nnodes = MSX.Nobjects[NODE];
360
+ int njuncs = MSX.Nobjects[NODE]-MSX.Nobjects[TANK];
361
+ int errcode;
362
+
363
+ errcode = 0;
364
+
365
+ MSX.Dispersion.Degree = (int *)calloc(MSX.Nobjects[NODE] + 1, sizeof(int));
366
+ ERRCODE(MEMCHECK(MSX.Dispersion.Degree));
367
+ memset(MSX.Dispersion.Degree,0,(nnodes+1)*sizeof(int));
368
+
369
+ /* NOTE: For purposes of node re-ordering, Tanks (nodes with */
370
+ /* indexes above Njuncs) have zero degree of adjacency. */
371
+
372
+ for (i=1; i<=njuncs; i++)
373
+ for (alink = MSX.Dispersion.Adjlist[i]; alink != NULL; alink = alink->next)
374
+ if (alink->node > 0) MSX.Dispersion.Degree[i]++;
375
+ }
376
+
377
+
378
+ int reordernodes()
379
+ /*
380
+ **--------------------------------------------------------------
381
+ ** Input: none
382
+ ** Output: returns 1 if successful, 0 if not
383
+ ** Purpose: re-orders nodes to minimize # of non-zeros that
384
+ ** will appear in factorized solution matrix
385
+ **--------------------------------------------------------------
386
+ */
387
+ {
388
+ int k, knode, m, n;
389
+ int nnodes = MSX.Nobjects[NODE];
390
+ int njuncs = MSX.Nobjects[NODE] - MSX.Nobjects[TANK];
391
+ for (k=1; k<=nnodes; k++)
392
+ {
393
+ MSX.Dispersion.Row[k] = k;
394
+ MSX.Dispersion.Order[k] = k;
395
+ }
396
+ n = njuncs;
397
+ for (k=1; k<=n; k++) /* Examine each junction */
398
+ {
399
+ m = mindegree(k,n); /* Node with lowest degree */
400
+ knode = MSX.Dispersion.Order[m]; /* Node's index */
401
+ if (!growlist(knode)) return(101); /* Augment adjacency list */
402
+ MSX.Dispersion.Order[m] = MSX.Dispersion.Order[k]; /* Switch order of nodes */
403
+ MSX.Dispersion.Order[k] = knode;
404
+ MSX.Dispersion.Degree[knode] = 0; /* In-activate node */
405
+ }
406
+ for (k=1; k<=n; k++) /* Assign nodes to rows of */
407
+ MSX.Dispersion.Row[MSX.Dispersion.Order[k]] = k; /* coeff. matrix */
408
+ return(0);
409
+ } /* End of reordernodes */
410
+
411
+
412
+ int mindegree(int k, int n)
413
+ /*
414
+ **--------------------------------------------------------------
415
+ ** Input: k = first node in list of active nodes
416
+ ** n = total number of junction nodes
417
+ ** Output: returns node index with fewest direct connections
418
+ ** Purpose: finds active node with fewest direct connections
419
+ **--------------------------------------------------------------
420
+ */
421
+ {
422
+ int i, m;
423
+ int min = n,
424
+ imin = n;
425
+
426
+ for (i=k; i<=n; i++)
427
+ {
428
+ m = MSX.Dispersion.Degree[MSX.Dispersion.Order[i]];
429
+ if (m < min)
430
+ {
431
+ min = m;
432
+ imin = i;
433
+ }
434
+ }
435
+ return(imin);
436
+ } /* End of mindegree */
437
+
438
+
439
+ int growlist(int knode)
440
+ /*
441
+ **--------------------------------------------------------------
442
+ ** Input: knode = node index
443
+ ** Output: returns 1 if successful, 0 if not
444
+ ** Purpose: creates new entries in knode's adjacency list for
445
+ ** all unlinked pairs of active nodes that are
446
+ ** adjacent to knode
447
+ **--------------------------------------------------------------
448
+ */
449
+ {
450
+ int node;
451
+ Padjlist alink;
452
+
453
+ /* Iterate through all nodes connected to knode */
454
+ for (alink = MSX.Dispersion.Adjlist[knode]; alink != NULL; alink = alink -> next)
455
+ {
456
+ node = alink->node; /* End node of connecting link */
457
+ if (MSX.Dispersion.Degree[node] > 0) /* End node is active */
458
+ {
459
+ MSX.Dispersion.Degree[node]--; /* Reduce degree of adjacency */
460
+ if (!newlink(alink)) /* Add to adjacency list */
461
+ return(0);
462
+ }
463
+ }
464
+ return(1);
465
+ } /* End of growlist */
466
+
467
+
468
+ int newlink(Padjlist alink)
469
+ /*
470
+ **--------------------------------------------------------------
471
+ ** Input: alink = element of node's adjacency list
472
+ ** Output: returns 1 if successful, 0 if not
473
+ ** Purpose: links end of current adjacent link to end nodes of
474
+ ** all links that follow it on adjacency list
475
+ **--------------------------------------------------------------
476
+ */
477
+ {
478
+ int inode, jnode;
479
+ Padjlist blink;
480
+
481
+ /* Scan all entries in adjacency list that follow anode. */
482
+ inode = alink->node; /* End node of connection to anode */
483
+ for (blink = alink->next; blink != NULL; blink = blink->next)
484
+ {
485
+ jnode = blink->node; /* End node of next connection */
486
+
487
+ /* If jnode still active, and inode not connected to jnode, */
488
+ /* then add a new connection between inode and jnode. */
489
+ if (MSX.Dispersion.Degree[jnode] > 0) /* jnode still active */
490
+ {
491
+ if (!linked(inode,jnode)) /* inode not linked to jnode */
492
+ {
493
+
494
+ /* Since new connection represents a non-zero coeff. */
495
+ /* in the solution matrix, update the coeff. count. */
496
+ MSX.Dispersion.Ncoeffs++;
497
+
498
+ /* Update adjacency lists for inode & jnode to */
499
+ /* reflect the new connection. */
500
+ if (!addlink(inode,jnode,MSX.Dispersion.Ncoeffs)) return(0);
501
+ if (!addlink(jnode,inode,MSX.Dispersion.Ncoeffs)) return(0);
502
+ MSX.Dispersion.Degree[inode]++;
503
+ MSX.Dispersion.Degree[jnode]++;
504
+ }
505
+ }
506
+ }
507
+ return(1);
508
+ } /* End of newlink */
509
+
510
+
511
+ int linked(int i, int j)
512
+ /*
513
+ **--------------------------------------------------------------
514
+ ** Input: i = node index
515
+ ** j = node index
516
+ ** Output: returns 1 if nodes i and j are linked, 0 if not
517
+ ** Purpose: checks if nodes i and j are already linked.
518
+ **--------------------------------------------------------------
519
+ */
520
+ {
521
+ Padjlist alink;
522
+ for (alink = MSX.Dispersion.Adjlist[i]; alink != NULL; alink = alink->next)
523
+ if (alink->node == j) return(1);
524
+ return(0);
525
+ } /* End of linked */
526
+
527
+
528
+ int addlink(int i, int j, int n)
529
+ /*
530
+ **--------------------------------------------------------------
531
+ ** Input: i = node index
532
+ ** j = node index
533
+ ** n = link index
534
+ ** Output: returns 1 if successful, 0 if not
535
+ ** Purpose: augments node i's adjacency list with node j
536
+ **--------------------------------------------------------------
537
+ */
538
+ {
539
+ Padjlist alink;
540
+ alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
541
+ if (alink == NULL) return(0);
542
+ alink->node = j;
543
+ alink->link = n;
544
+ alink->next = MSX.Dispersion.Adjlist[i];
545
+ MSX.Dispersion.Adjlist[i] = alink;
546
+ return(1);
547
+ } /* End of addlink */
548
+
549
+
550
+ int storesparse(int n)
551
+ /*
552
+ **--------------------------------------------------------------
553
+ ** Input: n = number of rows in solution matrix
554
+ ** Output: returns error code
555
+ ** Purpose: stores row indexes of non-zeros of each column of
556
+ ** lower triangular portion of factorized matrix
557
+ **--------------------------------------------------------------
558
+ */
559
+ {
560
+ Padjlist alink;
561
+ int i, ii, j, k, l, m;
562
+ int errcode = 0;
563
+
564
+ /* Allocate sparse matrix storage */
565
+ MSX.Dispersion.XLNZ = (int *) calloc(n+2, sizeof(int));
566
+ MSX.Dispersion.NZSUB = (int *) calloc(MSX.Dispersion.Ncoeffs+2, sizeof(int));
567
+ MSX.Dispersion.LNZ = (int *) calloc(MSX.Dispersion.Ncoeffs+2, sizeof(int));
568
+ ERRCODE(MEMCHECK(MSX.Dispersion.XLNZ));
569
+ ERRCODE(MEMCHECK(MSX.Dispersion.NZSUB));
570
+ ERRCODE(MEMCHECK(MSX.Dispersion.LNZ));
571
+ if (errcode) return(errcode);
572
+
573
+ /* Generate row index pointers for each column of matrix */
574
+ k = 0;
575
+ MSX.Dispersion.XLNZ[1] = 1;
576
+ for (i=1; i<=n; i++) /* column */
577
+ {
578
+ m = 0;
579
+ ii = MSX.Dispersion.Order[i];
580
+ for (alink = MSX.Dispersion.Adjlist[ii]; alink != NULL; alink = alink->next)
581
+ {
582
+ j = MSX.Dispersion.Row[alink->node]; /* row */
583
+ l = alink->link;
584
+ if (j > i && j <= n)
585
+ {
586
+ m++;
587
+ k++;
588
+ MSX.Dispersion.NZSUB[k] = j;
589
+ MSX.Dispersion.LNZ[k] = l;
590
+ }
591
+ }
592
+ MSX.Dispersion.XLNZ[i+1] = MSX.Dispersion.XLNZ[i] + m;
593
+ }
594
+ return(errcode);
595
+ } /* End of storesparse */
596
+
597
+
598
+ int ordersparse(int n)
599
+ /*
600
+ **--------------------------------------------------------------
601
+ ** Input: n = number of rows in solution matrix
602
+ ** Output: returns eror code
603
+ ** Purpose: puts row indexes in ascending order in NZSUB
604
+ **--------------------------------------------------------------
605
+ */
606
+ {
607
+ int i, k;
608
+ int *xlnzt, *nzsubt, *lnzt, *nzt;
609
+ int errcode = 0;
610
+
611
+ xlnzt = (int *) calloc(n+2, sizeof(int));
612
+ nzsubt = (int *) calloc(MSX.Dispersion.Ncoeffs+2, sizeof(int));
613
+ lnzt = (int *) calloc(MSX.Dispersion.Ncoeffs+2, sizeof(int));
614
+ nzt = (int *) calloc(n+2, sizeof(int));
615
+ ERRCODE(MEMCHECK(xlnzt));
616
+ ERRCODE(MEMCHECK(nzsubt));
617
+ ERRCODE(MEMCHECK(lnzt));
618
+ ERRCODE(MEMCHECK(nzt));
619
+ if (!errcode)
620
+ {
621
+
622
+ /* Count # non-zeros in each row */
623
+ for (i=1; i<=n; i++) nzt[i] = 0;
624
+ for (i=1; i<=n; i++)
625
+ {
626
+ for (k=MSX.Dispersion.XLNZ[i]; k<MSX.Dispersion.XLNZ[i+1]; k++) nzt[MSX.Dispersion.NZSUB[k]]++;
627
+ }
628
+ xlnzt[1] = 1;
629
+ for (i=1; i<=n; i++) xlnzt[i+1] = xlnzt[i] + nzt[i];
630
+
631
+ /* Transpose matrix twice to order column indexes */
632
+ transpose(n,MSX.Dispersion.XLNZ,MSX.Dispersion.NZSUB,MSX.Dispersion.LNZ,xlnzt,nzsubt,lnzt,nzt);
633
+ transpose(n,xlnzt,nzsubt,lnzt,MSX.Dispersion.XLNZ,MSX.Dispersion.NZSUB,MSX.Dispersion.LNZ,nzt);
634
+ }
635
+
636
+ /* Reclaim memory */
637
+ free(xlnzt);
638
+ free(nzsubt);
639
+ free(lnzt);
640
+ free(nzt);
641
+ return(errcode);
642
+ } /* End of ordersparse */
643
+
644
+
645
+ void transpose(int n, int *il, int *jl, int *xl, int *ilt, int *jlt,
646
+ int *xlt, int *nzt)
647
+ /*
648
+ **---------------------------------------------------------------------
649
+ ** Input: n = matrix order
650
+ ** il,jl,xl = sparse storage scheme for original matrix
651
+ ** nzt = work array
652
+ ** Output: ilt,jlt,xlt = sparse storage scheme for transposed matrix
653
+ ** Purpose: Determines sparse storage scheme for transpose of a matrix
654
+ **---------------------------------------------------------------------
655
+ */
656
+ {
657
+ int i, j, k, kk;
658
+
659
+ for (i=1; i<=n; i++) nzt[i] = 0;
660
+ for (i=1; i<=n; i++)
661
+ {
662
+ for (k=il[i]; k<il[i+1]; k++)
663
+ {
664
+ j = jl[k];
665
+ kk = ilt[j] + nzt[j];
666
+ jlt[kk] = i;
667
+ xlt[kk] = xl[k];
668
+ nzt[j]++;
669
+ }
670
+ }
671
+ } /* End of transpose */
672
+
673
+
674
+ int msx_linsolve(int n, double *Aii, double *Aij, double *B)
675
+ /*
676
+ **--------------------------------------------------------------
677
+ ** Input: n = number of equations
678
+ ** Aii = diagonal entries of solution matrix
679
+ ** Aij = non-zero off-diagonal entries of matrix
680
+ ** B = right hand side coeffs.
681
+ ** Output: B = solution values
682
+ ** returns 0 if solution found, or index of
683
+ ** equation causing system to be ill-conditioned
684
+ ** Purpose: solves sparse symmetric system of linear
685
+ ** equations using Cholesky factorization
686
+ **
687
+ ** NOTE: This procedure assumes that the solution matrix has
688
+ ** been symbolically factorized with the positions of
689
+ ** the lower triangular, off-diagonal, non-zero coeffs.
690
+ ** stored in the following integer arrays:
691
+ ** XLNZ (start position of each column in NZSUB)
692
+ ** NZSUB (row index of each non-zero in each column)
693
+ ** LNZ (position of each NZSUB entry in Aij array)
694
+ **
695
+ ** This procedure has been adapted from subroutines GSFCT and
696
+ ** GSSLV in the book "Computer Solution of Large Sparse
697
+ ** Positive Definite Systems" by A. George and J. W-H Liu
698
+ ** (Prentice-Hall, 1981).
699
+ **--------------------------------------------------------------
700
+ */
701
+ {
702
+
703
+ int i, istop, istrt, isub, j, k, kfirst, newk;
704
+ int errcode = 0;
705
+ double bj, diagj, ljk;
706
+
707
+ memset(MSX.Dispersion.temp,0,(n+1)*sizeof(double));
708
+ memset(MSX.Dispersion.link,0,(n+1)*sizeof(int));
709
+
710
+ /* Begin numerical factorization of matrix A into L */
711
+ /* Compute column L(*,j) for j = 1,...n */
712
+ for (j=1; j<=n; j++)
713
+ {
714
+ /* For each column L(*,k) that affects L(*,j): */
715
+ diagj = 0.0;
716
+ newk = MSX.Dispersion.link[j];
717
+ k = newk;
718
+ while (k != 0)
719
+ {
720
+
721
+ /* Outer product modification of L(*,j) by */
722
+ /* L(*,k) starting at first[k] of L(*,k). */
723
+ newk = MSX.Dispersion.link[k];
724
+ kfirst = MSX.Dispersion.first[k];
725
+ ljk = Aij[MSX.Dispersion.LNZ[kfirst]];
726
+ diagj += ljk*ljk;
727
+ istrt = kfirst + 1;
728
+ istop = MSX.Dispersion.XLNZ[k+1] - 1;
729
+ if (istop >= istrt)
730
+ {
731
+
732
+ /* Before modification, update vectors 'first' */
733
+ /* and 'link' for future modification steps. */
734
+ MSX.Dispersion.first[k] = istrt;
735
+ isub = MSX.Dispersion.NZSUB[istrt];
736
+ MSX.Dispersion.link[k] = MSX.Dispersion.link[isub];
737
+ MSX.Dispersion.link[isub] = k;
738
+
739
+ /* The actual mod is saved in vector 'temp'. */
740
+ for (i=istrt; i<=istop; i++)
741
+ {
742
+ isub = MSX.Dispersion.NZSUB[i];
743
+ MSX.Dispersion.temp[isub] += Aij[MSX.Dispersion.LNZ[i]]*ljk;
744
+ }
745
+ }
746
+ k = newk;
747
+ }
748
+
749
+ /* Apply the modifications accumulated */
750
+ /* in 'temp' to column L(*,j). */
751
+ diagj = Aii[j] - diagj;
752
+ if (diagj <= 0.0) /* Check for ill-conditioning */
753
+ {
754
+ errcode = j;
755
+ return errcode;
756
+ }
757
+ diagj = sqrt(diagj);
758
+ Aii[j] = diagj;
759
+ istrt = MSX.Dispersion.XLNZ[j];
760
+ istop = MSX.Dispersion.XLNZ[j+1] - 1;
761
+ if (istop >= istrt)
762
+ {
763
+ MSX.Dispersion.first[j] = istrt;
764
+ isub = MSX.Dispersion.NZSUB[istrt];
765
+ MSX.Dispersion.link[j] = MSX.Dispersion.link[isub];
766
+ MSX.Dispersion.link[isub] = j;
767
+ for (i=istrt; i<=istop; i++)
768
+ {
769
+ isub = MSX.Dispersion.NZSUB[i];
770
+ bj = (Aij[MSX.Dispersion.LNZ[i]] - MSX.Dispersion.temp[isub])/diagj;
771
+ Aij[MSX.Dispersion.LNZ[i]] = bj;
772
+ MSX.Dispersion.temp[isub] = 0.0;
773
+ }
774
+ }
775
+ } /* next j */
776
+
777
+ /* Foward substitution */
778
+ for (j=1; j<=n; j++)
779
+ {
780
+ bj = B[j]/Aii[j];
781
+ B[j] = bj;
782
+ istrt = MSX.Dispersion.XLNZ[j];
783
+ istop = MSX.Dispersion.XLNZ[j+1] - 1;
784
+ if (istop >= istrt)
785
+ {
786
+ for (i=istrt; i<=istop; i++)
787
+ {
788
+ isub = MSX.Dispersion.NZSUB[i];
789
+ B[isub] -= Aij[MSX.Dispersion.LNZ[i]]*bj;
790
+ }
791
+ }
792
+ }
793
+
794
+ /* Backward substitution */
795
+ for (j=n; j>=1; j--)
796
+ {
797
+ bj = B[j];
798
+ istrt = MSX.Dispersion.XLNZ[j];
799
+ istop = MSX.Dispersion.XLNZ[j+1] - 1;
800
+ if (istop >= istrt)
801
+ {
802
+ for (i=istrt; i<=istop; i++)
803
+ {
804
+ isub = MSX.Dispersion.NZSUB[i];
805
+ bj -= Aij[MSX.Dispersion.LNZ[i]]*B[isub];
806
+ }
807
+ }
808
+ B[j] = bj/Aii[j];
809
+ }
810
+
811
+ return(errcode);
812
+ } /* End of msx_linsolve */
813
+
814
+
815
+ /************************ END OF SMATRIX.C ************************/
816
+