Skip to content

Commit 86f462b

Browse files
committed
refactor grid tests without pytest-cases
1 parent 7124e6a commit 86f462b

File tree

2 files changed

+148
-107
lines changed

2 files changed

+148
-107
lines changed

autotest/test_grid.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from matplotlib import pyplot as plt
1414
from modflow_devtools.markers import requires_exe, requires_pkg
1515
from modflow_devtools.misc import has_pkg
16-
from pytest_cases import parametrize_with_cases
1716

1817
from flopy.discretization import StructuredGrid, UnstructuredGrid, VertexGrid
1918
from flopy.mf6 import MFSimulation
@@ -1090,7 +1089,17 @@ def test_voronoi_vertex_grid(function_tmpdir):
10901089
@flaky
10911090
@requires_exe("triangle")
10921091
@requires_pkg("shapely", "scipy")
1093-
@parametrize_with_cases("grid_info", cases=GridCases, prefix="voronoi")
1092+
@pytest.mark.parametrize(
1093+
"grid_info",
1094+
[
1095+
GridCases.voronoi_polygon(),
1096+
GridCases.voronoi_rectangle(),
1097+
GridCases.voronoi_circle(),
1098+
GridCases.voronoi_nested_circles(),
1099+
GridCases.voronoi_polygons(),
1100+
GridCases.voronoi_many_polygons(),
1101+
],
1102+
)
10941103
def test_voronoi_grid(request, function_tmpdir, grid_info):
10951104
name = (
10961105
request.node.name.replace("/", "_")
@@ -1242,8 +1251,8 @@ def test_unstructured_neighbors(unstructured_grid):
12421251
assert np.allclose(queen_neighbors, [0, 10, 1, 6, 11, 2, 3, 7, 8, 12, 13])
12431252

12441253

1245-
@parametrize_with_cases("grid", cases=GridCases, prefix="structured_cbd")
1246-
def test_structured_ncb_thickness(grid):
1254+
def test_structured_ncb_thickness():
1255+
grid = GridCases.structured_cbd_small()
12471256
thickness = grid.cell_thickness
12481257

12491258
assert thickness.shape[0] == grid.nlay + np.count_nonzero(
@@ -1265,29 +1274,40 @@ def test_structured_ncb_thickness(grid):
12651274
), "saturated_thickness is not properly indexing confining beds"
12661275

12671276

1268-
@parametrize_with_cases("grid", cases=GridCases, prefix="unstructured")
1277+
@pytest.mark.parametrize(
1278+
"grid", [GridCases.unstructured_small(), GridCases.unstructured_medium()]
1279+
)
12691280
def test_unstructured_iverts(grid):
12701281
iverts = grid.iverts
12711282
assert not any(
12721283
None in l for l in iverts
12731284
), "None type should not be returned in iverts list"
12741285

12751286

1276-
@parametrize_with_cases("grid", cases=GridCases, prefix="structured")
1287+
@pytest.mark.parametrize(
1288+
"grid", [GridCases.structured_small(), GridCases.structured_cbd_small()]
1289+
)
12771290
def test_get_lni_structured(grid):
12781291
for nn in range(0, grid.nnodes):
12791292
layer, i = grid.get_lni([nn])[0]
12801293
assert layer * grid.ncpl + i == nn
12811294

12821295

1283-
@parametrize_with_cases("grid", cases=GridCases, prefix="vertex")
1296+
@pytest.mark.parametrize(
1297+
"grid",
1298+
[
1299+
GridCases.vertex_small(),
1300+
],
1301+
)
12841302
def test_get_lni_vertex(grid):
12851303
for nn in range(0, grid.nnodes):
12861304
layer, i = grid.get_lni([nn])[0]
12871305
assert layer * grid.ncpl + i == nn
12881306

12891307

1290-
@parametrize_with_cases("grid", cases=GridCases, prefix="unstructured")
1308+
@pytest.mark.parametrize(
1309+
"grid", [GridCases.unstructured_small(), GridCases.unstructured_medium()]
1310+
)
12911311
def test_get_lni_unstructured(grid):
12921312
for nn in range(0, grid.nnodes):
12931313
layer, i = grid.get_lni([nn])[0]

autotest/test_grid_cases.py

Lines changed: 120 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from pathlib import Path
2+
from tempfile import TemporaryDirectory
3+
14
import numpy as np
25

36
from flopy.discretization import StructuredGrid, UnstructuredGrid, VertexGrid
@@ -6,7 +9,8 @@
69

710

811
class GridCases:
9-
def structured_small(self):
12+
@staticmethod
13+
def structured_small():
1014
nlay, nrow, ncol = 3, 2, 3
1115
delc = 1.0 * np.ones(nrow, dtype=float)
1216
delr = 1.0 * np.ones(ncol, dtype=float)
@@ -27,7 +31,8 @@ def structured_small(self):
2731
idomain=idomain,
2832
)
2933

