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

Refactor SplitExplicitFreeSurface #3894

Open
wants to merge 92 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
4044e16
new bottom height
simone-silvestri Oct 9, 2024
b83a6ae
now it should work
simone-silvestri Oct 9, 2024
5ebaa84
comment
simone-silvestri Oct 9, 2024
f9ad227
comment
simone-silvestri Oct 9, 2024
cbdb713
remove circular dependency for now
simone-silvestri Oct 9, 2024
7f08768
some bugfixes
simone-silvestri Oct 9, 2024
4b7926f
change name to column_height
simone-silvestri Oct 9, 2024
6b7b27c
correct column height
simone-silvestri Oct 9, 2024
fdee366
whoops
simone-silvestri Oct 9, 2024
d1c145c
another correction
simone-silvestri Oct 9, 2024
dbb411a
some more changes
simone-silvestri Oct 9, 2024
3ec05df
another correction
simone-silvestri Oct 9, 2024
4e4d40b
couple of more bugfixes
simone-silvestri Oct 9, 2024
a6362ee
more bugfixes
simone-silvestri Oct 9, 2024
3bce6fc
this should make it work
simone-silvestri Oct 9, 2024
0fa1a67
unify the formulations
simone-silvestri Oct 9, 2024
135cac3
correct implementation
simone-silvestri Oct 9, 2024
156dada
correct implementation
simone-silvestri Oct 9, 2024
04e7170
correct partial cell bottom
simone-silvestri Oct 9, 2024
9258572
use center immersed condition for grid fitted boundary
simone-silvestri Oct 9, 2024
9df333a
use the *correct* center node
simone-silvestri Oct 9, 2024
053b26f
no h for z-values!
simone-silvestri Oct 9, 2024
61b7e7f
simplify partial cells
simone-silvestri Oct 9, 2024
1098c58
make sure we don't go out of bounds
simone-silvestri Oct 9, 2024
9e35af6
back to immersed condition
simone-silvestri Oct 9, 2024
ac7c96d
name changes
simone-silvestri Oct 10, 2024
f8d5c18
bugfix
simone-silvestri Oct 10, 2024
fd8a028
another bugfix
simone-silvestri Oct 10, 2024
f518d41
fix bugs
simone-silvestri Oct 11, 2024
983b956
some corrections
simone-silvestri Oct 11, 2024
dfada79
another bugfix
simone-silvestri Oct 11, 2024
2705b03
domain_depth
simone-silvestri Oct 11, 2024
ca935b6
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 11, 2024
cc9cd4f
some remaining `z_bottom`s
simone-silvestri Oct 11, 2024
495f6ea
Merge branch 'ss/new-bottom-height' of github.com:CliMA/Oceananigans.…
simone-silvestri Oct 11, 2024
45b5cd4
back as it was
simone-silvestri Oct 11, 2024
d381037
bugfix
simone-silvestri Oct 16, 2024
b8401cc
correct correction
simone-silvestri Oct 16, 2024
cfd5c42
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 17, 2024
3ecd318
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 21, 2024
b967754
static_column_depth
simone-silvestri Oct 21, 2024
3f36846
Merge branch 'ss/new-bottom-height' of github.com:CliMA/Oceananigans.…
simone-silvestri Oct 21, 2024
9ff305e
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 21, 2024
6046d2d
Update src/Grids/grid_utils.jl
simone-silvestri Oct 21, 2024
8106a70
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 23, 2024
a10df16
address comments
simone-silvestri Oct 23, 2024
5ee7623
new comment
simone-silvestri Oct 23, 2024
9777ee6
another name change
simone-silvestri Oct 23, 2024
1d219af
AGFBIBG istead of AGFBIB and z_bottom only in TurbulenceClosures
simone-silvestri Oct 23, 2024
238fa74
some bugfixes
simone-silvestri Oct 23, 2024
90dfa7a
better definition of bottom height
simone-silvestri Oct 23, 2024
f2402f0
fixed partial cell
simone-silvestri Oct 23, 2024
55d7432
fixed partial cells
simone-silvestri Oct 23, 2024
849c253
remove OrthogonalSphericalShellGrids while we decide what to do
simone-silvestri Oct 23, 2024
510a858
these files shouldn't go here
simone-silvestri Oct 23, 2024
926feda
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 25, 2024
aea0601
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 28, 2024
dfd0b30
Merge remote-tracking branch 'origin/main' into ss/new-bottom-height
simone-silvestri Oct 28, 2024
fe2d552
new operators
simone-silvestri Oct 28, 2024
d66baf2
Merge branch 'main' into ss/new-bottom-height
simone-silvestri Oct 30, 2024
c57a321
this was removed
simone-silvestri Oct 30, 2024
d78d659
start refactoring
simone-silvestri Oct 30, 2024
8653278
start refactoring
simone-silvestri Oct 30, 2024
4c728b3
some more refactoring
simone-silvestri Oct 30, 2024
04087ad
big overhaul
simone-silvestri Oct 30, 2024
ba7eb2f
Merge remote-tracking branch 'origin/main' into ss/refactor-split-exp…
simone-silvestri Oct 31, 2024
b9ec92b
more refactoring
simone-silvestri Oct 31, 2024
57be084
compiles
simone-silvestri Oct 31, 2024
c522ad3
more refactoring...
simone-silvestri Oct 31, 2024
dad5654
correction
simone-silvestri Nov 1, 2024
a253cc0
use a module for now
simone-silvestri Nov 1, 2024
e350d1c
make module work
simone-silvestri Nov 1, 2024
81a2c61
some more organizational stuff
simone-silvestri Nov 1, 2024
cf6d49d
some more changes
simone-silvestri Nov 1, 2024
4c844d8
it compiles
simone-silvestri Nov 1, 2024
d6d0bc7
using free_surface_displacement_field
simone-silvestri Nov 1, 2024
bb5c992
include g_Earth
simone-silvestri Nov 1, 2024
611aaec
now it should run
simone-silvestri Nov 1, 2024
aface2e
a little bit of cleanup
simone-silvestri Nov 1, 2024
811a1a6
tests should run now
simone-silvestri Nov 1, 2024
b90e4ac
Merge branch 'main' into ss/refactor-split-explicit
simone-silvestri Nov 1, 2024
df17eca
Update Project.toml
simone-silvestri Nov 1, 2024
fee6ace
conceptually this is better
simone-silvestri Nov 1, 2024
3b45d3b
fix checkpointer test
simone-silvestri Nov 1, 2024
e06226b
fix split explicit settings
simone-silvestri Nov 1, 2024
b09ce27
fixing some more tests
simone-silvestri Nov 1, 2024
dd9bd45
import with_halo
simone-silvestri Nov 1, 2024
1b15f91
back on Enzyme
simone-silvestri Nov 1, 2024
2e21d03
fix tets
simone-silvestri Nov 4, 2024
07cf9ab
some fixes
simone-silvestri Nov 4, 2024
bc3833e
these are prognostic fields
simone-silvestri Nov 4, 2024
581848b
comment
simone-silvestri Nov 4, 2024
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
2 changes: 1 addition & 1 deletion ext/OceananigansMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
convert_arguments(pl::Type{<:AbstractPlot}, f::Field) =
convert_arguments(pl, convert_field_argument(f)...)

