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

Fix _immered_cell and adapt for PartialCellBottom #3682

Merged
merged 25 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d9529a2
fix a bug in the adapt
simone-silvestri Aug 5, 2024
0992e0a
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 8, 2024
6e5b307
first fix
simone-silvestri Aug 8, 2024
1bdf889
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 8, 2024
f1f249d
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 14, 2024
292f556
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 15, 2024
6b47133
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 29, 2024
ed6d252
bugfix
simone-silvestri Aug 29, 2024
ffca52b
these should be all the bugs, still need to add the tests
simone-silvestri Aug 29, 2024
0fd3986
another bugfix
simone-silvestri Aug 29, 2024
c55f8c3
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Aug 29, 2024
ee46994
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Sep 4, 2024
9c00248
Merge branch 'main' into ss/adapt-bottom-height
glwagner Sep 25, 2024
0ed035b
Clamp bottom_height to top and bottom of grid
glwagner Sep 25, 2024
00ffa57
Bugfix
glwagner Sep 25, 2024
e8f6bc3
Merge remote-tracking branch 'origin/glw/clamp-bottom-height' into ss…
glwagner Sep 25, 2024
2ed8f01
Implement clamping for both GF and PC bottom
glwagner Sep 25, 2024
a200fd0
Convert internal tide example to use extension
glwagner Sep 25, 2024
47eeff0
apply_regionally *sigh*
glwagner Sep 25, 2024
6f796a4
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Sep 25, 2024
9b1244a
Disambiguate
glwagner Sep 25, 2024
7ccd608
Merge branch 'ss/adapt-bottom-height' of https://github.com/CliMA/Oce…
glwagner Sep 25, 2024
9807bd0
using CairoMakie
simone-silvestri Sep 25, 2024
8d79478
Merge branch 'main' into ss/adapt-bottom-height
simone-silvestri Sep 27, 2024
0f39bc8
Merge branch 'main' into ss/adapt-bottom-height
glwagner Sep 27, 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
13 changes: 7 additions & 6 deletions examples/internal_tide.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Oceananigans
using Oceananigans.Units
using Oceananigans.ImmersedBoundaries: PartialCellBottom

# ## Grid

Expand Down Expand Up @@ -46,15 +47,15 @@ width = 20kilometers
hill(x) = h₀ * exp(-x^2 / 2width^2)
bottom(x) = - H + hill(x)

grid = ImmersedBoundaryGrid(underlying_grid, GridFittedBottom(bottom))
grid = ImmersedBoundaryGrid(underlying_grid, PartialCellBottom(bottom))

# Let's see how the domain with the bathymetry is.

x = xnodes(grid, Center())
bottom_boundary = interior(grid.immersed_boundary.bottom_height, :, 1, 1)
top_boundary = 0 * x

using CairoMakie
using GLMakie

