Skip to content

Commit ec0d3d0

Browse files
authored
Merge pull request #78 from EcohydrologyTeam/KW_testing
Algae Module Tests
2 parents 368bddc + 82a7ba9 commit ec0d3d0

17 files changed

+9205
-494
lines changed

examples/dev_sandbox/prof_nsm.py

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
"""A script to allow for debugging of the NSM module."""
2+
import time
3+
import sys
4+
import clearwater_modules as cwm
5+
from clearwater_modules.nsm1.model import NutrientBudget
6+
7+
8+
9+
10+
initial_state_values = {
11+
'Ap': 1,
12+
'Ab': 1,
13+
'NH4': 1,
14+
'NO3': 1,
15+
'OrgN': 1,
16+
'N2': 1,
17+
'TIP': 1,
18+
'OrgP': 1,
19+
'POC': 1,
20+
'DOC': 1,
21+
'DIC': 1,
22+
'POM': 1,
23+
'CBOD': 1,
24+
'DOX': 1,
25+
'PX': 1,
26+
'Alk': 1
27+
}
28+
29+
algae_parameters = {
30+
'AWd': 100,
31+
'AWc': 40,
32+
'AWn': 7.2,
33+
'AWp': 1,
34+
'AWa': 1000,
35+
'KL': 10,
36+
'KsN': 0.04,
37+
'KsP': 0.0012,
38+
'mu_max_20': 1,
39+
'kdp_20': 0.15,
40+
'krp_20': 0.2,
41+
'vsap': 0.15,
42+
'growth_rate_option': 1,
43+
'light_limitation_option': 1,
44+
'lambda0': .5,
45+
'lambda1': .5,
46+
'lambda2': .5,
47+
'lambdas': .5,
48+
'lambdam': .5,
49+
'Fr_PAR': .5
50+
}
51+
52+
balgae_parameters = {
53+
'BWd': 100,
54+
'BWc': 40,
55+
'BWn': 7.2,
56+
'BWp': 1,
57+
'BWa': 3500,
58+
59+
'KLb': 10,
60+
'KsNb': 0.25,
61+
'KsPb': 0.125,
62+
'Ksb': 10,
63+
'mub_max_20': 0.4,
64+
'krb_20': 0.2,
65+
'kdb_20': 0.3,
66+
'b_growth_rate_option': 1,
67+
'b_light_limitation_option': 1,
68+
'Fw': 0.9,
69+
'Fb': 0.9
70+
}
71+
72+
nitrogen_parameters = {
73+
'KNR': 0.6,
74+
'knit_20': 0.1,
75+
'kon_20': 0.1,
76+
'kdnit_20': 0.002,
77+
'rnh4_20': 0,
78+
'vno3_20': 0,
79+
'KsOxdn': 0.1,
80+
'PN': 0.5,
81+
'PNb': 0.5
82+
}
83+
84+
phosphorus_parameters = {
85+
'kop_20': 0.1,
86+
'rpo4_20': 0
87+
}
88+
89+
POM_parameters = {
90+
'kpom_20': 0.1
91+
}
92+
93+
CBOD_parameters = {
94+
'KsOxbod': 0.5,
95+
'kbod_20': 0.12,
96+
'ksbod_20': 0
97+
}
98+
99+
carbon_parameters = {
100+
'f_pocp': 0.9,
101+
'kdoc_20': 0.01,
102+
'f_pocb': 0.9,
103+
'kpoc_20': 0.005,
104+
'K_sOxmc': 1,
105+
'pCO2': 383,
106+
'FCO2': 0.2
107+
}
108+
109+
pathogen_parameters = {
110+
'kdx': 0.8,
111+
'apx': 1,
112+
'vx': 1
113+
}
114+
115+
alkalinity_parameters = {
116+
'r_alkaa': 1,
117+
'r_alkan': 1,
118+
'r_alkn': 1,
119+
'r_alkden': 1,
120+
'r_alkba': 1,
121+
'r_alkbn': 1
122+
}
123+
124+
global_parameters = {
125+
'use_NH4': True,
126+
'use_NO3': True,
127+
'use_OrgN': True,
128+
'use_TIP': True,
129+
'use_SedFlux': False,
130+
'use_DOX': True,
131+
'use_Algae': True,
132+
'use_Balgae': True,
133+
'use_OrgP': True,
134+
'use_POC': True,
135+
'use_DOC': True,
136+
'use_DIC': True,
137+
'use_N2': True,
138+
'use_Pathogen': True,
139+
'use_Alk': True,
140+
'use_POM': True
141+
}
142+
143+
144+
global_vars = {
145+
'vson': 0.01,
146+
'vsoc': 0.01,
147+
'vsop': 999,
148+
'vs': 999,
149+
'SOD_20': 999,
150+
'SOD_theta': 999,
151+
'vb': 0.01,
152+
'fcom': 0.4,
153+
'kaw_20_user': 999,
154+
'kah_20_user': 999,
155+
'hydraulic_reaeration_option': 2,
156+
'wind_reaeration_option': 2,
157+
'timestep': 86400,
158+
'depth': 1,
159+
'TwaterC': 20,
160+
'theta': 1.047,
161+
'velocity': 1,
162+
'flow': 2,
163+
'topwidth': 1,
164+
'slope': 2,
165+
'shear_velocity': 4,
166+
'pressure_atm': 2,
167+
'wind_speed': 4,
168+
'q_solar': 4,
169+
'Solid': 1,
170+
}
171+
172+
DOX_parameters = {
173+
'DOX': 6.5,
174+
}
175+
N2_parameters = {}
176+
177+
def main(iters: int):
178+
ti = time.time()
179+
# define starting state values
180+
nsm_model = NutrientBudget(
181+
initial_state_values=initial_state_values, # mandatory
182+
algae_parameters=algae_parameters,
183+
alkalinity_parameters=alkalinity_parameters,
184+
balgae_parameters=balgae_parameters,
185+
carbon_parameters=carbon_parameters,
186+
CBOD_parameters=CBOD_parameters,
187+
DOX_parameters=DOX_parameters,
188+
nitrogen_parameters=nitrogen_parameters,
189+
POM_parameters=POM_parameters,
190+
N2_parameters=N2_parameters,
191+
phosphorus_parameters=phosphorus_parameters,
192+
pathogen_parameters=pathogen_parameters,
193+
global_parameters=global_parameters,
194+
global_vars=global_vars,
195+
track_dynamic_variables=True, # default is true
196+
hotstart_dataset=None, # default is None
197+
time_dim='year', # default is "timestep"
198+
)
199+
# print(nsm_model.get_variable_names())
200+
print(nsm_model.dynamic_variables_names)
201+
202+
for _ in range(iters):
203+
nsm_model.increment_timestep()
204+
205+
206+
if __name__ == '__main__':
207+
if len(sys.argv) > 1:
208+
try:
209+
iters = int(sys.argv[1])
210+
print(f'Running {iters} iterations.')
211+
except ValueError:
212+
raise ValueError('Argument must be an integer # of iterations.')
213+
else:
214+
print('No argument given, defaulting to 100 iteration.')
215+
iters = 100
216+
217+
main(iters=iters)

