This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
forked from respec/HSPsquared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGQUAL.py
1562 lines (1393 loc) · 49.6 KB
/
GQUAL.py
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
''' Copyright (c) 2020 by RESPEC, INC.
Authors: Robert Heaphy, Ph.D. and Paul Duda
License: LGPL2
'''
from numpy import array, zeros, int64
from numba import njit
from math import exp
from HSP2.utilities import initm, make_numba_dict, hoursval, dayval
from HSP2.ADCALC import advect, oxrea
ERRMSGS =('GQUAL: one or more gquals are sediment-associated, but section sedtrn not active', #ERRMSG0
'GQUAL: simulation of photolysis requires aux1fg to be on to calculate average depth', #ERRMSG1
'GQUAL: simulation of volatilization in a free flowing stream requires aux3fg on', #ERRMSG2
'GQUAL: simulation of volatilization in a lake requires aux1fg on to calculate average depth', #ERRMSG3
'GQUAL: in advqal, the value of denom is zero, and ISQAL and RSQALS should also be zero', #ERRMSG4
'GQUAL: in advqal, the value of bsed is zero, and DSQAL and RBQALS should also be zero', #ERRMSG5
'GQUAL: the value of tempfg is 1, but timeseries TW is not available as input', #ERRMSG6
'GQUAL: the value of phflag is 1, but timeseries PHVAL is not available as input', #ERRMSG7
'GQUAL: the value of roxfg is 1, but timeseries ROC is not available as input', #ERRMSG8
'GQUAL: the value of cldfg is 1, but timeseries CLOUD is not available as input', #ERRMSG9
'GQUAL: the value of sdfg is 1, but timeseries SSED4 is not available as input', #ERRMSG10
'GQUAL: the value of phytfg is 1, but timeseries PHYTO is not available as input') #ERRMSG11
def gqual(io_manager, siminfo, uci, ts):
''' Simulate the behavior of a generalized quality constituent'''
errors = zeros(len(ERRMSGS)).astype(int64)
delt60 = siminfo['delt'] / 60 # delt60 - simulation time interval in hours
simlen = siminfo['steps']
delts = siminfo['delt'] * 60
uunits = siminfo['units']
AFACT = 43560.0
if uunits == 2:
# si units conversion
AFACT = 1000000.0
advectData = uci['advectData']
(nexits, vol, VOL, SROVOL, EROVOL, SOVOL, EOVOL) = advectData
svol = vol * AFACT
ts['VOL'] = VOL
ts['SROVOL'] = SROVOL
ts['EROVOL'] = EROVOL
for i in range(nexits):
ts['SOVOL' + str(i + 1)] = SOVOL[:, i]
ts['EOVOL' + str(i + 1)] = EOVOL[:, i]
ui = make_numba_dict(uci)
ui['simlen'] = siminfo['steps']
ui['uunits'] = siminfo['units']
ui['svol'] = svol
ui['vol'] = vol
ui['delt60'] = delt60
ui['delts'] = delts
ui['errlen'] = len(ERRMSGS)
# table-type gq-gendata
ngqual = 1
tempfg = 2
phflag = 2
roxfg = 2
cldfg = 2
sdfg = 2
phytfg = 2
# ui = uci['PARAMETERS']
if 'NGQUAL' in ui:
ngqual = int(ui['NGQUAL'])
tempfg = ui['TEMPFG']
phflag = ui['PHFLAG']
roxfg = ui['ROXFG']
cldfg = ui['CLDFG']
sdfg = ui['SDFG']
phytfg = ui['PHYTFG']
ui['ngqual'] = ngqual
ts['HRFG'] = hour24Flag(siminfo).astype(float)
ui_base = ui # save base ui dict before adding qual specific parms
for index in range(1, ngqual+1):
ui = ui_base
ui['index'] = index
# update UI values for this constituent here!
ui_parms = uci['GQUAL' + str(index)]
if 'GQADFG' + str((index * 2) - 1) in ui_parms:
# get atmos dep timeseries
gqadfgf = ui_parms['GQADFG' + str((index * 2) - 1)]
if gqadfgf > 0:
ts['GQADFX'] = initm(siminfo, uci, gqadfgf, 'GQUAL' + str(index) + '_MONTHLY/GQADFX', 0.0)
elif gqadfgf == -1:
ts['GQADFX'] = ts['GQADFX' + str(index) + ' 1']
gqadfgc = ui_parms['GQADFG' + str(index * 2)]
if gqadfgc > 0:
ts['GQADCN'] = initm(siminfo, uci, gqadfgc, 'GQUAL' + str(index) + '_MONTHLY/GQADCN', 0.0)
elif gqadfgc == -1:
ts['GQADCN'] = ts['GQADCN' + str(index) + ' 1']
if 'GQADFX' in ts:
ts['GQADFX'] *= delt60 / (24.0 * AFACT)
if 'GQADFX' not in ts:
ts['GQADFX'] = zeros(simlen)
if 'GQADCN' not in ts:
ts['GQADCN'] = zeros(simlen)
# table-type gq-flg2
gqpm2 = zeros(8)
gqpm2[7] = 2
if 'GQPM21' in ui_parms:
gqpm2[1] = ui_parms['GQPM21']
gqpm2[2] = ui_parms['GQPM22']
gqpm2[3] = ui_parms['GQPM23']
gqpm2[4] = ui_parms['GQPM24']
gqpm2[5] = ui_parms['GQPM25']
gqpm2[6] = ui_parms['GQPM26']
gqpm2[7] = ui_parms['GQPM27']
biop = 0.0
qalfg5 = ui_parms['QALFG5']
if qalfg5 == 1: # qual undergoes biodegradation
# BIOPM(1,I) # table-type gq-biopm
biop = ui_parms['BIO']
ts['BIO'] = zeros(simlen)
ts['BIO'].fill(biop)
# specifies source of biomass data using GQPM2(7,I)
if gqpm2[7] == 1 or gqpm2[7] == 3:
# BIOM = # from ts, monthly, constant
ts['BIO'] = initm(siminfo, uci, ui_parms['GQPM27'], 'GQUAL' + str(index) + '_MONTHLY/BIO',
ui_parms['BIO'])
# table-type gq-values
if tempfg == 2 and "TWAT" in ui_parms:
twat = ui_parms["TWAT"]
else:
twat = 60.0
if phflag == 2 and "PHVAL" in ui_parms:
phval = ui_parms["PHVAL"]
else:
phval = 7.0
if roxfg == 2 and "ROC" in ui_parms:
roc = ui_parms["ROC"]
else:
roc = 0.0
if cldfg == 2 and "CLD" in ui_parms:
cld = ui_parms["CLD"]
else:
cld = 0.0
if sdfg == 2 and "SDCNC" in ui_parms:
sdcnc = ui_parms["SDCNC"]
else:
sdcnc = 0.0
if phytfg == 2 and "PHY" in ui_parms:
phy = ui_parms["PHY"]
else:
phy = 0.0
# for the following, if the flag value is 1 the timeseries should already be available as input
if tempfg == 2 or tempfg == 3:
ts['TW_GQ'] = initm(siminfo, uci, tempfg, 'GQUAL' + str(index) + '_MONTHLY/WATEMP', twat)
if phflag == 2 or phflag == 3:
ts['PHVAL_GQ'] = initm(siminfo, uci, phflag, 'GQUAL' + str(index) + '_MONTHLY/PHVAL', phval)
if roxfg == 2 or roxfg == 3:
ts['ROC_GQ'] = initm(siminfo, uci, roxfg, 'GQUAL' + str(index) + '_MONTHLY/ROXYGEN', roc)
if cldfg == 2 or cldfg == 3:
ts['CLOUD_GQ'] = initm(siminfo, uci, cldfg, 'GQUAL' + str(index) + '_MONTHLY/CLOUD', cld)
if sdfg == 2 or sdfg == 3:
ts['SDCNC_GQ'] = initm(siminfo, uci, sdfg, 'GQUAL' + str(index) + '_MONTHLY/SEDCONC', sdcnc)
if phytfg == 2 or phytfg == 3:
ts['PHYTO_GQ'] = initm(siminfo, uci, phytfg, 'GQUAL' + str(index) + '_MONTHLY/PHYTO', phy)
# if any of these flags are 1 and the timeseries does not exist, that's a problem -- trigger message
ts['DAYVAL'] = dayval(siminfo, [4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4])
qalfg3 = ui_parms['QALFG3']
if qalfg3 == 1: # qual undergoes photolysis
# PHOTPM(1,I) # table-type gq-photpm
if 'EXTENDEDS_PHOTPM' in uci:
ttable = uci['EXTENDEDS_PHOTPM']
for i in range(1,21):
ui['photpm' + str(i)] = ttable['PHOTPM' + str(i-1)]
# table-type gq-alpha
if 'EXTENDEDS_ALPH' in uci:
ttable = uci['EXTENDEDS_ALPH']
for i in range(1,19):
ui['alph' + str(i)] = ttable['ALPH' + str(i-1)]
# table-type gq-gamma
if 'EXTENDEDS_GAMM' in uci:
ttable = uci['EXTENDEDS_GAMM']
for i in range(1,19):
ui['gamm' + str(i)] = ttable['GAMM' + str(i-1)]
# table-type gq-delta
if 'EXTENDEDS_DEL' in uci:
ttable = uci['EXTENDEDS_DEL']
for i in range(1,19):
ui['delta' + str(i)] = ttable['DEL' + str(i-1)]
# table-type gq-cldfact
if 'EXTENDEDS_KCLD' in uci:
ttable = uci['EXTENDEDS_KCLD']
for i in range(1,19):
ui['kcld' + str(i)] = ttable['KCLD' + str(i-1)]
# add contents of ui_parms to ui
for key, value in ui_parms.items():
if type(value) in {int, float}:
ui[key] = float(value)
# ui_combined = {**ui, **ui_parms}
############################################################################
errors = _gqual_(ui, ts) # run GQUAL simulation code
############################################################################
if nexits > 1:
u = uci['SAVE']
name = 'GQUAL' + str(index) # arbitrary identification
key1 = name + '_ODQAL'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['ODQAL']
del u['ODQAL']
key1 = name + '_OSQAL1'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['OSQAL']
key1 = name + '_OSQAL2'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['OSQAL']
key1 = name + '_OSQAL3'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['OSQAL']
del u['OSQAL']
key1 = name + '_TOSQAL'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['TOSQAL']
del u['TOSQAL']
return errors, ERRMSGS
#@njit(cache=True)
def _gqual_(ui, ts):
''' Simulate the behavior of a generalized quality constituent'''
errors = zeros(int(ui['errlen'])).astype(int64)
simlen = int(ui['simlen'])
nexits = int(ui['NEXITS'])
uunits = int(ui['uunits'])
delt60 = ui['delt60']
delts = ui['delts']
index = int(ui['index'])
ngqual = int(ui['ngqual'])
ddqal = zeros((8, ngqual + 1))
AFACT = 43560.0
if uunits == 2:
# si units conversion
AFACT = 1000000.0
tempfg = 2
phflag = 2
roxfg = 2
cldfg = 2
sdfg = 2
phytfg = 2
if 'TEMPFG' in ui:
tempfg = int(ui['TEMPFG'])
phflag = int(ui['PHFLAG'])
roxfg = int(ui['ROXFG'])
cldfg = int(ui['CLDFG'])
sdfg = int(ui['SDFG'])
phytfg = int(ui['PHYTFG'])
lat = int(ui['LAT'])
lkfg = int(ui['LKFG'])
len_ = 0.0
delth= 0.0
if 'LEN' in ui:
len_ = ui["LEN"] * 5280.0 # mi to feet
delth = ui["DELTH"]
if uunits == 2:
len_ = ui['LEN'] * 1000.0 # length of reach, in meters
HRFG = ts['HRFG'].astype(int64)
# table-type gq-qaldata
# qualid = ui['GQID']
dqal = ui['DQAL']
# concid = ui['CONCID']
conv = ui['CONV']
# qtyid = ui['QTYID']
vol = ui['vol']
svol = ui['svol']
rdqal = dqal * vol
cinv = 1.0 / conv # get reciprocal of unit conversion factor
# process flags for this constituent
# table-type gq-qalfg
qalfg = zeros(8)
qalfg[1] = ui['QALFG1']
qalfg[2] = ui['QALFG2']
qalfg[3] = ui['QALFG3']
qalfg[4] = ui['QALFG4']
qalfg[5] = ui['QALFG5']
qalfg[6] = ui['QALFG6']
qalfg[7] = ui['QALFG7']
# table-type gq-flg2
gqpm2 = zeros(8)
gqpm2[7] = 2
if 'GQPM21' in ui:
gqpm2[1] = ui['GQPM21']
gqpm2[2] = ui['GQPM22']
gqpm2[3] = ui['GQPM23']
gqpm2[4] = ui['GQPM24']
gqpm2[5] = ui['GQPM25']
gqpm2[6] = ui['GQPM26']
gqpm2[7] = ui['GQPM27']
# process parameters for this constituent
ka = 0.0
kb = 0.0
kn = 0.0
thhyd = 0.0
kox = 0.0
thox = 0.0
if qalfg[1] == 1: # qual undergoes hydrolysis
# HYDPM(1,I) # table-type gq-hydpm
ka = ui['KA'] * delts # convert rates from /sec to /ivl
kb = ui['KB'] * delts
kn = ui['KN'] * delts
thhyd = ui['THHYD']
if qalfg[2] == 1: # qual undergoes oxidation by free radical processes
# ROXPM(1,I) # table-type gq-roxpm
kox = ui['KOX'] * delts # convert rates from /sec to /ivl
thox = ui['THOX']
photpm = zeros(21)
if qalfg[3] == 1: # qual undergoes photolysis
# PHOTPM(1,I) # table-type gq-photpm
if 'photpm1' in ui:
for i in range(1,21):
photpm[i] = ui['photpm' + str(i)]
cfgas = 0.0
if qalfg[4] == 1: # qual undergoes volatilization
cfgas = ui['CFGAS'] # table-type gq-cfgas
biocon = 0.0
thbio = 0.0
biop = 0.0
if qalfg[5] == 1: # qual undergoes biodegradation
# BIOPM(1,I) # table-type gq-biopm
biocon = ui['BIOCON'] * delt60 / 24.0 # convert rate from /day to /ivl
thbio = ui['THBIO']
biop = ui['BIO']
fstdec = 0.0
thfst = 0.0
if qalfg[6] == 1: # qual undergoes "general" decay
# GENPM(1,I)) # table-type gq-gendecay
fstdec = ui['FSTDEC'] * delt60 / 24.0 # convert rate from /day to /ivl
thfst = ui['THFST']
adpm1 = zeros(7)
adpm2 = zeros(7)
adpm3 = zeros(7)
rsed = zeros(7)
sqal = zeros(7)
if qalfg[7] == 1: # constituent is sediment-associated
# get all required additional input
# ADDCPM # table-type gq-seddecay
# convert rates from /day to /ivl
addcpm1 = ui['ADDCP1'] * delt60 / 24.0 # convert rate from /day to /ivl
addcpm2 = ui['ADDCP2']
addcpm3 = ui['ADDCP3'] * delt60 / 24.0 # convert rate from /day to /ivl
addcpm4 = ui['ADDCP4']
# table-type gq-kd
adpm1[1] = ui['ADPM11']
adpm1[2] = ui['ADPM21']
adpm1[3] = ui['ADPM31']
adpm1[4] = ui['ADPM41']
adpm1[5] = ui['ADPM51']
adpm1[6] = ui['ADPM61']
# gq-adrate
adpm2[1] = ui['ADPM12'] * delt60 / 24.0 # convert rate from /day to /ivl
adpm2[2] = ui['ADPM22'] * delt60 / 24.0 # convert rate from /day to /ivl
adpm2[3] = ui['ADPM32'] * delt60 / 24.0 # convert rate from /day to /ivl
adpm2[4] = ui['ADPM42'] * delt60 / 24.0 # convert rate from /day to /ivl
adpm2[5] = ui['ADPM52'] * delt60 / 24.0 # convert rate from /day to /ivl
adpm2[6] = ui['ADPM62'] * delt60 / 24.0 # convert rate from /day to /ivl
# table-type gq-adtheta
if 'ADPM13' in ui:
adpm3[1] = ui['ADPM13']
adpm3[2] = ui['ADPM23']
adpm3[3] = ui['ADPM33']
adpm3[4] = ui['ADPM43']
adpm3[5] = ui['ADPM53']
adpm3[6] = ui['ADPM63']
else:
adpm3[1] = 1.07
adpm3[2] = 1.07
adpm3[3] = 1.07
adpm3[4] = 1.07
adpm3[5] = 1.07
adpm3[6] = 1.07
# table-type gq-sedconc
sqal[1] = ui['SQAL1']
sqal[2] = ui['SQAL2']
sqal[3] = ui['SQAL3']
sqal[4] = ui['SQAL4']
sqal[5] = ui['SQAL5']
sqal[6] = ui['SQAL6']
# find the total quantity of material on various forms of sediment
RSED1 = ts['RSED1'] # sediment storages - suspended sand
RSED2 = ts['RSED2'] # sediment storages - suspended silt
RSED3 = ts['RSED3'] # sediment storages - suspended clay
RSED4 = ts['RSED4'] # sediment storages - bed sand
RSED5 = ts['RSED5'] # sediment storages - bed silt
RSED6 = ts['RSED6'] # sediment storages - bed clay
rsed1 = RSED1[0]
rsed2 = RSED2[0]
rsed3 = RSED3[0]
if 'SSED1' in ui:
rsed1 = ui['SSED1']
rsed2 = ui['SSED2']
rsed3 = ui['SSED3']
if uunits == 1:
rsed[1] = RSED1[0] / 3.121E-08
rsed[2] = RSED2[0] / 3.121E-08
rsed[3] = RSED3[0] / 3.121E-08
rsed[4] = RSED4[0] / 3.121E-08
rsed[5] = RSED5[0] / 3.121E-08
rsed[6] = RSED6[0] / 3.121E-08
else:
rsed[1] = RSED1[0] / 1E-06 # 2.83E-08
rsed[2] = RSED2[0] / 1E-06
rsed[3] = RSED3[0] / 1E-06
rsed[4] = RSED4[0] / 1E-06
rsed[5] = RSED5[0] / 1E-06
rsed[6] = RSED6[0] / 1E-06
rsqal1 = sqal[1] * rsed1 * svol
rsqal2 = sqal[2] * rsed2 * svol
rsqal3 = sqal[3] * rsed3 * svol
rsqal4 = rsqal1 + rsqal2 + rsqal3
rsqal5 = sqal[4] * rsed[4]
rsqal6 = sqal[5] * rsed[5]
rsqal7 = sqal[6] * rsed[6]
rsqal8 = rsqal5 + rsqal6 + rsqal7
rsqal9 = rsqal1 + rsqal5
rsqal10 = rsqal2 + rsqal6
rsqal11 = rsqal3 + rsqal7
rsqal12 = rsqal9 + rsqal10 + rsqal11
else:
# qual not sediment-associated
rsqal1 = 0.0
rsqal2 = 0.0
rsqal3 = 0.0
rsqal4 = 0.0
rsqal5 = 0.0
rsqal6 = 0.0
rsqal7 = 0.0
rsqal8 = 0.0
rsqal9 = 0.0
rsqal10 = 0.0
rsqal11 = 0.0
rsqal12 = 0.0
# find total quantity of qual in the rchres
rrqal = rdqal + rsqal12
gqst1 = rrqal
# find values for global flags
# gqalfg indicates whether any qual undergoes each of the decay processes or is sediment-associated
# qalgfg indicates whether a qual undergoes any of the 6 decay processes
qalgfg = 0
if qalfg[1] > 0 or qalfg[2] > 0 or qalfg[3] > 0 or qalfg[4] > 0 or qalfg[5] > 0 or qalfg[6] > 0:
qalgfg = 1
# gdaufg indicates whether any constituent is a "daughter" compound through each of the 6 possible decay processes
gdaufg = 0
if gqpm2[1] > 0 or gqpm2[2] > 0 or gqpm2[3] > 0 or gqpm2[4] > 0 or gqpm2[5] > 0 or gqpm2[6] > 0:
gdaufg = 1
# daugfg indicates whether or not a given qual is a daughter compound
daugfg = 0
if gqpm2[1] > 0 or gqpm2[2] > 0 or gqpm2[3] > 0 or gqpm2[4] > 0 or gqpm2[5] > 0 or gqpm2[6] > 0:
daugfg = 1
# get initial value for all inputs which can be constant,
# vary monthly, or be a time series-some might be over-ridden by
# monthly values or time series
alph = zeros(19)
gamm = zeros(19)
delta = zeros(19)
kcld = zeros(19)
fact1 = 0.0
light = 1
if qalfg[3] == 1:
# table-type gq-alpha
if 'alph1' in ui:
for i in range(1, 19):
alph[i] = ui['alph' + str(i)]
# table-type gq-gamma
if 'gamm1' in ui:
for i in range(1, 19):
gamm[i] = ui['gamm' + str(i)]
# table-type gq-delta
if 'delta1' in ui:
for i in range(1, 19):
delta[i] = ui['delta' + str(i)]
# table-type gq-cldfact
if 'kcld1' in ui:
for i in range(1, 19):
kcld[i] = ui['kcld' + str(i)]
cfsaex = 1.0
if 'CFSAEX' in ui:
cfsaex = ui['CFSAEX']
# fact1 is a pre-calculated value used in photolysis simulation
fact1 = cfsaex * delt60 / 24.0
# decide which set of light data to use
light = (abs(int(lat)) + 5) // 10
if light == 0: # no table for equation, so use 10 deg table
light = 1
lset_month = ts['DAYVAL']
reamfg = 0
cforea = 0.0
tcginv = 0.0
reak = 0.0
reakt = 0.0
expred = 0.0
exprev = 0.0
if qalfg[4] == 1:
# one or more constituents undergoes volatilization process- input required to compute reaeration coefficient
# flags - table-type ox-flags
reamfg = 2
if 'REAMFG' in ui:
reamfg = ui["REAMFG"]
dopfg = 0
if 'DOPFG' in ui:
dopfg = ui["DOPFG"]
htfg = int(ui['HTFG'])
if htfg == 0:
elev = ui["ELEV"]
cfpres = ((288.0 - 0.001981 * elev) / 288.0) ** 5.256
lkfg = int(ui['LKFG'])
if lkfg == 1:
# table-type ox-cforea
cforea = 1.0
if 'CFOREA' in ui:
cforea = ui["CFOREA"]
if 'CFOREA' in ui:
cforea = ui["CFOREA"]
else:
if reamfg == 1:
# tsivoglou method - table-type ox-tsivoglou
reakt = ui["REAKT"]
tcginv = ui["TCGINV"]
elif reamfg == 2:
# owen/churchill/o'connor-dobbins # table-type ox-tcginv
tcginv = 1.047
if "TCGINV" in ui:
tcginv = ui["TCGINV"]
elif reamfg == 3:
# user formula - table-type ox-reaparm
tcginv = ui["TCGINV"]
reak = ui["REAK"]
expred = ui["EXPRED"]
exprev = ui["EXPREV"]
# process tables specifying relationship between "parent" and "daughter" compounds
# table-type gq-daughter
c = zeros((8,7))
if 'C21' in ui:
c[2,1] = ui["C21"]
c[3,1] = ui["C31"]
c[4,1] = ui["C41"]
c[5,1] = ui["C51"]
c[6,1] = ui["C61"]
c[7,1] = ui["C71"]
c[3,2] = ui["C32"]
c[4,2] = ui["C42"]
c[5,2] = ui["C52"]
c[6,2] = ui["C62"]
c[7,2] = ui["C72"]
c[4,3] = ui["C43"]
c[5,3] = ui["C53"]
c[6,3] = ui["C63"]
c[7,3] = ui["C73"]
c[5,4] = ui["C54"]
c[6,4] = ui["C64"]
c[7,4] = ui["C74"]
c[6,5] = ui["C65"]
c[7,5] = ui["C75"]
c[7,6] = ui["C76"]
if qalfg[7] == 1: # one or more quals are sediment-associated
sedfg = int(ui['SEDFG'])
if sedfg == 0: # section sedtrn not active
#ERRMSG
errors[0] += 1 # ERRMSG0: one or more gquals are sediment-associated, but section sedtrn not active
hydrfg = int(ui['HYDRFG'])
aux1fg = int(ui['AUX1FG'])
aux2fg = int(ui['AUX2FG'])
if hydrfg == 1: # check that required options in section hydr have been selected
if qalfg[3] == 1 and aux1fg == 0:
errors[1] += 1 # ERRMSG1: simulation of photolysis requires aux1fg to be on to calculate average depth
if qalfg[4] == 1:
lkfg = int(ui['LKFG'])
if lkfg == 0:
if aux2fg == 0:
errors[2] += 1 # ERRMSG2: simulation of volatilization in a free flowing stream requires aux3fg on
else:
if aux1fg == 0:
errors[3] += 1 # ERRMSG3: simulation of volatilization in a lake requires aux1fg on to calculate average depth
##################### end PGQUAL
if tempfg == 1:
if 'TW' not in ts:
errors[6] += 1 # ERRMSG6: timeseries not available
ts['TW_GQ'] = zeros(simlen)
else:
ts['TW_GQ'] = ts['TW']
if phflag == 1:
if 'PHVAL' not in ts:
errors[7] += 1 # ERRMSG7: timeseries not available
ts['PHVAL_GQ'] = zeros(simlen)
else:
ts['PHVAL_GQ'] = ts['PHVAL']
if roxfg == 1:
if 'ROC' not in ts:
errors[8] += 1 # ERRMSG8: timeseries not available
ts['ROC_GQ'] = zeros(simlen)
else:
ts['ROC_GQ'] = ts['ROC']
if cldfg == 1:
if 'CLOUD' not in ts:
errors[9] += 1 # ERRMSG9: timeseries not available
ts['CLOUD_GQ'] = zeros(simlen)
else:
ts['CLOUD_GQ'] = ts['CLOUD']
if sdfg == 1:
if 'SSED4' not in ts:
errors[10] += 1 # ERRMSG10: timeseries not available
ts['SDCNC_GQ'] = zeros(simlen)
else:
ts['SDCNC_GQ'] = ts['SSED4']
if phytfg == 1:
if 'PHYTO' not in ts:
errors[11] += 1 # ERRMSG11: timeseries not available
ts['PHYTO_GQ'] = zeros(simlen)
else:
ts['PHYTO_GQ'] = ts['PHYTO']
# get input timeseries
TW = ts['TW_GQ']
PHVAL = ts['PHVAL_GQ']
ROC = ts['ROC_GQ']
CLD = ts['CLOUD_GQ']
SDCNC = ts['SDCNC_GQ']
PHYTO = ts['PHYTO_GQ']
AVDEP = ts['AVDEP']
WIND = ts['WIND'] * 1609.0 # miles to meters
AVVEL = ts['AVVEL']
PREC = ts['PREC']
SAREA = ts['SAREA']
GQADFX = ts['GQADFX']
GQADCN = ts['GQADCN']
if 'BIO' not in ts:
ts['BIO'] = zeros(simlen)
BIO = ts['BIO']
VOL = ts['VOL']
SROVOL = ts['SROVOL']
EROVOL = ts['EROVOL']
SOVOL = zeros((simlen, nexits))
EOVOL = zeros((simlen, nexits))
for i in range(nexits):
SOVOL[:, i] = ts['SOVOL' + str(i + 1)]
EOVOL[:, i] = ts['EOVOL' + str(i + 1)]
# get incoming flow of constituent or zeros;
if ('GQUAL' + str(index) + '_IDQAL') not in ts:
ts['GQUAL' + str(index) + '_IDQAL'] = zeros(simlen)
IDQAL = ts['GQUAL' + str(index) + '_IDQAL']
if ('GQUAL' + str(index) + '_ISQAL1') not in ts:
ts['GQUAL' + str(index) + '_ISQAL1'] = zeros(simlen)
ISQAL1 = ts['GQUAL' + str(index) + '_ISQAL1']
if ('GQUAL' + str(index) + '_ISQAL2') not in ts:
ts['GQUAL' + str(index) + '_ISQAL2'] = zeros(simlen)
ISQAL2 = ts['GQUAL' + str(index) + '_ISQAL2']
if ('GQUAL' + str(index) + '_ISQAL3') not in ts:
ts['GQUAL' + str(index) + '_ISQAL3'] = zeros(simlen)
ISQAL3 = ts['GQUAL' + str(index) + '_ISQAL3']
if qalfg[7] == 1: # this constituent is associated with sediment
DEPSCR1 = ts['DEPSCR1']
DEPSCR2 = ts['DEPSCR2']
DEPSCR3 = ts['DEPSCR3']
ROSED1 = ts['ROSED1']
ROSED2 = ts['ROSED2']
ROSED3 = ts['ROSED3']
OSED1 = zeros((simlen, nexits))
OSED2 = zeros((simlen, nexits))
OSED3 = zeros((simlen, nexits))
for timeindex in range(simlen):
if nexits > 1:
for xindex in range(nexits):
OSED1[timeindex, xindex] = ts['OSED1' + str(xindex + 1)][timeindex]
OSED2[timeindex, xindex] = ts['OSED2' + str(xindex + 1)][timeindex]
OSED3[timeindex, xindex] = ts['OSED3' + str(xindex + 1)][timeindex]
else:
OSED1[timeindex, 0] = ts['ROSED1'][timeindex]
OSED2[timeindex, 0] = ts['ROSED2'][timeindex]
OSED3[timeindex, 0] = ts['ROSED3'][timeindex]
# this number is used to adjust reaction rates for temperature
# TW20 = TW - 20.0
name = 'GQUAL' + str(index) # arbitrary identification
# preallocate output arrays (always needed)
ADQAL1 = ts[name + '_ADQAL1'] = zeros(simlen)
ADQAL2 = ts[name + '_ADQAL2'] = zeros(simlen)
ADQAL3 = ts[name + '_ADQAL3'] = zeros(simlen)
ADQAL4 = ts[name + '_ADQAL4'] = zeros(simlen)
ADQAL5 = ts[name + '_ADQAL5'] = zeros(simlen)
ADQAL6 = ts[name + '_ADQAL6'] = zeros(simlen)
ADQAL7 = ts[name + '_ADQAL7'] = zeros(simlen)
DDQAL1 = ts[name + '_DDQAL1'] = zeros(simlen)
DDQAL2 = ts[name + '_DDQAL2'] = zeros(simlen)
DDQAL3 = ts[name + '_DDQAL3'] = zeros(simlen)
DDQAL4 = ts[name + '_DDQAL4'] = zeros(simlen)
DDQAL5 = ts[name + '_DDQAL5'] = zeros(simlen)
DDQAL6 = ts[name + '_DDQAL6'] = zeros(simlen)
DDQAL7 = ts[name + '_DDQAL7'] = zeros(simlen)
DQAL = ts[name + '_DQAL'] = zeros(simlen)
DSQAL1 = ts[name + '_DSQAL1'] = zeros(simlen)
DSQAL2 = ts[name + '_DSQAL2'] = zeros(simlen)
DSQAL3 = ts[name + '_DSQAL3'] = zeros(simlen)
DSQAL4 = ts[name + '_DSQAL4'] = zeros(simlen)
GQADDR = ts[name + '_GQADDR'] = zeros(simlen)
GQADEP = ts[name + '_GQADEP'] = zeros(simlen)
GQADWT = ts[name + '_GQADWT'] = zeros(simlen)
ISQAL4 = ts[name + '_ISQAL4'] = zeros(simlen)
PDQAL = ts[name + '_PDQAL'] = zeros(simlen)
RDQAL = ts[name + '_RDQAL'] = zeros(simlen)
RODQAL = ts[name + '_RODQAL'] = zeros(simlen)
ROSQAL1= ts[name + '_ROSQAL1'] = zeros(simlen)
ROSQAL2= ts[name + '_ROSQAL2'] = zeros(simlen)
ROSQAL3= ts[name + '_ROSQAL3'] = zeros(simlen)
ROSQAL4= ts[name + '_ROSQAL4'] = zeros(simlen)
RRQAL = ts[name + '_RRQAL'] = zeros(simlen)
RSQAL1 = ts[name + '_RSQAL1'] = zeros(simlen)
RSQAL2 = ts[name + '_RSQAL2'] = zeros(simlen)
RSQAL3 = ts[name + '_RSQAL3'] = zeros(simlen)
RSQAL4 = ts[name + '_RSQAL4'] = zeros(simlen)
RSQAL5 = ts[name + '_RSQAL5'] = zeros(simlen)
RSQAL6 = ts[name + '_RSQAL6'] = zeros(simlen)
RSQAL7 = ts[name + '_RSQAL7'] = zeros(simlen)
RSQAL8 = ts[name + '_RSQAL8'] = zeros(simlen)
RSQAL9 = ts[name + '_RSQAL9'] = zeros(simlen)
RSQAL10= ts[name + '_RSQAL10'] = zeros(simlen)
RSQAL11= ts[name + '_RSQAL11'] = zeros(simlen)
RSQAL12= ts[name + '_RSQAL12'] = zeros(simlen)
SQAL1 = ts[name + '_SQAL1'] = zeros(simlen)
SQAL2 = ts[name + '_SQAL2'] = zeros(simlen)
SQAL3 = ts[name + '_SQAL3'] = zeros(simlen)
SQAL4 = ts[name + '_SQAL4'] = zeros(simlen)
SQAL5 = ts[name + '_SQAL5'] = zeros(simlen)
SQAL6 = ts[name + '_SQAL6'] = zeros(simlen)
SQDEC1 = ts[name + '_SQDEC1'] = zeros(simlen)
SQDEC2 = ts[name + '_SQDEC2'] = zeros(simlen)
SQDEC3 = ts[name + '_SQDEC3'] = zeros(simlen)
SQDEC4 = ts[name + '_SQDEC4'] = zeros(simlen)
SQDEC5 = ts[name + '_SQDEC5'] = zeros(simlen)
SQDEC6 = ts[name + '_SQDEC6'] = zeros(simlen)
SQDEC7 = ts[name + '_SQDEC7'] = zeros(simlen)
TIQAL = ts[name + '_TIQAL'] = zeros(simlen)
TROQAL = ts[name + '_TROQAL'] = zeros(simlen)
TOQAL = zeros((simlen, nexits))
ODQAL = zeros((simlen, nexits))
OSQAL1 = zeros((simlen, nexits))
OSQAL2 = zeros((simlen, nexits))
OSQAL3 = zeros((simlen, nexits))
TOSQAL = zeros((simlen, nexits))
for loop in range(simlen):
# within time loop
# tw20 may be required for bed decay of qual even if tw is undefined (due to vol=0.0)
tw = TW[loop]
if uunits == 1:
tw = (tw - 32.0) * 0.5555 # 5.0 / 9.0
tw20 = tw - 20.0 # TW20[loop]
if tw <= -10.0:
tw20 = 0.0
# correct unrealistically high values of tw calculated in htrch
if tw >= 50.0:
tw20 = 30.0
phval = PHVAL[loop]
roc = ROC[loop]
cld = CLD[loop]
sdcnc = SDCNC[loop]
phy = PHYTO[loop]
prec = PREC[loop]
sarea= SAREA[loop]
vol = VOL[loop] * AFACT
toqal = TOQAL[loop]
tosqal = TOSQAL[loop]
# initialize sediment-related variables:
if qalfg[7] == 1: # constituent is sediment-associated
osed1 = zeros(nexits)
osed2 = zeros(nexits)
osed3 = zeros(nexits)
# define conversion factor:
cf = 1.0
if uunits == 1:
cf = 3.121e-8
else:
cf = 1.0e-6
depscr1 = DEPSCR1[loop] / cf
depscr2 = DEPSCR2[loop] / cf
depscr3 = DEPSCR3[loop] / cf
rosed1 = ROSED1[loop] / cf
rosed2 = ROSED2[loop] / cf
rosed3 = ROSED3[loop] / cf
for i in range(nexits):
osed1[i] = OSED1[loop, i] / cf
osed2[i] = OSED2[loop, i] / cf
osed3[i] = OSED3[loop, i] / cf
rsed = zeros(7)
rsed[1] = RSED1[loop] / cf
rsed[2] = RSED2[loop] / cf
rsed[3] = RSED3[loop] / cf
rsed[4] = RSED4[loop] / cf
rsed[5] = RSED5[loop] / cf
rsed[6] = RSED6[loop] / cf
isqal1 = ISQAL1[loop]
isqal2 = ISQAL2[loop]
isqal3 = ISQAL3[loop]
if uunits == 2: # uci is in metric units
avdepm = AVDEP[loop]
avdepe = AVDEP[loop] * 3.28
avvele = AVVEL[loop] * 3.28
else: # uci is in english units
avdepm = AVDEP[loop] * 0.3048
avdepe = AVDEP[loop]
avvele = AVVEL[loop]
fact2 = zeros(19)
if qalfg[3] > 0:
# one or more constituents undergoes photolysis decay
if avdepe > 0.17:
# depth of water in rchres is greater than two inches -
# consider photolysis; this criteria will also be applied to other decay processes
lset = lset_month[loop]
# southern hemisphere is 2 seasons out of phase
if lat < 0:
lset += 2
if lset > 4:
lset -= 4
for l in range(1, 19):
# evaluate the light extinction exponent- 2.76*klamda*d
kl = alph[l] + gamm[l] * sdcnc + delta[l] * phy
expnt= 2.76 * kl * avdepm * 100.0
# evaluate the cloud factor
cldl= (10.0 - cld * kcld[l]) / 10.0
if expnt <= -20.0:
expnt = -20.
if expnt >= 20.0:
expnt = 20.
# evaluate the precalculated factors fact2
# lit is data from the seq file
# fact2[l] = cldl * lit[l,lset] * (1.0 - exp(-expnt)) / expnt
fact2[l] = cldl * light_factor(l,lset,light) * (1.0 - exp(-expnt)) / expnt
else:
# depth of water in rchres is less than two inches -photolysis is not considered
pass
korea = 0.0
if qalfg[4] > 0:
# prepare to simulate volatilization by finding the oxygen reaeration coefficient
wind = 0.0
if lkfg == 1:
wind = WIND[loop]
if avdepe > 0.17: # rchres depth is sufficient to consider volatilization
# compute oxygen reaeration rate-korea
korea = oxrea(lkfg, wind, cforea, avvele, avdepe, tcginv, reamfg, reak, reakt, expred, exprev,
len_, delth, tw, delts, delt60, uunits)
# KOREA = OXREA(LKFG,WIND,CFOREA,AVVELE,AVDEPE,TCGINV,REAMFG,REAK,REAKT,EXPRED,EXPREV,LEN, DELTH,TWAT,DELTS,DELT60,UUNITS,KOREA)
else:
# rchres depth is not sufficient to consider volatilization
pass
# get data on inflow of dissolved material
gqadfx = GQADFX[loop]
gqadcn = GQADCN[loop]
gqaddr = sarea * conv * gqadfx # dry deposition;
gqadwt = prec * sarea * gqadcn # wet deposition;
gqadep = gqaddr + gqadwt # total atmospheric deposition
idqal = IDQAL[loop] * conv
indqal = idqal + gqaddr + gqadwt
# simulate advection of dissolved material
srovol = SROVOL[loop]
erovol = EROVOL[loop]
sovol = SOVOL[loop, :]
eovol = EOVOL[loop, :]
dqal, rodqal, odqal = advect(indqal, dqal, nexits, svol, vol, srovol, erovol, sovol, eovol)
bio = biop
if qalfg[5] > 0:
# get biomass input, if required (for degradation)
bio = BIO[loop]
if avdepe > 0.17: # simulate decay of dissolved material
hr = HRFG[loop]
ddqal[:,index] = ddecay(qalfg, tw20, ka, kb, kn, thhyd, phval,kox,thox, roc, fact2, fact1, photpm, korea, cfgas,
biocon, thbio, bio, fstdec, thfst, vol, dqal, hr, delt60)
# ddqal[1,index] = DDECAY(QALFG(1,I),TW20,HYDPM(1,I),PHVAL,ROXPM(1,I),ROC,FACT2(1),FACT1,PHOTPM(1,I),KOREA,CFGAS(I),
# BIOPM(1,I),BIO(I),GENPM(1,I),VOLSP,DQAL(I),HR,DELT60,DDQAL(1,I))
pdqal = 0.0
for k in range(1, 7):
if gqpm2[k] == 1: # this compound is a "daughter"-compute the contribution to it from its "parent(s)"
itobe = index - 1
for j in range(1,itobe):
pdqal = pdqal + ddqal[k,j]*c[j,k]
# update the concentration to account for decay and for input
# from decay of "parents"- units are conc/l
if vol > 0:
dqal = dqal + (pdqal - ddqal[7,index])/vol
else:
# rchres depth is less than two inches - dissolved decay is not considered
for l in range(1, 8):
ddqal[l,index] = 0.0
# 320 CONTINUE
pdqal = 0.0
adqal = zeros(8)
dsqal1 = 0.0
dsqal2 = 0.0
dsqal3 = 0.0
dsqal4 = 0.0
osqal1 = zeros(nexits)
osqal2 = zeros(nexits)
osqal3 = zeros(nexits)
osqal4 = zeros(nexits)
rosqal1 = 0.0
rosqal2 = 0.0
rosqal3 = 0.0
sqdec1 = 0.0
sqdec2 = 0.0
sqdec3 = 0.0
sqdec4 = 0.0
sqdec5 = 0.0
sqdec6 = 0.0
sqdec7 = 0.0
# zero the accumulators