Skip to content

Commit

Permalink
(0.90.3) Fix bug in adapting ScalarDiffusivity and `ScalarBiharmoni…
Browse files Browse the repository at this point in the history
…cDiffusivity` (#3413)

* Fix bug in adapting scalar diffusivity

* Use correct constructor for adapting ScalarDiffusivity and ScalarBiharmonicDiffusivity

* DiscreteBoundaryFunction -> DiscreteDiffusionFunction

* bump patch release

---------

Co-authored-by: Navid C. Constantinou <[email protected]>
  • Loading branch information
glwagner and navidcy authored Dec 29, 2023
1 parent 34f41e7 commit 61029a4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Oceananigans"
uuid = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
authors = ["Climate Modeling Alliance and contributors"]
version = "0.90.2"
version = "0.90.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
6 changes: 3 additions & 3 deletions src/TurbulenceClosures/discrete_diffusion_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ const UnlocalizedDDF = DiscreteDiffusionFunction{<:Nothing, <:No
const UnlocalizedUnparametrizedDDF = DiscreteDiffusionFunction{<:Nothing, <:Nothing, <:Nothing, <:Nothing}

@inline function getdiffusivity(dd::DiscreteDiffusionFunction{LX, LY, LZ},
i, j, k, grid, location, clock, fields) where {LX, LY, LZ}
i, j, k, grid, location, clock, fields) where {LX, LY, LZ}
from = (LX(), LY(), LZ())
return ℑxyz(i, j, k, grid, from, location, dd.func, clock, fields, dd.parameters)
end

@inline function getdiffusivity(dd::UnparameterizedDDF{LX, LY, LZ},
i, j, k, grid, location, clock, fields) where {LX, LY, LZ}
i, j, k, grid, location, clock, fields) where {LX, LY, LZ}
from = (LX(), LY(), LZ())
return ℑxyz(i, j, k, grid, from, location, dd.func, clock, fields)
end
Expand All @@ -99,5 +99,5 @@ end
dd.func(i, j, k, grid, location..., clock, fields)

Adapt.adapt_structure(to, dd::DiscreteDiffusionFunction{LX, LY, LZ}) where {LX, LY, LZ} =
DiscreteBoundaryFunction{LX, LY, LZ}(Adapt.adapt(to, dd.func),
DiscreteDiffusionFunction{LX, LY, LZ}(Adapt.adapt(to, dd.func),
Adapt.adapt(to, dd.parameters))
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,9 @@ end

Base.show(io::IO, closure::ScalarBiharmonicDiffusivity) = print(io, summary(closure))

adapt_structure(to, closure::ScalarBiharmonicDiffusivity) = ScalarBiharmonicDiffusivity(adapt(closure.ν),
adapt(closure.κ))
function Adapt.adapt_structure(to, closure::ScalarBiharmonicDiffusivity{F, <:Any, <:Any, N}) where {F, N}
ν = Adapt.adapt(to, closure.ν)
κ = Adapt.adapt(to, closure.κ)
return ScalarBiharmonicDiffusivity{F, N}(ν, κ)
end

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Adapt: adapt_structure
import Adapt
import Oceananigans.Grids: required_halo_size
using Oceananigans.Utils: prettysummary

struct ScalarDiffusivity{TD, F, V, K, N} <: AbstractScalarDiffusivity{TD, F, N}
ν :: V
κ :: K

ScalarDiffusivity{TD, F, N}::V, κ::K) where {TD, F, V, K, N} = new{TD, F, V, K, N}(ν, κ)
end

