Skip to content

Commit 4e58c3e

Browse files
mjrenomjreno
authored andcommitted
add test_interface.py
1 parent 705aa34 commit 4e58c3e

File tree

3 files changed

+211
-211
lines changed

3 files changed

+211
-211
lines changed

flopy4/mf6/interface/flopy3.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import Optional
23

34
import numpy as np
45
from flopy.datbase import DataInterface, DataListInterface, DataType
@@ -8,8 +9,7 @@
89
from flopy.mbase import ModelInterface
910
from flopy.pakbase import PackageInterface
1011
from flopy.plot.plotutil import PlotUtilities
11-
from typing import Optional
12-
from xattree import XatTree, _get_xatspec, _XatSpec
12+
from xattree import _get_xatspec
1313

1414
from flopy4.mf6.model import Model
1515
from flopy4.mf6.package import Package
@@ -67,11 +67,9 @@ def __init__(
6767
)
6868

6969
for c in model.children:
70-
xatspec = _get_xatspec(type(model.children[c]))
7170
p_fp3 = Flopy3Package(
71+
package=model.children[c],
7272
model=self,
73-
data=model.data[c],
74-
spec=xatspec,
7573
modeltime=modeltime,
7674
)
7775
self._plist.append(p_fp3)
@@ -156,9 +154,6 @@ def get_package_list(self, ftype=None):
156154
return [p.name for p in self._plist]
157155

158156
def plot(self, packages: list = None, **kwargs):
159-
# kwargs = {}
160-
# kwargs["filename_base"] = "modelif"
161-
162157
return PlotUtilities._plot_model_helper(
163158
self, SelPackList=packages, **kwargs
164159
)
@@ -167,38 +162,37 @@ def plot(self, packages: list = None, **kwargs):
167162
class Flopy3Package(PackageInterface):
168163
def __init__(
169164
self,
165+
package: Package,
170166
model: Optional[Flopy3Model] = None,
171-
data: Optional[XatTree] = None,
172-
spec: Optional[_XatSpec] = None,
173167
modeltime: Optional[ModelTime] = None,
174168
):
175169
self._model = model
176-
self._data = data
177-
self._spec = spec
170+
self._data = package.data
171+
self._spec = _get_xatspec(type(package))
178172
self._time = modeltime
179173
self._dlist = list()
180174

181-
for a in data.attrs:
182-
if data.attrs[a] is not None:
175+
for a in self._data.attrs:
176+
if self._data.attrs[a] is not None:
183177
d_fp3 = Flopy3Data(
184178
name=a,
185179
modelname=self.parent,
186180
modelgrid=model.modelgrid,
187181
modeltime=modeltime,
188-
data=data.attrs[a],
189-
spec=spec.flat[a],
182+
data=self._data.attrs[a],
183+
spec=self._spec.flat[a],
190184
)
191185
self.__dict__[f"{a}"] = d_fp3
192186
self._dlist.append(d_fp3)
193187

194-
for v in data.data_vars:
188+
for v in self._data.data_vars:
195189
d_fp3 = Flopy3Data(
196190
name=v,
197191
modelname=self.parent,
198192
modelgrid=model.modelgrid,
199193
modeltime=modeltime,
200-
data=data.data_vars[v],
201-
spec=spec.flat[v],
194+
data=self._data.data_vars[v],
195+
spec=self._spec.flat[v],
202196
)
203197
self.__dict__[f"{v}"] = d_fp3
204198
self._dlist.append(d_fp3)

test/test_component.py

Lines changed: 0 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from flopy4.mf6.component import COMPONENTS
88
from flopy4.mf6.constants import FILL_DNODATA
99
from flopy4.mf6.gwf import Chd, Dis, Gwf, Ic, Npf, Oc
10-
from flopy4.mf6.ims import Ims
11-
from flopy4.mf6.interface.flopy3 import Flopy3Model
1210
from flopy4.mf6.simulation import Simulation
1311
from flopy4.mf6.tdis import Tdis
1412

