Description
Describe the bug
Adjoint calculation with DirichletBC as Control does not work with NonlinearVariationalSolver.
Steps to Reproduce
import firedrake as fd
from firedrake import inner, grad, dx
from firedrake.adjoint import Control, ReducedFunctional
from pyadjoint import taylor_test, continue_annotation
continue_annotation()
def test_wrt_function_dirichlet_boundary():
mesh = fd.UnitSquareMesh(100, 100)
V = fd.FunctionSpace(mesh, "CG", 1)
R = fd.FunctionSpace(mesh, "R", 0)
bc_func = fd.Function(R, val=2.0)
bc = fd.DirichletBC(V, bc_func, 1)
u_ = fd.Function(V)
v = fd.TestFunction(V)
a = inner(grad(u_), grad(v)) * dx
L = v * dx
# Crashes when using NonlinearVariationalSolver.solve()
# Error: assert taylor_test(Jhat, bc_func, bc_func_perturb) > 1.9
problem = fd.NonlinearVariationalProblem(a - L, u_, bcs=bc)
solver = fd.NonlinearVariationalSolver(problem)
solver.solve()
# Passes when using solve()
# F = a - L
# fd.solve(F == 0, u_, bc)
J = fd.assemble(u_**2 * fd.dx)
c = Control(bc_func)
Jhat = ReducedFunctional(J, c)
bc_func_perturb = fd.Function(R, val=2.0)
assert taylor_test(Jhat, bc_func, bc_func_perturb) > 1.9
if __name__ == "__main__":
test_wrt_function_dirichlet_boundary()
Expected behavior
Describe what you expected to happen, in the case of a regression,
include details of a setup where this behaviour was last seen.
Error message
Running Taylor test
Computed residuals: [0.0933330000202759, 0.04666650001013795, 0.023333250005068976, 0.011666625002534488]
Computed convergence rates: [np.float64(1.0), np.float64(1.0), np.float64(1.0)]
Traceback (most recent call last):
File "/Users/miguel/work/glaciercore/tests/functional/adjoint/mfe-bc-adjoint.py", line 39, in <module>
test_wrt_function_dirichlet_boundary()
File "/Users/miguel/work/glaciercore/tests/functional/adjoint/mfe-bc-adjoint.py", line 35, in test_wrt_function_dirichlet_boundary
assert taylor_test(Jhat, bc_func, bc_func_perturb) > 1.9
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Environment:
- OS: MacOS add this as a label too!
- Python version: 3.11.8
- Output of
pip list
pip list output
Package Version Editable project location
alabaster 1.0.0
annotated-types 0.7.0
asttokens 3.0.0
attrs 25.3.0
babel 2.17.0
cachetools 6.0.0
cattrs 24.1.2
certifi 2025.4.26
cfgv 3.4.0
cgen 2020.1
charset-normalizer 3.4.2
checkpoint_schedules 1.0.4
click 8.2.1
codepy 2025.1
colorama 0.4.6
constantdict 2025.1.1
contourpy 1.3.2
coverage 7.9.1
cycler 0.12.1
Cython 3.0.11
decorator 4.4.2
defusedxml 0.7.1
distlib 0.3.9
docutils 0.21.2
execnet 2.1.1
executing 2.2.0
fenics-ufl 2025.1.0
filelock 3.18.0
firedrake 2025.4.1
firedrake-fiat 2025.4.0
flexcache 0.3
flexparser 0.4
fonttools 4.58.4
genpy 2022.1
gitdb 4.0.12
GitPython 3.1.41
glaciercore 0.0.0 /Users/miguel/work/glaciercore
gmsh 4.11.1
h5py 3.13.0
identify 2.6.12
idna 3.10
ImageHash 4.3.1
imageio 2.37.0
imagesize 1.4.1
immutabledict 4.2.1
iniconfig 2.1.0
ipython 9.3.0
ipython_pygments_lexers 1.1.1
islpy 2025.1.5
jedi 0.19.2
Jinja2 3.1.6
kiwisolver 1.4.8
lazy_loader 0.4
libsupermesh 2025.3.0
loopy 2025.1
Mako 1.3.10
markdown-it-py 3.0.0
MarkupSafe 3.0.2
matplotlib 3.10.0
matplotlib-inline 0.1.7
mdurl 0.1.2
memory-profiler 0.61.0
meshio 5.3.5
mistune 3.1.3
mpi-pytest 2025.5.0
mpi4py 4.0.3
mpmath 1.3.0
mypy 1.16.0
mypy_extensions 1.1.0
networkx 3.5
nodeenv 1.9.1
numexpr 2.11.0
numpy 2.2.3
opencv-contrib-python 4.11.0.86
packaging 25.0
parso 0.8.4
pathspec 0.12.1
petsc4py 3.23.3
pexpect 4.9.0
pillow 11.2.1
Pint 0.24.4
pip 24.0
pkgconfig 1.5.5
platformdirs 4.3.8
pluggy 1.6.0
pre_commit 4.2.0
progress 1.6
prompt_toolkit 3.0.51
psutil 7.0.0
ptyprocess 0.7.0
pudb 2025.1
pure_eval 0.2.3
pyadjoint-ad 2025.4.0
pycparser 2.22
pydantic 2.11.7
pydantic_core 2.33.2
Pygments 2.19.1
pymbolic 2024.2.2
pyparsing 3.2.3
pytest 8.3.4
pytest-cov 6.2.1
pytest-proceed 0.1.5
pytest-pudb 0.7.0
pytest-split 0.10.0
pytest-timeout 2.3.1
pytest-xdist 3.7.0
python-dateutil 2.9.0.post0
pytools 2025.1.6
PyWavelets 1.8.0
PyYAML 6.0.2
rdp 0.8
recursivenodes 0.2.0
requests 2.32.3
rich 14.0.0
rtree 1.4.0
ruff 0.11.0
scikit-image 0.25.1
scipy 1.15.3
setuptools 65.5.0
setuptools-scm 8.3.1
shapely 2.1.1
siphash24 1.7
six 1.17.0
slepc4py 3.23.1
smmap 5.0.2
snowballstemmer 3.0.1
Sphinx 8.0.2
sphinx-autodoc-typehints 2.5.0
sphinx-click 6.0.0
sphinx_mdinclude 0.6.2
sphinx-rtd-theme 3.0.1
sphinxcontrib-applehelp 2.0.0
sphinxcontrib-devhelp 2.0.0
sphinxcontrib-htmlhelp 2.1.0
sphinxcontrib-jquery 4.1
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 2.0.0
sphinxcontrib-serializinghtml 2.0.0
SQLAlchemy 2.0.41
sqlmodel 0.0.22
stack-data 0.6.3
symengine 0.14.1
sympy 1.14.0
tifffile 2025.6.11
traitlets 5.14.3
types-Pillow 10.2.0.20240822
typing_extensions 4.13.2
typing-inspection 0.4.1
urllib3 2.4.0
urwid 3.0.2
urwid_readline 0.15.1
virtualenv 20.31.2
vtk 9.4.1
wcwidth 0.2.13
wheel 0.45.1
yamllint 1.37.0
- Any relevant environment variables or modifications [eg: PYOP2_DEBUG=1]
Additional Info
Reverting the changes in firedrake/adjoint_utils/dirichletbc.py done by https://github.com/firedrakeproject/firedrake/pull/4284/files makes it work again