From d3296f1c5cc4b39e4444cf5f52ed6e81b677c968 Mon Sep 17 00:00:00 2001 From: Pablo Brubeck Date: Wed, 18 Dec 2024 07:39:00 -0600 Subject: [PATCH] Fix is_cellwise_constant(ReferenceGrad(x)) --- test/test_change_to_local.py | 4 ++-- ufl/checks.py | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/test/test_change_to_local.py b/test/test_change_to_local.py index 973195bc7..807bf1645 100755 --- a/test/test_change_to_local.py +++ b/test/test_change_to_local.py @@ -12,8 +12,8 @@ def test_change_to_reference_grad(): cell = triangle domain = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1)) - U = FunctionSpace(domain, FiniteElement("Lagrange", cell, 1, (), identity_pullback, H1)) - V = FunctionSpace(domain, FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1)) + U = FunctionSpace(domain, FiniteElement("Lagrange", cell, 3, (), identity_pullback, H1)) + V = FunctionSpace(domain, FiniteElement("Lagrange", cell, 3, (2,), identity_pullback, H1)) u = Coefficient(U) v = Coefficient(V) Jinv = JacobianInverse(domain) diff --git a/ufl/checks.py b/ufl/checks.py index 09ae4453b..cef9d1ac7 100644 --- a/ufl/checks.py +++ b/ufl/checks.py @@ -11,7 +11,7 @@ from ufl.core.expr import Expr from ufl.core.terminal import FormArgument from ufl.corealg.traversal import traverse_unique_terminals -from ufl.geometry import GeometricQuantity +from ufl.geometry import GeometricQuantity, SpatialCoordinate from ufl.sobolevspace import H1 @@ -34,7 +34,20 @@ def is_true_ufl_scalar(expression): def is_cellwise_constant(expr): """Return whether expression is constant over a single cell.""" - # TODO: Implement more accurately considering e.g. derivatives? + from ufl.coefficient import Coefficient + from ufl.differentiation import ReferenceGrad + + if isinstance(expr, ReferenceGrad): + (expr,) = expr.ufl_operands + if is_cellwise_constant(expr): + return True + elif isinstance(expr, SpatialCoordinate): + element = expr.ufl_domain().ufl_coordinate_element() + return element.embedded_superdegree <= 1 + elif isinstance(expr, Coefficient): + element = expr.ufl_element() + return element.embedded_superdegree <= 1 + return all(e.is_cellwise_constant() for e in traverse_unique_terminals(expr))