fig = Figure(size = (700, 200))
ax = Axis(fig[1, 1],
Expand Down Expand Up @@ -132,7 +133,7 @@ bᵢ(x, z) = Nᵢ² * z

set!(model, u=uᵢ, b=bᵢ)

# Now let's built a `Simulation`.
# Now let's build a `Simulation`.

Δt = 5minutes
stop_time = 4days
Expand Down Expand Up @@ -234,9 +235,9 @@ n = Observable(1)
title = @lift @sprintf("t = %1.2f days = %1.2f T₂",
round(times[$n] / day, digits=2) , round(times[$n] / T₂, digits=2))

u′ₙ = @lift u′_t[$n]
wₙ = @lift w_t[$n]
N²ₙ = @lift N²_t[$n]
u′ₙ = @lift interior(u′_t[$n], :, 1, :)
wₙ = @lift interior( w_t[$n], :, 1, :)
N²ₙ = @lift interior(N²_t[$n], :, 1, :)

axis_kwargs = (xlabel = "x [km]",
ylabel = "z [km]",
Expand Down
29 changes: 22 additions & 7 deletions src/ImmersedBoundaries/partial_cell_bottom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PartialCellBottom{H, E} <: AbstractGridFittedBottom{H}
minimum_fractional_cell_height :: E
end

const PCBIBG = ImmersedBoundaryGrid{<:Any, <:Any, <:Any, <:Any, <:Any, <:PartialCellBottom}
const PCBIBG{FT, TX, TY, TZ} = ImmersedBoundaryGrid{FT, TX, TY, TZ, <:Any, <:PartialCellBottom} where {FT, TX, TY, TZ}

function Base.summary(ib::PartialCellBottom)
hmax = maximum(parent(ib.bottom_height))
Expand Down Expand Up @@ -77,10 +77,10 @@ function on_architecture(arch, ib::PartialCellBottom{<:Field})
return PartialCellBottom(new_bottom_height, ib.minimum_fractional_cell_height)
end

Adapt.adapt_structure(to, ib::PartialCellBottom) = PartialCellBottom(adapt(to, ib.bottom_height.data),
Adapt.adapt_structure(to, ib::PartialCellBottom) = PartialCellBottom(adapt(to, ib.bottom_height),
ib.minimum_fractional_cell_height)

on_architecture(to, ib::PartialCellBottom) = PartialCellBottom(on_architecture(to, ib.bottom_height.data),
on_architecture(to, ib::PartialCellBottom) = PartialCellBottom(on_architecture(to, ib.bottom_height),
on_architecture(to, ib.minimum_fractional_cell_height))

"""
Expand All @@ -95,10 +95,13 @@ Criterion is h ≥ z - ϵ Δz

"""
@inline function _immersed_cell(i, j, k, underlying_grid, ib::PartialCellBottom)
# Face node above current cell
z = znode(i, j, k+1, underlying_grid, c, c, f)
h = @inbounds ib.bottom_height[i, j, 1]
return z ≤ h
# Face node below current cell
z = znode(i, j, k, underlying_grid, c, c, f)
h = @inbounds ib.bottom_height[i, j, 1]
ϵ = ib.minimum_fractional_cell_height
# z + Δz is equal to the face above the current cell
Δz = Δzᶜᶜᶜ(i, j, k, underlying_grid)
return z + Δz * (1 - ϵ) ≤ h
end

@inline bottom_cell(i, j, k, ibg::PCBIBG) = !immersed_cell(i, j, k, ibg.underlying_grid, ibg.immersed_boundary) &
Expand Down Expand Up @@ -137,6 +140,10 @@ end
return Δz
end

# Make sure Δz works for Flat topologies. We assume PCBIBG requires a Bounded Z-topology.
XFlatPCBIBG = ImmersedBoundaryGrid{<:Any, <:Flat, <:Any, <:Any, <:Any, <:PartialCellBottom}
YFlatPCBIBG = ImmersedBoundaryGrid{<:Any, <:Any, <:Flat, <:Any, <:Any, <:PartialCellBottom}

@inline Δzᶠᶜᶜ(i, j, k, ibg::PCBIBG) = min(Δzᶜᶜᶜ(i-1, j, k, ibg), Δzᶜᶜᶜ(i, j, k, ibg))
@inline Δzᶜᶠᶜ(i, j, k, ibg::PCBIBG) = min(Δzᶜᶜᶜ(i, j-1, k, ibg), Δzᶜᶜᶜ(i, j, k, ibg))
@inline Δzᶠᶠᶜ(i, j, k, ibg::PCBIBG) = min(Δzᶠᶜᶜ(i, j-1, k, ibg), Δzᶠᶜᶜ(i, j, k, ibg))
Expand All @@ -145,4 +152,12 @@ end
@inline Δzᶜᶠᶠ(i, j, k, ibg::PCBIBG) = min(Δzᶜᶜᶠ(i, j-1, k, ibg), Δzᶜᶜᶠ(i, j, k, ibg))
@inline Δzᶠᶠᶠ(i, j, k, ibg::PCBIBG) = min(Δzᶠᶜᶠ(i, j-1, k, ibg), Δzᶠᶜᶠ(i, j, k, ibg))

@inline Δzᶠᶜᶜ(i, j, k, ibg::XFlatPCBIBG) = Δzᶜᶜᶜ(i, j, k, ibg)
@inline Δzᶠᶜᶠ(i, j, k, ibg::XFlatPCBIBG) = Δzᶜᶜᶠ(i, j, k, ibg)
@inline Δzᶜᶠᶜ(i, j, k, ibg::YFlatPCBIBG) = Δzᶜᶜᶜ(i, j, k, ibg)

@inline Δzᶜᶠᶠ(i, j, k, ibg::YFlatPCBIBG) = Δzᶜᶜᶠ(i, j, k, ibg)
@inline Δzᶠᶠᶜ(i, j, k, ibg::XFlatPCBIBG) = Δzᶜᶠᶜ(i, j, k, ibg)
@inline Δzᶠᶠᶜ(i, j, k, ibg::YFlatPCBIBG) = Δzᶠᶜᶜ(i, j, k, ibg)

@inline z_bottom(i, j, ibg::PCBIBG) = @inbounds ibg.immersed_boundary.bottom_height[i, j, 1]
16 changes: 16 additions & 0 deletions test/test_immersed_boundary_grid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include("dependencies_for_runtests.jl")

grid = RectilinearGrid(; size=(2, 2, 2), extent = (1, 1, 1))

@testset "Testing Immersed Boundaries" begin

@info "Testing the immersed boundary construction..."

bottom(x, y) = -1 + 0.5 * exp(-x^2 - y^2)
ibg = ImmersedBoundaryGrid(grid, GridFittedBottom(bottom))

# Unit test (bottom is at the right position)

@info "Testing stably stratified initial conditions..."

end