function convert_arguments(pl::Type{<:AbstractPlot}, fop::AbstractOperation)
function convert_arguments(pl::Type{<:AbstractPlot}, op::AbstractOperation)
f = Field(op)
compute!(f)
return convert_arguments(pl, f)
Expand Down
95 changes: 95 additions & 0 deletions geostrophic_adjustment.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Run this script with
#
# $ mpiexec -n 4 julia --project distributed_nonhydrostatic_two_dimensional_turbulence.jl
#
# for example.
#
# You also probably should set
#
# $ export JULIA_NUM_THREADS=1

using MPI
using Oceananigans
using Oceananigans.DistributedComputations
using Oceananigans.DistributedComputations: Sizes
using Oceananigans.Grids: topology, architecture
using Oceananigans.Units: kilometers, meters
using Printf
using JLD2

arch = CPU()

# Distribute problem irregularly
Nx = 80
Lh = 100kilometers
Lz = 400meters

topo = (Bounded, Periodic, Bounded)
grid = RectilinearGrid(arch,
size = (Nx, 3, 2),
x = (0, Lh),
y = (0, Lh),
z = (-Lz, 0),
topology = topo)

# bottom(x, y) = x > 80kilometers && x < 90kilometers ? 100 : -500meters
# grid = ImmersedBoundaryGrid(grid, GridFittedBottom(bottom); active_cells_map = true)

