Skip to content

Commit 7c0f1eb

Browse files
committed
update(model_splitter.py): support for SSM and ATS
* fix support for SSM file mapping * add ATS support * replace match/case statements in ModelTime with if/else
1 parent 0d9c914 commit 7c0f1eb

File tree

3 files changed

+107
-23
lines changed

3 files changed

+107
-23
lines changed

autotest/test_model_splitter.py

+67
Original file line numberDiff line numberDiff line change
@@ -1598,3 +1598,70 @@ def test_package_observations():
15981598
vcid = vdict2[obsname]
15991599
if vcid != cellid:
16001600
raise AssertionError("Observation cellid not correctly mapped to new model")
1601+
1602+
1603+
@requires_exe("mf6")
1604+
def test_ats(function_tmpdir):
1605+
name = "ats_dev"
1606+
1607+
sim = flopy.mf6.MFSimulation()
1608+
ims = flopy.mf6.ModflowIms(sim, complexity="SIMPLE")
1609+
tdis = flopy.mf6.ModflowTdis(
1610+
sim,
1611+
perioddata=[
1612+
(100.0, 5, 1.0),
1613+
],
1614+
ats_perioddata=[(0, 10.0, 1e-05, 20, 2, 10)],
1615+
)
1616+
1617+
gwf = flopy.mf6.ModflowGwf(sim, modelname=name)
1618+
1619+
dx = 100
1620+
dy = 10
1621+
nlay = 1
1622+
nrow = 1
1623+
ncol = 10
1624+
delc = np.full((nrow,), dy / nrow)
1625+
delr = np.full((ncol,), dx / ncol)
1626+
top = 10
1627+
botm = 0
1628+
idomain = np.ones((nlay, nrow, ncol), dtype=int)
1629+
1630+
dis = flopy.mf6.ModflowGwfdis(
1631+
gwf,
1632+
nlay=nlay,
1633+
nrow=nrow,
1634+
ncol=ncol,
1635+
delr=delr,
1636+
delc=delc,
1637+
top=top,
1638+
botm=botm,
1639+
idomain=idomain,
1640+
)
1641+
1642+
ic = flopy.mf6.ModflowGwfic(gwf, strt=top)
1643+
npf = flopy.mf6.ModflowGwfnpf(gwf)
1644+
chd = flopy.mf6.ModflowGwfchd(
1645+
gwf, stress_period_data=[((0, 0, 0), 9.5), ((0, 0, 9), 7)]
1646+
)
1647+
1648+
budget_file = f"{name}.bud"
1649+
head_file = f"{name}.hds"
1650+
oc = flopy.mf6.ModflowGwfoc(
1651+
gwf,
1652+
budget_filerecord=budget_file,
1653+
head_filerecord=head_file,
1654+
saverecord=[("HEAD", "ALL"), ("BUDGET", "ALL")],
1655+
printrecord=[("BUDGET", "ALL")],
1656+
)
1657+
1658+
array = np.ones((nrow, ncol), dtype=int)
1659+
array[0, 5:] = 2
1660+
1661+
mfs = flopy.mf6.utils.Mf6Splitter(sim)
1662+
new_sim = mfs.split_model(array)
1663+
1664+
new_sim.set_sim_path(function_tmpdir)
1665+
new_sim.write_simulation()
1666+
success, _ = new_sim.run_simulation()
1667+
assert success

flopy/discretization/modeltime.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,11 @@ def set_tsmult():
739739
nonlocal tslens
740740
nonlocal tsmult
741741
tslens = [l for l in tslens if l > 0]
742-
match len(tslens):
743-
case 0 | 1:
744-
tsmult[kper] = 1.0
745-
case _:
746-
tsmult[kper] = tslens[1] / tslens[0]
742+
743+
if len(tslens) in (0, 1):
744+
tsmult[kper] = 1.0
745+
else:
746+
tsmult[kper] = tslens[-1] / tslens[-2]
747747

748748
for i in range(len(headers)):
749749
hdr = headers[i]

flopy/mf6/utils/model_splitter.py

+35-18
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def _remap_nodes(self, array):
921921
array = np.ravel(array)
922922

923923
idomain = self._modelgrid.idomain.reshape((-1, self._ncpl))
924-
mkeys = np.unique(array)
924+
mkeys = [int(i) for i in np.unique(array)]
925925
bad_keys = []
926926
for mkey in mkeys:
927927
count = 0
@@ -980,7 +980,7 @@ def _remap_nodes(self, array):
980980
except TypeError:
981981
xverts, yverts = None, None
982982

983-
for m in np.unique(array):
983+
for m in mkeys:
984984
cells = np.asarray(array == m).nonzero()[0]
985985
mapping = np.zeros((len(cells),), dtype=int)
986986
mapping[:] = cells
@@ -1002,12 +1002,12 @@ def _remap_nodes(self, array):
10021002
self._offsets[m] = {"xorigin": None, "yorigin": None}
10031003

10041004
new_ncpl = {}
1005-
for m in np.unique(array):
1005+
for m in mkeys:
10061006
new_ncpl[m] = 1
10071007
for i in grid_info[m][0]:
10081008
new_ncpl[m] *= i
10091009

1010-
for mdl in np.unique(array):
1010+
for mdl in mkeys:
10111011
mnodes = np.asarray(array == mdl).nonzero()[0]
10121012
mg_info = grid_info[mdl]
10131013
if mg_info is not None:
@@ -1021,10 +1021,10 @@ def _remap_nodes(self, array):
10211021
self._node_map[onode] = (mdl, nnode)
10221022

