Skip to content

Clean up update_boundary_conditions! #4503

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 6 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions src/BoundaryConditions/update_boundary_conditions.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Oceananigans: boundary_conditions

@inline update_boundary_conditions!(bcs, field, model) = nothing
@inline update_boundary_condition!(bc, side, field, model) = nothing
@inline update_boundary_condition!(bcs, field, model) = nothing

function update_boundary_condition!(bcs::FieldBoundaryConditions, field, model)
function update_boundary_conditions!(bcs::FieldBoundaryConditions, field, model)
update_boundary_condition!(bcs.west, Val(:west), field, model)
update_boundary_condition!(bcs.east, Val(:east), field, model)
update_boundary_condition!(bcs.south, Val(:south), field, model)
Expand All @@ -14,12 +14,12 @@ function update_boundary_condition!(bcs::FieldBoundaryConditions, field, model)
return nothing
end

update_boundary_condition!(fields::NamedTuple, model) = update_boundary_condition!(values(fields), model)
update_boundary_conditions!(fields::NamedTuple, model) = update_boundary_conditions!(values(fields), model)

function update_boundary_condition!(fields::Tuple, model)
function update_boundary_conditions!(fields::Tuple, model)
for field in fields
bcs = boundary_conditions(field)
update_boundary_condition!(bcs, field, model)
update_boundary_conditions!(bcs, field, model)
end

return nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Oceananigans.BoundaryConditions

