Skip to content

Commit 46dc0ed

Browse files
mjrenomjreno
authored andcommitted
add disv based load and create tests
1 parent 75b09d6 commit 46dc0ed

File tree

17 files changed

+326
-71
lines changed

17 files changed

+326
-71
lines changed

autotest/regression/test_mf6_netcdf.py

Lines changed: 171 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@
1111
import flopy
1212
from flopy.utils import import_optional_dependency
1313
from flopy.utils.datautil import DatumUtil
14-
15-
16-
class TestInfo:
17-
def __init__(
18-
self,
19-
original_simulation_folder,
20-
netcdf_simulation_folder,
21-
netcdf_output_file,
22-
):
23-
self.original_simulation_folder = original_simulation_folder
24-
self.netcdf_simulation_folder = netcdf_simulation_folder
25-
self.netcdf_output_file = netcdf_output_file
14+
from flopy.utils.gridutil import get_disv_kwargs
2615

2716

2817
@requires_pkg("xarray")
@@ -32,23 +21,19 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path):
3221
xr = import_optional_dependency("xarray")
3322
data_path_base = example_data_path / "mf6" / "netcdf"
3423
tests = {
35-
"test_gwf_sto01_mesh": TestInfo(
36-
"gwf_sto01",
37-
"gwf_sto01_write",
38-
"gwf_sto01.ugrid.nc",
39-
),
40-
"test_gwf_sto01_structured": TestInfo(
41-
"gwf_sto01",
42-
"gwf_sto01_write",
43-
"gwf_sto01.structured.nc",
44-
),
24+
"test_gwf_sto01_mesh": {
25+
"base_sim_dir": "gwf_sto01",
26+
"netcdf_output_file": "gwf_sto01.in.nc",
27+
},
28+
"test_gwf_sto01_structured": {
29+
"base_sim_dir": "gwf_sto01",
30+
"netcdf_output_file": "gwf_sto01.in.nc",
31+
},
4532
}
4633
ws = function_tmpdir / "ws"
4734
for base_folder, test_info in tests.items():
4835
print(f"RUNNING TEST: {base_folder}")
49-
data_path = os.path.join(
50-
data_path_base, base_folder, test_info.original_simulation_folder
51-
)
36+
data_path = os.path.join(data_path_base, base_folder, test_info["base_sim_dir"])
5237
# copy example data into working directory
5338
base_model_folder = os.path.join(ws, f"{base_folder}_base")
5439
test_model_folder = os.path.join(ws, f"{base_folder}_test")
@@ -76,7 +61,7 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path):
7661
print(f"cmp => {f}")
7762
base = os.path.join(base_model_folder, f)
7863
gen = os.path.join(test_model_folder, f)
79-
if f != test_info.netcdf_output_file:
64+
if f != test_info["netcdf_output_file"]:
8065
# "gwf_sto01.dis.ncf": # TODO wkt string missing on write?
8166
with open(base, "r") as file1, open(gen, "r") as file2:
8267
# Skip first line
@@ -91,25 +76,23 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path):
9176

9277

9378
@requires_pkg("xarray")
94-
@requires_exe("mf6")
9579
@pytest.mark.regression
9680
def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
9781
xr = import_optional_dependency("xarray")
9882
data_path_base = example_data_path / "mf6" / "netcdf"
9983
tests = {
100-
"test_gwf_sto01_mesh": TestInfo(
101-
"gwf_sto01",
102-
"gwf_sto01_write",
103-
"gwf_sto01.ugrid.nc",
104-
),
105-
"test_gwf_sto01_structured": TestInfo(
106-
"gwf_sto01",
107-
"gwf_sto01_write",
108-
"gwf_sto01.structured.nc",
109-
),
84+
"test_gwf_sto01_mesh": {
85+
"base_sim_dir": "gwf_sto01",
86+
"netcdf_output_file": "gwf_sto01.in.nc",
87+
"netcdf_type": "mesh2d",
88+
},
89+
"test_gwf_sto01_structured": {
90+
"base_sim_dir": "gwf_sto01",
91+
"netcdf_output_file": "gwf_sto01.in.nc",
92+
"netcdf_type": "structured",
93+
},
11094
}
11195
name = "gwf_sto01"
112-
cases = ["mesh2d", "structured"]
11396

