|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from flopy.utils.faceutil import get_shared_face_3d |
| 5 | + |
| 6 | + |
| 7 | +def test_get_shared_face_3d_structured_horizontal(): |
| 8 | + """Test get_shared_face_3d for horizontal barrier in structured grid. |
| 9 | +
|
| 10 | + Tests same layer, adjacent cells. |
| 11 | + """ |
| 12 | + from flopy.discretization import StructuredGrid |
| 13 | + |
| 14 | + # Create a simple 2x2x1 structured grid |
| 15 | + nlay, nrow, ncol = 1, 2, 2 |
| 16 | + delr = np.array([1.0, 1.0]) |
| 17 | + delc = np.array([1.0, 1.0]) |
| 18 | + top = np.array([[10.0, 10.0], [10.0, 10.0]]) |
| 19 | + botm = np.array([[[0.0, 0.0], [0.0, 0.0]]]) |
| 20 | + |
| 21 | + grid = StructuredGrid(delc=delc, delr=delr, top=top, botm=botm) |
| 22 | + |
| 23 | + # Test horizontal barrier between cells (0, 0, 0) and (0, 0, 1) |
| 24 | + # These are horizontally adjacent |
| 25 | + cellid1 = (0, 0, 0) |
| 26 | + cellid2 = (0, 0, 1) |
| 27 | + |
| 28 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 29 | + |
| 30 | + assert face is not None |
| 31 | + assert len(face) == 4 # Vertical face should have 4 vertices |
| 32 | + |
| 33 | + # Check that face is vertical (two z-coordinates: top and bottom) |
| 34 | + z_coords = sorted({v[2] for v in face}) |
| 35 | + assert len(z_coords) == 2 |
| 36 | + assert z_coords[0] == 0.0 # bottom |
| 37 | + assert z_coords[1] == 10.0 # top |
| 38 | + |
| 39 | + # Check x-coordinate is at the shared boundary (x=1.0) |
| 40 | + x_coords = {v[0] for v in face} |
| 41 | + assert 1.0 in x_coords |
| 42 | + |
| 43 | + |
| 44 | +def test_get_shared_face_3d_structured_vertical(): |
| 45 | + """Test get_shared_face_3d for vertical barrier in structured grid. |
| 46 | +
|
| 47 | + Tests different layers, same horizontal position. |
| 48 | + """ |
| 49 | + from flopy.discretization import StructuredGrid |
| 50 | + |
| 51 | + # Create a simple 2x2x2 structured grid (2 layers) |
| 52 | + nlay, nrow, ncol = 2, 2, 2 |
| 53 | + delr = np.array([1.0, 1.0]) |
| 54 | + delc = np.array([1.0, 1.0]) |
| 55 | + top = np.array([[10.0, 10.0], [10.0, 10.0]]) |
| 56 | + botm = np.array([[[5.0, 5.0], [5.0, 5.0]], [[0.0, 0.0], [0.0, 0.0]]]) |
| 57 | + |
| 58 | + grid = StructuredGrid(delc=delc, delr=delr, top=top, botm=botm) |
| 59 | + |
| 60 | + # Test vertical barrier between cells (0, 0, 0) and (1, 0, 0) |
| 61 | + # These are vertically adjacent |
| 62 | + cellid1 = (0, 0, 0) |
| 63 | + cellid2 = (1, 0, 0) |
| 64 | + |
| 65 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 66 | + |
| 67 | + assert face is not None |
| 68 | + assert len(face) == 2 # Horizontal face should have 2 vertices (the edge) |
| 69 | + |
| 70 | + # Check that face is horizontal (all z-coordinates the same at interface) |
| 71 | + z_coords = {v[2] for v in face} |
| 72 | + assert len(z_coords) == 1 |
| 73 | + assert next(iter(z_coords)) == 5.0 # Interface between layers |
| 74 | + |
| 75 | + |
| 76 | +def test_get_shared_face_3d_vertex(): |
| 77 | + """Test get_shared_face_3d for vertex grid.""" |
| 78 | + from flopy.discretization import VertexGrid |
| 79 | + |
| 80 | + # Create a simple 2-cell vertex grid |
| 81 | + nlay = 2 |
| 82 | + ncpl = 2 |
| 83 | + |
| 84 | + # Two square cells side by side |
| 85 | + vertices = [ |
| 86 | + [0, 0.0, 0.0], |
| 87 | + [1, 1.0, 0.0], |
| 88 | + [2, 2.0, 0.0], |
| 89 | + [3, 0.0, 1.0], |
| 90 | + [4, 1.0, 1.0], |
| 91 | + [5, 2.0, 1.0], |
| 92 | + ] |
| 93 | + |
| 94 | + cell2d = [ |
| 95 | + [0, 0.5, 0.5, 4, 0, 1, 4, 3], |
| 96 | + [1, 1.5, 0.5, 4, 1, 2, 5, 4], |
| 97 | + ] |
| 98 | + |
| 99 | + top = np.array([10.0, 10.0]) |
| 100 | + botm = np.array([[5.0, 5.0], [0.0, 0.0]]) |
| 101 | + |
| 102 | + grid = VertexGrid(vertices=vertices, cell2d=cell2d, top=top, botm=botm, nlay=nlay) |
| 103 | + |
| 104 | + # Test horizontal barrier between cells (0, 0) and (0, 1) in same layer |
| 105 | + cellid1 = (0, 0) |
| 106 | + cellid2 = (0, 1) |
| 107 | + |
| 108 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 109 | + |
| 110 | + assert face is not None |
| 111 | + assert len(face) == 4 # Vertical face |
| 112 | + |
| 113 | + # Test vertical barrier between cells (0, 0) and (1, 0) in different layers |
| 114 | + cellid1 = (0, 0) |
| 115 | + cellid2 = (1, 0) |
| 116 | + |
| 117 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 118 | + |
| 119 | + assert face is not None |
| 120 | + assert len(face) == 2 # Horizontal face |
| 121 | + |
| 122 | + # Check all z-coordinates are the same (interface elevation) |
| 123 | + z_coords = {v[2] for v in face} |
| 124 | + assert len(z_coords) == 1 |
| 125 | + assert next(iter(z_coords)) == 5.0 |
| 126 | + |
| 127 | + |
| 128 | +def test_get_shared_face_3d_unstructured(): |
| 129 | + """Test get_shared_face_3d for unstructured grid with explicit 3D vertices.""" |
| 130 | + from flopy.discretization import UnstructuredGrid |
| 131 | + |
| 132 | + # Create a simple 2-cell unstructured grid with 3D vertices |
| 133 | + # Two hexahedral cells stacked vertically |
| 134 | + vertices = [ |
| 135 | + # Bottom layer vertices (z=0) |
| 136 | + [0, 0.0, 0.0, 0.0], |
| 137 | + [1, 1.0, 0.0, 0.0], |
| 138 | + [2, 1.0, 1.0, 0.0], |
| 139 | + [3, 0.0, 1.0, 0.0], |
| 140 | + # Middle layer vertices (z=5) |
| 141 | + [4, 0.0, 0.0, 5.0], |
| 142 | + [5, 1.0, 0.0, 5.0], |
| 143 | + [6, 1.0, 1.0, 5.0], |
| 144 | + [7, 0.0, 1.0, 5.0], |
| 145 | + # Top layer vertices (z=10) |
| 146 | + [8, 0.0, 0.0, 10.0], |
| 147 | + [9, 1.0, 0.0, 10.0], |
| 148 | + [10, 1.0, 1.0, 10.0], |
| 149 | + [11, 0.0, 1.0, 10.0], |
| 150 | + ] |
| 151 | + |
| 152 | + iverts = [ |
| 153 | + [0, 1, 2, 3, 4, 5, 6, 7], # Bottom cell |
| 154 | + [4, 5, 6, 7, 8, 9, 10, 11], # Top cell |
| 155 | + ] |
| 156 | + |
| 157 | + xc = np.array([0.5, 0.5]) |
| 158 | + yc = np.array([0.5, 0.5]) |
| 159 | + top = np.array([10.0, 10.0]) |
| 160 | + botm = np.array([0.0, 5.0]) |
| 161 | + |
| 162 | + grid = UnstructuredGrid( |
| 163 | + vertices=vertices, |
| 164 | + iverts=iverts, |
| 165 | + xcenters=xc, |
| 166 | + ycenters=yc, |
| 167 | + top=top, |
| 168 | + botm=botm, |
| 169 | + ncpl=[2], |
| 170 | + ) |
| 171 | + |
| 172 | + # Test shared face between two vertically stacked cells |
| 173 | + cellid1 = (0,) |
| 174 | + cellid2 = (1,) |
| 175 | + |
| 176 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 177 | + |
| 178 | + assert face is not None |
| 179 | + assert len(face) == 4 # Should have 4 shared vertices at the interface |
| 180 | + |
| 181 | + # Check all shared vertices are at z=5.0 (the interface) |
| 182 | + z_coords = {v[2] for v in face} |
| 183 | + assert len(z_coords) == 1 |
| 184 | + assert next(iter(z_coords)) == 5.0 |
| 185 | + |
| 186 | + |
| 187 | +def test_get_shared_face_3d_error_cases(): |
| 188 | + """Test error handling in get_shared_face_3d.""" |
| 189 | + from flopy.discretization import StructuredGrid |
| 190 | + |
| 191 | + nlay, nrow, ncol = 1, 2, 2 |
| 192 | + delr = np.array([1.0, 1.0]) |
| 193 | + delc = np.array([1.0, 1.0]) |
| 194 | + top = np.array([[10.0, 10.0], [10.0, 10.0]]) |
| 195 | + botm = np.array([[[0.0, 0.0], [0.0, 0.0]]]) |
| 196 | + |
| 197 | + grid = StructuredGrid(delc=delc, delr=delr, top=top, botm=botm) |
| 198 | + |
| 199 | + # Test with same cellid |
| 200 | + with pytest.raises(ValueError, match="cellid1 and cellid2 must be different"): |
| 201 | + get_shared_face_3d(grid, (0, 0, 0), (0, 0, 0)) |
| 202 | + |
| 203 | + # Test with non-adjacent cells (should return None) |
| 204 | + cellid1 = (0, 0, 0) |
| 205 | + cellid2 = (0, 1, 1) # Diagonally opposite, not adjacent |
| 206 | + |
| 207 | + face = get_shared_face_3d(grid, cellid1, cellid2) |
| 208 | + assert face is None |
0 commit comments