coriolis = FPlane(f=1e-4)

model = HydrostaticFreeSurfaceModel(; grid,
coriolis,
free_surface = SplitExplicitFreeSurface(grid; substeps=10))

gaussian(x, L) = exp(-x^2 / 2L^2)

U = 0.1 # geostrophic velocity
L = Lh / 40 # gaussian width
x₀ = Lh / 4 # gaussian center
vᵍ(x, y, z) = -U * (x - x₀) / L * gaussian(x - x₀, L)

g = model.free_surface.gravitational_acceleration
η = model.free_surface.η
η₀ = coriolis.f * U * L / g # geostrophic free surface amplitude

ηᵍ(x) = η₀ * gaussian(x - x₀, L)
ηⁱ(x, y, z) = 2 * ηᵍ(x)

set!(model, v = vᵍ)
set!(model, η = ηⁱ)

gravity_wave_speed = sqrt(g * grid.Lz) # hydrostatic (shallow water) gravity wave speed
Δt = 2 * model.grid.Δxᶜᵃᵃ / gravity_wave_speed
simulation = Simulation(model; Δt, stop_iteration = 1000)

ut = []
vt = []
ηt = []

save_u(sim) = push!(ut, deepcopy(sim.model.velocities.u))
save_v(sim) = push!(vt, deepcopy(sim.model.velocities.v))
save_η(sim) = push!(ηt, deepcopy(sim.model.free_surface.η))

function progress_message(sim)
@info @sprintf("[%.2f%%], iteration: %d, time: %.3f, max|w|: %.2e",
100 * sim.model.clock.time / sim.stop_time, sim.model.clock.iteration,
sim.model.clock.time, maximum(abs, sim.model.velocities.u))
end

simulation.callbacks[:save_η] = Callback(save_η, IterationInterval(1))
simulation.callbacks[:save_v] = Callback(save_v, IterationInterval(1))
simulation.callbacks[:save_u] = Callback(save_u, IterationInterval(1))
simulation.callbacks[:progress] = Callback(progress_message, IterationInterval(10))

run!(simulation)

iter = Observable(1)
η_img = @lift(interior(ηt[$iter], :, 1, 1))
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, η_img, color=:black)

GLMakie.record(fig, "test.mp4", 1:length(ηt), framerate=2) do i
@info "Animating iteration $i/$(length(ηt))..."
iter[] = i
end
1 change: 1 addition & 0 deletions src/Grids/Grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export xnodes, ynodes, znodes, λnodes, φnodes
export spacings
export xspacings, yspacings, zspacings, xspacing, yspacing, zspacing
export minimum_xspacing, minimum_yspacing, minimum_zspacing
export static_column_depthᶜᶜᵃ, static_column_depthᶠᶜᵃ, static_column_depthᶜᶠᵃ, static_column_depthᶠᶠᵃ
export offset_data, new_data
export on_architecture

Expand Down
9 changes: 9 additions & 0 deletions src/Grids/grid_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ coordinate_summary(topo, Δ::Union{AbstractVector, AbstractMatrix}, name) =
name, prettysummary(minimum(parent(Δ))),
name, prettysummary(maximum(parent(Δ))))

