Skip to content

Commit

Permalink
run_model
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielTollenaar committed Dec 10, 2024
1 parent 2e4fc0f commit cda369b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ribasim_nl/ribasim_nl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from ribasim_nl.case_conversions import pascal_to_snake_case
from ribasim_nl.geometry import split_basin
from ribasim_nl.run_model import run

manning_data = manning_resistance.Static(length=[100], manning_n=[0.04], profile_width=[10], profile_slope=[1])
level_data = level_boundary.Static(level=[0])
Expand Down Expand Up @@ -97,6 +98,15 @@ def graph(self):
def next_node_id(self):
return self.node_table().df.index.max() + 1

def run(self, stream_output=True, returncode=True):
"""_summary_
Args:
stream_output (bool, optional): stream output in IDE. Defaults to True.
returncode (bool, optional): return returncode after running model. Defaults to True.
"""
return run(self.filepath, stream_output=stream_output, returncode=returncode)

# methods relying on networkx. Discuss making this all in a subclass of Model
def _upstream_nodes(self, node_id):
# get upstream nodes
Expand Down
44 changes: 44 additions & 0 deletions src/ribasim_nl/ribasim_nl/run_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# %%
import os
import subprocess
from pathlib import Path


def run(
toml_path: Path,
stream_output: bool = True,
returncode: bool = True,
):
"""To run a Ribasim model
Args:
toml_path (Path): path to your ribasim toml-file
stream_output (bool, optional): stream output in IDE. Defaults to False.
returncode (bool, optional): return return code after running model. Defaults to True.
"""
env = os.environ.copy()

input = ""
proc = subprocess.Popen(
["ribasim", toml_path.as_posix()],
cwd=toml_path.parent.as_posix(),
env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="ascii",
)
if stream_output:
with proc:
proc.stdin.write(input)
proc.stdin.close()
for line in proc.stdout:
print(line, end="")
outs = None
else:
outs, _ = proc.communicate(input)

if returncode:
return proc.returncode
else:
return outs

0 comments on commit cda369b

Please sign in to comment.