Skip to content

Fix assembly and solves with the Hessian of an interpolation - #5551

Merged
pbrubeck merged 50 commits into
mainfrom
danshapero/interp-hessian
Nov 9, 2026
Merged

pbrubeck merged 50 commits into
mainfrom
danshapero/interp-hessian

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #5477: solving derivative(J, u) == 0 failed for a functional J containing interpolate(u, P) with P on a VertexOnlyMesh. The Hessian of the data term is Iᵀ M I.

Depends on FEniCS/ufl#544 (stacked on FEniCS/ufl#527), which computes that Hessian symbolically. The DROP BEFORE MERGE commit installs that branch in CI.

Derivative expansion

  • preprocess_base_form expands the derivatives of a Form only if it has base form operators, whatever its mat_type. Plain Forms and Slate tensors are left to TSFC. Before, the "matfree" residual assembler skipped the expansion, so the chain rule through the interpolation produced a FormSum that OneFormAssembler rejects.
  • A "matfree" 2-form is still expanded only when its action is taken. MatrixFreeAssembler accepts any 2-form BaseForm, such as a FormSum, and keeps the tree intact so that a single implicit matrix assembles its action.

Dual slot of base form operators

UFL now simplifies Action(N(u; v̂), F) to N(u; F) for any base form operator and 1-form F, so the assembler can no longer split an operator from its dual slot by building an Action. Restructure case (5) and the Interpolate(v, 2-form) split are removed. Instead, an operator that cannot consume its assembled dual slot is assembled as N(u; v̂) and contracted with it:

  • an Interpolate whose dual slot assembles to a Matrix;
  • an ExternalOperator with a Cofunction or Matrix in its dual slot and no assembly method registered for it (assembly_method()).

The numerics of the Action branch move to contract, which both paths use.