#####
##### Static column depth
#####

@inline static_column_depthᶜᶜᵃ(i, j, grid) = grid.Lz
@inline static_column_depthᶜᶠᵃ(i, j, grid) = grid.Lz
@inline static_column_depthᶠᶜᵃ(i, j, grid) = grid.Lz
@inline static_column_depthᶠᶠᵃ(i, j, grid) = grid.Lz

#####
##### Spherical geometry
#####
Expand Down
9 changes: 4 additions & 5 deletions src/ImmersedBoundaries/ImmersedBoundaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import Base: show, summary
import Oceananigans.Grids: cpu_face_constructor_x, cpu_face_constructor_y, cpu_face_constructor_z,
x_domain, y_domain, z_domain

import Oceananigans.Grids: architecture, on_architecture, with_halo, inflate_halo_size_one_dimension,
import Oceananigans.Grids: architecture, with_halo, inflate_halo_size_one_dimension,
xnode, ynode, znode, λnode, φnode, node,
ξnode, ηnode, rnode,
ξname, ηname, rname, node_names,
xnodes, ynodes, znodes, λnodes, φnodes, nodes,
ξnodes, ηnodes, rnodes,
static_column_depthᶜᶜᵃ, static_column_depthᶠᶜᵃ, static_column_depthᶜᶠᵃ, static_column_depthᶠᶠᵃ,
inactive_cell


import Oceananigans.Architectures: on_architecture

import Oceananigans.Fields: fractional_x_index, fractional_y_index, fractional_z_index

"""
Expand Down Expand Up @@ -92,10 +95,6 @@ with_halo(halo, ibg::ImmersedBoundaryGrid) =
inflate_halo_size_one_dimension(req_H, old_H, _, ::IBG) = max(req_H + 1, old_H)
inflate_halo_size_one_dimension(req_H, old_H, ::Type{Flat}, ::IBG) = 0

# Defining the bottom
@inline z_bottom(i, j, grid) = znode(i, j, 1, grid, c, c, f)
@inline z_bottom(i, j, ibg::IBG) = error("The function `bottom` has not been defined for $(summary(ibg))!")

function Base.summary(grid::ImmersedBoundaryGrid)
FT = eltype(grid)
TX, TY, TZ = topology(grid)
Expand Down
18 changes: 0 additions & 18 deletions src/ImmersedBoundaries/abstract_grid_fitted_boundary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,3 @@ const AGFB = AbstractGridFittedBoundary
@inline immersed_cell(i, j, k, grid::AbstractGrid{<:Any, <:Any, Flat, Flat}, ib::AGFB) = _immersed_cell(i, 1, 1, grid, ib)
@inline immersed_cell(i, j, k, grid::AbstractGrid{<:Any, Flat, Flat, Flat}, ib::AGFB) = _immersed_cell(1, 1, 1, grid, ib)
end

function clamp_bottom_height!(bottom_field, grid)
launch!(architecture(grid), grid, :xy, _clamp_bottom_height!, bottom_field, grid)
return nothing
end

const c = Center()
const f = Face()

@kernel function _clamp_bottom_height!(z, grid)
i, j = @index(Global, NTuple)
Nz = size(grid, 3)
zmin = znode(i, j, 1, grid, c, c, f)
zmax = znode(i, j, Nz+1, grid, c, c, f)
@inbounds z[i, j, 1] = clamp(z[i, j, 1], zmin, zmax)
end


84 changes: 56 additions & 28 deletions src/ImmersedBoundaries/grid_fitted_bottom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ abstract type AbstractGridFittedBottom{H} <: AbstractGridFittedBoundary end

# To enable comparison with PartialCellBottom in the limiting case that
# fractional cell height is 1.0.
struct CenterImmersedCondition end
struct InterfaceImmersedCondition end

Base.summary(::CenterImmersedCondition) = "CenterImmersedCondition"
Base.summary(::InterfaceImmersedCondition) = "InterfaceImmersedCondition"
struct CenterImmersedCondition end

struct GridFittedBottom{H, I} <: AbstractGridFittedBottom{H}
bottom_height :: H
immersed_condition :: I
end

Base.summary(::CenterImmersedCondition) = "CenterImmersedCondition"
Base.summary(::InterfaceImmersedCondition) = "InterfaceImmersedCondition"

const GFBIBG = ImmersedBoundaryGrid{<:Any, <:Any, <:Any, <:Any, <:Any, <:GridFittedBottom}

"""
Expand All @@ -36,6 +36,7 @@ Return a bottom immersed boundary.
Keyword Arguments
=================


