Skip to content

Commit 3772b2c

Browse files
fill it in
1 parent 78de681 commit 3772b2c

File tree

7 files changed

+93
-25
lines changed

7 files changed

+93
-25
lines changed

experiments/veros_forced_simulation.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ClimaOcean
22
using PythonCall
33
using Oceananigans
4+
using Printf
45

56
VerosModule = Base.get_extension(ClimaOcean, :ClimaOceanPythonCallExt)
67
VerosModule.remove_outputs(:global_4deg)
@@ -11,4 +12,29 @@ VerosModule.veros_settings_set!(ocean, "dt_tracer", 1800.0)
1112
atmos = JRA55PrescribedAtmosphere(; backend = JRA55NetCDFBackend(10))
1213
radiation = Radiation()
1314
coupled_model = OceanSeaIceModel(ocean, nothing; atmosphere=atmos, radiation)
15+
simulation = Simulation(coupled_model; Δt = 1800, stop_iteration = 100)
1416

17+
wall_time = Ref(time_ns())
18+
19+
function progress(sim)
20+
ocean = sim.model.ocean
21+
umax = maximum(PyArray(ocean.setup.state.variables.u))
22+
vmax = maximum(PyArray(ocean.setup.state.variables.v))
23+
wmax = maximum(PyArray(ocean.setup.state.variables.w))
24+
25+
step_time = 1e-9 * (time_ns() - wall_time[])
26+
27+
msg1 = @sprintf("time: %s, iteration: %d, Δt: %s, ", prettytime(sim), iteration(sim), prettytime(sim.Δt))
28+
msg5 = @sprintf("maximum(u): (%.2f, %.2f, %.2f) m/s, ", umax, vmax, wmax)
29+
msg6 = @sprintf("wall time: %s \n", prettytime(step_time))
30+
31+
@info msg1 * msg5 * msg6
32+
33+
wall_time[] = time_ns()
34+
35+
return nothing
36+
end
37+
38+
add_callback!(simulation, progress, IterationInterval(10))
39+
40+
run!(simulation)

ext/ClimaOceanPythonCallExt/ClimaOceanPythonCallExt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ using Dates: DateTime
1010

1111
include("clima_ocean_copernicus.jl")
1212
include("clima_ocean_veros.jl")
13+
include("veros_state_exchanger.jl")
1314

1415
end # module ClimaOceanPythonCallExt

ext/ClimaOceanPythonCallExt/clima_ocean_veros.jl

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ using CondaPkg
33
using ClimaOcean.OceanSeaIceModels: reference_density, heat_capacity, SeaIceSimulation
44

55
import Oceananigans.Fields: set!
6-
import Oceananigans.TimeSteppers: time_step!
6+
import Oceananigans.TimeSteppers: time_step!, initialize!
77

8-
import ClimaOcean.OceanSeaIceModels: OceanSeaIceModel
8+
import ClimaOcean.OceanSeaIceModels: OceanSeaIceModel, default_nan_checker
9+
import Oceananigans.Architectures: architecture
10+
11+
import Base: eltype
912

