Running SWAN¶
rompy-swan writes a SWAN workspace: the command file INPUT and the files it refers to. Running SWAN on it is done by rompy's run backends. LocalConfig runs a command on your machine, and DockerConfig runs SWAN in a container, so no local SWAN build is needed. This page covers both, parallel runs with MPI, running from the command line and checking that a run worked.
The examples use a modelrun built as in Your first model. They need SWAN or Docker, so they are not run when the docs are built.
Generate once, then run¶
SWAN reads the command file INPUT in its working directory, so every backend runs SWAN inside the workspace folder. ModelRun.run(backend, workspace_dir=...) runs it on an existing workspace. Pass the folder returned by generation, so the files are not generated a second time:
Without workspace_dir, run() generates the workspace first. It returns True if the command finished without error, which does not mean SWAN computed anything: see Checking a run.
With your own SWAN installation¶
LocalConfig runs a shell command in the workspace folder. Use it when SWAN is installed on your machine or cluster node, for example from the SWAN website:
from rompy.backends import LocalConfig
backend = LocalConfig(command="swan.exe", timeout=3600)
modelrun.run(backend, workspace_dir=workspace)
env_vars adds environment variables, and stream_output=True prints SWAN's output as it runs. Build SWAN with NetCDF support if the output components write .nc files.
With Docker¶
The public image ghcr.io/rom-py/swan contains SWAN built with NetCDF output, serially and with MPI, and is tested with workspaces generated by rompy-swan before it is published. DockerConfig mounts the workspace into the container and runs executable there:
from rompy.backends import DockerConfig
backend = DockerConfig(image="ghcr.io/rom-py/swan:41.51", executable="swan.exe")
modelrun.run(backend, workspace_dir=workspace)
| Tag | SWAN |
|---|---|
41.51 |
SWAN release 41.51 |
latest |
The newest release |
Pin a version tag so runs stay reproducible; latest moves when a new release is published. The image has three commands:
| Command | What runs |
|---|---|
swan.exe |
The MPI build when started by mpirun with 2 or more processes, the serial build otherwise |
swan-serial |
The serial build |
swan-mpi |
The MPI build |
Use an image with NetCDF
The official image from the SWAN team, delftwaves/swan, is built without NetCDF. SWAN then writes BLOCK and SPECOUT files ending in .nc as text, which xarray cannot open. Use ghcr.io/rom-py/swan, or a local SWAN build with NetCDF.
Output files are owned by root
The container runs as root, so the files SWAN writes into the workspace are owned by root on Linux. Remove or change them with sudo, or run the container yourself with --user (see below).
In parallel with MPI¶
SWAN splits the computational grid between MPI processes. Nothing changes in the configuration: the number of processes is set when SWAN is launched. DockerConfig sets it with cpu and the launcher with mpiexec, and the image's swan.exe then picks its MPI build:
backend = DockerConfig(
image="ghcr.io/rom-py/swan:41.51", executable="swan.exe", mpiexec="mpirun", cpu=4
)
modelrun.run(backend, workspace_dir=workspace)
rompy runs mpirun --allow-run-as-root -n 4 swan.exe in the container, so cpu must not exceed the cores Open MPI finds in the container (usually the physical cores). With your own MPI build of SWAN, use LocalConfig(command="mpirun -n 4 swan.exe").
With MPI, SWAN writes one log per process, PRINT-001, PRINT-002, ..., instead of a single PRINT, and combines the output files. MPI pays off for large grids and long nonstationary runs; for small models, starting the processes costs more than it saves.
From the command line¶
rompy run generates and runs a YAML model configuration (see Configuration) with a backend described in a second YAML file. The backend file holds the fields of the backend class plus its type (local, docker or slurm):
For MPI, add mpiexec: mpirun and cpu: 4 to the backend file. rompy run --dry-run only generates the workspace, --skip-generate runs a workspace generated earlier, and rompy backends create --backend-type docker writes a template backend file.
Docker without rompy¶
The image also runs a generated workspace directly. From inside the workspace folder:
docker run --rm -v "$PWD":/data ghcr.io/rom-py/swan:41.51 swan.exe
docker run --rm -v "$PWD":/data ghcr.io/rom-py/swan:41.51 mpirun -n 4 swan.exe
On Linux, add --user "$(id -u):$(id -g)" to keep your own ownership of the output files. When more processes are asked for than the machine has cores, Open MPI stops with There are not enough slots available; ask for fewer, or add --oversubscribe after mpirun.
Checking a run¶
SWAN writes into the workspace:
| File | Content |
|---|---|
PRINT (PRINT-001, ... with MPI) |
The run log: the commands read, the settings used, warnings, errors and the progress of the computation |
Errfile |
The error messages, when there are any |
norm_end |
Written at the end of the run, with Normal end of run and the run number |
swaninit |
SWAN's initialisation file, created on the first run in a folder |
| Output files | Named by the fname of the output components, e.g. swangrid.nc |
SWAN can stop a computation because of an error and still end normally: the backend reports success, and norm_end is written. For example, a model without wind and with quadruplets on writes this to PRINT and computes nothing:
** Error : It is not recommended to use quadruplets
** Error : in combination with zero wind conditions.
** No start of computation because of error level: 2
** To ignore this error, change [maxerr] with the SET command
So always check PRINT for errors, and that the output files exist:
from pathlib import Path
for print_file in sorted(Path(workspace).glob("PRINT*")):
lines = print_file.read_text().splitlines()
errors = [line for line in lines if "** Error" in line]
warnings = [line for line in lines if "** Warning" in line]
print(f"{print_file.name}: {len(errors)} errors, {len(warnings)} warnings")
Warnings are common and often harmless, for example Limiter is de-activated in case of no quadruplets. In a stationary computation, PRINT also reports the convergence of each iteration (accuracy OK in 100.00 % of wet grid points).
NetCDF map output from a stationary run has a run dimension instead of time:
Troubleshooting lists common problems and their fixes.
See it in the notebooks¶
See it in the notebooks
- 1. Your first SWAN model (tutorial)
- 6. A nonstationary hindcast (tutorial)
- 7. Configuration as YAML and the rompy CLI (tutorial)
- Output (example)
- Running SWAN (example)
- Stationary and nonstationary computations (example)
- Hotstart and chained runs (example)
- Nesting (example)
- Physics sensitivity (example)