{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Model components\n", "\n", "- SWAN config components to fully prescribe the INPUT command file\n", "- Components are modular subclasses of `rompy.swan.components.base.BaseComponent`\n", "- Arguments to each component are pydantic types or SWAN subcomponents\n", "- The `model_type` attribute defines unique values to help parsing components from configs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import yaml\n", "from pathlib import Path\n", "\n", "from rompy.model import ModelRun\n", "from rompy.swan.config import SwanConfigComponents" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mInit signature:\u001b[0m\n", "\u001b[0mSwanConfigComponents\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmodel_type\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mLiteral\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'swan'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'swan'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtemplate\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'/source/csiro/rompy/rompy/templates/swan2'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcheckout\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'main'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mproject\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPROJECT\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSET\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMODE\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcoordinates\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartup\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOORDINATES\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcgrid\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcgrid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mREGULAR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcgrid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCURVILINEAR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcgrid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUNSTRUCTURED\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minpgrid\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minpgrid\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mINPGRIDS\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mboundary\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mboundary\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mBOUNDSPEC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mboundary\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mBOUNDNEST1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mboundary\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mBOUNDNEST2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mboundary\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mBOUNDNEST3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mboundary\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mINITIAL\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mphysics\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mrompy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mswan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcomponents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mphysics\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPHYSICS\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mextra_data\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m SWAN config class.\n", "\u001b[0;31mInit docstring:\u001b[0m\n", "Create a new model by parsing and validating input data from keyword arguments.\n", "\n", "Raises ValidationError if the input data cannot be parsed to form a valid model.\n", "\u001b[0;31mFile:\u001b[0m /source/csiro/rompy/rompy/swan/config.py\n", "\u001b[0;31mType:\u001b[0m ModelMetaclass\n", "\u001b[0;31mSubclasses:\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "SwanConfigComponents?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Yaml config example" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'project': {'model_type': 'project',\n", " 'name': 'Test rompy setup',\n", " 'nr': '0001'},\n", " 'set': {'model_type': 'set',\n", " 'level': 0.0,\n", " 'depmin': 0.05,\n", " 'maxerr': 1,\n", " 'grav': 9.81,\n", " 'rho': 1025.0,\n", " 'cdcap': 0.0025,\n", " 'direction_convention': 'nautical'},\n", " 'mode': {'model_type': 'mode',\n", " 'kind': 'nonstationary',\n", " 'dim': 'twodimensional'},\n", " 'coordinates': {'model_type': 'coordinates',\n", " 'kind': {'model_type': 'spherical'}},\n", " 'cgrid': {'model_type': 'regular',\n", " 'spectrum': {'model_type': 'spectrum',\n", " 'mdc': 36,\n", " 'flow': 0.04,\n", " 'fhigh': 0.4},\n", " 'xlenc': 100.0,\n", " 'ylenc': 100.0,\n", " 'mxc': 10,\n", " 'myc': 10},\n", " 'inpgrid': {'inpgrids': [{'model_type': 'regular',\n", " 'grid_type': 'bottom',\n", " 'xpinp': 0.0,\n", " 'ypinp': 0.0,\n", " 'alpinp': 0.0,\n", " 'mxinp': 10,\n", " 'myinp': 10,\n", " 'dxinp': 0.1,\n", " 'dyinp': 0.1,\n", " 'excval': -999.0,\n", " 'readinp': {'model_type': 'readinp', 'fname1': 'bottom.txt'}},\n", " {'model_type': 'regular',\n", " 'grid_type': 'wind',\n", " 'xpinp': 0.0,\n", " 'ypinp': 0.0,\n", " 'alpinp': 0.0,\n", " 'mxinp': 10,\n", " 'myinp': 10,\n", " 'dxinp': 0.1,\n", " 'dyinp': 0.1,\n", " 'excval': -999.0,\n", " 'readinp': {'model_type': 'readinp', 'fname1': 'wind.txt'},\n", " 'nonstationary': {'tbeg': '2023-01-01T00:00:00',\n", " 'delt': 'PT30M',\n", " 'tend': '2023-02-01T00:00:00',\n", " 'deltfmt': 'hr'}}]},\n", " 'boundary': {'model_type': 'boundspec',\n", " 'shapespec': {'model_type': 'shapespec',\n", " 'shape': {'model_type': 'tma', 'd': 12.0}},\n", " 'location': {'model_type': 'side', 'side': 'west'},\n", " 'data': {'model_type': 'constantpar',\n", " 'hs': 1.0,\n", " 'per': 10.0,\n", " 'dir': 0.0,\n", " 'dd': 10.0}},\n", " 'initial': {'model_type': 'initial',\n", " 'kind': {'model_type': 'hotsingle',\n", " 'fname': 'hotfile.txt',\n", " 'format': 'free'}},\n", " 'physics': {'gen': {'model_type': 'gen3',\n", " 'source_terms': {'model_type': 'westhuysen'}},\n", " 'sswell': {'model_type': 'zieger'},\n", " 'wcapping': {'model_type': 'wcapab',\n", " 'cds2': 5e-05,\n", " 'br': 0.00175,\n", " 'current': True,\n", " 'cds3': 0.8},\n", " 'quadrupl': {'iquad': 2, 'lambd': 0.25},\n", " 'breaking': {'model_type': 'constant', 'alpha': 1.0, 'gamma': 0.73},\n", " 'friction': {'model_type': 'collins', 'cfw': 0.015}}}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(\"swan-config-components.yml\") as stream:\n", " config_dict = yaml.load(stream, Loader=yaml.Loader)\n", "config_dict" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "SwanConfigComponents(model_type='swan', template='/source/csiro/rompy/rompy/templates/swan2', checkout='main', project=PROJECT(model_type='project', name='Test rompy setup', nr='0001', title1=None, title2=None, title3=None), set=SET(model_type='set', level=0.0, nor=None, depmin=0.05, maxmes=None, maxerr=1, grav=9.81, rho=1025.0, cdcap=0.0025, inrhog=None, hsrerr=None, direction_convention='nautical', pwtail=None, froudmax=None, icewind=None), mode=MODE(model_type='mode', kind='nonstationary', dim='twodimensional'), coordinates=COORDINATES(model_type='coordinates', kind=SPHERICAL(model_type='spherical', projection='ccm'), reapeating=False), cgrid=REGULAR(model_type='regular', spectrum=SPECTRUM(model_type='spectrum', mdc=36, flow=0.04, fhigh=0.4, msc=None, dir1=None, dir2=None), xpc=0.0, ypc=0.0, alpc=0.0, xlenc=100.0, ylenc=100.0, mxc=10, myc=10), inpgrid=INPGRIDS(model_type='inpgrids', inpgrids=[REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=None, readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='bottom.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1), REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=NONSTATIONARY(model_type='nonstationary', tbeg=datetime.datetime(2023, 1, 1, 0, 0), delt=datetime.timedelta(seconds=1800), tend=datetime.datetime(2023, 2, 1, 0, 0), tfmt='%Y%m%d.%H%M%S', deltfmt='hr', suffix='inp'), readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='wind.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1)]), boundary=BOUNDSPEC(model_type='boundspec', shapespec=SHAPESPEC(model_type='shapespec', shape=TMA(model_type='tma', gamma=3.3, d=12.0), per_type='peak', dspr_type='power'), location=SIDE(model_type='side', side='west', direction='ccw'), data=CONSTANTPAR(model_type='constantpar', hs=1.0, per=10.0, dir=0.0, dd=10.0)), initial=INITIAL(model_type='initial', kind=HOTSINGLE(model_type='hotsingle', fname='hotfile.txt', format='free')), physics=PHYSICS(model_type='physics', gen=GEN3(model_type='gen3', source_terms=WESTHUYSEN(model_type='westhuysen', wind_drag='wu', agrow=False, a=0.0015, cds2=None, br=None)), sswell=ZIEGER(model_type='zieger', b1=None), negatinp=None, wcapping=WCAPAB(model_type='wcapab', cds2=5e-05, br=0.00175, current=True, cds3=0.8), quadrupl=QUADRUPL(model_type='quadrupl', iquad=2, lambd=0.25, cn14=None, csh1=None, csh2=None, csh3=None), breaking=BREAKCONSTANT(model_type='constant', alpha=1.0, gamma=0.73), friction=COLLINS(model_type='collins', cfw=0.015)))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swan_config = SwanConfigComponents(**config_dict)\n", "swan_config" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The render component method:\n", "\n", "- Renders the component into the SWAN CMD string\n", "- Uses the `key=value` convention so the order of parameters is not important\n", "- Splits long commands to avoid exceeding Fortran maximum length" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"PROJECT name='Test rompy setup' nr='0001'\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swan_config.project.render()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PROJECT name='Test rompy setup' nr='0001'\n", "\n", "SET level=0.0 depmin=0.05 maxerr=1 grav=9.81 rho=1025.0 cdcap=0.0025 NAUTICAL\n", "\n", "MODE NONSTATIONARY TWODIMENSIONAL\n", "\n", "COORDINATES SPHERICAL CCM\n", "\n", "CGRID REGULAR xpc=0.0 ypc=0.0 alpc=0.0 xlenc=100.0 ylenc=100.0 mxc=10 myc=10 CIRCLE mdc=36 flow=0.04 fhigh=0.4\n", "\n", "INPGRID BOTTOM REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0\n", "READINP BOTTOM fac=1.0 fname1='bottom.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "INPGRID WIND REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0 &\n", " NONSTATIONARY tbeginp=20230101.000000 deltinp=0.5 HR tendinp=20230201.000000\n", "READINP WIND fac=1.0 fname1='wind.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "BOUND SHAPESPEC TMA gamma=3.3 d=12.0 PEAK DSPR POWER\n", "BOUNDSPEC SIDE WEST CCW CONSTANT PAR hs=1.0 per=10.0 dir=0.0 dd=10.0\n", "\n", "INITIAL HOTSTART SINGLE fname='hotfile.txt' FREE\n", "\n", "GEN3 WESTHUYSEN DRAG WU\n", "SSWELL ZIEGER\n", "WCAPPING AB cds2=5e-05 br=0.00175 CURRENT cds3=0.8\n", "QUADRUPL iquad=2 lambda=0.25\n", "BREAKING CONSTANT alpha=1.0 gamma=0.73\n", "FRICTION COLLINS cfw=0.015\n", "\n" ] } ], "source": [ "for component in config_dict.keys():\n", " print(getattr(swan_config, component).render() + \"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generating the INPUT file" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Helper functions to dump the contents of input and template\n", "\n", "def dump_input(model):\n", " input_file = Path(model.output_dir) / model.run_id / \"INPUT\"\n", " print(input_file.read_text())\n", "\n", "def dump_template(model):\n", " template_file = list(Path(model.config.template).glob(\"*\"))[0] / \"INPUT\"\n", " print(template_file.read_text())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:rompy.core.model:\n", "INFO:rompy.core.model:-----------------------------------------------------\n", "INFO:rompy.core.model:Model settings:\n", "INFO:rompy.core.model:\n", "period: \n", "\n", "\tStart: 2020-02-21 04:00:00\n", "\tEnd: 2020-02-24 04:00:00\n", "\tDuration: 3 days, 0:00:00\n", "\tInterval: 0:15:00\n", "\tInclude End: True\n", "\n", "\n", "output_dir: \n", "./\n", "\n", "config: \n", "model_type='swan' template='/source/csiro/rompy/rompy/templates/swan2' checkout='main' project=PROJECT(model_type='project', name='Test rompy setup', nr='0001', title1=None, title2=None, title3=None) set=SET(model_type='set', level=0.0, nor=None, depmin=0.05, maxmes=None, maxerr=1, grav=9.81, rho=1025.0, cdcap=0.0025, inrhog=None, hsrerr=None, direction_convention='nautical', pwtail=None, froudmax=None, icewind=None) mode=MODE(model_type='mode', kind='nonstationary', dim='twodimensional') coordinates=COORDINATES(model_type='coordinates', kind=SPHERICAL(model_type='spherical', projection='ccm'), reapeating=False) cgrid=REGULAR(model_type='regular', spectrum=SPECTRUM(model_type='spectrum', mdc=36, flow=0.04, fhigh=0.4, msc=None, dir1=None, dir2=None), xpc=0.0, ypc=0.0, alpc=0.0, xlenc=100.0, ylenc=100.0, mxc=10, myc=10) inpgrid=INPGRIDS(model_type='inpgrids', inpgrids=[REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=None, readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='bottom.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1), REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=NONSTATIONARY(model_type='nonstationary', tbeg=datetime.datetime(2023, 1, 1, 0, 0), delt=datetime.timedelta(seconds=1800), tend=datetime.datetime(2023, 2, 1, 0, 0), tfmt='%Y%m%d.%H%M%S', deltfmt='hr', suffix='inp'), readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='wind.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1)]) boundary=BOUNDSPEC(model_type='boundspec', shapespec=SHAPESPEC(model_type='shapespec', shape=TMA(model_type='tma', gamma=3.3, d=12.0), per_type='peak', dspr_type='power'), location=SIDE(model_type='side', side='west', direction='ccw'), data=CONSTANTPAR(model_type='constantpar', hs=1.0, per=10.0, dir=0.0, dd=10.0)) initial=INITIAL(model_type='initial', kind=HOTSINGLE(model_type='hotsingle', fname='hotfile.txt', format='free')) physics=PHYSICS(model_type='physics', gen=GEN3(model_type='gen3', source_terms=WESTHUYSEN(model_type='westhuysen', wind_drag='wu', agrow=False, a=0.0015, cds2=None, br=None)), sswell=ZIEGER(model_type='zieger', b1=None), negatinp=None, wcapping=WCAPAB(model_type='wcapab', cds2=5e-05, br=0.00175, current=True, cds3=0.8), quadrupl=QUADRUPL(model_type='quadrupl', iquad=2, lambd=0.25, cn14=None, csh1=None, csh2=None, csh3=None), breaking=BREAKCONSTANT(model_type='constant', alpha=1.0, gamma=0.73), friction=COLLINS(model_type='collins', cfw=0.015))\n", "INFO:rompy.core.model:-----------------------------------------------------\n", "INFO:rompy.core.model:Generating model input files in ./\n", "INFO:rompy.core.model:\n", "INFO:rompy.core.model:Successfully generated project in ./\n", "INFO:rompy.core.model:-----------------------------------------------------\n" ] }, { "data": { "text/plain": [ "'/source/csiro/rompy/notebooks/components/tmp/test1'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Instantiate ModelRun with swan_config and the default template\n", "model = ModelRun(\n", " run_id=\"tmp/test1\",\n", " config=swan_config,\n", " output_dir=\"./\",\n", ")\n", "\n", "# Generate the INPUT file\n", "model.generate()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!------------------------------------------------------------ Startup -------------------------------------------------------------\n", "\n", "{% if config.project %}{{config.project.render()}}{% endif %}\n", "{% if config.set != None %}{{config.set.render()}}{% endif %}\n", "{% if config.mode != None %}{{config.mode.render()}}{% endif %}\n", "{% if config.coordinates != None %}{{config.coordinates.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------------- Computational Grid -------------------------------------------------------\n", "\n", "{% if config.cgrid %}{{config.cgrid.render()}}{% endif %}\n", "\n", "\n", "!----------------------------------------------------------- Input Grids ----------------------------------------------------------\n", "\n", "{% if config.inpgrid %}{{config.inpgrid.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------- Boundary and Initial conditions ------------------------------------------------\n", "\n", "{% if config.boundary %}{{config.boundary.render()}}{% endif %}\n", "\n", "{% if config.initial %}{{config.initial.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------------------ Physics -------------------------------------------------------------\n", "\n", "{% if config.physics %}{{config.physics.render()}}{% endif %}\n", "\n" ] } ], "source": [ "# Examine the default template\n", "\n", "dump_template(model)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!------------------------------------------------------------ Startup -------------------------------------------------------------\n", "\n", "PROJECT name='Test rompy setup' nr='0001'\n", "SET level=0.0 depmin=0.05 maxerr=1 grav=9.81 rho=1025.0 cdcap=0.0025 NAUTICAL\n", "MODE NONSTATIONARY TWODIMENSIONAL\n", "COORDINATES SPHERICAL CCM\n", "\n", "\n", "!------------------------------------------------------- Computational Grid -------------------------------------------------------\n", "\n", "CGRID REGULAR xpc=0.0 ypc=0.0 alpc=0.0 xlenc=100.0 ylenc=100.0 mxc=10 myc=10 CIRCLE mdc=36 flow=0.04 fhigh=0.4\n", "\n", "\n", "!----------------------------------------------------------- Input Grids ----------------------------------------------------------\n", "\n", "INPGRID BOTTOM REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0\n", "READINP BOTTOM fac=1.0 fname1='bottom.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "INPGRID WIND REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0 &\n", " NONSTATIONARY tbeginp=20230101.000000 deltinp=0.5 HR tendinp=20230201.000000\n", "READINP WIND fac=1.0 fname1='wind.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "\n", "!------------------------------------------------- Boundary and Initial conditions ------------------------------------------------\n", "\n", "BOUND SHAPESPEC TMA gamma=3.3 d=12.0 PEAK DSPR POWER\n", "BOUNDSPEC SIDE WEST CCW CONSTANT PAR hs=1.0 per=10.0 dir=0.0 dd=10.0\n", "\n", "INITIAL HOTSTART SINGLE fname='hotfile.txt' FREE\n", "\n", "\n", "!------------------------------------------------------------ Physics -------------------------------------------------------------\n", "\n", "GEN3 WESTHUYSEN DRAG WU\n", "SSWELL ZIEGER\n", "WCAPPING AB cds2=5e-05 br=0.00175 CURRENT cds3=0.8\n", "QUADRUPL iquad=2 lambda=0.25\n", "BREAKING CONSTANT alpha=1.0 gamma=0.73\n", "FRICTION COLLINS cfw=0.015\n", "\n" ] } ], "source": [ "# Examine the generated input\n", "\n", "dump_input(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mix up hardcoded and rendered commands\n", "\n", "Use the same SWAN config object onto a modified template with the physics component hardcoded" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "swan_config.template = \"./templates/swan-physics-predefined\"\n", "model = ModelRun(\n", " run_id=\"test2\",\n", " config=swan_config,\n", " output_dir=\"./tmp\",\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:rompy.core.model:\n", "INFO:rompy.core.model:-----------------------------------------------------\n", "INFO:rompy.core.model:Model settings:\n", "INFO:rompy.core.model:\n", "period: \n", "\n", "\tStart: 2020-02-21 04:00:00\n", "\tEnd: 2020-02-24 04:00:00\n", "\tDuration: 3 days, 0:00:00\n", "\tInterval: 0:15:00\n", "\tInclude End: True\n", "\n", "\n", "output_dir: \n", "./tmp\n", "\n", "config: \n", "model_type='swan' template='./templates/swan-physics-predefined' checkout='main' project=PROJECT(model_type='project', name='Test rompy setup', nr='0001', title1=None, title2=None, title3=None) set=SET(model_type='set', level=0.0, nor=None, depmin=0.05, maxmes=None, maxerr=1, grav=9.81, rho=1025.0, cdcap=0.0025, inrhog=None, hsrerr=None, direction_convention='nautical', pwtail=None, froudmax=None, icewind=None) mode=MODE(model_type='mode', kind='nonstationary', dim='twodimensional') coordinates=COORDINATES(model_type='coordinates', kind=SPHERICAL(model_type='spherical', projection='ccm'), reapeating=False) cgrid=REGULAR(model_type='regular', spectrum=SPECTRUM(model_type='spectrum', mdc=36, flow=0.04, fhigh=0.4, msc=None, dir1=None, dir2=None), xpc=0.0, ypc=0.0, alpc=0.0, xlenc=100.0, ylenc=100.0, mxc=10, myc=10) inpgrid=INPGRIDS(model_type='inpgrids', inpgrids=[REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=None, readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='bottom.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1), REGULAR(model_type='regular', grid_type=, excval=-999.0, nonstationary=NONSTATIONARY(model_type='nonstationary', tbeg=datetime.datetime(2023, 1, 1, 0, 0), delt=datetime.timedelta(seconds=1800), tend=datetime.datetime(2023, 2, 1, 0, 0), tfmt='%Y%m%d.%H%M%S', deltfmt='hr', suffix='inp'), readinp=READINP(model_type='readinp', grid_type=, fac=1.0, idla=1, nhedf=0, nhedvec=0, format='free', form=None, idfm=None, fname1='wind.txt', fname2=None, nhedt=0), xpinp=0.0, ypinp=0.0, alpinp=0.0, mxinp=10, myinp=10, dxinp=0.1, dyinp=0.1)]) boundary=BOUNDSPEC(model_type='boundspec', shapespec=SHAPESPEC(model_type='shapespec', shape=TMA(model_type='tma', gamma=3.3, d=12.0), per_type='peak', dspr_type='power'), location=SIDE(model_type='side', side='west', direction='ccw'), data=CONSTANTPAR(model_type='constantpar', hs=1.0, per=10.0, dir=0.0, dd=10.0)) initial=INITIAL(model_type='initial', kind=HOTSINGLE(model_type='hotsingle', fname='hotfile.txt', format='free')) physics=PHYSICS(model_type='physics', gen=GEN3(model_type='gen3', source_terms=WESTHUYSEN(model_type='westhuysen', wind_drag='wu', agrow=False, a=0.0015, cds2=None, br=None)), sswell=ZIEGER(model_type='zieger', b1=None), negatinp=None, wcapping=WCAPAB(model_type='wcapab', cds2=5e-05, br=0.00175, current=True, cds3=0.8), quadrupl=QUADRUPL(model_type='quadrupl', iquad=2, lambd=0.25, cn14=None, csh1=None, csh2=None, csh3=None), breaking=BREAKCONSTANT(model_type='constant', alpha=1.0, gamma=0.73), friction=COLLINS(model_type='collins', cfw=0.015))\n", "INFO:rompy.core.model:-----------------------------------------------------\n", "INFO:rompy.core.model:Generating model input files in ./tmp\n", "INFO:rompy.core.model:\n", "INFO:rompy.core.model:Successfully generated project in ./tmp\n", "INFO:rompy.core.model:-----------------------------------------------------\n" ] }, { "data": { "text/plain": [ "'/source/csiro/rompy/notebooks/components/tmp/test2'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.generate()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!------------------------------------------------------------ Startup -------------------------------------------------------------\n", "\n", "{% if config.project %}{{config.project.render()}}{% endif %}\n", "{% if config.set != None %}{{config.set.render()}}{% endif %}\n", "{% if config.mode != None %}{{config.mode.render()}}{% endif %}\n", "{% if config.coordinates != None %}{{config.coordinates.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------------- Computational Grid -------------------------------------------------------\n", "\n", "{% if config.cgrid %}{{config.cgrid.render()}}{% endif %}\n", "\n", "\n", "!----------------------------------------------------------- Input Grids ----------------------------------------------------------\n", "\n", "{% if config.inpgrid %}{{config.inpgrid.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------- Boundary and Initial conditions ------------------------------------------------\n", "\n", "{% if config.boundary %}{{config.boundary.render()}}{% endif %}\n", "\n", "{% if config.initial %}{{config.initial.render()}}{% endif %}\n", "\n", "\n", "!------------------------------------------------------------ Physics -------------------------------------------------------------\n", "\n", "GEN3 WESTH\n", "\n" ] } ], "source": [ "# Examine the template\n", "\n", "dump_template(model)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!------------------------------------------------------------ Startup -------------------------------------------------------------\n", "\n", "PROJECT name='Test rompy setup' nr='0001'\n", "SET level=0.0 depmin=0.05 maxerr=1 grav=9.81 rho=1025.0 cdcap=0.0025 NAUTICAL\n", "MODE NONSTATIONARY TWODIMENSIONAL\n", "COORDINATES SPHERICAL CCM\n", "\n", "\n", "!------------------------------------------------------- Computational Grid -------------------------------------------------------\n", "\n", "CGRID REGULAR xpc=0.0 ypc=0.0 alpc=0.0 xlenc=100.0 ylenc=100.0 mxc=10 myc=10 CIRCLE mdc=36 flow=0.04 fhigh=0.4\n", "\n", "\n", "!----------------------------------------------------------- Input Grids ----------------------------------------------------------\n", "\n", "INPGRID BOTTOM REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0\n", "READINP BOTTOM fac=1.0 fname1='bottom.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "INPGRID WIND REGULAR xpinp=0.0 ypinp=0.0 alpinp=0.0 mxinp=10 myinp=10 dxinp=0.1 dyinp=0.1 EXCEPTION excval=-999.0 &\n", " NONSTATIONARY tbeginp=20230101.000000 deltinp=0.5 HR tendinp=20230201.000000\n", "READINP WIND fac=1.0 fname1='wind.txt' idla=1 nhedf=0 nhedt=0 nhedvec=0 FREE\n", "\n", "\n", "!------------------------------------------------- Boundary and Initial conditions ------------------------------------------------\n", "\n", "BOUND SHAPESPEC TMA gamma=3.3 d=12.0 PEAK DSPR POWER\n", "BOUNDSPEC SIDE WEST CCW CONSTANT PAR hs=1.0 per=10.0 dir=0.0 dd=10.0\n", "\n", "INITIAL HOTSTART SINGLE fname='hotfile.txt' FREE\n", "\n", "\n", "!------------------------------------------------------------ Physics -------------------------------------------------------------\n", "\n", "GEN3 WESTH\n", "\n" ] } ], "source": [ "# And the generated INPUT\n", "\n", "dump_input(model)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 4 }