Expand All @@ -25,37 +24,42 @@ Otherwise `κ` must be a `NamedTuple` with values for every tracer individually.
Arguments
=========
* `time_discretization`: either `ExplicitTimeDiscretization()` (default) or `VerticallyImplicitTimeDiscretization()`.
* `formulation`:
- `HorizontalFormulation()` for diffusivity applied in the horizontal direction(s)
- `VerticalFormulation()` for diffusivity applied in the vertical direction,
- `ThreeDimensionalFormulation()` (default) for diffusivity applied isotropically to all directions
* `FT`: the float datatype (default: `Float64`)
* `time_discretization`: either `ExplicitTimeDiscretization()` (default)
or `VerticallyImplicitTimeDiscretization()`.
* `formulation`:
- `HorizontalFormulation()` for diffusivity applied in the horizontal direction(s)
- `VerticalFormulation()` for diffusivity applied in the vertical direction,
- `ThreeDimensionalFormulation()` (default) for diffusivity applied isotropically to all directions
* `FT`: the float datatype (default: `Float64`)
Keyword arguments
=================
* `ν`: Viscosity. `Number`, three-dimensional `AbstractArray`, `Field`, or `Function`.
* `κ`: Diffusivity. `Number`, `AbstractArray`, `Field`, `Function`, or
`NamedTuple` of diffusivities with entries for each tracer.
* `discrete_form`: `Boolean`; default: `False`.
When prescribing the viscosities or diffusivities as functions, depending on the value of keyword argument
`discrete_form`, the constructor expects:
- `discrete_form = false` (default): functions of the grid's native coordinates and time, e.g., `(x, y, z, t)` for
a `RectilinearGrid` or `(λ, φ, z, t)` for a `LatitudeLongitudeGrid`.
* `ν`: Viscosity. `Number`, three-dimensional `AbstractArray`, `Field`, or `Function`.
* `κ`: Diffusivity. `Number`, `AbstractArray`, `Field`, `Function`, or
`NamedTuple` of diffusivities with entries for each tracer.
* `discrete_form`: `Boolean`; default: `False`.
- `discrete_form = true`:
- with `loc = (nothing, nothing, nothing)` (default): functions of `(i, j, k, grid, ℓx, ℓy, ℓz)` with `ℓx`, `ℓy` and `ℓz` either `Face()` or `Center()`.
- with `loc = (ℓx, ℓy, ℓz)` with `ℓx`, `ℓy` and `ℓz` either `Face()` or `Center()`: functions of `(i, j, k, grid)`.
When prescribing the viscosities or diffusivities as functions, depending on the
value of keyword argument `discrete_form`, the constructor expects:
- `parameters`: `NamedTuple` with parameters used by the functions that compute viscosity and/or diffusivity; default: `nothing`.
* `discrete_form = false` (default): functions of the grid's native coordinates
and time, e.g., `(x, y, z, t)` for a `RectilinearGrid` or
`(λ, φ, z, t)` for a `LatitudeLongitudeGrid`.
* `discrete_form = true`:
- with `loc = (nothing, nothing, nothing)` (default):
functions of `(i, j, k, grid, ℓx, ℓy, ℓz)` with `ℓx`, `ℓy`
and `ℓz` either `Face()` or `Center()`.
- with `loc = (ℓx, ℓy, ℓz)` with `ℓx`, `ℓy`
and `ℓz` either `Face()` or `Center()`: functions of `(i, j, k, grid)`.
* `parameters`: `NamedTuple` with parameters used by the functions
that compute viscosity and/or diffusivity; default: `nothing`.
Examples
========
Expand Down Expand Up @@ -110,7 +114,8 @@ function ScalarDiffusivity(time_discretization=ExplicitTimeDiscretization(),
required_halo_size = 1)

if formulation == HorizontalFormulation() && time_discretization == VerticallyImplicitTimeDiscretization()
throw(ArgumentError("VerticallyImplicitTimeDiscretization is only supported for `VerticalFormulation` or `ThreeDimensionalFormulation`"))
throw(ArgumentError("VerticallyImplicitTimeDiscretization is only supported for \
`VerticalFormulation` or `ThreeDimensionalFormulation`"))
end

κ = convert_diffusivity(FT, κ; discrete_form, loc, parameters)
Expand Down Expand Up @@ -198,5 +203,9 @@ end

Base.show(io::IO, closure::ScalarDiffusivity) = print(io, summary(closure))

adapt_structure(to, closure::ScalarDiffusivity) = ScalarDiffusivity(adapt(closure.ν),
adapt(closure.κ))
function Adapt.adapt_structure(to, closure::ScalarDiffusivity{TD, F, <:Any, <:Any, N}) where {TD, F, N}
ν = Adapt.adapt(to, closure.ν)
κ = Adapt.adapt(to, closure.κ)
return ScalarDiffusivity{TD, F, N}(ν, κ)
end

2 comments on commit 61029a4

@navidcy
Copy link
Collaborator

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

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.90.3 -m "<description of version>" 61029a4bcb589d4f70fd2b975359f11d3263439b
git push origin v0.90.3

Please sign in to comment.