Skip to content

Commit 0a47a5f

Browse files
mjrenomjreno
authored andcommitted
add drng
1 parent c839739 commit 0a47a5f

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

flopy4/mf6/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
MF6 = "mf6"
44
FILL_DEFAULT = np.nan
5-
FILL_DNODATA = 3.0e30
5+
FILL_DNODATA = 3e30
66
LENBOUNDNAME = 40

flopy4/mf6/converter/unstructure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _unstructure_grid_component(value: Component) -> dict[str, Any]:
268268
if not np.all(val == FILL_DNODATA):
269269
if key not in blocks:
270270
blocks[key] = {}
271-
blocks[f"period {kper + 1}"][arr_name] = val
271+
blocks[key][arr_name] = val
272272

273273
return {name: block for name, block in blocks.items() if name != "period"}
274274

flopy4/mf6/gwf/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from flopy4.mf6.gwf.chdg import Chdg
1212
from flopy4.mf6.gwf.dis import Dis
1313
from flopy4.mf6.gwf.drn import Drn
14+
from flopy4.mf6.gwf.drng import Drng
1415
from flopy4.mf6.gwf.ic import Ic
1516
from flopy4.mf6.gwf.npf import Npf
1617
from flopy4.mf6.gwf.oc import Oc
@@ -22,7 +23,7 @@
2223
from flopy4.mf6.utils import open_cbc, open_hds
2324
from flopy4.utils import to_path
2425

25-
__all__ = ["Gwf", "Chd", "Chdg", "Dis", "Drn", "Ic", "Npf", "Oc", "Rch", "Sto", "Wel"]
26+
__all__ = ["Gwf", "Chd", "Chdg", "Dis", "Drn", "Drng", "Ic", "Npf", "Oc", "Rch", "Sto", "Wel"]
2627

2728

2829
def convert_grid(value):
@@ -81,9 +82,10 @@ def budget(self):
8182
sto: Sto | None = field(block="packages", default=None)
8283
chd: list[Chd] = field(block="packages")
8384
chdg: list[Chdg] = field(block="packages")
84-
wel: list[Wel] = field(block="packages")
8585
drn: list[Drn] = field(block="packages")
86+
drng: list[Drng] = field(block="packages")
8687
rch: list[Rch] = field(block="packages")
88+
wel: list[Wel] = field(block="packages")
8789
output: Output = attrs.field(
8890
default=attrs.Factory(lambda self: Gwf.Output(self), takes_self=True)
8991
)

flopy4/mf6/gwf/drng.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from pathlib import Path
2+
from typing import ClassVar, Optional
3+
4+
import numpy as np
5+
6+
# from attrs import Converter
7+
from numpy.typing import NDArray
8+
from xattree import xattree
9+
10+
# from flopy4.mf6.converter import structure_array
11+
from flopy4.mf6.package import Package
12+
from flopy4.mf6.spec import array, field, path
13+
from flopy4.mf6.utils.grid import update_maxbound
14+
from flopy4.utils import to_path
15+
16+
17+
@xattree
18+
class Drng(Package):
19+
multi_package: ClassVar[bool] = True
20+
auxiliary: Optional[list[str]] = array(block="options", default=None)
21+
auxmultname: Optional[str] = field(block="options", default=None)
22+
print_input: bool = field(block="options", default=False)
23+
print_flows: bool = field(block="options", default=False)
24+
readarraygrid: bool = field(block="options", default=True)
25+
save_flows: bool = field(block="options", default=False)
26+
obs_filerecord: Optional[Path] = path(
27+
block="options", default=None, converter=to_path, inout="fileout"
28+
)
29+
export_array_netcdf: bool = field(block="options", default=False)
30+
mover: bool = field(block="options", default=False)
31+
dev_cubic_scaling: bool = field(default=False, block="options")
32+
maxbound: Optional[int] = field(block="dimensions", default=None, init=False)
33+
elev: Optional[NDArray[np.float64]] = array(
34+
block="period",
35+
dims=(
36+
"nper",
37+
"nodes",
38+
),
39+
default=None,
40+
# converter=Converter(structure_array, takes_self=True, takes_field=True),
41+
on_setattr=update_maxbound,
42+
)
43+
cond: Optional[NDArray[np.float64]] = array(
44+
block="period",
45+
dims=(
46+
"nper",
47+
"nodes",
48+
),
49+
default=None,
50+
# converter=Converter(structure_array, takes_self=True, takes_field=True),
51+
on_setattr=update_maxbound,
52+
)
53+
aux: Optional[NDArray[np.float64]] = array(
54+
block="period",
55+
dims=(
56+
"nper",
57+
"nodes",
58+
),
59+
default=None,
60+
# converter=Converter(structure_array, takes_self=True, takes_field=True),
61+
on_setattr=update_maxbound,
62+
)

test/test_mf6_codec.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from pprint import pprint
44

55
import pytest
6+
import numpy as np
67

78
from flopy4.mf6.codec import dumps, loads
9+
from flopy4.mf6.constants import FILL_DNODATA
810
from flopy4.mf6.converter import COMPONENT_CONVERTER
911

1012

@@ -257,6 +259,49 @@ def test_dumps_chd():
257259
pprint(loaded)
258260

259261

262+
def test_dumps_chdg():
263+
from flopy4.mf6.gwf import Chdg, Dis, Gwf
264+
265+
nlay = 1
266+
nrow = 10
267+
ncol = 10
268+
269+
dis = Dis(nlay=nlay, nrow=nrow, ncol=ncol)
270+
gwf = Gwf(dis=dis)
271+
272+
head = np.full((nlay, nrow, ncol), FILL_DNODATA, dtype=float)
273+
head[0, 0, 0] = 1.0
274+
head[0, 9, 9] = 0.0
275+
chd = Chdg(
276+
parent=gwf,
277+
head=np.expand_dims(head.ravel(), axis=0),
278+
save_flows=True,
279+
print_input=True,
280+
dims={"nper": 1},
281+
)
282+
283+
dumped = dumps(COMPONENT_CONVERTER.unstructure(chd))
284+
print("CHD dump:")
285+
print(dumped)
286+
287+
assert "BEGIN PERIOD 1" in dumped
288+
assert "END PERIOD 1" in dumped
289+
290+
period_section = dumped.split("BEGIN PERIOD 1")[1].split("END PERIOD 1")[0].strip()
291+
lines = [line.strip() for line in period_section.split("\n") if line.strip()]
292+
293+
assert len(lines) == 12
294+
assert "READARRAYGRID" in dumped
295+
assert "MAXBOUND 2" in dumped
296+
dump_data = [[float(x) for x in line.split()] for line in lines[2:12]]
297+
dump_head = np.array(dump_data)
298+
assert np.allclose(head, dump_head)
299+
300+
loaded = loads(dumped)
301+
print("CHDG load:")
302+
pprint(loaded)
303+
304+
260305
def test_dumps_wel():
261306
from flopy4.mf6.gwf import Dis, Gwf, Wel
262307

0 commit comments

Comments
 (0)