Skip to content

Commit

Permalink
Merge pull request #170 from OceanBioME/jsw/optional-naning-for-negat…
Browse files Browse the repository at this point in the history
…ivity-protection

Added `invalid_fill_value` to `ScaleNegativeTracers`
  • Loading branch information
jagoosw authored Mar 28, 2024
2 parents cec152b + de524f7 commit 12cfa7f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
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.10.1"
version = "0.10.2"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
8 changes: 6 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ julia> Pkg.add("OceanBioME")

## Running your first model

As a simple example lets run a Nutrient-Phytoplankton-Zooplankton-Detritus (NPZD) model in a two-dimensional simulation of a buoyancy front. This example requires Oceananigans, so we install that first:
As a simple example lets run a Nutrient-Phytoplankton-Zooplankton-Detritus (NPZD) model in a two-dimensional simulation of a buoyancy front. This example requires Oceananigans, so we install that first via:

```@example quickstart
```julia
using Pkg; Pkg.add("Oceananigans")
```

and then:

```@example quickstart
using OceanBioME, Oceananigans
using Oceananigans.Units
Expand Down
3 changes: 2 additions & 1 deletion src/Models/AdvectedPopulations/LOBSTER/LOBSTER.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ function LOBSTER(; grid,
open_bottom::Bool = true,

scale_negatives = false,
invalid_fill_value = NaN,

particles::P = nothing,
modifiers::M = nothing) where {FT, LA, S, P, M}
Expand Down Expand Up @@ -350,7 +351,7 @@ function LOBSTER(; grid,
sinking_velocities)

if scale_negatives
scaler = ScaleNegativeTracers(underlying_biogeochemistry, grid)
scaler = ScaleNegativeTracers(underlying_biogeochemistry, grid; invalid_fill_value)
if isnothing(modifiers)
modifiers = scaler
elseif modifiers isa Tuple
Expand Down
3 changes: 2 additions & 1 deletion src/Models/AdvectedPopulations/NPZD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function NutrientPhytoplanktonZooplanktonDetritus(; grid,
open_bottom::Bool = true,

scale_negatives = false,
invalid_fill_value = NaN,

particles::P = nothing,
modifiers::M = nothing) where {FT, LA, S, P, M}
Expand All @@ -200,7 +201,7 @@ function NutrientPhytoplanktonZooplanktonDetritus(; grid,
sinking_velocities)

if scale_negatives
scaler = ScaleNegativeTracers(underlying_biogeochemistry, grid)
scaler = ScaleNegativeTracers(underlying_biogeochemistry, grid; invalid_fill_value)
modifiers = isnothing(modifiers) ? scaler : (modifiers..., scaler)
end

