Skip to content

Commit 1336e60

Browse files
committed
keep supporting 3-tuples with dummy values for backcompat
1 parent 937f86f commit 1336e60

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed

autotest/test_cellbudgetfile.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,3 +1010,88 @@ def test_cellbudgetfile_get_ts_aux_vars_mf6_disu(dis_sim):
10101010
chd = cbc.get_data(text="CHD")
10111011
for field in ["node", "node2", "q"]:
10121012
assert field in chd[0].dtype.names
1013+
1014+
1015+
@pytest.mark.requires_exe("mf6")
1016+
def test_cellbudgetfile_get_ts_backwards_compatible_idx_format(
1017+
dis_sim, function_tmpdir
1018+
):
1019+
from flopy.mf6 import ModflowGwfchd, ModflowGwfdisu, ModflowGwfdisv
1020+
1021+
sim = dis_sim
1022+
gwf = sim.get_model()
1023+
dis_grid = gwf.modelgrid
1024+
1025+
# DISV
1026+
disv_kwargs = get_disv_kwargs(
1027+
nlay=dis_grid.nlay,
1028+
nrow=dis_grid.nrow,
1029+
ncol=dis_grid.ncol,
1030+
delr=dis_grid.delr,
1031+
delc=dis_grid.delc,
1032+
tp=dis_grid.top,
1033+
botm=dis_grid.botm,
1034+
)
1035+
gwf.remove_package("dis")
1036+
gwf.remove_package("chd")
1037+
disv = ModflowGwfdisv(gwf, **disv_kwargs)
1038+
chd_spd = [[0, 0, 10.0], [0, 24, 0.0]]
1039+
chd = ModflowGwfchd(gwf, stress_period_data=chd_spd)
1040+
1041+
sim.set_sim_path(function_tmpdir / "disv_backcompat")
1042+
sim.write_simulation()
1043+
success, _ = sim.run_simulation(silent=False)
1044+
assert success
1045+
1046+
cbc = gwf.output.budget()
1047+
1048+
ts_new = cbc.get_ts(idx=(0, 4), text="DATA-SPDIS", variable="qx")
1049+
ts_old = cbc.get_ts(idx=(0, 0, 4), text="DATA-SPDIS", variable="qx")
1050+
1051+
np.testing.assert_array_equal(
1052+
ts_new,
1053+
ts_old,
1054+
)
1055+
1056+
# DISU
1057+
disu_kwargs = get_disu_kwargs(
1058+
nlay=dis_grid.nlay,
1059+
nrow=dis_grid.nrow,
1060+
ncol=dis_grid.ncol,
1061+
delr=dis_grid.delr,
1062+
delc=dis_grid.delc,
1063+
tp=dis_grid.top,
1064+
botm=dis_grid.botm,
1065+
return_vertices=True,
1066+
)
1067+
gwf.remove_package("disv")
1068+
gwf.remove_package("chd")
1069+
disu = ModflowGwfdisu(gwf, **disu_kwargs)
1070+
chd_spd = [[0, 10.0], [24, 0.0]]
1071+
chd = ModflowGwfchd(gwf, stress_period_data=chd_spd)
1072+
1073+
sim.set_sim_path(function_tmpdir / "disu_backcompat")
1074+
sim.write_simulation()
1075+
success, _ = sim.run_simulation(silent=False)
1076+
assert success
1077+
1078+
cbc = gwf.output.budget()
1079+
1080+
ts_new = cbc.get_ts(idx=4, text="DATA-SPDIS", variable="qx")
1081+
ts_old = cbc.get_ts(idx=(0, 0, 4), text="DATA-SPDIS", variable="qx")
1082+
1083+
np.testing.assert_array_equal(
1084+
ts_new,
1085+
ts_old,
1086+
)
1087+
1088+
# test lists too
1089+
ts_new_list = cbc.get_ts(idx=[4, 10], text="DATA-SPDIS", variable="qx")
1090+
ts_old_list = cbc.get_ts(
1091+
idx=[(0, 0, 4), (0, 0, 10)], text="DATA-SPDIS", variable="qx"
1092+
)
1093+
1094+
np.testing.assert_array_equal(
1095+
ts_new_list,
1096+
ts_old_list,
1097+
)

flopy/utils/binaryfile/__init__.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,13 @@ def get_ts(self, idx, text=None, times=None, variable="q"):
16151615
16161616
- Structured grids (DIS): (layer, row, column) or list of such
16171617
- Vertex grids (DISV): (layer, cellid) or list of such
1618-
- Unstructured grids (DISU): node number, (node number,) or list of such
1618+
- Unstructured grids (DISU): node number or list of such
16191619
16201620
All indices must be zero-based.
1621+
1622+
For backwards compatibility, DISV and DISU grids also accept the old
1623+
3-tuple format with dummy values: (layer, dummy, cellid) for DISV and
1624+
(dummy, dummy, node) for DISU.
16211625
text : str
16221626
The text identifier for the record. Examples include
16231627
'RIVER LEAKAGE', 'STORAGE', 'FLOW RIGHT FACE', etc.
@@ -1760,11 +1764,23 @@ def _cellids(self, idx):
17601764
raise ValueError(f"Column index {j} out of range [0, {self.ncol})")
17611765
cellid.append((k, i, j))
17621766
elif grid_type == "vertex":
1763-
if not isinstance(item, (list, tuple)) or len(item) != 2:
1767+
if isinstance(item, (list, tuple)):
1768+
if len(item) == 2:
1769+
# proper format: (layer, cellid)
1770+
k, cell = item
1771+
elif len(item) == 3:
1772+
# old format: (layer, dummy, cellid)
1773+
k, cell = item[0], item[2]
1774+
else:
1775+
raise ValueError(
1776+
f"Expected DISV cell index (layer, cellid) "
1777+
f"or (layer, dummy, cellid), got: {item}"
1778+
)
1779+
else:
17641780
raise ValueError(
1765-
f"Expected DISV cell index (layer, cellid), got: {item}"
1781+
f"Expected DISV cell index (layer, cellid) or "
1782+
f"(layer, dummy, cellid), got: {item}"
17661783
)
1767-
k, cell = item
17681784
if k < 0 or k >= self.nlay:
17691785
raise Exception(f"Layer index {k} out of range [0, {self.nlay})")
17701786
if cell < 0 or cell >= self.modelgrid.ncpl:
@@ -1773,9 +1789,26 @@ def _cellids(self, idx):
17731789
)
17741790
cellid.append((k, cell))
17751791
else:
1776-
if not isinstance(item, (int, np.integer)):
1777-
raise Exception(f"Expected DISU node number , got: {item}")
1778-
node = int(item)
1792+
if isinstance(item, (int, np.integer)):
1793+
# proper format: just node number
1794+
node = int(item)
1795+
elif isinstance(item, (list, tuple)):
1796+
if len(item) == 3:
1797+
# old format: (dummy, dummy, node)
1798+
node = int(item[2])
1799+
elif len(item) == 1:
1800+
# Also support single-element tuple
1801+
node = int(item[0])
1802+
else:
1803+
raise ValueError(
1804+
f"Expected DISU node number or (dummy, dummy, node), "
1805+
f"got: {item}"
1806+
)
1807+
else:
1808+
raise ValueError(
1809+
f"Expected DISU node number or (dummy, dummy, node), "
1810+
f"got: {item}"
1811+
)
17791812
if node < 0 or node >= self.modelgrid.nnodes:
17801813
raise Exception(
17811814
f"Node index {node} out of range [0, {self.modelgrid.nnodes})"

0 commit comments

Comments
 (0)