Skip to content

Commit 0f6a0c5

Browse files
committed
wip
1 parent 52bffaf commit 0f6a0c5

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

flopy4/mf6/__init__.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
from pathlib import Path
2-
31
from flopy4.mf6.codec import dump, load
42
from flopy4.mf6.component import Component
53
from flopy4.uio import DEFAULT_REGISTRY
64

5+
# register io methods for ascii format input files
6+
# TODO: call this "mf6" or something? since it might include binary files
7+
DEFAULT_REGISTRY.register_loader(Component, "ascii", lambda c: load(c.path))
8+
DEFAULT_REGISTRY.register_writer(Component, "ascii", lambda c: dump(c, c.path))
79

8-
def _default_filename(component: Component) -> str:
9-
"""Default path for a component, based on its name."""
10-
if filename := component.filename:
11-
return filename
12-
name = component.name # type: ignore
13-
cls_name = component.__class__.__name__.lower()
14-
return f"{name}.{cls_name}"
15-
16-
17-
def _path(component: Component) -> str:
18-
"""Default path for a component, based on its name."""
19-
if hasattr(component, "path") and component.path is not None:
20-
path = Path(component.path).expanduser().resolve()
21-
if path.is_dir():
22-
return str(path / _default_filename(component))
23-
return str(path)
24-
return _default_filename(component)
25-
26-
27-
DEFAULT_REGISTRY.register_loader(Component, "ascii", lambda component: load(_path(component)))
28-
DEFAULT_REGISTRY.register_writer(
29-
Component, "ascii", lambda component: dump(component, _path(component))
30-
)
10+
# TODO: explore what a netcdf format for the whole simulation would look like?

flopy4/mf6/component.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC
22
from collections.abc import MutableMapping
3+
from pathlib import Path
34

45
from modflow_devtools.dfn import Dfn, Field
56
from xattree import xattree
@@ -32,11 +33,26 @@ class Component(ABC, MutableMapping):
3233

3334
filename: str = field(default=None)
3435

36+
@property
37+
def path(self) -> Path:
38+
if self.filename is None:
39+
raise ValueError(f"{self.name} has no filename.") # type: ignore
40+
return Path.cwd() / self.filename
41+
42+
def _default_filename(self) -> str:
43+
name = self.name # type: ignore
44+
cls_name = self.__class__.__name__.lower()
45+
return f"{name}.{cls_name}"
46+
3547
@classmethod
3648
def __attrs_init_subclass__(cls):
3749
COMPONENTS[cls.__name__.lower()] = cls
3850
cls.dfn = cls.get_dfn()
3951

52+
def __attrs_post_init__(self):
53+
if not self.filename:
54+
self.filename = self._default_filename()
55+
4056
def __getitem__(self, key):
4157
return self.children[key] # type: ignore
4258

flopy4/mf6/context.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from abc import ABC
2+
from pathlib import Path
3+
4+
from xattree import xattree
5+
6+
from flopy4.mf6.component import Component
7+
from flopy4.mf6.spec import field
8+
9+
10+
@xattree
11+
class Context(Component, ABC):
12+
workspace: Path = field(default=None)
13+
14+
@property
15+
def path(self) -> Path:
16+
if self.workspace is None:
17+
raise ValueError(f"{self.name} has no workspace.")
18+
if self.filename is None:
19+
raise ValueError(f"{self.name} has no filename.") # type: ignore
20+
return self.workspace / self.filename

flopy4/mf6/simulation.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from os import PathLike
2-
from pathlib import Path
32
from warnings import warn
43

54
from flopy.discretization.modeltime import ModelTime
65
from modflow_devtools.misc import cd, run_cmd
76
from xattree import xattree
87

9-
from flopy4.mf6.component import Component
8+
from flopy4.mf6.context import Context
109
from flopy4.mf6.exchange import Exchange
1110
from flopy4.mf6.model import Model
1211
from flopy4.mf6.solution import Solution
@@ -23,12 +22,11 @@ def convert_time(value):
2322

2423

2524
@xattree
26-
class Simulation(Component):
25+
class Simulation(Context):
2726
models: dict[str, Model] = field()
2827
exchanges: dict[str, Exchange] = field()
2928
solutions: dict[str, Solution] = field()
3029
tdis: Tdis = field(converter=convert_time)
31-
workspace: Path = field(default=None)
3230
filename: str = field(default="mfsim.nam")
3331

3432
def __attrs_post_init__(self):
@@ -40,13 +38,6 @@ def __attrs_post_init__(self):
4038
)
4139
self.filename = "mfsim.nam"
4240

43-
@property
44-
def path(self) -> Path:
45-
"""Return the path to the simulation namefile."""
46-
if self.workspace is None:
47-
raise ValueError("Simulation has no workspace path.")
48-
return Path(self.workspace).expanduser().resolve() / self.filename
49-
5041
@property
5142
def time(self) -> ModelTime:
5243
"""Return the simulation time discretization."""

0 commit comments

Comments
 (0)