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 69 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ uuid = "41889b7c-c35c-4f16-abd7-94b299d9fd29"
version = "0.0.1"

[deps]
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
NamedArrays = "86f7a689-2022-50b4-a561-43c23ac3c673"
OceanBioME = "a49af516-9db8-4be4-be45-1dad61c5a376"
Oceananigans = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
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
2 changes: 1 addition & 1 deletion examples/differential_equations.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This example shows how to integrate a Biogeochemistry (BGC) box model with DifferentialEquations.
using Agate.Library.Light

using DifferentialEquations
using OrdinaryDiffEq
using Plots

using Oceananigans.Units
Expand Down
91 changes: 91 additions & 0 deletions examples/emergent_4P/emergent_functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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_palat(
prey_data,
predator_data;
volume_key::String="volume",
optimum_ratio_key::String="optimum_predator_prey_ratio",
protection_key::String="protection",
)
prey_volume = prey_data[volume_key]
predator_volume = predator_data[volume_key]
optimum_predator_prey_ratio = predator_data[optimum_ratio_key]
protection = prey_data[protection_key]

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_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

"""
Default fall-back function if no emergent function is defined
"""
function default_emergent(my_parameter::Real)
return my_parameter
end

function dummy_emergent_assimilation_efficiency(
prey_data, predator_data; assimilation_efficiency_key::String="assimilation_efficiency"
)
assimilation_efficiency = 0
# predators don't eat other predators
if prey_data[assimilation_efficiency_key] == 0
assimilation_efficiency = predator_data[assimilation_efficiency_key]
end
return assimilation_efficiency
end
78 changes: 78 additions & 0 deletions examples/emergent_4P/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Oceananigans.Units
using Agate

defined_parameters = Dict(
"P" => Dict(
"n" => 2,
"min_volume" => 1,
"max_volume" => 10,
"splitting" => "log_splitting",
"growth_a" => 1,
"growth_b" => 1,
"protection" => 0,
"optimum_predator_prey_ratio" => 0,
"nitrogen_half_saturation_a" => 1,
"nitrogen_half_saturation_b" => 1,
"predation_rate_a" => 0,
"predation_rate_b" => 0,
"linear_mortality" => 8e-7 / second,
"holling_half_saturation" => 0,
"quadratic_mortality" => 0,
"alpha" => 0.1953 / day,
"assimilation_efficiency" => 0,
),
"Z" => Dict(
"n" => 2,
"min_volume" => 10,
"max_volume" => 100,
"splitting" => "linear_splitting",
"growth_a" => 0,
"growth_b" => 0,
"protection" => 1,
"optimum_predator_prey_ratio" => 10,
"nitrogen_half_saturation_a" => 0,
"nitrogen_half_saturation_b" => 0,
"predation_rate_a" => 1,
"predation_rate_b" => 1,
"linear_mortality" => 8e-7 / second,
"holling_half_saturation" => 5.0,
"quadratic_mortality" => 1e-6 / second,
"alpha" => 1e-99 / day,
"assimilation_efficiency" => 0.32,
),
)

emergent_parameters = compute_darwin_parameters(defined_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", "palatability", "volume"])
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 == "volume")
for (i, p) in enumerate(plankton_order)
emergent_row = params[p, :]
true_row = parameters[Symbol(key)][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()...
2 changes: 1 addition & 1 deletion examples/inference_hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Agate
using Agate.Library.Light

using DifferentialEquations
using OrdinaryDiffEq
using LinearAlgebra
using Plots
using StatsPlots
Expand Down
1 change: 1 addition & 0 deletions src/Agate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include("Models/Models.jl")

using .Models

export compute_darwin_parameters
export define_tracer_functions

end # module
10 changes: 5 additions & 5 deletions src/Models/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export define_tracer_functions
parameters, tracers; auxiliary_fields=[:PAR], helper_functions=nothing
) -> DataType

Creates an Oceananigans.Biogeochemistry model type.
Create an Oceananigans.Biogeochemistry model type.

# Arguments
- `parameters`: named sequence of values of the form ((<field name> = <default value>, ...)
Expand Down Expand Up @@ -61,8 +61,8 @@ end
Create a subtype of Oceananigans.Biogeochemistry with field names defined in `parameters`.

# Arguments
- `struct_name`: name for the new struct passed as a Symbol. The struct will be accessible
as: `Agate.Models.Dynamic.<struct_name>`
- `struct_name`: name for the struct to create passed as a Symbol (the new struct will be
accessible as `Agate.Models.Dynamic.<struct_name>`)
- `parameters`: named sequence of values of the form (<field name> = <default value>, ...)

Note that the field names defined in `parameters` can't be any of [:x, :y, :z, :t] as these
Expand Down Expand Up @@ -183,7 +183,7 @@ end
"""
parse_expression(f_expr) -> Vector

Returns all symbols (argument names and method names) called in expression.
Return all symbols (argument names and method names) called in expression.

# Example
```julia
Expand Down Expand Up @@ -211,7 +211,7 @@ end
"""
expression_check(args, f_expr) -> nothing

Checks that all methods and arguments are defined. Specifically:
Check that all methods and arguments are defined. Specifically:
- vector `args` contains all arguments of expression `f_expr`
- all methods called in `f_expr` are defined in module (e.g., Base, Main, Agate)
If not, throws an UnderVarError.
Expand Down
4 changes: 4 additions & 0 deletions src/Models/Models.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module Models

include("Dynamic.jl")
include("Parameters.jl")

using .Dynamic
using .Parameters

export compute_darwin_parameters
export define_tracer_functions

end # module
Loading
Loading