11497
# static model data
11598
# temporal discretization
@@ -189,10 +172,7 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
189172
ws = function_tmpdir / "ws"
190173
for idx, (base_folder, test_info) in enumerate(tests.items()):
191174
print(f"RUNNING TEST: {base_folder}")
192-
netcdf = cases[idx]
193-
data_path = os.path.join(
194-
data_path_base, base_folder, test_info.original_simulation_folder
195-
)
175+
data_path = os.path.join(data_path_base, base_folder, test_info["base_sim_dir"])
196176
# copy example data into working directory
197177
base_model_folder = os.path.join(ws, f"{base_folder}_base")
198178
test_model_folder = os.path.join(ws, f"{base_folder}_test")
@@ -301,7 +281,62 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
301281
)
302282

303283
sim.set_sim_path(test_model_folder)
304-
sim.write_simulation(netcdf=netcdf)
284+
sim.write_simulation(netcdf=test_info["netcdf_type"])
285+
286+
# compare generated files
287+
gen_files = [
288+
f
289+
for f in os.listdir(test_model_folder)
290+
if os.path.isfile(os.path.join(test_model_folder, f))
291+
]
292+
base_files = [
293+
f
294+
for f in os.listdir(base_model_folder)
295+
if os.path.isfile(os.path.join(base_model_folder, f))
296+
]
297+
# assert len(gen_files) == len(base_files)
298+
for f in base_files:
299+
print(f"cmp => {f}")
300+
base = os.path.join(base_model_folder, f)
301+
gen = os.path.join(test_model_folder, f)
302+
if f != test_info["netcdf_output_file"]:
303+
with open(base, "r") as file1, open(gen, "r") as file2:
304+
# Skip first line
305+
next(file1)
306+
next(file2)
307+
308+
for line1, line2 in zip(file1, file2):
309+
assert line1 == line2
310+
else:
311+
# TODO compare nc files
312+
assert os.path.exists(gen)
313+
314+
315+
@requires_pkg("xarray")
316+
@pytest.mark.regression
317+
def test_load_netcdf_disv01b(function_tmpdir, example_data_path):
318+
xr = import_optional_dependency("xarray")
319+
data_path_base = example_data_path / "mf6" / "netcdf"
320+
tests = {
321+
"test_gwf_disv01b": {
322+
"base_sim_dir": "disv01b",
323+
"netcdf_output_file": "disv01b.in.nc",
324+
},
325+
}
326+
ws = function_tmpdir / "ws"
327+
for base_folder, test_info in tests.items():
328+
print(f"RUNNING TEST: {base_folder}")
329+
data_path = os.path.join(data_path_base, base_folder, test_info["base_sim_dir"])
330+
# copy example data into working directory
331+
base_model_folder = os.path.join(ws, f"{base_folder}_base")
332+
test_model_folder = os.path.join(ws, f"{base_folder}_test")
333+
shutil.copytree(data_path, base_model_folder)
334+
# load example
335+
sim = flopy.mf6.MFSimulation.load(sim_ws=base_model_folder)
336+
# change simulation path
337+
sim.set_sim_path(test_model_folder)
338+
# write example simulation to reset path
339+
sim.write_simulation()
305340

