-
Notifications
You must be signed in to change notification settings - Fork 9
/
Inflow.cpp
426 lines (337 loc) · 12.1 KB
/
Inflow.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
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 Inflow.cpp
//! contact: m.guidolin [at] exeter.ac.uk
//! \date 2013-03
#include"ca2D.hpp"
#include"ArgsData.hpp"
#include"Inflow.hpp"
#include"Utilities.hpp"
#include<iostream>
#include<fstream>
// -------------------------//
// Include the CA 2D functions //
// -------------------------//
#include CA_2D_INCLUDE(computeArea)
#include CA_2D_INCLUDE(addInflow)
int initIEventFromCSV(const std::string& filename, IEvent& ie)
{
ie.u = 0.0;
ie.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]);
ie.name = CA::trimToken(str," \t\r");
}
if(CA::compareCaseInsensitive("Inflow",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]);
ie.ins.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]);
ie.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]);
ie.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]);
ie.zone.push_back(value);
}
}
if(CA::compareCaseInsensitive("Analytical Solution U",tokens[0],true))
{
found_tok=true;
READ_TOKEN(found_tok,ie.u,tokens[1],tokens[0]);
}
if(CA::compareCaseInsensitive("Analytical Solution N",tokens[0],true))
{
found_tok=true;
READ_TOKEN(found_tok,ie.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;
}
InflowManager::InflowManager(CA::Grid& GRID, const std::vector<IEvent>& ies):
_grid(GRID),
_ies(ies),
_datas(ies.size())
{
for(size_t i = 0; i<_ies.size(); ++i)
{
initData(_ies[i], _datas[i]);
}
}
InflowManager::~InflowManager()
{
}
//! Add the computational domain of the inflow events into the given domain.
void InflowManager::addDomain(CA::BoxList& compdomain)
{
for(size_t i = 0; i<_datas.size(); ++i)
{
compdomain.add(_datas[i].box_area);
}
}
void InflowManager::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 InflowManager::prepare(CA::Real t, CA::Real period_time_dt, CA::Real next_dt)
{
// Loop through the inflow event(s).
for(size_t i = 0; i<_ies.size(); ++i)
{
// Compute the difference of inflow betwenn the expected and the
// added. This inflow should be added/subtracted as one off when it
// reaches high value.
_datas[i].one_off_inflow = (_datas[i].expected_inflow - _datas[i].total_inflow);
// Set the volume to zero.
_datas[i].volume = 0.0;
size_t index = _datas[i].index;
// If the index is larger than the available ins/time, do
// nothing.
if(index >= _ies[i].ins.size() )
continue;
// Compute the total volume for the period time.
CA::Real volume = 0;
if(index != _ies[i].ins.size() -1)
{
CA::Real y0 = _ies[i].ins[index];
CA::Real y1 = _ies[i].ins[index+1];
CA::Real x0 = _ies[i].times[index];
CA::Real x1 = _ies[i].times[index+1];
CA::Real t0 = t;
CA::Real t1 = t + period_time_dt;
CA::Real yt0= y0 + (y1-y0) * ( (t0 - x0)/(x1 - x0) );
CA::Real yt1= y0 + (y1-y0) * ( (t1 - x0)/(x1 - x0) );
volume = 0.5*(t1-t0)*(yt1-yt0)+(t1-t0)*(yt0);
}
_datas[i].volume = volume;
// The expected amount of inflow for the next period.
_datas[i].expected_inflow = volume;
// Reset the total amount of inflow for the next period.
_datas[i].total_inflow = 0.0;
}
}
CA::Real InflowManager::volume()
{
CA::Real inflow_volume = 0.0;
// Loop through the inflow event(s).
for(size_t i = 0; i<_ies.size(); ++i)
inflow_volume += _datas[i].volume;
return inflow_volume;
}
void InflowManager::add(CA::CellBuffReal& WD, CA::CellBuffState& MASK, CA::Real t, CA::Real dt)
{
// Loop through the inflow event(s).
for(size_t i = 0; i<_ies.size(); ++i)
{
// ANALYTICAl SOLUTION!
// If the value of U is set (differ from zero). Then the
// analytical solution is compute with C=0
if(_ies[i].u!=0)
{
CA::Real pn = std::pow(_ies[i].n,2);
CA::Real pu = std::pow(_ies[i].u,3);
CA::Real level_now = std::pow((7.0/3.0)*(0-pn*pu*(-_ies[i].u*(t))) ,(3.0/7.0));
CA::Real level_prev = std::pow((7.0/3.0)*(0-pn*pu*(-_ies[i].u*(t-dt))) ,(3.0/7.0));
CA::Real volume = _ies[i].u*((level_now+level_prev)/2)*_grid.length()*dt; // per cell!!!
CA::Execute::function(_datas[i].box_area, addInflow, _grid, WD, MASK, volume);
continue;
}
size_t index = _datas[i].index;
// If the index is larger than the available ins/time, do
// nothing.
if(index >= _ies[i].ins.size() )
continue;
// Compute the inflow volume at specific time using
// interpolation. Check if the index is the last available
// one, then there is no inflow.
CA::Real volume = 0;
if(index != _ies[i].ins.size() -1)
{
CA::Real y0 = _ies[i].ins[index];
CA::Real y1 = _ies[i].ins[index+1];
CA::Real x0 = _ies[i].times[index];
CA::Real x1 = _ies[i].times[index+1];
CA::Real t0 = t - dt;
CA::Real t1 = t;
CA::Real yt0= y0 + (y1-y0) * ( (t0 - x0)/(x1 - x0) );
CA::Real yt1= y0 + (y1-y0) * ( (t1 - x0)/(x1 - x0) );
volume = 0.5*(t1-t0)*(yt1-yt0)+(t1-t0)*(yt0);
}
// Increse the amount of inflow added into the period.
// It needs to be added before the correction.
_datas[i].total_inflow += volume;
// Correct the volume
volume = static_cast<double>(volume)+_datas[i].one_off_inflow;
_datas[i].one_off_inflow = 0.0; // Reset the one of inflow.
// ATTENTION The volume is the total volume, it need to be
// divided by the number of cells that are going to receive the
// inflow.
volume = volume/(_datas[i].grid_area/(_grid.area()));
// Add (or subtract) the given volume into the water detph of the
// given area.
// Do not add it if it is zero.
if(std::abs(volume)>=SMALL_INFLOW)
{
CA::Execute::function(_datas[i].box_area, addInflow, _grid, WD, MASK, volume);
}
// Check if the simulation time now is equal or higher than the
// time of the NEXT index.
if(t >= _ies[i].times[index+1])
index++;
// Update index.
_datas[i].index = index;
}
}
CA::Real InflowManager::potentialVA(CA::Real t, CA::Real period_time_dt)
{
CA::Real potential_va = 0.0;
// Compute the total amount of volume that is going to be added into
// each cell for the time period. Calculate the extra amout of
// water depth taht would be added in a second. Compute the possible velocity
// using the critical velocity.
for(size_t i = 0; i<_ies.size(); ++i)
{
size_t index = _datas[i].index;
// If the index is larger than the available ins/time, do
// nothing.
if(index >= _ies[i].ins.size() )
continue;
// Compute the total volume for the period time.
CA::Real volume = 0;
if(index != _ies[i].ins.size() -1)
{
CA::Real y0 = _ies[i].ins[index];
CA::Real y1 = _ies[i].ins[index+1];
CA::Real x0 = _ies[i].times[index];
CA::Real x1 = _ies[i].times[index+1];
CA::Real t0 = t;
CA::Real t1 = t + period_time_dt;
CA::Real yt0= y0 + (y1-y0) * ( (t0 - x0)/(x1 - x0) );
CA::Real yt1= y0 + (y1-y0) * ( (t1 - x0)/(x1 - x0) );
volume = 0.5*(t1-t0)*(yt1-yt0)+(t1-t0)*(yt0);
}
// ATTENTION The volume is the total volume, to get the volume per
// cell it need to be divided by the number of cells that are
// going to receive the inflow. Then it need to be divided by the
// cell area.
CA::Real wd = volume/(_datas[i].grid_area*period_time_dt);
// Compute the potential velocity.
potential_va = std::max(potential_va, std::sqrt(wd*static_cast<CA::Real>(9.81)));
}
// ATTENTION! This does not need to be precise but just give a rough estimation
return potential_va;
}
CA::Real InflowManager::endTime()
{
CA::Real t_end = 0.0;
// Loop through the inflow event(s).
for(size_t i = 0; i<_ies.size(); ++i)
{
// Loop through the time steps.
for(size_t j = 1; j<_ies[i].times.size(); j++)
{
// Check if there is no inflow at this time but there was at
// previous time updated end_t.
if(_ies[i].ins[j-1]>0.0 && _ies[i].ins[j]==0)
t_end = std::max(t_end,_ies[i].times[j]);
}
}
return t_end;
}
int InflowManager::initData(const IEvent& ie, Data& data)
{
data.index = 0;
// If the area vector does not contain four values, then the box
// stay empty. NO INFLOW.
if(ie.area.size()==4)
{
// Compute the given box area.
data.box_area = CA::Box::create(_grid, ie.area[0], ie.area[1], ie.area[2], ie.area[3]);
}
// This is new. The area could have been identified as a zone, i.e. x,y,w,h/
if(ie.zone.size()==4)
{
// Compute the given box area from the zone.
CA::Point tl( CA::Point::create(_grid, ie.zone[0], ie.zone[1]) );
CA::Unsigned w = static_cast<CA::Unsigned>( std::ceil(ie.zone[2] / _grid.length()) );
CA::Unsigned h = static_cast<CA::Unsigned>( std::ceil(ie.zone[3] / _grid.length()) );
data.box_area = CA::Box(tl.x(),tl.y(),w,h);
}
data.grid_area = 0.0;
data.volume = 0.0;
return 0;
}