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

Invoke mask_immersed_field! when converting args for Makie #3725

Merged
merged 11 commits into from
Aug 25, 2024
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,6 @@ if ci_build
deploydocs(repo = "github.com/CliMA/OceananigansDocumentation.git",
versions = ["stable" => "v^", "dev" => "dev", "v#.#.#"],
forcepush = true,
push_preview = true,
push_preview = false,
devbranch = "main")
end
20 changes: 6 additions & 14 deletions examples/internal_tide.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,11 @@ wmax = maximum(abs, w_t[end])
times = u′_t.times
nothing #hide

# For visualization purposes, we mask the region below the bathymetry with NaNs.

using Oceananigans.ImmersedBoundaries: mask_immersed_field!

for φ_t in (u′_t, w_t, N²_t), n in 1:length(times)
mask_immersed_field!(φ_t[n], NaN)
end

# We retrieve each field's coordinates and convert from meters to kilometers.

xu, yu, zu = nodes(u′_t[1])
xw, yw, zw = nodes(w_t[1])
xN², yN², zN² = nodes(N²_t[1])
xu, _, zu = nodes(u′_t[1])
xw, _, zw = nodes(w_t[1])
xN², _, zN² = nodes(N²_t[1])

xu = xu ./ 1e3
xw = xw ./ 1e3
Expand All @@ -242,9 +234,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 interior(u′_t[$n], :, 1, :)
wₙ = @lift interior( w_t[$n], :, 1, :)
N²ₙ = @lift interior(N²_t[$n], :, 1, :)
u′ₙ = @lift u′_t[$n]
wₙ = @lift w_t[$n]
N²ₙ = @lift N²_t[$n]

axis_kwargs = (xlabel = "x [km]",
ylabel = "z [km]",
Expand Down
23 changes: 18 additions & 5 deletions ext/OceananigansMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module OceananigansMakieExt

using Oceananigans
using Oceananigans.Architectures: on_architecture
using Oceananigans.ImmersedBoundaries: mask_immersed_field!

using MakieCore: AbstractPlot
import MakieCore: convert_arguments, _create_plot
Expand All @@ -22,7 +23,20 @@ end
convert_arguments(pl::Type{<:AbstractPlot}, f::Field) =
convert_arguments(pl, convert_field_argument(f)...)

function flattened_cpu_interior(f)
"""
make_plottable_array(f)

Convert a field `f` to an array that can be plotted with Makie by

- masking immersed cells (for fields on immersed boundary
grids) with NaNs;
- dropping singleton dimensions, and
- transferring data from GPU to CPU if necessary.
"""
function make_plottable_array(f)

mask_immersed_field!(f, NaN)

Nx, Ny, Nz = size(f)

ii = drop_singleton_indices(Nx)
Expand All @@ -37,8 +51,7 @@ end

function convert_field_argument(f::Field)

# Drop singleton dimensions and convert to CPU if necessary
fi_cpu = flattened_cpu_interior(f)
fi_cpu = make_plottable_array(f)

# Indices of the non-zero dimensions
d1 = findfirst(n -> n > 1, size(f))
Expand Down Expand Up @@ -83,12 +96,12 @@ end
#####

function convert_arguments(pl::Type{<:AbstractPlot}, ξ1::AbstractArray, f::Field)
fi_cpu = flattened_cpu_interior(f)
fi_cpu = make_plottable_array(f)
return convert_arguments(pl, ξ1, fi_cpu)
end

function convert_arguments(pl::Type{<:AbstractPlot}, ξ1::AbstractArray, ξ2::AbstractArray, f::Field)
fi_cpu = flattened_cpu_interior(f)
fi_cpu = make_plottable_array(f)
return convert_arguments(pl, ξ1, ξ2, fi_cpu)
end

Expand Down