1013
"""
1114
install_veros()
@@ -24,7 +27,12 @@ struct VerosOceanSimulation{S}
2427
setup :: S
2528
end
2629

27-
time_step!(sim::VerosOceanSimulation, Δt) = sim.setup.step()
30+
default_nan_checker(model::OceanSeaIceModel{<:Any, <:Any, <:VerosOceanSimulation}) = nothing
31+
32+
initialize!(::ClimaOceanPythonCallExt.VerosOceanSimulation{Py}) = nothing
33+
time_step!(ocean::VerosOceanSimulation, Δt) = ocean.setup.step(ocean.setup.state)
34+
architecture(model::OceanSeaIceModel{<:Any, <:Any, <:VerosOceanSimulation}) = CPU()
35+
eltype(model::OceanSeaIceModel{<:Any, <:Any, <:VerosOceanSimulation}) = Float64
2836

2937
function remove_outputs(setup::Symbol)
3038
rm("$(setup).averages.nc", force=true)
@@ -34,15 +42,31 @@ function remove_outputs(setup::Symbol)
3442
return nothing
3543
end
3644

37-
const Field2D = Field{<:Any, <:Any, <:Nothing}
45+
const CCField2D = Field{<:Center, <:Center, <:Nothing}
46+
const FCField2D = Field{<:Face, <:Center, <:Nothing}
47+
const CFField2D = Field{<:Center, <:Face, <:Nothing}
3848

39-
function set!(field::Field2D, pyarray::Py, k=pyconvert(Int, pyarray.shape[2]))
49+
function set!(field::CCField2D, pyarray::Py, k=pyconvert(Int, pyarray.shape[2]))
4050
array = PyArray(pyarray)
4151
Nx, Ny, Nz = size(array)
4252
set!(field, view(array, 3:Nx-2, 3:Ny-2, k, 1))
4353
return field
4454
end
4555

56+
function set!(field::FCField2D, pyarray::Py, k=pyconvert(Int, pyarray.shape[2]))
57+
array = PyArray(pyarray)
58+
Nx, Ny, Nz = size(array)
59+
set!(field, view(array, 3:Nx-2, 3:Ny-2, k, 1))
60+
return field
61+
end
62+
63+
function set!(field::CFField2D, pyarray::Py, k=pyconvert(Int, pyarray.shape[2]))
64+
array = PyArray(pyarray)
65+
Nx, Ny, Nz = size(array)
66+
set!(field, view(array, 3:Nx-2, 2:Ny-2, k, 1))
67+
return field
68+
end
69+
4670
function veros_ocean_simulation(setup, setup_name)
4771
setups = pyimport("veros.setups." * setup)
4872
setup = @eval $setups.$setup_name()
@@ -53,13 +77,13 @@ function veros_ocean_simulation(setup, setup_name)
5377
return VerosOceanSimulation(setup)
5478
end
5579

56-
function surface_grid(setup::VerosOceanSimulation)
80+
function surface_grid(ocean::VerosOceanSimulation)
5781

58-
xf = Array(PyArray(setup.state.variables.xu))
59-
yf = Array(PyArray(setup.state.variables.yu))
82+
xf = Array(PyArray(ocean.setup.state.variables.xu))
83+
yf = Array(PyArray(ocean.setup.state.variables.yu))
6084

61-
xc = Array(PyArray(setup.state.variables.xt))
62-
yc = Array(PyArray(setup.state.variables.yt))
85+
xc = Array(PyArray(ocean.setup.state.variables.xt))
86+
yc = Array(PyArray(ocean.setup.state.variables.yt))
6387

6488
xf = xf[2:end-2]
6589
yf = yf[2:end-2]
@@ -79,7 +103,7 @@ function surface_grid(setup::VerosOceanSimulation)
79103
Nx = length(xc)
80104
Ny = length(yc)
81105

82-
return LatitudeLongitudeGrid(size=(Nx, Ny), longitude=xf, latitude=yf, topology=(TX, Bounded, Flat))
106+
return LatitudeLongitudeGrid(size=(Nx, Ny), longitude=xf, latitude=yf, topology=(TX, Bounded, Flat), halo=(2, 2))
83107
end
84108

85109
function veros_set!(ocean::VerosOceanSimulation, v, x)

ext/ClimaOceanPythonCallExt/veros_state_exchanger.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
using Oceananigans.Models: initialization_update_state!
2+
3+
using ClimaOcean.OceanSeaIceModels.InterfaceComputations: ExchangeAtmosphereState,
4+
atmosphere_exchanger,
5+
SimilarityTheoryFluxes,
6+
Radiation
7+
18
import ClimaOcean.OceanSeaIceModels.InterfaceComputations:
29
state_exchanger,
310
sea_ice_ocean_interface,
@@ -8,7 +15,6 @@ import ClimaOcean.OceanSeaIceModels.InterfaceComputations:
815
get_radiative_forcing,
916
fill_up_net_fluxes!
1017

11-
1218
mutable struct VerosStateExchanger{G, OST, AST, AEX}
1319
exchange_grid :: G
1420
exchange_ocean_state :: OST
@@ -33,12 +39,12 @@ function state_exchanger(ocean::VerosOceanSimulation, atmosphere)
3339
exchange_ocean_state = ExchangeOceanState(exchange_grid)
3440
exchange_atmosphere_state = ExchangeAtmosphereState(exchange_grid)
3541

36-
atmosphere_exchanger = AtmosphereExchanger(atmosphere, exchange_grid)
42+
atmos_exchanger = atmosphere_exchanger(atmosphere, exchange_grid)
3743

3844
return VerosStateExchanger(exchange_grid,
3945
exchange_ocean_state,
4046
exchange_atmosphere_state,
41-
atmosphere_exchanger)
47+
atmos_exchanger)
4248
end
4349

4450
atmosphere_ocean_interface(ocean::VerosOceanSimulation, args...) =
@@ -77,8 +83,19 @@ end
7783
@inline get_radiative_forcing(ocean::VerosOceanSimulation) = nothing
7884

7985
function fill_up_net_fluxes!(ocean::VerosOceanSimulation, net_ocean_fluxes)
80-
veros_set!(ocean, "surface_taux", net_ocean_fluxes.u)
81-
veros_set!(ocean, "surface_tauy", net_ocean_fluxes.v)
82-
86+
nx = pyconvert(Int, ocean.state.settings.nx)
87+
ny = pyconvert(Int, ocean.state.settings.ny)
88+
t1 = parent(net_ocean_fluxes.u)[:, 1:44, 1]
89+
t2 = parent(net_ocean_fluxes.v)[:, 1:44, 1]
90+
91+
ta = zeros(size(t1)..., 12)
92+
tb = zeros(size(t2)..., 12)
93+
for t in 1:12
94+
ta[:, :, t] .= t1
95+
tb[:, :, t] .= t2
96+
end
97+
98+
veros_set!(ocean, "taux", ta)
99+
veros_set!(ocean, "tauy", tb)
83100
return nothing
84101
end

src/OceanSeaIceModels/InterfaceComputations/assemble_net_fluxes.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fill_up_net_fluxes!(ocean, net_ocean_fluxes) = nothing
3636

3737
function compute_net_ocean_fluxes!(coupled_model)
3838
sea_ice = coupled_model.sea_ice
39-
grid = coupled_model.exchanger.exchange_grid
39+
grid = coupled_model.interfaces.exchanger.exchange_grid
4040
arch = architecture(grid)
4141
clock = coupled_model.clock
4242

@@ -60,13 +60,13 @@ function compute_net_ocean_fluxes!(coupled_model)
6060
freshwater_flux = atmosphere_fields.Mp.data
6161

6262
ice_concentration = sea_ice_concentration(sea_ice)
63-
ocean_salinity = ocean.model.tracers.S
63+
ocean_salinity = get_ocean_state(coupled_model.ocean, coupled_model.interfaces.exchanger).S
6464
atmos_ocean_properties = coupled_model.interfaces.atmosphere_ocean_interface.properties
6565
ocean_properties = coupled_model.interfaces.ocean_properties
6666
kernel_parameters = interface_kernel_parameters(grid)
6767

6868
ocean_surface_temperature = coupled_model.interfaces.atmosphere_ocean_interface.temperature
69-
penetrating_radiation = get_radiative_forcing(ocean)
69+
penetrating_radiation = get_radiative_forcing(coupled_model.ocean)
7070

7171
launch!(arch, grid, kernel_parameters,
7272
_assemble_net_ocean_fluxes!,
@@ -84,7 +84,7 @@ function compute_net_ocean_fluxes!(coupled_model)
8484
atmos_ocean_properties,
8585
ocean_properties)
8686

87-
fill_up_net_fluxes!(ocean, net_ocean_fluxes)
87+
fill_up_net_fluxes!(coupled_model.ocean, net_ocean_fluxes)
8888

8989
return nothing
9090
end

src/OceanSeaIceModels/InterfaceComputations/interpolate_atmospheric_state.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function interpolate_atmosphere_state!(interfaces, atmosphere::PrescribedAtmosph
2626
atmosphere_grid = atmosphere.grid
2727

2828
# Basic model properties
29-
grid = ocean.model.grid
29+
grid = coupled_model.interfaces.exchanger.exchange_grid
3030
arch = architecture(grid)
3131
clock = coupled_model.clock
3232

@@ -125,8 +125,8 @@ function interpolate_atmosphere_state!(interfaces, atmosphere::PrescribedAtmosph
125125
#
126126
# TODO: find a better design for this that doesn't have redundant
127127
# arrays for the barotropic potential
128-
u_potential = forcing_barotropic_potential(ocean.model.forcing.u)
129-
v_potential = forcing_barotropic_potential(ocean.model.forcing.v)
128+
u_potential = forcing_barotropic_potential(ocean)
129+
v_potential = forcing_barotropic_potential(ocean)
130130
ρₒ = coupled_model.interfaces.ocean_properties.reference_density
131131

132132
if !isnothing(u_potential)

src/OceanSeaIceModels/ocean_sea_ice_model.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Base.show(io::IO, cm::OSIM)
4545
end
4646

4747
print(io, summary(cm), "\n")
48-
print(io, "├── ocean: ", summary(cm.ocean.model), "\n")
48+
print(io, "├── ocean: ", summary(cm.ocean), "\n")
4949
print(io, "├── atmosphere: ", summary(cm.atmosphere), "\n")
5050
print(io, "├── sea_ice: ", sea_ice_summary, "\n")
5151
print(io, "└── interfaces: ", summary(cm.interfaces))

0 commit comments

Comments
 (0)