-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathvalve.cpp
More file actions
491 lines (370 loc) · 12 KB
/
Copy pathvalve.cpp
File metadata and controls
491 lines (370 loc) · 12 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* EPANET 3
*
* Copyright (c) 2016 Open Water Analytics
* Licensed under the terms of the MIT License (see the LICENSE file for details).
*
*/
#include "valve.h"
#include "node.h"
#include "curve.h"
#include "Core/network.h"
#include "Core/constants.h"
#include "Models/headlossmodel.h"
#include <cmath>
#include <algorithm>
using namespace std;
const char* Valve::ValveTypeWords[] = {"PRV", "PSV", "FCV", "TCV", "PBV", "GPV"};
const double MIN_LOSS_COEFF = 0.1; // default minor loss coefficient
//-----------------------------------------------------------------------------
// Constructor/Destructor
Valve::Valve(string name_) :
Link(name_),
valveType(TCV),
lossFactor(0.0),
hasFixedStatus(false),
elev(0.0)
{
initStatus = VALVE_ACTIVE;
initSetting = 0;
}
Valve::~Valve() {}
//-----------------------------------------------------------------------------
// Return the string representation of the valve's type.
string Valve::typeStr()
{
return ValveTypeWords[valveType];
}
//-----------------------------------------------------------------------------
// Convert a valve's properties from user to internal units.
void Valve::convertUnits(Network* nw)
{
// ... convert diameter units
diameter /= nw->ucf(Units::DIAMETER);
// ... apply a minimum minor loss coeff. if necessary
double c = lossCoeff;
if ( c < MIN_LOSS_COEFF ) c = MIN_LOSS_COEFF;
// ... convert minor loss from V^2/2g basis to Q^2 basis
lossFactor = 0.02517 * c / pow(diameter, 4);
// ... convert initial valve setting units
initSetting = convertSetting(nw, initSetting);
}
//-----------------------------------------------------------------------------
// Convert the units of a valve's flow or pressure setting.
double Valve::convertSetting(Network* nw, double s)
{
switch (valveType)
{
// ... convert pressure valve setting
case PRV:
case PSV:
case PBV: s /= nw->ucf(Units::PRESSURE); break;
// ... convert flow valve setting
case FCV: s /= nw->ucf(Units::FLOW); break;
default: break;
}
if (valveType == PRV) elev = toNode->elev;
if (valveType == PSV) elev = fromNode->elev;
return s;
}
//-----------------------------------------------------------------------------
// Set a valve's initial status.
void Valve::setInitStatus(int s)
{
initStatus = s;
hasFixedStatus = true;
}
//-----------------------------------------------------------------------------
// Set a valve's initial setting.
void Valve::setInitSetting(double s)
{
initSetting = s;
initStatus = VALVE_ACTIVE;
hasFixedStatus = false;
}
//-----------------------------------------------------------------------------
// Initialize a valve's state.
void Valve::initialize(bool reInitFlow)
{
status = initStatus;
setting = initSetting;
if ( reInitFlow ) setInitFlow();
hasFixedStatus = (initStatus != VALVE_ACTIVE);
}
//-----------------------------------------------------------------------------
// Initialize a valve's flow rate.
void Valve::setInitFlow()
{
// ... flow at velocity of 1 ft/s
flow = PI * diameter * diameter / 4.0;
if (valveType == FCV)
{
flow = setting;
}
}
//-----------------------------------------------------------------------------
// Find the flow velocity through a valve.
double Valve::getVelocity()
{
double area = PI * diameter * diameter / 4.0;
return flow / area;
}
//-----------------------------------------------------------------------------
// Find the Reynolds Number of flow through a valve.
double Valve::getRe(const double q, const double viscos)
{
return abs(q) / (PI * diameter * diameter / 4.0) * diameter / viscos;
}
//-----------------------------------------------------------------------------
// Return a valve's setting in user units.
double Valve::getSetting(Network* nw)
{
switch(valveType)
{
case PRV:
case PSV:
case PBV: return setting * nw->ucf(Units::PRESSURE);
case FCV: return setting * nw->ucf(Units::FLOW);
default: return setting;
}
}
//-----------------------------------------------------------------------------
// Find a valve's head loss and its gradient.
void Valve::findHeadLoss(Network* nw, double q)
{
hLoss = 0.0;
hGrad = 0.0;
// ... valve is temporarily closed (e.g., tries to drain an empty tank)
if ( status == TEMP_CLOSED)
{
HeadLossModel::findClosedHeadLoss(q, hLoss, hGrad);
}
// ... valve has fixed status (OPEN or CLOSED)
else if ( hasFixedStatus )
{
if (status == LINK_CLOSED)
{
HeadLossModel::findClosedHeadLoss(q, hLoss, hGrad);
}
else if (status == LINK_OPEN) findOpenHeadLoss(q);
}
// ... head loss for active valves depends on valve type
else switch (valveType)
{
case PBV: findPbvHeadLoss(q); break;
case TCV: findTcvHeadLoss(q); break;
case GPV: findGpvHeadLoss(nw, q); break;
case FCV: findFcvHeadLoss(q); break;
// ... PRVs & PSVs without fixed status can be either
// OPEN, CLOSED, or ACTIVE.
case PRV:
case PSV:
if ( status == LINK_CLOSED )
{
HeadLossModel::findClosedHeadLoss(q, hLoss, hGrad);
}
else if ( status == LINK_OPEN ) findOpenHeadLoss(q);
break;
}
}
//-----------------------------------------------------------------------------
// Find the head loss and its gradient for a fully open valve.
void Valve::findOpenHeadLoss(double q)
{
hGrad = 2.0 * lossFactor * abs(q);
if ( hGrad < MIN_GRADIENT )
{
hGrad = MIN_GRADIENT;
hLoss = hGrad * q;
}
else hLoss = hGrad * q / 2.0;
}
//-----------------------------------------------------------------------------
// Find the head loss and its gradient for a pressure breaker valve.
void Valve::findPbvHeadLoss(double q)
{
// ... treat as open valve if minor loss > valve setting
double mloss = lossFactor * q * q;
if ( mloss >= abs(setting) ) findOpenHeadLoss(q);
// ... otherwise force head loss across valve equal to setting
else
{
hGrad = MIN_GRADIENT;
hLoss = setting;
}
}
//-----------------------------------------------------------------------------
// Find the head loss and its gradient for a throttle control valve.
void Valve::findTcvHeadLoss(double q)
{
//... save open valve loss factor
double f = lossFactor;
// ... convert throttled loss coeff. setting to a loss factor
double d2 = diameter * diameter;
lossFactor = 0.025173 * setting / d2 / d2;
// ... throttled loss coeff. can't be less than fully open coeff.
lossFactor = max(lossFactor, f);
// ... use the setting's loss factor to compute head loss
findOpenHeadLoss(q);
// ... restore open valve loss factor
lossFactor = f;
}
//-----------------------------------------------------------------------------
// Find the head loss and its derivative for a general purpose valve.
void Valve::findGpvHeadLoss(Network* nw, double q)
{
// ... retrieve head loss curve for valve
int curveIndex = (int)setting;
Curve* curve = nw->curve(curveIndex);
// ... retrieve units conversion factors (curve is in user's units)
double ucfFlow = nw->ucf(Units::FLOW);
double ucfHead = nw->ucf(Units::LENGTH);
// ... find slope (r) and intercept (h0) of curve segment
double qRaw = abs(q) * ucfFlow;
double r, h0;
curve->findSegment(qRaw, r, h0);
// ... convert to internal units
r *= ucfFlow / ucfHead;
h0 /= ucfHead;
// ... determine head loss and derivative for this curve segment
hGrad = r; //+ 2.0 * lossFactor * abs(q);
hLoss = h0 + r * abs(q); // + lossFactor * q * q;
if ( q < 0.0 ) hLoss = -hLoss;
}
//-----------------------------------------------------------------------------
// Find the head loss and its gradient for a flow control valve.
void Valve::findFcvHeadLoss(double q)
{
double xflow = q - setting; // flow in excess of the setting
// ... apply a large head loss factor to the flow excess
if (xflow > 0.0)
{
hLoss = lossFactor * setting * setting + HIGH_RESISTANCE * xflow;
hGrad = HIGH_RESISTANCE;
}
// ... otherwise treat valve as an open valve
else
{
if ( q < 0.0 )
{
HeadLossModel::findClosedHeadLoss(q, hLoss, hGrad);
}
else findOpenHeadLoss(q);
}
}
//-----------------------------------------------------------------------------
// Update a valve's status.
void Valve::updateStatus(double q, double h1, double h2)
{
if ( hasFixedStatus ) return;
int newStatus = status;
switch ( valveType )
{
case PRV: newStatus = updatePrvStatus(q, h1, h2); break;
case PSV: newStatus = updatePsvStatus(q, h1, h2); break;
default: break;
}
if ( newStatus != status )
{
if ( newStatus == Link::LINK_CLOSED ) flow = ZERO_FLOW;
status = newStatus;
}
}
//-----------------------------------------------------------------------------
// Update the status of a pressure reducing valve.
int Valve::updatePrvStatus(double q, double h1, double h2)
{
int s = status; // new valve status
double hset = setting + elev; // head setting
switch ( status )
{
case VALVE_ACTIVE:
if ( q < -ZERO_FLOW ) s = LINK_CLOSED;
else if ( h1 < hset ) s = LINK_OPEN;
break;
case LINK_OPEN:
if ( q < -ZERO_FLOW ) s = LINK_CLOSED;
else if ( h2 > hset ) s = VALVE_ACTIVE;
break;
case LINK_CLOSED:
if ( h1 > hset && h2 < hset ) s = VALVE_ACTIVE;
else if ( h1 < hset && h1 > h2 ) s = LINK_OPEN;
break;
}
return s;
}
//-----------------------------------------------------------------------------
// Update the status of a pressure sustaining valve.
int Valve::updatePsvStatus(double q, double h1, double h2)
{
int s = status; // new valve status
double hset = setting + elev; // head setting
switch (status)
{
case VALVE_ACTIVE:
if (q < -ZERO_FLOW ) s = LINK_CLOSED;
else if (h2 > hset ) s = LINK_OPEN;
break;
case LINK_OPEN:
if (q < -ZERO_FLOW ) s = LINK_CLOSED;
else if (h1 < hset ) s = VALVE_ACTIVE;
break;
case LINK_CLOSED:
if ( h2 < hset && h1 > hset) s = VALVE_ACTIVE;
else if ( h2 > hset && h1 > h2 ) s = LINK_OPEN;
break;
}
return s;
}
//-----------------------------------------------------------------------------
// Change the setting of a valve.
bool Valve::changeSetting(
double newSetting, bool makeChange, const string reason, ostream& msgLog)
{
if ( newSetting != setting )
{
if ( makeChange )
{
hasFixedStatus = false;
status = Link::LINK_OPEN;
msgLog << "\n " << reason;
setting = newSetting;
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Change the status of a valve.
bool Valve::changeStatus(
int newStatus, bool makeChange, const string reason, ostream& msgLog)
{
if ( !hasFixedStatus || status != newStatus )
{
if ( makeChange )
{
msgLog << "\n " << reason;
status = newStatus;
hasFixedStatus = true;
setting = 0.0; // reset setting when status is fixed
if ( status == LINK_CLOSED ) flow = ZERO_FLOW;
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Check for negative flow in a PRV/PSV valve (for debugging only)
void Valve::validateStatus(Network* nw, double qTol)
{
switch (valveType)
{
case PRV:
case PSV:
if (flow < -qTol)
{
nw->msgLog << "\nValve " << name << " flow = " << flow*nw->ucf(Units::FLOW);
}
break;
default: break;
}
}