From bc266fe0e240065eb18a6108fa525a67a68aed50 Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Tue, 5 Oct 2021 18:00:49 -0700 Subject: [PATCH 1/2] Fix(_mg_resync): Added checks to reset the modelgrid resync * update to allow model to resync modelgrid when discretization or bas package is modified * added test_mf6_modelgrid_update to t007_test.py --- autotest/t007_test.py | 42 ++++++++++++++++++++++++++++++++++++++++++ flopy/mf6/mfpackage.py | 6 ++++++ flopy/pakbase.py | 10 ++++++++++ 3 files changed, 58 insertions(+) diff --git a/autotest/t007_test.py b/autotest/t007_test.py index 3d11d454ad..e16a860534 100644 --- a/autotest/t007_test.py +++ b/autotest/t007_test.py @@ -650,6 +650,11 @@ def test_sr(): if mm.modelgrid.proj4 != "test test test": raise AssertionError() + mm.dis.top = 5000 + + if not np.allclose(mm.dis.top.array, mm.modelgrid.top): + raise AssertionError("modelgrid failed dynamic update test") + def test_dis_sr(): @@ -683,6 +688,42 @@ def test_dis_sr(): np.testing.assert_almost_equal(y, yul) +def test_mf6_modelgrid_update(): + base_dir = os.path.join("..", "examples", "data", "mf6") + # dis + model_ws = os.path.join(base_dir, "test001a_Tharmonic") + sim = flopy.mf6.MFSimulation.load(sim_ws=model_ws) + gwf = sim.get_model("flow15") + + mg = gwf.modelgrid + gwf.dis.top = 12 + + if not np.allclose(gwf.dis.top.array, gwf.modelgrid.top): + raise AssertionError("StructuredGrid failed dynamic update test") + + # disv + model_ws = os.path.join(base_dir, "test003_gwfs_disv") + sim = flopy.mf6.MFSimulation.load(sim_ws=model_ws) + gwf = sim.get_model("gwf_1") + + mg = gwf.modelgrid + gwf.disv.top = 6.12 + + if not np.allclose(gwf.disv.top.array, gwf.modelgrid.top): + raise AssertionError("VertexGrid failed dynamic update test") + + # disu + model_ws = os.path.join(base_dir, "test006_gwf3") + sim = flopy.mf6.MFSimulation.load(sim_ws=model_ws) + gwf = sim.get_model("gwf_1") + + mg = gwf.modelgrid + gwf.disu.top = 101 + + if not np.allclose(gwf.disu.top.array, gwf.modelgrid.top): + raise AssertionError("UnstructuredGrid failed dynamic update test") + + def test_twri_mg(): name = "twri.nam" ml = flopy.modflow.Modflow.load(name, model_ws=pth, check=False) @@ -1666,6 +1707,7 @@ def main(): # test_tricontour_NaN() # test_export_contourf() test_sr() + # test_mf6_modelgrid_update() # test_shapefile_polygon_closed() # test_mapview_plot_bc() # test_crosssection_plot_bc() diff --git a/flopy/mf6/mfpackage.py b/flopy/mf6/mfpackage.py index 9b10e39428..68ac1141aa 100644 --- a/flopy/mf6/mfpackage.py +++ b/flopy/mf6/mfpackage.py @@ -1614,6 +1614,12 @@ def __setattr__(self, name, value): package=self._get_pname(), ) return + + if all(hasattr(self, attr) for attr in ["model_or_sim", "_package_type"]): + if hasattr(self.model_or_sim, "_mg_resync"): + if not self.model_or_sim._mg_resync: + self.model_or_sim._mg_resync = self._mg_resync + super().__setattr__(name, value) def __repr__(self): diff --git a/flopy/pakbase.py b/flopy/pakbase.py index 610f94c13c..a628c865ae 100644 --- a/flopy/pakbase.py +++ b/flopy/pakbase.py @@ -78,6 +78,12 @@ def plottable(self): def has_stress_period_data(self): return self.__dict__.get("stress_period_data", None) is not None + @property + def _mg_resync(self): + if self.package_type.lower()[:4] in ("dis", "bas"): + return True + return False + @staticmethod def _check_thresholds(chk, array, active, thresholds, name): """Checks array against min and max threshold values.""" @@ -605,6 +611,10 @@ def __setattr__(self, key, value): ) value = new_list + if all(hasattr(self, attr) for attr in ["parent", "_name"]): + if not self.parent._mg_resync: + self.parent._mg_resync = self._mg_resync + super().__setattr__(key, value) @property From bf2c59aaa689de186bd4c80685532802ac7149cd Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Tue, 5 Oct 2021 18:02:41 -0700 Subject: [PATCH 2/2] linting --- flopy/mf6/mfpackage.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flopy/mf6/mfpackage.py b/flopy/mf6/mfpackage.py index 68ac1141aa..4eeeb6ca07 100644 --- a/flopy/mf6/mfpackage.py +++ b/flopy/mf6/mfpackage.py @@ -1615,7 +1615,9 @@ def __setattr__(self, name, value): ) return - if all(hasattr(self, attr) for attr in ["model_or_sim", "_package_type"]): + if all( + hasattr(self, attr) for attr in ["model_or_sim", "_package_type"] + ): if hasattr(self.model_or_sim, "_mg_resync"): if not self.model_or_sim._mg_resync: self.model_or_sim._mg_resync = self._mg_resync