Skip to content

Commit 5843397

Browse files
add the veros experiment
1 parent 5b03a33 commit 5843397

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

experiments/veros_forced_simulation.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,51 @@ using PythonCall
33
using Oceananigans
44
using Printf
55

6+
#####
7+
##### A Prognostic Python Ocean (Veros) Simulation
8+
#####
9+
10+
# We import the Veros 4 degree ocean simulation setup, which consists of a near-global ocean
11+
# with a uniform resolution of 4 degrees in both latitude and longitude and a latitude range spanning
12+
# from 80S to 80N. The setup is defined in the `veros.setups.global_4deg` module.
13+
14+
# Before importing the setup, we need to ensure that the Veros module is loaded
15+
# and that every output is removed to avoid conflicts.
16+
617
VerosModule = Base.get_extension(ClimaOcean, :ClimaOceanPythonCallExt)
718
VerosModule.remove_outputs(:global_4deg)
819

9-
ocean = VerosModule.veros_ocean_simulation("global_4deg", :GlobalFourDegreeSetup)
20+
# Actually loading and instantiating the Veros setup in the variable `ocean`.
21+
# This setup uses by default a different time-step for tracers and momentum,
22+
# so we set it to the same value (1800 seconds) for both.
23+
24+
ocean = VerosModule.VerosOceanSimulation("global_4deg", :GlobalFourDegreeSetup)
25+
1026
VerosModule.veros_settings_set!(ocean, "dt_tracer", 1800.0)
27+
VerosModule.veros_settings_set!(ocean, "dt_momentum", 1800.0)
28+
29+
#####
30+
##### A Prescribed Atmosphere (JRA55)
31+
#####
1132

1233
atmos = JRA55PrescribedAtmosphere(; backend = JRA55NetCDFBackend(10))
34+
35+
#####
36+
##### An ice-free ocean forced by a prescribed atmosphere
37+
#####
38+
1339
radiation = Radiation()
1440
coupled_model = OceanSeaIceModel(ocean, nothing; atmosphere=atmos, radiation)
1541
simulation = Simulation(coupled_model; Δt = 1800, stop_iteration = 100000)
1642

43+
#####
44+
##### A simple progress callback
45+
#####
46+
47+
# We set up a progress callback that will print the current time, iteration, and maximum velocities
48+
# at every 5 iterations. It also collects the surface velocity fields and the net fluxes
49+
# into the arrays `s`, `tx`, and `ty` for later visualization.
50+
1751
wall_time = Ref(time_ns())
1852

1953
s = []
@@ -51,4 +85,8 @@ end
5185

5286
add_callback!(simulation, progress, IterationInterval(5))
5387

88+
#####
89+
##### Let's go!!
90+
#####
91+
5492
run!(simulation)

ext/ClimaOceanPythonCallExt/veros_ocean_simulation.jl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Base: eltype
1414
"""
1515
install_veros()
1616
17-
Install the Copernicus Marine CLI using CondaPkg.
17+
Install the Veros ocean model Marine CLI using CondaPkg.
1818
Returns a NamedTuple containing package information if successful.
1919
"""
2020
function install_veros()
@@ -70,7 +70,18 @@ function set!(field::CFField2D, pyarray::Py, k=pyconvert(Int, pyarray.shape[2]))
7070
return field
7171
end
7272

73-
function veros_ocean_simulation(setup, setup_name)
73+
"""
74+
VerosOceanSimulation(setup, setup_name::Symbol)
75+
76+
Creates and initializes a preconfigured Veros ocean simulation using the
77+
specified setup module and setup name.
78+
79+
Arguments
80+
==========
81+
- `setup::AbstractString`: The name of the Veros setup module to import (e.g., `"global_4deg"`).
82+
- `setup_name::Symbol`: The name of the setup class or function within the module to instantiate (e.g., `:GlobalFourDegreeSetup`).
83+
"""
84+
function VerosOceanSimulation(setup, setup_name::Symbol)
7485
setups = pyimport("veros.setups." * setup)
7586
setup = @eval $setups.$setup_name()
7687

@@ -80,6 +91,18 @@ function veros_ocean_simulation(setup, setup_name)
8091
return VerosOceanSimulation(setup)
8192
end
8293

94+
"""
95+
surface_grid(ocean::VerosOceanSimulation)
96+
97+
Constructs a `LatitudeLongitudeGrid` representing the surface grid of the given `VerosOceanSimulation` object.
98+
Notes: Veros always uses a LatitudeLongitudeGrid with 2 halos in both the latitude and longitude directions.
99+
Both latitude and longitude can be either stretched or uniform, depending on the setup, and while the meridional
100+
direction (latitude) is always Bounded, the zonal direction (longitude) can be either Periodic or Bounded.
101+
102+
Arguments
103+
==========
104+
- `ocean::VerosOceanSimulation`: The ocean simulation object containing the grid state variables.
105+
"""
83106
function surface_grid(ocean::VerosOceanSimulation)
84107

85108
xf = Array(PyArray(ocean.setup.state.variables.xu))
@@ -109,20 +132,31 @@ function surface_grid(ocean::VerosOceanSimulation)
109132
return LatitudeLongitudeGrid(size=(Nx, Ny), longitude=xf, latitude=yf, topology=(TX, Bounded, Flat), halo=(2, 2))
110133
end
111134

135+
"""
136+
veros_set!(ocean, v, x)
137+
138+
Set the `v` variable in the `ocean` model to the value of `x`.
139+
"""
112140
function veros_set!(ocean::VerosOceanSimulation, v, x)
113-
s = ocean.setup
141+
setup = ocean.setup
114142
pyexec("""
115143
with setup.state.variables.unlock():
116144
setup.state.variables.__setattr__(y, t)
117-
""", Main, (y=v, t=x, setup=s))
145+
""", Main, (y=v, t=x, setup=setup))
118146
end
119147

120-
function veros_settings_set!(ocean::VerosOceanSimulation, v, x)
121-
s = ocean.setup
148+
149+
"""
150+
veros_settings_set!(ocean, v, x)
151+
152+
Set the `s` setting in the `ocean` model to the value of `x`.
153+
"""
154+
function veros_settings_set!(ocean::VerosOceanSimulation, s, x)
155+
setup = ocean.setup
122156
pyexec("""
123157
with setup.state.settings.unlock():
124158
setup.state.settings.__setattr__(y, t)
125-
""", Main, (y=v, t=x, setup=s))
159+
""", Main, (y=s, t=x, setup=setup))
126160
end
127161

128162
function OceanSeaIceModel(ocean::VerosOceanSimulation, sea_ice=nothing;

0 commit comments

Comments
 (0)