Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ jobs:
conda activate bluemath-tk
python --version
python -m unittest discover tests/datamining/
python -m unittest discover tests/downloaders/
python -m unittest discover tests/interpolation/
python -m unittest discover tests/predictor/
python -m unittest discover tests/wrappers/
60 changes: 22 additions & 38 deletions bluemath_tk/wrappers/swash/swash_example.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
import inspect
import os
import os.path as op

import numpy as np

from bluemath_tk.datamining.lhs import LHS
from bluemath_tk.datamining.mda import MDA
from bluemath_tk.wrappers.swash.swash_wrapper import SwashModelWrapper
from bluemath_tk.wrappers.swash.swash_wrapper import HySwashVeggyModelWrapper


class ChySwashModelWrapper(SwashModelWrapper):
"""
Wrapper for the SWASH model with friction.
"""

default_Cf = 0.0002

def build_case(
self,
case_context: dict,
case_dir: str,
) -> None:
super().build_case(case_context=case_context, case_dir=case_dir)

# Build the input friction file
friction = np.ones((len(self.depth_array))) * self.default_Cf
friction[
int(self.fixed_parameters["Cf_ini"]) : int(self.fixed_parameters["Cf_fin"])
] = case_context["Cf"]
np.savetxt(os.path.join(case_dir, "friction.txt"), friction, fmt="%.6f")


