Skip to content

Commit d096f2b

Browse files
committed
Add compat layer with dofmaps and cmaps
1 parent 50948f9 commit d096f2b

8 files changed

Lines changed: 32 additions & 14 deletions

File tree

examples/transfer_tags_to_submesh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def plot_mesh(mesh: dolfinx.mesh.Mesh, values=None):
8282
plotter = pyvista.Plotter()
8383
V_linear = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
8484
linear_grid = pyvista.UnstructuredGrid(*dolfinx.plot.vtk_mesh(V_linear))
85-
if compat.get_cmap(mesh).degree > 1:
85+
if compat.cmap(mesh).degree > 1:
8686
ugrid = pyvista.UnstructuredGrid(*dolfinx.plot.vtk_mesh(mesh))
8787
if values is not None:
8888
ugrid.cell_data["Marker"] = values

src/scifem/compat.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
"""Layer for small backward compatibility wrappers for DOLFINx"""
22

3+
import numpy.typing as npt
4+
import numpy as np
35
import dolfinx
46

57

6-
def get_cmap(mesh: dolfinx.mesh.Mesh) -> dolfinx.fem.CoordinateElement:
8+
def cmap(mesh: dolfinx.mesh.Mesh) -> dolfinx.fem.CoordinateElement:
79
"""Get the basix Cmap for the mesh."""
10+
if hasattr(mesh.geometry, "cmaps"):
11+
return mesh.geometry.cmaps[0]
812
if callable(mesh.geometry.cmap):
913
return mesh.geometry.cmap()
1014
else:
1115
return mesh.geometry.cmap
16+
17+
18+
def dofmap(mesh: dolfinx.mesh.Mesh) -> npt.NDArray[np.int32]:
19+
"""Get the dofmap for the geometry."""
20+
if hasattr(mesh.geometry, "dofmaps"):
21+
return mesh.geometry.dofmaps[0]
22+
if callable(mesh.geometry.dofmap):
23+
return mesh.geometry.dofmap()
24+
else:
25+
return mesh.geometry.dofmap

src/scifem/eval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import ufl
1111
from scipy.optimize import minimize
1212
from ufl.algorithms.signature import compute_expression_signature
13-
from .compat import get_cmap
13+
from . import compat
1414

1515

1616
T = typing.TypeVar("T", int, float)
@@ -155,9 +155,9 @@ def find_cell_extrema(
155155
x_ref = x0
156156

157157
_cell = np.array([cell], dtype=np.int32)
158-
mesh_nodes = mesh.geometry.x[mesh.geometry.dofmap[cell], : mesh.geometry.dim]
158+
mesh_nodes = mesh.geometry.x[compat.dofmap(mesh)[cell], : mesh.geometry.dim]
159159
_x_p = np.zeros(3)
160-
cmap = get_cmap(mesh)
160+
cmap = compat.cmap(mesh)
161161

162162
def eval_J(x_ref):
163163
# Evaluating basis functions through {py:func}`dolfinx.fem.Function.eval`

src/scifem/geometry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy.typing as npt
44
import warnings
55
from scifem._scifem import closest_point_projection_float64, closest_point_projection_float32
6+
from . import compat
67

78

89
def project_onto_simplex(
@@ -162,7 +163,7 @@ def _closest_point_projection(
162163
element = mesh.ufl_domain().ufl_coordinate_element().sub_elements[0]
163164
tdim = mesh.topology.dim
164165
# Get the coordinates of the nodes for the specified cell
165-
node_coords = mesh.geometry.x[mesh.geometry.dofmap[cells]][:, :, : mesh.geometry.dim]
166+
node_coords = mesh.geometry.x[compat.dofmap(mesh)[cells]][:, :, : mesh.geometry.dim]
166167
target_points = target_points.reshape(-1, 3)
167168

168169
# Constraints and Bounds

src/scifem/mesh.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import basix
1212
import ufl
1313

14+
from . import compat
15+
1416
__all__ = [
1517
"create_entity_markers",
1618
"transfer_meshtags_to_submesh",
@@ -413,7 +415,7 @@ def create_geometry_function_space(
413415
414416
"""
415417
geom_imap = mesh.geometry.index_map()
416-
geom_dofmap = mesh.geometry.dofmap
418+
geom_dofmap = compat.dofmap(mesh)
417419
ufl_domain = mesh.ufl_domain()
418420
assert ufl_domain is not None
419421
sub_el = ufl_domain.ufl_coordinate_element().sub_elements[0]

src/scifem/point_source.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import numpy.typing as npt
2424
import ufl
2525

26-
from .compat import get_cmap
26+
from . import compat
2727
from .utils import unroll_dofmap
2828

2929
__all__ = ["PointSource"]
@@ -122,11 +122,11 @@ def compute_cell_contributions(self):
122122
mesh = self._function_space.mesh
123123
# Pull owning points back to reference cell
124124
mesh_nodes = mesh.geometry.x
125-
cmap = get_cmap(mesh)
125+
cmap = compat.cmap(mesh)
126126

127127
ref_x = np.zeros((len(self._cells), mesh.topology.dim), dtype=mesh.geometry.x.dtype)
128128
for i, (point, cell) in enumerate(zip(self._points, self._cells)):
129-
geom_dofs = mesh.geometry.dofmap[cell]
129+
geom_dofs = compat.dofmap(mesh)[cell]
130130
ref_x[i] = cmap.pull_back(point.reshape(-1, 3), mesh_nodes[geom_dofs])
131131

132132
# Create expression evaluating a trial function (i.e. just the basis function)

tests/test_geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import numpy.typing as npt
55
from scipy.optimize import minimize
6-
from scifem import closest_point_projection
6+
from scifem import closest_point_projection, compat
77
from scifem.geometry import project_onto_simplex, _closest_point_projection
88
import ufl
99
import basix.ufl
@@ -35,7 +35,7 @@ def scipy_project_point_to_element(
3535
element = mesh.ufl_domain().ufl_coordinate_element().sub_elements[0]
3636

3737
# Get the coordinates of the nodes for the specified cell
38-
node_coords = mesh.geometry.x[mesh.geometry.dofmap[cells]][:, :, : mesh.geometry.dim]
38+
node_coords = mesh.geometry.x[compat.dofmap(mesh)[cells]][:, :, : mesh.geometry.dim]
3939
target_points = target_points.reshape(-1, 3)
4040
# cmap = mesh.geometry.cmap
4141

tests/test_point_source.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ def test_outside():
8888
cells = dolfinx.geometry.compute_colliding_cells(mesh, cell_candidates, point)
8989
# Only use evaluate for points on current processor
9090
# BUG: In DOLFINx 0.8.0, links(0) yields a too long array
91+
9192
if cells.offsets[-1] > 0:
9293
cell = cells.links(0)[0]
93-
geom_dofs = mesh.geometry.dofmap[cell]
94-
ref_x = compat.get_cmap(mesh).pull_back(point.reshape(-1, 3), mesh.geometry.x[geom_dofs])
94+
geom_dofs = compat.dofmap(mesh)[cell]
95+
ref_x = compat.cmap(mesh).pull_back(point.reshape(-1, 3), mesh.geometry.x[geom_dofs])
9596
ref_values = V.ufl_element().tabulate(0, ref_x).flatten()
9697
b_nonzero = np.flatnonzero(b.x.array)
9798
dofs = V.dofmap.cell_dofs(cell)

0 commit comments

Comments
 (0)