Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/scottrp/flopy into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrp committed Dec 4, 2023
2 parents 54154cb + 788a8df commit 290fb31
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 102 deletions.
1 change: 0 additions & 1 deletion .docs/Notebooks/gridgen_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

# +
import os
import shutil
import sys
from tempfile import TemporaryDirectory

Expand Down
135 changes: 99 additions & 36 deletions autotest/test_gridgen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from shutil import copyfile, which
from shutil import which

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -11,12 +11,13 @@
from modflow_devtools.misc import has_pkg

import flopy
from flopy.discretization.unstructuredgrid import UnstructuredGrid
from flopy.discretization.vertexgrid import VertexGrid
from flopy.utils.gridgen import Gridgen


@requires_exe("gridgen")
def test_ctor_accepts_path_or_string(function_tmpdir):
def test_ctor_accepts_path_or_string_model_ws(function_tmpdir):
grid = GridCases().structured_small()

g = Gridgen(grid, model_ws=function_tmpdir)
Expand All @@ -26,12 +27,15 @@ def test_ctor_accepts_path_or_string(function_tmpdir):
assert g.model_ws == function_tmpdir


def base_grid():
def get_structured_grid():
"""Get a small version of the first grid in:
.docs/Notebooks/gridgen_example.py"""

Lx = 100.0
Ly = 100.0
nlay = 2
nrow = 51
ncol = 51
nrow = 11
ncol = 11
delr = Lx / ncol
delc = Ly / nrow
h0 = 10
Expand All @@ -54,37 +58,96 @@ def base_grid():


@requires_exe("gridgen")
def test_add_refinement_feature_accepts_filename(function_tmpdir):
# same as 1st grid in .docs/Notebooks/gridgen_example.py
grid = base_grid()

# create initial refined grid (writing refinement
# feature shapefile to the workspace)
g1 = Gridgen(grid, model_ws=function_tmpdir)
g1.add_refinement_features(
[[[(0, 0), (0, 60), (40, 80), (60, 0), (0, 0)]]],
"polygon",
1,
range(grid.nlay),
)
g1.build()
g1 = VertexGrid(**g1.get_gridprops_vertexgrid())
# g1.plot()
# g1.show()

# recreate gridgen object, using shapefile from the
# first run
g2 = Gridgen(grid, model_ws=function_tmpdir)
g2.add_refinement_features("rf0.shp", "polygon", 1, range(grid.nlay))
g2.build()
g2 = VertexGrid(**g2.get_gridprops_vertexgrid())
# g2.plot()
# plt.show()

assert g1.nnodes > grid.nnodes
assert g1.ncpl != grid.ncpl
assert g1.ncpl == g2.ncpl
assert g1.nnodes == g2.nnodes
# GRIDGEN seems not to like paths containing "[" or "]", as
# function_tmpdir does with parametrization, do it manually
# @pytest.mark.parametrize("grid_type", ["vertex", "unstructured"])
def test_add_active_domain(function_tmpdir): # , grid_type):
bgrid = get_structured_grid()

# test providing active domain various ways
for grid_type in ["vertex", "unstructured"]:
grids = []
for feature in [
[[[(0, 0), (0, 60), (40, 80), (60, 0), (0, 0)]]],
function_tmpdir / "ad0.shp",
function_tmpdir / "ad0",
"ad0.shp",
"ad0",
]:
print(
"Testing add_active_domain() for",
grid_type,
"grid with features",
feature,
)
gridgen = Gridgen(bgrid, model_ws=function_tmpdir)
gridgen.add_active_domain(
feature,
range(bgrid.nlay),
)
gridgen.build()
grid = (
VertexGrid(**gridgen.get_gridprops_vertexgrid())
if grid_type == "vertex"
else UnstructuredGrid(
**gridgen.get_gridprops_unstructuredgrid()
)
)
grid.plot()
grids.append(grid)
# plt.show()

assert grid.nnodes < bgrid.nnodes
assert not np.array_equal(grid.ncpl, bgrid.ncpl)
assert all(np.array_equal(grid.ncpl, g.ncpl) for g in grids)
assert all(grid.nnodes == g.nnodes for g in grids)


@requires_exe("gridgen")
# GRIDGEN seems not to like paths containing "[" or "]", as
# function_tmpdir does with parametrization, do it manually
# @pytest.mark.parametrize("grid_type", ["vertex", "unstructured"])
def test_add_refinement_feature(function_tmpdir): # , grid_type):
bgrid = get_structured_grid()

# test providing refinement feature various ways
for grid_type in ["vertex", "unstructured"]:
grids = []
for features in [
[[[(0, 0), (0, 60), (40, 80), (60, 0), (0, 0)]]],
function_tmpdir / "rf0.shp",
function_tmpdir / "rf0",
"rf0.shp",
"rf0",
]:
print(
"Testing add_refinement_feature() for",
grid_type,
"grid with features",
features,
)
gridgen = Gridgen(bgrid, model_ws=function_tmpdir)
gridgen.add_refinement_features(
features,
"polygon",
1,
range(bgrid.nlay),
)
gridgen.build()
grid = (
VertexGrid(**gridgen.get_gridprops_vertexgrid())
if grid_type == "vertex"
else UnstructuredGrid(
**gridgen.get_gridprops_unstructuredgrid()
)
)
grid.plot()
# plt.show()

assert grid.nnodes > bgrid.nnodes
assert not np.array_equal(grid.ncpl, bgrid.ncpl)
assert all(np.array_equal(grid.ncpl, g.ncpl) for g in grids)
assert all(grid.nnodes == g.nnodes for g in grids)


@pytest.mark.slow
Expand Down
Loading

0 comments on commit 290fb31

Please sign in to comment.