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

Automatically change parameter and return float types #222

Merged
merged 11 commits into from
Dec 2, 2024
2 changes: 1 addition & 1 deletion Project.toml
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.13.2"
version = "0.13.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
4 changes: 2 additions & 2 deletions docs/src/model_components/individuals/slatissima.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using OceanBioME
kelp_bgc = SugarKelp()

# output
SugarKelp{FT} biogeochemistry (Broch & Slagstad, 2012) tracking the `N`itrogen and `C`arbon in a frond of `A`rea
SugarKelp{Float64} biogeochemistry (Broch & Slagstad, 2012) tracking the `N`itrogen and `C`arbon in a frond of `A`rea
```
which can be put into `BiogeochemicalParticles`, or you can directly manifest particles:
```jldoctest
Expand All @@ -24,7 +24,7 @@ grid = RectilinearGrid(size = (1, 1, 1), extent = (1, 1, 1));
particles = SugarKelpParticles(10; grid)

# output
10 BiogeochemicalParticles with SugarKelp{FT} biogeochemistry:
10 BiogeochemicalParticles with SugarKelp{Float64} biogeochemistry:
├── fields: (:A, :N, :C)
└── coupled tracers: (:NO₃, :NH₄, :DIC, :O₂, :DOC, :DON, :bPOC, :bPON)

Expand Down
4 changes: 2 additions & 2 deletions src/Light/2band.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct TwoBandPhotosyntheticallyActiveRadiation{FT, F, SPAR}
end