Expand Down
1 change: 1 addition & 0 deletions src/OceanBioME.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ update_tendencies!(bgc, modifiers::Tuple, model) = [update_tendencies!(bgc, modi

function update_biogeochemical_state!(bgc::Biogeochemistry, model)
update_biogeochemical_state!(model, bgc.modifiers)
synchronize(device(architecture(model)))
update_biogeochemical_state!(model, bgc.light_attenuation)
end

Expand Down
47 changes: 31 additions & 16 deletions src/Utils/negative_tracers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ end
##### Infastructure to rescale negative values
#####

struct ScaleNegativeTracers{FA, SA, W}
tracers :: FA
scalefactors :: SA
warn :: W

ScaleNegativeTracers(tracers::FA, scalefactors::SA, warn::W) where {FA, SA, W} =
warn ? error("Warning not currently implemented") : new{FA, SA, W}(tracers, scalefactors, warn)
struct ScaleNegativeTracers{FA, SA, FV, W}
tracers :: FA
scalefactors :: SA
invalid_fill_value :: FV
warn :: W

ScaleNegativeTracers(tracers::FA, scalefactors::SA, invalid_fill_value::FV, warn::W) where {FA, SA, W, FV} =
warn ? error("Warning not currently implemented") : new{FA, SA, FV, W}(tracers, scalefactors, invalid_fill_value, warn)
end

adapt_structure(to, snt::ScaleNegativeTracers) = ScaleNegativeTracers(adapt(to, snt.tracers),
adapt(to, snt.scalefactors),
adapt(to, snt.invalid_fill_value),
adapt(to, snt.warn))

"""
ScaleNegativeTracers(; tracers, scalefactors = ones(length(tracers)), warn = false)
ScaleNegativeTracers(; tracers, scalefactors = ones(length(tracers)), warn = false, invalid_fill_value = NaN)
Constructs a modifier to scale `tracers` so that none are negative. Use like:
```julia
Expand All @@ -60,14 +62,27 @@ github](https://github.com/OceanBioME/OceanBioME.jl/discussions/48).
Future plans include implement a positivity-preserving timestepping scheme as the ideal alternative.
If `warn` is true then scaling will raise a warning.
~~If `warn` is true then scaling will raise a warning.~~
`invalid_fill_value` specifies the value to set the total cell content to if the total is less than 0
(meaning that total tracer conservation can not be enforced). If the value is set to anything other than
`NaN` this scheme no longer conserves mass. While this may be useful to prevent spurious numerics leading
to crashing care should be taken that the mass doesn't deviate too much.
This scheme is similar to that used by [NEMO-PISCES](https://www.nemo-ocean.eu/), although they scale the
tendency rather than the value, while other Earth system models simply set negative tracers to zero, for
example [NCAR's MARBL](https://marbl-ecosys.github.io/versions/latest_release/index.html) and
[NEMO-TOPAZ2](https://zenodo.org/records/2648099), which does not conserve mass. More complicated schemes
exist, for example [ROMS-BECS](https://zenodo.org/records/3988618) uses an implicite-itterative
approach where each component is updated in sequence to garantee mass conservation, possibly at the
expense of numerical precision.
"""
function ScaleNegativeTracers(tracers; scalefactors = ones(length(tracers)), warn = false)
function ScaleNegativeTracers(tracers; scalefactors = ones(length(tracers)), invalid_fill_value = NaN, warn = false)
if length(scalefactors) != length(tracers)
error("Incorrect number of scale factors provided")
end

return ScaleNegativeTracers(tracers, scalefactors, warn)
return ScaleNegativeTracers(tracers, scalefactors, invalid_fill_value, warn)
end

"""
Expand All @@ -77,11 +92,11 @@ Construct a modifier to scale the conserved tracers in `model`.
If `warn` is true then scaling will raise a warning.
"""
function ScaleNegativeTracers(bgc::AbstractBiogeochemistry, grid; warn = false)
function ScaleNegativeTracers(bgc::AbstractBiogeochemistry, grid; invalid_fill_value = NaN, warn = false)
tracers = conserved_tracers(bgc)
scalefactors = on_architecture(architecture(grid), ones(length(tracers)))

return ScaleNegativeTracers(tracers, scalefactors, warn)
return ScaleNegativeTracers(tracers, scalefactors, invalid_fill_value, warn)
end

summary(scaler::ScaleNegativeTracers) = string("Mass conserving negative scaling of $(scaler.tracers)")
Expand All @@ -97,10 +112,10 @@ function update_biogeochemical_state!(model, scale::ScaleNegativeTracers)

tracers_to_scale = Tuple(model.tracers[tracer_name] for tracer_name in keys(scale.tracers))

scale_for_negs_kernel!(tracers_to_scale, scale.scalefactors)
scale_for_negs_kernel!(tracers_to_scale, scale.scalefactors, scale.invalid_fill_value)
end

@kernel function scale_for_negs!(tracers, scalefactors)
@kernel function scale_for_negs!(tracers, scalefactors, invalid_fill_value)
i, j, k = @index(Global, NTuple)

t, p = 0.0, 0.0
Expand All @@ -115,7 +130,7 @@ end
end
end

t < 0 && (t = NaN)
t < 0 && (t = invalid_fill_value)

for tracer in tracers
value = @inbounds tracer[i, j, k]
Expand Down

2 comments on commit 12cfa7f

@jagoosw
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

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/103795

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.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

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:

git tag -a v0.10.2 -m "<description of version>" 12cfa7fd9edb95a5058cab9c32983285d2ddb952
git push origin v0.10.2

Please sign in to comment.