examples/model_architecture.ipynb

+92-89
Large diffs are not rendered by default.

examples/model_architecture_nsm.ipynb

+943-221
Large diffs are not rendered by default.
+4-36
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
"""
2-
=======================================================================================
3-
Nutrient Simulation Module 1 (NSM1)
4-
=======================================================================================
5-
6-
Developed by:
7-
* Dr. Todd E. Steissberg (ERDC-EL)
8-
* Dr. Billy E. Johnson (ERDC-EL, LimnoTech)
9-
* Dr. Zhonglong Zhang (Portland State University)
10-
* Mr. Mark Jensen (HEC)
11-
12-
This module computes the water quality of a single computational cell. The algorithms
13-
and structure of this program were adapted from the Fortran 95 version of this module,
14-
developed by:
15-
* Dr. Billy E. Johnson (ERDC-EL)
16-
* Dr. Zhonglong Zhang (Portland State University)
17-
* Mr. Mark Jensen (HEC)
18-
19-
Version 1.0
20-
21-
Initial Version: June 5, 2021
22-
"""
23-
24-
from clearwater_modules.nsm1 import algae
25-
from clearwater_modules.nsm1 import alkalinity
26-
from clearwater_modules.nsm1 import balgae
27-
from clearwater_modules.nsm1 import carbon
28-
from clearwater_modules.nsm1 import CBOD
29-
from clearwater_modules.nsm1 import DOX
30-
from clearwater_modules.nsm1 import nitrogen
31-
from clearwater_modules.nsm1 import POM
32-
33-
34-
class NSM1:
35-
def __init__(self):
36-
pass
1+
from clearwater_modules.nsm1 import state_variables
2+
from clearwater_modules.nsm1 import dynamic_variables
3+
from clearwater_modules.nsm1 import static_variables
4+
from clearwater_modules.nsm1.model import NutrientBudget