30-
def structured_cbd_small(self):
34+
@staticmethod
35+
def structured_cbd_small():
3136
nlay = 3
3237
nrow = ncol = 15
3338
laycbd = np.array([1, 2, 0], dtype=int)
@@ -61,7 +66,8 @@ def structured_cbd_small(self):
6166
laycbd=laycbd,
6267
)
6368

64-
def vertex_small(self):
69+
@staticmethod
70+
def vertex_small():
6571
nlay, ncpl = 3, 5
6672
vertices = [
6773
[0, 0.0, 3.0],
@@ -99,7 +105,8 @@ def vertex_small(self):
99105
idomain=idomain,
100106
)
101107

102-
def unstructured_small(self):
108+
@staticmethod
109+
def unstructured_small():
103110
nlay = 3
104111
ncpl = [5, 5, 5]
105112
vertices = [
@@ -167,7 +174,8 @@ def unstructured_small(self):
167174
idomain=idomain.flatten(),
168175
)
169176

170-
def unstructured_medium(self):
177+
@staticmethod
178+
def unstructured_medium():
171179
iverts = [
172180
[4, 3, 2, 1, 0, None],
173181
[7, 0, 1, 6, 5, None],
@@ -211,7 +219,8 @@ def unstructured_medium(self):
211219

212220
return UnstructuredGrid(verts, iverts, ncpl=[len(iverts)])
213221

214-
def voronoi_polygon(self, function_tmpdir):
222+
@staticmethod
223+
def voronoi_polygon():
215224
ncpl = 3803
216225
domain = [
217226
[1831.381546, 6335.543757],
@@ -237,18 +246,20 @@ def voronoi_polygon(self, function_tmpdir):
237246
max_area = 100.0**2
238247
angle = 30
239248

240-
tri = Triangle(
241-
maximum_area=max_area, angle=angle, model_ws=str(function_tmpdir)
242-
)
243-
tri.add_polygon(poly)
244-
tri.build(verbose=False)
245-
vor = VoronoiGrid(tri)
246-
gridprops = vor.get_gridprops_vertexgrid()
247-
grid = VertexGrid(**gridprops, nlay=1)
249+
with TemporaryDirectory() as tempdir:
250+
tri = Triangle(
251+
maximum_area=max_area, angle=angle, model_ws=str(Path(tempdir))
252+
)
253+
tri.add_polygon(poly)
254+
tri.build(verbose=False)
255+
vor = VoronoiGrid(tri)
256+
gridprops = vor.get_gridprops_vertexgrid()
257+
grid = VertexGrid(**gridprops, nlay=1)
248258

249259
return ncpl, vor, gridprops, grid
250260

251-
def voronoi_rectangle(self, function_tmpdir):
261+
@staticmethod
262+
def voronoi_rectangle():
252263
ncpl = 1679
253264
xmin = 0.0
254265
xmax = 2.0
@@ -260,18 +271,20 @@ def voronoi_rectangle(self, function_tmpdir):
260271
max_area = 0.001
261272
angle = 30
262273

263-
tri = Triangle(
264-
maximum_area=max_area, angle=angle, model_ws=str(function_tmpdir)
265-
)
266-
tri.add_polygon(poly)
267-
tri.build(verbose=False)
268-
vor = VoronoiGrid(tri)
269-
gridprops = vor.get_gridprops_vertexgrid()
270-
grid = VertexGrid(**gridprops, nlay=1)
274+
with TemporaryDirectory() as tempdir:
275+
tri = Triangle(
276+
maximum_area=max_area, angle=angle, model_ws=str(Path(tempdir))
277+
)
278+
tri.add_polygon(poly)
279+
tri.build(verbose=False)
280+
vor = VoronoiGrid(tri)
281+
gridprops = vor.get_gridprops_vertexgrid()
282+
grid = VertexGrid(**gridprops, nlay=1)
271283

272284
return ncpl, vor, gridprops, grid
273285

274-
def voronoi_circle(self, function_tmpdir):
286+
@staticmethod
287+
def voronoi_circle():
275288
ncpl = 538
276289
theta = np.arange(0.0, 2 * np.pi, 0.2)
277290
radius = 100.0
@@ -281,18 +294,20 @@ def voronoi_circle(self, function_tmpdir):
281294
max_area = 50
282295
angle = 30
283296

284-
tri = Triangle(
285-
maximum_area=max_area, angle=angle, model_ws=str(function_tmpdir)
286-
)
287-
tri.add_polygon(poly)
288-
tri.build(verbose=False)
289-
vor = VoronoiGrid(tri)
290-
gridprops = vor.get_gridprops_vertexgrid()
291-
grid = VertexGrid(**gridprops, nlay=1)
297+
with TemporaryDirectory() as tempdir:
298+
tri = Triangle(
299+
maximum_area=max_area, angle=angle, model_ws=str(Path(tempdir))
300+
)
301+
tri.add_polygon(poly)
302+
tri.build(verbose=False)
303+
vor = VoronoiGrid(tri)
304+
gridprops = vor.get_gridprops_vertexgrid()
305+
grid = VertexGrid(**gridprops, nlay=1)
292306

293307
return ncpl, vor, gridprops, grid
294308

295-
def voronoi_nested_circles(self, function_tmpdir):
309+
@staticmethod
310+
def voronoi_nested_circles():
296311
ncpl = 300
297312

298313
theta = np.arange(0.0, 2 * np.pi, 0.2)
@@ -311,86 +326,92 @@ def voronoi_nested_circles(self, function_tmpdir):
311326
max_area = 100
312327
angle = 30
313328

314-
tri = Triangle(
315-
maximum_area=max_area, angle=angle, model_ws=str(function_tmpdir)
316-
)
317-
for poly in polys:
318-
tri.add_polygon(poly)
319-
tri.add_hole((25, 25))
320-
tri.build(verbose=False)
321-
vor = VoronoiGrid(tri)
322-
gridprops = vor.get_gridprops_vertexgrid()
323-
grid = VertexGrid(**gridprops, nlay=1)
329+
with TemporaryDirectory() as tempdir:
330+
tri = Triangle(
331+
maximum_area=max_area, angle=angle, model_ws=str(Path(tempdir))
332+
)
333+
for poly in polys:
334+
tri.add_polygon(poly)
335+
tri.add_hole((25, 25))
336+
tri.build(verbose=False)
337+
vor = VoronoiGrid(tri)
338+
gridprops = vor.get_gridprops_vertexgrid()
339+
grid = VertexGrid(**gridprops, nlay=1)
324340

325341
return ncpl, vor, gridprops, grid
326342

327-
def voronoi_polygons(self, function_tmpdir):
343+
@staticmethod
344+
def voronoi_polygons():
328345
ncpl = 410
329346
active_domain = [(0, 0), (100, 0), (100, 100), (0, 100)]
330347
area1 = [(10, 10), (40, 10), (40, 40), (10, 40)]
331348
area2 = [(60, 60), (80, 60), (80, 80), (60, 80)]
332-
tri = Triangle(angle=30, model_ws=str(function_tmpdir))
333-
tri.add_polygon(active_domain)
334-
tri.add_polygon(area1)
335-
tri.add_polygon(area2)
336-
tri.add_region(
337-
(1, 1), 0, maximum_area=100
338-
) # point inside active domain
339-
tri.add_region((11, 11), 1, maximum_area=10) # point inside area1
340-
tri.add_region((61, 61), 2, maximum_area=3) # point inside area2
341-
tri.build(verbose=False)
342-
vor = VoronoiGrid(tri)
343-
gridprops = vor.get_gridprops_vertexgrid()
344-
grid = VertexGrid(**gridprops, nlay=1)
349+
350+
with TemporaryDirectory() as tempdir:
351+
tri = Triangle(angle=30, model_ws=str(Path(tempdir)))
352+
tri.add_polygon(active_domain)
353+
tri.add_polygon(area1)
354+
tri.add_polygon(area2)
355+
tri.add_region(
356+
(1, 1), 0, maximum_area=100
357+
) # point inside active domain
358+
tri.add_region((11, 11), 1, maximum_area=10) # point inside area1
359+
tri.add_region((61, 61), 2, maximum_area=3) # point inside area2
360+
tri.build(verbose=False)
361+
vor = VoronoiGrid(tri)
362+
gridprops = vor.get_gridprops_vertexgrid()
363+
grid = VertexGrid(**gridprops, nlay=1)
345364

346365
return ncpl, vor, gridprops, grid
347366

348-
def voronoi_many_polygons(self, function_tmpdir):
367+
@staticmethod
368+
def voronoi_many_polygons():
349369
ncpl = 1305
350370
active_domain = [(0, 0), (100, 0), (100, 100), (0, 100)]
351371
area1 = [(10, 10), (40, 10), (40, 40), (10, 40)]
352372
area2 = [(70, 70), (90, 70), (90, 90), (70, 90)]
353373

354-
tri = Triangle(angle=30, model_ws=str(function_tmpdir))
355-
356-
# requirement that active_domain is first polygon to be added
357-
tri.add_polygon(active_domain)
358-
359-
# requirement that any holes be added next
360-
theta = np.arange(0.0, 2 * np.pi, 0.2)
361-
radius = 10.0
362-
x = radius * np.cos(theta) + 50.0
363-
y = radius * np.sin(theta) + 70.0
364-
circle_poly0 = [(x, y) for x, y in zip(x, y)]
365-
tri.add_polygon(circle_poly0)
366-
tri.add_hole((50, 70))
367-
368-
# Add a polygon to force cells to conform to it
369-
theta = np.arange(0.0, 2 * np.pi, 0.2)
370-
radius = 10.0
371-
x = radius * np.cos(theta) + 70.0
372-
y = radius * np.sin(theta) + 20.0
373-
circle_poly1 = [(x, y) for x, y in zip(x, y)]
374-
tri.add_polygon(circle_poly1)
375-
# tri.add_hole((70, 20))
376-
377-
# add line through domain to force conforming cells
378-
line = [(x, x) for x in np.linspace(11, 89, 100)]
379-
tri.add_polygon(line)
380-
381-
# then regions and other polygons should follow
382-
tri.add_polygon(area1)
383-
tri.add_polygon(area2)
384-
tri.add_region(
385-
(1, 1), 0, maximum_area=100
386-
) # point inside active domain
387-
tri.add_region((11, 11), 1, maximum_area=10) # point inside area1
388-
tri.add_region((70, 70), 2, maximum_area=1) # point inside area2
389-
390-
tri.build(verbose=False)
391-
392-
vor = VoronoiGrid(tri)
393-
gridprops = vor.get_gridprops_vertexgrid()
394-
grid = VertexGrid(**gridprops, nlay=1)
374+
with TemporaryDirectory() as tempdir:
375+
tri = Triangle(angle=30, model_ws=str(Path(tempdir)))
376+
377+
# requirement that active_domain is first polygon to be added
378+
tri.add_polygon(active_domain)
379+
380+
# requirement that any holes be added next
381+
theta = np.arange(0.0, 2 * np.pi, 0.2)
382+
radius = 10.0
383+
x = radius * np.cos(theta) + 50.0
384+
y = radius * np.sin(theta) + 70.0
385+
circle_poly0 = [(x, y) for x, y in zip(x, y)]
386+
tri.add_polygon(circle_poly0)
387+
tri.add_hole((50, 70))
388+
389+
# Add a polygon to force cells to conform to it
390+
theta = np.arange(0.0, 2 * np.pi, 0.2)
391+
radius = 10.0
392+
x = radius * np.cos(theta) + 70.0
393+
y = radius * np.sin(theta) + 20.0
394+
circle_poly1 = [(x, y) for x, y in zip(x, y)]
395+
tri.add_polygon(circle_poly1)
396+
# tri.add_hole((70, 20))
397+
398+
# add line through domain to force conforming cells
399+
line = [(x, x) for x in np.linspace(11, 89, 100)]
400+
tri.add_polygon(line)
401+
402+
# then regions and other polygons should follow
403+
tri.add_polygon(area1)
404+
tri.add_polygon(area2)
405+
tri.add_region(
406+
(1, 1), 0, maximum_area=100
407+
) # point inside active domain
408+
tri.add_region((11, 11), 1, maximum_area=10) # point inside area1
409+
tri.add_region((70, 70), 2, maximum_area=1) # point inside area2
410+
411+
tri.build(verbose=False)
412+
413+
vor = VoronoiGrid(tri)
414+
gridprops = vor.get_gridprops_vertexgrid()
415+
grid = VertexGrid(**gridprops, nlay=1)
395416

396417
return ncpl, vor, gridprops, grid

0 commit comments

Comments
 (0)