firedrakeproject / firedrake

Firedrake is an automated system for the portable solution of partial differential equations using the finite element method (FEM)
https://firedrakeproject.org
Other
482 stars 157 forks source link

Derivative returns cofunction instead of function when the functional is independent of a control #3635

Closed colinjcotter closed 1 week ago

colinjcotter commented 2 weeks ago

Describe the bug If a ReducedFunctional is independent of a Control, it returns Cofunction instead of Function.

Steps to Reproduce

Run this script:

from firedrake import *
from firedrake.adjoint import *
import pytest
from numpy.testing import assert_allclose

continue_annotation()

mesh = UnitSquareMesh(4, 4)
R = FunctionSpace(mesh, "R", 0)
Controls = []

for i in range(3):
    x = function.Function(R, val=i)
    Controls.append(Control(x))
J = assemble(x * x * dx(domain=mesh))

Jhat = ReducedFunctional(J, Controls)
der = Jhat.derivative()
for der0 in der:
    print(type(der0))

The type is Cofunction for the first two components.

Expected behavior

The type should be Function in each case.