# Usage example
if __name__ == "__main__":
# Define the output directory
output_dir = "/home/tausiaj/GitHub-GeoOcean/BlueMath_tk/test_cases/CHY" # CHANGE THIS TO YOUR DESIRED OUTPUT DIRECTORY!
output_dir = (
"test_cases/HySwashVeggy/" # CHANGE THIS TO YOUR DESIRED OUTPUT DIRECTORY!
)
# Templates directory
swash_file_path = op.dirname(inspect.getfile(SwashModelWrapper))
swash_file_path = op.dirname(inspect.getfile(HySwashVeggyModelWrapper))
templates_dir = op.join(swash_file_path, "templates")
# Fixed parameters
fixed_parameters = {
"dxinp": 1.5, # bathymetry grid spacing
"default_Cf": 0.002, # Friction manning coefficient (m^-1/3 s)
"Cf_ini": 700 / 1.5, # Friction start cell
"Cf_fin": 1250 / 1.5, # Friction end cell
"dxinp": 1, # bathymetry grid spacing
"Plants_ini": 750, # Vegetation start cell
"Plants_fin": 900, # Vegetation end cell
"comptime": 7200, # Simulation duration (s)
"warmup": 7200 * 0.15, # Warmup duration (s)
"n_nodes_per_wavelength": 60, # number of nodes per wavelength
"n_nodes_per_wavelength": 80, # number of nodes per wavelength
}
# LHS
variables_to_analyse_in_metamodel = ["Hs", "Hs_L0", "WL", "Cf", "Cr"]
variables_to_analyse_in_metamodel = [
"Hs",
"Hs_L0",
"WL",
"vegetation_height",
"plants_density",
]
lhs_parameters = {
"num_dimensions": 5,
"num_samples": 10000,
"dimensions_names": variables_to_analyse_in_metamodel,
"lower_bounds": [0.15, 0.0005, -0.6, 0.025, 0.4],
"upper_bounds": [1.6, 0.009, 0.356, 0.2, 0.8],
"lower_bounds": [0.5, 0.005, 0, 0, 0],
"upper_bounds": [2, 0.05, 1, 1.5, 1000],
}
lhs = LHS(num_dimensions=len(variables_to_analyse_in_metamodel))
df_dataset = lhs.generate(
Expand All @@ -68,8 +52,8 @@ def build_case(
mda = MDA(num_centers=mda_parameters.get("num_centers"))
mda.fit(data=df_dataset)
metamodel_parameters = mda.centroids.to_dict(orient="list")
# ChySwashModelWrapper
swash_wrapper = ChySwashModelWrapper(
# HySwashVeggyModelWrapper
swash_wrapper = HySwashVeggyModelWrapper(
templates_dir=templates_dir,
metamodel_parameters=metamodel_parameters,
fixed_parameters=fixed_parameters,
Expand All @@ -82,6 +66,6 @@ def build_case(
swash_wrapper.run_cases(launcher="docker_serial", num_workers=5)
# Post-process the results
swash_wrapper.postprocess_cases(
output_vars=["Msetup", "Hrms", "Hfreqs"], force=True
output_vars=["Ru2", "Runlev", "Msetup", "Hrms", "Hfreqs"]
)
print("Done!")
36 changes: 36 additions & 0 deletions bluemath_tk/wrappers/swash/swash_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,39 @@ def calculate_spectral_analysis(
ds = ds.assign_coords({"case_num": [output_nc["case_num"].values]})

return ds


class HySwashVeggyModelWrapper(SwashModelWrapper):
"""
Wrapper for the SWASH model with vegetation.
"""

def build_case(self, case_context: dict, case_dir: str) -> None:
super().build_case(case_context=case_context, case_dir=case_dir)

# Build the input vegetation file
plants = np.zeros((len(self.depth_array)))
plants[
int(self.fixed_parameters["Plants_ini"]) : int(
self.fixed_parameters["Plants_fin"]
)
] = case_context["plants_density"]
np.savetxt(os.path.join(case_dir, "plants.txt"), plants, fmt="%.6f")


class ChySwashModelWrapper(SwashModelWrapper):
"""
Wrapper for the SWASH model with friction.
"""

default_Cf = 0.0002

def build_case(self, case_context: dict, case_dir: str) -> None:
super().build_case(case_context=case_context, case_dir=case_dir)

# Build the input friction file
friction = np.ones((len(self.depth_array))) * self.default_Cf
friction[
int(self.fixed_parameters["Cf_ini"]) : int(self.fixed_parameters["Cf_fin"])
] = case_context["Cf"]
np.savetxt(os.path.join(case_dir, "friction.txt"), friction, fmt="%.6f")
116 changes: 58 additions & 58 deletions bluemath_tk/wrappers/swash/templates/INPUT
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
$Project name
PROJECT 'NAME' '{{ case_num }}'
$
$Set water level
SET LEVEL={{ WL }}
$
$(1D-mode, flume) or (2D-mode, basin)
MODE DYNanic ONED
COORD CARTesian
$Computational grid: geographic location, size, resolution and orientation
CGRID 0 0 0 {{ xlenc }} 0 {{ mxc }} 0
$
$Multi-layered mode
VERT 5
$
$Reading bathymetry values from file
INPGRID BOTTOM 0 0 0 {{ mxinp }} 0 {{ dxinp }} 0.0
READINP BOTTOM 1 'depth.bot' 1 0 FREE
$
$Reading friction values from file
INPGRID FRICTION 0 0 0 {{ mxinp }} 0 {{ dxinp }} 0.0
READINP FRICTION 1 'friction.txt' 1 0 FREE
$
FRIC MANNING
$Initial values for flow variables
INIT ZERO
$
$Hydraulic boundary conditions
BOU SIDE W CCW BTYPE WEAK CON SERIES 'waves.bnd'
BOU SIDE E CCW BTYPE RADIATION
SPON E 10
$
$Physics
BREAK {{ Cr }}
$Numerics
NONHYDROSTATIC BOX 1. PRECONDITIONER ILU
$
$Output quantities
DISCRET UPW MOM
DISCRET UPW UMOM H NONE
DISCRET UPW WMOM H NONE
DISCRET CORR
$
$Time integration
TIMEI 0.1 0.5
$
QUANTITY XP hexp=10
QUANT RUNUP delrp 0.01
$
CURVE 'line' 0 0 {{ xlenc }} {{ xlenc }} 0
TABLE 'line' HEAD 'output.tab' TSEC XP YP WATL OUTPUT 0 1 SEC
$
TABLE 'NOGRID' HEAD 'run.tab' TSEC RUNUP OUTPUT 0 1 SEC
$
$Starts computation
TEST 1,0
COMPUTE 000000.000 {{ deltc }} SEC {{ tendc }}
STOP
$Project name
PROJECT 'Veggy' '{{ case_num }}'
$
$Set water level
SET LEVEL={{WL}}
$
$(1D-mode, flume) or (2D-mode, basin)
MODE DYNanic ONED
COORD CARTesian
$Computational grid: geographic location, size, resolution and orientation
CGRID 0 0 0 {{ xlenc }} 0 {{ mxc }} 0
$
$Multi-layered mode
VERT 1
$
$Reading bathymetry values from file
INPGRID BOTTOM 0 0 0 {{ mxinp }} 0 {{ dxinp }} 0.0
READINP BOTTOM 1 'depth.bot' 1 0 FREE
$

$Initial values for flow variables
INIT ZERO
$
$Hydraulic boundary conditions
BOU SIDE W CCW BTYPE WEAK CON SERIES 'waves.bnd'
BOU SIDE E CCW BTYPE RADIATION
SPON E 10
$
$Physics
BREAK
FRIC MANNING 0.01
VEGETATION {{ vegetation_height }} 0.005 1 0.2
INPGRID NPLANTS 0 0 0 {{ mxinp }} 0 {{ dxinp }} 0.0
READINP NPLANTS 1 'plants.txt' 1 0 FREE
$Numerics
NONHYDrostatic BOX 1. PREConditioner ILU
$
$Output quantities
DISCRET UPW MOM
DISCRET UPW UMOM H NONE
DISCRET UPW WMOM H NONE
DISCRET CORR
$
$Time integration
TIMEI 0.1 0.5
$
QUANTITY XP hexp=10
QUANT RUNUP delrp 0.01
$
CURVE 'line' 0 0 {{ xlenc }} {{ xlenc }} 0
TABLE 'line' HEAD 'output.tab' TSEC XP YP WATL OUTPUT 0 1 SEC
$
TABLE 'NOGRID' HEAD 'run.tab' TSEC RUNUP OUTPUT 0 1 SEC
$
$Starts computation
TEST 1,0
COMPUTE 000000.000 {{ deltc }} SEC {{ tendc }}
STOP
$
Loading
Loading