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

Refactor emergent parameters code #81

Open
wants to merge 88 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
327365f
create function to dynamically expand the parameter dictionary with p…
nanophyto Oct 30, 2024
6ba3ad5
added dummy function to create N2P2ZD example palat matrix
nanophyto Oct 31, 2024
52d3147
dummy emergent growth function
nanophyto Oct 31, 2024
ab5e047
dummy emergent nitrogen saturation function
nanophyto Oct 31, 2024
1076aed
rename dummy palat function
nanophyto Oct 31, 2024
523195f
dummy emergent nitrogen half saturation function
nanophyto Oct 31, 2024
8fa6c3a
fix formatting
nanophyto Oct 31, 2024
e5bc6ff
start merging into one file
nanophyto Oct 31, 2024
84fcfc2
add NamedArrays dependency
nanophyto Oct 31, 2024
8fa7fc0
fix formatting...
nanophyto Oct 31, 2024
4fd4c5a
move emergent functions to one file
nanophyto Nov 1, 2024
1e13ffb
move helper functions
nanophyto Nov 1, 2024
3293c74
create main file that loads functions and creates dict
nanophyto Nov 1, 2024
188e236
first try at multiple dispatch (broken)
nanophyto Nov 1, 2024
1cef1f4
formatting...
nanophyto Nov 1, 2024
5d5d558
tidy/fix helper functions
nanophyto Nov 4, 2024
e95ec69
working example
nanophyto Nov 4, 2024
32f6e89
tidying
nanophyto Nov 4, 2024
d60f038
formatting
nanophyto Nov 4, 2024
30ec979
move dispatch code to emergent_functions.jl
nanophyto Nov 4, 2024
c9a0eda
create working example
nanophyto Nov 4, 2024
fa5b027
formatting
nanophyto Nov 4, 2024
9aa203d
update parameters to match N2P2D example
nanophyto Nov 4, 2024
bba282f
check NamedArrays output
nanophyto Nov 4, 2024
cb08e5b
working example of splitting step
nanophyto Nov 7, 2024
0e8dcc9
renaming for clarity
nanophyto Nov 7, 2024
10bb69a
move splitting functions to helper_functions.jl and merge examples
nanophyto Nov 7, 2024
28d487a
wrap last step into function and merge intermediate_parameter value w…
nanophyto Nov 7, 2024
8c1ea22
add biogeochem parameters
nanophyto Nov 7, 2024
f970dad
update N2P2ZD example with constant alpha
nanophyto Nov 7, 2024
842ddd1
update example with missing parameters
nanophyto Nov 7, 2024
d695d12
merge main
radka-j Nov 12, 2024
b7404a1
add tests and argument types
nanophyto Nov 13, 2024
8e11ebe
formatting :)
nanophyto Nov 13, 2024
51d90f8
rm outdated function
radka-j Nov 15, 2024
dde2eee
fix file path
radka-j Nov 15, 2024
4bb051a
start mv functions to parameters
radka-j Nov 15, 2024
44fb31f
disable tests for emergent parameters
radka-j Nov 15, 2024
2937ac7
Revert "fix file path"
radka-j Nov 15, 2024
86810ce
revert commit and fix file path
radka-j Nov 15, 2024
24738e7
undo changes
radka-j Nov 15, 2024
970702b
add separate palatability and assimilation efficiency functions
radka-j Nov 15, 2024
12d41c9
Merge branch 'N_plankton' into emergent_params
radka-j Nov 15, 2024
6336132
disable emergent params tests
radka-j Nov 15, 2024
e9a538c
recreate emergent_2D_array
radka-j Nov 15, 2024
c13ebed
update emergent functions
radka-j Nov 15, 2024
066a67a
fix assimilation example
radka-j Nov 15, 2024
436d778
minor clean up
radka-j Nov 15, 2024
e5a6560
DifferentialEquations -> OrdinaryDiffEq
radka-j Nov 18, 2024
4e2dbae
get argnames from function, not from list
radka-j Nov 18, 2024
b0fe3bc
refactor emergent functions
radka-j Nov 18, 2024
061fc17
update docstrings
radka-j Nov 18, 2024
63358b7
clean up file
radka-j Nov 18, 2024
0c2f395
add compute_darwin_parameters function
radka-j Nov 19, 2024
0ca6339
fix bugs
radka-j Nov 19, 2024
3f43722
fix bug with param names
radka-j Nov 19, 2024
225954f
add note about assimilation_efficiency param
radka-j Nov 19, 2024
c04b39c
update example
radka-j Nov 19, 2024
3e8f44f
delete helper functions file
radka-j Nov 19, 2024
bcce922
fix order of args passed to splitting functions
radka-j Nov 19, 2024
61c595b
update docstrings
radka-j Nov 19, 2024
5428904
update dummy_emergent_assimilation_efficiency function
radka-j Nov 19, 2024
fc897ef
update example parameters to match N2P2ZD example
radka-j Nov 19, 2024
901c36a
update tests
radka-j Nov 19, 2024
1453ea1
fix alpha value in example
radka-j Nov 19, 2024
bc61fa9
compare all parameters in emergent example with N2P2ZD
radka-j Nov 19, 2024
bc0dac5
formatting
radka-j Nov 19, 2024
ed240b1
add note
radka-j Nov 19, 2024
afd1a07
update example
radka-j Nov 19, 2024
4b13761
add _matrix to palatability and assimilation_efficiency parameters
radka-j Nov 20, 2024
2097808
start parameters refactor, 1D params completed (2D to do)
radka-j Nov 22, 2024
59ea3a0
update dependencies
radka-j Nov 22, 2024
37758f0
refactor palatability and assimilation
radka-j Nov 22, 2024
54d4e86
update example
radka-j Nov 25, 2024
09b7e65
rename module Dynamic -> Biogeochemistry
radka-j Nov 25, 2024
21cc955
update docstrings
radka-j Nov 25, 2024
880b594
update docstrings
radka-j Nov 26, 2024
4a2558f
update docstring
radka-j Nov 26, 2024
b781c23
rename light object
radka-j Nov 28, 2024
997610b
merge branch
radka-j Dec 2, 2024
f110e3f
update docstring
radka-j Dec 2, 2024
ce24d59
re-add dependencies dropped during branch merge
radka-j Dec 2, 2024
8c0c454
Merge branch 'main' into emergent_params
radka-j Dec 3, 2024
3d4edd8
Merge branch 'main' into emergent_params
radka-j Dec 3, 2024
4f42147
merge main
radka-j Dec 3, 2024
8554c46
clean up NPZD example
radka-j Dec 3, 2024
b64661a
add missing example dependency
radka-j Dec 3, 2024
9940070
add phyto and zoo growth functions using named arrays
radka-j Dec 3, 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
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ uuid = "41889b7c-c35c-4f16-abd7-94b299d9fd29"
version = "0.0.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673"
OceanBioME = "a49af516-9db8-4be4-be45-1dad61c5a376"
Oceananigans = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
DifferentialEquations = "7.15.0"
OceanBioME = "0.13.2"
Oceananigans = "0.93.3"
StatsPlots = "0.15.7"
Turing = "0.35.3"
julia = "1.9"

