Skip to content

Commit ff2511e

Browse files
mjrenomjreno
authored andcommitted
add drng
1 parent f85656d commit ff2511e

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
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
@@ -259,7 +259,7 @@ def _unstructure_grid_component(value: Component) -> dict[str, Any]:
259259
if not np.all(val == FILL_DNODATA):
260260
if key not in blocks:
261261
blocks[key] = {}
262-
blocks[f"period {kper + 1}"][arr_name] = val
262+
blocks[key][arr_name] = val
263263

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

flopy4/mf6/gwf/__init__.py

Lines changed: 3 additions & 1 deletion
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
@@ -21,7 +22,7 @@
2122
from flopy4.mf6.utils import open_cbc, open_hds
2223
from flopy4.utils import to_path
2324

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

2627

2728
def convert_grid(value):
@@ -82,6 +83,7 @@ def budget(self):
8283
chdg: list[Chdg] = field(block="packages")
8384
wel: list[Wel] = field(block="packages")
8485
drn: list[Drn] = field(block="packages")
86+
drng: list[Drng] = field(block="packages")
8587
output: Output = attrs.field(
8688
default=attrs.Factory(lambda self: Gwf.Output(self), takes_self=True)
8789
)

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from pprint import pprint
44

5+
import numpy as np
6+
57
from flopy4.mf6.codec import dumps, loads
8+
from flopy4.mf6.constants import FILL_DNODATA
69
from flopy4.mf6.converter import COMPONENT_CONVERTER
710

811

@@ -236,6 +239,49 @@ def test_dumps_chd():
236239
pprint(loaded)
237240

238241

242+
def test_dumps_chdg():
243+
from flopy4.mf6.gwf import Chdg, Dis, Gwf
244+
245+
nlay = 1
246+
nrow = 10
247+
ncol = 10
248+
249+
dis = Dis(nlay=nlay, nrow=nrow, ncol=ncol)
250+
gwf = Gwf(dis=dis)
251+
252+
head = np.full((nlay, nrow, ncol), FILL_DNODATA, dtype=float)
253+
head[0, 0, 0] = 1.0
254+
head[0, 9, 9] = 0.0
255+
chd = Chdg(
256+
parent=gwf,
257+
head=np.expand_dims(head.ravel(), axis=0),
258+
save_flows=True,
259+
print_input=True,
260+
dims={"nper": 1},
261+
)
262+
263+
dumped = dumps(COMPONENT_CONVERTER.unstructure(chd))
264+
print("CHD dump:")
265+
print(dumped)
266+
267+
assert "BEGIN PERIOD 1" in dumped
268+
assert "END PERIOD 1" in dumped
269+
270+
period_section = dumped.split("BEGIN PERIOD 1")[1].split("END PERIOD 1")[0].strip()
271+
lines = [line.strip() for line in period_section.split("\n") if line.strip()]
272+
273+
assert len(lines) == 12
274+
assert "READARRAYGRID" in dumped
275+
assert "MAXBOUND 2" in dumped
276+
dump_data = [[float(x) for x in line.split()] for line in lines[2:12]]
277+
dump_head = np.array(dump_data)
278+
assert np.allclose(head, dump_head)
279+
280+
loaded = loads(dumped)
281+
print("CHDG load:")
282+
pprint(loaded)
283+
284+
239285
def test_dumps_wel():
240286
from flopy4.mf6.gwf import Dis, Gwf, Wel
241287

0 commit comments

Comments
 (0)