Skip to content

Lockup

COMPUTE, HOTFILE and STOP: the computations and the end of the command file.

SWAN Lockup Components

This module contains components for controlling SWAN model execution flow, including computation, hotfile output, and program termination.

logger module-attribute

logger = get_logger(__name__)

TIMES_TYPE module-attribute

HOTTIMES_TYPE module-attribute

HOTTIMES_TYPE = Union[list[datetime], list[int]]

HOTFILE pydantic-model

HOTFILE(**data: Any)

Bases: BaseComponent

Write intermediate results.

HOTFILE 'fname' ->FREE|UNFORMATTED

This command can be used to write the entire wave field at the end of a computation to a so-called hotfile, to be used as initial condition in a subsequent SWAN run (see command INITIAL HOTSTART). This command must be entered immediately after a COMPUTE command.

The user may choose the format of the hotfile to be written either as free or unformatted. If the free format is chosen, then this format is identical to the format of the files written by the SPECOUT command (option SPEC2D). This hotfile is therefore an ASCII file which is human readable.

An unformatted (or binary) file usually requires less space on your computer than an ASCII file. Moreover, it can be readed by a subsequent SWAN run much faster than an ASCII file. Especially, when the hotfile might become a big file, the choice for unformatted is preferable. Note that your compiler and OS should follow the same ABI (Application Binary Interface) conventions (e.g. word size, endianness), so that unformatted hotfiles may transfer properly between different OS or platforms. This implies that the present and subsequent SWAN runs do not have to be carried out on the same operating system (e.g. Windows, Linux) or on the same computer, provided that distinct ABI conventions have been followed.

Note

For parallel MPI runs, more than one hotfile will be generated depending on the number of processors (fname-001, fname-002, etc).

Examples:

from rompy_swan.components.lockup import HOTFILE
hotfile = HOTFILE(fname="hotfile.swn")
print(hotfile.render())
hotfile = HOTFILE(fname="hotfile.dat", format="unformatted")
print(hotfile.render())
HOTFILE fname='hotfile.swn'
HOTFILE fname='hotfile.dat' UNFORMATTED

Fields:

Validators:

model_type pydantic-field

model_type: Literal['hotfile', 'HOTFILE'] = 'hotfile'

Model type discriminator

fname pydantic-field

fname: Path

Name of the file to which the wave field is written

format pydantic-field

format: Optional[Literal['free', 'unformatted']] = None

Choose between free (SWAN ASCII) or unformatted (binary) format

max_length pydantic-validator

max_length(fname: Path) -> Path

cmd

cmd() -> str

Command file string for this component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

COMPUTE pydantic-model

COMPUTE(**data: Any)

Bases: BaseComponent

Single SWAN computation in stationary mode.

COMPUTE
HOTFILE 'fname' ->FREE|UNFORMATTED

