-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtank.cpp
More file actions
272 lines (206 loc) · 6.69 KB
/
tank.cpp
File metadata and controls
272 lines (206 loc) · 6.69 KB
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
/* EPANET 3
*
* Copyright (c) 2016 Open Water Analytics
* Licensed under the terms of the MIT License (see the LICENSE file for details).
*
*/
#include "tank.h"
#include "curve.h"
#include "qualsource.h"
#include "Core/network.h"
#include "Core/constants.h"
#include "Core/error.h"
#include <algorithm>
using namespace std;
//-----------------------------------------------------------------------------
// Tank Constructor
Tank::Tank(string name_) :
Node(name_),
initHead(0.0),
minHead(0.0),
maxHead(0.0),
diameter(0.0),
minVolume(0.0),
bulkCoeff(MISSING),
volCurve(nullptr),
maxVolume(0.0),
volume(0.0),
area(0.0),
ucfLength(1.0),
pastHead(0.0),
pastVolume(0.0),
pastOutflow(0.0)
{
fullDemand = 0.0;
fixedGrade = true;
}
//-----------------------------------------------------------------------------
// Convert user's input units to internal units
void Tank::convertUnits(Network* nw)
{
// ... convert from user to internal units
ucfLength = nw->ucf(Units::LENGTH);
initHead /= ucfLength;
minHead /= ucfLength;
maxHead /= ucfLength;
diameter /= ucfLength;
area = PI * diameter * diameter / 4.0;
elev /= ucfLength;
minVolume /= nw->ucf(Units::VOLUME);
initQual /= nw->ucf(Units::CONCEN);
// ... assign default bulk reaction rate coeff.
if ( bulkCoeff == MISSING ) bulkCoeff = nw->option(Options::BULK_COEFF);
}
//-----------------------------------------------------------------------------
// Check that tank has valid data
void Tank::validate(Network* nw)
{
// ... check for enough info to compute volume
if ( diameter == 0.0 && volCurve == nullptr )
{
throw NetworkError(NetworkError::INVALID_VOLUME_CURVE, name);
}
// ... check that volume curve (depth v. volume in user units)
// covers range of depth limits
if ( volCurve )
{
if ( volCurve->size() < 2 )
{
throw NetworkError(NetworkError::INVALID_VOLUME_CURVE, name);
}
double tankHead = volCurve->x(0) / ucfLength + elev;
minHead = max(minHead, tankHead);
tankHead = volCurve->x(volCurve->size() - 1) / ucfLength + elev;
maxHead = min(maxHead, tankHead);
}
// ... check for consistent depth limits
if ( maxHead < minHead )
{
throw NetworkError(NetworkError::INVALID_TANK_LEVELS, name);
}
initHead = max(initHead, minHead);
initHead = min(initHead, maxHead);
}
//-----------------------------------------------------------------------------
// Initialize state of tank
void Tank::initialize(Network* nw)
{
head = initHead;
pastHead = initHead;
outflow = 0.0;
pastOutflow = 0.0;
quality = initQual;
if (qualSource) qualSource->quality = quality;
updateArea();
if ( volCurve ) minVolume = findVolume(minHead);
else if ( minVolume == 0.0 ) minVolume = (minHead - elev) * area;
volume = findVolume(head);
maxVolume = findVolume(maxHead);
fixedGrade = true;
}
//-----------------------------------------------------------------------------
// Compute tank volume from water surface elevation
double Tank::findVolume(double aHead)
{
// ... convert head to water depth
double depth = aHead - elev;
// ... tank has a volume curve (in original user units)
if ( volCurve )
{
// ... find slope and intercept of curve segment containing depth
depth *= ucfLength;
double slope, intercept;
volCurve->findSegment(depth, slope, intercept);
// ... compute volume and convert to ft3
double ucfArea = ucfLength * ucfLength;
return (slope * depth + intercept) / (ucfArea * ucfLength);
}
// ... tank is cylindrical
if ( minVolume > 0.0 ) depth = max(aHead - minHead, 0.0);
return minVolume + area * depth;
}
//-----------------------------------------------------------------------------
// Compute tank surface area from water depth
void Tank::updateArea()
{
// ... tank has a volume curve (in original user units)
if ( volCurve )
{
// ... find slope of curve segment containing depth
double slope, intercept;
double depth = head - elev;
volCurve->findSegment(depth*ucfLength, slope, intercept);
// ... curve segment slope (dV/dy) is avg. area over interval;
// convert to internal units
area = slope / ucfLength / ucfLength;
}
// ... area of cylindrical tank remains constant
}
//-----------------------------------------------------------------------------
// Compute water surface elevation from tank volume
double Tank::findHead(double aVolume)
{
// ... tank has a volume curve (in original user units)
if ( volCurve )
{
double ucfArea = ucfLength * ucfLength;
aVolume *= ucfArea * ucfLength;
return elev + volCurve->getXofY(aVolume) / ucfLength;
}
// ... tank is cylindrical
else
{
aVolume = max(0.0, aVolume - minVolume);
return minHead + aVolume / area;
}
}
//-----------------------------------------------------------------------------
// Update tank volume after a given time step
void Tank::updateVolume(int tstep)
{
// ... new volume based on current outflow
volume += outflow * tstep;
// ... check if min/max levels reached within an additional 1 second of flow
double v1 = volume + outflow;
if ( v1 <= minVolume )
{
volume = minVolume;
head = minHead;
}
else if ( v1 >= maxVolume )
{
volume = maxVolume;
head = maxHead;
}
// ... find head at new volume
else head = findHead(volume);
}
//-----------------------------------------------------------------------------
// Find time to fill (or empty) tank to a given volume
int Tank::timeToVolume(double v)
{
// ... make sure target volume is within bounds
v = max(v, minVolume);
v = min(v, maxVolume);
// ... make sure outflow is positive for filling or negative for emptying
if ( (v-volume) * outflow <= 0.0 ) return -1;
// ... required time is volume change over outflow rate
double t = (v - volume) / outflow;
return (int) (t + 0.5);
}
//-----------------------------------------------------------------------------
// Check if there is inflow to a full tank or outflow from an empty one
bool Tank::isClosed(double flow)
{
if ( !fixedGrade ) return false;
if ( head >= maxHead && flow < 0.0 ) return true;
if ( head <= minHead && flow > 0.0 ) return true;
return false;
}
//-----------------------------------------------------------------------------
// Find fixed grade water surface elevation
void Tank::setFixedGrade()
{
fixedGrade = true;
//head = findHead(volume);
}