Exporting a model setup

This example shows how to save the inputs used to construct an Agate model. Saving the setup makes it easier to recreate the same model later, or to share the model configuration with someone else.

Loading dependencies

The example uses Agate.jl to construct the model and OceanBioME.jl to provide a simple box-model grid. Agate.Manifests provides the functions used to export and reload the model setup.

using Agate
using Agate.Manifests: construct_from_manifest, export_manifest
using Agate.Models: NiPiZD
using OceanBioME: BoxModelGrid

Construct a model and setup record

NiPiZD.construct_with_manifest accepts the same keywords as NiPiZD.construct. In addition to the biogeochemistry object, it returns a setup record describing the model recipe and constructor inputs. Agate stores this setup as a manifest, which is a JSON-compatible dictionary.

grid = BoxModelGrid()

bgc, manifest = NiPiZD.construct_with_manifest(;
    grid,
    phyto_size_structure=(n=3, min_esd=1.0, max_esd=10.0, splitting=:log_splitting),
    zoo_size_structure=(n=2, min_esd=20.0, max_esd=100.0, splitting=:linear_splitting),
)

Export the manifest

export_manifest writes the setup to a JSON file. The exported file records the biological model configuration, but not the physical grid, so the grid remains explicit when the setup is loaded again.

manifest_path = tempname() * ".json"
export_manifest(manifest_path, manifest)

println("Wrote model manifest to ", manifest_path)
Wrote model manifest to /tmp/jl_W7kNCpgRM5.json

Reload the manifest

construct_from_manifest reads the exported manifest and reconstructs the Agate biogeochemistry object. The grid is supplied separately because it describes the physical domain rather than the biological model setup.

reloaded_bgc = construct_from_manifest(manifest_path; grid)

println(typeof(bgc))
println(typeof(reloaded_bgc))
Agate.Construction.AgateBGC{@NamedTuple{detritus_remineralization::Float64, mortality_export_fraction::Float64, linear_mortality::Vector{Float64}, quadratic_mortality::Vector{Float64}, maximum_growth_rate::Vector{Float64}, nutrient_half_saturation::Vector{Float64}, alpha::Vector{Float64}, maximum_predation_rate::Vector{Float64}, holling_half_saturation::Vector{Float64}, palatability_matrix::Matrix{Float64}, assimilation_matrix::Matrix{Float64}, optimum_predator_prey_ratio::Vector{Float64}, specificity::Vector{Float64}, protection::Vector{Float64}, assimilation_efficiency::Vector{Float64}, interactions::Agate.Configuration.InteractionMatrices{Matrix{Float64}, Matrix{Float64}, Vector{Int64}, Vector{Int64}, Vector{Int64}, Vector{Int64}}}, @NamedTuple{N::Agate.Tendencies.var"#16#17"{:smith, :liebig, Symbol, Symbol, Tuple{Tuple{Symbol, Symbol}}, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, D::Agate.Tendencies.var"#25#26"{Symbol, Symbol, Symbol}, Z1::Agate.Tendencies.var"#13#14"{Int64}, Z2::Agate.Tendencies.var"#13#14"{Int64}, P1::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, P2::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, P3::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}}, Agate.Runtime.Tracers{Agate.Runtime.TracerIndex{(:N, :D, :Z1, :Z2, :P1, :P2, :P3), (:Z, :P), (:PAR,), 2}}, Nothing, NTuple{5, Float64}}
Agate.Construction.AgateBGC{@NamedTuple{detritus_remineralization::Float64, mortality_export_fraction::Float64, linear_mortality::Vector{Float64}, quadratic_mortality::Vector{Float64}, maximum_growth_rate::Vector{Float64}, nutrient_half_saturation::Vector{Float64}, alpha::Vector{Float64}, maximum_predation_rate::Vector{Float64}, holling_half_saturation::Vector{Float64}, palatability_matrix::Matrix{Float64}, assimilation_matrix::Matrix{Float64}, optimum_predator_prey_ratio::Vector{Float64}, specificity::Vector{Float64}, protection::Vector{Float64}, assimilation_efficiency::Vector{Float64}, interactions::Agate.Configuration.InteractionMatrices{Matrix{Float64}, Matrix{Float64}, Vector{Int64}, Vector{Int64}, Vector{Int64}, Vector{Int64}}}, @NamedTuple{N::Agate.Tendencies.var"#16#17"{:smith, :liebig, Symbol, Symbol, Tuple{Tuple{Symbol, Symbol}}, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, D::Agate.Tendencies.var"#25#26"{Symbol, Symbol, Symbol}, Z1::Agate.Tendencies.var"#13#14"{Int64}, Z2::Agate.Tendencies.var"#13#14"{Int64}, P1::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, P2::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}, P3::Agate.Tendencies.var"#8#10"{:smith, :liebig, Int64, Tuple{Agate.Tendencies.NutrientCoupling{:N, :nutrient_half_saturation, :one, ((:D, :detritus_remineralization),)}}}}, Agate.Runtime.Tracers{Agate.Runtime.TracerIndex{(:N, :D, :Z1, :Z2, :P1, :P2, :P3), (:Z, :P), (:PAR,), 2}}, Nothing, NTuple{5, Float64}}

This page was generated using Literate.jl.