* `bottom_height`: an array or function that gives the height of the
bottom in absolute ``z`` coordinates.

Expand Down Expand Up @@ -72,50 +73,77 @@ Base.summary(ib::GridFittedBottom{<:Function}) = @sprintf("GridFittedBottom(%s)"
function Base.show(io::IO, ib::GridFittedBottom)
print(io, summary(ib), '\n')
print(io, "├── bottom_height: ", prettysummary(ib.bottom_height), '\n')
print(io, "└── immersed_condition: ", summary(ib.immersed_condition))
end

@inline z_bottom(i, j, ibg::GFBIBG) = @inbounds ibg.immersed_boundary.bottom_height[i, j, 1]
on_architecture(arch, ib::GridFittedBottom) = GridFittedBottom(on_architecture(arch, ib.bottom_height), ib.immersed_condition)

function on_architecture(arch, ib::GridFittedBottom{<:Field})
architecture(ib.bottom_height) == arch && return ib
arch_grid = on_architecture(arch, ib.bottom_height.grid)
new_bottom_height = Field{Center, Center, Nothing}(arch_grid)
set!(new_bottom_height, ib.bottom_height)
fill_halo_regions!(new_bottom_height)
return GridFittedBottom(new_bottom_height, ib.immersed_condition)
end

Adapt.adapt_structure(to, ib::GridFittedBottom) = GridFittedBottom(adapt(to, ib.bottom_height), adapt(to, ib.immersed_condition))

"""
ImmersedBoundaryGrid(grid, ib::GridFittedBottom)

Return a grid with `GridFittedBottom` immersed boundary (`ib`).

Computes `ib.bottom_height` and wraps it in a Field.
Computes `ib.bottom_height` and wraps it in a Field. `ib.bottom_height` is the z-coordinate of top-most interface
of the last ``immersed`` cell in the column.
"""
function ImmersedBoundaryGrid(grid, ib::GridFittedBottom)
bottom_field = Field{Center, Center, Nothing}(grid)
set!(bottom_field, ib.bottom_height)
@apply_regionally clamp_bottom_height!(bottom_field, grid)
@apply_regionally compute_numerical_bottom_height!(bottom_field, grid, ib)
fill_halo_regions!(bottom_field)
new_ib = GridFittedBottom(bottom_field, ib.immersed_condition)
new_ib = GridFittedBottom(bottom_field)
TX, TY, TZ = topology(grid)
return ImmersedBoundaryGrid{TX, TY, TZ}(grid, new_ib)
end

@inline function _immersed_cell(i, j, k, underlying_grid, ib::GridFittedBottom{<:Any, <:InterfaceImmersedCondition})
z = znode(i, j, k+1, underlying_grid, c, c, f)
h = @inbounds ib.bottom_height[i, j, 1]
return z ≤ h
compute_numerical_bottom_height!(bottom_field, grid, ib) =
launch!(architecture(grid), grid, :xy, _compute_numerical_bottom_height!, bottom_field, grid, ib)

@kernel function _compute_numerical_bottom_height!(bottom_field, grid, ib::GridFittedBottom)
i, j = @index(Global, NTuple)
zb = @inbounds bottom_field[i, j, 1]
@inbounds bottom_field[i, j, 1] = znode(i, j, 1, grid, c, c, f)
condition = ib.immersed_condition
for k in 1:grid.Nz
z⁺ = znode(i, j, k+1, grid, c, c, f)
z = znode(i, j, k, grid, c, c, c)
bottom_cell = ifelse(condition isa CenterImmersedCondition, z ≤ zb, z⁺ ≤ zb)
@inbounds bottom_field[i, j, 1] = ifelse(bottom_cell, z⁺, zb)
end
end

@inline function _immersed_cell(i, j, k, underlying_grid, ib::GridFittedBottom{<:Any, <:CenterImmersedCondition})
z = znode(i, j, k, underlying_grid, c, c, c)
h = @inbounds ib.bottom_height[i, j, 1]
return z ≤ h
@inline function _immersed_cell(i, j, k, underlying_grid, ib::GridFittedBottom)
z = znode(i, j, k, underlying_grid, c, c, c)
zb = @inbounds ib.bottom_height[i, j, 1]
return z ≤ zb
end

on_architecture(arch, ib::GridFittedBottom) = GridFittedBottom(ib.bottom_height, ib.immersed_condition)
#####
##### Static column depth
#####

function on_architecture(arch, ib::GridFittedBottom{<:Field})
architecture(ib.bottom_height) == arch && return ib
arch_grid = on_architecture(arch, ib.bottom_height.grid)
new_bottom_height = Field{Center, Center, Nothing}(arch_grid)
set!(new_bottom_height, ib.bottom_height)
fill_halo_regions!(new_bottom_height)
return GridFittedBottom(new_bottom_height, ib.immersed_condition)
end
const AGFBIBG = ImmersedBoundaryGrid{<:Any, <:Any, <:Any, <:Any, <:Any, <:AbstractGridFittedBottom}

@inline static_column_depthᶜᶜᵃ(i, j, ibg::AGFBIBG) = @inbounds znode(i, j, ibg.Nz+1, ibg, c, c, f) - ibg.immersed_boundary.bottom_height[i, j, 1]
@inline static_column_depthᶜᶠᵃ(i, j, ibg::AGFBIBG) = min(static_column_depthᶜᶜᵃ(i, j-1, ibg), static_column_depthᶜᶜᵃ(i, j, ibg))
@inline static_column_depthᶠᶜᵃ(i, j, ibg::AGFBIBG) = min(static_column_depthᶜᶜᵃ(i-1, j, ibg), static_column_depthᶜᶜᵃ(i, j, ibg))
@inline static_column_depthᶠᶠᵃ(i, j, ibg::AGFBIBG) = min(static_column_depthᶠᶜᵃ(i, j-1, ibg), static_column_depthᶠᶜᵃ(i, j, ibg))

# Make sure column_height works for horizontally-Flat topologies.
XFlatAGFIBG = ImmersedBoundaryGrid{<:Any, <:Flat, <:Any, <:Any, <:Any, <:AbstractGridFittedBottom}
YFlatAGFIBG = ImmersedBoundaryGrid{<:Any, <:Any, <:Flat, <:Any, <:Any, <:AbstractGridFittedBottom}

Adapt.adapt_structure(to, ib::GridFittedBottom) = GridFittedBottom(adapt(to, ib.bottom_height),
ib.immersed_condition)
@inline static_column_depthᶠᶜᵃ(i, j, ibg::XFlatAGFIBG) = static_column_depthᶜᶜᵃ(i, j, ibg)
@inline static_column_depthᶜᶠᵃ(i, j, ibg::YFlatAGFIBG) = static_column_depthᶜᶜᵃ(i, j, ibg)
@inline static_column_depthᶠᶠᵃ(i, j, ibg::XFlatAGFIBG) = static_column_depthᶜᶠᵃ(i, j, ibg)
@inline static_column_depthᶠᶠᵃ(i, j, ibg::YFlatAGFIBG) = static_column_depthᶠᶜᵃ(i, j, ibg)
Loading