"""
TwoBandPhotosyntheticallyActiveRadiation(; grid,
TwoBandPhotosyntheticallyActiveRadiation(; grid::AbstractGrid{FT},
water_red_attenuation::FT = 0.225, # 1/m
water_blue_attenuation::FT = 0.0232, # 1/m
chlorophyll_red_attenuation::FT = 0.037, # 1/(m * (mgChl/m³) ^ eʳ)
Expand All @@ -94,7 +94,7 @@ Keyword Arguments
which should be `f(x, y, t)` where `x` and `y` are the native coordinates (i.e. meters for rectilinear grids
and latitude/longitude as appropriate)
"""
function TwoBandPhotosyntheticallyActiveRadiation(; grid,
function TwoBandPhotosyntheticallyActiveRadiation(; grid::AbstractGrid{FT},
water_red_attenuation::FT = 0.225, # 1/m
water_blue_attenuation::FT = 0.0232, # 1/m
chlorophyll_red_attenuation::FT = 0.037, # 1/(m * (mgChl/m³) ^ eʳ)
Expand Down
2 changes: 1 addition & 1 deletion src/Light/Light.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using KernelAbstractions, Oceananigans.Units
using Oceananigans.Architectures: device, architecture, on_architecture
using Oceananigans.Utils: launch!
using Oceananigans: Center, Face, fields
using Oceananigans.Grids: node, znodes, znode
using Oceananigans.Grids: node, znodes, znode, AbstractGrid
using Oceananigans.Fields: CenterField, TracerFields, location
using Oceananigans.BoundaryConditions: fill_halo_regions!,
ValueBoundaryCondition,
Expand Down
4 changes: 2 additions & 2 deletions src/Light/compute_euphotic_depth.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Oceananigans.Fields: ConstantField, ZeroField

@kernel function _compute_euphotic_depth!(euphotic_depth, PAR, grid, cutoff)
@kernel function _compute_euphotic_depth!(euphotic_depth, PAR, grid::AbstractGrid{FT}, cutoff) where FT
i, j = @index(Global, NTuple)

surface_PAR = @inbounds (PAR[i, j, grid.Nz] + PAR[i, j, grid.Nz + 1])/2

@inbounds euphotic_depth[i, j, 1] = -Inf
@inbounds euphotic_depth[i, j, 1] = convert(FT, -Inf)

for k in grid.Nz-1:-1:1
PARₖ = @inbounds PAR[i, j, k]
Expand Down
12 changes: 6 additions & 6 deletions src/Light/multi_band.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct MultiBandPhotosyntheticallyActiveRadiation{T, F, FN, K, E, C, SPAR, SPARD
end

"""
MultiBandPhotosyntheticallyActiveRadiation(; grid,
MultiBandPhotosyntheticallyActiveRadiation(; grid::AbstractGrid{FT},
bands = ((400, 500), (500, 600), (600, 700)), #nm
base_bands = MOREL_λ,
base_water_attenuation_coefficient = MOREL_kʷ,
Expand Down Expand Up @@ -65,15 +65,15 @@ Keyword Arguments
and latitude/longitude as appropriate)

"""
function MultiBandPhotosyntheticallyActiveRadiation(; grid,
function MultiBandPhotosyntheticallyActiveRadiation(; grid::AbstractGrid{FT},
bands = ((400, 500), (500, 600), (600, 700)), #nm
base_bands = MOREL_λ,
base_water_attenuation_coefficient = MOREL_kʷ,
base_chlorophyll_exponent = MOREL_e,
base_chlorophyll_attenuation_coefficient = MOREL_χ,
field_names = ntuple(n->par_symbol(n), Val(length(bands))),
surface_PAR = default_surface_PAR,
surface_PAR_division = fill(1 / length(bands), length(bands)))
surface_PAR_division = fill(1 / length(bands), length(bands))) where FT
Nbands = length(bands)

kʷ = zeros(eltype(grid), Nbands)
Expand Down Expand Up @@ -108,9 +108,9 @@ function MultiBandPhotosyntheticallyActiveRadiation(; grid,
return MultiBandPhotosyntheticallyActiveRadiation(total_PAR,
fields,
tuple(field_names...),
,
e,
χ,
convert.(FT, kʷ),
convert.(FT, e),
convert.(FT, χ),
surface_PAR,
surface_PAR_division)
end
Expand Down
9 changes: 5 additions & 4 deletions src/Models/AdvectedPopulations/LOBSTER/LOBSTER.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export LOBSTER

using Oceananigans.Units
using Oceananigans.Fields: Field, TracerFields, CenterField, ZeroField
using Oceananigans.Grids: AbstractGrid

using OceanBioME.Light: TwoBandPhotosyntheticallyActiveRadiation, default_surface_PAR
using OceanBioME: setup_velocity_fields, show_sinking_velocities, Biogeochemistry, ScaleNegativeTracers
Expand Down Expand Up @@ -169,7 +170,7 @@ struct LOBSTER{FT, B, W} <: AbstractContinuousFormBiogeochemistry
end

"""
LOBSTER(; grid,
LOBSTER(; grid::AbstractGrid{FT},
phytoplankton_preference::FT = 0.5,
maximum_grazing_rate::FT = 9.26e-6, # 1/s
grazing_half_saturation::FT = 1.0, # mmol N/m³
Expand Down Expand Up @@ -254,7 +255,7 @@ LOBSTER{Float64} with carbonates ❌, oxygen ❌, variable Redfield ratio ❌ an
Modifiers: Nothing
```
"""
function LOBSTER(; grid,
function LOBSTER(; grid::AbstractGrid{FT},
phytoplankton_preference::FT = 0.5,
maximum_grazing_rate::FT = 9.26e-6, # 1/s
grazing_half_saturation::FT = 1.0, # mmol N/m³
Expand Down Expand Up @@ -457,9 +458,9 @@ const VariableRedfieldLobster = Union{LOBSTER{<:Any, <:Val{(false, false, true)}

@inline redfield(::Val{:P}, bgc::LOBSTER) = (1 + bgc.organic_carbon_calcate_ratio) * bgc.phytoplankton_redfield
@inline redfield(::Val{:Z}, bgc::LOBSTER) = bgc.phytoplankton_redfield
@inline redfield(::Union{Val{:NO₃}, Val{:NH₄}, Val{:Alk}, Val{:O₂}}, bgc::LOBSTER) = 0
@inline redfield(::Union{Val{:NO₃}, Val{:NH₄}, Val{:Alk}, Val{:O₂}}, bgc::LOBSTER{FT}) where FT = convert(FT, 0)
@inline redfield(::Union{Val{:sPOM}, Val{:bPOM}, Val{:DOM}}, bgc::LOBSTER) = bgc.organic_redfield
@inline redfield(::Union{Val{:sPOC}, Val{:bPOC}, Val{:DOC}, Val{:DIC}}, bgc::LOBSTER) = 1
@inline redfield(::Union{Val{:sPOC}, Val{:bPOC}, Val{:DOC}, Val{:DIC}}, bgc::LOBSTER{FT}) where FT = convert(FT, 1)

@inline redfield(i, j, k, ::Val{:sPON}, bgc::VariableRedfieldLobster, tracers) = @inbounds tracers.sPOC[i, j, k] / tracers.sPON[i, j, k]
@inline redfield(i, j, k, ::Val{:bPON}, bgc::VariableRedfieldLobster, tracers) = @inbounds tracers.bPOC[i, j, k] / tracers.bPON[i, j, k]
Expand Down
11 changes: 6 additions & 5 deletions src/Models/AdvectedPopulations/NPZD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using Oceananigans.Biogeochemistry: AbstractContinuousFormBiogeochemistry

using Oceananigans.Units
using Oceananigans.Fields: ZeroField
using Oceananigans.Grids: AbstractGrid

using OceanBioME.Light: TwoBandPhotosyntheticallyActiveRadiation, default_surface_PAR
using OceanBioME: setup_velocity_fields, show_sinking_velocities
Expand Down Expand Up @@ -93,7 +94,7 @@ struct NutrientPhytoplanktonZooplanktonDetritus{FT, W} <: AbstractContinuousForm
end

"""
NutrientPhytoplanktonZooplanktonDetritus(; grid,
NutrientPhytoplanktonZooplanktonDetritus(; grid::AbstractGrid{FT},
initial_photosynthetic_slope::FT = 0.1953 / day, # 1/(W/m²)/s
base_maximum_growth::FT = 0.6989 / day, # 1/s
nutrient_half_saturation::FT = 2.3868, # mmol N/m³
Expand Down Expand Up @@ -154,7 +155,7 @@ NutrientPhytoplanktonZooplanktonDetritus{Float64} model, with (:P, :D) sinking
Modifiers: Nothing
```
"""
function NutrientPhytoplanktonZooplanktonDetritus(; grid,
function NutrientPhytoplanktonZooplanktonDetritus(; grid::AbstractGrid{FT},
initial_photosynthetic_slope::FT = 0.1953 / day, # 1/(W/m²)/s
base_maximum_growth::FT = 0.6989 / day, # 1/s
nutrient_half_saturation::FT = 2.3868, # mmol N/m³
Expand Down Expand Up @@ -316,8 +317,8 @@ adapt_structure(to, npzd::NPZD) =
adapt(to, npzd.sinking_velocities))

@inline redfield(i, j, k, val_tracer_name, bgc::NPZD, tracers) = redfield(val_tracer_name, bgc)
@inline redfield(::Union{Val{:N}}, bgc::NPZD) = 0
@inline redfield(::Union{Val{:P}, Val{:Z}, Val{:D}}, bgc::NPZD) = 6.56
@inline redfield(::Union{Val{:N}}, bgc::NPZD{FT}) where FT = convert(FT, 0)
@inline redfield(::Union{Val{:P}, Val{:Z}, Val{:D}}, bgc::NPZD{FT}) where FT = convert(FT, 6.56)

@inline nitrogen_flux(i, j, k, grid, advection, bgc::NPZD, tracers) = sinking_flux(i, j, k, grid, advection, Val(:D), bgc, tracers) +
sinking_flux(i, j, k, grid, advection, Val(:P), bgc, tracers)
Expand All @@ -329,6 +330,6 @@ adapt_structure(to, npzd::NPZD) =
@inline conserved_tracers(::NPZD) = (:N, :P, :Z, :D)
@inline sinking_tracers(bgc::NPZD) = keys(bgc.sinking_velocities)

@inline chlorophyll(bgc::NPZD, model) = 1.31 * model.tracers.P
@inline chlorophyll(bgc::NPZD{FT}, model) where FT = convert(FT, 1.31) * model.tracers.P

end # module
43 changes: 23 additions & 20 deletions src/Models/AdvectedPopulations/PISCES/PISCES.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ using OceanBioME.Models.CarbonChemistryModel: CarbonChemistry

using Oceananigans.Biogeochemistry: AbstractBiogeochemistry
using Oceananigans.Fields: set!
using Oceananigans.Grids: φnodes, RectilinearGrid
using Oceananigans.Grids: φnodes, RectilinearGrid, AbstractGrid

import OceanBioME: redfield, conserved_tracers, maximum_sinking_velocity, chlorophyll

Expand Down Expand Up @@ -174,7 +174,7 @@ include("adapts.jl")
include("show_methods.jl")

"""
PISCES(; grid,
PISCES(; grid::AbstractGrid{FT},
phytoplankton = MixedMondoNanoAndDiatoms(),
zooplankton = MicroAndMesoZooplankton(),
dissolved_organic_matter = DissolvedOrganicCarbon(),
Expand Down Expand Up @@ -285,41 +285,42 @@ the classes to a single `phytoplankton` if more classes are required (see
was desired a way to specify arbitary tracers for arguments would be required.
"""
function PISCES(; grid,
phytoplankton = MixedMondoNanoAndDiatoms(),
zooplankton = MicroAndMesoZooplankton(),
dissolved_organic_matter = DissolvedOrganicCarbon(),
particulate_organic_matter = TwoCompartementCarbonIronParticles(),
FT = eltype(grid),
phytoplankton = MixedMondoNanoAndDiatoms(FT),
zooplankton = MicroAndMesoZooplankton(FT),
dissolved_organic_matter = DissolvedOrganicCarbon(FT),
particulate_organic_matter = TwoCompartementCarbonIronParticles(FT),

nitrogen = NitrateAmmonia(),
iron = SimpleIron(),
nitrogen = NitrateAmmonia{FT}(),
iron = SimpleIron{FT}(),
silicate = Silicate(),
oxygen = Oxygen(),
oxygen = Oxygen{FT}(),
phosphate = Phosphate(),

inorganic_carbon = InorganicCarbon(),

# from Aumount 2005 rather than 2015 since it doesn't work the other way around
first_anoxia_thresehold = 6.0,
second_anoxia_thresehold = 1.0,
first_anoxia_thresehold = convert(FT, 6.0),
second_anoxia_thresehold = convert(FT, 1.0),

nitrogen_redfield_ratio = 16/122,
phosphate_redfield_ratio = 1/122,
nitrogen_redfield_ratio = convert(FT, 16/122),
phosphate_redfield_ratio = convert(FT, 1/122),

mixed_layer_shear = 1.0,
background_shear = 0.01,
mixed_layer_shear = convert(FT, 1.0),
background_shear = convert(FT, 0.01),

latitude = PrescribedLatitude(45),
latitude = PrescribedLatitude{FT}(45),
day_length = CBMDayLength(),

mixed_layer_depth = Field{Center, Center, Nothing}(grid),
euphotic_depth = Field{Center, Center, Nothing}(grid),

silicate_climatology = ConstantField(7.5),
silicate_climatology = ConstantField(convert(FT, 7.5)),

mean_mixed_layer_vertical_diffusivity = Field{Center, Center, Nothing}(grid),
mean_mixed_layer_light = Field{Center, Center, Nothing}(grid),

carbon_chemistry = CarbonChemistry(),
carbon_chemistry = CarbonChemistry(FT),
calcite_saturation = CenterField(grid),

surface_photosynthetically_active_radiation = default_surface_PAR,
Expand All @@ -328,9 +329,9 @@ function PISCES(; grid,
MultiBandPhotosyntheticallyActiveRadiation(; grid,
surface_PAR = surface_photosynthetically_active_radiation),

sinking_speeds = (POC = 2/day,
sinking_speeds = (POC = convert(FT, 2/day),
# might be more efficient to just precompute this
GOC = Field(KernelFunctionOperation{Center, Center, Face}(DepthDependantSinkingSpeed(),
GOC = Field(KernelFunctionOperation{Center, Center, Face}(DepthDependantSinkingSpeed{FT}(),
grid,
mixed_layer_depth,
euphotic_depth))),
Expand All @@ -345,6 +346,8 @@ function PISCES(; grid,

@warn "This implementation of PISCES is in early development and has not yet been validated against the operational version"

eltype(grid) == FT || @warn "The float type for parameters ($FT) does not match the grids float type ($(eltype(grid))). This will cause type instability."

if !isnothing(sediment) && !open_bottom
@warn "You have specified a sediment model but not `open_bottom` which will not work as the tracer will settle in the bottom cell"
end
Expand Down
Loading
Loading