-
Notifications
You must be signed in to change notification settings - Fork 9
/
WaterLevel.cpp
400 lines (320 loc) · 11.2 KB
/
WaterLevel.cpp
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Copyright (c) 2013 Centre for Water Systems,
University of Exeter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
//! \file WaterLevel.cpp
//! contact: m.guidolin [at] exeter.ac.uk
//! \date 2013-03
#include"ArgsData.hpp"
#include"WaterLevel.hpp"
#include"Utilities.hpp"
#include<iostream>
#include<fstream>
// -------------------------//
// Include the CA 2D functions //
// -------------------------//
#include CA_2D_INCLUDE(computeArea)
#include CA_2D_INCLUDE(addRaise)
int initWLEventFromCSV(const std::string& filename, WLEvent& wle)
{
wle.u = 0.0;
wle.n = 0.01;
std::ifstream ifile(filename.c_str());
if(!ifile)
{
std::cerr<<"Error opening CSV file: "<<filename<<std::endl;
return 1;
}
// Parse the file line by line until the end of file
// and retrieve the tokens of each line.
while(!ifile.eof())
{
// If true the token was identified;
bool found_tok = false;
std::vector<std::string> tokens( CA::getLineTokens(ifile, ',') );
// If the tokens vector is empty we reached the eof or an
// empty line... continue.
if(tokens.empty())
continue;
if(CA::compareCaseInsensitive("Event Name",tokens[0],true))
{
std::string str;
READ_TOKEN(found_tok,str,tokens[1],tokens[0]);
wle.name = CA::trimToken(str," \t\r");
}
if(CA::compareCaseInsensitive("Water Level",tokens[0],true))
{
found_tok=true;
for (size_t i=1; i<tokens.size(); ++i)
{
CA::Real value;
READ_TOKEN(found_tok,value,tokens[i],tokens[0]);
wle.wls.push_back(value);
}
}
if(CA::compareCaseInsensitive("Time",tokens[0],true))
{
found_tok=true;
for (size_t i=1; i<tokens.size(); ++i)
{
CA::Real value;
READ_TOKEN(found_tok,value,tokens[i],tokens[0]);
wle.times.push_back(value);
}
}
if(CA::compareCaseInsensitive("Area",tokens[0],true))
{
found_tok=true;
for (size_t i=1; i<tokens.size(); ++i)
{
CA::Real value;
READ_TOKEN(found_tok,value,tokens[i],tokens[0]);
wle.area.push_back(value);
}
}
if(CA::compareCaseInsensitive("Zone",tokens[0],true))
{
found_tok=true;
for (size_t i=1; i<tokens.size(); ++i)
{
CA::Real value;
READ_TOKEN(found_tok,value,tokens[i],tokens[0]);
wle.zone.push_back(value);
}
}
if(CA::compareCaseInsensitive("Analytical Solution U",tokens[0],true))
{
found_tok=true;
READ_TOKEN(found_tok,wle.u,tokens[1],tokens[0]);
}
if(CA::compareCaseInsensitive("Analytical Solution N",tokens[0],true))
{
found_tok=true;
READ_TOKEN(found_tok,wle.n,tokens[1],tokens[0]);
}
// If the token was not identified stop!
if(!found_tok)
{
std::cerr<<"Element '"<<CA::trimToken(tokens[0])<<"' not identified"<<std::endl; \
return 1;
}
}
return 0;
}
WaterLevelManager::WaterLevelManager(CA::Grid& GRID, const std::vector<WLEvent>& wles):
_grid(GRID),
_wles(wles),
_datas(wles.size())
{
for(size_t i = 0; i<_wles.size(); ++i)
{
initData(_wles[i], _datas[i]);
}
}
WaterLevelManager::~WaterLevelManager()
{
}
//! Add the computational domain of the WaterLevel events into the given domain.
void WaterLevelManager::addDomain(CA::BoxList& compdomain)
{
for(size_t i = 0; i<_datas.size(); ++i)
{
compdomain.add(_datas[i].box_area);
}
}
void WaterLevelManager::analyseArea(CA::CellBuffReal& TMP, CA::CellBuffState& MASK, CA::BoxList& domain)
{
for(size_t i = 0; i<_datas.size(); ++i)
{
TMP.fill(domain, 0.0);
CA::Execute::function(_datas[i].box_area, computeArea, _grid, TMP, MASK);
TMP.sequentialOp(_datas[i].box_area, _datas[i].grid_area, CA::Seq::Add);
}
}
void WaterLevelManager::getElevation(CA::CellBuffReal& ELV)
{
// Loop through the WaterLevel event(s).
for(size_t i = 0; i<_wles.size(); ++i)
{
// Retrieve the minimum elevation of the given area.
ELV.sequentialOp(_datas[i].box_area, _datas[i].last_level, CA::Seq::MinAbs);
}
}
void WaterLevelManager::prepare(CA::Real t, CA::Real period_time_dt, CA::Real next_dt)
{
// Loop through the WaterLevel event(s).
for(size_t i = 0; i<_wles.size(); ++i)
{
// Set the volume to zero.
_datas[i].volume = 0.0;
size_t index = _datas[i].index;
// If the index is larger than the available rain/time, do
// nothing.
if(index >= _wles[i].wls.size() )
continue;
// Compute the water level at specific are using
// interpolation. Check if the index is the last available
// one. In this case use only one value.
CA::Real level = 0;
if(index == _wles[i].wls.size() -1)
{
level = _wles[i].wls[index];
}
else
{
CA::Real y0 = _wles[i].wls[index];
CA::Real y1 = _wles[i].wls[index+1];
CA::Real x0 = _wles[i].times[index];
CA::Real x1 = _wles[i].times[index+1];
level = y0 + (y1-y0) * ( ((t+period_time_dt) - x0)/(x1 - x0) );
}
// ATTENTION At the moment to work the last level must be the original elevation.
//_datas[i].last_level = level;
}
}
CA::Real WaterLevelManager::volume()
{
CA::Real wl_volume = 0.0;
// Loop through the water level event(s).
for(size_t i = 0; i<_wles.size(); ++i)
wl_volume += _datas[i].volume;
return wl_volume;
}
void WaterLevelManager::add(CA::CellBuffReal& WD, CA::CellBuffReal& ELV,
CA::CellBuffState& MASK, CA::Real t, CA::Real next_dt)
{
// Loop through the WaterLevel event(s).
for(size_t i = 0; i<_wles.size(); ++i)
{
// If the value of U is set (differ from zero). Then the
// analytical solution is compute with C=0
if(_wles[i].u!=0)
{
CA::Real pn = std::pow(_wles[i].n,2);
CA::Real pu = std::pow(_wles[i].u,3);
CA::Real level = std::pow((7.0/3.0)*(0-pn*pu*(-_wles[i].u*(t+next_dt))) ,(3.0/7.0));
// Given the way the CA2D model work, we need to set the water
// depth instead of the water level. Thus the water depth value
// at specific location is the value of the water level event
// minus the elevation.
CA::Execute::function(_datas[i].box_area, addRaise, _grid, WD, ELV, MASK, level);
continue;
}
size_t index = _datas[i].index;
// If the index is larger than the available rain/time, do
// nothing.
if(index >= _wles[i].wls.size() )
continue;
// Compute the water level at specific are using
// interpolation. Check if the index is the last available
// one. In this case use only one value.
CA::Real level = 0;
if(index == _wles[i].wls.size() -1)
{
level = _wles[i].wls[index];
}
else
{
CA::Real y0 = _wles[i].wls[index];
CA::Real y1 = _wles[i].wls[index+1];
CA::Real x0 = _wles[i].times[index];
CA::Real x1 = _wles[i].times[index+1];
level = y0 + (y1-y0) * ( (t - x0)/(x1 - x0) );
}
// Given the way the CA2D model work, we need to set the water
// depth instead of the water level. Thus the water depth value
// at specific location is the value of the water level event
// minus the elevation.
CA::Execute::function(_datas[i].box_area, addRaise, _grid, WD, ELV, MASK, level);
// Check if the simulation time now is equal or higher than the
// time of the NEXT index.
if(t >= _wles[i].times[index+1])
index++;
// Update index.
_datas[i].index = index;
}
}
CA::Real WaterLevelManager::potentialVA(CA::Real t, CA::Real period_time_dt)
{
CA::Real potential_va = 0.0;
// Use the differnce in water level between the previous water level
// set and the new one that is going to happen. If it is the first
// call then the previous level is the elevation value.
// Loop through the water level event(s).
for(size_t i = 0; i<_wles.size(); ++i)
{
size_t index = _datas[i].index;
// If the index is larger than the available time, do
// nothing.
if(index >= _wles[i].wls.size() )
continue;
// Compute the water level at specific are using
// interpolation. Check if the index is the last available
// one. In this case use only one value.
CA::Real level = 0;
if(index == _wles[i].wls.size() -1)
{
level = _wles[i].wls[index];
}
else
{
CA::Real y0 = _wles[i].wls[index];
CA::Real y1 = _wles[i].wls[index+1];
CA::Real x0 = _wles[i].times[index];
CA::Real x1 = _wles[i].times[index+1];
level = y0 + (y1-y0) * ( ((t+period_time_dt) - x0)/(x1 - x0) );
}
// Retrieve the difference from the previous water level and use
// diss difference to compute the critical velocity.
CA::Real water_diff = std::abs(level - _datas[i].last_level);
potential_va = std::max(potential_va, std::sqrt( static_cast<CA::Real>(9.81)*( water_diff) ) );
}
// ATTENTION! This does not need to be precise but just give a rough estimation
return potential_va;
}
CA::Real WaterLevelManager::endTime()
{
CA::Real t_end = 0.0;
// Loop through the water level event(s).
for(size_t i = 0; i<_wles.size(); ++i)
{
t_end = std::max(t_end,_wles[i].times.back());
}
return t_end;
}
int WaterLevelManager::initData(const WLEvent& wle, Data& data)
{
data.index = 0;
// If the area vector does not contain four values, then the box
// stay empty. NO WATER LEVEL.
if(wle.area.size()==4)
{
// Compute the given box area.
data.box_area = CA::Box::create(_grid, wle.area[0], wle.area[1], wle.area[2], wle.area[3]);
}
// This is new. The area could have been identified as a zone, i.e. x,y,w,h/
if(wle.zone.size()==4)
{
// Compute the given box area from the zone.
CA::Point tl( CA::Point::create(_grid, wle.zone[0], wle.zone[1]) );
CA::Unsigned w = static_cast<CA::Unsigned>( std::ceil(wle.zone[2] / _grid.length()) );
CA::Unsigned h = static_cast<CA::Unsigned>( std::ceil(wle.zone[3] / _grid.length()) );
data.box_area = CA::Box(tl.x(),tl.y(),w,h);
}
return 0;
}