10231023
new_connections = {
1024-
i: {"internal": {}, "external": {}} for i in np.unique(array)
1024+
i: {"internal": {}, "external": {}} for i in mkeys
10251025
}
1026-
exchange_meta = {i: {} for i in np.unique(array)}
1027-
usg_meta = {i: {} for i in np.unique(array)}
1026+
exchange_meta = {i: {} for i in mkeys}
1027+
usg_meta = {i: {} for i in mkeys}
10281028
for node, conn in self._connection.items():
10291029
mdl, nnode = self._node_map[node]
10301030
for ix, cnode in enumerate(conn):
@@ -1173,8 +1173,9 @@ def _map_verts_iverts(self, array):
11731173
if iverts is None:
11741174
return
11751175

1176-
ivlut = {mkey: {} for mkey in np.unique(array)}
1177-
for mkey in np.unique(array):
1176+
mkeys = [int(i) for i in np.unique(array)]
1177+
ivlut = {mkey: {} for mkey in mkeys}
1178+
for mkey in mkeys:
11781179
new_iv = 0
11791180
new_iverts = []
11801181
new_verts = []
@@ -1217,7 +1218,7 @@ def _create_sln_tdis(self):
12171218
new_sim : MFSimulation object
12181219
"""
12191220
for pak in self._sim.sim_package_list:
1220-
if pak.package_abbr in ("gwfgwt", "gwfgwf", "gwfgwe"):
1221+
if pak.package_abbr in ("gwfgwt", "gwfgwf", "gwfgwe", "utlats"):
12211222
continue
12221223
pak_cls = PackageContainer.package_factory(pak.package_abbr, "")
12231224
signature = inspect.signature(pak_cls)
@@ -1226,7 +1227,11 @@ def _create_sln_tdis(self):
12261227
if key in ("simulation", "loading_package", "pname", "kwargs"):
12271228
continue
12281229
elif key == "ats_perioddata":
1229-
continue
1230+
data = getattr(pak, "ats")
1231+
if len(data._packages) > 0:
1232+
data = data._packages[0].perioddata.array
1233+
d[key] = data
1234+
12301235
else:
12311236
data = getattr(pak, key)
12321237
if hasattr(data, "array"):
@@ -1273,7 +1278,7 @@ def _remap_cell2d(self, item, cell2d, mapped_data):
12731278

12741279
return mapped_data
12751280

1276-
def _remap_filerecords(self, item, value, mapped_data):
1281+
def _remap_filerecords(self, item, value, mapped_data, namfile=False):
12771282
"""
12781283
Method to create new file record names and map them to their
12791284
associated models
@@ -1299,15 +1304,16 @@ def _remap_filerecords(self, item, value, mapped_data):
12991304
"obs_filerecord",
13001305
"concentration_filerecord",
13011306
"ts_filerecord",
1302-
"temperature_filerecord"
1307+
"temperature_filerecord",
1308+
"nc_mesh2d_filerecord"
13031309
):
13041310
value = value.array
13051311
if value is None:
13061312
pass
13071313
else:
13081314
value = value[0][0]
13091315
for mdl in mapped_data.keys():
1310-
if mapped_data[mdl]:
1316+
if mapped_data[mdl] or namfile:
13111317
new_val = value.split(".")
13121318
new_val = f"{'.'.join(new_val[0:-1])}_{mdl :0{self._fdigits}d}.{new_val[-1]}"
13131319
mapped_data[mdl][item] = new_val
@@ -2580,7 +2586,10 @@ def _remap_ssm(self, package, mapped_data):
25802586
continue
25812587
records.append(tuple(rec))
25822588

2583-
mapped_data[mkey]["sources"] = records
2589+
if records:
2590+
mapped_data[mkey]["sources"] = records
2591+
else:
2592+
mapped_data[mkey]["sources"] = None
25842593

25852594
return mapped_data
25862595

@@ -3793,20 +3802,28 @@ def split_model(self, array):
37933802
)
37943803
self._create_sln_tdis()
37953804

3796-
nam_options = {}
3805+
nam_options = {mkey: {} for mkey in self._new_ncpl.keys()}
3806+
# todo: change this to model by model options bc nc_filerecord stuff
37973807
for item, value in self._model.name_file.blocks[
37983808
"options"
37993809
].datasets.items():
38003810
if item == "list":
38013811
continue
3802-
nam_options[item] = value.array
3812+
if value.array is None:
3813+
continue
3814+
if item.endswith("_filerecord"):
3815+
self._remap_filerecords(item, value, nam_options, namfile=True)
3816+
else:
3817+
for mkey in self._new_ncpl.keys():
3818+
nam_options[mkey][item] = value.array
38033819
self._model_dict = {}
3820+
# todo: trap the nc_mesh2d_filerecord stuff...
38043821
for mkey in self._new_ncpl.keys():
38053822
mdl_cls = PackageContainer.model_factory(self._model_type)
38063823
self._model_dict[mkey] = mdl_cls(
38073824
self._new_sim,
38083825
modelname=f"{self._modelname}_{mkey :0{self._fdigits}d}",
3809-
**nam_options,
3826+
**nam_options[mkey],
38103827
)
38113828

38123829
for package in self._model.packagelist:

0 commit comments

Comments
 (0)