@@ -201,193 +199,3 @@ def test_init_big_sim():
201199
chd.head.data.todense(),
202200
sim.models["gwf"].chd[0].data.head.data.todense(),
203201
)
204-
205-
206-
def test_modelif():
207-
from flopy.mbase import ModelInterface
208-
from flopy.pakbase import PackageInterface
209-
210-
time = ModelTime(perlen=[1.0], nstp=[1], tsmult=[1.0])
211-
grid = StructuredGrid(nlay=1, nrow=10, ncol=10)
212-
213-
dims = {
214-
"nlay": grid.nlay,
215-
"nrow": grid.nrow,
216-
"ncol": grid.ncol,
217-
}
218-
219-
dis = Dis(**dims)
220-
dis.nogrb = True
221-
dis.xorigin = 0.0
222-
dis.yorigin = 0.0
223-
224-
dims["nper"] = time.nper
225-
dims["nnodes"] = grid.nnodes
226-
227-
# ims = Ims(dims=dims)
228-
ims = Ims()
229-
ims.inner_hclose = 1e-6
230-
ims.inner_rclose = 0.1000000
231-
ic = Ic(dims=dims)
232-
oc = Oc(dims=dims)
233-
npf = Npf(dims=dims)
234-
chd = Chd(dims=dims, head={"*": {(0, 0, 0): 1.0, (0, 9, 9): 0.0}})
235-
236-
gwf = Gwf(
237-
dis=dis,
238-
ic=ic,
239-
oc=oc,
240-
npf=npf,
241-
chd=[chd],
242-
dims=dims,
243-
)
244-
245-
pnames = ["dis", "ic", "oc", "npf", "chd0"]
246-
ptypes = ["DIS", "IC", "OC", "NPF", "CHD"]
247-
248-
gwfif = Flopy3Model(model=gwf, modeltime=time, ims=ims)
249-
assert isinstance(gwfif, ModelInterface)
250-
assert gwfif.modelgrid
251-
assert gwfif.modelgrid.nlay == gwf.dis.nlay
252-
assert gwfif.modelgrid.nrow == gwf.dis.nrow
253-
assert gwfif.modelgrid.ncol == gwf.dis.ncol
254-
assert gwfif.modelgrid.nnodes == grid.nnodes
255-
256-
assert gwfif.solver_tols == (ims.inner_hclose, ims.inner_rclose)
257-
assert np.all(
258-
np.equal(gwfif.laytyp, np.zeros(gwfif.modelgrid.nnodes, dtype=int))
259-
)
260-
261-
# model packages
262-
assert gwfif.get_package_list() == pnames
263-
for i, p in enumerate(gwfif.packagelist):
264-
assert isinstance(p, PackageInterface)
265-
assert p.name == pnames[i]
266-
assert p.package_type == ptypes[i]
267-
# assert p.parent == "gwf"
268-
# TODO oc?
269-
if p.name == "chd0" or p.name == "oc":
270-
assert p.has_stress_period_data
271-
else:
272-
assert not p.has_stress_period_data
273-
274-
# package data
275-
dlist = [d.name for d in p.data_list]
276-
print(f"PACKAGE {p.name} data list => {dlist}")
277-
for d in p.data_list:
278-
print(f"{p.name} data={d.name}")
279-
# assert d.model == "gwf"
280-
print(f"dtype: {d.dtype}")
281-
print(f"data_type: {d.data_type}")
282-
print(f"array: {d.array}\n")
283-
284-
kwargs = {}
285-
kwargs["filename_base"] = "modelif"
286-
287-
# gwfif.plot(**kwargs)
288-
gwfif.plot(filename_base="modelif")
289-
290-
291-
def norun_test_cbd_small():
292-
import sys
293-
294-
sys.path.append("/home/mjreno/.clone/usgs/flopy/autotest")
295-
from test_grid_cases import GridCases
296-
297-
time = ModelTime(perlen=[1.0], nstp=[1], tsmult=[1.0])
298-
299-
cbd_small = GridCases.structured_cbd_small()
300-
dims = {
301-
"nlay": cbd_small.nlay,
302-
"nrow": cbd_small.nrow,
303-
"ncol": cbd_small.ncol,
304-
}
305-
dis = Dis(**dims)
306-
dims["nper"] = time.nper
307-
dims["nnodes"] = cbd_small.nnodes
308-
gwf = Gwf(
309-
dis=dis,
310-
dims=dims,
311-
)
312-
gwfif = Flopy3Model(model=gwf, modelgrid=cbd_small, modeltime=time)
313-
gwfif.plot(filename_base="cbd_small")
314-
315-
316-
def norun_test_grid2():
317-
lx = 5.0
318-
lz = 1.0
319-
nlay = 1
320-
nrow = 1
321-
ncol = 5
322-
nper = 1
323-
delc = 1.0
324-
delr = lx / ncol
325-
delz = lz / nlay
326-
adelc = np.full((nrow), delc)
327-
adelr = np.full((ncol), delr)
328-
top = [0.0, 0.0, -0.90, 0.0, 0.0]
329-
botm = list(top - np.arange(delz, nlay * delz + delz, delz))
330-
botm[2] = -1.0
331-
idomain = np.full((nlay, nrow, ncol), 1)
332-
333-
dims = {
334-
"nlay": nlay,
335-
"nrow": nrow,
336-
"ncol": ncol,
337-
}
338-
339-
time = ModelTime(perlen=[1.0], nstp=[1], tsmult=[1.0])
340-
dis = Dis(**dims)
341-
dis.delr = adelr
342-
dis.delc = adelc
343-
dis.top = top
344-
dis.botm = botm
345-
# dis.idomain = idomain
346-
dims["nper"] = time.nper
347-
dims["nnodes"] = nlay * nrow * ncol
348-
gwf = Gwf(
349-
dis=dis,
350-
dims=dims,
351-
)
352-
gwfif = Flopy3Model(model=gwf, modeltime=time)
353-
gwfif.plot(filename_base="grid2")
354-
355-
# dis = flopy.mf6.ModflowGwfdis(
356-
# gwf,
357-
# nlay=nlay,
358-
# nrow=nrow,
359-
# ncol=ncol,
360-
# delr=delr,
361-
# delc=delc,
362-
# top=top,
363-
# botm=botm,
364-
# idomain=idomain,
365-
# )
366-
367-
368-
# demo failed case
369-
def test_mgrid2():
370-
lx = 5.0
371-
lz = 1.0
372-
nlay = 1
373-
nrow = 1
374-
ncol = 5
375-
delc = 1.0
376-
delr = lx / ncol
377-
delz = lz / nlay
378-
adelc = np.full((nrow), delc)
379-
adelr = np.full((ncol), delr)
380-
381-
dims = {
382-
"nlay": nlay,
383-
"nrow": nrow,
384-
"ncol": ncol,
385-
}
386-
387-
dis = Dis(**dims)
388-
389-
print(dis.delr.dims) # prints ('ncol',)
390-
print(dis.delc.dims) # prints ('nrow',)
391-
392-
dis.delr = adelr # succeeds
393-
dis.delc = adelc # fails

0 commit comments

Comments
 (0)