From b0b2223670179b5270f10dfd99739f3deafaba10 Mon Sep 17 00:00:00 2001 From: Chris Nicol Date: Fri, 3 Dec 2021 06:39:16 +1100 Subject: [PATCH 1/4] fix(UnstructuredGrid) Loading an mf6 DISU model with variable voronoi grid results in extraneous iverts entries of None (Iverts appears to be a consistent (not jagged) shape). This results in a vertexdict indexing error in UnstructuredGrid._build_grid_geometry_info when using plot() methods. Filter out None entries from iverts in UnstructuredGrid._build_grid_geometry_info --- flopy/discretization/unstructuredgrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flopy/discretization/unstructuredgrid.py b/flopy/discretization/unstructuredgrid.py index 05983f3ede..e0fec65e4c 100644 --- a/flopy/discretization/unstructuredgrid.py +++ b/flopy/discretization/unstructuredgrid.py @@ -563,7 +563,7 @@ def _build_grid_geometry_info(self): xcellvert = [] ycellvert = [] - for ix in iverts: + for ix in (iv for iv in iverts if iv is not None): xcellvert.append(vertexdict[ix][0]) ycellvert.append(vertexdict[ix][1]) From 87d7e35cdd315524f4579b512c99684789db0aea Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Mon, 6 Dec 2021 14:41:47 -0800 Subject: [PATCH 2/4] update(MFModel, UnstructuredGrid): updates to filter None type from iverts recarrays --- autotest/t080_test.py | 53 ++++++++++++++++++++++++ flopy/discretization/unstructuredgrid.py | 7 ++-- flopy/mf6/mfmodel.py | 2 +- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/autotest/t080_test.py b/autotest/t080_test.py index 4aa8dd1d98..9e5675d95a 100644 --- a/autotest/t080_test.py +++ b/autotest/t080_test.py @@ -4,6 +4,7 @@ import os import platform +import numpy as np try: from shapely.geometry import Polygon @@ -128,5 +129,57 @@ def test_mfusg(): return +def test_usg_iverts(): + iverts = [ + [4, 3, 2, 1, 0, None], + [7, 0, 1, 6, 5, None], + [11, 10, 9, 8, 2, 3], + [1, 6, 13, 12, 8, 2], + [15, 14, 13, 6, 5, None], + [10, 9, 18, 17, 16, None], + [8, 12, 20, 19, 18, 9], + [22, 14, 13, 12, 20, 21], + [24, 17, 18, 19, 23, None], + [21, 20, 19, 23, 25, None] + ] + verts = [ + [0.0, 22.5], + [5.1072, 22.5], + [7.5, 24.0324], + [7.5, 30.0], + [0.0, 30.0], + [0.0, 7.5], + [4.684, 7.5], + [0.0, 15.0], + [14.6582, 21.588], + [22.5, 24.3766], + [22.5, 30.0], + [15.0, 30.0], + [15.3597, 8.4135], + [7.5, 5.6289], + [7.5, 0.0], + [0.0, 0.0], + [30.0, 30.0], + [30.0, 22.5], + [25.3285, 22.5], + [24.8977, 7.5], + [22.5, 5.9676], + [22.5, 0.0], + [15.0, 0.0], + [30.0, 7.5], + [30.0, 15.0], + [30.0, 0.0] + ] + + grid = flopy.discretization.UnstructuredGrid( + verts, iverts, ncpl=[len(iverts)] + ) + + iverts = grid.iverts + if any(None in l for l in iverts): + raise ValueError("None type should not be returned in iverts list") + + if __name__ == "__main__": test_mfusg() + test_usg_iverts() diff --git a/flopy/discretization/unstructuredgrid.py b/flopy/discretization/unstructuredgrid.py index e0fec65e4c..ab1283fb6b 100644 --- a/flopy/discretization/unstructuredgrid.py +++ b/flopy/discretization/unstructuredgrid.py @@ -200,7 +200,8 @@ def nvert(self): @property def iverts(self): - return self._iverts + if self._iverts is not None: + return [list(filter((None).__ne__, i)) for i in self._iverts] @property def verts(self): @@ -559,11 +560,11 @@ def _build_grid_geometry_info(self): yvertices = [] # build xy vertex and cell center info - for iverts in self._iverts: + for iverts in self.iverts: xcellvert = [] ycellvert = [] - for ix in (iv for iv in iverts if iv is not None): + for ix in iverts: xcellvert.append(vertexdict[ix][0]) ycellvert.append(vertexdict[ix][1]) diff --git a/flopy/mf6/mfmodel.py b/flopy/mf6/mfmodel.py index d12c3f570f..5c9035c95b 100644 --- a/flopy/mf6/mfmodel.py +++ b/flopy/mf6/mfmodel.py @@ -421,7 +421,7 @@ def modelgrid(self): xcenters = None ycenters = None else: - iverts = [list(i)[4:] for i in cell2d] + iverts = [list(filter((None).__ne__, i))[4:] for i in cell2d] xcenters = dis.cell2d.array["xc"] ycenters = dis.cell2d.array["yc"] vertices = dis.vertices.array From 771a965b2c1cf4edb395c3ba294b2ebf9319cacc Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Mon, 6 Dec 2021 14:49:33 -0800 Subject: [PATCH 3/4] updated local version of black and performed linting --- flopy/utils/gridintersect.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/flopy/utils/gridintersect.py b/flopy/utils/gridintersect.py index 6e3d9141ab..115eaf4ea0 100644 --- a/flopy/utils/gridintersect.py +++ b/flopy/utils/gridintersect.py @@ -45,7 +45,6 @@ def ignore_shapely_warnings_for_object_array(): ) yield - elif SHAPELY_LT_18 and NUMPY_GE_121: @contextlib.contextmanager @@ -58,7 +57,6 @@ def ignore_shapely_warnings_for_object_array(): ) yield - else: @contextlib.contextmanager From 4ff08603c7c94fd96eba874bc227ec8864da073a Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Tue, 7 Dec 2021 16:15:42 -0800 Subject: [PATCH 4/4] update(VertexGrid): added filter for None types on cell2d --- flopy/discretization/vertexgrid.py | 25 ++++++++++++++++++------- flopy/mf6/mfmodel.py | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/flopy/discretization/vertexgrid.py b/flopy/discretization/vertexgrid.py index 207bc972ed..75870012cc 100644 --- a/flopy/discretization/vertexgrid.py +++ b/flopy/discretization/vertexgrid.py @@ -125,7 +125,20 @@ def nvert(self): @property def iverts(self): - return [[t[0]] + list(t)[4:] for t in self._cell2d] + if self._cell2d is not None: + return [[t[0]] + list(t)[4:] for t in self.cell2d] + elif self._cell1d is not None: + return [[t[0]] + list(t)[3:] for t in self.cell1d] + + @property + def cell1d(self): + if self._cell1d is not None: + return [list(filter((None).__ne__, t)) for t in self._cell1d] + + @property + def cell2d(self): + if self._cell2d is not None: + return [list(filter((None).__ne__, t)) for t in self._cell2d] @property def verts(self): @@ -342,7 +355,7 @@ def _build_grid_geometry_info(self): zcenters = [] zvertices = [] vertexdict = {v[0]: [v[1], v[2], v[3]] for v in self._vertices} - for cell1d in self._cell1d: + for cell1d in self.cell1d: cell1d = tuple(cell1d) xcenters.append(cell1d[1]) ycenters.append(cell1d[2]) @@ -350,8 +363,7 @@ def _build_grid_geometry_info(self): vert_number = [] for i in cell1d[3:]: - if i is not None: - vert_number.append(int(i)) + vert_number.append(int(i)) xcellvert = [] ycellvert = [] @@ -367,15 +379,14 @@ def _build_grid_geometry_info(self): else: vertexdict = {v[0]: [v[1], v[2]] for v in self._vertices} # build xy vertex and cell center info - for cell2d in self._cell2d: + for cell2d in self.cell2d: cell2d = tuple(cell2d) xcenters.append(cell2d[1]) ycenters.append(cell2d[2]) vert_number = [] for i in cell2d[4:]: - if i is not None: - vert_number.append(int(i)) + vert_number.append(int(i)) xcellvert = [] ycellvert = [] diff --git a/flopy/mf6/mfmodel.py b/flopy/mf6/mfmodel.py index 5c9035c95b..d12c3f570f 100644 --- a/flopy/mf6/mfmodel.py +++ b/flopy/mf6/mfmodel.py @@ -421,7 +421,7 @@ def modelgrid(self): xcenters = None ycenters = None else: - iverts = [list(filter((None).__ne__, i))[4:] for i in cell2d] + iverts = [list(i)[4:] for i in cell2d] xcenters = dis.cell2d.array["xc"] ycenters = dis.cell2d.array["yc"] vertices = dis.vertices.array