Skip to content

Commit

Permalink
baseline tests check ascii parity
Browse files Browse the repository at this point in the history
  • Loading branch information
mjreno authored and mjreno committed Jan 14, 2025
1 parent 1a3ddf2 commit 75b09d6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 53 deletions.
98 changes: 72 additions & 26 deletions autotest/regression/test_mf6_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,32 @@ def test_load_netcdf_gwfsto01(function_tmpdir, example_data_path):
next(file2)

for line1, line2 in zip(file1, file2):
assert line1 == line2
assert line1.lower() == line2.lower()
else:
# TODO compare nc files
assert os.path.exists(gen)
continue


@requires_pkg("xarray")
@requires_exe("mf6")
@pytest.mark.regression
def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
xr = import_optional_dependency("xarray")

cases = ["structured", "mesh2d"]
data_path_base = example_data_path / "mf6" / "netcdf"
tests = {
"test_gwf_sto01_mesh": TestInfo(
"gwf_sto01",
"gwf_sto01_write",
"gwf_sto01.ugrid.nc",
),
"test_gwf_sto01_structured": TestInfo(
"gwf_sto01",
"gwf_sto01_write",
"gwf_sto01.structured.nc",
),
}
name = "gwf_sto01"
cases = ["mesh2d", "structured"]

# static model data
# temporal discretization
Expand Down Expand Up @@ -174,17 +186,29 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
ske = [6e-4, 3e-4, 6e-4]

# build
for name in cases:
# name = cases[0]
ws = function_tmpdir / name
ws = function_tmpdir / "ws"
for idx, (base_folder, test_info) in enumerate(tests.items()):
print(f"RUNNING TEST: {base_folder}")
netcdf = cases[idx]
data_path = os.path.join(
data_path_base, base_folder, test_info.original_simulation_folder
)
# copy example data into working directory
base_model_folder = os.path.join(ws, f"{base_folder}_base")
test_model_folder = os.path.join(ws, f"{base_folder}_test")
shutil.copytree(data_path, base_model_folder)

# build MODFLOW 6 files
sim = flopy.mf6.MFSimulation(
sim_name=name, version="mf6", exe_name="mf6", sim_ws=ws
)
# create tdis package
tdis = flopy.mf6.ModflowTdis(
sim, time_units="DAYS", nper=nper, perioddata=tdis_rc
sim,
time_units="DAYS",
start_date_time="2041-01-01t00:00:00-05:00",
nper=nper,
perioddata=tdis_rc,
)

# create gwf model
Expand Down Expand Up @@ -276,21 +300,43 @@ def test_create_netcdf_gwfsto01(function_tmpdir, example_data_path):
printrecord=[("HEAD", "LAST"), ("BUDGET", "ALL")],
)

sim.write_simulation(netcdf=name)

try:
success, buff = flopy.run_model(
"mf6",
ws / "mfsim.nam",
model_ws=ws,
report=True,
)
except Exception:
warn(
"MODFLOW 6 serial test",
name,
f"failed with error:\n{format_exc()}",
)
success = False

assert success
sim.set_sim_path(test_model_folder)
sim.write_simulation(netcdf=netcdf)

# compare generated files
gen_files = [
f
for f in os.listdir(test_model_folder)
if os.path.isfile(os.path.join(test_model_folder, f))
]
base_files = [
f
for f in os.listdir(base_model_folder)
if os.path.isfile(os.path.join(base_model_folder, f))
]
# assert len(gen_files) == len(base_files)
for f in base_files:
print(f"cmp => {f}")
base = os.path.join(base_model_folder, f)
gen = os.path.join(test_model_folder, f)
if f != test_info.netcdf_output_file:
with open(base, "r") as file1, open(gen, "r") as file2:
# Skip first line
next(file1)
next(file2)

for line1, line2 in zip(file1, file2):
if "netcdf filein" in line1.lower():
if netcdf == "mesh2d":
suffix = "ugrid"
else:
suffix = netcdf
assert line1 == f" NETCDF FILEIN gwf_sto01.{suffix}.nc\n"
assert line2 == " NETCDF FILEIN gwf_sto01.in.nc\n"
else:
assert line1 == line2
else:
# TODO compare nc files
assert os.path.exists(
os.path.join(test_model_folder, "gwf_sto01.in.nc")
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ BEGIN options
END options

BEGIN packages
dis6 gwf_sto01.dis dis
ic6 gwf_sto01.ic ic
npf6 gwf_sto01.npf npf
sto6 gwf_sto01.sto sto
rch6 gwf_sto01.rcha rcha_0
wel6 gwf_sto01.wel wel_0
chd6 gwf_sto01.chd chd_0
oc6 gwf_sto01.oc oc
DIS6 gwf_sto01.dis dis
IC6 gwf_sto01.ic ic
NPF6 gwf_sto01.npf npf
STO6 gwf_sto01.sto sto
RCH6 gwf_sto01.rcha rcha_0
WEL6 gwf_sto01.wel wel_0
CHD6 gwf_sto01.chd chd_0
OC6 gwf_sto01.oc oc
END packages
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
BEGIN options
BUDGET FILEOUT gwf_sto01.cbc
HEAD FILEOUT gwf_sto01.hds
HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 general
HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 GENERAL
END options

BEGIN period 1
SAVE head all
SAVE budget all
PRINT head all
PRINT budget all
SAVE HEAD ALL
SAVE BUDGET ALL
PRINT HEAD LAST
PRINT BUDGET ALL
END period 1

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ BEGIN options
END options

BEGIN packages
dis6 gwf_sto01.dis dis
ic6 gwf_sto01.ic ic
npf6 gwf_sto01.npf npf
sto6 gwf_sto01.sto sto
rch6 gwf_sto01.rcha rcha_0
wel6 gwf_sto01.wel wel_0
chd6 gwf_sto01.chd chd_0
oc6 gwf_sto01.oc oc
DIS6 gwf_sto01.dis dis
IC6 gwf_sto01.ic ic
NPF6 gwf_sto01.npf npf
STO6 gwf_sto01.sto sto
RCH6 gwf_sto01.rcha rcha_0
WEL6 gwf_sto01.wel wel_0
CHD6 gwf_sto01.chd chd_0
OC6 gwf_sto01.oc oc
END packages
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
BEGIN options
BUDGET FILEOUT gwf_sto01.cbc
HEAD FILEOUT gwf_sto01.hds
HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 general
HEAD PRINT_FORMAT COLUMNS 10 WIDTH 15 DIGITS 6 GENERAL
END options

BEGIN period 1
SAVE head all
SAVE budget all
PRINT head last
PRINT budget all
SAVE HEAD ALL
SAVE BUDGET ALL
PRINT HEAD LAST
PRINT BUDGET ALL
END period 1

2 changes: 1 addition & 1 deletion flopy/utils/model_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _create_layered_var(
):
try:
for layer in range(data.shape[0]):
layer_vname = f"{varname}_l{layer+1}"
layer_vname = f"{varname}_l{layer + 1}"
var_d = {layer_vname: (nc_shape, data[layer].flatten())}
self._dataset = self._dataset.assign(var_d)
self._dataset[layer_vname].attrs["modflow6_input"] = tag
Expand Down

0 comments on commit 75b09d6

Please sign in to comment.