Skip to content

Commit 769ef56

Browse files
authored
write the quickstart simulation! not yet correctly.. (#154)
still need to reformulate tdis perioddata like oc, since we extracted separate arrays for tdis in the same way. but we can now call write() on a simulation. and I think it's close.
1 parent a1f661f commit 769ef56

File tree

12 files changed

+55
-18
lines changed

12 files changed

+55
-18
lines changed

flopy4/mf6/component.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ class Component(ABC, MutableMapping):
3838

3939
@property
4040
def path(self) -> Path:
41+
"""Get the path to the component's input file."""
4142
return Path.cwd() / self.filename
4243

43-
def _default_filename(self) -> str:
44+
def default_filename(self) -> str:
45+
"""
46+
Generate a default filename for the component.
47+
By default, this is the component's name then
48+
the class name in lowercase, separated by dot.
49+
50+
Override this method in subclasses to provide
51+
a custom default filename.
52+
"""
4453
name = self.name # type: ignore
4554
cls_name = self.__class__.__name__.lower()
4655
return f"{name}.{cls_name}"
@@ -50,10 +59,6 @@ def __attrs_init_subclass__(cls):
5059
COMPONENTS[cls.__name__.lower()] = cls
5160
cls.dfn = cls.get_dfn()
5261

53-
def __attrs_post_init__(self):
54-
if not self.filename:
55-
self.filename = self._default_filename()
56-
5762
def __getitem__(self, key):
5863
return self.children[key] # type: ignore
5964

@@ -89,12 +94,21 @@ def get_dfn(cls) -> Dfn:
8994
**blocks,
9095
)
9196

97+
def _preio(self, format: str) -> None:
98+
"""Place for any pre-IO setup"""
99+
if not self.filename:
100+
self.filename = self.default_filename()
101+
92102
def load(self, format: str) -> None:
103+
"""Load the component from an input file."""
104+
self._preio(format=format)
93105
self._load(format=format)
94106
for child in self.children.values(): # type: ignore
95-
child.load(format)
107+
child.load(format=format)
96108

97109
def write(self, format: str) -> None:
110+
"""Write the component to an input file."""
111+
self._preio(format=format)
98112
self._write(format=format)
99113
for child in self.children.values(): # type: ignore
100-
child.write(format)
114+
child.write(format=format)

flopy4/mf6/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Context(Component, ABC):
1212
workspace: Path = field(default=None)
1313

1414
def __attrs_post_init__(self):
15+
super().__attrs_post_init__()
1516
if self.workspace is None:
1617
self.workspace = Path.cwd()
1718

flopy4/mf6/filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def array_chunks(value: xr.DataArray, chunks: Mapping[Hashable, int] | None = No
9595
}
9696
value = value.chunk(chunks)
9797
for chunk in value.data.blocks:
98-
yield chunk.compute()
98+
yield np.squeeze(chunk.compute())
9999

100100

101101
def array2string(value: NDArray) -> str:
@@ -112,6 +112,7 @@ def array2string(value: NDArray) -> str:
112112
if value.ndim == 1:
113113
# add an axis to 1d arrays so np.savetxt writes elements on 1 line
114114
value = value[None]
115+
value = np.atleast_1d(value)
115116
format = (
116117
"%d"
117118
if np.issubdtype(value.dtype, np.integer)

flopy4/mf6/gwf/dis.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Dis(Package):
7676

7777
def __attrs_post_init__(self):
7878
self.nnodes = self.ncol * self.nrow * self.nlay
79+
super().__attrs_post_init__()
7980

8081
def to_grid(self) -> StructuredGrid:
8182
"""

flopy4/mf6/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
@xattree
99
class Model(Component, ABC):
10-
pass
10+
def default_filename(self) -> str:
11+
return f"{self.name}.nam" # type: ignore

flopy4/mf6/package.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77

88
@xattree
99
class Package(Component, ABC):
10-
pass
10+
def default_filename(self) -> str:
11+
name = self.parent.name if self.parent else self.name # type: ignore
12+
cls_name = self.__class__.__name__.lower()
13+
return f"{name}.{cls_name}"

flopy4/mf6/simulation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Simulation(Context):
3030
filename: str = field(default="mfsim.nam", init=False)
3131

3232
def __attrs_post_init__(self):
33+
super().__attrs_post_init__()
3334
if self.filename != "mfsim.nam":
3435
warn(
3536
"Simulation filename must be 'mfsim.nam'.",

flopy4/mf6/templates/blocks.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% import 'macros.jinja' as macros with context %}
22
{% for block_name, block_ in (dfn|dict_blocks).items() %}
33
BEGIN {{ block_name.upper() }}
4-
{% for field in block_.values() -%}
4+
{% for field in block_.values() if (field|field_value) is not none -%}
55
{{ macros.field(field) }}
66
{%- endfor %}
77
END {{ block_name.upper() }}

flopy4/mf6/templates/macros.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ this macro receives the block definition. from that
6262
it looks up the value of the one variable with the
6363
same name as the block, which custom converter has
6464
made sure exists in a sparse dict representation of
65-
an array. we need to spin this out into a block for
65+
an array. we need to expand this into a block for
6666
each stress period.
6767
#}
6868
{% set dict = data[block_name] %}

test/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
pytest_plugins = ["modflow_devtools.fixtures"]
4+
35
PROJ_ROOT_PATH = Path(__file__).parents[1]
46
DOCS_PATH = PROJ_ROOT_PATH / "docs"
57
EXAMPLES_PATH = DOCS_PATH / "examples"

0 commit comments

Comments
 (0)