Skip to content

Commit

Permalink
rebase with prt components, remove mock data and use real models for …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
wpbonelli committed May 23, 2024
1 parent e19eec2 commit e8531e3
Show file tree
Hide file tree
Showing 7 changed files with 656 additions and 307 deletions.
6 changes: 5 additions & 1 deletion autotest/test_mp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ def test_loadtxt(function_tmpdir, mp6_test_path):
pthld = PathlineFile(pthfile)
ra = loadtxt(pthfile, delimiter=" ", skiprows=3, dtype=pthld._dtype)
ra2 = loadtxt(
pthfile, delimiter=" ", skiprows=3, dtype=pthld._dtype, use_pandas=False
pthfile,
delimiter=" ",
skiprows=3,
dtype=pthld._dtype,
use_pandas=False,
)
assert np.array_equal(ra, ra2)

Expand Down
145 changes: 99 additions & 46 deletions autotest/test_plotutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from flopy.utils.modpathfile import PathlineFile as MpPathlineFile
from flopy.utils.prtfile import PathlineFile as PrtPathlineFile


nlay = 1
nrow = 10
ncol = 10
Expand Down Expand Up @@ -73,9 +72,7 @@ def gwf_sim(function_tmpdir):
pname="tdis",
time_units="DAYS",
nper=nper,
perioddata=[
(perlen, nstp, tsmult)
],
perioddata=[(perlen, nstp, tsmult)],
)

# create gwf model
Expand Down Expand Up @@ -131,9 +128,10 @@ def gwf_sim(function_tmpdir):


