Skip to content

Make overloaded DirichletBC follow updates to its value Function - #99

Open
finsberg wants to merge 1 commit into
mainfrom
fix/dirichletbc-live-value
Open

finsberg wants to merge 1 commit into
mainfrom
fix/dirichletbc-live-value

Conversation

@finsberg

@finsberg finsberg commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Since every bc value is packed into a fresh Function on V (_pack_bc_value), bc.g is a snapshot of g taken at construction. The standard time-dependent pattern (build the bc once, then assign(new_value, g) before every solve) therefore silently solved with the construction-time value (typically zero), and the tape also kept depending on g's construction-time block variable, so replay and gradients were wrong too. test_time_dependent_bc_replay did not catch it, since it only compares replay against an equally wrong forward run.

DirichletBC now keeps its source expression and a compiled interpolation Expression, and exposes _ad_refresh(annotate), which LinearProblem/ NonlinearProblem call on every bc before each solve: it re-packs the current source value into bc.g, and when annotating and the source's block variables changed since the last packing, records a fresh ExprInterpolationBlock + DirichletBCBlock so the solve depends on the bc value at that point of the tape.

Since every bc value is packed into a fresh Function on V
(_pack_bc_value), `bc.g` is a snapshot of `g` taken at construction.
The standard time-dependent pattern -- build the bc once, then
`assign(new_value, g)` before every solve -- therefore silently solved
with the construction-time value (typically zero), and the tape also
kept depending on `g`'s construction-time block variable, so replay and
gradients were wrong too. test_time_dependent_bc_replay did not catch
it, since it only compares replay against an equally wrong forward run.

DirichletBC now keeps its source expression and a compiled interpolation
Expression, and exposes `_ad_refresh(annotate)`, which LinearProblem/
NonlinearProblem call on every bc before each solve: it re-packs the
current source value into `bc.g`, and when annotating and the source's
block variables changed since the last packing, records a fresh
ExprInterpolationBlock + DirichletBCBlock so the solve depends on the bc
value at that point of the tape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@finsberg
finsberg requested a review from jorgensd September 30, 2026 08:16

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant