Repository navigation
FInAT: hash elements by value - #304
Conversation
FInAT elements compared and hashed by identity, so two constructions of the same element were different cache keys, and Firedrake had to key its caches on entity_dofs_key/entity_permutations_key instead, which cannot tell apart elements that share their DoF layout but not their basis. Record the constructor arguments in FiniteElementBase.__new__ and derive __eq__ and __hash__ from the type and those arguments, matched to the parameters of __init__. Elements that wrap other elements hash recursively, and FIAT cells and quadrature rules already hash by value. Fixes #275. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3f712d4 to
8a7b6da
Compare
connorjward
left a comment
There was a problem hiding this comment.
It would be a nicer approach to make everything into a frozen dataclass, which is effectively the same thing as this but much more explicit. But this does work.
This also has the issue that mutating the element will not update the hash key. For example:
import FIAT
from finat import *
triangle = FIAT.ufc_cell("triangle")
cg = Lagrange(triangle, 1)
cg_copy = Lagrange(triangle, 1)
dg = DiscontinuousLagrange(triangle, 1)
cg._element = dg._element
assert cg != cg_copy # failsThis is admittedly contrived, so this might not be a concern.
Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
We don't ever mutate an element, so we don't need to worry about this. |
I guess you mean make every element a frozen dataclass. It seems a much more invasive change and I don't think I want to do it in this PR. |
AI assistance: OpenAI Codex.
connorjward
left a comment
There was a problem hiding this comment.
Happy with your explanations. Thanks!
rckirby
left a comment
There was a problem hiding this comment.
Seems fine to me as well, happy to live with the caveats discussed.
Fixes #275.
Summary
FInAT elements compared and hashed by identity. Two constructions of the same element were different cache keys, so Firedrake keys its caches on
entity_dofs_key/entity_permutations_keyinstead. Those keys cannot tell apart elements that share their DoF layout but not their basis, e.g. DG2equispacedandspectral, which gives wrong multigrid transfers.FiniteElementBasenow records the constructor arguments, and equality and hashing come from them:Elements that wrap other elements (
TensorProductElement,EnrichedElement,TensorFiniteElement, ...) hash recursively. FIAT cells and quadrature rules already hash by value.EnrichedElement.__new__now passes its arguments to the base__new__.Evidence
New tests in
test/finat/test_hash.py:test/finat/test_hash.pyAfter: 13 passed
test/FIAT/regression, which needs a download): 2618 passed.firedrake/tests/tsfc) against this branch: 374 passed.create_elementfrom the UFL element list hash. Of those, 702 pairs that compare equal come from different UFL spellings (aliases, default variants), and every pair tabulates identically.Merge Danger
Door: two-way
Blast Radius: medium
Every FInAT element now compares by value, so caches keyed on elements can merge entries that were separate before. Two elements compare equal only when they have the same type and the same constructor arguments. A default hidden behind
**kwargs(e.g. FIAT'svariantdefault inLagrange) is not normalised, which costs a cache miss, never a false match. The hash depends onPYTHONHASHSEED, like the identity hash did, so it is not yet suitable for disk caches.AI assistance: this PR was written with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code