forked from USEPA/Stormwater-Management-Model
-
Notifications
You must be signed in to change notification settings - Fork 76
/
subcatch.c
1190 lines (1027 loc) · 41.9 KB
/
subcatch.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
//-----------------------------------------------------------------------------
// subcatch.c
//
// Project: EPA SWMM5
// Version: 5.1
// Date: 03/19/14 (Build 5.1.000)
// 04/19/14 (Build 5.1.006)
// 03/19/15 (Build 5.1.008)
// 04/30/15 (Build 5.1.009)
// 08/05/15 (Build 5.1.010)
// 08/01/16 (Build 5.1.011)
// 03/14/17 (Build 5.1.012)
// 05/10/18 (Build 5.1.013)
// Author: L. Rossman
//
// Subcatchment runoff functions.
//
// Build 5.1.008:
// - Support added for keeping separate track of drain outflows from LIDs.
// - Processing of inflow/outflow volumes over a time step was refactored.
// - Reported subcatchment runoff includes both surface runoff and LID
// drain flows, even though latter can be routed elsewhere.
// - Runon now distributed only over non-LID area of a subcatchment, unless
// LID covers full area.
// - Pollutant buildup and washoff functions were moved to surfqual.c.
//
// Build 5.1.009:
// - Runon for full LID subcatchment added to statistical summary.
//
// Build 5.1.010:
// - Fixed a bug introduced in 5.1.008 that forgot to include LID
// exfiltration as inflow sent to GW routine.
//
// Build 5.1.011:
// - Subcatchment percent imperviousness not allowed to exceed 100.
//
// Build 5.1.012:
// - Subcatchment bottom elevation used instead of aquifer's when
// saving water table value to results file.
//
// Build 5.1.013:
// - Rain gage isUsed property now set in subcatch_validate().
// - Cumulative impervious and pervious area runoff volumes added
// to subcatchment statistics.
// - Support added for monthly adjustment of subcatchment's depression
// storage, pervious N, and infiltration.
//
//-----------------------------------------------------------------------------
#define _CRT_SECURE_NO_DEPRECATE
#include <math.h>
#include <string.h>
#include "headers.h"
#include "lid.h"
#include "odesolve.h"
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
const double MCOEFF = 1.49; // constant in Manning Eq.
const double MEXP = 1.6666667; // exponent in Manning Eq.
const double ODETOL = 0.0001; // acceptable error for ODE solver
//-----------------------------------------------------------------------------
// Globally shared variables
//-----------------------------------------------------------------------------
// Volumes (ft3) for a subcatchment over a time step
double Vevap; // evaporation
double Vpevap; // pervious area evaporation
double Vinfil; // non-LID infiltration
double Vinflow; // non-LID precip + snowmelt + runon + ponded water
double Voutflow; // non-LID runoff to subcatchment's outlet
double VlidIn; // impervious area flow to LID units
double VlidInfil; // infiltration from LID units
double VlidOut; // surface outflow from LID units
double VlidDrain; // drain outflow from LID units
double VlidReturn; // LID outflow returned to pervious area
//-----------------------------------------------------------------------------
// Locally shared variables
//-----------------------------------------------------------------------------
static TSubarea* theSubarea; // subarea to which getDdDt() is applied
static double Dstore; // monthly adjusted depression storage (ft) //(5.1.013)
static double Alpha; // monthly adjusted runoff coeff. //
static char *RunoffRoutingWords[] = { w_OUTLET, w_IMPERV, w_PERV, NULL};
//-----------------------------------------------------------------------------
// External functions (declared in funcs.h)
//-----------------------------------------------------------------------------
// subcatch_readParams (called from parseLine in input.c)
// subcatch_readSubareaParams (called from parseLine in input.c)
// subcatch_readLanduseParams (called from parseLine in input.c)
// subcatch_readInitBuildup (called from parseLine in input.c)
// subcatch_validate (called from project_validate)
// subcatch_initState (called from project_init)
// subcatch_setOldState (called from runoff_execute)
// subcatch_getRunon (called from runoff_execute)
// subcatch_addRunon (called from subcatch_getRunon,
// lid_addDrainRunon, & runoff_getOutfallRunon)
// subcatch_getRunoff (called from runoff_execute)
// subcatch_hadRunoff (called from runoff_execute)
// subcatch_getFracPerv (called from gwater_initState)
// subcatch_getStorage (called from massbal_getRunoffError)
// subcatch_getDepth (called from findPondedLoads in surfqual.c)
// subcatch_getBuildup (called from surfqual_getWashoff)
// subcatch_getWtdOutflow (called from addWetWeatherInflows in routing.c)
// subcatch_getResults (called from output_saveSubcatchResults)
//-----------------------------------------------------------------------------
// Function declarations
//-----------------------------------------------------------------------------
static void getNetPrecip(int j, double* netPrecip, double tStep);
static double getSubareaRunoff(int subcatch, int subarea, double area,
double rainfall, double evap, double tStep);
static double getSubareaInfil(int j, TSubarea* subarea, double precip,
double tStep);
static double findSubareaRunoff(TSubarea* subarea, double tRunoff);
static void updatePondedDepth(TSubarea* subarea, double* tx);
static void getDdDt(double t, double* d, double* dddt);
static void adjustSubareaParams(int subareaType, int subcatch); //(5.1.013)
//=============================================================================
int subcatch_readParams(int j, char* tok[], int ntoks)
//
// Input: j = subcatchment index
// tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads subcatchment parameters from a tokenized line of input data.
//
// Data has format:
// Name RainGage Outlet Area %Imperv Width Slope CurbLength Snowpack
//
{
int i, k, m;
char* id;
double x[9];
// --- check for enough tokens
if ( ntoks < 8 ) return error_setInpError(ERR_ITEMS, "");
// --- check that named subcatch exists
id = project_findID(SUBCATCH, tok[0]);
if ( id == NULL ) return error_setInpError(ERR_NAME, tok[0]);
// --- check that rain gage exists
k = project_findObject(GAGE, tok[1]);
if ( k < 0 ) return error_setInpError(ERR_NAME, tok[1]);
x[0] = k;
// --- check that outlet node or subcatch exists
m = project_findObject(NODE, tok[2]);
x[1] = m;
m = project_findObject(SUBCATCH, tok[2]);
x[2] = m;
if ( x[1] < 0.0 && x[2] < 0.0 )
return error_setInpError(ERR_NAME, tok[2]);
// --- read area, %imperv, width, slope, & curb length
for ( i = 3; i < 8; i++)
{
if ( ! getDouble(tok[i], &x[i]) || x[i] < 0.0 )
return error_setInpError(ERR_NUMBER, tok[i]);
}
// --- if snowmelt object named, check that it exists
x[8] = -1;
if ( ntoks > 8 )
{
k = project_findObject(SNOWMELT, tok[8]);
if ( k < 0 ) return error_setInpError(ERR_NAME, tok[8]);
x[8] = k;
}
// --- assign input values to subcatch's properties
Subcatch[j].ID = id;
Subcatch[j].gage = (int)x[0];
Subcatch[j].outNode = (int)x[1];
Subcatch[j].outSubcatch = (int)x[2];
Subcatch[j].area = x[3] / UCF(LANDAREA);
Subcatch[j].fracImperv = MIN(x[4], 100.0) / 100.0;
Subcatch[j].width = x[5] / UCF(LENGTH);
Subcatch[j].slope = x[6] / 100.0;
Subcatch[j].curbLength = x[7];
Subcatch[j].nPervPattern = -1; //(5.1.013
Subcatch[j].dStorePattern = -1; //
Subcatch[j].infilPattern = -1; //
// --- create the snow pack object if it hasn't already been created
if ( x[8] >= 0 )
{
if ( !snow_createSnowpack(j, (int)x[8]) )
return error_setInpError(ERR_MEMORY, "");
}
return 0;
}
//=============================================================================
int subcatch_readSubareaParams(char* tok[], int ntoks)
//
// Input: tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads subcatchment's subarea parameters from a tokenized
// line of input data.
//
// Data has format:
// Subcatch Imperv_N Perv_N Imperv_S Perv_S PctZero RouteTo (PctRouted)
//
{
int i, j, k, m;
double x[7];
// --- check for enough tokens
if ( ntoks < 7 ) return error_setInpError(ERR_ITEMS, "");
// --- check that named subcatch exists
j = project_findObject(SUBCATCH, tok[0]);
if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]);
// --- read in Mannings n, depression storage, & PctZero values
for (i = 0; i < 5; i++)
{
if ( ! getDouble(tok[i+1], &x[i]) || x[i] < 0.0 )
return error_setInpError(ERR_NAME, tok[i+1]);
}
// --- check for valid runoff routing keyword
m = findmatch(tok[6], RunoffRoutingWords);
if ( m < 0 ) return error_setInpError(ERR_KEYWORD, tok[6]);
// --- get percent routed parameter if present (default is 100)
x[5] = m;
x[6] = 1.0;
if ( ntoks >= 8 )
{
if ( ! getDouble(tok[7], &x[6]) || x[6] < 0.0 || x[6] > 100.0 )
return error_setInpError(ERR_NUMBER, tok[7]);
x[6] /= 100.0;
}
// --- assign input values to each type of subarea
Subcatch[j].subArea[IMPERV0].N = x[0];
Subcatch[j].subArea[IMPERV1].N = x[0];
Subcatch[j].subArea[PERV].N = x[1];
Subcatch[j].subArea[IMPERV0].dStore = 0.0;
Subcatch[j].subArea[IMPERV1].dStore = x[2] / UCF(RAINDEPTH);
Subcatch[j].subArea[PERV].dStore = x[3] / UCF(RAINDEPTH);
Subcatch[j].subArea[IMPERV0].fArea = Subcatch[j].fracImperv * x[4] / 100.0;
Subcatch[j].subArea[IMPERV1].fArea = Subcatch[j].fracImperv * (1.0 - x[4] / 100.0);
Subcatch[j].subArea[PERV].fArea = (1.0 - Subcatch[j].fracImperv);
// --- assume that all runoff from each subarea goes to subcatch outlet
for (i = IMPERV0; i <= PERV; i++)
{
Subcatch[j].subArea[i].routeTo = TO_OUTLET;
Subcatch[j].subArea[i].fOutlet = 1.0;
}
// --- modify routing if pervious runoff routed to impervious area
// (fOutlet is the fraction of runoff not routed)
k = (int)x[5];
if ( Subcatch[j].fracImperv == 0.0
|| Subcatch[j].fracImperv == 1.0 ) k = TO_OUTLET;
if ( k == TO_IMPERV && Subcatch[j].fracImperv )
{
Subcatch[j].subArea[PERV].routeTo = k;
Subcatch[j].subArea[PERV].fOutlet = 1.0 - x[6];
}
// --- modify routing if impervious runoff routed to pervious area
if ( k == TO_PERV )
{
Subcatch[j].subArea[IMPERV0].routeTo = k;
Subcatch[j].subArea[IMPERV1].routeTo = k;
Subcatch[j].subArea[IMPERV0].fOutlet = 1.0 - x[6];
Subcatch[j].subArea[IMPERV1].fOutlet = 1.0 - x[6];
}
return 0;
}
//=============================================================================
int subcatch_readLanduseParams(char* tok[], int ntoks)
//
// Input: tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads assignment of landuses to subcatchment from a tokenized
// line of input data.
//
// Data has format:
// Subcatch landuse percent .... landuse percent
//
{
int j, k, m;
double f;
// --- check for enough tokens
if ( ntoks < 3 ) return error_setInpError(ERR_ITEMS, "");
// --- check that named subcatch exists
j = project_findObject(SUBCATCH, tok[0]);
if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]);
// --- process each pair of landuse - percent items
for ( k = 2; k <= ntoks; k = k+2)
{
// --- check that named land use exists and is followed by a percent
m = project_findObject(LANDUSE, tok[k-1]);
if ( m < 0 ) return error_setInpError(ERR_NAME, tok[k-1]);
if ( k+1 > ntoks ) return error_setInpError(ERR_ITEMS, "");
if ( ! getDouble(tok[k], &f) )
return error_setInpError(ERR_NUMBER, tok[k]);
// --- store land use fraction in subcatch's landFactor property
Subcatch[j].landFactor[m].fraction = f/100.0;
}
return 0;
}
//=============================================================================
int subcatch_readInitBuildup(char* tok[], int ntoks)
//
// Input: tok[] = array of string tokens
// ntoks = number of tokens
// Output: returns an error code
// Purpose: reads initial pollutant buildup on subcatchment from
// tokenized line of input data.
//
// Data has format:
// Subcatch pollut initLoad .... pollut initLoad
//
{
int j, k, m;
double x;
// --- check for enough tokens
if ( ntoks < 3 ) return error_setInpError(ERR_ITEMS, "");
// --- check that named subcatch exists
j = project_findObject(SUBCATCH, tok[0]);
if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]);
// --- process each pair of pollutant - init. load items
for ( k = 2; k <= ntoks; k = k+2)
{
// --- check for valid pollutant name and loading value
m = project_findObject(POLLUT, tok[k-1]);
if ( m < 0 ) return error_setInpError(ERR_NAME, tok[k-1]);
if ( k+1 > ntoks ) return error_setInpError(ERR_ITEMS, "");
if ( ! getDouble(tok[k], &x) )
return error_setInpError(ERR_NUMBER, tok[k]);
// --- store loading in subcatch's initBuildup property
Subcatch[j].initBuildup[m] = x;
}
return 0;
}
//=============================================================================
void subcatch_validate(int j)
//
// Input: j = subcatchment index
// Output: none
// Purpose: checks for valid subcatchment input parameters.
//
{
int i;
double area;
double nonLidArea = Subcatch[j].area;
// --- check for ambiguous outlet name
if ( Subcatch[j].outNode >= 0 && Subcatch[j].outSubcatch >= 0 )
report_writeErrorMsg(ERR_SUBCATCH_OUTLET, Subcatch[j].ID);
// --- validate subcatchment's groundwater component
gwater_validate(j);
// --- validate placement of LIDs in the subcatchment
nonLidArea -= Subcatch[j].lidArea;
// --- compute alpha (i.e. WCON in old SWMM) for overland flow
// NOTE: the area which contributes to alpha for both imperv
// subareas w/ and w/o depression storage is the total imperv area.
for (i = IMPERV0; i <= PERV; i++)
{
if ( i == PERV )
{
area = (1.0 - Subcatch[j].fracImperv) * nonLidArea;
}
else
{
area = Subcatch[j].fracImperv * nonLidArea;
}
Subcatch[j].subArea[i].alpha = 0.0;
//// Possible change to how sub-area width should be assigned. ////
//// area = nonLidArea;
/////////////////////////////////////////////////////////////////////
if ( area > 0.0 && Subcatch[j].subArea[i].N > 0.0 )
{
Subcatch[j].subArea[i].alpha = MCOEFF * Subcatch[j].width / area *
sqrt(Subcatch[j].slope) / Subcatch[j].subArea[i].N;
}
}
// --- set isUsed property of subcatchment's rain gage //(5.1.013)
i = Subcatch[j].gage; //
if (i >= 0) Gage[i].isUsed = TRUE; //
}
//=============================================================================
void subcatch_initState(int j)
//
// Input: j = subcatchment index
// Output: none
// Purpose: Initializes the state of a subcatchment.
//
{
int i;
//// isUsed property of subcatchment's rain gage now set in subcatch_validate //(5.1.013)
// --- initialize rainfall, runoff, & snow depth
Subcatch[j].rainfall = 0.0;
Subcatch[j].oldRunoff = 0.0;
Subcatch[j].newRunoff = 0.0;
Subcatch[j].oldSnowDepth = 0.0;
Subcatch[j].newSnowDepth = 0.0;
Subcatch[j].runon = 0.0;
Subcatch[j].evapLoss = 0.0;
Subcatch[j].infilLoss = 0.0;
// --- initialize state of infiltration, groundwater, & snow pack objects
if ( Subcatch[j].infil == j ) infil_initState(j, InfilModel);
if ( Subcatch[j].groundwater ) gwater_initState(j);
if ( Subcatch[j].snowpack ) snow_initSnowpack(j);
// --- initialize state of sub-areas
for (i = IMPERV0; i <= PERV; i++)
{
Subcatch[j].subArea[i].depth = 0.0;
Subcatch[j].subArea[i].inflow = 0.0;
Subcatch[j].subArea[i].runoff = 0.0;
}
// --- initialize runoff quality
surfqual_initState(j);
}
//=============================================================================
void subcatch_setOldState(int j)
//
// Input: j = subcatchment index
// Output: none
// Purpose: replaces old state of subcatchment with new state.
//
{
int i;
Subcatch[j].oldRunoff = Subcatch[j].newRunoff;
Subcatch[j].oldSnowDepth = Subcatch[j].newSnowDepth;
for (i = IMPERV0; i <= PERV; i++)
{
Subcatch[j].subArea[i].inflow = 0.0;
}
for (i = 0; i < Nobjects[POLLUT]; i++)
{
Subcatch[j].oldQual[i] = Subcatch[j].newQual[i];
Subcatch[j].newQual[i] = 0.0;
}
lid_setOldGroupState(j);
}
//=============================================================================
double subcatch_getFracPerv(int j)
//
// Purpose: determines what fraction of subcatchment area, including any LID
// area, is pervious.
// Input: j = subcatchment index
// Output: returns fraction of area with pervious cover
//
{
double fracPerv = 1.0 - Subcatch[j].fracImperv;
if ( Subcatch[j].lidArea > 0.0 )
{
fracPerv = (fracPerv * (Subcatch[j].area - Subcatch[j].lidArea) +
lid_getPervArea(j)) / Subcatch[j].area;
fracPerv = MIN(fracPerv, 1.0);
}
return fracPerv;
}
//=============================================================================
double subcatch_getStorage(int j)
//
// Input: j = subcatchment index
// Output: returns total volume of stored water (ft3)
// Purpose: finds total volume of water stored on a subcatchment's surface
// and its LIDs at the current time.
//
{
int i;
double v = 0.0;
for ( i = IMPERV0; i <= PERV; i++)
{
v += Subcatch[j].subArea[i].depth * Subcatch[j].subArea[i].fArea;
}
return v * (Subcatch[j].area - Subcatch[j].lidArea) +
lid_getStoredVolume(j);
}
//=============================================================================
void subcatch_getRunon(int j)
//
// Input: j = subcatchment index
// Output: none
// Purpose: Routes runoff from a subcatchment to its outlet subcatchment
// or between its subareas.
//
{
int k; // outlet subcatchment index
int p; // pollutant index
double q; // runon to outlet subcatchment (ft/sec)
double q1, q2; // runoff from imperv. areas (ft/sec)
double pervArea; // subcatchment pervious area (ft2)
// --- add previous period's runoff from this subcatchment to the
// runon of the outflow subcatchment, if it exists
k = Subcatch[j].outSubcatch;
q = Subcatch[j].oldRunoff;
if ( k >= 0 && k != j )
{
subcatch_addRunonFlow(k, q);
for (p = 0; p < Nobjects[POLLUT]; p++)
{
Subcatch[k].newQual[p] += q * Subcatch[j].oldQual[p] * LperFT3;
}
}
// --- add any LID underdrain flow sent from this subcatchment to
// other subcatchments
if ( Subcatch[j].lidArea > 0.0 ) lid_addDrainRunon(j);
// --- add to sub-area inflow any outflow from other subarea in previous period
// (NOTE: no transfer of runoff pollutant load, since runoff loads are
// based on runoff flow from entire subcatchment.)
// --- Case 1: imperv --> perv
if ( Subcatch[j].fracImperv < 1.0 &&
Subcatch[j].subArea[IMPERV0].routeTo == TO_PERV )
{
// --- add area-wtd. outflow from imperv1 subarea to perv area inflow
q1 = Subcatch[j].subArea[IMPERV0].runoff *
Subcatch[j].subArea[IMPERV0].fArea;
q2 = Subcatch[j].subArea[IMPERV1].runoff *
Subcatch[j].subArea[IMPERV1].fArea;
q = q1 + q2;
Subcatch[j].subArea[PERV].inflow += q *
(1.0 - Subcatch[j].subArea[IMPERV0].fOutlet) /
Subcatch[j].subArea[PERV].fArea;
}
// --- Case 2: perv --> imperv
if ( Subcatch[j].fracImperv > 0.0 &&
Subcatch[j].subArea[PERV].routeTo == TO_IMPERV &&
Subcatch[j].subArea[IMPERV1].fArea > 0.0 )
{
q = Subcatch[j].subArea[PERV].runoff;
Subcatch[j].subArea[IMPERV1].inflow +=
q * (1.0 - Subcatch[j].subArea[PERV].fOutlet) *
Subcatch[j].subArea[PERV].fArea /
Subcatch[j].subArea[IMPERV1].fArea;
}
// --- Add any return flow from LID units to pervious subarea
if ( Subcatch[j].lidArea > 0.0 && Subcatch[j].fracImperv < 1.0 )
{
pervArea = Subcatch[j].subArea[PERV].fArea *
(Subcatch[j].area - Subcatch[j].lidArea);
q = lid_getFlowToPerv(j);
if ( pervArea > 0.0 )
{
Subcatch[j].subArea[PERV].inflow += q / pervArea;
}
}
}
//=============================================================================
void subcatch_addRunonFlow(int k, double q)
//
// Input: k = subcatchment index
// q = runon flow rate (cfs) to subcatchment k
// Output: none
// Purpose: Updates the total runon flow (ft/s) seen by a subcatchment that
// receives runon flow from an upstream subcatchment.
//
{
int i;
double nonLidArea;
// --- distribute runoff from upstream subcatchment (in cfs)
// uniformly over the non-LID area of current subcatchment (ft/sec)
if ( Subcatch[k].area <= 0.0 ) return;
nonLidArea = Subcatch[k].area - Subcatch[k].lidArea;
if ( nonLidArea > 0.0 ) q = q / nonLidArea;
else q = q / Subcatch[k].area;
Subcatch[k].runon += q;
// --- assign this flow to the 3 types of subareas
for (i = IMPERV0; i <= PERV; i++)
{
Subcatch[k].subArea[i].inflow += q;
}
}
//=============================================================================
double subcatch_getRunoff(int j, double tStep)
//
// Input: j = subcatchment index
// tStep = time step (sec)
// Output: returns total runoff produced by subcatchment (ft/sec)
// Purpose: Computes runoff & new storage depth for subcatchment.
//
// The 'runoff' value returned by this function is the total runoff
// generated (in ft/sec) by the subcatchment before any internal
// re-routing is applied. It is used to compute pollutant washoff.
//
// The 'outflow' value computed here (in cfs) is the surface runoff
// that actually leaves the subcatchment after any LID controls are
// applied and is saved to Subcatch[j].newRunoff.
//
{
int i; // subarea index
double nonLidArea; // non-LID portion of subcatch area (ft2)
double area; // sub-area or subcatchment area (ft2)
double netPrecip[3]; // subarea net precipitation (ft/sec)
double vRain; // rainfall (+ snowfall) volume (ft3)
double vRunon = 0.0; // runon volume from other areas (ft3)
double vOutflow = 0.0; // runoff volume leaving subcatch (ft3)
double runoff = 0.0; // total runoff flow on subcatch (cfs)
double evapRate = 0.0; // potential evaporation rate (ft/sec)
double subAreaRunoff; // sub-area runoff rate (cfs) //(5.1.013)
double vImpervRunoff = 0.0; // impervious area runoff volume (ft3) //
double vPervRunoff = 0.0; // pervious area runoff volume (ft3) //
// --- initialize shared water balance variables
Vevap = 0.0;
Vpevap = 0.0;
Vinfil = 0.0;
Voutflow = 0.0;
VlidIn = 0.0;
VlidInfil = 0.0;
VlidOut = 0.0;
VlidDrain = 0.0;
VlidReturn = 0.0;
// --- find volume of inflow to non-LID portion of subcatchment as existing
// ponded water + any runon volume from upstream areas;
// rainfall and snowmelt will be added as each sub-area is analyzed
nonLidArea = Subcatch[j].area - Subcatch[j].lidArea;
vRunon = Subcatch[j].runon * tStep * nonLidArea;
Vinflow = vRunon + subcatch_getDepth(j) * nonLidArea;
// --- find LID runon only if LID occupies full subcatchment
if ( nonLidArea == 0.0 )
vRunon = Subcatch[j].runon * tStep * Subcatch[j].area;
// --- get net precip. (rainfall + snowfall + snowmelt) on the 3 types
// of subcatchment sub-areas and update Vinflow with it
getNetPrecip(j, netPrecip, tStep);
// --- find potential evaporation rate
if ( Evap.dryOnly && Subcatch[j].rainfall > 0.0 ) evapRate = 0.0;
else evapRate = Evap.rate;
// --- set monthly infiltration adjustment factor //(5.1.013)
infil_setInfilFactor(j); //(5.1.013)
// --- examine each type of sub-area (impervious w/o depression storage,
// impervious w/ depression storage, and pervious)
if ( nonLidArea > 0.0 ) for (i = IMPERV0; i <= PERV; i++)
{
// --- get runoff from sub-area updating Vevap, Vpevap,
// Vinfil & Voutflow)
area = nonLidArea * Subcatch[j].subArea[i].fArea;
Subcatch[j].subArea[i].runoff =
getSubareaRunoff(j, i, area, netPrecip[i], evapRate, tStep);
subAreaRunoff = Subcatch[j].subArea[i].runoff * area; //(5.1.013)
if (i == PERV) vPervRunoff = subAreaRunoff * tStep; //
else vImpervRunoff += subAreaRunoff * tStep; //
runoff += subAreaRunoff; //
}
// --- evaluate any LID treatment provided (updating Vevap,
// Vpevap, VlidInfil, VlidIn, VlidOut, & VlidDrain)
if ( Subcatch[j].lidArea > 0.0 )
{
lid_getRunoff(j, tStep);
}
// --- update groundwater levels & flows if applicable
if ( !IgnoreGwater && Subcatch[j].groundwater )
{
gwater_getGroundwater(j, Vpevap, Vinfil+VlidInfil, tStep);
}
// --- save subcatchment's total loss rates (ft/s)
area = Subcatch[j].area;
Subcatch[j].evapLoss = Vevap / tStep / area;
Subcatch[j].infilLoss = (Vinfil + VlidInfil) / tStep / area;
// --- find net surface runoff volume
// (VlidDrain accounts for LID drain flows)
vOutflow = Voutflow // runoff from all non-LID areas
- VlidIn // runoff treated by LID units
+ VlidOut; // runoff from LID units
Subcatch[j].newRunoff = vOutflow / tStep;
// --- obtain external precip. volume (without any snowmelt)
vRain = Subcatch[j].rainfall * tStep * area;
// --- update the cumulative stats for this subcatchment
stats_updateSubcatchStats(j, vRain, vRunon, Vevap, Vinfil + VlidInfil,
vImpervRunoff, vPervRunoff, vOutflow + VlidDrain, //(5.1.013)
Subcatch[j].newRunoff + VlidDrain/tStep);
// --- include this subcatchment's contribution to overall flow balance
// only if its outlet is a drainage system node
if ( Subcatch[j].outNode == -1 && Subcatch[j].outSubcatch != j )
{
vOutflow = 0.0;
}
// --- update mass balances
massbal_updateRunoffTotals(RUNOFF_RAINFALL, vRain);
massbal_updateRunoffTotals(RUNOFF_EVAP, Vevap);
massbal_updateRunoffTotals(RUNOFF_INFIL, Vinfil+VlidInfil);
massbal_updateRunoffTotals(RUNOFF_RUNOFF, vOutflow);
// --- return area-averaged runoff (ft/s)
return runoff / area;
}
//=============================================================================
void getNetPrecip(int j, double* netPrecip, double tStep)
{
//
// Purpose: Finds combined rainfall + snowmelt on a subcatchment.
// Input: j = subcatchment index
// tStep = time step (sec)
// Output: netPrecip = rainfall + snowmelt over each type of subarea (ft/s)
//
int i, k;
double rainfall = 0.0; // rainfall (ft/sec)
double snowfall = 0.0; // snowfall (ft/sec)
// --- get current rainfall or snowfall from rain gage (in ft/sec)
k = Subcatch[j].gage;
if ( k >= 0 )
{
gage_getPrecip(k, &rainfall, &snowfall);
}
// --- assign total precip. rate to subcatch's rainfall property
Subcatch[j].rainfall = rainfall + snowfall;
// --- determine net precipitation input (netPrecip) to each sub-area
// --- if subcatch has a snowpack, then base netPrecip on possible snow melt
if ( Subcatch[j].snowpack && !IgnoreSnowmelt )
{
Subcatch[j].newSnowDepth =
snow_getSnowMelt(j, rainfall, snowfall, tStep, netPrecip);
}
// --- otherwise netPrecip is just sum of rainfall & snowfall
else
{
for (i=IMPERV0; i<=PERV; i++) netPrecip[i] = rainfall + snowfall;
}
}
//=============================================================================
double subcatch_getBuildup(int j, int p)
//
// Input: j = subcatchment index
// p = pollutant index
// Output: returns total buildup of each pollutant on subcatchment surface (lbs or kg)
// Purpose: computes current mass of buildup remaining on subcatchment surface
//
{
int i;
double load = 0.0;
for (i = 0; i < Nobjects[LANDUSE]; i++)
{
load += Subcatch[j].landFactor[i].buildup[p];
}
return load;
}
//=============================================================================
double subcatch_getDepth(int j)
//
// Input: j = subcatchment index
// Output: returns average depth of ponded water (ft)
// Purpose: finds average depth of water over the non-LID portion of a
// subcatchment
//
{
int i;
double fArea;
double depth = 0.0;
for (i = IMPERV0; i <= PERV; i++)
{
fArea = Subcatch[j].subArea[i].fArea;
if ( fArea > 0.0 ) depth += Subcatch[j].subArea[i].depth * fArea;
}
return depth;
}
//=============================================================================
double subcatch_getWtdOutflow(int j, double f)
//
// Input: j = subcatchment index
// f = weighting factor.
// Output: returns weighted runoff value
// Purpose: computes wtd. combination of old and new subcatchment runoff.
//
{
if ( Subcatch[j].area == 0.0 ) return 0.0;
return (1.0 - f) * Subcatch[j].oldRunoff + f * Subcatch[j].newRunoff;
}
//=============================================================================
void subcatch_getResults(int j, double f, float x[])
//
// Input: j = subcatchment index
// f = weighting factor
// Output: x = array of results
// Purpose: computes wtd. combination of old and new subcatchment results.
//
{
int p; // pollutant index
int k; // rain gage index
double f1 = 1.0 - f;
double z;
double runoff;
TGroundwater* gw; // ptr. to groundwater object
// --- retrieve rainfall for current report period
k = Subcatch[j].gage;
if ( k >= 0 ) x[SUBCATCH_RAINFALL] = (float)Gage[k].reportRainfall;
else x[SUBCATCH_RAINFALL] = 0.0f;
// --- retrieve snow depth
z = ( f1 * Subcatch[j].oldSnowDepth +
f * Subcatch[j].newSnowDepth ) * UCF(RAINDEPTH);
x[SUBCATCH_SNOWDEPTH] = (float)z;
// --- retrieve runoff and losses
x[SUBCATCH_EVAP] = (float)(Subcatch[j].evapLoss * UCF(EVAPRATE));
x[SUBCATCH_INFIL] = (float)(Subcatch[j].infilLoss * UCF(RAINFALL));
runoff = f1 * Subcatch[j].oldRunoff + f * Subcatch[j].newRunoff;
// --- add any LID drain flow to reported runoff
if ( Subcatch[j].lidArea > 0.0 )
{
runoff += f1 * lid_getDrainFlow(j, PREVIOUS) +
f * lid_getDrainFlow(j, CURRENT);
}
// --- if runoff is really small, report it as zero
if ( runoff < MIN_RUNOFF * Subcatch[j].area ) runoff = 0.0;
x[SUBCATCH_RUNOFF] = (float)(runoff * UCF(FLOW));
// --- retrieve groundwater results
gw = Subcatch[j].groundwater;
if ( gw )
{
z = (f1 * gw->oldFlow + f * gw->newFlow) * Subcatch[j].area * UCF(FLOW);
x[SUBCATCH_GW_FLOW] = (float)z;
z = (gw->bottomElev + gw->lowerDepth) * UCF(LENGTH);
x[SUBCATCH_GW_ELEV] = (float)z;
z = gw->theta;
x[SUBCATCH_SOIL_MOIST] = (float)z;
}
else
{
x[SUBCATCH_GW_FLOW] = 0.0f;
x[SUBCATCH_GW_ELEV] = 0.0f;
x[SUBCATCH_SOIL_MOIST] = 0.0f;
}
// --- retrieve pollutant washoff
if ( !IgnoreQuality ) for (p = 0; p < Nobjects[POLLUT]; p++ )
{
if ( runoff == 0.0 ) z = 0.0;
else z = f1 * Subcatch[j].oldQual[p] + f * Subcatch[j].newQual[p];
x[SUBCATCH_WASHOFF+p] = (float)z;
}
}
//=============================================================================
// SUB-AREA METHODS
//=============================================================================
double getSubareaRunoff(int j, int i, double area, double precip, double evap,
double tStep)
//
// Purpose: computes runoff & losses from a subarea over the current time step.
// Input: j = subcatchment index
// i = subarea index
// area = sub-area area (ft2)
// precip = rainfall + snowmelt over subarea (ft/sec)
// evap = evaporation (ft/sec)
// tStep = time step (sec)
// Output: returns runoff rate from the sub-area (cfs);
// updates shared variables Vinflow, Vevap, Vpevap, Vinfil & Voutflow.
//
{
double tRunoff; // time over which runoff occurs (sec)
double surfMoisture; // surface water available (ft/sec)
double surfEvap; // evap. used for surface water (ft/sec)
double infil = 0.0; // infiltration rate (ft/sec)
double runoff = 0.0; // runoff rate (ft/sec)
TSubarea* subarea; // pointer to subarea being analyzed
// --- no runoff if no area
if ( area == 0.0 ) return 0.0;
// --- assign pointer to current subarea
subarea = &Subcatch[j].subArea[i];
// --- assume runoff occurs over entire time step
tRunoff = tStep;
// --- determine evaporation loss rate
surfMoisture = subarea->depth / tStep;
surfEvap = MIN(surfMoisture, evap);
// --- compute infiltration loss rate
if ( i == PERV ) infil = getSubareaInfil(j, subarea, precip, tStep);
// --- add precip to other subarea inflows
subarea->inflow += precip;
surfMoisture += subarea->inflow;
// --- update total inflow, evaporation & infiltration volumes
Vinflow += precip * area * tStep;
Vevap += surfEvap * area * tStep;
if ( i == PERV ) Vpevap += Vevap;
Vinfil += infil * area * tStep;
// --- assign adjusted runoff coeff. & storage to shared variables //(5.1.013)
Alpha = subarea->alpha; //
Dstore = subarea->dStore; //
adjustSubareaParams(i, j); //
// --- if losses exceed available moisture then no ponded water remains
if ( surfEvap + infil >= surfMoisture )
{
subarea->depth = 0.0;
}
// --- otherwise reduce inflow by losses and update depth
// of ponded water and time over which runoff occurs
else
{