-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater2D.C
387 lines (339 loc) · 13.4 KB
/
water2D.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Modified from Liu's code, THU
for linking with SWMM
*/
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
#include <sstream>
#include "fvCFD.H"
#include "pimpleControl.H"
#include "singlePhaseTransportModel.H"
#include "turbulenceModel.H"
#include "waterQualityModel.H"
#include "wallFvPatch.H"
#include "emptyFvPatch.H"
#include "volumeFluxInletVelocityFvPatchVectorField.H"
#include "OFstream.H"
#include "OFstream.C"
#include "regIOobjectWrite.C"
#include "water2D.H"
#include "swmm5.h"
#include <dlfcn.h>
struct Source{
friend Istream& operator>>(Istream& is, Source& xyd);
friend Ostream& operator<<(Ostream& os, const Source& xyd);
label position;
double value;
};
struct Gate{
friend Istream& operator>>(Istream& is, Gate& xyd);
friend Ostream& operator<<(Ostream& os, const Gate& xyd);
label top;
label bottom;
double opening;
double width;
};
struct Weir{
friend Istream& operator>>(Istream& is, Weir& xyd);
friend Ostream& operator<<(Ostream& os, const Weir& xyd);
label top;
label bottom;
double elevation;
double width;
};
// friend function for Source, Gate, Weir
Istream& operator>>(Istream& is, Source& xyd){
token firstToken(is);
if (firstToken.pToken() != token::BEGIN_LIST)
{
FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
<< "incorrect first token, expected '(' or '{', found "
<< firstToken.info()
<< exit(FatalIOError);
}
is >> xyd.position >> xyd.value;
token lastToken(is);
while(!(lastToken.pToken() == token::END_LIST))
{
is >> lastToken;
}
return is;
}
Ostream& operator<<(Ostream& os, const Source& xyd){
os << '(' << xyd.position << ' ' << xyd.value << ')';
return os;
}
Istream& operator>>(Istream& is, Gate& xyd){
token firstToken(is);
if (firstToken.pToken() != token::BEGIN_LIST)
{
FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
<< "incorrect first token, expected '(' or '{', found "
<< firstToken.info()
<< exit(FatalIOError);
}
is >> xyd.top >> xyd.bottom >> xyd.opening >> xyd.width;
token lastToken(is);
while(!(lastToken.pToken() == token::END_LIST))
{
is >> lastToken;
}
return is;
}
Ostream& operator<<(Ostream& os, const Gate& xyd){
os << '(' << xyd.top << ' ' << xyd.bottom << ' ' << xyd.opening << ' ' << xyd.width << ')';
return os;
}
Istream& operator>>(Istream& is, Weir& xyd){
token firstToken(is);
if (firstToken.pToken() != token::BEGIN_LIST)
{
FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
<< "incorrect first token, expected '(' or '{', found "
<< firstToken.info()
<< exit(FatalIOError);
}
is >> xyd.top >> xyd.bottom >> xyd.elevation >> xyd.width;
token lastToken(is);
while(!(lastToken.pToken() == token::END_LIST))
{
is >> lastToken;
}
return is;
}
Ostream& operator<<(Ostream& os, const Weir& xyd){
os << '(' << xyd.top << ' ' << xyd.bottom << ' ' << xyd.elevation << ' ' << xyd.width << ')';
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::validArgs.append("Rainfile");
argList::validArgs.append("SWMM_input");
argList::validArgs.append("SWMM_report");
argList::validArgs.append("SWMM_output");
# include "setRootCase.H"
# include "createTime.H"
# include "createMesh.H"
# include "createFields.H" // Time created first, Mesh created next, Field created finally
# include "computeWaterHeight.H"
# include "computeFrictionVelocity.H"
# include "readTimeControls.H"
pimpleControl pimple(mesh);
void *handle = dlopen("../platforms/linux64GccDPOpt/lib/libswmm5.so", RTLD_LAZY);
if (!handle){
Info << "\n Open DLL error\n" << endl;
Info << dlerror() << endl;
return -1;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
typedef int (*swmm_open_t)(char* f1, char *f2, char* f3);
typedef int (*swmm_start_t)(int saveResults);
typedef int (*swmm_step_t)(double* elapsedTime);
typedef int (*swmm_end_t)(void);
typedef int (*swmm_report_t)();
typedef int (*swmm_close_t)();
typedef int (*Swmm_link_t)(double* SWMM_DT);
typedef int (*Swmm_valid_t)(int NodeID, double* outflow);
typedef int (*Swmm_to_2D_t)(int* NodeID, double* Node_h,double* Cell_Q);
typedef int (*twoD_to_Swmm_t)(int* NodeID_j, double* Node_h);
swmm_open_t swmm_open = (swmm_open_t) dlsym(handle, "swmm_open");
swmm_start_t swmm_start = (swmm_start_t)dlsym(handle, "swmm_start");
swmm_step_t swmm_step = (swmm_step_t)dlsym(handle, "swmm_step");
swmm_end_t swmm_end = (swmm_end_t)dlsym(handle, "swmm_end");
swmm_report_t swmm_report = (swmm_report_t)dlsym(handle, "swmm_report");
swmm_close_t swmm_close = (swmm_close_t)dlsym(handle, "swmm_close");
Swmm_link_t Swmm_link = (Swmm_link_t)dlsym(handle, "Swmm_Link");
Swmm_valid_t Swmm_valid = (Swmm_valid_t)dlsym(handle, "Swmm_valid");
Swmm_to_2D_t Swmm_to_2D = (Swmm_to_2D_t)dlsym(handle, "Swmm_to_2D");
twoD_to_Swmm_t twoD_to_Swmm = (twoD_to_Swmm_t)dlsym(handle, "twoD_to_Swmm");
Info << "\n SWMM funcs are loaded successfully\n" << endl;
long newHour, oldHour = 0;
long theDay, theHour;
int minute = 0;
int line_index = 0;
char* arg_data = (char *)malloc((args[1].length()+1)*sizeof(char));
args[1].copy(arg_data, args[1].length(), 0);
std::ifstream rain_file(arg_data); //rainfile: "../run/constant/rainfall.txt"
free(arg_data);
std::string line;
double rain;
// result of outflow for validation
OFstream valid_file("validation.txt");
// initialize SWMM flags
static int IsOpenFlag = false;
static int IsStartedFlag = false;
static int SaveResultsFlag = false;
// open the files & read input data
int ErrorCode = 0;
char* arg_data_2 = (char *)malloc((args[2].length()+1)*sizeof(char));
args[2].copy(arg_data_2, args[2].length(), 0);
char* arg_data_3 = (char *)malloc((args[3].length()+1)*sizeof(char));
args[3].copy(arg_data_3, args[3].length(), 0);
char* arg_data_4 = (char *)malloc((args[4].length()+1)*sizeof(char));
args[4].copy(arg_data_4, args[4].length(), 0);
swmm_open(arg_data_2, arg_data_3, arg_data_4); // "swmm_part/swmm_part.inp", "swmm_part/swmm_part.rpt", "swmm_part/swmm_part.out"
free(arg_data_2);
free(arg_data_3);
free(arg_data_4);
if (!ErrorCode){
Info << "\n SWMM project opening\n" << endl;
swmm_start(true);
double elapsedTime = 0.0; // SWMM simulation time and also synchronization time, in decimal days (SWMM simulation requirement)
double deltaT_2D = runTime.deltaTValue();
double deltaT_SWMM = 0.0; // deltaT for SWMM simulation, in seconds
Swmm_link(&deltaT_SWMM); // get SWMM fixed routing step
Info << "The SWMM routing step is: " << deltaT_SWMM << "s\n" << endl;
Info << "The 2D simulation step is: " << deltaT_2D << "s\n" << endl;
int block_sync = deltaT_SWMM/deltaT_2D; // SWMM deltaT / 2D deltaT, synchronize every 30*2D deltaT
int block = block_sync;
if (!ErrorCode){
do{
Info<< "\nStarting time loop\n" << endl;
Info << "\n SWMM Simulation time = " << runTime.timeName() << nl << endl;
// read net rainfall
Info<< "Update cell's q from new rainfall\n" << endl;
while(std::getline(rain_file, line)){
if (line_index == minute){
std::istringstream line_stream(line);
line_stream >> rain;
forAll(cellVolumes, i){
q[i] += rain*alpha[i];
break;
}
}
else{
line_index++;
}
}
line_index = 0; // reset index of lines to 0
minute++;
// execute 2D simulation for 60s
while (runTime.run() && block>0 ){
Info<< "Water2D simulation time = " << runTime.timeName() << nl << endl;
#include "readTimeControls.H"
#include "CourantNo.H"
#include "setDeltaT.H"
// --- Pressure-velocity PIMPLE corrector loop start
while (pimple.loop()){ // Pimple = PISO + simple
// Momentum predictor
phiv = phi/fvc::interpolate(h/cZDdt); // phi: mass flux
fvVectorMatrix hUEqn
(
fvm::ddt(hU)
+ fvm::div(phiv, hU) // for diffusive wave simulation, e.g. dry and wet condition, just drop this term
+ turbulence->divR(hU)
);
hUEqn.relax();
if (pimple.momentumPredictor()){
solve(hUEqn == (-g * h * fvc::grad(zeta) - fvm::Sp(rCs * fricU / h, hU))) ;
#include "computeFrictionVelocity.H"
}
// --- Pressure corrector loop
while (pimple.correct()){
volScalarField rUA = 1.0 / (hUEqn.A() + rCs * fricU / h); // hUEqn.A() return the central coefficient of hUEqn
volScalarField ghrUAf = g * rUA * h;
hU = rUA * hUEqn.H(); // hUEqn.H() return the (L+U)\cdot u_0
phi = (fvc::interpolate(hU) & mesh.Sf())
+ fvc::interpolate(rUA)*fvc::ddtCorr(h, hU, phi);
while (pimple.correctNonOrthogonal()){
// water level corrector
volScalarField a = cZDdt;
fvScalarMatrix zetaEqn(
fvm::ddt(a, zeta) // use zeta to compute h
+ fvc::div(phi)
- fvm::laplacian(ghrUAf, zeta)
- q // add source term here?
);
zetaEqn.solve(mesh.solver(zeta.select(pimple.finalInnerIter())));
if (pimple.finalNonOrthogonalIter()){
phi += zetaEqn.flux();
}
}
#include "caculateGateFlux.H"
#include "caculateWeirFlux.H"
hU -= ghrUAf * fvc::grad(zeta);
hU.correctBoundaryConditions();
U.correctBoundaryConditions();
U = hU / h;
phi0 = phi;
#include "computeWaterHeight.H"
#include "computeFrictionVelocity.H"
}
} //pimple loop end
turbulence->correct();
//waterQuality->correct();
#include "computeFlux.H"
Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
<< " ClockTime = " << runTime.elapsedClockTime() << " s"
<< nl << endl;
runTime++;
block--;
} // end 2D simulation
// external inflow to SWMM according to old depth in conduits
forAll(cellVolumes,i){
int n = gully_num[i];
if (n != -1){
twoD_to_Swmm(&n, &zeta[i]);
}
}
// execute SWMM
swmm_step(&elapsedTime);
newHour = (long)(elapsedTime*24.0);
if (newHour > oldHour){
theDay = (long) elapsedTime;
theHour = (long)((elapsedTime - floor(elapsedTime))*24);
Info<< "the day: " << theDay << "the hour: " << theHour << "\n" << endl;
oldHour = newHour;
}
Info<< "the elapsed time: " << elapsedTime << "\n" << endl;
Info<< "SWMM steps successfully\n" << endl;
// outflow for validation
double outflow = 0;
Swmm_valid(30, &outflow);
valid_file<< "30 " << outflow << "\t";
Swmm_valid(19, &outflow);
valid_file<< "19 " << outflow << "\n";
Swmm_valid(40, &outflow);
valid_file<< "40 " << outflow << "\t";
Swmm_valid(41, &outflow);
valid_file<< "41 " << outflow << "\n";
// update water2D source term
Info<< "Update cell's q from new water level of conduits\n" << endl;
forAll(cellVolumes,i){
q[i] = 0; // initialize flow for next second simualtion
int n = gully_num[i];
double q_cell = 0;
if (n == -1){
continue;
}
else{
Swmm_to_2D(&n, &zeta[i], &q_cell);
q[i] += q_cell/cellVolumes[i];
if (n==30 || n==40 || n==41){
Info<< n << " " << outflow << endl;
}
}
}
runTime.writeNow(); // write 2D simulation result
block = block_sync;
}
while(elapsedTime>0.0 && !ErrorCode);
Info<< "End\n" << endl;
swmm_end();
swmm_report();
swmm_close();
}//SWMM end
else{
Info<< "SWMM can't be started\n" << endl;
}
}
else{
Info<< "SWMM can't be opened\n" << endl;
}
return 0;
}
// ************************************************************************* //