Skip to content

BUG: Interpolating an expression whose Constant vanishes under differentiation fails an assertion in PyOP2 #5468

Description

@TrueCP6

Describe the bug
Interpolating a UFL expression fails with a bare AssertionError from PyOP2 if a Constant in the expression drops out when derivatives are expanded. For example, (c*y).dx(0) is identically zero, so c does not appear in the generated kernel, but PyOP2 still appears to count c as a kernel argument. Assembling a form that contains the same expression works correctly.

The failure also occurs when the vanished term is combined with other terms, e.g. (c*y).dx(0) + y. I found it after I changed physical parameters from Python floats to Constants, so that kernels are not recompiled for each parameter set. A velocity component of the form psi.dx(0), where psi does not depend on x, then caused this error.

Steps to Reproduce

  1. Code:
from firedrake import *

mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, "CG", 1)
x, y = SpatialCoordinate(mesh)
c = Constant(2.0)

assemble((c*y).dx(0) * dx)            # works
Function(V).interpolate((c*y).dx(0))  # AssertionError
  1. Command executed: python bug.py (serial). The same error occurs under mpiexec and on extruded meshes.

Expected behavior
The interpolation gives a zero-valued Function, as it does when Constant(2.0) is replaced by the float 2.0.

Error message

Traceback (most recent call last):
  File "bug.py", line 9, in <module>
    Function(V).interpolate((c*y).dx(0))  # AssertionError
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "petsc4py/PETSc/Log.pyx", line 250, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "petsc4py/PETSc/Log.pyx", line 251, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "/home/truec/firedrake/firedrake/function.py", line 382, in interpolate
    return assemble(interp, tensor=self, ad_block_tag=ad_block_tag)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "petsc4py/PETSc/Log.pyx", line 250, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "petsc4py/PETSc/Log.pyx", line 251, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "/home/truec/firedrake/firedrake/adjoint_utils/assembly.py", line 30, in wrapper
    output = assemble(form, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/assemble.py", line 151, in assemble
    return get_assembler(expr, *args, **kwargs).assemble(**assemble_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/assemble.py", line 433, in assemble
    result = BaseFormAssembler.base_form_postorder_traversal(self._form, visitor, visited)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/assemble.py", line 657, in base_form_postorder_traversal
    visited[e] = visitor(e, *(visited[arg] for arg in operands))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/assemble.py", line 429, in visitor
    return self.base_form_assembly_visitor(e, t, bcs, *operands)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/assemble.py", line 629, in base_form_assembly_visitor
    return interpolator.assemble(tensor=tensor, bcs=bcs, mat_type=self._mat_type, sub_mat_type=self._sub_mat_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/interpolation.py", line 364, in assemble
    result = self._get_callable(tensor=tensor, bcs=bcs, mat_type=mat_type, sub_mat_type=sub_mat_type)()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/interpolation.py", line 814, in _get_callable
    loops.extend(_build_interpolation_callables(sub_expr, sub_op2_tensor, self.access, self.subset, bcs))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<decorator-gen-31>", line 2, in _build_interpolation_callables
  File "/home/truec/firedrake/firedrake/utils.py", line 172, in wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/firedrake/interpolation.py", line 1172, in _build_interpolation_callables
    parloop = op2.ParLoop(*parloop_args)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/pyop2/parloop.py", line 706, in ParLoop
    return LegacyParloop(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/pyop2/parloop.py", line 735, in LegacyParloop
    global_knl = GlobalKernel(local_knl, global_knl_args,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/pyop2/global_kernel.py", line 294, in __init__
    for larg, garg in zip(local_kernel.arguments, arguments)
                          ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/truec/firedrake/pyop2/local_kernel.py", line 144, in arguments
    assert len(self.accesses) == len(self.dtypes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Environment:

  • OS: WSL2 (Ubuntu 24.04.5 LTS, kernel 6.6.87.2-microsoft-standard-WSL2)
  • Python version: 3.12.3
  • Firedrake: main at de93479 (2026-09-16)
  • Output of pip list:
pip list
Package                        Version       Editable project location
------------------------------ ------------- -------------------------
alabaster                      1.0.0
anyio                          4.15.1
asttokens                      3.0.2
attrs                          26.1.0
babel                          2.18.0
beautifulsoup4                 4.15.0
bibtexparser                   2.0.1
bleach                         6.4.0
cachetools                     7.1.8
certifi                        2026.7.22
cgen                           2025.1
charset-normalizer             3.5.1
checkpoint_schedules           1.0.4
click                          8.5.0
codepy                         2025.1
colorama                       0.4.6
comm                           0.2.3
constantdict                   2025.3
contourpy                      1.3.3
cycler                         0.12.1
cyclopts                       4.25.2
Cython                         3.3.0
debugpy                        1.8.21
decorator                      4.4.2
defusedxml                     0.7.1
docstring_parser               0.18.0
docutils                       0.22.4
executing                      2.2.1
fastjsonschema                 2.22.2
fenics-ufl                     2026.2.0.dev0
firedrake                      2026.5.0.dev0 /home/truec/firedrake
firedrake-fiat                 2026.5.0.dev0
firedrake-rtree                2026.2.0
fonttools                      4.65.0
genpy                          2022.1
h11                            0.16.0
h5py                           3.16.0
hatchling                      1.32.0
idna                           3.19
imagesize                      2.0.1
immutabledict                  4.3.1
iniconfig                      2.3.0
ipykernel                      7.3.0
ipympl                         0.10.0
ipython                        9.17.1
ipython_pygments_lexers        1.1.1
ipywidgets                     8.1.9
islpy                          2026.1
jedi                           0.20.0
Jinja2                         3.1.6
jsonschema                     4.26.0
jsonschema-specifications      2025.9.1
jupyter_client                 8.10.0
jupyter_core                   5.9.1
jupyterlab_pygments            0.3.0
jupyterlab_widgets             3.0.17
jupytext                       1.19.5
kiwisolver                     1.5.1
latexcodec                     3.0.1
libsupermesh                   2026.4.dev0
loopy                          2025.2
Mako                           1.4.1
markdown-it-py                 4.2.0
MarkupSafe                     3.0.3
matplotlib                     3.11.1
matplotlib-inline              0.2.2
mdit-py-plugins                0.6.1
mdurl                          0.1.2
meson                          1.12.0
meson-python                   0.21.1
mistune                        3.3.4
mpi-pytest                     2026.0
mpi4py                         4.1.2
mpmath                         1.3.0
nbclient                       0.11.0
nbconvert                      7.17.1
nbformat                       5.11.1
nbsphinx                       0.9.8
nest-asyncio2                  1.7.2
numpy                          2.5.3
numpy-typing-compat            20260602.2.5
numpydoc                       1.10.0
optype                         0.18.0
packaging                      26.3
pandas                         3.0.6
pandocfilters                  1.5.1
parso                          0.8.7
pathspec                       1.1.1
petsc4py                       3.26.0.dev0
petsctools                     2026.1.dev0
pexpect                        4.9.0
pillow                         12.3.0
pip                            24.0
pkgconfig                      1.6.0
platformdirs                   4.11.8
pluggy                         1.6.0
pooch                          1.9.0
progress                       1.6.1
prompt_toolkit                 3.0.53
psutil                         7.2.2
ptyprocess                     0.7.0
pure_eval                      0.2.4
Py-BOBYQA                      1.5.0
pyadjoint-ad                   2026.4.1
pybind11                       3.1.0
pybtex                         0.26.1
pybtex-docutils                1.0.3
pycparser                      3.0
Pygments                       2.21.0
pylatexenc                     2.11
pylit                          0.8.0
pymbolic                       2025.1
pyparsing                      3.3.2
pypdf                          6.19.0
pyproject-metadata             0.12.1
pytest                         9.1.1
python-dateutil                2.9.0.post0
pytools                        2026.1.1
pyvista                        0.49.0
pyvista-validation             0.2.2
PyYAML                         6.0.3
pyzmq                          27.2.0
recursivenodes                 0.2.0
referencing                    0.37.0
requests                       2.34.2
rich                           15.0.0
rich-rst                       2.1.0
roman-numerals                 4.1.0
rpds-py                        2026.6.3
rtree                          1.4.1
scikit_build_core              1.0.3
scipy                          1.18.1
scipy-stubs                    1.18.1.0
scooby                         0.12.0
setuptools                     84.0.0
siphash24                      1.9
six                            1.17.0
snowballstemmer                3.1.1
soupsieve                      2.9.2
Sphinx                         9.1.0
sphinx-autobuild               2025.8.25
sphinx-copybutton              0.5.2
sphinx-reredirects             1.1.0
sphinxcontrib-applehelp        2.0.0
sphinxcontrib-bibtex           2.7.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
sphinxcontrib-svg2pdfconverter 2.1.0
sphinxcontrib-youtube          1.5.0
stack-data                     0.6.3
starlette                      1.6.0
symengine                      0.14.1
sympy                          1.14.0
tinycss2                       1.5.1
tomlkit                        0.15.1
tornado                        6.5.8
traitlets                      5.16.1
trove-classifiers              2026.6.1.19
typing_extensions              4.16.0
urllib3                        2.7.0
uvicorn                        0.52.4
vtk                            9.7.0
watchfiles                     1.2.0
wcwidth                        0.8.3
webencodings                   0.6.1
websockets                     17.1
widgetsnbextension             4.0.16
  • Any relevant environment variables or modifications: none

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