306341
# compare generated files
307342
gen_files = [
@@ -319,24 +354,104 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
319354
print(f"cmp => {f}")
320355
base = os.path.join(base_model_folder, f)
321356
gen = os.path.join(test_model_folder, f)
322-
if f != test_info.netcdf_output_file:
357+
if f != test_info["netcdf_output_file"]:
323358
with open(base, "r") as file1, open(gen, "r") as file2:
324359
# Skip first line
325360
next(file1)
326361
next(file2)
327362

328363
for line1, line2 in zip(file1, file2):
329-
if "netcdf filein" in line1.lower():
330-
if netcdf == "mesh2d":
331-
suffix = "ugrid"
332-
else:
333-
suffix = netcdf
334-
assert line1 == f" NETCDF FILEIN gwf_sto01.{suffix}.nc\n"
335-
assert line2 == " NETCDF FILEIN gwf_sto01.in.nc\n"
336-
else:
337-
assert line1 == line2
364+
assert line1.lower() == line2.lower()
338365
else:
339366
# TODO compare nc files
340-
assert os.path.exists(
341-
os.path.join(test_model_folder, "gwf_sto01.in.nc")
342-
)
367+
assert os.path.exists(gen)
368+
369+
370+
@requires_pkg("xarray")
371+
@pytest.mark.regression
372+
def test_create_netcdf_disv01b(function_tmpdir, example_data_path):
373+
xr = import_optional_dependency("xarray")
374+
data_path_base = example_data_path / "mf6" / "netcdf"
375+
tests = {
376+
"test_gwf_disv01b": {
377+
"base_sim_dir": "disv01b",
378+
"netcdf_output_file": "disv01b.in.nc",
379+
"netcdf_type": "mesh2d",
380+
},
381+
}
382+
383+
name = "disv01b"
384+
nlay = 3
385+
nrow = 3
386+
ncol = 3
387+
delr = 10.0
388+
delc = 10.0
389+
top = 0
390+
botm = [-10, -20, -30]
391+
xoff = 100000000.0
392+
yoff = 100000000.0
393+
disvkwargs = get_disv_kwargs(nlay, nrow, ncol, delr, delc, top, botm, xoff, yoff)
394+
idomain = np.ones((nlay, nrow * ncol), dtype=int)
395+
idomain[0, 1] = 0
396+
disvkwargs["idomain"] = idomain
397+
398+
# build
399+
ws = function_tmpdir / "ws"
400+
for idx, (base_folder, test_info) in enumerate(tests.items()):
401+
print(f"RUNNING TEST: {base_folder}")
402+
data_path = os.path.join(data_path_base, base_folder, test_info["base_sim_dir"])
403+
# copy example data into working directory
404+
base_model_folder = os.path.join(ws, f"{base_folder}_base")
405+
test_model_folder = os.path.join(ws, f"{base_folder}_test")
406+
shutil.copytree(data_path, base_model_folder)
407+
408+
sim = flopy.mf6.MFSimulation(
409+
sim_name=name,
410+
version="mf6",
411+
exe_name="mf6",
412+
sim_ws=ws,
413+
)
414+
tdis = flopy.mf6.ModflowTdis(sim, start_date_time="2041-01-01t00:00:00-05:00")
415+
gwf = flopy.mf6.ModflowGwf(sim, modelname=name)
416+
ims = flopy.mf6.ModflowIms(sim, print_option="SUMMARY")
417+
disv = flopy.mf6.ModflowGwfdisv(gwf, **disvkwargs)
418+
ic = flopy.mf6.ModflowGwfic(gwf, strt=0.0)
419+
npf = flopy.mf6.ModflowGwfnpf(gwf)
420+
spd = {0: [[(0, 0), 1.0], [(0, nrow * ncol - 1), 0.0]]}
421+
chd = flopy.mf6.modflow.mfgwfchd.ModflowGwfchd(gwf, stress_period_data=spd)
422+
oc = flopy.mf6.ModflowGwfoc(
423+
gwf,
424+
head_filerecord=f"{name}.hds",
425+
saverecord=[("HEAD", "ALL")],
426+
)
427+
428+
sim.set_sim_path(test_model_folder)
429+
sim.write_simulation(netcdf=test_info["netcdf_type"])
430+
431+
# compare generated files
432+
gen_files = [
433+
f
434+
for f in os.listdir(test_model_folder)
435+
if os.path.isfile(os.path.join(test_model_folder, f))
436+
]
437+
base_files = [
438+
f
439+
for f in os.listdir(base_model_folder)
440+
if os.path.isfile(os.path.join(base_model_folder, f))
441+
]
442+
# assert len(gen_files) == len(base_files)
443+
for f in base_files:
444+
print(f"cmp => {f}")
445+
base = os.path.join(base_model_folder, f)
446+
gen = os.path.join(test_model_folder, f)
447+
if f != test_info["netcdf_output_file"]:
448+
with open(base, "r") as file1, open(gen, "r") as file2:
449+
# Skip first line
450+
next(file1)
451+
next(file2)
452+
453+
for line1, line2 in zip(file1, file2):
454+
assert line1 == line2
455+
else:
456+
# TODO compare nc files
457+
assert os.path.exists(gen)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# File generated by Flopy version 3.10.0.dev1 on 01/14/2025 at 13:13:00.
2+
BEGIN options
3+
END options
4+
5+
BEGIN dimensions
6+
MAXBOUND 2
7+
END dimensions
8+
9+
BEGIN period 1
10+
1 1 1.00000000E+00
11+
1 9 0.00000000E+00
12+
END period 1
13+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# NOT generated by Flopy
2+
BEGIN options
3+
END options
4+
5+
BEGIN dimensions
6+
NLAY 3
7+
NCPL 9
8+
NVERT 16
9+
END dimensions
10+
11+
BEGIN griddata
12+
top NETCDF
13+
botm NETCDF
14+
idomain NETCDF
15+
END griddata
16+
17+
BEGIN vertices
18+
1 1.00000000E+08 1.00000030E+08
19+
2 1.00000010E+08 1.00000030E+08
20+
3 1.00000020E+08 1.00000030E+08
21+
4 1.00000030E+08 1.00000030E+08
22+
5 1.00000000E+08 1.00000020E+08
23+
6 1.00000010E+08 1.00000020E+08
24+
7 1.00000020E+08 1.00000020E+08
25+
8 1.00000030E+08 1.00000020E+08
26+
9 1.00000000E+08 1.00000010E+08
27+
10 1.00000010E+08 1.00000010E+08
28+
11 1.00000020E+08 1.00000010E+08
29+
12 1.00000030E+08 1.00000010E+08
30+
13 1.00000000E+08 1.00000000E+08
31+
14 1.00000010E+08 1.00000000E+08
32+
15 1.00000020E+08 1.00000000E+08
33+
16 1.00000030E+08 1.00000000E+08
34+
END vertices
35+
36+
BEGIN cell2d
37+
1 1.00000005E+08 1.00000025E+08 4 1 2 6 5
38+
2 1.00000015E+08 1.00000025E+08 4 2 3 7 6
39+
3 1.00000025E+08 1.00000025E+08 4 3 4 8 7
40+
4 1.00000005E+08 1.00000015E+08 4 5 6 10 9
41+
5 1.00000015E+08 1.00000015E+08 4 6 7 11 10
42+
6 1.00000025E+08 1.00000015E+08 4 7 8 12 11
43+
7 1.00000005E+08 1.00000005E+08 4 9 10 14 13
44+
8 1.00000015E+08 1.00000005E+08 4 10 11 15 14
45+
9 1.00000025E+08 1.00000005E+08 4 11 12 16 15
46+
END cell2d
47+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# NOT generated by Flopy
2+
BEGIN options
3+
END options
4+
5+
BEGIN griddata
6+
strt NETCDF
7+
END griddata
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# File generated by Flopy version 3.10.0.dev1 on 01/14/2025 at 13:12:59.
2+
BEGIN options
3+
PRINT_OPTION summary
4+
END options
5+
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# NOT generated by Flopy
2+
BEGIN options
3+
NETCDF FILEIN disv01b.in.nc
4+
END options
5+
6+
BEGIN packages
7+
DISV6 disv01b.disv disv
8+
IC6 disv01b.ic ic
9+
NPF6 disv01b.npf npf
10+
CHD6 disv01b.chd chd_0
11+
OC6 disv01b.oc oc
12+
END packages
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# NOT generated by Flopy
2+
BEGIN options
3+
END options
4+
5+
BEGIN griddata
6+
icelltype NETCDF
7+
k NETCDF
8+
END griddata
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# File generated by Flopy version 3.10.0.dev1 on 01/14/2025 at 13:13:00.
2+
BEGIN options
3+
HEAD FILEOUT disv01b.hds
4+
END options
5+
6+
BEGIN period 1
7+
SAVE HEAD ALL
8+
END period 1
9+

0 commit comments

Comments
 (0)