-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from OceanBioME/jsw-ch/speedup-boxmodels
Make `BoxModel`s speedy again (+ introduce prescribed PAR)
- Loading branch information
Showing
10 changed files
with
227 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "OceanBioME" | ||
uuid = "a49af516-9db8-4be4-be45-1dad61c5a376" | ||
authors = ["Jago Strong-Wright <[email protected]> and contributors"] | ||
version = "0.10.3" | ||
version = "0.10.4" | ||
|
||
[deps] | ||
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# # Calibrating a biogeochemical model with `EnsembleKalmanProcesses` | ||
# | ||
# In this example we calibrate some of the parameters for the [NPZD](@ref NPZD) model | ||
# in a simple box model setup using a data assimilation package [EnsembleKalmanProcesses](https://github.com/CliMA/EnsembleKalmanProcesses.jl). | ||
# First we setup the model and generate synthetic data with "true" parameters. We then | ||
# define priors and setup an EKP to solve. | ||
# | ||
# While this is a very simple situation it illustrates the ease of integration with | ||
# data assimilation tools. Examples given in the EnsembleKalmanProcesses docs illustrate | ||
# how the package can be used to solve more complex forward models. | ||
|
||
# ## Install dependencies | ||
# First we ensure we have the required dependencies installed | ||
# ```julia | ||
# using Pkg | ||
# pkg "add OceanBioME, Oceananigans, CairoMakie, EnsembleKalmanProcesses, Distributions" | ||
# ``` | ||
|
||
using OceanBioME, Oceananigans.Units, Oceananigans, BenchmarkTools | ||
|
||
const year = years = 365day | ||
|
||
# Setup the forward model | ||
|
||
@inline PAR⁰(t) = 60 * (1 - cos((t + 15days) * 2π / year)) * (1 / (1 + 0.2 * exp(-((mod(t, year) - 200days) / 50days)^2))) + 2 | ||
|
||
z = -10 # nominal depth of the box for the PAR profile | ||
@inline PAR(t)::Float64 = PAR⁰(t) * exp(0.2z) # Modify the PAR based on the nominal depth and exponential decay | ||
|
||
function run_box_simulation() | ||
biogeochemistry = NutrientPhytoplanktonZooplanktonDetritus(; grid = BoxModelGrid, | ||
light_attenuation_model = nothing) | ||
|
||
model = BoxModel(; biogeochemistry, forcing = (; PAR)) | ||
|
||
set!(model, N = 10.0, P = 0.1, Z = 0.01) | ||
|
||
simulation = Simulation(model; Δt = 20minutes, stop_iteration = 1000, verbose = false) | ||
|
||
#simulation.output_writers[:fields] = JLD2OutputWriter(model, model.fields; filename = "box_benchmarking.jld2", schedule = IterationInterval(20), overwrite_existing = true) | ||
|
||
fast_output = SpeedyOutput("box_benchmarking.jld2") | ||
|
||
simulation.callbacks[:output] = Callback(fast_output, IterationInterval(20);) | ||
|
||
@info "Running the model..." | ||
run!(simulation) | ||
end | ||
|
||
function fast_output(sim, fname) | ||
file = jldopen(fname, "w+") | ||
|
||
model = sim.model | ||
|
||
t = time(sim) | ||
|
||
file["fields/$t"] = model.field_values | ||
|
||
close(file) | ||
|
||
return nothing | ||
end | ||
##### | ||
##### results | ||
##### | ||
|
||
# Config: 1000 iterations with output every 8 hours and 20min steps | ||
# origional @btime 317.5ms (1483607 allocations: 243.03 MiB) | ||
# removing kernel launching from store tendencies: 291.546 ms (1293607 allocations: 187.95 MiB) | ||
# removed kernel launching from rk3 substepping: 265.823 ms (1000607 allocations: 79.11 MiB) | ||
# removed broadcasting in update state: 120.605 ms (619379 allocations: 63.98 MiB) | ||
# no outputs: 23.523 ms (370344 allocations: 30.31 MiB) | ||
# outputting every 20 steps: 1.181s | ||
# outputting every 20 steps with `SpeedyOutput`: 34ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
struct SpeedyOutput{FN, B} # I will make this an `AbstractOutputWriter` at some point but this will do for now | ||
filename :: FN | ||
overwrite_existing :: B | ||
|
||
function SpeedyOutput(filename::FN; overwrite_existing::B = true) where {FN, B} | ||
return new{FN, B}(filename, overwrite_existing) | ||
end | ||
end | ||
|
||
function (save::SpeedyOutput)(simulation) | ||
model = simulation.model | ||
|
||
t = time(simulation) | ||
iter = model.clock.iteration | ||
|
||
file = jldopen(save.filename, ifelse(save.overwrite_existing, "w+", "w")) | ||
|
||
file["timeseries/t/$iter"] = t | ||
|
||
for (i, name) in enumerate(keys(model.fields)) | ||
file["timeseries/$name/$iter"] = model.field_values[i] | ||
end | ||
|
||
close(file) | ||
|
||
return nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
33b611a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
33b611a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/111689
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: