Skip to content

Commit 8876d43

Browse files
committed
Add a signature to ZeroBaseForm
1 parent 80629e9 commit 8876d43

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

test/test_duals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ def test_zero_base_form_list_arguments():
188188
assert hash(f) == hash(ZeroBaseForm((v,)))
189189

190190

191+
def test_zero_base_form_signature():
192+
domain_2d = Mesh(LagrangeElement(triangle, 1, (2,)))
193+
f_2d = LagrangeElement(triangle, 1)
194+
V = FunctionSpace(domain_2d, f_2d)
195+
196+
v = TestFunction(V)
197+
f = ZeroBaseForm((v,))
198+
199+
assert f.signature() == ZeroBaseForm((v,)).signature()
200+
assert isinstance(f.signature(), str)
201+
202+
W = FunctionSpace(domain_2d, LagrangeElement(triangle, 2))
203+
w = TestFunction(W)
204+
assert f.signature() != ZeroBaseForm((w,)).signature()
205+
206+
191207
def test_zero_base_form_reconstruct():
192208
# ZeroBaseForm._ufl_expr_reconstruct_ inherited BaseForm's default,
193209
# `type(self)(*operands)`, which unpacks `ufl_operands` into positional

ufl/form.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Modified by Nacime Bouziani, 2020.
1212
# Modified by Jørgen S. Dokken 2023.
1313

14+
import hashlib
1415
import numbers
1516
import typing
1617
import warnings
@@ -884,6 +885,7 @@ class ZeroBaseForm(BaseForm):
884885
"_coefficients",
885886
"_domains",
886887
"_hash",
888+
"_signature",
887889
# Pyadjoint compatibility
888890
"form",
889891
"ufl_operands",
@@ -896,6 +898,7 @@ def __init__(self, arguments):
896898
self._arguments = arguments
897899
self.ufl_operands = arguments
898900
self._hash = None
901+
self._signature = None
899902
self._domains = None
900903
self.form = None
901904

@@ -927,6 +930,19 @@ def empty(self):
927930
"""Returns whether the ZeroBaseForm has no components, which is always true."""
928931
return True
929932

933+
def signature(self):
934+
"""Return a signature for use with JIT caches."""
935+
if self._signature is None:
936+
renumbering = {
937+
domain: i for i, domain in enumerate(self.ufl_domains())
938+
}
939+
data = tuple(
940+
argument._ufl_signature_data_(renumbering)
941+
for argument in self.arguments()
942+
)
943+
self._signature = hashlib.sha512(str(data).encode("utf-8")).hexdigest()
944+
return self._signature
945+
930946
def __ne__(self, other):
931947
"""Overwrite BaseForm.__neq__ which relies on `equals`."""
932948
return not self == other

0 commit comments

Comments
 (0)