Skip to content

Mesh: extend P1 cell_sizes to high-order meshes #4218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions firedrake/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2399,10 +2399,29 @@ def cell_sizes(self):

This is computed by the :math:`L^2` projection of the local mesh element size."""
from firedrake.ufl_expr import CellSize
from firedrake.function import Function
from firedrake.functionspace import FunctionSpace
from firedrake.projection import project
P1 = FunctionSpace(self, "Lagrange", 1)
return project(CellSize(self), P1)

if self.topological_dimension() == 0:
# On vertex-only meshes we define the cell sizes as 1
P0 = FunctionSpace(self, "DG", 0)
return Function(P0).assign(1)

if self.ufl_coordinate_element().degree() > 1:
# We need a P1 mesh, as CellSize is not defined on high-order meshes
VectorP1 = self.coordinates.function_space().reconstruct(degree=1)
mesh = Mesh(Function(VectorP1).interpolate(self.coordinates))
else:
mesh = self

P1 = FunctionSpace(mesh, "Lagrange", 1)
h = project(CellSize(mesh), P1)

if P1.mesh() is not self:
# Transfer the Function on the P1 mesh into the original mesh
h = Function(P1.reconstruct(mesh=self), val=h.dat)
return h

def clear_cell_sizes(self):
"""Reset the :attr:`cell_sizes` field on this mesh geometry.
Expand Down
13 changes: 12 additions & 1 deletion tests/firedrake/regression/test_interpolate_zany.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def expect(V, which):
return a + b


def test_interpolate(V, mesh, which, expect, tolerance):
def test_interpolate_zany_into_cg(V, mesh, which, expect, tolerance):
degree = V.ufl_element().degree()
Vcg = FunctionSpace(mesh, "P", degree)

Expand All @@ -71,3 +71,14 @@ def test_interpolate(V, mesh, which, expect, tolerance):
g.interpolate(a + b)

assert numpy.allclose(norm(g - expect), 0, atol=tolerance)


def test_high_order_mesh_cell_sizes():
msh1 = UnitSquareMesh(2, 2)
h1 = msh1.cell_sizes

P2 = msh1.coordinates.function_space().reconstruct(degree=2)
msh2 = Mesh(Function(P2).interpolate(msh1.coordinates))
h2 = msh2.cell_sizes

assert numpy.allclose(h1.dat.data, h2.dat.data)