Skip to content

Commit

Permalink
fix(mbase): warn if duplicate pkgs or units (#1964)
Browse files Browse the repository at this point in the history
* consistent with MFSimulation verbosity defaults
* use category=UserWarning for mbase.py warnings
* closes #1817
  • Loading branch information
wpbonelli committed Sep 30, 2023
1 parent 3522dce commit b848f96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
42 changes: 19 additions & 23 deletions autotest/test_mp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,7 @@ def test_pathline_plotting(function_tmpdir):


@requires_exe("mf6", "mp7")
@pytest.mark.parametrize("verbose", [True, False])
def test_mp7sim_replacement(function_tmpdir, capfd, verbose):
def test_mp7sim_replacement(function_tmpdir, capfd):
mf6sim = Mp7Cases.mf6(function_tmpdir)
mf6sim.write_simulation()
mf6sim.run_simulation()
Expand All @@ -911,7 +910,6 @@ def test_mp7sim_replacement(function_tmpdir, capfd, verbose):
flowmodel=mf6sim.get_model(mf6sim.name),
exe_name="mp7",
model_ws=mf6sim.sim_path,
verbose=verbose,
)
defaultiface6 = {"RCH": 6, "EVT": 6}
mpbas = Modpath7Bas(mp, porosity=0.1, defaultiface=defaultiface6)
Expand All @@ -931,27 +929,25 @@ def test_mp7sim_replacement(function_tmpdir, capfd, verbose):
zones=Mp7Cases.zones,
particlegroups=Mp7Cases.particlegroups,
)
# add a duplicate mp7sim package
mpsim = Modpath7Sim(
mp,
simulationtype="combined",
trackingdirection="forward",
weaksinkoption="pass_through",
weaksourceoption="pass_through",
budgetoutputoption="summary",
budgetcellnumbers=[1049, 1259],
traceparticledata=[1, 1000],
referencetime=[0, 0, 0.0],
stoptimeoption="extend",
timepointdata=[500, 1000.0],
zonedataoption="on",
zones=Mp7Cases.zones,
particlegroups=Mp7Cases.particlegroups,
)

cap = capfd.readouterr()
msg = "Two packages of the same type"
assert verbose == (msg in cap.out)
# add a duplicate mp7sim package
with pytest.warns(UserWarning, match="Two packages of the same type"):
mpsim = Modpath7Sim(
mp,
simulationtype="combined",
trackingdirection="forward",
weaksinkoption="pass_through",
weaksourceoption="pass_through",
budgetoutputoption="summary",
budgetcellnumbers=[1049, 1259],
traceparticledata=[1, 1000],
referencetime=[0, 0, 0.0],
stoptimeoption="extend",
timepointdata=[500, 1000.0],
zonedataoption="on",
zones=Mp7Cases.zones,
particlegroups=Mp7Cases.particlegroups,
)

mp.write_input()
success, buff = mp.run_model()
Expand Down
24 changes: 13 additions & 11 deletions flopy/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def _resolve(exe_name):
if exe_path is None:
if forgive:
warn(
f"The program {exe_name} does not exist or is not executable."
f"The program {exe_name} does not exist or is not executable.",
category=UserWarning,
)
return None

Expand Down Expand Up @@ -416,7 +417,8 @@ def __init__(
except:
warn(
f"\n{model_ws} not valid, "
f"workspace-folder was changed to {os.getcwd()}\n"
f"workspace-folder was changed to {os.getcwd()}\n",
category=UserWarning,
)
model_ws = os.getcwd()
self._model_ws = str(model_ws)
Expand Down Expand Up @@ -647,20 +649,20 @@ def add_package(self, p):
pn = p.name[idx]
except:
pn = p.name
if self.verbose:
print(
f"\nWARNING:\n unit {u} of package {pn} already in use."
)
warn(
f"Unit {u} of package {pn} already in use.",
category=UserWarning,
)
self.package_units.append(u)
for i, pp in enumerate(self.packagelist):
if pp.allowDuplicates:
continue
elif isinstance(p, type(pp)):
if self.verbose:
print(
"\nWARNING:\n Two packages of the same type, "
f"Replacing existing '{p.name[0]}' package."
)
warn(
"Two packages of the same type, "
f"Replacing existing '{p.name[0]}' package.",
category=UserWarning,
)
self.packagelist[i] = p
return
if self.verbose:
Expand Down

0 comments on commit b848f96

Please sign in to comment.