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 pathCONS.py
241 lines (194 loc) · 6.78 KB
/
CONS.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
''' Copyright (c) 2020 by RESPEC, INC.
Authors: Robert Heaphy, Ph.D. and Paul Duda
License: LGPL2
'''
"""
C convert variables to external units
FACTA= 1.0/CCONV(I)
C rchres-wide variables
PIFLX= CNIF(I,LEV)*FACTA
C storages
PRCON = CNST(I,1)*FACTA
PRCONS= CNST(I,LEV)*FACTA
C computed fluxes
PCFLX1= CNCF1(I,LEV)*FACTA
"""
'''
NOTE: NCONS derived from DataFrame length ????
NOTE: COADFG flags NOT saved. Use logic: if ts present, use it; otherwise look for monthly table.
'''
from numpy import zeros
from HSP2.ADCALC import advect
from numba import njit
from HSP2.utilities import make_numba_dict, initm
ERRMSG = []
def cons(io_manager, siminfo, uci, ts):
''' Simulate behavior of conservative constituents; calculate concentration
of conservative constituents after advection'''
errorsV = zeros(len(ERRMSG), dtype=int)
simlen = siminfo['steps']
uunits = siminfo['units']
AFACT = 43560.0
if uunits == 2:
# si units conversion constants, 1 hectare is 10000 sq m
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)
nexits = int(ui['NEXITS'])
ui['simlen'] = siminfo['steps']
ui['uunits'] = siminfo['units']
ui['svol'] = svol
ui['delt60'] = siminfo['delt'] / 60 # delt60 - simulation time interval in hours
# vol = ui['VOL']
# conactive = ui['CONACTIVE'] # dict
ncons = 1
if 'PARAMETERS' in uci:
if 'NCONS' in uci['PARAMETERS']:
ncons = uci['PARAMETERS']['NCONS']
for index in range(ncons):
icon = str(index + 1)
parms = uci['CONS' + icon]
conid = parms['CONID'] # string name of the conservative constituent
con = parms['CON'] # initial concentration of the conservative
concid= parms['CONCID'] # string which specifies the concentration units for the conservative constituent.
ui['conv'] = parms['CONV'] # conversion factor from QTYID/VOL to the desired concentration units
qtyid = parms['QTYID'] # string which specifies the units for inflow or outflow of constituent; e.g. kg
name = 'CONS' + icon # arbitrary identification, default CONxx
ui['icon'] = index + 1
ui['con'] = con
# # dry deposition; flag: COADFG; monthly COAFXM; value: COADFX
# COADFG1 = ui['COADFG1'] # table-type cons-ad-flags
# COADFX = getit() # flag: COADFG; monthly COAFXM; value: COADFX
# # wet deposition; flag: COADFG; monthly COACNM; value COADCN
# COADFG2 = ui['COADFG2'] # table-type cons-ad-flags
# COADCN = getit() # flag: COADFG; monthly COACNM; value COADCN
if 'FLAGS' in uci:
u = uci['FLAGS']
# get atmos dep timeseries
coadfg1 = u['COADFG' + str((index * 2) - 1)]
if coadfg1 > 0:
ts['COADFX'] = initm(siminfo, uci, coadfg1, 'CONS' + str(index) + '_MONTHLY/COADFX', 0.0)
elif coadfg1 == -1:
ts['COADFX'] = ts['COADFX'+ str(index)]
coadfg2 = u['COADFG' + str(index * 2)]
if coadfg2 > 0:
ts['COADCN'] = initm(siminfo, uci, coadfg2, 'CONS' + str(index) + '_MONTHLY/COADCN', 0.0)
elif coadfg2 == -1:
ts['COADCN'] = ts['COADCN' + str(index)]
if 'COADFX' not in ts:
ts['COADFX'] = zeros(simlen)
if 'COADCN' not in ts:
ts['COADCN'] = zeros(simlen)
############################################################################
errors = _cons_(ui, ts) # run CONS simulation code
############################################################################
if nexits > 1:
u = uci['SAVE']
key1 = name + '_OCON'
for i in range(nexits):
u[f'{key1}{i + 1}'] = u['OCON']
del u['OCON']
return errorsV, ERRMSG
@njit(cache=True)
def _cons_(ui, ts):
''' Simulate behavior of conservative constituents; calculate concentration
of conservative constituents after advection'''
simlen = int(ui['simlen'])
nexits = int(ui['NEXITS'])
uunits = int(ui['uunits'])
conv = ui['conv']
svol = ui['svol']
delt60 = ui['delt60']
name = 'CONS' + str(int(ui['icon']))
con = ui['con']
AFACT = 43560.0
if uunits == 2:
# si units conversion constants, 1 hectare is 10000 sq m
AFACT = 1000000.0
PREC = ts['PREC']
SAREA = ts['SAREA']
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)]
COADFX = ts['COADFX'] * delt60 / (24.0 * AFACT)
COADCN = ts['COADCN']
# preallocate output arrays (always needed)
ROCON = ts[name + '_ROCON'] = zeros(simlen)
CON = ts[name + '_CON'] = zeros(simlen)
RCON = ts[name + '_RCON'] = zeros(simlen)
# preallocate output arrays for atmospheric deposition
COADDR = ts[name + '_COADDR'] = zeros(simlen)
COADWT = ts[name + '_COADWT'] = zeros(simlen)
COADEP = ts[name + '_COADEP'] = zeros(simlen)
# get incoming flow of constituent or zeros;
if (name + '_ICON') not in ts:
ts[name + '_ICON'] = zeros(simlen)
ICON = ts[name + '_ICON'] * conv * AFACT * VOL
OCON = zeros((simlen, nexits))
for loop in range(simlen):
sarea = SAREA[loop]
prec = PREC[loop]
vol = VOL[loop] * AFACT
coadfx = COADFX[loop]
coadcn = COADCN[loop]
if vol > 0.0:
icon = ICON[loop] / vol
else:
icon = ICON[loop]
coaddr = sarea * conv * coadfx # dry deposition;
coadwt = prec * sarea * coadcn # wet deposition;
adtot = coaddr + coadwt # total atmospheric deposition
incon = icon + coaddr + coadwt
srovol = SROVOL[loop]
erovol = EROVOL[loop]
sovol = SOVOL[loop, :]
eovol = EOVOL[loop, :]
con, rocon, ocon = advect(incon, con, nexits, svol, vol, srovol, erovol, sovol, eovol)
svol = vol # svol is volume at start of time step, update for next time thru
CON[loop] = con
ROCON[loop] = rocon / conv # outflow
OCON[loop,:] = ocon / conv
RCON[loop] = con * vol / conv # total storage of constituent
COADWT[loop] = coadwt
COADDR[loop] = coaddr
COADEP[loop] = adtot
if nexits > 1:
for i in range(nexits):
ts[name + '_OCON' + str(i + 1)] = OCON[:, i]
return
def expand_CONS_masslinks(flags, uci, dat, recs):
if flags['CONS']:
ncons = 1
if 'PARAMETERS' in uci:
if 'NCONS' in uci['PARAMETERS']:
ncons = uci['PARAMETERS']['NCONS']
for i in range(1, ncons + 1):
# ICONS loop for each cons
rec = {}
rec['MFACTOR'] = dat.MFACTOR
rec['SGRPN'] = 'CONS'
if dat.SGRPN == "ROFLOW":
rec['SMEMN'] = 'ROCON'
else:
rec['SMEMN'] = 'OCON'
rec['SMEMSB1'] = dat.SMEMSB1 # first sub is exit number
rec['SMEMSB2'] = dat.SMEMSB2
rec['TMEMN'] = 'ICON'
rec['TMEMSB1'] = dat.TMEMSB1
rec['TMEMSB2'] = dat.TMEMSB2
rec['SVOL'] = dat.SVOL
recs.append(rec)
return recs