Skip to content

Commit cee28f6

Browse files
authored
Further fixes to FiniteElement Python wrapper (#3554)
* Fix finite element signature * Same issue with interpolation points * Update API in tests * Update demos
1 parent debd46e commit cee28f6

11 files changed

+26
-27
lines changed

python/demo/demo_axis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ def create_eps_mu(pml, rho, eps_bkg, mu_bkg):
786786
v_dg_el = element("DG", msh.basix_cell(), degree, shape=(3,), dtype=real_type)
787787
W = fem.functionspace(msh, v_dg_el)
788788
Es_dg = fem.Function(W)
789-
Es_expr = fem.Expression(Esh, W.element.interpolation_points())
789+
Es_expr = fem.Expression(Esh, W.element.interpolation_points)
790790
Es_dg.interpolate(Es_expr)
791791
with VTXWriter(msh.comm, "sols/Es.bp", Es_dg) as f:
792792
f.write(0.0)

python/demo/demo_elasticity.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def σ(v):
250250

251251
# +
252252
W = functionspace(msh, ("Discontinuous Lagrange", 0))
253-
sigma_vm_expr = Expression(sigma_vm, W.element.interpolation_points())
253+
sigma_vm_expr = Expression(sigma_vm, W.element.interpolation_points)
254254
sigma_vm_h = Function(W)
255255
sigma_vm_h.interpolate(sigma_vm_expr)
256256
# -

python/dolfinx/fem/element.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def interpolation_points(self) -> npt.NDArray[np.floating]:
225225
the points will typically be the quadrature points used to evaluate moment degrees of
226226
freedom.
227227
"""
228-
return self._cpp_object.interpolation_points
228+
return self._cpp_object.interpolation_points()
229229

230230
@property
231231
def interpolation_ident(self) -> bool:

python/dolfinx/wrappers/fem.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ void declare_function_space(nb::module_& m, std::string type)
100100
[](dolfinx::fem::FiniteElement<T>* self,
101101
basix::FiniteElement<T>& element,
102102
std::optional<std::vector<std::size_t>> block_shape,
103-
bool symmetric)
104-
{
103+
bool symmetric) {
105104
new (self) dolfinx::fem::FiniteElement<T>(element, block_shape,
106105
symmetric);
107106
},
@@ -284,7 +283,7 @@ void declare_function_space(nb::module_& m, std::string type)
284283
nb::arg("x"), nb::arg("cell_permutations"), nb::arg("dim"))
285284
.def_prop_ro("needs_dof_transformations",
286285
&dolfinx::fem::FiniteElement<T>::needs_dof_transformations)
287-
.def("signature", &dolfinx::fem::FiniteElement<T>::signature);
286+
.def_prop_ro("signature", &dolfinx::fem::FiniteElement<T>::signature);
288287
}
289288
}
290289

python/test/unit/fem/test_discrete_operators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_gradient_interpolation(cell_type, p, q):
169169
u = Function(V, uvec, dtype=dtype)
170170
u.interpolate(lambda x: 2 * x[0] ** p + 3 * x[1] ** p)
171171

172-
grad_u = Expression(ufl.grad(u), W.element.interpolation_points(), dtype=dtype)
172+
grad_u = Expression(ufl.grad(u), W.element.interpolation_points, dtype=dtype)
173173
w_expr = Function(W, dtype=dtype)
174174
w_expr.interpolate(grad_u)
175175

python/test/unit/fem/test_dof_permuting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_dof_positions(cell_type, space_type):
166166
entities = {i: {} for i in range(1, tdim)}
167167
for cell in range(coord_dofs.shape[0]):
168168
# Push coordinates forward
169-
X = V.element.interpolation_points()
169+
X = V.element.interpolation_points
170170
xg = x_g[coord_dofs[cell], :tdim]
171171
x = cmap.push_forward(X, xg)
172172

python/test/unit/fem/test_dofmap.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def test_higher_order_coordinate_map(points, celltype, order):
327327
mesh = create_mesh(MPI.COMM_WORLD, cells, points, domain)
328328

329329
V = functionspace(mesh, ("Lagrange", 2))
330-
X = V.element.interpolation_points()
330+
X = V.element.interpolation_points
331331
coord_dofs = mesh.geometry.dofmap
332332
x_g = mesh.geometry.x
333333
cmap = mesh.geometry.cmap
@@ -402,7 +402,7 @@ def test_higher_order_tetra_coordinate_map(order):
402402
)
403403
mesh = create_mesh(MPI.COMM_WORLD, cells, points, domain)
404404
V = functionspace(mesh, ("Lagrange", order))
405-
X = V.element.interpolation_points()
405+
X = V.element.interpolation_points
406406
x_dofs = mesh.geometry.dofmap
407407
x_g = mesh.geometry.x
408408

python/test/unit/fem/test_expression.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_rank0(dtype):
4848
f.interpolate(lambda x: x[0] ** 2 + 2.0 * x[1] ** 2)
4949

5050
ufl_expr = ufl.grad(f)
51-
points = vdP1.element.interpolation_points()
51+
points = vdP1.element.interpolation_points
5252

5353
compiled_expr = Expression(ufl_expr, points, dtype=dtype)
5454
num_cells = mesh.topology.index_map(2).size_local
@@ -98,7 +98,7 @@ def test_rank1_hdiv(dtype):
9898
RT1 = functionspace(mesh, ("RT", 2))
9999
f = ufl.TrialFunction(RT1)
100100

101-
points = vdP1.element.interpolation_points()
101+
points = vdP1.element.interpolation_points
102102
compiled_expr = Expression(f, points, dtype=dtype)
103103
num_cells = mesh.topology.index_map(2).size_local
104104
array_evaluated = compiled_expr.eval(mesh, np.arange(num_cells, dtype=np.int32))
@@ -355,7 +355,7 @@ def test_expression_eval_cells_subset(dtype):
355355
u = dolfinx.fem.Function(V, dtype=dtype)
356356
u.x.array[:] = dofs_to_cells
357357
u.x.scatter_forward()
358-
e = dolfinx.fem.Expression(u, V.element.interpolation_points())
358+
e = dolfinx.fem.Expression(u, V.element.interpolation_points)
359359

360360
# Test eval on single cell
361361
for c in range(cells_imap.size_local):
@@ -387,8 +387,8 @@ def test_expression_comm(dtype):
387387
mesh = create_unit_square(MPI.COMM_WORLD, 4, 4, dtype=xtype)
388388
v = Constant(mesh, dtype(1))
389389
u = Function(functionspace(mesh, ("Lagrange", 1)), dtype=dtype)
390-
Expression(v, u.function_space.element.interpolation_points(), comm=MPI.COMM_WORLD)
391-
Expression(v, u.function_space.element.interpolation_points(), comm=MPI.COMM_SELF)
390+
Expression(v, u.function_space.element.interpolation_points, comm=MPI.COMM_WORLD)
391+
Expression(v, u.function_space.element.interpolation_points, comm=MPI.COMM_SELF)
392392

393393

394394
def compute_exterior_facet_entities(mesh, facets):

python/test/unit/fem/test_function_space.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_sub(Q, W):
123123
assert W.element.num_sub_elements == X.element.num_sub_elements
124124
assert W.element.space_dimension == X.element.space_dimension
125125
assert W.value_shape == X.value_shape
126-
assert W.element.interpolation_points().shape == X.element.interpolation_points().shape
126+
assert W.element.interpolation_points.shape == X.element.interpolation_points.shape
127127
assert W.element == X.element
128128

129129

python/test/unit/fem/test_interpolation.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def test_nedelec_spatial(order, dim):
618618
# The expression (x,y,z) is contained in the N1curl function space
619619
# order>1
620620
f_ex = x
621-
f = Expression(f_ex, V.element.interpolation_points())
621+
f = Expression(f_ex, V.element.interpolation_points)
622622
u.interpolate(f)
623623
assert np.abs(assemble_scalar(form(ufl.inner(u - f_ex, u - f_ex) * ufl.dx))) == pytest.approx(
624624
0, abs=1e-10
@@ -628,7 +628,7 @@ def test_nedelec_spatial(order, dim):
628628
# order
629629
V2 = functionspace(mesh, ("N2curl", 1))
630630
w = Function(V2)
631-
f2 = Expression(f_ex, V2.element.interpolation_points())
631+
f2 = Expression(f_ex, V2.element.interpolation_points)
632632
w.interpolate(f2)
633633
assert np.abs(assemble_scalar(form(ufl.inner(w - f_ex, w - f_ex) * ufl.dx))) == pytest.approx(0)
634634

@@ -650,7 +650,7 @@ def test_vector_interpolation_spatial(order, dim, affine):
650650

651651
# The expression (x,y,z)^n is contained in space
652652
f = ufl.as_vector([x[i] ** order for i in range(dim)])
653-
u.interpolate(Expression(f, V.element.interpolation_points()))
653+
u.interpolate(Expression(f, V.element.interpolation_points))
654654
assert np.abs(assemble_scalar(form(ufl.inner(u - f, u - f) * ufl.dx))) == pytest.approx(0)
655655

656656

@@ -663,7 +663,7 @@ def test_2D_lagrange_to_curl(order):
663663
u1 = Function(W)
664664
u1.interpolate(lambda x: x[0])
665665
f = ufl.as_vector((u0, u1))
666-
f_expr = Expression(f, V.element.interpolation_points())
666+
f_expr = Expression(f, V.element.interpolation_points)
667667
u.interpolate(f_expr)
668668
x = ufl.SpatialCoordinate(mesh)
669669
f_ex = ufl.as_vector((-x[1], x[0]))
@@ -679,7 +679,7 @@ def test_de_rahm_2D(order):
679679
g = ufl.grad(w)
680680
Q = functionspace(mesh, ("N2curl", order - 1))
681681
q = Function(Q)
682-
q.interpolate(Expression(g, Q.element.interpolation_points()))
682+
q.interpolate(Expression(g, Q.element.interpolation_points))
683683
x = ufl.SpatialCoordinate(mesh)
684684
g_ex = ufl.as_vector((1 + x[1], 4 * x[1] + x[0]))
685685
assert np.abs(assemble_scalar(form(ufl.inner(q - g_ex, q - g_ex) * ufl.dx))) == pytest.approx(
@@ -692,7 +692,7 @@ def test_de_rahm_2D(order):
692692
def curl2D(u):
693693
return ufl.as_vector((ufl.Dx(u[1], 0), -ufl.Dx(u[0], 1)))
694694

695-
v.interpolate(Expression(curl2D(ufl.grad(w)), V.element.interpolation_points()))
695+
v.interpolate(Expression(curl2D(ufl.grad(w)), V.element.interpolation_points))
696696
h_ex = ufl.as_vector((1, -1))
697697
assert np.abs(assemble_scalar(form(ufl.inner(v - h_ex, v - h_ex) * ufl.dx))) == pytest.approx(
698698
0, abs=np.sqrt(np.finfo(mesh.geometry.x.dtype).eps)
@@ -720,7 +720,7 @@ def test_interpolate_subset(order, dim, affine, callable_):
720720
x = ufl.SpatialCoordinate(mesh)
721721
f = x[1] ** order
722722
if not callable_:
723-
expr = Expression(f, V.element.interpolation_points())
723+
expr = Expression(f, V.element.interpolation_points)
724724
u.interpolate(expr, cells_local)
725725
else:
726726
u.interpolate(lambda x: x[1] ** order, cells_local)
@@ -760,7 +760,7 @@ def test_interpolate_callable_subset(bound):
760760
u0, u1 = Function(V), Function(V)
761761
x = ufl.SpatialCoordinate(mesh)
762762
f = x[0]
763-
expr = Expression(f, V.element.interpolation_points())
763+
expr = Expression(f, V.element.interpolation_points)
764764
u0.interpolate(lambda x: x[0], cells_local)
765765
u1.interpolate(expr, cells_local)
766766
assert np.allclose(u0.x.array, u1.x.array, rtol=1.0e-6, atol=1.0e-6)
@@ -1114,7 +1114,7 @@ def modified_grad(x):
11141114
V_sub = functionspace(submesh, ("N2curl", 1))
11151115
u_sub = Function(V_sub)
11161116

1117-
parent_expr = Expression(ufl.grad(u), V_sub.element.interpolation_points())
1117+
parent_expr = Expression(ufl.grad(u), V_sub.element.interpolation_points)
11181118

11191119
# Map from parent to sub mesh
11201120

@@ -1136,7 +1136,7 @@ def modified_grad(x):
11361136

11371137
# Map exact solution (based on quadrature points) back to parent mesh
11381138
sub_vec = ufl.as_vector((-0.2 * u_sub_exact[1], 0.1 * u_sub_exact[0]))
1139-
sub_expr = Expression(sub_vec, W.element.interpolation_points())
1139+
sub_expr = Expression(sub_vec, W.element.interpolation_points)
11401140

11411141
# Mapping back needs to be restricted to the subset of cells in the submesh
11421142
w.interpolate(sub_expr, cells=sub_to_parent, expr_mesh=submesh, cell_map=parent_to_sub)

python/test/unit/fem/test_petsc_discrete_operators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_gradient_interpolation_petsc(self, cell_type, p, q):
103103
u = Function(V)
104104
u.interpolate(lambda x: 2 * x[0] ** p + 3 * x[1] ** p)
105105

106-
grad_u = Expression(ufl.grad(u), W.element.interpolation_points())
106+
grad_u = Expression(ufl.grad(u), W.element.interpolation_points)
107107
w_expr = Function(W)
108108
w_expr.interpolate(grad_u)
109109

0 commit comments

Comments
 (0)