Skip to content

Commit aec5ddc

Browse files
committed
[JTH] initial struc for delft3d and more docus
1 parent 50e63d8 commit aec5ddc

File tree

5 files changed

+117
-26
lines changed

5 files changed

+117
-26
lines changed

bluemath_tk/wrappers/_base_wrappers.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,25 @@ def build_cases(self, mode: str = "one_by_one") -> None:
348348
f"{len(self.cases_dirs)} cases created in {mode} mode and saved in {self.output_dir}"
349349
)
350350

351+
def run_case(self, case_dir: str, launcher: str) -> None:
352+
"""
353+
Run the case based on the launcher specified.
354+
355+
Parameters
356+
----------
357+
case_dir : str
358+
The case directory.
359+
launcher : str
360+
The launcher to run the case.
361+
"""
362+
363+
# Get launcher command from the available launchers
364+
launcher = self.list_available_launchers().get(launcher, launcher)
365+
366+
self.logger.info(f"Running case in {case_dir} with launcher={launcher}.")
367+
os.chdir(case_dir)
368+
self._exec_bash_commands(str_cmd=launcher)
369+
351370
def run_cases(
352371
self,
353372
launcher: str,
@@ -379,9 +398,6 @@ def run_cases(
379398
cases_dir_to_run = [self.cases_dirs[case] for case in cases_to_run]
380399
else:
381400
cases_dir_to_run = copy.deepcopy(self.cases_dirs)
382-
cases_exec_commands = [
383-
launcher.replace("$case_dir", case_dir) for case_dir in cases_dir_to_run
384-
]
385401

386402
if parallel:
387403
num_threads = self.get_num_processors_available()
@@ -390,29 +406,28 @@ def run_cases(
390406
)
391407
with ThreadPoolExecutor(max_workers=num_threads) as executor:
392408
future_to_case = {
393-
executor.submit(
394-
self._exec_bash_commands, case_exec_command
395-
): case_exec_command
396-
for case_exec_command in cases_exec_commands
409+
executor.submit(self.run_case, case_dir, launcher): case_dir
410+
for case_dir in cases_dir_to_run
397411
}
398412
for future in as_completed(future_to_case):
399-
case_exec_command = future_to_case[future]
413+
case_dir = future_to_case[future]
400414
try:
401415
future.result()
402416
except Exception as exc:
403417
self.logger.error(
404-
f"Job {case_exec_command} generated an exception: {exc}."
418+
f"Job for {case_dir} generated an exception: {exc}."
405419
)
406420
else:
407421
self.logger.info(f"Running cases sequentially with launcher={launcher}.")
408-
for case_exec_command in cases_exec_commands:
422+
for case_dir in cases_dir_to_run:
409423
try:
410-
self._exec_bash_commands(
411-
str_cmd=case_exec_command,
424+
self.run_case(
425+
case_dir=case_dir,
426+
launcher=launcher,
412427
)
413428
except Exception as exc:
414429
self.logger.error(
415-
f"Job {case_exec_command} generated an exception: {exc}."
430+
f"Job for {case_dir} generated an exception: {exc}."
416431
)
417432

418433
if launcher == "docker" or "docker" in launcher:
@@ -427,23 +442,14 @@ def run_cases_bulk(
427442
launcher: str,
428443
) -> None:
429444
"""
430-
Run the cases based on the scheduler, script, and parameters.
445+
Run the cases based on the launcher specified.
431446
432447
Parameters
433448
----------
434449
launcher : str
435450
The launcher to run the cases.
436-
437-
Raises
438-
------
439-
ValueError
440-
If the launcher is not recognized or the script does not exist.
441451
"""
442452

443-
if launcher not in self.list_available_launchers():
444-
raise ValueError(
445-
f"Invalid launcher: {launcher}, not in {self.list_available_launchers()}."
446-
)
447453
self.logger.info(f"Running cases with launcher={launcher}.")
448454
self._exec_bash_commands(str_cmd=launcher)
449455

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from .._base_wrappers import BaseModelWrapper
2+
3+
4+
class Delft3dModelWrapper(BaseModelWrapper):
5+
"""
6+
Wrapper for the Delft3d model.
7+
8+
Attributes
9+
----------
10+
default_parameters : dict
11+
The default parameters type for the wrapper.
12+
available_launchers : dict
13+
The available launchers for the wrapper.
14+
"""
15+
16+
default_parameters = {}
17+
18+
available_launchers = {"geoocean-cluster": "launchDelft3d.sh"}
19+
20+
def __init__(
21+
self,
22+
templates_dir: str,
23+
model_parameters: dict,
24+
output_dir: str,
25+
templates_name: dict = "all",
26+
debug: bool = True,
27+
) -> None:
28+
"""
29+
Initialize the Delft3d model wrapper.
30+
"""
31+
32+
super().__init__(
33+
templates_dir=templates_dir,
34+
model_parameters=model_parameters,
35+
output_dir=output_dir,
36+
templates_name=templates_name,
37+
default_parameters=self.default_parameters,
38+
)
39+
self.set_logger_name(
40+
name=self.__class__.__name__, level="DEBUG" if debug else "INFO"
41+
)

bluemath_tk/wrappers/swash/swash_wrapper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ class SwashModelWrapper(BaseModelWrapper):
5959
}
6060

6161
available_launchers = {
62-
"bash": "cd $case_dir && swashrun -input input.sws",
63-
"docker": "docker run --rm -v $case_dir:/case_dir -w /case_dir tausiaj/swash-geoocean:11.01 swashrun -input input.sws",
62+
"bash": "swashrun -input input.sws",
63+
"docker": "docker run --rm -v .:/case_dir -w /case_dir tausiaj/swash-geoocean:11.01 swashrun -input input.sws",
64+
"geoocean-cluster": "launchSwash.sh",
6465
}
6566

6667
postprocess_functions = {

docs/contribute.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,46 @@ By contributing to the BlueMath package, you agree that your contributions will
7070
If you have any questions or need further assistance, feel free to reach out to the maintainers.
7171

7272
Thank you for your contributions and support!
73+
74+
## Documentation
75+
76+
When creating new `python` code, it is essential to properly document all new classes and functions. Below, we show how the **docstrings** of classes should look, so the community can properly learn how to use **BlueMath**.
77+
78+
Code example:
79+
```python
80+
import numpy as np
81+
82+
class HyWavesExample:
83+
"""
84+
This class implements a HyWaves Metamodel Example for nearshore wave propagations.
85+
86+
Attributes
87+
----------
88+
waves_model : str
89+
The waves numerical model to use.
90+
statistical_model : str, optional
91+
The statistical model to use. Default is "MDA".
92+
93+
Methods
94+
-------
95+
run_model -> np.ndarray
96+
Runs the waves numerical model and returns the output.
97+
"""
98+
99+
def __init__(self, waves_model: str, statistical_model: str = "MDA") -> None:
100+
self.waves_model = waves_model
101+
self.statistical_model = statistical_model
102+
103+
def run_model(self, launcher: str) -> np.ndarray:
104+
"""
105+
Runs the numerical waves model.
106+
107+
Parameters
108+
----------
109+
launcher : str
110+
The launcher to use.
111+
"""
112+
113+
self.run_model(launcher=launcher, model=self.waves_model)
114+
return self.get_model_ouput()
115+
```

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ conda activate bluemath
2323
5. Finally, install package in development mode:
2424
```sh
2525
pip install -e .
26-
```
26+
```

0 commit comments

Comments
 (0)