Skip to content

Commit 677347b

Browse files
glwagnergiordano
andauthored
[GPU] Add adapt_structure for MoistAirBuoyancy, AnelasticFormulation, and ReferenceState (#94)
* add adapt_structure for MoistAirBuoyancy and ReferenceState * Update examples/free_convection.jl Co-authored-by: Mosè Giordano <[email protected]> * add using CUDA * fix * add adapt for AnelasticFormulation --------- Co-authored-by: Mosè Giordano <[email protected]>
1 parent 0ef0e9e commit 677347b

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

examples/free_convection.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ using Oceananigans
22
using Oceananigans.Units
33
using Printf
44
using Breeze
5+
using CUDA
56

6-
arch = CPU()
7-
8-
Nx = Nz = 64
7+
arch = GPU()
8+
Nx = Nz = 128
99
Lz = 4 * 1024
1010
grid = RectilinearGrid(arch, size=(Nx, Nz), x=(0, 2Lz), z=(0, Lz), topology=(Periodic, Flat, Bounded))
1111

@@ -26,17 +26,15 @@ vapor_flux = FluxBoundaryCondition(1e-2)
2626
qᵗ_bcs = FieldBoundaryConditions(bottom=vapor_flux)
2727

2828
advection = WENO() #(momentum=WENO(), θ=WENO(), q=WENO(bounds=(0, 1)))
29-
tracers = (, :q)
3029
model = NonhydrostaticModel(; grid, advection, buoyancy,
3130
tracers = (, :qᵗ),
3231
boundary_conditions ==θ_bcs, qᵗ=qᵗ_bcs))
3332

3433
Lz = grid.Lz
35-
Δθ = 5 # K
34+
Δθ = 2 # K
3635
Tₛ = buoyancy.reference_state.potential_temperature # K
37-
θᵢ(x, z) = Tₛ + Δθ * z / Lz + 1e-2 * Δθ * randn()
38-
qᵗᵢ(x, z) = 0 # 1e-2 + 1e-5 * rand()
39-
set!(model, θ=θᵢ, qᵗ=qᵗᵢ)
36+
θᵢ(x, z) = Tₛ + Δθ * z / Lz + 1e-2 * Δθ * rand()
37+
set!(model, θ=θᵢ)
4038

4139
simulation = Simulation(model, Δt=10, stop_iteration=1000) #stop_time=4hours)
4240
conjure_time_step_wizard!(simulation, cfl=0.7)
@@ -70,7 +68,7 @@ function progress(sim)
7068
iteration(sim), prettytime(sim), prettytime(sim.Δt), umax, vmax, wmax)
7169

7270
msg *= @sprintf(", max(qˡ): %.2e, min(δ): %.2e, extrema(θ): (%.2e, %.2e)",
73-
qᵗmin, qᵗmax, qˡmax, δmax, θmin, θmax)
71+
qˡmax, δmax, θmin, θmax)
7472

7573
@info msg
7674

@@ -92,6 +90,7 @@ simulation.output_writers[:jld2] = ow
9290

9391
run!(simulation)
9492

93+
#=
9594
wt = FieldTimeSeries("free_convection.jld2", "θ")
9695
θt = FieldTimeSeries("free_convection.jld2", "θ")
9796
Tt = FieldTimeSeries("free_convection.jld2", "T")
@@ -154,3 +153,4 @@ record(fig, "free_convection.mp4", 1:Nt, framerate=12) do nn
154153
@info "Drawing frame $nn of $Nt..."
155154
n[] = nn
156155
end
156+
=#

src/AtmosphereModels/anelastic_formulation.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using Oceananigans.Operators: Δzᵃᵃᶜ, Δzᵃᵃᶠ, divᶜᶜᶜ, Δzᶜ
1414
using Oceananigans.Solvers: solve!
1515

1616
using KernelAbstractions: @kernel, @index
17+
using Adapt: Adapt, adapt
1718

1819
import Oceananigans.Solvers: tridiagonal_direction, compute_main_diagonal!, compute_lower_diagonal!
1920
import Oceananigans.TimeSteppers: compute_pressure_correction!, make_pressure_correction!
@@ -26,6 +27,9 @@ struct AnelasticFormulation{R}
2627
reference_state :: R
2728
end
2829

30+
Adapt.adapt_structure(to, formulation::AnelasticFormulation) =
31+
AnelasticFormulation(adapt(to, formulation.reference_state))
32+
2933
const AnelasticModel = AtmosphereModel{<:AnelasticFormulation}
3034

3135
function Base.summary(formulation::AnelasticFormulation)

src/MoistAirBuoyancies.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ struct MoistAirBuoyancy{RS, AT} <: AbstractBuoyancyFormulation{Nothing}
4242
thermodynamics :: AT
4343
end
4444

45+
Adapt.adapt_structure(to, mb::MoistAirBuoyancy) =
46+
MoistAirBuoyancy(adapt(to, mb.reference_state),
47+
adapt(to, mb.thermodynamics))
48+
4549
"""
4650
MoistAirBuoyancy(grid;
4751
base_pressure = 101325,

src/Thermodynamics/reference_states.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Oceananigans: Oceananigans, Center, Field, set!, fill_halo_regions!
2+
using Adapt: Adapt, adapt
23

34
#####
45
##### Reference state computations for Boussinesq and Anelastic models
@@ -11,6 +12,12 @@ struct ReferenceState{FT, F}
1112
density :: F
1213
end
1314

15+
Adapt.adapt_structure(to, ref::ReferenceState) =
16+
ReferenceState(adapt(to, ref.base_pressure),
17+
adapt(to, ref.potential_temperature),
18+
adapt(to, ref.pressure),
19+
adapt(to, ref.density))
20+
1421
Base.eltype(::ReferenceState{FT}) where FT = FT
1522

1623
function Base.summary(ref::ReferenceState)

0 commit comments

Comments
 (0)