Skip to content

Tangent linear model of Function.assign from an AdjFloat fails with AttributeError #5782

Backlog

Description

@sghelichkhani

When a Function is assigned from an AdjFloat control, rf.derivative() works but rf.tlm(h) and rf.hessian(h) fail with AttributeError: 'AdjFloat' object has no attribute 'ufl_function_space'. This happens in serial, for CG and R target spaces.

from firedrake import *
from firedrake.adjoint import *

continue_annotation()
mesh = UnitSquareMesh(4, 4)
V = FunctionSpace(mesh, "CG", 1)
a = AdjFloat(0.5)
u = Function(V).assign(a)
J = assemble(u**2 * dx)  # J = a**2
pause_annotation()
rf = ReducedFunctional(J, Control(a))
print("derivative", rf.derivative(), "expected", 2 * 0.5)
print("tlm", rf.tlm(AdjFloat(1.0)), "expected", 2 * 0.5)

The derivative prints 1.0 up to round-off, as expected. The TLM call should also give 1.0, but it raises

  File ".../firedrake/adjoint_utils/blocks/assembly.py", line 137, in evaluate_tlm_component
    dform += firedrake.action(firedrake.derivative(form, c_rep),
  ...
  File ".../ufl/algorithms/formtransformations.py", line 480, in compute_form_action
    elif coefficient.ufl_function_space() != fs:
AttributeError: 'AdjFloat' object has no attribute 'ufl_function_space'

I see this on main (b6bbb54) and on release (881e99d), with pyadjoint 1b85329.

As far as I can tell the cause is FunctionAssignBlock.evaluate_tlm_component in firedrake/adjoint_utils/blocks/function.py. For an assignment without an expression it returns tlm_inputs[0] as the TLM of the output, so the TLM of the Function is the AdjFloat tangent of the control. The next block then fails when it uses it as a Function. Assigning from a Real Function to a CG Function has the same problem in a milder form: the TLM of the CG Function is then a Real Function, which happens to give the right numbers in assemble.

Found with Claude Code (Opus 5.5).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions