Skip to content

Commit d27eded

Browse files
authored
Add Constant handler for ufl2unicode now it's a DAGTraverser (#5058)
* Add Constant handler to ufl2unicode * Make ufl2unicode tests for Constant stronger
1 parent 0f0ae3b commit d27eded

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

firedrake/constant.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from ufl.core.ufl_type import UFLType
1414
from ufl.corealg.multifunction import MultiFunction
1515
from ufl.formatting.ufl2unicode import (
16-
Expression2UnicodeHandler, UC, subscript_number, PrecedenceRules,
17-
colorama,
16+
Expression2UnicodeHandler, UC, subscript_number, colorama,
1817
)
1918
from functools import cached_property
2019
from ufl.utils.counted import Counted
@@ -192,7 +191,7 @@ def _unicode_format_firedrake_constant(self, o):
192191

193192

194193
# This monkey patches ufl2unicode support for Firedrake constants
195-
Expression2UnicodeHandler.firedrake_constant = _unicode_format_firedrake_constant
194+
Expression2UnicodeHandler.process.register(Constant, _unicode_format_firedrake_constant)
196195

197196
# This is internally done in UFL by the ufl_type decorator, but we cannot
198197
# do the same here, because we want to use the class name Constant
@@ -209,4 +208,3 @@ def _unicode_format_firedrake_constant(self, o):
209208

210209
# These caches need rebuilding for the new type to be registered
211210
MultiFunction._handlers_cache = {}
212-
ufl.formatting.ufl2unicode._precrules = PrecedenceRules()

tests/firedrake/regression/test_constant.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,17 @@ def test_derivative_wrt_constant():
277277

278278
def test_constant_ufl2unicode():
279279
mesh = UnitIntervalMesh(1)
280-
a = Constant(1.0, name="a")
281-
b = Constant(2.0, name="b")
280+
a = Constant(1.0, name="a", count=0)
281+
b = Constant(2.0, name="b", count=1)
282282
F = a * a * b * b * dx(mesh)
283-
_ = ufl2unicode(F)
283+
assert ufl2unicode(F) == "∫[rest of domain] ((C₀ C₀) C₁) C₁ 𝐝𝐱"
284284

285285
dFda = derivative(F, u=a)
286286
dFdb = derivative(F, u=b)
287-
_ = ufl2unicode(dFda)
288-
_ = ufl2unicode(dFdb)
287+
assert ufl2unicode(dFda) == "∫[rest of domain] ((v C₀ + v C₀) C₁) C₁ 𝐝𝐱"
288+
assert ufl2unicode(dFdb) == "∫[rest of domain] v (C₀ C₀) C₁ + (v C₀ C₀) C₁ 𝐝𝐱"
289289

290290
dFda_du = derivative(F, u=a, du=IntValue(1))
291291
dFdb_du = derivative(F, u=b, du=IntValue(1))
292-
_ = ufl2unicode(dFda_du)
293-
_ = ufl2unicode(dFdb_du)
292+
assert ufl2unicode(dFda_du) == "∫[rest of domain] ((C₀ + C₀) C₁) C₁ 𝐝𝐱"
293+
assert ufl2unicode(dFdb_du) == "∫[rest of domain] (C₀ C₀) C₁ + (C₀ C₀) C₁ 𝐝𝐱"

0 commit comments

Comments
 (0)