using Oceananigans: UpdateStateCallsite
using Oceananigans.Biogeochemistry: update_biogeochemical_state!
using Oceananigans.BoundaryConditions: update_boundary_condition!, replace_horizontal_vector_halos!
using Oceananigans.BoundaryConditions: update_boundary_conditions!, replace_horizontal_vector_halos!
using Oceananigans.TurbulenceClosures: compute_diffusivities!
using Oceananigans.ImmersedBoundaries: mask_immersed_field!, mask_immersed_field_xy!, inactive_node
using Oceananigans.Models: update_model_field_time_series!
Expand Down Expand Up @@ -37,7 +37,7 @@ function update_state!(model::HydrostaticFreeSurfaceModel, grid, callbacks; comp
@apply_regionally update_model_field_time_series!(model, model.clock)

# Update the boundary conditions
@apply_regionally update_boundary_condition!(fields(model), model)
@apply_regionally update_boundary_conditions!(fields(model), model)

tupled_fill_halo_regions!(prognostic_fields(model), grid, model.clock, fields(model), async=true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Oceananigans: UpdateStateCallsite
using Oceananigans.Architectures
using Oceananigans.BoundaryConditions
using Oceananigans.Biogeochemistry: update_biogeochemical_state!
using Oceananigans.BoundaryConditions: update_boundary_condition!
using Oceananigans.BoundaryConditions: update_boundary_conditions!
using Oceananigans.TurbulenceClosures: compute_diffusivities!
using Oceananigans.Fields: compute!
using Oceananigans.ImmersedBoundaries: mask_immersed_field!
Expand All @@ -28,7 +28,7 @@ function update_state!(model::NonhydrostaticModel, callbacks=[]; compute_tendenc
update_model_field_time_series!(model, model.clock)

# Update the boundary conditions
update_boundary_condition!(fields(model), model)
update_boundary_conditions!(fields(model), model)

# Fill halos for velocities and tracers
fill_halo_regions!(merge(model.velocities, model.tracers), model.clock, fields(model);
Expand Down
93 changes: 40 additions & 53 deletions src/Models/boundary_mean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,46 @@ import Adapt: adapt_structure
import Base: summary, show
import Oceananigans.BoundaryConditions: update_boundary_condition!

"""
BoundaryAdjacentMean

Stores the boundary mean `value` of a `Field`. Updated by calling
```jldoctest
julia> using Oceananigans

julia> using Oceananigans.Models: BoundaryAdjacentMean

julia> grid = RectilinearGrid(size = (16, 16, 16), extent = (3, 4, 5))
16×16×16 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── Periodic x ∈ [0.0, 3.0) regularly spaced with Δx=0.1875
├── Periodic y ∈ [0.0, 4.0) regularly spaced with Δy=0.25
└── Bounded z ∈ [-5.0, 0.0] regularly spaced with Δz=0.3125

julia> cf = CenterField(grid)
16×16×16 Field{Center, Center, Center} on RectilinearGrid on CPU
├── grid: 16×16×16 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── boundary conditions: FieldBoundaryConditions
│ └── west: Periodic, east: Periodic, south: Periodic, north: Periodic, bottom: ZeroFlux, top: ZeroFlux, immersed: ZeroFlux
└── data: 22×22×22 OffsetArray(::Array{Float64, 3}, -2:19, -2:19, -2:19) with eltype Float64 with indices -2:19×-2:19×-2:19
└── max=0.0, min=0.0, mean=0.0

julia> set!(cf, (x, y, z) -> sin(2π * y / 4))
16×16×16 Field{Center, Center, Center} on RectilinearGrid on CPU
├── grid: 16×16×16 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── boundary conditions: FieldBoundaryConditions
│ └── west: Periodic, east: Periodic, south: Periodic, north: Periodic, bottom: ZeroFlux, top: ZeroFlux, immersed: ZeroFlux
└── data: 22×22×22 OffsetArray(::Array{Float64, 3}, -2:19, -2:19, -2:19) with eltype Float64 with indices -2:19×-2:19×-2:19
└── max=0.980785, min=-0.980785, mean=1.10534e-16

julia> bam = BoundaryAdjacentMean(grid, :east)
BoundaryAdjacentMean: (0.0)

julia> bam(:east, cf)
-1.5612511283791264e-18

```
"""
struct BoundaryAdjacentMean{FF, BV}
flux_field :: FF
value :: BV

BoundaryAdjacentMean(grid, side;
flux_field::FF = boundary_reduced_field(Val(side), grid),
value::BV = Ref(zero(grid))) where {FF, BV} =
@doc """
BoundaryAdjacentMean(grid, side;
flux_field::FF = boundary_reduced_field(Val(side), grid),
value::BV = Ref(zero(grid)))

Store the boundary mean `value` of a `Field`. Updated by calling

```jldoctest
julia> using Oceananigans

julia> using Oceananigans.Models: BoundaryAdjacentMean

julia> grid = RectilinearGrid(size = (16, 16, 16), extent = (3, 4, 5));

julia> cf = CenterField(grid);

julia> set!(cf, (x, y, z) -> sin(2π * y / 4))
16×16×16 Field{Center, Center, Center} on RectilinearGrid on CPU
├── grid: 16×16×16 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── boundary conditions: FieldBoundaryConditions
│ └── west: Periodic, east: Periodic, south: Periodic, north: Periodic, bottom: ZeroFlux, top: ZeroFlux, immersed: ZeroFlux
└── data: 22×22×22 OffsetArray(::Array{Float64, 3}, -2:19, -2:19, -2:19) with eltype Float64 with indices -2:19×-2:19×-2:19
└── max=0.980785, min=-0.980785, mean=1.10534e-16

julia> bam = BoundaryAdjacentMean(grid, :east)
BoundaryAdjacentMean: (0.0)

julia> bam(:east, cf)

julia> bam
BoundaryAdjacentMean: (-1.5612511283791264e-18)
```
"""
BoundaryAdjacentMean(grid, side;
flux_field::FF = boundary_reduced_field(Val(side), grid),
value::BV = Ref(zero(grid))) where {FF, BV} =
new{FF, BV}(flux_field, value)
end

Expand All @@ -62,7 +56,7 @@ end
Adapt.adapt_structure(to, mo::BoundaryAdjacentMean) =
BoundaryAdjacentMean(; flux_fields = nothing, value = adapt(to, mo.value[]))

Base.show(io::IO, bam::BoundaryAdjacentMean) = print(io, summary(bam)*"\n")
Base.show(io::IO, bam::BoundaryAdjacentMean) = print(io, summary(bam))
Base.summary(bam::BoundaryAdjacentMean) = "BoundaryAdjacentMean: ($(bam.value[]))"

@inline boundary_reduced_field(::Union{Val{:west}, Val{:east}}, grid) = Field{Center, Nothing, Nothing}(grid)
Expand Down Expand Up @@ -93,7 +87,7 @@ Base.summary(bam::BoundaryAdjacentMean) = "BoundaryAdjacentMean: ($(bam.value[])

(bam::BoundaryAdjacentMean)(side, u) = bam(Val(side), u)

# computes the boundary mean and stores/returns it
# Computes the boundary mean and stores it.
function (bam::BoundaryAdjacentMean)(val_side::Val, u)
grid = u.grid

Expand All @@ -111,15 +105,8 @@ function (bam::BoundaryAdjacentMean)(val_side::Val, u)

bam.value[] /= CUDA.@allowscalar bam.flux_field[iB, jB, kB]

return bam.value[]
return nothing
end

# let this get updated in boundary conditions

const MOOBC = BoundaryCondition{<:Open, <:BoundaryAdjacentMean}

@inline function update_boundary_condition!(bc::MOOBC, val_side, u, model)
bc.condition(val_side, u)

return nothing
end
@inline update_boundary_condition!(bc::MOOBC, val_side, u, model) = bc.condition(val_side, u)