BUG: Interpolating into a Real space gives an adjoint scaled by the number of cells

Abierto
#5,644 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
22/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

The bug sits in how interpolation into the Real space is assembled and annotated for the adjoint, so start with the interpolate path that handles an R-space target and with the pyadjoint annotation for it. Reproduce the three-mesh script from the issue and confirm paired and dJ/dm scale with num_cells. Done means the paired value equals exp(0.3) and the derivative equals 2 exp(0.6) on all meshes, matching a finite-difference check.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug

Describe the bug

Interpolating an expression into a Real space gives the correct forward value. Pairing the same interpolation with a cofunction, or taking its derivative through the tape, gives a result multiplied by the number of cells in the mesh. A Taylor test on a functional that depends on such an interpolation converges at first order.

Steps to Reproduce

from firedrake import *
from firedrake.adjoint import *

for n in (1, 2, 4):
    mesh = UnitSquareMesh(n, n)
    R = FunctionSpace(mesh, "R", 0)
    m = Function(R).assign(0.3)

    # Forward: correct
    u = assemble(interpolate(exp(m), R))

    # Pairing with a cofunction equal to one: expected exp(0.3)
    c = Cofunction(R.dual())
    c.dat.data[:] = 1.0
    paired = assemble(interpolate(exp(m), c))

    # Derivative through the tape: expected d/dm exp(2m) = 2 exp(0.6)
    continue_annotation()
    J = assemble(assemble(interpolate(exp(m), R)) ** 2 * dx)
    pause_annotation()
    dJdm = ReducedFunctional(J, Control(m)).derivative().dat.data_ro[0]
    get_working_tape().clear_tape()

    print(
        f"cells={mesh.num_cells():2d}  forward={u.dat.data_ro[0]:.6f}  "
        f"paired={float(paired):.6f}  expected={float(exp(0.3)):.6f}  "
        f"dJ/dm={dJdm:.6f}  expected={2 * float(exp(0.6)):.6f}"
    )

Output:

cells= 2  forward=1.349859  paired=2.699718  expected=1.349859  dJ/dm=7.288475  expected=3.644238
cells= 8  forward=1.349859  paired=10.798870  expected=1.349859  dJ/dm=29.153901  expected=3.644238
cells=32  forward=1.349859  paired=43.195482  expected=1.349859  dJ/dm=116.615603  expected=3.644238

Expected behavior

The paired value should be exp(0.3) and the derivative 2 exp(0.6) on every mesh, independent of the number of cells. Instead both are exactly the number of cells times the expected value. A central finite difference through the same ReducedFunctional gives the expected derivative.

Error message

None. The result is silently wrong.

Environment:

  • OS: macOS 15.6 (arm64)
  • Python version: 3.14.7
  • Firedrake version: 2026.10.0, with pyadjoint-ad 2026.10.0
  • Run in serial

Additional Info

Evaluating the expression into the Real space as its integral over the mesh divided by the mesh volume, assemble(expr * dx) / volume, differentiates correctly and works around the problem.

Lenguaje dominante
Python
Estrellas
684
Forks
204
Merge medio
4 d 14 h
PR fusionados (30 d)
46

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de firedrakeproject/firedrake

Todos los issues de firedrakeproject/firedrake

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.