@pytest.fixture
def mp7_sim(gwf_sim, function_tmpdir):
def mp7_sim(gwf_sim):
gwf = gwf_sim.get_model()
mp7_ws = function_tmpdir / "mp7"
ws = gwf_sim.sim_path.parent
mp7_ws = ws / "mp7"
releasepts_mp7 = [
# node number, localx, localy, localz
(0, float(f"0.{i + 1}"), float(f"0.{i + 1}"), 0.5)
Expand Down Expand Up @@ -172,9 +170,10 @@ def mp7_sim(gwf_sim, function_tmpdir):


@pytest.fixture
def prt_sim(function_tmpdir):
gwf_ws = function_tmpdir / "gwf"
prt_ws = function_tmpdir / "prt"
def prt_sim(gwf_sim):
ws = gwf_sim.sim_path.parent
gwf_ws = ws / "gwf"
prt_ws = ws / "prt"
prt_name = "plotutil_prt"
gwf_name = "plotutil_gwf"
releasepts_prt = [
Expand Down Expand Up @@ -219,9 +218,7 @@ def prt_sim(function_tmpdir):
)

# create mip package
flopy.mf6.ModflowPrtmip(
prt, pname="mip", porosity=porosity
)
flopy.mf6.ModflowPrtmip(prt, pname="mip", porosity=porosity)

# create prp package
prp_track_file = f"{prt_name}.prp.trk"
Expand Down Expand Up @@ -276,8 +273,16 @@ def prt_sim(function_tmpdir):


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_pathlines(prt_sim, dataframe):
def test_to_mp7_pathlines(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

prt_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
if not dataframe:
prt_pls = prt_pls.to_records(index=False)
mp7_pls = to_mp7_pathlines(prt_pls)

assert (
Expand All @@ -291,13 +296,18 @@ def test_to_mp7_pathlines(prt_sim, dataframe):


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_pathlines_empty(dataframe):
def test_to_mp7_pathlines_empty(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

prt_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
prt_pls = (
pd.DataFrame.from_records(
[], columns=PrtPathlineFile.dtype.names
)
pd.DataFrame.from_records([], columns=prt_pls.dtypes)
if dataframe
else np.recarray((0,), dtype=PrtPathlineFile.dtype)
else np.recarray((0,), dtype=prt_pls.to_records(index=False).dtype)
)
mp7_pls = to_mp7_pathlines(prt_pls)

Expand All @@ -308,46 +318,69 @@ def test_to_mp7_pathlines_empty(dataframe):


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_pathlines_noop(mp7_sim, dataframe):
plf = flopy.utils.PathlineFile(Path(mp7_sim.model_ws) / f"{mp7_sim.name}.mppth")
og_pls = pd.DataFrame(
plf.get_destination_pathline_data(range(mp7_sim.modelgrid.nnodes), to_recarray=True)
def test_to_mp7_pathlines_noop(gwf_sim, mp7_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

mp7_sim.write_input()
mp7_sim.run_model()

plf = flopy.utils.PathlineFile(
Path(mp7_sim.model_ws) / f"{mp7_sim.name}.mppth"
)
og_pls = plf.get_destination_pathline_data(
range(gwf_sim.get_model().modelgrid.nnodes), to_recarray=True
)
if dataframe:
og_pls = pd.DataFrame(og_pls)
mp7_pls = to_mp7_pathlines(og_pls)

assert (
type(mp7_pls)
== type(mp7_pls)
== type(og_pls)
== (pd.DataFrame if dataframe else np.recarray)
)
assert set(
dict(mp7_pls.dtypes).keys() if dataframe else mp7_pls.dtype.names
) == set(MpPathlineFile.dtypes[7].names)
assert np.array_equal(
mp7_pls if dataframe else pd.DataFrame(mp7_pls), og_pls
pd.DataFrame(mp7_pls) if dataframe else mp7_pls, og_pls
)


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_endpoints(prt_sim, dataframe):
def test_to_mp7_endpoints(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

prt_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
prt_eps = prt_pls[prt_pls.ireason == 3]
mp7_eps = to_mp7_endpoints(mp7_eps)

if not dataframe:
prt_eps = prt_eps.to_records(index=False)
mp7_eps = to_mp7_endpoints(prt_eps)

assert np.isclose(mp7_eps.time[0], prt_eps.t.max())
assert set(
dict(mp7_eps.dtypes).keys() if dataframe else mp7_eps.dtype.names
) == set(MpEndpointFile.dtypes[7].names)


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_endpoints_empty(dataframe):
def test_to_mp7_endpoints_empty(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

prt_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
mp7_eps = to_mp7_endpoints(
pd.DataFrame.from_records(
[], columns=PrtPathlineFile.dtype.names
)
pd.DataFrame.from_records([], columns=prt_pls.dtypes)
if dataframe
else np.recarray((0,), dtype=PrtPathlineFile.dtype)
else np.recarray((0,), dtype=prt_pls.to_records(index=False).dtype)
)

assert mp7_eps.empty if dataframe else mp7_eps.size == 0
Expand All @@ -357,26 +390,41 @@ def test_to_mp7_endpoints_empty(dataframe):


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_mp7_endpoints_noop(mp7_sim, dataframe):
def test_to_mp7_endpoints_noop(gwf_sim, mp7_sim, dataframe):
"""Test a recarray or dataframe which already contains MP7 endpoint data"""
epf = flopy.utils.EndpointFile(Path(mp7_sim.model_ws) / f"{mp7_sim.name}.mpend")
og_eps = pd.DataFrame(
epf.get_destination_endpoint_data(range(mp7_sim.modelgrid.nnodes), to_recarray=True)

gwf_sim.write_simulation()
gwf_sim.run_simulation()

mp7_sim.write_input()
mp7_sim.run_model()

epf = flopy.utils.EndpointFile(
Path(mp7_sim.model_ws) / f"{mp7_sim.name}.mpend"
)
og_eps = epf.get_destination_endpoint_data(
range(gwf_sim.get_model().modelgrid.nnodes)
)
if dataframe:
og_eps = pd.DataFrame(og_eps)
mp7_eps = to_mp7_endpoints(og_eps)

assert np.array_equal(
mp7_eps if dataframe else pd.DataFrame(mp7_eps), og_eps
pd.DataFrame(mp7_eps) if dataframe else mp7_eps, og_eps
)


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_prt_pathlines_roundtrip(prt_sim, dataframe):
def test_to_prt_pathlines_roundtrip(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

og_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
mp7_pls = to_mp7_pathlines(
og_pls
if dataframe
else og_pls.to_records(index=False)
og_pls if dataframe else og_pls.to_records(index=False)
)
prt_pls = to_prt_pathlines(mp7_pls)

Expand All @@ -395,13 +443,18 @@ def test_to_prt_pathlines_roundtrip(prt_sim, dataframe):


@pytest.mark.parametrize("dataframe", [True, False])
def test_to_prt_pathlines_roundtrip_empty(dataframe):
def test_to_prt_pathlines_roundtrip_empty(gwf_sim, prt_sim, dataframe):
gwf_sim.write_simulation()
gwf_sim.run_simulation()

prt_sim.write_simulation()
prt_sim.run_simulation()

og_pls = pd.read_csv(prt_sim.sim_path / f"{prt_sim.name}.trk.csv")
og_pls = to_mp7_pathlines(
pd.DataFrame.from_records(
[], columns=PrtPathlineFile.dtype.names
)
pd.DataFrame.from_records([], columns=og_pls.dtypes)
if dataframe
else np.recarray((0,), dtype=PrtPathlineFile.dtype)
else np.recarray((0,), dtype=og_pls.to_records(index=False).dtype)
)
prt_pls = to_prt_pathlines(og_pls)

Expand Down
Loading

0 comments on commit e8531e3

Please sign in to comment.