Skip to content

Include background tracer and velocities in closure tendency terms #3646

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

Merged
merged 28 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
46d22a7
Use total tracer in diffusive flux calculation
glwagner Jul 1, 2024
719e4c2
Also use total_c in the immersed flux divergence
glwagner Jul 1, 2024
794088f
Move background fields around
glwagner Jul 2, 2024
719216a
Merge branch 'main' into glw/background-flux-divergence
glwagner Jul 2, 2024
433e250
Adapt backgroundFields and add option to include or not in closure fl…
glwagner Jul 2, 2024
b4e640c
Merge branch 'glw/background-flux-divergence' of https://github.com/C…
glwagner Jul 2, 2024
db9a08b
Merge branch 'main' into glw/background-flux-divergence
glwagner Aug 6, 2024
3f0f0cd
Update src/Models/NonhydrostaticModels/background_fields.jl
glwagner Aug 8, 2024
07ccb2e
Update src/Models/NonhydrostaticModels/background_fields.jl
glwagner Aug 8, 2024
fa3e3f0
Try to make things behave in expected ways
glwagner Aug 8, 2024
aded97b
Merge branch 'main' into glw/background-flux-divergence
glwagner Aug 8, 2024
7aef5af
Merge branch 'main' into glw/background-flux-divergence
glwagner Aug 9, 2024
8f72bd3
Merge branch 'main' into glw/background-flux-divergence
glwagner Aug 10, 2024
7528478
Merge branch 'main' into glw/background-flux-divergence
glwagner Aug 13, 2024
f89445d
Dont import background field from Fields
glwagner Aug 13, 2024
af40866
Merge remote-tracking branch 'origin/main' into glw/background-flux-d…
glwagner Oct 23, 2024
670e760
Merge branch 'main' into glw/background-flux-divergence
glwagner Oct 23, 2024
b1c2234
Correct accessing velocities from the background field (#3862)
liuchihl Oct 23, 2024
be9cd4f
Merge branch 'main' into glw/background-flux-divergence
glwagner Oct 23, 2024
a7e8e26
Merge branch 'main' into glw/background-flux-divergence
glwagner Nov 14, 2024
1d6a5d9
Merge branch 'main' into glw/background-flux-divergence
glwagner Jan 4, 2025
4267e97
add a unit test for background flux divergence and clean up unrelated…
liuchihl Jan 30, 2025
47e97b0
Merge branch 'main' into glw/background-flux-divergence
glwagner Mar 11, 2025
7883532
Support constatn field
glwagner Mar 13, 2025
12bb618
Merge branch 'main' into glw/background-flux-divergence
glwagner Apr 9, 2025
1e6ef4c
rm background fields from fields
glwagner Apr 19, 2025
83bb0a7
relax constrains on backgroundField
glwagner Apr 22, 2025
a0dc12f
Merge remote-tracking branch 'origin/main' into glw/background-flux-d…
glwagner May 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Fields/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ include("field_indices.jl")
include("scans.jl")
include("regridding_fields.jl")
include("field_tuples.jl")
include("background_fields.jl")
include("interpolate.jl")
include("show_fields.jl")
include("broadcasting_abstract_fields.jl")
Expand Down
82 changes: 0 additions & 82 deletions src/Fields/background_fields.jl

This file was deleted.

4 changes: 2 additions & 2 deletions src/Models/Models.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Models

export
NonhydrostaticModel,
NonhydrostaticModel, BackgroundField, BackgroundFields,
ShallowWaterModel, ConservativeFormulation, VectorInvariantFormulation,
HydrostaticFreeSurfaceModel, ZStar, ZCoordinate,
ExplicitFreeSurface, ImplicitFreeSurface, SplitExplicitFreeSurface,
Expand Down Expand Up @@ -101,7 +101,7 @@ include("HydrostaticFreeSurfaceModels/HydrostaticFreeSurfaceModels.jl")
include("ShallowWaterModels/ShallowWaterModels.jl")
include("LagrangianParticleTracking/LagrangianParticleTracking.jl")

using .NonhydrostaticModels: NonhydrostaticModel, PressureField
using .NonhydrostaticModels: NonhydrostaticModel, PressureField, BackgroundField, BackgroundFields

using .HydrostaticFreeSurfaceModels:
HydrostaticFreeSurfaceModel,
Expand Down
3 changes: 2 additions & 1 deletion src/Models/NonhydrostaticModels/NonhydrostaticModels.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module NonhydrostaticModels

export NonhydrostaticModel
export NonhydrostaticModel, BackgroundField, BackgroundFields

using DocStringExtensions

Expand Down Expand Up @@ -65,6 +65,7 @@ nonhydrostatic_pressure_solver(grid) = nonhydrostatic_pressure_solver(architectu
##### NonhydrostaticModel definition
#####

include("background_fields.jl")
include("nonhydrostatic_model.jl")
include("pressure_field.jl")
include("show_nonhydrostatic_model.jl")
Expand Down
121 changes: 121 additions & 0 deletions src/Models/NonhydrostaticModels/background_fields.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Oceananigans.Fields: ZeroField, ConstantField, AbstractField, FunctionField, location
using Oceananigans.Utils: prettysummary

using Adapt

function background_velocity_fields(fields, grid, clock)
u = get(fields, :u, ZeroField())
v = get(fields, :v, ZeroField())
w = get(fields, :w, ZeroField())

u = regularize_background_field(Face, Center, Center, u, grid, clock)
v = regularize_background_field(Center, Face, Center, v, grid, clock)
w = regularize_background_field(Center, Center, Face, w, grid, clock)

return (; u, v, w)
end

function background_tracer_fields(bg, tracer_names, grid, clock)
tracer_fields =
Tuple(c ∈ keys(bg) ?
regularize_background_field(Center, Center, Center, getindex(bg, c), grid, clock) :
ZeroField()
for c in tracer_names)

return NamedTuple{tracer_names}(tracer_fields)
end

#####
##### BackgroundFields (with option for including background closure fluxes)
#####

struct BackgroundFields{Q, U, C}
velocities :: U
tracers :: C
function BackgroundFields{Q}(velocities::U, tracers::C) where {Q, U, C}
return new{Q, U, C}(velocities, tracers)
end
end

Adapt.adapt_structure(to, bf::BackgroundFields{Q}) where Q =
BackgroundFields{Q}(adapt(to, bf.velocities), adapt(to, bf.tracers))

const BackgroundFieldsWithClosureFluxes = BackgroundFields{true}

function BackgroundFields(; background_closure_fluxes=false, fields...)
u = get(fields, :u, ZeroField())
v = get(fields, :v, ZeroField())
w = get(fields, :w, ZeroField())
velocities = (; u, v, w)
tracers = NamedTuple(name => fields[name] for name in keys(fields) if !(name ∈ (:u, :v, :w)))
return BackgroundFields{background_closure_fluxes}(velocities, tracers)
end

function BackgroundFields(background_fields::BackgroundFields{Q}, tracer_names, grid, clock) where Q
velocities = background_velocity_fields(background_fields.velocities, grid, clock)
tracers = background_tracer_fields(background_fields.tracers, tracer_names, grid, clock)
return BackgroundFields{Q}(velocities, tracers)
end

function BackgroundFields(background_fields::NamedTuple, tracer_names, grid, clock)
velocities = background_velocity_fields(background_fields, grid, clock)
tracers = background_tracer_fields(background_fields, tracer_names, grid, clock)
return BackgroundFields{false}(velocities, tracers)
end

"""
BackgroundField{F, P}

Temporary container for storing information about `BackgroundFields`.
"""
struct BackgroundField{F, P}
func:: F
parameters :: P
end

"""
BackgroundField(func; parameters=nothing)

Returns a `BackgroundField` to be passed to `NonhydrostaticModel` for use
as a background velocity or tracer field.

If `parameters` is not provided, `func` must be callable with the signature

```julia
func(x, y, z, t)
```

If `parameters` is provided, `func` must be callable with the signature

```julia
func(x, y, z, t, parameters)
```
"""
BackgroundField(func; parameters=nothing) = BackgroundField(func, parameters)

# Fallback, assuming a function
regularize_background_field(LX, LY, LZ, f::BackgroundField, grid, clock) =
FunctionField{LX, LY, LZ}(f.func, grid; clock, parameters=f.parameters)

regularize_background_field(LX, LY, LZ, f::BackgroundField{<:Number}, grid, clock) = ConstantField(f.func)

regularize_background_field(LX, LY, LZ, f::BackgroundField{<:Function}, grid, clock) =
FunctionField{LX, LY, LZ}(f.func, grid; clock, parameters=f.parameters)

regularize_background_field(LX, LY, LZ, func::Function, grid, clock) =
FunctionField{LX, LY, LZ}(func, grid; clock=clock)

regularize_background_field(LX, LY, LZ, ::ZeroField, grid, clock) = ZeroField()

function regularize_background_field(LX, LY, LZ, field::AbstractField, grid, clock)
if location(field) != (LX, LY, LZ)
throw(ArgumentError("Cannot use field at $(location(field)) as a background field at $((LX, LY, LZ))"))
end

return field
end

Base.show(io::IO, field::BackgroundField{F, P}) where {F, P} =
print(io, "BackgroundField{$F, $P}", "\n",
"├── func: $(prettysummary(field.func))", "\n",
"└── parameters: $(field.parameters)")
5 changes: 3 additions & 2 deletions src/Models/NonhydrostaticModels/nonhydrostatic_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Oceananigans.Advection: Centered, adapt_advection_order
using Oceananigans.BuoyancyFormulations: validate_buoyancy, regularize_buoyancy, SeawaterBuoyancy
using Oceananigans.Biogeochemistry: validate_biogeochemistry, AbstractBiogeochemistry, biogeochemical_auxiliary_fields
using Oceananigans.BoundaryConditions: regularize_field_boundary_conditions
using Oceananigans.Fields: BackgroundFields, Field, tracernames, VelocityFields, TracerFields, CenterField
using Oceananigans.Fields: Field, tracernames, VelocityFields, TracerFields, CenterField
using Oceananigans.Forcings: model_forcing
using Oceananigans.Grids: inflate_halo_size, with_halo, architecture
using Oceananigans.ImmersedBoundaries: ImmersedBoundaryGrid
Expand All @@ -24,6 +24,7 @@ import Oceananigans.Models: total_velocities, default_nan_checker, timestepper

const ParticlesOrNothing = Union{Nothing, AbstractLagrangianParticles}
const AbstractBGCOrNothing = Union{Nothing, AbstractBiogeochemistry}
const BFOrNamedTuple = Union{BackgroundFields, NamedTuple}

# TODO: this concept may be more generally useful,
# but for now we use it only for hydrostatic pressure anomalies for now.
Expand Down Expand Up @@ -122,7 +123,7 @@ function NonhydrostaticModel(; grid,
boundary_conditions::NamedTuple = NamedTuple(),
tracers = (),
timestepper = :RungeKutta3,
background_fields::NamedTuple = NamedTuple(),
background_fields::BFOrNamedTuple = NamedTuple(),
particles::ParticlesOrNothing = nothing,
biogeochemistry::AbstractBGCOrNothing = nothing,
velocities = nothing,
Expand Down
Loading