Skip to content

Commit

Permalink
feat(mf2005,mf2k): allow loading custom flopy packages (#2404)
Browse files Browse the repository at this point in the history
Add extra_pkgs kwarg, which updates the mfnam_packages dictionary.
  • Loading branch information
dbrakenhoff authored Jan 10, 2025
1 parent 68883c4 commit f058122
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions autotest/test_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ def test_modflow_load(namfile, example_data_path):
assert model.model_ws == str(mpath)


def test_modflow_load_with_extra_pkg(example_data_path):
namfile = Path("freyberg") / "freyberg.nam"
mpath = Path(example_data_path / namfile).parent

# extra pkg
dummy_extra_pkg_for_test = {"DUM": "Dummy"}

model = Modflow.load(
mpath / namfile.name,
verbose=True,
model_ws=mpath,
extra_pkgs=dummy_extra_pkg_for_test,
)
assert model.mfnam_packages["DUM"] == "Dummy"


@pytest.mark.parametrize(
"path,expected",
[
Expand Down
12 changes: 12 additions & 0 deletions flopy/modflow/mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Modflow(BaseModel):
Location for external files.
verbose : bool, default False
Print additional information to the screen.
extra_pkgs : dict, optional
Add custom packages classes to mfnam_packages. Allows for loading models
with custom packages not contained in the standard flopy distribution.
Attributes
----------
Expand Down Expand Up @@ -113,6 +116,7 @@ def __init__(
model_ws: Union[str, os.PathLike] = os.curdir,
external_path: Optional[Union[str, os.PathLike]] = None,
verbose=False,
extra_pkgs: Optional[dict] = None,
**kwargs,
):
super().__init__(
Expand Down Expand Up @@ -224,6 +228,8 @@ def __init__(
"vdf": flopy.seawat.SeawatVdf,
"vsc": flopy.seawat.SeawatVsc,
}
if extra_pkgs:
self.mfnam_packages.update(extra_pkgs)

def __repr__(self):
nrow, ncol, nlay, nper = self.get_nrow_ncol_nlay_nper()
Expand Down Expand Up @@ -632,6 +638,7 @@ def load(
load_only=None,
forgive=False,
check=True,
extra_pkgs: Optional[dict] = None,
):
"""
Load an existing MODFLOW model.
Expand Down Expand Up @@ -661,6 +668,10 @@ def load(
useful for debugging. Default False.
check : boolean, optional
Check model input for common errors. Default True.
extra_pkgs : dict, optional
Add custom packages classes to mfnam_packages. Allows for loading models
with custom packages not contained in the standard flopy distribution.
Returns
-------
Expand Down Expand Up @@ -692,6 +703,7 @@ def load(
exe_name=exe_name,
verbose=verbose,
model_ws=model_ws,
extra_pkgs=extra_pkgs,
**attribs,
)

Expand Down

0 comments on commit f058122

Please # to comment.