-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathlink.c
2692 lines (2344 loc) · 83.3 KB
/
link.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
//-----------------------------------------------------------------------------
// link.c
//
// Project: EPA SWMM5
// Version: 5.2
// Date: 06/12/23 (Build 5.2.4)
// Author: L. Rossman
// M. Tryby (EPA)
//
// Conveyance system link functions
//
// Update History
// ==============
// Build 5.1.007:
// - Optional surcharging of weirs introduced.
// Build 5.1.008:
// - Bug in finding flow through surcharged weir fixed.
// - Bug in finding if conduit is upstrm/dnstrm full fixed.
// - Monthly conductivity adjustment applied to conduit seepage.
// - Conduit seepage limited by conduit's flow rate.
// Build 5.1.010:
// - Support added for new ROADWAY_WEIR object.
// - Time of last setting change initialized for links.
// Build 5.1.011:
// - Crest elevation of regulator links raised to downstream invert.
// - Fixed converting roadWidth weir parameter to internal units.
// - Weir shape parameter deprecated.
// - Extra geometric parameters ignored for non-conduit open rectangular
// cross sections.
// Build 5.1.012:
// - Conduit seepage rate now based on flow width, not wetted perimeter.
// - Formula for side flow weir corrected.
// - Crest length contraction adjustments corrected.
// Build 5.1.013:
// - Maximum depth adjustments made for storage units that can surcharge.
// - Support added for head-dependent weir coefficient curves.
// - Adjustment of regulator link crest offset to match downstream node invert
// now only done for Dynamic Wave flow routing.
// Build 5.1.014:
// - Conduit evap. and seepage losses initialized to 0 in conduit_initState()
// and not allowed to exceed current flow rate in conduit_getLossRate().
// Build 5.2.0:
// - Support added for Streets and Inlets.
// - Support added for variable speed pumps.
// Build 5.2.1
// - Warning no longer issued when conduit elevation drop < MIN_DELTA_Z.
// Build 5.2.2:
// - Warning for conduit elevation drop < MIN_DELTA_Z restored.
// Build 5.2.4:
// - Conduit evap+seepage loss under DW routing limited by conduit volume.
//-----------------------------------------------------------------------------
#define _CRT_SECURE_NO_DEPRECATE
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "headers.h"
#include "inlet.h"
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
static const double MIN_DELTA_Z = 0.001; // minimum elevation change for conduit
// slopes (ft)
//-----------------------------------------------------------------------------
// External functions (declared in funcs.h)
//-----------------------------------------------------------------------------
// link_readParams (called by parseLine in input.c)
// link_readXsectParams (called by parseLine in input.c)
// link_readLossParams (called by parseLine in input.c)
// link_validate (called by project_validate in project.c)
// link_initState (called by initObjects in swmm5.c)
// link_setOldHydState (called by routing_execute in routing.c)
// link_setOldQualState (called by routing_execute in routing.c)
// link_setTargetSetting (called by routing_execute in routing.c)
// link_setSetting (called by routing_execute in routing.c)
// link_getResults (called by output_saveLinkResults)
// link_getLength (called in dwflow.c, kinwave.c & flowrout.c)
// link_getFroude (called in dwflow.c)
// link_getInflow (called in flowrout.c & dynwave.c)
// link_setOutfallDepth (called in flowrout.c & dynwave.c)
// link_getYcrit (called by link_setOutfallDepth & in dwflow.c)
// link_getYnorm (called by conduit_initState, link_setOutfallDepth & in dwflow.c)
// link_getVelocity (called by link_getResults & stats_updateLinkStats)
// link_getPower (called by stats_updateLinkStats in stats.c)
// link_getLossRate (called in dwflow.c, kinwave.c & flowrout.c)
//-----------------------------------------------------------------------------
// Local functions
//-----------------------------------------------------------------------------
static void link_setParams(int j, int type, int n1, int n2, int k, double x[]);
static void link_convertOffsets(int j);
static double link_getOffsetHeight(int j, double offset, double elev);
static int conduit_readParams(int j, int k, char* tok[], int ntoks);
static void conduit_validate(int j, int k);
static void conduit_initState(int j, int k);
static void conduit_reverse(int j, int k);
static double conduit_getLength(int j);
static double conduit_getLengthFactor(int j, int k, double roughness);
static double conduit_getSlope(int j);
static double conduit_getInflow(int j);
static double conduit_getLossRate(int j, int routeModel, double q,
double tstep);
static int pump_readParams(int j, int k, char* tok[], int ntoks);
static void pump_validate(int j, int k);
static void pump_initState(int j, int k);
static double pump_getInflow(int j);
static int orifice_readParams(int j, int k, char* tok[], int ntoks);
static void orifice_validate(int j, int k);
static void orifice_setSetting(int j, double tstep);
static double orifice_getWeirCoeff(int j, int k, double h);
static double orifice_getInflow(int j);
static double orifice_getFlow(int j, int k, double head, double f,
int hasFlapGate);
static int weir_readParams(int j, int k, char* tok[], int ntoks);
static void weir_validate(int j, int k);
static void weir_setSetting(int j);
static double weir_getInflow(int j);
static double weir_getOpenArea(int j, double y);
static void weir_getFlow(int j, int k, double head, double dir,
int hasFlapGate, double* q1, double* q2);
static double weir_getOrificeFlow(int j, double head, double y, double cOrif);
static double weir_getdqdh(int k, double dir, double h, double q1, double q2);
static int outlet_readParams(int j, int k, char* tok[], int ntoks);
static double outlet_getFlow(int k, double head);
static double outlet_getInflow(int j);
//=============================================================================
int link_readParams(int j, int type, int k, char* tok[], int ntoks)
//
// Input: j = link index
// type = link type code
// k = link type index
// tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads parameters for a specific type of link from a
// tokenized line of input data.
//
{
switch ( type )
{
case CONDUIT: return conduit_readParams(j, k, tok, ntoks);
case PUMP: return pump_readParams(j, k, tok, ntoks);
case ORIFICE: return orifice_readParams(j, k, tok, ntoks);
case WEIR: return weir_readParams(j, k, tok, ntoks);
case OUTLET: return outlet_readParams(j, k, tok, ntoks);
default: return 0;
}
}
//=============================================================================
int link_readXsectParams(char* tok[], int ntoks)
//
// Input: tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads a link's cross section parameters from a tokenized
// line of input data.
// Formats:
// Link Shape Geom1 Geom2 Geom3 Geom4 (Barrels Culvert)
// Link IRREGULAR TransectID
// Link STREET StreetID
//
{
int i, j, k;
double x[4];
// --- check for minimum number of tokens
if (ntoks < 3) return error_setInpError(ERR_ITEMS, "");
// --- get index of link
j = project_findObject(LINK, tok[0]);
if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]);
// --- get code of xsection shape
k = findmatch(tok[1], XsectTypeWords);
if ( k < 0 ) return error_setInpError(ERR_KEYWORD, tok[1]);
// --- assign default number of barrels to conduit
if ( Link[j].type == CONDUIT ) Conduit[Link[j].subIndex].barrels = 1;
// --- assume link is not a culvert
Link[j].xsect.culvertCode = 0;
// --- for irregular shape, find index of transect object
if ( k == IRREGULAR )
{
i = project_findObject(TRANSECT, tok[2]);
if ( i < 0 ) return error_setInpError(ERR_NAME, tok[2]);
Link[j].xsect.type = k;
Link[j].xsect.transect = i;
return 0;
}
// --- for street cross section, find index of Street object
else if (k == STREET_XSECT)
{
i = project_findObject(STREET, tok[2]);
if (i < 0) return error_setInpError(ERR_NAME, tok[2]);
Link[j].xsect.type = k;
Link[j].xsect.transect = i;
return 0;
}
else
{
// --- check that geometric parameters are present
if (ntoks < 6) return error_setInpError(ERR_ITEMS, "");
// --- parse max. depth & shape curve for a custom shape
if ( k == CUSTOM )
{
if ( !getDouble(tok[2], &x[0]) || x[0] <= 0.0 )
return error_setInpError(ERR_NUMBER, tok[2]);
i = project_findObject(CURVE, tok[3]);
if ( i < 0 ) return error_setInpError(ERR_NAME, tok[3]);
Link[j].xsect.type = k;
Link[j].xsect.transect = i;
Link[j].xsect.yFull = x[0] / UCF(LENGTH);
}
// --- parse and save geometric parameters
else for (i = 2; i <= 5; i++)
{
if ( !getDouble(tok[i], &x[i-2]) )
return error_setInpError(ERR_NUMBER, tok[i]);
}
// --- ignore extra parameters for non-conduit open rectangular shapes
if ( Link[j].type != CONDUIT && k == RECT_OPEN )
{
x[2] = 0.0;
x[3] = 0.0;
}
if ( !xsect_setParams(&Link[j].xsect, k, x, UCF(LENGTH)) )
{
return error_setInpError(ERR_NUMBER, "");
}
// --- parse number of barrels if present
if ( Link[j].type == CONDUIT && ntoks >= 7 )
{
i = atoi(tok[6]);
if ( i <= 0 ) return error_setInpError(ERR_NUMBER, tok[6]);
else Conduit[Link[j].subIndex].barrels = (char)i;
}
// --- parse culvert code if present
if ( Link[j].type == CONDUIT && ntoks >= 8 )
{
i = atoi(tok[7]);
if ( i < 0 ) return error_setInpError(ERR_NUMBER, tok[7]);
else Link[j].xsect.culvertCode = i;
}
}
return 0;
}
//=============================================================================
int link_readLossParams(char* tok[], int ntoks)
//
// Input: tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads local loss parameters for a link from a tokenized
// line of input data.
//
// Format: LinkID cInlet cOutlet cAvg FlapGate(YES/NO) SeepRate
//
{
int i, j, k;
double x[3];
double seepRate = 0.0;
if ( ntoks < 4 ) return error_setInpError(ERR_ITEMS, "");
j = project_findObject(LINK, tok[0]);
if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]);
for (i=1; i<=3; i++)
{
if ( ! getDouble(tok[i], &x[i-1]) || x[i-1] < 0.0 )
return error_setInpError(ERR_NUMBER, tok[i]);
}
k = 0;
if ( ntoks >= 5 )
{
k = findmatch(tok[4], NoYesWords);
if ( k < 0 ) return error_setInpError(ERR_KEYWORD, tok[4]);
}
if ( ntoks >= 6 )
{
if ( ! getDouble(tok[5], &seepRate) )
return error_setInpError(ERR_NUMBER, tok[5]);
}
Link[j].cLossInlet = x[0];
Link[j].cLossOutlet = x[1];
Link[j].cLossAvg = x[2];
Link[j].hasFlapGate = k;
Link[j].seepRate = seepRate / UCF(RAINFALL);
return 0;
}
//=============================================================================
void link_setParams(int j, int type, int n1, int n2, int k, double x[])
//
// Input: j = link index
// type = link type code
// n1 = index of upstream node
// n2 = index of downstream node
// k = index of link's sub-type
// x = array of parameter values
// Output: none
// Purpose: sets parameters for a link.
//
{
Link[j].node1 = n1;
Link[j].node2 = n2;
Link[j].type = type;
Link[j].subIndex = k;
Link[j].offset1 = 0.0;
Link[j].offset2 = 0.0;
Link[j].q0 = 0.0;
Link[j].qFull = 0.0;
Link[j].setting = 1.0;
Link[j].targetSetting = 1.0;
Link[j].hasFlapGate = 0;
Link[j].qLimit = 0.0; // 0 means that no limit is defined
Link[j].direction = 1;
switch (type)
{
case CONDUIT:
Conduit[k].length = x[0] / UCF(LENGTH);
Conduit[k].modLength = Conduit[k].length;
Conduit[k].roughness = x[1];
Link[j].offset1 = x[2] / UCF(LENGTH);
Link[j].offset2 = x[3] / UCF(LENGTH);
Link[j].q0 = x[4] / UCF(FLOW);
Link[j].qLimit = x[5] / UCF(FLOW);
break;
case PUMP:
Pump[k].pumpCurve = (int)x[0];
Link[j].hasFlapGate = FALSE;
Pump[k].initSetting = x[1];
Pump[k].yOn = x[2] / UCF(LENGTH);
Pump[k].yOff = x[3] / UCF(LENGTH);
Pump[k].xMin = 0.0;
Pump[k].xMax = 0.0;
break;
case ORIFICE:
Orifice[k].type = (int)x[0];
Link[j].offset1 = x[1] / UCF(LENGTH);
Link[j].offset2 = Link[j].offset1;
Orifice[k].cDisch = x[2];
Link[j].hasFlapGate = (x[3] > 0.0) ? 1 : 0;
Orifice[k].orate = x[4] * 3600.0;
break;
case WEIR:
Weir[k].type = (int)x[0];
Link[j].offset1 = x[1] / UCF(LENGTH);
Link[j].offset2 = Link[j].offset1;
Weir[k].cDisch1 = x[2];
Link[j].hasFlapGate = (x[3] > 0.0) ? 1 : 0;
Weir[k].endCon = x[4];
Weir[k].cDisch2 = x[5];
Weir[k].canSurcharge = (int)x[6];
Weir[k].roadWidth = x[7] / UCF(LENGTH);
Weir[k].roadSurface = (int)x[8];
Weir[k].cdCurve = (int)x[9];
break;
case OUTLET:
Link[j].offset1 = x[0] / UCF(LENGTH);
Link[j].offset2 = Link[j].offset1;
Outlet[k].qCoeff = x[1];
Outlet[k].qExpon = x[2];
Outlet[k].qCurve = (int)x[3];
Link[j].hasFlapGate = (x[4] > 0.0) ? 1 : 0;
Outlet[k].curveType = (int)x[5];
xsect_setParams(&Link[j].xsect, DUMMY, NULL, 0.0);
break;
}
}
//=============================================================================
void link_validate(int j)
//
// Input: j = link index
// Output: none
// Purpose: validates a link's properties.
//
{
int n;
if ( LinkOffsets == ELEV_OFFSET ) link_convertOffsets(j);
switch ( Link[j].type )
{
case CONDUIT: conduit_validate(j, Link[j].subIndex); break;
case PUMP: pump_validate(j, Link[j].subIndex); break;
case ORIFICE: orifice_validate(j, Link[j].subIndex); break;
case WEIR: weir_validate(j, Link[j].subIndex); break;
}
// --- check if crest of regulator opening < invert of downstream node
switch ( Link[j].type )
{
case ORIFICE:
case WEIR:
case OUTLET:
if ( Node[Link[j].node1].invertElev + Link[j].offset1 <
Node[Link[j].node2].invertElev )
{
if (RouteModel == DW)
{
Link[j].offset1 = Node[Link[j].node2].invertElev -
Node[Link[j].node1].invertElev;
report_writeWarningMsg(WARN10b, Link[j].ID);
}
else report_writeWarningMsg(WARN10a, Link[j].ID);
}
}
// --- force max. depth of end nodes to be >= link crown height
// at non-storage nodes
// --- skip pumps and bottom orifices
if ( Link[j].type == PUMP ||
(Link[j].type == ORIFICE &&
Orifice[Link[j].subIndex].type == BOTTOM_ORIFICE) ) return;
// --- extend upstream node's full depth to link's crown elevation
n = Link[j].node1;
if ( Node[n].type != STORAGE || Node[n].surDepth > 0.0 )
{
Node[n].fullDepth = MAX(Node[n].fullDepth,
Link[j].offset1 + Link[j].xsect.yFull);
}
// --- do same for downstream node only for conduit links
n = Link[j].node2;
if ( (Node[n].type != STORAGE || Node[n].surDepth > 0.0) &&
Link[j].type == CONDUIT )
{
Node[n].fullDepth = MAX(Node[n].fullDepth,
Link[j].offset2 + Link[j].xsect.yFull);
}
}
//=============================================================================
void link_convertOffsets(int j)
//
// Input: j = link index
// Output: none
// Purpose: converts offset elevations to offset heights for a link.
//
{
double elev;
elev = Node[Link[j].node1].invertElev;
Link[j].offset1 = link_getOffsetHeight(j, Link[j].offset1, elev);
if ( Link[j].type == CONDUIT )
{
elev = Node[Link[j].node2].invertElev;
Link[j].offset2 = link_getOffsetHeight(j, Link[j].offset2, elev);
}
else Link[j].offset2 = Link[j].offset1;
}
//=============================================================================
double link_getOffsetHeight(int j, double offset, double elev)
//
// Input: j = link index
// offset = link elevation offset (ft)
// elev = node invert elevation (ft)
// Output: returns offset distance above node invert (ft)
// Purpose: finds offset height for one end of a link.
//
{
if ( offset <= MISSING || Link[j].type == PUMP) return 0.0;
offset -= elev;
if ( offset >= 0.0 ) return offset;
if ( offset >= -MIN_DELTA_Z ) return 0.0;
report_writeWarningMsg(WARN03, Link[j].ID);
return 0.0;
}
//=============================================================================
void link_initState(int j)
//
// Input: j = link index
// Output: none
// Purpose: initializes a link's state variables at start of simulation.
//
{
int p;
// --- initialize hydraulic state
Link[j].oldFlow = Link[j].q0;
Link[j].newFlow = Link[j].q0;
Link[j].oldDepth = 0.0;
Link[j].newDepth = 0.0;
Link[j].oldVolume = 0.0;
Link[j].newVolume = 0.0;
Link[j].setting = 1.0;
Link[j].targetSetting = 1.0;
Link[j].timeLastSet = StartDate;
Link[j].inletControl = FALSE;
Link[j].normalFlow = FALSE;
if ( Link[j].type == CONDUIT ) conduit_initState(j, Link[j].subIndex);
if ( Link[j].type == PUMP ) pump_initState(j, Link[j].subIndex);
// --- initialize water quality state
for (p = 0; p < Nobjects[POLLUT]; p++)
{
Link[j].oldQual[p] = 0.0;
Link[j].newQual[p] = 0.0;
Link[j].totalLoad[p] = 0.0;
}
}
//=============================================================================
double link_getInflow(int j)
//
// Input: j = link index
// Output: returns link flow rate (cfs)
// Purpose: finds total flow entering a link during current time step.
//
{
if ( Link[j].setting == 0 ) return 0.0;
switch ( Link[j].type )
{
case CONDUIT: return conduit_getInflow(j);
case PUMP: return pump_getInflow(j);
case ORIFICE: return orifice_getInflow(j);
case WEIR: return weir_getInflow(j);
case OUTLET: return outlet_getInflow(j);
default: return node_getOutflow(Link[j].node1, j);
}
}
//=============================================================================
void link_setOldHydState(int j)
//
// Input: j = link index
// Output: none
// Purpose: replaces link's old hydraulic state values with current ones.
//
{
int k;
Link[j].oldDepth = Link[j].newDepth;
Link[j].oldFlow = Link[j].newFlow;
Link[j].oldVolume = Link[j].newVolume;
if ( Link[j].type == CONDUIT )
{
k = Link[j].subIndex;
Conduit[k].q1Old = Conduit[k].q1;
Conduit[k].q2Old = Conduit[k].q2;
}
}
//=============================================================================
void link_setOldQualState(int j)
//
// Input: j = link index
// Output: none
// Purpose: replaces link's old water quality state values with current ones.
//
{
int p;
for (p = 0; p < Nobjects[POLLUT]; p++)
{
Link[j].oldQual[p] = Link[j].newQual[p];
Link[j].newQual[p] = 0.0;
}
}
//=============================================================================
void link_setTargetSetting(int j)
//
// Input: j = link index
// Output: none
// Purpose: updates a link's target setting.
//
{
int k, n1;
if ( Link[j].type == PUMP )
{
k = Link[j].subIndex;
n1 = Link[j].node1;
Link[j].targetSetting = Link[j].setting;
if ( Pump[k].yOff > 0.0 &&
Link[j].setting > 0.0 &&
Node[n1].newDepth < Pump[k].yOff ) Link[j].targetSetting = 0.0;
if ( Pump[k].yOn > 0.0 &&
Link[j].setting == 0.0 &&
Node[n1].newDepth > Pump[k].yOn ) Link[j].targetSetting = 1.0;
}
}
//=============================================================================
void link_setSetting(int j, double tstep)
//
// Input: j = link index
// tstep = time step over which setting is adjusted
// Output: none
// Purpose: updates a link's setting as a result of a control action.
//
{
if ( Link[j].type == ORIFICE ) orifice_setSetting(j, tstep);
else if ( Link[j].type == WEIR ) weir_setSetting(j);
else Link[j].setting = Link[j].targetSetting;
}
//=============================================================================
int link_setFlapGate(int j, int n1, int n2, double q)
//
// Input: j = link index
// n1 = index of node on upstream end of link
// n2 = index of node on downstream end of link
// q = signed flow value (value and units don't matter)
// Output: returns TRUE if there is reverse flow through a flap gate
// associated with the link.
// Purpose: based on the sign of the flow, determines if a flap gate
// associated with the link should close or not.
//
{
int n = -1;
// --- check for reverse flow through link's flap gate
if ( Link[j].hasFlapGate )
{
if ( q * (double)Link[j].direction < 0.0 ) return TRUE;
}
// --- check for Outfall with flap gate node on inflow end of link
if ( q < 0.0 ) n = n2;
if ( q > 0.0 ) n = n1;
if ( n >= 0 &&
Node[n].type == OUTFALL &&
Outfall[Node[n].subIndex].hasFlapGate ) return TRUE;
return FALSE;
}
//=============================================================================
void link_getResults(int j, double f, float x[])
//
// Input: j = link index
// f = time weighting factor
// Output: x = array of weighted results
// Purpose: retrieves time-weighted average of old and new results for a link.
//
{
int p; // pollutant index
double y, // depth
q, // flow
u, // velocity
v, // volume
c; // capacity, setting or concentration
double f1 = 1.0 - f;
y = f1*Link[j].oldDepth + f*Link[j].newDepth;
q = f1*Link[j].oldFlow + f*Link[j].newFlow;
v = f1*Link[j].oldVolume + f*Link[j].newVolume;
u = link_getVelocity(j, q, y);
c = 0.0;
if (Link[j].type == CONDUIT)
{
if (Link[j].xsect.type != DUMMY)
c = xsect_getAofY(&Link[j].xsect, y) / Link[j].xsect.aFull;
}
else c = Link[j].setting;
// --- override time weighting for pump flow between on/off states
if (Link[j].type == PUMP && Link[j].oldFlow*Link[j].newFlow == 0.0)
{
if ( f >= f1 ) q = Link[j].newFlow;
else q = Link[j].oldFlow;
}
y *= UCF(LENGTH);
v *= UCF(VOLUME);
q *= UCF(FLOW) * (double)Link[j].direction;
u *= UCF(LENGTH) * (double)Link[j].direction;
x[LINK_DEPTH] = (float)y;
x[LINK_FLOW] = (float)q;
x[LINK_VELOCITY] = (float)u;
x[LINK_VOLUME] = (float)v;
x[LINK_CAPACITY] = (float)c;
if ( !IgnoreQuality ) for (p = 0; p < Nobjects[POLLUT]; p++)
{
c = f1*Link[j].oldQual[p] + f*Link[j].newQual[p];
x[LINK_QUAL+p] = (float)c;
}
}
//=============================================================================
void link_setOutfallDepth(int j)
//
// Input: j = link index
// Output: none
// Purpose: sets depth at outfall node connected to link j.
//
{
int k; // conduit index
int n; // outfall node index
double z; // invert offset height (ft)
double q; // flow rate (cfs)
double yCrit = 0.0; // critical flow depth (ft)
double yNorm = 0.0; // normal flow depth (ft)
// --- find which end node of link is an outfall
if ( Node[Link[j].node2].type == OUTFALL )
{
n = Link[j].node2;
z = Link[j].offset2;
}
else if ( Node[Link[j].node1].type == OUTFALL )
{
n = Link[j].node1;
z = Link[j].offset1;
}
else return;
// --- find both normal & critical depth for current flow
if ( Link[j].type == CONDUIT )
{
k = Link[j].subIndex;
q = fabs(Link[j].newFlow / Conduit[k].barrels);
yNorm = link_getYnorm(j, q);
yCrit = link_getYcrit(j, q);
}
// --- set new depth at node
node_setOutletDepth(n, yNorm, yCrit, z);
}
//=============================================================================
double link_getYcrit(int j, double q)
//
// Input: j = link index
// q = link flow rate (cfs)
// Output: returns critical depth (ft)
// Purpose: computes critical depth for given flow rate.
//
{
return xsect_getYcrit(&Link[j].xsect, q);
}
//=============================================================================
double link_getYnorm(int j, double q)
//
// Input: j = link index
// q = link flow rate (cfs)
// Output: returns normal depth (ft)
// Purpose: computes normal depth for given flow rate.
//
{
int k;
double s, a, y;
if ( Link[j].type != CONDUIT ) return 0.0;
if ( Link[j].xsect.type == DUMMY ) return 0.0;
q = fabs(q);
k = Link[j].subIndex;
if ( q > Conduit[k].qMax ) q = Conduit[k].qMax;
if ( q <= 0.0 ) return 0.0;
s = q / Conduit[k].beta;
a = xsect_getAofS(&Link[j].xsect, s);
y = xsect_getYofA(&Link[j].xsect, a);
return y;
}
//=============================================================================
double link_getLength(int j)
//
// Input: j = link index
// Output: returns length (ft)
// Purpose: finds true length of a link.
//
{
if ( Link[j].type == CONDUIT ) return conduit_getLength(j);
return 0.0;
}
//=============================================================================
double link_getVelocity(int j, double flow, double depth)
//
// Input: j = link index
// flow = link flow rate (cfs)
// depth = link flow depth (ft)
// Output: returns flow velocity (fps)
// Purpose: finds flow velocity given flow and depth.
//
{
double area;
double veloc = 0.0;
int k;
if ( depth <= 0.01 ) return 0.0;
if ( Link[j].type == CONDUIT )
{
k = Link[j].subIndex;
flow /= Conduit[k].barrels;
area = xsect_getAofY(&Link[j].xsect, depth);
if (area > FUDGE ) veloc = flow / area;
}
return veloc;
}
//=============================================================================
double link_getFroude(int j, double v, double y)
//
// Input: j = link index
// v = flow velocity (fps)
// y = flow depth (ft)
// Output: returns Froude Number
// Purpose: computes Froude Number for given velocity and flow depth
//
{
TXsect* xsect = &Link[j].xsect;
// --- return 0 if link is not a conduit
if ( Link[j].type != CONDUIT ) return 0.0;
// --- return 0 if link empty or closed conduit is full
if ( y <= FUDGE ) return 0.0;
if ( !xsect_isOpen(xsect->type) &&
xsect->yFull - y <= FUDGE ) return 0.0;
// --- compute hydraulic depth
y = xsect_getAofY(xsect, y) / xsect_getWofY(xsect, y);
// --- compute Froude No.
return fabs(v) / sqrt(GRAVITY * y);
}
//=============================================================================
double link_getPower(int j)
//
// Input: j = link index
// Output: returns power consumed by link in kwatts
// Purpose: computes power consumed by head loss (or head gain) of
// water flowing through a link
//
{
int n1 = Link[j].node1;
int n2 = Link[j].node2;
double dh = (Node[n1].invertElev + Node[n1].newDepth) -
(Node[n2].invertElev + Node[n2].newDepth);
double q = fabs(Link[j].newFlow);
return fabs(dh) * q / 8.814 * KWperHP;
}
//=============================================================================
double link_getLossRate(int j, int routeModel, double q, double tstep)
//
// Input: j = link index
// routeModel = flow routing model type
// q = flow rate (ft3/sec)
// tstep = time step (sec)
// Output: returns uniform loss rate in link (ft3/sec)
// Purpose: computes rate at which flow volume is lost in a link due to
// evaporation and seepage.
//
{
if ( Link[j].type == CONDUIT )
return conduit_getLossRate(j, routeModel, q, tstep);
else return 0.0;
}
//=============================================================================
char link_getFullState(double a1, double a2, double aFull)
//
// Input: a1 = upstream link area (ft2)
// a2 = downstream link area (ft2)
// aFull = area of full conduit
// Output: returns fullness state of a link
// Purpose: determines if a link is upstream, downstream or completely full.
//
{
if ( a1 >= aFull )
{
if ( a2 >= aFull ) return ALL_FULL;
else return UP_FULL;
}
if ( a2 >= aFull ) return DN_FULL;
return 0;
}
//=============================================================================
// C O N D U I T M E T H O D S
//=============================================================================
int conduit_readParams(int j, int k, char* tok[], int ntoks)
//
// Input: j = link index
// k = conduit index
// tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads conduit parameters from a tokenzed line of input.
//
{
int n1, n2;
double x[6];
char* id;
// --- check for valid ID and end node IDs
if ( ntoks < 7 ) return error_setInpError(ERR_ITEMS, "");
id = project_findID(LINK, tok[0]); // link ID
if ( id == NULL ) return error_setInpError(ERR_NAME, tok[0]);
n1 = project_findObject(NODE, tok[1]); // upstrm. node
if ( n1 < 0 ) return error_setInpError(ERR_NAME, tok[1]);
n2 = project_findObject(NODE, tok[2]); // dwnstrm. node
if ( n2 < 0 ) return error_setInpError(ERR_NAME, tok[2]);
// --- parse length & Mannings N
if ( !getDouble(tok[3], &x[0]) )
return error_setInpError(ERR_NUMBER, tok[3]);
if ( !getDouble(tok[4], &x[1]) )
return error_setInpError(ERR_NUMBER, tok[4]);
// --- parse offsets
if ( LinkOffsets == ELEV_OFFSET && *tok[5] == '*' ) x[2] = MISSING;
else if ( !getDouble(tok[5], &x[2]) )
return error_setInpError(ERR_NUMBER, tok[5]);
if ( LinkOffsets == ELEV_OFFSET && *tok[6] == '*' ) x[3] = MISSING;
else if ( !getDouble(tok[6], &x[3]) )
return error_setInpError(ERR_NUMBER, tok[6]);
// --- parse optional parameters
x[4] = 0.0; // init. flow
if ( ntoks >= 8 )
{
if ( !getDouble(tok[7], &x[4]) )
return error_setInpError(ERR_NUMBER, tok[7]);
}
x[5] = 0.0;
if ( ntoks >= 9 )
{
if ( !getDouble(tok[8], &x[5]) )
return error_setInpError(ERR_NUMBER, tok[8]);
}
// --- add parameters to data base
Link[j].ID = id;
link_setParams(j, CONDUIT, n1, n2, k, x);
return 0;
}
//=============================================================================
void conduit_validate(int j, int k)
//
// Input: j = link index
// k = conduit index
// Output: none
// Purpose: validates a conduit's properties.
//
{
double aa;