Expand Down
2 changes: 1 addition & 1 deletion examples/N2P2ZD/tracers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parameters = (
linear_mortality=[8e-7, 8e-7, 8e-7, 8e-7] / second,
quadratic_mortality=[0, 0, 1e-6, 1e-6] / second,
maximum_predation_rate=[0, 0, 8.86e-5, 4.88e-5] / second,
alpha=[0.5, 0.1953, 1e-99, 1e-99] / day,
alpha=[0.1953, 0.1953, 1e-99, 1e-99] / day,
feeding_export_poc_doc_fraction=0.5,
mortality_export_fraction=0.5,
palatability=[
Expand Down
8 changes: 0 additions & 8 deletions examples/NPZD/functions.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
monod_limitation(R, k) = R / (k + R)
holling_type_2(R, k) = R / (k + R)

Q₁₀(T) = 1.88^(T / 10) # T in °C

smith_light_limitation(PAR, α, μ₀) = α * PAR / sqrt(μ₀^2 + α^2 * PAR^2)

idealized_remineralization(D, r) = r * D
Expand All @@ -22,9 +20,3 @@ end
linear_loss(P, l) = l * P

quadratic_loss(P, l) = l * P^2

function summed_linear_loss(P, l)
return sum([
linear_loss(P[plankton_index], l[plankton_index]) for plankton_index in eachindex(P)
])
end
3 changes: 1 addition & 2 deletions examples/NPZD/tracers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ parameters = (
kₚ=0.5573,
β=0.9116,
lᶻᵈ=0.3395 / day,
lⁿ=[0.066, 0.0102] / day, #[lᵖⁿ, lᶻⁿ]
rᵈⁿ=0.1213 / day,
α=0.1953 / day,
)

tracers = Dict(
"N" => :(
summed_linear_loss([P, Z], lⁿ) + idealized_remineralization(D, rᵈⁿ) -
linear_loss(P, lᵖⁿ) + linear_loss(P, lᶻⁿ) + idealized_remineralization(D, rᵈⁿ) -
idealized_photosynthetic_growth(N, P, PAR, μ₀, kₙ, α)
),
"D" => :(
Expand Down
78 changes: 78 additions & 0 deletions examples/emergent_4P/emergent_functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

function dummy_emergent_growth(growth_a::Real, growth_b::Real, volume::Real)
rate = 0.0
if growth_a == 0
return rate
else
if volume == 1
rate = 7.190e-6
elseif volume == 10
rate = 2.216e-5
end
end

return rate
end

function dummy_emergent_predation_rate(
predation_rate_a::Real, predation_rate_b::Real, volume::Real
)
rate = 0
if predation_rate_a == 0
return rate = 0 # Early return if volume_a is zero
else
# Set rate based on the value of volume
if volume == 10
rate = 8.86e-5
elseif volume == 100
rate = 4.88e-5
end
end

return rate
end

function dummy_emergent_nitrogen_half_saturation(
nitrogen_half_saturation_a::Real, nitrogen_half_saturation_b::Real, volume::Real
)
if nitrogen_half_saturation_a == 0
return rate = 0 # Early return if volume_a is zero
else
# Set rate based on the value of volume
if volume == 1
rate = 6.73e-3
elseif volume == 10
rate = 0.12
end
end

return rate
end

function dummy_emergent_palat(prey_data, predator_data)
prey_volume = prey_data["volumes"]
predator_volume = predator_data["volumes"]
optimum_predator_prey_ratio = predator_data["optimum_predator_prey_ratio"]
protection = prey_data["protection"]

ratio = predator_volume / prey_volume

if optimum_predator_prey_ratio == 0
palat = 0.0
elseif ratio == optimum_predator_prey_ratio
palat = 1 * (1 - protection)
else
palat = 0.3 * (1 - protection)
end
return palat
end

function dummy_emergent_assimilation_efficiency(prey_data, predator_data)
assimilation_efficiency = 0
# predators don't eat other predators
# Q: is this the right condition for determining whether to retrieve this value?
if prey_data["assimilation_efficiency"] == 0
assimilation_efficiency = predator_data["assimilation_efficiency"]
end
return assimilation_efficiency
end
87 changes: 87 additions & 0 deletions examples/emergent_4P/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Oceananigans.Units
using Agate

# Q: SHOULD THIS BE A NAMEDTUPLE INSTEAD?
# TODO: users shouldn't have to specify 0 parameters --> do we just assume 0 and fill it OR can we update tracers to do without them entirely?
# NOTE: assimilation efficiency has to create an array of values rather than compute anything --> is there a better way to do that?
# NOTE: don't need to declare units as per second since this is the default time unit i.e., 1 == 1/sec

defined_parameters = Dict(
"P" => Dict(
"n" => 2,
"volumes" =>
Dict("min_volume" => 1, "max_volume" => 10, "splitting" => "log_splitting"),
"allometry" => Dict(
"maximum_growth_rate" => Dict("a" => 1, "b" => 1),
"nitrogen_half_saturation" => Dict("a" => 1, "b" => 1),
"maximum_predation_rate" => Dict("a" => 0, "b" => 0),
),
"palatability" => Dict("optimum_predator_prey_ratio" => 0, "protection" => 0),
"assimilation_efficiency" =>
Dict("can_be_eaten" => 1, "can_eat" => 0, "assimilation_efficiency" => 0),
# anything else goes here
"linear_mortality" => 8e-7 / second,
"holling_half_saturation" => 0,
"quadratic_mortality" => 0,
"alpha" => 0.1953 / day,
),
"Z" => Dict(
"n" => 2,
"volumes" => Dict(
"min_volume" => 10,
"max_volume" => 100,
"splitting" => "linear_splitting",
),
"allometry" => Dict(
"maximum_growth_rate" => Dict("a" => 0, "b" => 0),
"nitrogen_half_saturation" => Dict("a" => 0, "b" => 0),
"maximum_predation_rate" => Dict("a" => 1, "b" => 1),
),
"palatability" => Dict("optimum_predator_prey_ratio" => 10, "protection" => 1),
"assimilation_efficiency" => Dict(
"can_be_eaten" => 0, "can_eat" => 1, "assimilation_efficiency" => 0.32
),
# anything else goes here
"linear_mortality" => 8e-7 / second,
"holling_half_saturation" => 5.0,
"quadratic_mortality" => 1e-6 / second,
# this should be 0
"alpha" => 1e-99 / day,
),
)

emergent_parameters = compute_darwin_parameters(defined_parameters)
println(emergent_parameters)

# sanity check --> compare generated values to parameters in N2P2ZD example
# NOTE: eventually the emergent functions will be updated to true ones
# that's why we don't include this in tests

include(joinpath("..", "N2P2ZD", "tracers.jl"))

plankton_order = ["P1", "P2", "Z1", "Z2"]

for (key, params) in emergent_parameters
# start with arrays of values
if !(key ∈ ["assimilation_efficiency_matrix", "palatability_matrix", "volumes"])
comparison = all(parameters[Symbol(key)] .== [params[p] for p in plankton_order])
println(key, " values are the same: ", comparison)
# matrices of values -> compare row at a time
elseif !(key == "volumes")
for (i, p) in enumerate(plankton_order)
emergent_row = params[p, :]
true_row = parameters[Symbol(replace(key, "_matrix" => ""))][i, :]
comparison = all(true_row .== [emergent_row[p] for p in plankton_order])
println(key, " ", p, " values are the same: ", comparison)
end
end
end

# # for simplicity define the biogeochemistry dict seperately
# biogeochemistry_parameters = Dict(
# "detritus_remineralization" => 0.1213 / day,
# "feeding_export_poc_doc_fraction" => 0.5,
# "mortality_export_fraction" => 0.5,
# )
# created_parameters = merge(biogeochemistry_parameters, emergent_parameters)
#note that this dictionary would need to be converted to a named tuple to work with create_bgc_struc()...
Loading
Loading