Skip to content

Examples

Lorenzo Mentaschi edited this page Oct 24, 2018 · 34 revisions

Sample script for regular grids

An example of script running alphaBetaLab on regular grids can be found in /alphaBetaLabGitRepo/examples/ww3/regularMesh/bathyAndObstructionsCaribbean/obstFileBuilder.py

From a global domain, this example estimates the UOST parameters on a subdomain covering the Caribbean sea.

The first line of such script imports the module numpy for vector computation:

import numpy as np

The subsequent 2 lines import the components of alphaBetaLab used in this example:

from alphaBetaLab.abOptionManager import abOptions
from alphaBetaLab.abEstimateAndSave import abEstimateAndSaveRegularEtopo1, regularGridSpecWW3

Follows the definition of the spectral mesh:

dirs = np.linspace(0, 2*np.pi, 25)
nfreq = 25
minfrq = .04118
frqfactor = 1.1
freqs = [minfrq*(frqfactor**i) for i in range(1,nfreq + 1)]

Then the spatial grid is defined:

gridname = 'g_glb150'
maskFilePath = gridname + '.mask'
regularGridSpec = regularGridSpecWW3(
        xmin= -180, ymin=-70,
        dx=1.5, dy=1.5,
        nx=240, ny=94,
        maskFilePath=maskFilePath)

The bathymetry file path is defined:

etopoFilePath = '/mypath/gridgen1.1/reference_data/etopo1.nc'

The output directory is selected:

outputDestDir = './output/'

The number of cores for parallelization is defined:

nParWorker = 32

In this example only the part of the domain on the Caribbean is considered. The limits of such subdomain are defined with the instructions

llcrnr = [-100, 5]
urcrnr = [-55, 30]

where llcrnr is the low-left corner, urcrnr is the up-right corner.

The options object with the subdomain is created. This step is optional: if no option is provided to the algorithm, the default parameters will be used on the whole domain.

opt = abOptions(llcrnr=llcrnr, urcrnr=urcrnr)

The final instruction launches the algorithm and saves the output files

abEstimateAndSaveRegularEtopo1(dirs, freqs, gridname, regularGridSpec, etopoFilePath, outputDestDir, nParWorker, opt)

To launch the elaboration the user should use the console commands:

$ cd /alphaBetaLabGitRepo/examples/ww3/regularMesh/bathyAndObstructionsCaribbean/
$ /myminiconda/bin/python obstFileBuilder.py 

where /myminiconda/bin/python is the python instance where alphaBetaLab is installed (see the installation guide).

The elaboration of this example should take a few minutes on a 12-core workstation. At the end, the output files obstructions_local.g_glb150.in and obstructions_shadow.g_glb150.in should be found in the directory ./output/ defined above.

A similar (slower) example can be found in /alphaBetaLabGitRepo/examples/ww3/regularMesh/bathyAndObstructionsGlobal/obstFileBuilder.py, that estimates the UOST parameters for the whole global domain.

Sample script for triangular meshes

An example of script running alphaBetaLab on triangular meshes can be found in /alphaBetaLabGitRepo/examples/ww3/triangularMesh/bathyAndObstructions/obstFileBuilder.py

As for the previous example, the first lines are dedicated to importing into the script the needed components of alphaBetaLab and other libraries:

import numpy as np
from alphaBetaLab.abOptionManager import abOptions
from alphaBetaLab.abEstimateAndSave import triMeshSpecFromMshFile, abEstimateAndSaveTriangularEtopo1

The spectral grid is then defined

dirs = np.linspace(0, 2*np.pi, 25)
nfreq = 25
minfrq = .04118
frqfactor = 1.1
freqs = [minfrq*(frqfactor**i) for i in range(1,nfreq + 1)]

The mesh is defined instantiating a triangular mesh object, which loads the specifications from a msh file:

gridname = 'ww3'
mshfile = 'med.msh'
triMeshSpec = triMeshSpecFromMshFile(mshfile)

The path of the bathymetry file is set:

etopoFilePath = '/mypath/gridgen1.1/reference_data/etopo1.nc'

The output directory is assigned:

outputDestDir = './output/'

The number of cores for running parallel is defined:

nParWorker = 32

The options object is instantiated, with a parameter indicating the minimum size of the cells that should be considered:

minSizeKm = 3
opt = abOptions(minSizeKm=minSizeKm)

Finally, the instruction to perform the computation and save the output is given:

abEstimateAndSaveTriangularEtopo1(dirs, freqs, gridname, triMeshSpec, etopoFilePath, outputDestDir, nParWorker)

To launch the elaboration the user should use the console commands:

$ cd /alphaBetaLabGitRepo/examples/ww3/triangularMesh/bathyAndObstructions/
$ /myminiconda/bin/python obstFileBuilder.py 

where /myminiconda/bin/python is the python instance where alphaBetaLab is installed (see the installation guide).

At the end of the elaboration, which may require some minutes, the output files obstructions_local.ww3.in and obstructions_shadow.ww3.in should be found in the directory ./output/ defined above.

Clone this wiki locally