If the SWAN mode is stationary (see command MODE, stationary is SWAN's default), then SWAN makes a single computation, started by a COMPUTE command without times. The wave field at the end of the computation can be written to a hotfile.

If the SWAN mode is nonstationary, computations are made at given times, either stationary (COMPUTE_STAT) or nonstationary (COMPUTE_NONSTAT).

To verify input to SWAN (e.g., all input fields such as water depth, wind fields, etc), SWAN can be run without computations (that is: zero iterations by using command NUM ACCUR MXITST=0).

For small domains, i.e. less than 100 km or 1 deg, a stationary computation is recommended. Otherwise, a nonstationary computation is advised.

Examples

from rompy_swan.components.lockup import COMPUTE
comp = COMPUTE()
print(comp.render())
comp = COMPUTE(hotfile=dict(fname="hotfile.swn"))
print(comp.render())
COMPUTE
COMPUTE
HOTFILE fname='hotfile.swn'

Fields:

model_type pydantic-field

model_type: Literal['compute', 'COMPUTE'] = 'compute'

Model type discriminator

hotfile pydantic-field

hotfile: Optional[HOTFILE] = None

Write the wave field at the end of the computation to a hotfile

cmd

cmd() -> list

Command file strings for this component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

COMPUTE_STAT pydantic-model

COMPUTE_STAT(**data: Any)

Bases: BaseComponent

Multiple SWAN stationary computations.

COMPUTE STATIONARY [time]
HOTFILE 'fname' ->FREE|UNFORMATTED
COMPUTE STATIONARY [time]
COMPUTE STATIONARY [time]
HOTFILE 'fname' ->FREE|UNFORMATTED
.
.

This component can be used to define multiple stationary compute commands and write intermediate results as hotfiles between then.

Note

The field times is optional to allow for the case where the user wants to set times dynamically after instantiating this component.

Examples:

from rompy_swan.subcomponents.time import STATIONARY, NONSTATIONARY
from rompy_swan.components.lockup import COMPUTE_STAT
time = STATIONARY(time="1990-01-01T00:00:00")
comp = COMPUTE_STAT(times=time)
print(comp.render())
times = NONSTATIONARY(
    tbeg="1990-01-01T00:00:00",
    tend="1990-01-01T03:00:00",
    delt="PT1H",
)
comp = COMPUTE_STAT(times=times)
print(comp.render())
hotfile = dict(fname="./hotfile.swn")
hottimes=["1990-01-01T03:00:00"]
comp = COMPUTE_STAT(times=times, hotfile=hotfile, hottimes=hottimes)
print(comp.render())
comp = COMPUTE_STAT(times=times, hotfile=hotfile, hottimes=[2, -1])
print(comp.render())
COMPUTE STATIONARY time=19900101.000000
COMPUTE STATIONARY time=19900101.000000
COMPUTE STATIONARY time=19900101.010000
COMPUTE STATIONARY time=19900101.020000
COMPUTE STATIONARY time=19900101.030000
COMPUTE STATIONARY time=19900101.000000
COMPUTE STATIONARY time=19900101.010000
COMPUTE STATIONARY time=19900101.020000
COMPUTE STATIONARY time=19900101.030000
HOTFILE fname='hotfile_19900101T030000.swn'
COMPUTE STATIONARY time=19900101.000000
COMPUTE STATIONARY time=19900101.010000
COMPUTE STATIONARY time=19900101.020000
HOTFILE fname='hotfile_19900101T020000.swn'
COMPUTE STATIONARY time=19900101.030000
HOTFILE fname='hotfile_19900101T030000.swn'

Fields:

Validators:

model_type pydantic-field

model_type: Literal['stat', 'STAT'] = 'stat'

Model type discriminator

times pydantic-field

times: TIMES_TYPE

Compute times

hotfile pydantic-field

hotfile: Optional[HOTFILE] = None

Write results to restart files

hottimes pydantic-field

hottimes: HOTTIMES_TYPE = []

Times to write hotfiles, can be a list of datetimes or times indices

suffix pydantic-field

suffix: str = '_%Y%m%dT%H%M%S'

Time-template suffix to add to hotfile fname

hotids property

hotids: list

List time ids at which to write hotfiles.

timestamp_to_datetime pydantic-validator

timestamp_to_datetime(hottimes: TIMES_TYPE) -> TIMES_TYPE

Ensure pandas.Timestamp entries are coerced into datatime.

hotfile_with_hottimes pydantic-validator

hotfile_with_hottimes() -> COMPUTE_NONSTAT

cmd

cmd() -> list

Command file string for this component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

COMPUTE_NONSTAT pydantic-model

COMPUTE_NONSTAT(**data: Any)

Bases: COMPUTE_STAT

Multiple SWAN nonstationary computations.

COMPUTE NONSTATIONARY [tbegc] [deltc] SEC|MIN|HR|DAY [tendc]
HOTFILE 'fname' ->FREE|UNFORMATTED
COMPUTE NONSTATIONARY [tbegc] [deltc] SEC|MIN|HR|DAY [tendc]
HOTFILE 'fname' ->FREE|UNFORMATTED
.
.

This component can be used to define multiple nonstationary compute commands and write intermediate results as hotfiles between then.

In the case MODE NONSTATIONARY several commands COMPUTE can appear, where the wave state at the end of one computation is used as initial state for the next one, unless a command INIT appears in between the two COMPUTE commands. This enables the user to make a stationary computation to obtain the initial state for a nonstationary computation and/or to change the computational time step during a computation, to change a boundary condition etc. This also has the advantage of not using a hotfile since, it can be very large in size.

For a nonstationary computation, a time step of at most 10 minutes is advised (when you are choosing a time step larger than 10 minutes, the action density limiter (see command NUM) becomes probably a part of the physics).

Also, the time step should be chosen such that the Courant number is smaller than 10 for the fastest (or dominant) wave. Otherwise, a first order upwind scheme is recommended in that case; see command PROP BSBT. If you want to run a high resolution model with a very large time step, e.g. 1 hour, you may apply multiple COMPUT STAT commands. For a small time step (<= 10 minutes), no more than 1 iteration per time step is recommended (see command NUM ... NONSTAT mxitns).

Note

The field times is optional to allow for the case where the user wants to set times dynamically after instantiating this component.

Examples:

from rompy_swan.subcomponents.time import NONSTATIONARY
from rompy_swan.components.lockup import COMPUTE_NONSTAT
times = NONSTATIONARY(
    tbeg="1990-01-01T00:00:00",
    tend="1990-02-01T00:00:00",
    delt="PT1H",
    dfmt="hr",
)
comp = COMPUTE_NONSTAT(times=times)
print(comp.render())
comp = COMPUTE_NONSTAT(
    times=times,
    hotfile=dict(fname="hotfile.swn", format="free"),
    hottimes=["1990-02-01T00:00:00"],
)
print(comp.render())
comp = COMPUTE_NONSTAT(
    times=times,
    initstat=True,
    hotfile=dict(fname="hotfile", format="free"),
    hottimes=[6, 12, 18, -1],
)
print(comp.render())
COMPUTE NONSTATIONARY tbegc=19900101.000000 deltc=1.0 HR tendc=19900201.000000
COMPUTE NONSTATIONARY tbegc=19900101.000000 deltc=1.0 HR tendc=19900201.000000
HOTFILE fname='hotfile_19900201T000000.swn' FREE
COMPUTE STATIONARY time=19900101.000000
COMPUTE NONSTATIONARY tbegc=19900101.000000 deltc=1.0 HR tendc=19900101.060000
HOTFILE fname='hotfile_19900101T060000' FREE
COMPUTE NONSTATIONARY tbegc=19900101.060000 deltc=1.0 HR tendc=19900101.120000
HOTFILE fname='hotfile_19900101T120000' FREE
COMPUTE NONSTATIONARY tbegc=19900101.120000 deltc=1.0 HR tendc=19900101.180000
HOTFILE fname='hotfile_19900101T180000' FREE
COMPUTE NONSTATIONARY tbegc=19900101.180000 deltc=1.0 HR tendc=19900201.000000
HOTFILE fname='hotfile_19900201T000000' FREE

Fields:

Validators:

model_type pydantic-field

model_type: Literal['nonstat', 'NONSTAT'] = 'nonstat'

Model type discriminator

times pydantic-field

Compute times

initstat pydantic-field

initstat: bool = False

Run a STATIONARY computation at the initial time prior to the NONSTATIONARY computation(s) to prescribe initial conditions

hotfile pydantic-field

hotfile: Optional[HOTFILE] = None

Write results to restart files

hottimes pydantic-field

hottimes: HOTTIMES_TYPE = []

Times to write hotfiles, can be a list of datetimes or times indices

suffix pydantic-field

suffix: str = '_%Y%m%dT%H%M%S'

Time-template suffix to add to hotfile fname

hotids property

hotids: list

List time ids at which to write hotfiles.

times_suffix pydantic-validator

times_suffix(times: NONSTATIONARY) -> NONSTATIONARY

cmd

cmd() -> list

Command file string for this component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

timestamp_to_datetime pydantic-validator

timestamp_to_datetime(hottimes: TIMES_TYPE) -> TIMES_TYPE

Ensure pandas.Timestamp entries are coerced into datatime.

hotfile_with_hottimes pydantic-validator

hotfile_with_hottimes() -> COMPUTE_NONSTAT

STOP pydantic-model

STOP(**data: Any)

Bases: BaseComponent

End of commands.

STOP

This required command marks the end of the commands in the command file. Note that the command STOP may be the last command in the input file; any information in the input file beyond this command is ignored.

Examples

from rompy_swan.components.lockup import STOP
stop = STOP()
print(stop.render())
STOP

Fields:

model_type pydantic-field

model_type: Literal['stop', 'STOP'] = 'stop'

Model type discriminator

cmd

cmd() -> str

Command file string for this component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.