Skip to content

Commit 344ddbb

Browse files
authored
Merge pull request #20 from GeoOcean/feature/wrappers
baseic wrappersss
2 parents 6907e58 + ef47570 commit 344ddbb

File tree

5 files changed

+186
-1
lines changed

5 files changed

+186
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ env.bak/
9696
venv.bak/
9797

9898
# Local notebooks
99-
notebooks/
99+
notebooks/
100+
test_cases/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
from typing import List
3+
from jinja2 import Environment, FileSystemLoader
4+
from abc import abstractmethod
5+
from ..core.models import BlueMathModel
6+
7+
8+
class BaseModelWrapper(BlueMathModel):
9+
"""
10+
Base class for model wrappers.
11+
"""
12+
13+
@abstractmethod
14+
def __init__(
15+
self,
16+
templates_dir: str,
17+
templates_name: List[str],
18+
model_parameters: dict,
19+
output_dir: str,
20+
):
21+
super().__init__()
22+
self.templates_dir = templates_dir
23+
self.templates_name = templates_name
24+
self.model_parameters = model_parameters
25+
self.output_dir = output_dir
26+
self.env = Environment(loader=FileSystemLoader(self.templates_dir))
27+
28+
def build_cases_one_by_one(self):
29+
num_cases = len(next(iter(self.model_parameters.values())))
30+
for param, values in self.model_parameters.items():
31+
if len(values) != num_cases:
32+
raise ValueError(
33+
f"All parameters must have the same number of values in one_by_one mode, check {param}"
34+
)
35+
for case_num in range(num_cases):
36+
case_folder = os.path.join(self.output_dir, f"{case_num:04}")
37+
os.makedirs(case_folder, exist_ok=True)
38+
case_context = {
39+
param: values[case_num]
40+
for param, values in self.model_parameters.items()
41+
}
42+
for template_name in self.templates_name:
43+
self.render_file_from_template(
44+
template_name=template_name,
45+
context=case_context,
46+
output_filename=os.path.join(case_folder, template_name),
47+
)
48+
49+
def build_cases(self, mode: str = "one_by_one"):
50+
if mode == "one_by_one":
51+
self.build_cases_one_by_one()
52+
53+
def render_file_from_template(
54+
self, template_name: str, context: dict, output_filename: str = None
55+
):
56+
template = self.env.get_template(name=template_name)
57+
rendered_content = template.render(context)
58+
if output_filename is None:
59+
output_filename = os.path.join(self.output_dir, template_name)
60+
with open(output_filename, "w") as f:
61+
f.write(rendered_content)
62+
63+
@abstractmethod
64+
def run_model(self, input_file):
65+
pass
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import subprocess
2+
from bluemath_tk.wrappers._base_wrappers import BaseModelWrapper
3+
4+
5+
class SwashModelWrapper(BaseModelWrapper):
6+
"""
7+
Wrapper for the SWASH model.
8+
https://swash.sourceforge.io/online_doc/swashuse/swashuse.html#input-and-output-files
9+
"""
10+
11+
default_parameters = {
12+
"param1": "default_type1",
13+
}
14+
15+
def __init__(
16+
self,
17+
templates_dir: str,
18+
templates_name: dict,
19+
model_parameters: dict,
20+
output_dir: str,
21+
):
22+
super().__init__(
23+
templates_dir=templates_dir,
24+
templates_name=templates_name,
25+
model_parameters=model_parameters,
26+
output_dir=output_dir,
27+
)
28+
self.set_logger_name(self.__class__.__name__)
29+
30+
def run_model(self, input_file):
31+
# Implement the logic to run the SWASH model with the given input file
32+
# For example, using subprocess to call the SWASH executable
33+
result = subprocess.run(
34+
["swashrun", input_file], capture_output=True, text=True
35+
)
36+
return result.stdout, result.stderr
37+
38+
39+
# Usage example
40+
if __name__ == "__main__":
41+
# Define the input parameters
42+
templates_dir = (
43+
"/home/tausiaj/GitHub-GeoOcean/BlueMath/bluemath_tk/wrappers/swash/templates/"
44+
)
45+
templates_name = ["input.sws"]
46+
model_parameters = {
47+
"vegetation_height": [1, 2, 3],
48+
}
49+
output_dir = "/home/tausiaj/GitHub-GeoOcean/BlueMath/test_cases/swash/"
50+
# Create an instance of the SWASH model wrapper
51+
swan_model = SwashModelWrapper(
52+
templates_dir=templates_dir,
53+
templates_name=templates_name,
54+
model_parameters=model_parameters,
55+
output_dir=output_dir,
56+
)
57+
# Build the input files
58+
swan_model.build_cases()
59+
# Run the model
60+
# result = swan_model.run_model(input_file)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
$Project name '{{ project_name }}' '{{ case_id }}'
2+
PROJECT 'PEPE'
3+
$
4+
$Set water level
5+
SET LEVEL=1.0
6+
$
7+
$(1D-mode, flume) or (2D-mode, basin)
8+
MODE DYNanic ONED
9+
COORD CARTesian
10+
$Computational grid: geographic location, size, resolution and orientation
11+
CGRID 0 0 0 1399 0 1567 0
12+
$
13+
$Multi-layered mode
14+
VERT 1
15+
$
16+
$Reading bathymetry values from file
17+
INPGRID BOTTOM 0 0 0 1399 0 1.00 1
18+
READINP BOTTOM 1 'depth.bot' 1 0 FREE
19+
$
20+
$Reading vegetation values from file
21+
INPGRID NPLANTS 0 0 0 1399 0 1 1
22+
READINP NPLANTS 1 'plants.txt' 1 0 FREE
23+
$
24+
$Initial values for flow variables
25+
INIT ZERO
26+
$
27+
$Hydraulic boundary conditions
28+
BOU SIDE W CCW BTYPE WEAK CON SERIES 'waves.bnd'
29+
BOU SIDE E CCW BTYPE RADIATION
30+
SPON E 10
31+
$
32+
$Physics
33+
BREAK
34+
VEGETATION {{ vegetation_height }} 0.5009019570650376 1 1.0
35+
$Numerics
36+
NONHYDrostatic BOX 1. PREConditioner ILU
37+
$
38+
$Output quantities
39+
DISCRET UPW MOM
40+
DISCRET UPW UMOM H NONE
41+
DISCRET UPW WMOM H NONE
42+
DISCRET CORR
43+
$
44+
$Time integration
45+
TIMEI 0.1 0.5
46+
$
47+
QUANTITY XP hexp=10
48+
QUANT RUNUP delrp 0.01
49+
$
50+
CURVE 'line' 0 0 1399 1399 0
51+
TABLE 'line' HEAD 'output.tab' TSEC XP YP BOTL WATL QMAG OUTPUT 0 1 SEC
52+
$
53+
TABLE 'NOGRID' HEAD 'run.tab' TSEC RUNUP OUTPUT 0 1 SEC
54+
$
55+
$Starts computation
56+
TEST 1,0
57+
COMPUTE 000000.000 0.0168 SEC 002000.000
58+
STOP
59+
$

0 commit comments

Comments
 (0)