src/clearwater_modules/nsm1/algae/processes.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import numba
55
import xarray as xr
6+
import numpy as np
67
from clearwater_modules.shared.processes import arrhenius_correction
78
import math
89

@@ -116,7 +117,7 @@ def FL(
116117
Ap: xr.DataArray,
117118
PAR: xr.DataArray,
118119
light_limitation_option: int,
119-
KL: xr.DataArray,
120+
KL: int,
120121
) -> xr.DataArray:
121122
"""Calculate Algal light limitation: FL (unitless).
122123
@@ -131,12 +132,13 @@ def FL(
131132

132133
KEXT = L * depth
133134

135+
134136
FL = xr.where(Ap <= 0.0 or KEXT <= 0.0 or PAR <= 0.0, 0,
135137
xr.where(light_limitation_option==1, (1.0 / KEXT) * math.log((KL + PAR) /(KL + PAR * math.exp(-KEXT))),
136138
xr.where(light_limitation_option==2,
137-
xr.where(abs(KL)<0.0000000001, 1, (1.0 / KEXT) * math.log( (PAR / KL + ((1.0 + (PAR / KL)**2.0)**0.5)) / (PAR * math.exp(-KEXT) / KL + ((1.0 + (PAR * math.exp(-KEXT) / KL)**2.0)**0.5)))),
139+
xr.where(abs(KL)< 0.0000000001, 1, (1.0 / KEXT) * math.log( (PAR / KL + ((1.0 + (PAR / KL)**2.0)**0.5)) / (PAR * math.exp(-KEXT) / KL + ((1.0 + (PAR * math.exp(-KEXT) / KL)**2.0)**0.5)))),
138140
xr.where(light_limitation_option==3,
139-
xr.where(abs(KL)<0.0000000001,0,(2.718/KEXT) * (math.exp(-PAR/KL * math.exp(-KEXT)) - math.exp(-PAR/KL))), "NaN"))))
141+
xr.where(abs(KL)< 0.0000000001,0,(2.718/KEXT) * (math.exp(-PAR/KL * math.exp(-KEXT)) - math.exp(-PAR/KL))), "NaN"))))
140142

141143

142144
FL= xr.where(FL > 1.0, 1.0,

src/clearwater_modules/nsm1/balgae/dynamic_variables.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
class Variable(base.Variable):
1313
...
1414

15-
1615
Variable(
1716
name='mub_max_tc',
1817
long_name='Maximum benthic algal growth rate',

src/clearwater_modules/nsm1/carbon/static_variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ class Variable(base.Variable):
7272
name='roc',
7373
long_name='O2:C ratio for carbon oxidation',
7474
units='mg-O2/mg-C',
75-
description='32/12',
75+
description='O2:C ratio for carbon oxidation (32/12)',
7676
use='static'
7777
)

0 commit comments

Comments
 (0)