Your first model¶
This page builds a small stationary SWAN model of swell arriving off south-western Australia, writes its workspace and shows how to run it. Every step on this page is executed when the docs are built, so the output shown is what the code produces. The model uses the coarse test data in the repository's tests/data folder. Tutorial lesson 1 builds a similar model at higher resolution off Perth, as a notebook with plots of the results.
1. Run period¶
TimeRange comes from rompy and defines when the model runs. This first model is stationary: SWAN computes the wave field for a single moment, so the start and end are the same.
from rompy.core.time import TimeRange
period = TimeRange(start="2023-01-01T00:00", end="2023-01-01T00:00", interval="1h")
2. Computational grid¶
A SwanGrid describes a regular grid by its lower-left corner, spacing and number of points, here in longitude and latitude. The computational grid component, REGULAR, adds the spectral grid: 36 directions and frequencies from 0.04 to 1 Hz. It renders the SWAN CGRID command:
from rompy_swan.components.cgrid import REGULAR
from rompy_swan.grid import SwanGrid
from rompy_swan.subcomponents.spectrum import SPECTRUM
grid = SwanGrid(x0=110.0, y0=-36.0, dx=0.5, dy=0.5, nx=13, ny=15)
cgrid = REGULAR(grid=grid.component, spectrum=SPECTRUM(mdc=36, flow=0.04, fhigh=1.0))
print(cgrid.render())
CGRID REGULAR xpc=110.0 ypc=-36.0 alpc=0.0 xlenc=6.0 ylenc=7.0 mxc=12 myc=14 CIRCLE mdc=36 flow=0.04 fhigh=1.0
3. Bathymetry¶
A SwanDataGrid turns a dataset into a SWAN input grid. Here it reads the elevation from a GEBCO file. SWAN expects depths positive down, so fac=-1 flips the sign, and the data is cropped to the grid with a buffer. The DataInterface writes the input grid files and their INPGRID and READINP commands when the workspace is generated.
from rompy.core.source import SourceFile
from rompy_swan.data import SwanDataGrid
from rompy_swan.interface import DataInterface
bottom = SwanDataGrid(
var="bottom",
source=SourceFile(uri="tests/data/gebco-1deg.nc"),
z1="elevation",
fac=-1.0,
coords={"x": "lon", "y": "lat"},
buffer=1.0,
)
inpgrid = DataInterface(bottom=bottom)
4. Wave boundary¶
Swell enters through the western side of the grid. BOUNDSPEC applies a JONSWAP spectrum with a significant wave height of 2 m, a peak period of 12 s, waves coming from 240° and a directional spreading of 20°:
from rompy_swan.components.boundary import BOUNDSPEC
from rompy_swan.subcomponents.boundary import CONSTANTPAR, SIDE
from rompy_swan.subcomponents.spectrum import JONSWAP, SHAPESPEC
boundary = BOUNDSPEC(
shapespec=SHAPESPEC(shape=JONSWAP(gamma=3.3), per_type="peak", dspr_type="degrees"),
location=SIDE(side="west", direction="ccw"),
data=CONSTANTPAR(hs=2.0, per=12.0, dir=240.0, dd=20.0),
)
print(boundary.render())
BOUND SHAPESPEC JONSWAP gamma=3.3 PEAK DSPR DEGREES
BOUNDSPEC SIDE WEST CCW CONSTANT PAR hs=2.0 per=12.0 dir=240.0 dd=20.0
5. Startup, physics, output and computation¶
- Startup:
SET NAUTICALmakes directions mean coming from, clockwise from north, and the coordinates are spherical. - Physics:
GEN3is SWAN's third-generation physics. There is no wind in this model, so the quadruplet interactions are switched off, as SWAN requires. - Output: a
BLOCKwrites maps on the computational grid (COMPGRID) to a NetCDF file. - Lockup:
COMPUTEruns the single computation of stationary mode, SWAN's default.
Commands that SWAN groups together are given as a group, such as STARTUP or PHYSICS:
from rompy_swan.components.group import LOCKUP, OUTPUT, PHYSICS, STARTUP
from rompy_swan.components.lockup import COMPUTE
from rompy_swan.components.output import BLOCK
from rompy_swan.components.physics import GEN3, OFF, OFFS
from rompy_swan.components.startup import COORDINATES, PROJECT, SET
from rompy_swan.subcomponents.startup import SPHERICAL
startup = STARTUP(
project=PROJECT(name="First model", nr="t01"),
set=SET(direction_convention="nautical"),
coordinates=COORDINATES(kind=SPHERICAL()),
)
physics = PHYSICS(gen=GEN3(), deactivate=OFFS(offs=[OFF(physics="quadrupl")]))
output = OUTPUT(
block=BLOCK(sname="COMPGRID", fname="swangrid.nc", output=["depth", "hsign", "dir", "tps"])
)
lockup = LOCKUP(compute=COMPUTE())
6. The model configuration¶
SwanConfig gathers the pieces. Only cgrid is required; everything else is added as needed.
from rompy_swan.config import SwanConfig
config = SwanConfig(
startup=startup,
cgrid=cgrid,
inpgrid=inpgrid,
boundary=boundary,
physics=physics,
output=output,
lockup=lockup,
)
7. Generate the workspace¶
rompy's ModelRun combines the configuration with the run period and an output directory. Calling it writes the workspace into output_dir/run_id: the bathymetry cropped to the grid (bottom.grd) and the command file INPUT.
from pathlib import Path
from rompy.model import ModelRun
modelrun = ModelRun(run_id="first_model", period=period, output_dir=OUT_DIR, config=config)
workspace = Path(modelrun())
print(sorted(p.name for p in workspace.iterdir()))
Each object above wrote its commands to INPUT, in the order SWAN expects (comment lines starting with ! are left out here):
input_file = (workspace / "INPUT").read_text()
print("\n".join(line for line in input_file.splitlines() if line and line[0] != "!"))
PROJECT name='First model' nr='t01'
SET NAUTICAL
COORDINATES SPHERICAL CCM
CGRID REGULAR xpc=110.0 ypc=-36.0 alpc=0.0 xlenc=6.0 ylenc=7.0 mxc=12 myc=14 CIRCLE mdc=36 flow=0.04 fhigh=1.0
INPGRID BOTTOM REG 109.0 -37.0 0.0 8 9 1.0 1.0 EXC 99.0
READINP BOTTOM -1.0 'bottom.grd' 3 FREE
BOUND SHAPESPEC JONSWAP gamma=3.3 PEAK DSPR DEGREES
BOUNDSPEC SIDE WEST CCW CONSTANT PAR hs=2.0 per=12.0 dir=240.0 dd=20.0
GEN3 WESTHUYSEN DRAG WU
OFF QUADRUPL
BLOCK sname='COMPGRID' fname='swangrid.nc' &
DEPTH &
HSIGN &
DIR &
TPS
COMPUTE
STOP
8. Run SWAN¶
The easiest way to run SWAN is the public Docker image, through rompy's DockerConfig backend:
from rompy.backends import DockerConfig
backend = DockerConfig(image="ghcr.io/rom-py/swan:41.51", executable="swan.exe")
modelrun.run(backend, workspace_dir=workspace)
SWAN writes a log of the run to PRINT and the maps to swangrid.nc in the workspace. With your own SWAN installation, run swan.exe from inside the workspace folder instead. Running SWAN covers backends, MPI and checking a run.
Next steps¶
- The tutorial goes through each part of the model in turn, on a realistic model off Perth.
- How rompy-swan works explains how these objects become the command file.
- The User guide covers each part of the model in detail.