Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions discretize/_extensions/tree_ext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5238,24 +5238,26 @@ cdef class _TreeMesh:
for cell in self.tree.cells :
next_cell = cell.neighbors[1]
prev_cell = cell.neighbors[0]
# handle extrapolation to boundary faces
if next_cell == NULL:
# handle extrapolation to boundary faces. The -x face is handled
# first: a cell spanning the whole mesh has no neighbor on either
# side, and the +x branch below skips the rest of the loop.
if prev_cell == NULL:
if dim == 2:
ind = cell.edges[3].index # +x face
ind = cell.edges[2].index # -x face
else:
ind = cell.faces[1].index # +x face
ind = cell.faces[0].index # -x face
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue
if prev_cell == NULL:
if next_cell == NULL:
if dim == 2:
ind = cell.edges[2].index # -x face
ind = cell.edges[3].index # +x face
else:
ind = cell.faces[0].index # -x face
ind = cell.faces[1].index # +x face
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue

if next_cell.is_leaf():
if next_cell.level == cell.level:
Expand Down Expand Up @@ -5355,24 +5357,26 @@ cdef class _TreeMesh:
for cell in self.tree.cells :
next_cell = cell.neighbors[3]
prev_cell = cell.neighbors[2]
# handle extrapolation to boundary faces
if next_cell == NULL:
# handle extrapolation to boundary faces. The -y face is handled
# first: a cell spanning the whole mesh has no neighbor on either
# side, and the +y branch below skips the rest of the loop.
if prev_cell == NULL:
if dim == 2:
ind = cell.edges[1].index
ind = cell.edges[0].index
else:
ind = cell.faces[3].index
ind = cell.faces[2].index
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue
if prev_cell == NULL:
if next_cell == NULL:
if dim == 2:
ind = cell.edges[0].index
ind = cell.edges[1].index
else:
ind = cell.faces[2].index
ind = cell.faces[3].index
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue

if next_cell.is_leaf():
if next_cell.level == cell.level:
Expand Down Expand Up @@ -5474,18 +5478,20 @@ cdef class _TreeMesh:
for cell in self.tree.cells :
next_cell = cell.neighbors[5]
prev_cell = cell.neighbors[4]
# handle extrapolation to boundary faces
if next_cell == NULL:
ind = cell.faces[5].index # +z face
# handle extrapolation to boundary faces. The -z face is handled
# first: a cell spanning the whole mesh has no neighbor on either
# side, and the +z branch below skips the rest of the loop.
if prev_cell == NULL:
ind = cell.faces[4].index # -z face
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue
if prev_cell == NULL:
ind = cell.faces[4].index # -z face
if next_cell == NULL:
ind = cell.faces[5].index # +z face
I[2*ind ] = ind
J[2*ind ] = cell.index
V[2*ind ] = 1.0
continue

if next_cell.is_leaf():
if next_cell.level == cell.level:
Expand Down
46 changes: 46 additions & 0 deletions tests/tree/test_tree_operators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest
import discretize

MESHTYPES = ["uniformTree", "randomTree"]
Expand Down Expand Up @@ -1135,3 +1136,48 @@ def test_orderCC2F(self):
self.expectedOrders = 1
self.orderTest()
self.expectedOrders = 2


def _full_width_tree(dim):
# A small refinement in the middle of a base mesh with few cells across
# leaves coarse cells that span the whole mesh in the horizontal
# directions, with no neighbor on either side.
h = [np.full(8, 100.0)] * (dim - 1) + [np.full(64, 50.0)]
mesh = discretize.TreeMesh(h, origin="C" * dim, diagonal_balance=True)
mesh.refine_box([-100.0] * dim, [100.0] * dim, levels=-1)
return mesh


@pytest.mark.parametrize("dim", [2, 3])
@pytest.mark.parametrize(
"operator",
["average_cell_to_face_x", "average_cell_to_face_y", "average_cell_to_face_z"],
)
def test_average_cell_to_face_full_width_cells(dim, operator):
"""Every face, including both boundary faces of a full-width cell, gets a value."""
if dim == 2 and operator.endswith("_z"):
pytest.skip("no z faces in 2D")
mesh = _full_width_tree(dim)
axis = "xyz".index(operator[-1])
# the test mesh must actually contain cells with no neighbor on either side
widths = mesh.h_gridded[:, axis]
extent = mesh.nodes[:, axis].max() - mesh.nodes[:, axis].min()
if axis < dim - 1:
assert np.any(np.isclose(widths, extent))
A = getattr(mesh, operator)
np.testing.assert_allclose(A @ np.ones(mesh.n_cells), 1.0)


@pytest.mark.parametrize("dim", [2, 3])
def test_average_cell_to_face_boundary_faces_use_their_cell(dim):
mesh = _full_width_tree(dim)
Pf = mesh.project_face_to_boundary_face
A = (Pf @ mesh.average_cell_to_face).tocsr()
A.eliminate_zeros()
# boundary faces take the value of the one cell they bound
assert np.all(np.diff(A.indptr) == 1)
cells = A.indices
faces = Pf @ mesh.faces
lo = mesh.cell_centers[cells] - mesh.h_gridded[cells] / 2
hi = mesh.cell_centers[cells] + mesh.h_gridded[cells] / 2
assert np.all((faces >= lo - 1e-8) & (faces <= hi + 1e-8))
Loading