Dolfin adjoint optimization cost function possibilities

Dear all,

I was wondering if dolfin adjoint can deal with functions for forms as cost functions. Specifically, id like to know if it can differentiate and optimize things like

J(W) = \int {f(W)}dx+\log (\int u_W(t = T, x)dx),

where u_W(t = T,x) is the solution at time T of a partial differential equation containting W as a source term, i.e something like \partial_t u_W = \nabla^2 u_W + W(x,t)u_W. I know I could just compute the derivatives manually in terms of the inner forms:

\frac{\delta J(W)}{\delta W} = \frac{\delta }{\delta W} \int {f(W)}dx+ \frac{1 }{\int u_W(t = T, x)dx} \frac{\delta }{\delta W} \int u_W(t = T, x)dx,

But id like to know if i can use the functional J(W) directly in dolfin adjoint functions such as ReducedFunctional or minimize.

Thanks in advance!

log is implemented in pyadjoint (as of 2025.04.0): pyadjoint/pyadjoint/adjfloat.py at a4f940a7899809a65d43d9fb9e6049a70755a30a · dolfin-adjoint/pyadjoint · GitHub

import pyadjoint
import numpy as np
from dolfin_adjoint import *

z = pyadjoint.AdjFloat(0.2)
g = pyadjoint.adjfloat.log(z**2)

c = pyadjoint.Control(z)

J = g**2
Jhat = pyadjoint.ReducedFunctional(J, c)
dJdc = Jhat.derivative()

# Reference
# 2 * g * g'(z)  =  2 log(z^2) * 1/(z**2)* 2z = 4 log(z^2)/z
print(dJdc, 4 * np.log(float(z) ** 2) / float(z))

which yields:
-64.377516497364 -64.37751649736401
Currently, one would have to upgrade to a pyadjoint version past the commit of:

One can do this with python3 -m pip install -U pyadjoint-ad
The status of dolfin-adjoint with the latest released version of pyadjoint has been summarized at: needs pinning `pyadjoint-ad<2025.04` · Issue #29 · dolfin-adjoint/dolfin-adjoint · GitHub