Skip to content

Commit 3031fe1

Browse files
committed
steal sim run from imod-python, stub write on base component
1 parent 5dfdda3 commit 3031fe1

File tree

5 files changed

+83
-77
lines changed

5 files changed

+83
-77
lines changed

flopy4/mf6/component.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,22 @@ class Component(ABC, MutableMapping):
1313
def __attrs_init_subclass__(cls):
1414
COMPONENTS[cls.__name__.lower()] = cls
1515

16-
def __attrs_post_init__(self):
17-
self._where = type(self).__xattree__["where"]
18-
1916
def __getitem__(self, key):
20-
data = getattr(self, self._where)
21-
return data.children[key]
17+
return self.children[key] # type: ignore
2218

2319
def __setitem__(self, key, value):
24-
data = getattr(self, self._where)
25-
if key in data.children:
26-
data.update({key: value})
27-
else:
28-
data = data.assign({key: value})
29-
setattr(self, self._where, data)
20+
self.children[key] = value # type: ignore
3021

3122
def __delitem__(self, key):
32-
data = getattr(self, self._where)
33-
data = data.drop_nodes(key)
34-
setattr(self, self._where, data)
23+
del self.children[key] # type: ignore
3524

3625
def __iter__(self):
37-
data = getattr(self, self._where)
38-
return iter(data.children)
26+
return iter(self.children) # type: ignore
3927

4028
def __len__(self):
41-
data = getattr(self, self._where)
42-
return len(data.children)
29+
return len(self.children) # type: ignore
30+
31+
def write(self) -> None:
32+
# TODO: write with jinja to file
33+
for child in self.children.values(): # type: ignore
34+
child.write()

flopy4/mf6/simulation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import subprocess
2+
from os import PathLike
13
from pathlib import Path
24

35
from flopy.discretization.modeltime import ModelTime
6+
from modflow_devtools.misc import set_dir
47
from xattree import field, xattree
58

69
from flopy4.mf6.component import Component
@@ -31,3 +34,15 @@ class Simulation(Component):
3134
@property
3235
def time(self) -> ModelTime:
3336
return self.tdis.to_time()
37+
38+
def run(self, exe: str | PathLike = "mf6") -> None:
39+
"""Run the simulation using the given executable."""
40+
if self.path is None:
41+
raise ValueError(f"Simulation {self.name} has no workspace path.")
42+
with set_dir(self.path):
43+
result = subprocess.run(exe, capture_output=True)
44+
if result.returncode != 0:
45+
raise RuntimeError(
46+
f"Simulation {self.name}: {exe} failed to run with returncode " # type: ignore
47+
f"{result.returncode}, and error message:\n\n{result.stdout.decode()} "
48+
)

0 commit comments

Comments
 (0)