A form in the dual slot contracts an argument as an Action does (FEniCS/ufl#527): Argument 0 if the expression and the form both have an Argument 0, and the highest-numbered primal argument otherwise. Interpolate takes its target space from that argument, so interpolate(v, A) with a test function v and a 2-form A still composes with A.

Adjoint of a rank-one Action

Adjoint also assembles an operand that is already a Cofunction or Function. Action(x, A) is the row x^H A, so Adjoint(Action(x, A)) is the column A^H x. contract already assembles Action(x, A) with multHermitian, so the branch returns that result without conjugating again. FEniCS/ufl#544 restructures Action(Adjoint(A), x) to Adjoint(Action(x, A)).

Argument reconstruction

Argument.reconstruct and Coargument.reconstruct no longer reject a change of value shape. restructure_base_form builds a contraction placeholder in the target space of a map, which can have another value shape than the free argument of the map. The form splitter had bypassed the guard by calling ufl.Argument.reconstruct directly, and now calls a.reconstruct. Coargument loses its guard too, since the splitter also reconstructs a Coargument onto a subspace.

Vertex-only meshes

Only the output matrix uses allocation_integral_types. An inner matrix may live on a mesh without them, for example a vertex-only mesh.

Tests

  • test_solve_interp_hessian: a Newton solve with the Hessian of an interpolation onto a vertex-only mesh.
  • test_matfree_form_sum_uses_implicit_matrix and test_matfree_slate_sum_product_uses_implicit_matrix.
  • test_reduced_functional.py::test_interpolate runs a second-order Taylor test with the Hessian.
  • test_assemble_action_adjoint: Action(Adjoint(A), x) for a Matrix and for sums with a Form or Matrix, with and without an output tensor, against the assembled A^H x. It passes with the current Differentiate the dual slot of base form operators FEniCS/ufl#544 head and with the prototype of that restructuring (real mode, 1 rank), and fails with the prototype without the new branch.
  • test_interpolate_form_different_shapes: a 2-form dual slot whose arguments differ in shape. It failed with a shape mismatch when the target space was read from the highest-numbered argument.

🤖 Generated with Claude Code

Moved from fork PR #5478 to the upstream branch. Prepared with Codex.

danshapero and others added 30 commits September 22, 2026 14:28
Solving derivative(J, u) == 0 for a functional J that contains
interpolate(u, P), with P a space on a vertex-only mesh, failed in two
places in the base form assembler:

- The residual assembler of the solver uses mat_type="matfree", which
  skipped the expansion of derivatives. The chain rule through the
  interpolation turns the residual into a FormSum, which the 1-form
  assembler rejects. The mat_type only applies to 2-forms, so
  derivatives of 0-forms and 1-forms are now always expanded.
- Every inner matrix of the DAG was allocated with the integral types of
  the output matrix. The mass matrix on the vertex-only mesh then asked
  for interior facets, which that mesh does not have. Only the output
  matrix now uses those integral types.

The test also needs FEniCS/ufl#525, which fixes the symbolic Hessian.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Install UFL from danshapero/fix-bfo-hessian (FEniCS/ufl#525) in the
shared and documentation CI environments, so that CI tests this PR
against the matching UFL fix. Drop this commit once #525 merges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…late

Reconstructing an Interpolate now accepts the argument_slots keyword, and
replaces a plain UFL Coargument in the dual slot with a Firedrake Argument
on the dual of the target space. Also expand derivatives of matfree 0- and
1-forms that contain base form operators, so that the DAG traversal can
assemble them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Expanding the derivatives of a Form with base form operators may turn it
into another BaseForm, which determines how it is assembled. Plain Forms
and Slate tensors keep their type, so TSFC expands their derivatives.
A 'matfree' matrix is still expanded when its action is taken.

`preprocess_base_form` used `mat_type` to decide, so a 1-form with base
form operators, whose default `mat_type` is 'matfree', was not expanded.
`ufl.replace` then expanded it inside the assembly visitor, turning it
into a FormSum. Expand it in preprocessing instead, and drop the second
traversal in the visitor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
FEniCS/ufl#525 was split into #530, #531 and #532.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UFL now simplifies Action(N(u; v*), F) to N(u; F) for any base form
operator N and 1-form F, so the assembler can no longer split an operator
from its dual slot by building an Action: restructure case (5) and the
Interpolate split folded straight back.

Instead, when an operator cannot consume its assembled dual slot, assemble
N(u; vhat) with a coargument vhat and contract it with the assembled dual
slot:

- An Interpolate whose dual slot assembles to a Matrix.
- An ExternalOperator with a Cofunction or Matrix in its dual slot and no
  assembly method registered for it. `assembly_method()` looks this up.

The numerics of the Action branch move to `contract`, which both paths
use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An assembled Function has no arguments, so the contraction of an external
operator with its assembled dual slot failed. Use the arguments of
N(u; vhat) instead. Rename dual_slot_coargument to
replace_dual_slot_by_coargument, which is what it does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s integrals

UFL returns a ZeroBaseForm for a vanishing derivative, and a derivative
with base form operators may be a FormSum, neither of which has
integrals(). The Hessian of a solve whose residual is linear in its
solution raised AttributeError in _assemble_soa_eq_rhs. is_zero checks
for a ZeroBaseForm or an empty Form, as AssembleBlock already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UFL no longer simplifies Actions and Adjoints when they are constructed.
preprocess_base_form calls ufl's restructure_base_form after expanding
derivatives, and the Interpolators restructure the Actions they build
when splitting a Cofunction dual argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UFL now splits a rank 2 dual slot off a base form operator into an
Action, so an Interpolate or ExternalOperator never sees an assembled
Matrix in its dual slot.

An ExternalOperator without an assembly method for its assembled
Cofunction dual slot is now reconstructed once, with a coargument in
place of the Cofunction, and its result is contracted with the
Cofunction. This removes replace_dual_slot_by_coargument and
contracted_arguments. contract becomes a static method of
BaseFormAssembler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It now sits next to _matrix_free_assembler, the assembler it selects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pbrubeck and others added 2 commits November 5, 2026 11:00
Action(x, A) is the row x^H A, and Adjoint(Action(x, A)) is the column A^H x.
contract already assembles Action(x, A) with multHermitian, so the Adjoint of an
assembled Cofunction or Function returns it without conjugating again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiE9EdXTj3cqXdNadcRsGs
restructure_base_form builds a contraction placeholder for the target space
of a map, which can have another value shape than the free argument of the map.
The guard rejected this, and the form splitter bypassed it by calling the UFL
method directly. Remove the guards and call reconstruct on the argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiE9EdXTj3cqXdNadcRsGs
@pbrubeck pbrubeck added the ci:complex Run the test suite in complex mode label Nov 5, 2026
@pbrubeck
pbrubeck requested a review from connorjward November 6, 2026 15:51
Comment thread firedrake/assemble.py Resolved
@pbrubeck
pbrubeck requested a review from leo-collins November 6, 2026 16:21

@leo-collins leo-collins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm happy with this

Comment thread firedrake/interpolation.py Resolved

@connorjward connorjward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Otherwise looks good to me

Comment thread tests/pyop3/insn/test_loop.py Resolved
@pbrubeck
pbrubeck force-pushed the danshapero/interp-hessian branch from b78669d to 9d21060 Compare November 8, 2026 22:12
Co-authored-by: Leo Collins <leocollins511@gmail.com>
connorjward
connorjward previously approved these changes Nov 9, 2026

@connorjward connorjward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Subject to removing the UFL branch pointers and creating that issue, this is good to go.

@pbrubeck
pbrubeck requested a review from connorjward November 9, 2026 10:33
@pbrubeck pbrubeck removed the ci:complex Run the test suite in complex mode label Nov 9, 2026
Comment thread .github/actions/install/action.yml Resolved
Comment thread .github/workflows/core.yml Resolved
Co-authored-by: Pablo Brubeck <brubeck@protonmail.com>
@pbrubeck
pbrubeck merged commit d28a628 into main Nov 9, 2026
5 of 7 checks passed
@pbrubeck
pbrubeck deleted the danshapero/interp-hessian branch November 9, 2026 22:28
pbrubeck added a commit that referenced this pull request Nov 17, 2026
Assemble 0*dx and 0*ds, which raised an IndexError on the release
branch (#3598). Main already returns 0, since #5551 left the
preprocessing of a plain form to TSFC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pbrubeck added a commit that referenced this pull request Nov 27, 2026
* Assemble a ZeroBaseForm like any other form

Dispatch a ZeroBaseForm to the 0-, 1- and 2-form assemblers, so that
assemble() returns a zero float, Cofunction or Matrix instead of the
symbolic ZeroBaseForm. TSFC generates no kernels for a form without
integrals, so the assemblers only allocate the output, zero it, and
apply the boundary conditions. A zero 2-form with Dirichlet boundary
conditions therefore gets the identity on the boundary rows, as any
other assembled matrix does.

Use COMM_SELF for the compilation cache and the scalar output of a form
without a mesh, since such a form has no integrals and nothing is
compiled or reduced.

This fixes the adjoint of a solve whose residual depends on a control
only through a conditional predicate (#5571), where dF/dm is a
ZeroBaseForm.

Requires a UFL in which ZeroBaseForm provides integrals(), constants(),
terminal_numbering() and _cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Test the assembly of a vanishing 0-form

Assemble 0*dx and 0*ds, which raised an IndexError on the release
branch (#3598). Main already returns 0, since #5551 left the
preprocessing of a plain form to TSFC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* DROP BEFORE MERGE: Install the UFL branch for ZeroBaseForm

Install UFL from the branch pbrubeck/zero-base-form-cache over the UFL
main pin, in the composite install action and in the docs job. The
branch adds ZeroBaseForm.terminal_numbering() and _cache, which the
assembly of a ZeroBaseForm needs. Install it after Firedrake, since the
direct URL pin on UFL main would otherwise reinstall main over it.

Drop this commit once
FEniCS/ufl#556 merges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Remove temporary UFL branch override

Use UFL main now that FEniCS/ufl#556 has merged.

Assisted-by: OpenAI Codex

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LLM used An LLM was used in the production of this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interpolation inside a system matrix doesn't work

4 participants