Skip to content

replace: only expand CoefficientDerivative#451

Open
pbrubeck wants to merge 9 commits into
FEniCS:mainfrom
firedrakeproject:pbrubeck/fix/replace-coefficient-derivative
Open

replace: only expand CoefficientDerivative#451
pbrubeck wants to merge 9 commits into
FEniCS:mainfrom
firedrakeproject:pbrubeck/fix/replace-coefficient-derivative

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

replace was unecessarily expanding all derivatives too early. This becomes a problem in Irksome where we introduce a TimeDerivative that cannot be expanded in UFL.

This PR avoids this problem by expanding CoefficientDerivative only.

@jorgensd jorgensd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pbrubeck pbrubeck force-pushed the pbrubeck/fix/replace-coefficient-derivative branch from adabd95 to 8357fa8 Compare February 19, 2026 10:37
CoefficientDerivativeRuleDispatcher registered the abstract Derivative
type to reuse_if_untouched, so that foreign Derivative subtypes (e.g.
irksome.TimeDerivative) are left alone. But since Grad, ReferenceGrad,
etc. are themselves Derivative subtypes with no more specific
registration on this subclass, they were *also* being left untouched
-- including when they appear inside the content of a
CoefficientDerivative that is about to be Gateaux-differentiated. That
content needs the same normalization the full DerivativeRuleDispatcher
gives it (e.g. pushing a spatial Grad down through Indexed to a bare
terminal) before GateauxDerivativeRuleset can process it; skipping
that step left GateauxDerivativeRuleset.Grad staring at
Grad(Indexed(Coefficient, ...)) instead of Grad(Coefficient), which it
does not know how to handle -- this is what test_replace_mixed_element
hit via replace() on a mixed-element coefficient.

Fix: recurse into a CoefficientDerivative's own content with a plain
(cached) DerivativeRuleDispatcher instead of `self`, so nested content
gets full normalization while everything *outside* CoefficientDerivative
nodes keeps the narrow, foreign-Derivative-safe treatment. Also fix the
resulting equality-vs-signature test flakiness in
test_replace_mixed_element (independently built expressions can differ
in dummy index numbering) and two pre-existing, unrelated mypy
var-annotated errors in test_classcoverage.py that blocked `mypy test/`
on this branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants