-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydraul.c
2486 lines (2183 loc) · 81.6 KB
/
hydraul.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*********************************************************************
HYDRAUL.C -- Hydraulic Simulator for EPANET Program
VERSION: 2.00
DATE: 6/5/00
9/7/00
10/25/00
12/29/00
3/1/01
11/19/01
6/24/02
8/15/07 (2.00.11)
2/14/08 (2.00.12)
AUTHOR: L. Rossman
US EPA - NRMRL
This module contains the network hydraulic simulator.
It simulates the network's hydraulic behavior over an
an extended period of time and writes its results to the
binary file HydFile.
The entry points for this module are:
openhyd() -- called from ENopenH() in EPANET.C
inithyd() -- called from ENinitH() in EPANET.C
runhyd() -- called from ENrunH() in EPANET.C
nexthyd() -- called from ENnextH() in EPANET.C
closehyd() -- called from ENcloseH() in EPANET.C
tankvolume() -- called from ENsetnodevalue() in EPANET.C
setlinkstatus(),
setlinksetting(),
resistance()-- all called from ENsetlinkvalue() in EPANET.C
External functions called by this module are:
createsparse() -- see SMATRIX.C
freesparse() -- see SMATRIX.C
linsolve() -- see SMATRIX.C
checkrules() -- see RULES.C
interp() -- see EPANET.C
savehyd() -- see OUTPUT.C
savehydstep() -- see OUTPUT.C
writehydstat() -- see REPORT.C
writehyderr() -- see REPORT.C
writehydwarn() -- see REPORT.C
*******************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "hash.h"
#include "text.h"
#include "types.h"
#include "funcs.h"
#define EXTERN extern
#include "vars.h"
#define QZERO 1.e-6 /* Equivalent to zero flow */
#define CBIG 1.e8 /* Big coefficient */
#define CSMALL 1.e-6 /* Small coefficient */
/* Constants used for computing Darcy-Weisbach friction factor */
#define A1 0.314159265359e04 /* 1000*PI */
#define A2 0.157079632679e04 /* 500*PI */
#define A3 0.502654824574e02 /* 16*PI */
#define A4 6.283185307 /* 2*PI */
#define A8 4.61841319859 /* 5.74*(PI/4)^.9 */
#define A9 -8.685889638e-01 /* -2/ln(10) */
#define AA -1.5634601348 /* -2*.9*2/ln(10) */
#define AB 3.28895476345e-03 /* 5.74/(4000^.9) */
#define AC -5.14214965799e-03 /* AA*AB */
/*** Updated 3/1/01 ***/
/* Flag used to halt taking further time steps */
int Haltflag;
/* Relaxation factor used for updating flow changes */ //(2.00.11 - LR)
double RelaxFactor; //(2.00.11 - LR)
/* Function to find flow coeffs. through open/closed valves */ //(2.00.11 - LR)
void valvecoeff(int k); //(2.00.11 - LR)
int openhyd()
/*
*--------------------------------------------------------------
* Input: none
* Output: returns error code
* Purpose: opens hydraulics solver system
*--------------------------------------------------------------
*/
{
int i;
int errcode = 0;
ERRCODE(createsparse()); /* See SMATRIX.C */
ERRCODE(allocmatrix()); /* Allocate solution matrices */
for (i=1; i<=Nlinks; i++) /* Initialize flows */
initlinkflow(i,Link[i].Stat,Link[i].Kc);
return(errcode);
}
/*** Updated 3/1/01 ***/
void inithyd(int initflag)
/*
**--------------------------------------------------------------
** Input: initflag > 0 if link flows should be re-initialized
** = 0 if not
** Output: none
** Purpose: initializes hydraulics solver system
**--------------------------------------------------------------
*/
{
int i,j;
/* Initialize tanks */
for (i=1; i<=Ntanks; i++)
{
Tank[i].V = Tank[i].V0;
H[Tank[i].Node] = Tank[i].H0;
/*** Updated 10/25/00 ***/
D[Tank[i].Node] = 0.0;
OldStat[Nlinks+i] = TEMPCLOSED;
}
/* Initialize emitter flows */
memset(E,0,(Nnodes+1)*sizeof(double));
for (i=1; i<=Njuncs; i++)
if (Node[i].Ke > 0.0) E[i] = 1.0;
/* Initialize links */
for (i=1; i<=Nlinks; i++)
{
/* Initialize status and setting */
S[i] = Link[i].Stat;
K[i] = Link[i].Kc;
/* Start active control valves in ACTIVE position */ //(2.00.11 - LR)
if (
(Link[i].Type == PRV || Link[i].Type == PSV
|| Link[i].Type == FCV) //(2.00.11 - LR)
&& (Link[i].Kc != MISSING)
) S[i] = ACTIVE; //(2.00.11 - LR)
/*** Updated 3/1/01 ***/
/* Initialize flows if necessary */
if (S[i] <= CLOSED) Q[i] = QZERO;
else if (ABS(Q[i]) <= QZERO || initflag > 0)
initlinkflow(i, S[i], K[i]);
/* Save initial status */
OldStat[i] = S[i];
}
/* Reset pump energy usage */
for (i=1; i<=Npumps; i++)
{
for (j=0; j<6; j++) Pump[i].Energy[j] = 0.0;
}
/* Re-position hydraulics file */
if (Saveflag) fseek(HydFile,HydOffset,SEEK_SET);
/*** Updated 3/1/01 ***/
/* Initialize current time */
Haltflag = 0;
Htime = 0;
Hydstep = 0;
Rtime = Rstep;
}
int runhyd(long *t)
/*
**--------------------------------------------------------------
** Input: none
** Output: t = pointer to current time (in seconds)
** Returns: error code
** Purpose: solves network hydraulics in a single time period
**--------------------------------------------------------------
*/
{
int iter; /* Iteration count */
int errcode; /* Error code */
double relerr; /* Solution accuracy */
/* Find new demands & control actions */
*t = Htime;
demands();
controls();
/* Solve network hydraulic equations */
errcode = netsolve(&iter,&relerr);
if (!errcode)
{
/* Report new status & save results */
if (Statflag) writehydstat(iter,relerr);
/*** Updated 3/1/01 ***/
/* If system unbalanced and no extra trials */
/* allowed, then activate the Haltflag. */
if (relerr > Hacc && ExtraIter == -1) Haltflag = 1;
/* Report any warning conditions */
if (!errcode) errcode = writehydwarn(iter,relerr);
}
return(errcode);
} /* end of runhyd */
int nexthyd(long *tstep)
/*
**--------------------------------------------------------------
** Input: none
** Output: tstep = pointer to time step (in seconds)
** Returns: error code
** Purpose: finds length of next time step & updates tank
** levels and rule-based contol actions
**--------------------------------------------------------------
*/
{
long hydstep; /* Actual time step */
int errcode = 0; /* Error code */
/*** Updated 3/1/01 ***/
/* Save current results to hydraulics file and */
/* force end of simulation if Haltflag is active */
if (Saveflag) errcode = savehyd(&Htime);
if (Haltflag) Htime = Dur;
/* Compute next time step & update tank levels */
*tstep = 0;
hydstep = 0;
if (Htime < Dur) hydstep = timestep();
if (Saveflag) errcode = savehydstep(&hydstep);
/* Compute pumping energy */
if (Dur == 0) addenergy(0);
else if (Htime < Dur) addenergy(hydstep);
/* Update current time. */
if (Htime < Dur) /* More time remains */
{
Htime += hydstep;
if (Htime >= Rtime) Rtime += Rstep;
}
else
{
Htime++; /* Force completion of analysis */
}
*tstep = hydstep;
return(errcode);
}
void closehyd()
/*
**--------------------------------------------------------------
** Input: none
** Output: returns error code
** Purpose: closes hydraulics solver system
**--------------------------------------------------------------
*/
{
freesparse(); /* see SMATRIX.C */
freematrix();
}
int allocmatrix()
/*
**--------------------------------------------------------------
** Input: none
** Output: returns error code
** Purpose: allocates memory used for solution matrix coeffs.
**--------------------------------------------------------------
*/
{
int errcode = 0;
Aii = (double *) calloc(Nnodes+1,sizeof(double));
Aij = (double *) calloc(Ncoeffs+1,sizeof(double));
F = (double *) calloc(Nnodes+1,sizeof(double));
E = (double *) calloc(Nnodes+1,sizeof(double));
P = (double *) calloc(Nlinks+1,sizeof(double));
Y = (double *) calloc(Nlinks+1,sizeof(double));
X = (double *) calloc(MAX((Nnodes+1),(Nlinks+1)),sizeof(double));
OldStat = (char *) calloc(Nlinks+Ntanks+1, sizeof(char));
ERRCODE(MEMCHECK(Aii));
ERRCODE(MEMCHECK(Aij));
ERRCODE(MEMCHECK(F));
ERRCODE(MEMCHECK(E));
ERRCODE(MEMCHECK(P));
ERRCODE(MEMCHECK(Y));
ERRCODE(MEMCHECK(X));
ERRCODE(MEMCHECK(OldStat));
return(errcode);
} /* end of allocmatrix */
void freematrix()
/*
**--------------------------------------------------------------
** Input: none
** Output: none
** Purpose: frees memory used for solution matrix coeffs.
**--------------------------------------------------------------
*/
{
free(Aii);
free(Aij);
free(F);
free(E);
free(P);
free(Y);
free(X);
free(OldStat);
} /* end of freematrix */
void initlinkflow(int i, char s, double k)
/*
**--------------------------------------------------------------------
** Input: i = link index
** s = link status
** k = link setting (i.e., pump speed)
** Output: none
** Purpose: sets initial flow in link to QZERO if link is closed,
** to design flow for a pump, or to flow at velocity of
** 1 fps for other links.
**--------------------------------------------------------------------
*/
{
if (s == CLOSED) Q[i] = QZERO;
else if (Link[i].Type == PUMP) Q[i] = k*Pump[PUMPINDEX(i)].Q0;
else Q[i] = PI*SQR(Link[i].Diam)/4.0;
}
/*** Updated 9/7/00 ***/
void setlinkflow(int k, double dh)
/*
**--------------------------------------------------------------
** Input: k = link index
** dh = headloss across link
** Output: none
** Purpose: sets flow in link based on current headloss
**--------------------------------------------------------------
*/
{
int i,p;
double h0;
double x,y;
switch (Link[k].Type)
{
case CV:
case PIPE:
/* For Darcy-Weisbach formula: */
/* use approx. inverse of formula. */
if (Formflag == DW)
{
x = -log(K[k]/3.7/Link[k].Diam);
y = sqrt(ABS(dh)/Link[k].R/1.32547);
Q[k] = x*y;
}
/* For Hazen-Williams or Manning formulas: */
/* use inverse of formula. */
else
{
x = ABS(dh)/Link[k].R;
y = 1.0/Hexp;
Q[k] = pow(x,y);
}
/* Change sign of flow to match sign of headloss */
if (dh < 0.0) Q[k] = -Q[k];
break;
case PUMP:
/* Convert headloss to pump head gain */
dh = -dh;
p = PUMPINDEX(k);
/* For custom pump curve, interpolate from curve */
if (Pump[p].Ptype == CUSTOM)
{
dh = -dh*Ucf[HEAD]/SQR(K[k]);
i = Pump[p].Hcurve;
Q[k] = interp(Curve[i].Npts,Curve[i].Y,Curve[i].X,
dh)*K[k]/Ucf[FLOW];
}
/* Otherwise use inverse of power curve */
else
{
h0 = -SQR(K[k])*Pump[p].H0;
x = pow(K[k],2.0-Pump[p].N);
x = ABS(h0-dh)/(Pump[p].R*x),
y = 1.0/Pump[p].N;
Q[k] = pow(x,y);
}
break;
}
}
void setlinkstatus(int index, char value, char *s, double *k)
/*----------------------------------------------------------------
** Input: index = link index
** value = 0 (CLOSED) or 1 (OPEN)
** s = pointer to link status
** k = pointer to link setting
** Output: none
** Purpose: sets link status to OPEN or CLOSED
**----------------------------------------------------------------
*/
{
/* Status set to open */
if (value == 1)
{
/* Adjust link setting for pumps & valves */
if (Link[index].Type == PUMP) *k = 1.0;
/*** Updated 9/7/00 ***/
if (Link[index].Type > PUMP
&& Link[index].Type != GPV) *k = MISSING;
/* Reset link flow if it was originally closed */
// if (*s <= CLOSED) initlinkflow(index, OPEN, *k);
*s = OPEN;
}
/* Status set to closed */
else if (value == 0)
{
/* Adjust link setting for pumps & valves */
if (Link[index].Type == PUMP) *k = 0.0;
/*** Updated 9/7/00 ***/
if (Link[index].Type > PUMP
&& Link[index].Type != GPV) *k = MISSING;
/* Reset link flow if it was originally open */
// if (*s > CLOSED) initlinkflow(index, CLOSED, *k);
*s = CLOSED;
}
}
void setlinksetting(int index, double value, char *s, double *k)
/*----------------------------------------------------------------
** Input: index = link index
** value = pump speed or valve setting
** s = pointer to link status
** k = pointer to link setting
** Output: none
** Purpose: sets pump speed or valve setting, adjusting link
** status and flow when necessary
**----------------------------------------------------------------
*/
{
/* For a pump, status is OPEN if speed > 0, CLOSED otherwise */
if (Link[index].Type == PUMP)
{
*k = value;
if (value > 0 && *s <= CLOSED)
{
*s = OPEN;
// initlinkflow(index, OPEN, value);
}
if (value == 0 && *s > CLOSED)
{
*s = CLOSED;
// initlinkflow(index, CLOSED, value);
}
}
/*** Updated 9/7/00 ***/
/* For FCV, activate it */
else if (Link[index].Type == FCV)
{
// if (*s <= CLOSED) initlinkflow(index, OPEN, value);
*k = value;
*s = ACTIVE;
}
/* Open closed control valve with fixed status (setting = MISSING) */
else
{
if (*k == MISSING && *s <= CLOSED)
{
// initlinkflow(index, OPEN, value);
*s = OPEN;
}
*k = value;
}
}
void resistance(int k)
/*
**--------------------------------------------------------------------
** Input: k = link index
** Output: none
** Purpose: computes link flow resistance
**--------------------------------------------------------------------
*/
{
double e,d,L;
Link[k].R = CSMALL;
//if (Link[k].Type == PIPE || Link[k].Type == CV) //(2.00.11 - LR)
switch (Link[k].Type)
{
/* Link is a pipe. Compute resistance based on headloss formula. */
/* Friction factor for D-W formula gets included during solution */
/* process in pipecoeff() function. */
case CV:
case PIPE:
e = Link[k].Kc; /* Roughness coeff. */
d = Link[k].Diam; /* Diameter */
L = Link[k].Len; /* Length */
switch(Formflag)
{
case HW: Link[k].R = 4.727*L/pow(e,Hexp)/pow(d,4.871);
break;
case DW: Link[k].R = L/2.0/32.2/d/SQR(PI*SQR(d)/4.0);
break;
case CM: Link[k].R = SQR(4.0*e/(1.49*PI*d*d))*
pow((d/4.0),-1.333)*L;
}
break;
/* Link is a pump. Use negligible resistance. */
case PUMP:
Link[k].R = CBIG; //CSMALL;
break;
/* Link is a valve. Compute resistance for open valve assuming */
/* length is 2*diameter and friction factor is 0.02. Use with */
/* other formulas as well since resistance should be negligible.*/
/*** This way of treating valve resistance has been deprecated ***/ //(2.00.11 - LR)
/*** since resulting resistance is not always negligible. ***/ //(2.00.11 - LR)
/*
default:
d = Link[k].Diam;
L = 2.0*d;
Link[k].R = 0.02*L/2.0/32.2/d/SQR(PI*SQR(d)/4.0);
break;
*/
}
}
void demands()
/*
**--------------------------------------------------------------------
** Input: none
** Output: none
** Purpose: computes demands at nodes during current time period
**--------------------------------------------------------------------
*/
{
int i,j,n;
long k,p;
double djunc, sum;
Pdemand demand;
/* Determine total elapsed number of pattern periods */
p = (Htime+Pstart)/Pstep;
/* Update demand at each node according to its assigned pattern */
Dsystem = 0.0; /* System-wide demand */
for (i=1; i<=Njuncs; i++)
{
sum = 0.0;
for (demand = Node[i].D; demand != NULL; demand = demand->next)
{
/*
pattern period (k) = (elapsed periods) modulus
(periods per pattern)
*/
j = demand->Pat;
k = p % (long) Pattern[j].Length;
djunc = (demand->Base)*Pattern[j].F[k]*Dmult;
if (djunc > 0.0) Dsystem += djunc;
sum += djunc;
}
D[i] = sum;
}
/* Update head at fixed grade nodes with time patterns. */
for (n=1; n<=Ntanks; n++)
{
if (Tank[n].A == 0.0)
{
j = Tank[n].Pat;
if (j > 0)
{
k = p % (long) Pattern[j].Length;
i = Tank[n].Node;
H[i] = Node[i].El*Pattern[j].F[k];
}
}
}
/* Update status of pumps with utilization patterns */
for (n=1; n<=Npumps; n++)
{
j = Pump[n].Upat;
if (j > 0)
{
i = Pump[n].Link;
k = p % (long) Pattern[j].Length;
setlinksetting(i, Pattern[j].F[k], &S[i], &K[i]);
}
}
} /* End of demands */
int controls()
/*
**---------------------------------------------------------------------
** Input: none
** Output: number of links whose setting changes
** Purpose: implements simple controls based on time or tank levels
**---------------------------------------------------------------------
*/
{
int i, k, n, reset, setsum;
double h, vplus;
double v1, v2;
double k1, k2;
char s1, s2;
/* Examine each control statement */
setsum = 0;
for (i=1; i<=Ncontrols; i++)
{
/* Make sure that link is defined */
reset = 0;
if ( (k = Control[i].Link) <= 0) continue;
/* Link is controlled by tank level */
if ((n = Control[i].Node) > 0 && n > Njuncs)
{
h = H[n];
vplus = ABS(D[n]);
v1 = tankvolume(n-Njuncs,h);
v2 = tankvolume(n-Njuncs,Control[i].Grade);
if (Control[i].Type == LOWLEVEL && v1 <= v2 + vplus)
reset = 1;
if (Control[i].Type == HILEVEL && v1 >= v2 - vplus)
reset = 1;
}
/* Link is time-controlled */
if (Control[i].Type == TIMER)
{
if (Control[i].Time == Htime) reset = 1;
}
/* Link is time-of-day controlled */
if (Control[i].Type == TIMEOFDAY)
{
if ((Htime + Tstart) % SECperDAY == Control[i].Time) reset = 1;
}
/* Update link status & pump speed or valve setting */
if (reset == 1)
{
if (S[k] <= CLOSED) s1 = CLOSED;
else s1 = OPEN;
s2 = Control[i].Status;
k1 = K[k];
k2 = k1;
if (Link[k].Type > PIPE) k2 = Control[i].Setting;
if (s1 != s2 || k1 != k2)
{
S[k] = s2;
K[k] = k2;
if (Statflag) writecontrolaction(k,i);
// if (s1 != s2) initlinkflow(k, S[k], K[k]);
setsum++;
}
}
}
return(setsum);
} /* End of controls */
long timestep(void)
/*
**----------------------------------------------------------------
** Input: none
** Output: returns time step until next change in hydraulics
** Purpose: computes time step to advance hydraulic simulation
**----------------------------------------------------------------
*/
{
long n,t,tstep;
/* Normal time step is hydraulic time step */
tstep = Hstep;
/* Revise time step based on time until next demand period */
n = ((Htime+Pstart)/Pstep) + 1; /* Next pattern period */
t = n*Pstep - Htime; /* Time till next period */
if (t > 0 && t < tstep) tstep = t;
/* Revise time step based on time until next reporting period */
t = Rtime - Htime;
if (t > 0 && t < tstep) tstep = t;
/* Revise time step based on smallest time to fill or drain a tank */
tanktimestep(&tstep);
/* Revise time step based on smallest time to activate a control */
controltimestep(&tstep);
/* Evaluate rule-based controls (which will also update tank levels) */
if (Nrules > 0) ruletimestep(&tstep);
else tanklevels(tstep);
return(tstep);
}
void tanktimestep(long *tstep)
/*
**-----------------------------------------------------------------
** Input: *tstep = current time step
** Output: *tstep = modified current time step
** Purpose: revises time step based on shortest time to fill or
** drain a tank
**-----------------------------------------------------------------
*/
{
int i,n;
double h,q,v;
long t;
/* (D[n] is net flow rate into (+) or out of (-) tank at node n) */
for (i=1; i<=Ntanks; i++)
{
if (Tank[i].A == 0.0) continue; /* Skip reservoirs */
n = Tank[i].Node;
h = H[n]; /* Current tank grade */
q = D[n]; /* Flow into tank */
if (ABS(q) <= QZERO) continue;
if (q > 0.0 && h < Tank[i].Hmax)
{
v = Tank[i].Vmax - Tank[i].V; /* Volume to fill */
}
else if (q < 0.0 && h > Tank[i].Hmin)
{
v = Tank[i].Vmin - Tank[i].V; /* Volume to drain (-) */
}
else continue;
t = (long)ROUND(v/q); /* Time to fill/drain */
if (t > 0 && t < *tstep) *tstep = t;
}
}
void controltimestep(long *tstep)
/*
**------------------------------------------------------------------
** Input: *tstep = current time step
** Output: *tstep = modified current time step
** Purpose: revises time step based on shortest time to activate
** a simple control
**------------------------------------------------------------------
*/
{
int i,j,k,n;
double h,q,v;
long t,t1,t2;
for (i=1; i<=Ncontrols; i++)
{
t = 0;
if ( (n = Control[i].Node) > 0) /* Node control: */
{
if ((j = n-Njuncs) <= 0) continue; /* Node is a tank */
h = H[n]; /* Current tank grade */
q = D[n]; /* Flow into tank */
if (ABS(q) <= QZERO) continue;
if
( (h < Control[i].Grade &&
Control[i].Type == HILEVEL && /* Tank below hi level */
q > 0.0) /* & is filling */
|| (h > Control[i].Grade &&
Control[i].Type == LOWLEVEL && /* Tank above low level */
q < 0.0) /* & is emptying */
)
{ /* Time to reach level */
v = tankvolume(j,Control[i].Grade)-Tank[j].V;
t = (long)ROUND(v/q);
}
}
if (Control[i].Type == TIMER) /* Time control: */
{
if (Control[i].Time > Htime)
t = Control[i].Time - Htime;
}
if (Control[i].Type == TIMEOFDAY) /* Time-of-day control: */
{
t1 = (Htime + Tstart) % SECperDAY;
t2 = Control[i].Time;
if (t2 >= t1) t = t2 - t1;
else t = SECperDAY - t1 + t2;
}
if (t > 0 && t < *tstep) /* Revise time step */
{
/* Check if rule actually changes link status or setting */
k = Control[i].Link;
if (
(Link[k].Type > PIPE && K[k] != Control[i].Setting) ||
(S[k] != Control[i].Status)
)
*tstep = t;
}
}
} /* End of timestep */
void ruletimestep(long *tstep)
/*
**--------------------------------------------------------------
** Input: *tstep = current time step (sec)
** Output: *tstep = modified time step
** Purpose: updates next time step by checking if any rules
** will fire before then; also updates tank levels.
**--------------------------------------------------------------
*/
{
long tnow, /* Start of time interval for rule evaluation */
tmax, /* End of time interval for rule evaluation */
dt, /* Normal time increment for rule evaluation */
dt1; /* Actual time increment for rule evaluation */
/* Find interval of time for rule evaluation */
tnow = Htime;
tmax = tnow + *tstep;
/* If no rules, then time increment equals current time step */
if (Nrules == 0)
{
dt = *tstep;
dt1 = dt;
}
/* Otherwise, time increment equals rule evaluation time step and */
/* first actual increment equals time until next even multiple of */
/* Rulestep occurs. */
else
{
dt = Rulestep;
dt1 = Rulestep - (tnow % Rulestep);
}
/* Make sure time increment is no larger than current time step */
dt = MIN(dt, *tstep);
dt1 = MIN(dt1, *tstep);
if (dt1 == 0) dt1 = dt;
/* Step through time, updating tank levels, until either */
/* a rule fires or we reach the end of evaluation period. */
/*
** Note: we are updating the global simulation time (Htime)
** here because it is used by functions in RULES.C
** to evaluate rules when checkrules() is called.
** It is restored to its original value after the
** rule evaluation process is completed (see below).
** Also note that dt1 will equal dt after the first
** time increment is taken.
*/
do
{
Htime += dt1; /* Update simulation clock */
tanklevels(dt1); /* Find new tank levels */
if (checkrules(dt1)) break; /* Stop if rules fire */
dt = MIN(dt, tmax - Htime); /* Update time increment */
dt1 = dt; /* Update actual increment */
} while (dt > 0); /* Stop if no time left */
/* Compute an updated simulation time step (*tstep) */
/* and return simulation time to its original value */
*tstep = Htime - tnow;
Htime = tnow;
}
void addenergy(long hstep)
/*
**-------------------------------------------------------------
** Input: hstep = time step (sec)
** Output: none
** Purpose: accumulates pump energy usage
**-------------------------------------------------------------
*/
{
int i,j,k;
long m,n;
double c0,c, /* Energy cost (cost/kwh) */
f0, /* Energy cost factor */
dt, /* Time interval (hr) */
e, /* Pump efficiency (fraction) */
q, /* Pump flow (cfs) */
p, /* Pump energy (kw) */
psum = 0.0; /* Total energy (kw) */
/* Determine current time interval in hours */
if (Dur == 0) dt = 1.0;
else if (Htime < Dur) dt = (double) hstep / 3600.0;
else dt = 0.0;
if (dt == 0.0) return;
n = (Htime+Pstart)/Pstep;
/* Compute default energy cost at current time */
c0 = Ecost;
f0 = 1.0;
if (Epat > 0)
{
m = n % (long)Pattern[Epat].Length;
f0 = Pattern[Epat].F[m];
}
/* Examine each pump */
for (j=1; j<=Npumps; j++)
{
/* Skip closed pumps */
k = Pump[j].Link;
if (S[k] <= CLOSED) continue;
q = MAX(QZERO, ABS(Q[k]));
/* Find pump-specific energy cost */
if (Pump[j].Ecost > 0.0) c = Pump[j].Ecost;
else c = c0;
if ( (i = Pump[j].Epat) > 0)
{
m = n % (long)Pattern[i].Length;
c *= Pattern[i].F[m];
}
else c *= f0;
/* Find pump energy & efficiency */
getenergy(k,&p,&e);
psum += p;
/* Update pump's cumulative statistics */
Pump[j].Energy[0] += dt; /* Time on-line */
Pump[j].Energy[1] += e*dt; /* Effic.-hrs */
Pump[j].Energy[2] += p/q*dt; /* kw/cfs-hrs */
Pump[j].Energy[3] += p*dt; /* kw-hrs */
Pump[j].Energy[4] = MAX(Pump[j].Energy[4],p);
Pump[j].Energy[5] += c*p*dt; /* cost-hrs. */
}
/* Update maximum kw value */
Emax = MAX(Emax,psum);
} /* End of pumpenergy */
void getenergy(int k, double *kw, double *eff)
/*
**----------------------------------------------------------------
** Input: k = link index
** Output: *kw = kwatt energy used
** *eff = efficiency (pumps only)
** Purpose: computes flow energy associated with link k
**----------------------------------------------------------------
*/
{
int i,j;
double dh, q, e;
/*** Updated 6/24/02 ***/
/* No energy if link is closed */
if (S[k] <= CLOSED)
{
*kw = 0.0;
*eff = 0.0;
return;
}
/*** End of update ***/