Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algae Module Tests #78

Merged
merged 22 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4a9473e
Created aggregate static_variables, dynamic_variables, and processes
imscw95 Mar 4, 2024
02db508
Corrected BWn constant
imscw95 Mar 4, 2024
05fd9fc
Corrected KsOxbod
imscw95 Mar 4, 2024
2662668
Updated nsm constants file to remove duplicates
imscw95 Mar 4, 2024
acf6203
Updated dynamic variables that were coming up as circular, solved all…
imscw95 Mar 4, 2024
9accadd
fix variables and processes
sjordan29 Mar 5, 2024
fd5f224
Corrected use_Balgae argument, had to make "a" not capitalized
imscw95 Mar 6, 2024
88f0556
Changed the L var xr.where implementation to match the `var: xr.DataA…
imscw95 Mar 6, 2024
c9aa339
Removed njit from xr.where and replaced nested xr.where with np.selec…
imscw95 Mar 11, 2024
3c053d4
Made some np.select updates
imscw95 Mar 12, 2024
e422178
Updated FL np.select
imscw95 Mar 13, 2024
03162ec
Replaced all nested xr.where with np.select.
imscw95 Mar 18, 2024
6c406b0
troubleshooting
sjordan29 Mar 18, 2024
c08fe72
Merge branch 'main' into KW_testing
kewalak Mar 18, 2024
9c26236
Removed all numba.njit.
imscw95 Mar 18, 2024
d0706dd
Start Algae Testing
kewalak Mar 18, 2024
509c4c2
Merge remote-tracking branch 'origin/IJM_NSM_nb' into KW_testing
kewalak Mar 26, 2024
69bbfef
Algae Testing
kewalak Mar 26, 2024
db5ab9a
Finished Algae Tests
kewalak Mar 27, 2024
77ac3f7
Corrected np.select syntax in the processes.py file, troubleshooted t…
imscw95 Mar 28, 2024
33a748f
Corrected all processes.py syntax and functions to work with test_6
imscw95 Mar 29, 2024
82a7ba9
change updateable static variable
sjordan29 Apr 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions examples/dev_sandbox/prof_nsm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
"""A script to allow for debugging of the NSM module."""
import time
import sys
import clearwater_modules as cwm
from clearwater_modules.nsm1.model import NutrientBudget




initial_state_values = {
'Ap': 1,
'Ab': 1,
'NH4': 1,
'NO3': 1,
'OrgN': 1,
'N2': 1,
'TIP': 1,
'OrgP': 1,
'POC': 1,
'DOC': 1,
'DIC': 1,
'POM': 1,
'CBOD': 1,
'DOX': 1,
'PX': 1,
'Alk': 1
}

algae_parameters = {
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved
'AWd': 100,
'AWc': 40,
'AWn': 7.2,
'AWp': 1,
'AWa': 1000,
'KL': 10,
'KsN': 0.04,
'KsP': 0.0012,
'mu_max_20': 1,
'kdp_20': 0.15,
'krp_20': 0.2,
'vsap': 0.15,
'growth_rate_option': 1,
'light_limitation_option': 1,
'lambda0': .5,
'lambda1': .5,
'lambda2': .5,
'lambdas': .5,
'lambdam': .5,
'Fr_PAR': .5
}

balgae_parameters = {
'BWd': 100,
'BWc': 40,
'BWn': 7.2,
'BWp': 1,
'BWa': 3500,

'KLb': 10,
'KsNb': 0.25,
'KsPb': 0.125,
'Ksb': 10,
'mub_max_20': 0.4,
'krb_20': 0.2,
'kdb_20': 0.3,
'b_growth_rate_option': 1,
'b_light_limitation_option': 1,
'Fw': 0.9,
'Fb': 0.9
}

nitrogen_parameters = {
'KNR': 0.6,
'knit_20': 0.1,
'kon_20': 0.1,
'kdnit_20': 0.002,
'rnh4_20': 0,
'vno3_20': 0,
'KsOxdn': 0.1,
'PN': 0.5,
'PNb': 0.5
}

phosphorus_parameters = {
'kop_20': 0.1,
'rpo4_20': 0
}

POM_parameters = {
'kpom_20': 0.1
}

CBOD_parameters = {
'KsOxbod': 0.5,
'kbod_20': 0.12,
'ksbod_20': 0
}

carbon_parameters = {
'f_pocp': 0.9,
'kdoc_20': 0.01,
'f_pocb': 0.9,
'kpoc_20': 0.005,
'K_sOxmc': 1,
'pCO2': 383,
'FCO2': 0.2
}

pathogen_parameters = {
'kdx': 0.8,
'apx': 1,
'vx': 1
}

alkalinity_parameters = {
'r_alkaa': 1,
'r_alkan': 1,
'r_alkn': 1,
'r_alkden': 1,
'r_alkba': 1,
'r_alkbn': 1
}

global_parameters = {
'use_NH4': True,
'use_NO3': True,
'use_OrgN': True,
'use_TIP': True,
'use_SedFlux': False,
'use_DOX': True,
'use_Algae': True,
'use_Balgae': True,
'use_OrgP': True,
'use_POC': True,
'use_DOC': True,
'use_DIC': True,
'use_N2': True,
'use_Pathogen': True,
'use_Alk': True,
'use_POM': True
}


global_vars = {
'vson': 0.01,
'vsoc': 0.01,
'vsop': 999,
'vs': 999,
'SOD_20': 999,
'SOD_theta': 999,
'vb': 0.01,
'fcom': 0.4,
'kaw_20_user': 999,
'kah_20_user': 999,
'hydraulic_reaeration_option': 2,
'wind_reaeration_option': 2,
'timestep': 86400,
'depth': 1,
'TwaterC': 20,
'theta': 1.047,
'velocity': 1,
'flow': 2,
'topwidth': 1,
'slope': 2,
'shear_velocity': 4,
'pressure_atm': 2,
'wind_speed': 4,
'q_solar': 4,
'Solid': 1,
}

DOX_parameters = {
'DOX': 6.5,
}
N2_parameters = {}

def main(iters: int):
ti = time.time()
# define starting state values
nsm_model = NutrientBudget(
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved
initial_state_values=initial_state_values, # mandatory
algae_parameters=algae_parameters,
alkalinity_parameters=alkalinity_parameters,
balgae_parameters=balgae_parameters,
carbon_parameters=carbon_parameters,
CBOD_parameters=CBOD_parameters,
DOX_parameters=DOX_parameters,
nitrogen_parameters=nitrogen_parameters,
POM_parameters=POM_parameters,
N2_parameters=N2_parameters,
phosphorus_parameters=phosphorus_parameters,
pathogen_parameters=pathogen_parameters,
global_parameters=global_parameters,
global_vars=global_vars,
track_dynamic_variables=True, # default is true
hotstart_dataset=None, # default is None
time_dim='year', # default is "timestep"
)
# print(nsm_model.get_variable_names())
print(nsm_model.dynamic_variables_names)

for _ in range(iters):
nsm_model.increment_timestep()


if __name__ == '__main__':
if len(sys.argv) > 1:
try:
iters = int(sys.argv[1])
print(f'Running {iters} iterations.')
except ValueError:
raise ValueError('Argument must be an integer # of iterations.')
else:
print('No argument given, defaulting to 100 iteration.')
iters = 100

main(iters=iters)
181 changes: 92 additions & 89 deletions examples/model_architecture.ipynb

Large diffs are not rendered by default.

1,164 changes: 943 additions & 221 deletions examples/model_architecture_nsm.ipynb
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

40 changes: 4 additions & 36 deletions src/clearwater_modules/nsm1/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
"""
=======================================================================================
Nutrient Simulation Module 1 (NSM1)
=======================================================================================
Developed by:
* Dr. Todd E. Steissberg (ERDC-EL)
* Dr. Billy E. Johnson (ERDC-EL, LimnoTech)
* Dr. Zhonglong Zhang (Portland State University)
* Mr. Mark Jensen (HEC)
This module computes the water quality of a single computational cell. The algorithms
and structure of this program were adapted from the Fortran 95 version of this module,
developed by:
* Dr. Billy E. Johnson (ERDC-EL)
* Dr. Zhonglong Zhang (Portland State University)
* Mr. Mark Jensen (HEC)
Version 1.0
Initial Version: June 5, 2021
"""

from clearwater_modules.nsm1 import algae
from clearwater_modules.nsm1 import alkalinity
from clearwater_modules.nsm1 import balgae
from clearwater_modules.nsm1 import carbon
from clearwater_modules.nsm1 import CBOD
from clearwater_modules.nsm1 import DOX
from clearwater_modules.nsm1 import nitrogen
from clearwater_modules.nsm1 import POM


class NSM1:
def __init__(self):
pass
from clearwater_modules.nsm1 import state_variables
from clearwater_modules.nsm1 import dynamic_variables
from clearwater_modules.nsm1 import static_variables
from clearwater_modules.nsm1.model import NutrientBudget
8 changes: 5 additions & 3 deletions src/clearwater_modules/nsm1/algae/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import numba
import xarray as xr
import numpy as np
from clearwater_modules.shared.processes import arrhenius_correction
import math

Expand Down Expand Up @@ -116,7 +117,7 @@ def FL(
Ap: xr.DataArray,
PAR: xr.DataArray,
light_limitation_option: int,
KL: xr.DataArray,
KL: int,
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved
) -> xr.DataArray:
"""Calculate Algal light limitation: FL (unitless).
Expand All @@ -131,12 +132,13 @@ def FL(

KEXT = L * depth


FL = xr.where(Ap <= 0.0 or KEXT <= 0.0 or PAR <= 0.0, 0,
xr.where(light_limitation_option==1, (1.0 / KEXT) * math.log((KL + PAR) /(KL + PAR * math.exp(-KEXT))),
xr.where(light_limitation_option==2,
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)))),
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)))),
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved
xr.where(light_limitation_option==3,
xr.where(abs(KL)<0.0000000001,0,(2.718/KEXT) * (math.exp(-PAR/KL * math.exp(-KEXT)) - math.exp(-PAR/KL))), "NaN"))))
xr.where(abs(KL)< 0.0000000001,0,(2.718/KEXT) * (math.exp(-PAR/KL * math.exp(-KEXT)) - math.exp(-PAR/KL))), "NaN"))))
sjordan29 marked this conversation as resolved.
Show resolved Hide resolved


FL= xr.where(FL > 1.0, 1.0,
Expand Down
1 change: 0 additions & 1 deletion src/clearwater_modules/nsm1/balgae/dynamic_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
class Variable(base.Variable):
...


Variable(
name='mub_max_tc',
long_name='Maximum benthic algal growth rate',
Expand Down
2 changes: 1 addition & 1 deletion src/clearwater_modules/nsm1/carbon/static_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ class Variable(base.Variable):
name='roc',
long_name='O2:C ratio for carbon oxidation',
units='mg-O2/mg-C',
description='32/12',
description='O2:C ratio for carbon oxidation (32/12)',
use='static'
)
Loading
Loading