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

オープン
#5,644 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
22/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
684
フォーク
204
平均マージ
4日 14時間
マージ済み PR(30日)
46

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

firedrakeproject/firedrake のほかの issue

firedrakeproject/firedrake の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。