Skip to content

Commit 86817d3

Browse files
authored
cleanup (#158)
misc cleanup, also pin llvmlite to avoid issues with numba
1 parent d493924 commit 86817d3

File tree

7 files changed

+480
-476
lines changed

7 files changed

+480
-476
lines changed

flopy4/adapters.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22

3-
from flopy.discretization import StructuredGrid
3+
from flopy.discretization.grid import Grid
4+
from flopy.discretization.structuredgrid import StructuredGrid
5+
from flopy.discretization.unstructuredgrid import UnstructuredGrid
6+
from flopy.discretization.vertexgrid import VertexGrid
47

58

69
class StructuredGridWrapper(StructuredGrid):
@@ -108,3 +111,34 @@ def from_binary_grid_file(cls, file_path, verbose=False):
108111
ia=grb_obj.ia,
109112
ja=grb_obj.ja,
110113
)
114+
115+
116+
def get_kij(nn: int, nlay: int, nrow: int, ncol: int) -> tuple[int, int, int]:
117+
nodes = nlay * nrow * ncol
118+
if nn < 0 or nn >= nodes:
119+
raise ValueError(f"Node number {nn} is out of bounds (1 to {nodes})")
120+
k = (nn - 1) / (ncol * nrow) + 1
121+
ij = nn - (k - 1) * ncol * nrow
122+
i = (ij - 1) / ncol + 1
123+
j = ij - (i - 1) * ncol
124+
return int(k), int(i), int(j)
125+
126+
127+
def get_jk(nn: int, ncpl: int) -> tuple[int, int]:
128+
if nn < 0 or nn >= ncpl:
129+
raise ValueError(f"Node number {nn} is out of bounds (1 to {ncpl})")
130+
k = (nn - 1) / ncpl + 1
131+
j = nn - (k - 1) * ncpl
132+
return int(j), int(k)
133+
134+
135+
def get_cellid(nn: int, grid: Grid) -> tuple[int, ...]:
136+
match grid:
137+
case StructuredGrid():
138+
return get_kij(nn, *grid.shape)
139+
case VertexGrid():
140+
return get_jk(nn, grid.ncpl)
141+
case UnstructuredGrid():
142+
return (nn,)
143+
case _:
144+
raise TypeError(f"Unsupported grid type: {type(grid)}")

flopy4/mf6/codec/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141

4242
def _make_converter() -> Converter:
43+
# TODO: document what is converter's responsibility vs Jinja's
44+
# TODO: how can we make sure writing remains lazy for list input?
45+
# don't eagerly unstructure to dict, lazily access from the template?
46+
4347
from flopy4.mf6.component import Component
4448
from flopy4.mf6.gwf.chd import Chd
4549
from flopy4.mf6.gwf.oc import Oc

flopy4/mf6/codec/converter.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import numpy as np
44
import sparse
55
import xattree
6-
from flopy.discretization.grid import Grid
7-
from flopy.discretization.structuredgrid import StructuredGrid
8-
from flopy.discretization.unstructuredgrid import UnstructuredGrid
9-
from flopy.discretization.vertexgrid import VertexGrid
106
from numpy.typing import NDArray
117
from xarray import DataArray
128
from xattree import get_xatspec
139

10+
from flopy4.adapters import get_cellid
1411
from flopy4.mf6.component import Component
1512
from flopy4.mf6.config import SPARSE_THRESHOLD
1613
from flopy4.mf6.constants import FILL_DNODATA
@@ -147,7 +144,8 @@ def unstructure_array(value: DataArray) -> dict:
147144

148145
def unstructure_component(value: Component) -> dict[str, Any]:
149146
data = xattree.asdict(value)
150-
for block in get_blocks(value.dfn).values():
147+
blocks = get_blocks(value.dfn)
148+
for block in blocks.values():
151149
for field_name, field in block.items():
152150
if is_list_field(field):
153151
data[field_name] = unstructure_array(data[field_name])
@@ -180,37 +178,6 @@ def unstructure_tdis(value: Any) -> dict[str, Any]:
180178
return data
181179

182180

183-
def get_kij(nn: int, nlay: int, nrow: int, ncol: int) -> tuple[int, int, int]:
184-
nodes = nlay * nrow * ncol
185-
if nn < 0 or nn >= nodes:
186-
raise ValueError(f"Node number {nn} is out of bounds (1 to {nodes})")
187-
k = (nn - 1) / (ncol * nrow) + 1
188-
ij = nn - (k - 1) * ncol * nrow
189-
i = (ij - 1) / ncol + 1
190-
j = ij - (i - 1) * ncol
191-
return int(k), int(i), int(j)
192-
193-
194-
def get_jk(nn: int, ncpl: int) -> tuple[int, int]:
195-
if nn < 0 or nn >= ncpl:
196-
raise ValueError(f"Node number {nn} is out of bounds (1 to {ncpl})")
197-
k = (nn - 1) / ncpl + 1
198-
j = nn - (k - 1) * ncpl
199-
return int(j), int(k)
200-
201-
202-
def get_cellid(nn: int, grid: Grid) -> tuple[int, ...]:
203-
match grid:
204-
case StructuredGrid():
205-
return get_kij(nn, *grid.shape)
206-
case VertexGrid():
207-
return get_jk(nn, grid.ncpl)
208-
case UnstructuredGrid():
209-
return (nn,)
210-
case _:
211-
raise TypeError(f"Unsupported grid type: {type(grid)}")
212-
213-
214181
def unstructure_chd(value: Any) -> dict[str, Any]:
215182
if (parent := value.parent) is None:
216183
raise ValueError(

flopy4/mf6/component.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ class Component(ABC, MutableMapping):
2323
Notes
2424
-----
2525
All subclasses of `Component` must be decorated with `xattree`.
26-
27-
We use the `children` attribute provided by `xattree`. We know
28-
children are also `Component`s, but mypy does not. TODO: fix??
29-
Then we can remove the `# type: ignore` comments.
3026
"""
3127

3228
_load = IO(Loader) # type: ignore
@@ -60,6 +56,9 @@ def __attrs_init_subclass__(cls):
6056
cls.dfn = cls.get_dfn()
6157

6258
def __getitem__(self, key):
59+
# We use `children` from `xattree` to implement MutableMapping.
60+
# children are also `Component`s, but mypy doesn't know this..
61+
# TODO fix, then we can remove the `# type: ignore` comments.
6362
return self.children[key] # type: ignore
6463

6564
def __setitem__(self, key, value):
@@ -76,7 +75,7 @@ def __len__(self):
7675

7776
@classmethod
7877
def get_dfn(cls) -> Dfn:
79-
"""Generate the component's MODFLOW 6 definition."""
78+
"""Get the component's definition (i.e. specification)."""
8079
fields = {field_name: to_dfn_field(field) for field_name, field in fields_dict(cls).items()}
8180
blocks: dict[str, dict[str, Field]] = {}
8281
for field_name, field_ in fields.items():
@@ -95,7 +94,7 @@ def get_dfn(cls) -> Dfn:
9594
)
9695

9796
def _preio(self, format: str) -> None:
98-
"""Place for any pre-IO setup"""
97+
# prep for io operations
9998
if not self.filename:
10099
self.filename = self.default_filename()
101100

0 commit comments

Comments
 (0)