Repository navigation
A dimensional value in the residual no longer stops an adjoint gradient (#765) - #1047
Conversation
…nt (#765) The peels that expand named expressions for the chain rule kept a named expression as a leaf only when its value was a bare number. A dimensional value is a Pint quantity, not a SymPy object, so the substitution raised SympifyError and took out every gradient of a residual holding one, whatever the parameter: a dimensional constant, or a stock ViscoPlasticFlowModel, whose yield_stress_min defaults to 0 Pa even in a model with no reference quantities. The leaf test was written out three times: adjoint._peel, adjoint._peel_except, and the solver's _peel_except. One rule, adjoint._is_leaf, now serves all three, and the solver's copy delegates. A dimensional value is a leaf for the same reason as a bare number: the JIT passes it as a run-time constant. test_0032 (tier B): dJ/dalpha = -J/alpha on linear Stokes, for a stock viscoplastic model and for a dimensional factor beside the parameter. Both raised on 8f0ed0a. 42 serial adjoint and transcript files: 546 passed. fault_friction.py -uw_check_only 1: adjoint/FD 0.99999 to 1.00000, as on base. Underworld development team with AI support from Claude Code
Adversarial review (H1 worker) of the combined branch 19fdbc0, from
|
| # | sev | finding | evidence |
|---|---|---|---|
| F1 | Blocker | integral() is not non-dimensional. Integral.evaluate labels its result in coord_units**dim, which is km² for a 30 km box, but the number is in model lengths. So non_dimensionalise divides by an extra (L_ref/km)^dim. On a 30 km unit box: ∫1 returns 1.111e-3 instead of 1, and ∫x, ∫σ, ∫η and ∫η·x are off by the same 1/900. With the flag off, the factor also depends on the integrand: ∫x is off by 3.7e-5 and ∫σ by 1/(900·σ_ref). |
p_integral_frame.py → p_integral_frame.out |
| F2 | Blocker | The gradient now mixes frames. J, the volume sensitivity and the explicit term go through integral() (volume, /L²). The facet term goes through it too, but scales as /L. Field duals (dual_on, inner), the misfit duals that drive μ, and the essential-datum term stay in PETSc's frame. On a units model whose parameters are bare numbers, base was right and the branch is wrong: with the flag on, the traction (facet) dJ/dα is off ×30 and the field dual ×900; with the flag off, the volume dJ/dα is off ×30, traction ×900 and the field dual ×27000 (misfit u_y·sin(πx) + u_x·y). On the plain misfit u_y·sin(πx), base matches FD in every case except the datum (see F4); the branch is off ×30 (traction) and ×900 (field dual), in both flag modes. |
p_grad_frames.py; grad_base*.out, grad_branch*.out |
| F3 | Blocker | fault_friction.py -uw_check_only 1 (units model with volume + Top misfit terms). Base: adjoint/FD ratio 0.99999 to 1.00000 on all four segments. Branch: ratios 49.17, 3.22, 3.74 and 4.42, and J falls from 1.484e-3 to 6.50e-5. |
ex_dev8f.log, ex_h765.log |
| F4 | High (pre-existing, still live) | The essential-datum sensitivity is wrong whenever reference quantities are set, on base and on the branch, with either flag. For α on a Dirichlet lid we get 5.55e6 against an FD of 6.17e-3: a factor of 9.0e8, which is (30 km in m)². _essential_datum_sensitivity reads uw.function.evaluate(expr, X[node]) and the nodal coordinates in metres, which is the #771 frame leak at a site #763 does not list. [read] for the site; the number was run. |
grad_* "datum" rows |
| F5 | Medium | Attack 2 (refs set, flag off), answered. Integral.evaluate returns PETSc's raw value (SI magnitudes on model-length coordinates), labelled integrand_units*coord_units**dim. So non_dimensionalise is wrong there. On a well-conditioned flag-off model (η = 2 Pa s, η_ref = 1e21), the branch gives dJ/dα = 5.44e-29 against -J/α = 5.44e-08, which is off by η_ref. |
p_minus11.py → p_minus11_branch.out |
| F6 | Info | The -11 in the flag-off rig is conditioning, not this change. Whenever PETSc sees a viscosity of 1e12 or more, the forward solve already fails (reason -3, 0 iterations, u = 0, with no exception raised). The adjoint then fails with -11. This happens with the flag on too, when η_ref = 1 leaves 1e22 in PETSc's frame. Base shows the same -11. A silent forward failure (-3 with no raise) deserves its own issue. | p_minus11_base.out, p_minus11_branch.out |
| F7 | Low | test_0025 cannot detect F1 or F2. A common factor on J and dJ/dα passes -J/α, and both cases use only volume terms. The candidate fix below passes test_0025 as well. An absolute assertion would catch both: g["J"] equal to the no-units J of the same ND problem, or J against an FD that sums frames (a field dual, or a facet term). |
t0025_alt_raw.log: 2 passed |
| F8 | Info [read] | With the peel change, _residual_is_linear_in_unknown now returns True for linear models that carry dimensional constants. Before, SympifyError was swallowed into False, which forced a consistent-tangent rebuild for the adjoint. The new answer is correct. The except Exception: return False at petsc_generic_snes_solvers.pyx:2879 is an uncommented swallow (Charter §4). It predates this change. |
code |
The fix we propose for #763
The adjoint state, the residual and the duals all live in the frame PETSc integrated in, so integral() should return
PETSc's raw value. Neither float(evaluate()) nor non_dimensionalise(evaluate()) gives that number. As a probe we
patched integral() to call evaluate with units inference switched off (alt765_raw.py, p_grad_frames.py --raw).
With that patch:
- every case matches FD in every mode (no units, flag on, flag off) except the datum (F4). That covers volume, facet,
explicit, field dual and dimensional η. (grad_branch_raw.out) on viscgives J = -1.9584e-04, the no-units value of the same ND problem.- flag off with η = 2 Pa s or 1e6 Pa s: dJ/dα equals -J/α.
- test_0025: 2 passed.
In the library, that is an evaluate(dimensional=False) (or a private raw path) on Integral and BdIntegral, used by
adjoint.integral().
The author's questions
- Peel change:
dual_on,misfit_duals,_reads_of, text readers, and a bare-number parameter. The peel change is
safe.- A leaf's value is a number, None or a Pint quantity, so it can hold no field symbol. Of the named expressions in
seven stock solvers' F0/F1, no leaf hides a field or the parameter (p_leaf_census.out). Stock VP, VEP and TI
models carry 1 Pa s and 0 Pa defaults, which is why every gradient through them failed on base. - A mutable-Matrix value counts as a leaf under the rule, but
uw.expressionarithmetic expands it before the peel
sees it (p_matrix_leaf.out). - dJ/dα for a bare-number α matches FD for a viscosity, a datum, a traction, an explicit misfit and a field dual. In
the no-units model it is bitwise equal to base for J and dJ/dα in all five cases (bitwise_nounits.out). - Text readers [read]:
misfit_dualsand_reads_ofonly usetoken in textas a gate, and a leaf's name in the
text at most adds a zero dual. H4's_datum_reads_of(cb23e50, Adjoint field dual is silently zero for a field that enters through a Dirichlet datum #787) also matchestoken in str(_peel(bc.fn)).
A bare-number expression whose printed name contains a field token, such as{T}_{wall}beside a fieldT, now
stays in that text and would trigger a spurious refusal. The case is far-fetched. Matching on.atoms(Function)
instead of text would close it. Not run, since we did not rebuild H4's branch.
- A leaf's value is a number, None or a Pint quantity, so it can hold no field symbol. Of the named expressions in
- Refs set, flag off. See F5 and F6:
non_dimensionaliseis wrong in that mode, and the -11 is conditioning. - Parallel. test_0025 at np 2: 2 passed. The 24 adjoint files at np 2, base against branch: see
"Parallel" below.
Parallel
There is no parallel regression. The details are in np2_summary.out and np2/.
- All adjoint files at np 2. We ran each of the 24 adjoint files on its own (
mpirun -n 2, supervised), base
against branch. Both ranks give identical outcomes on every file.- The same failures appear on both builds: test_0019
test_poisson_gradient_in_the_diffusivity_matches_finite_differences
(ValueError: zero-size array to reduction, an empty-rankmaxin the test), test_0812
test_poisson_check_bc_values, and on rank 1 only, test_0018..._notes_a_refusal_once_per_changeand test_1057
..._drops_into_the_supg_solver. - test_0026 aborts (exit 59) and test_1131 times out (exit 124) on both builds.
- These are pre-existing and need their own triage. test_0019 at np 2 is the only FD check in the adjoint suite, and
it is red on development. - One mpirun over all 24 files died with exit 59 on both builds, which is why we ran them one per file.
- The same failures appear on both builds: test_0019
- test_0025 at np 2. 2 passed.
- Sensitivity against FD at np 2 (
p_grad_frames.py --plain,grad_np2_*.out).- No units: visc 0.99999999, traction 1.00000000 and field dual 1.00000000, identical to np 1 and to base.
- Flag on with dimensional η: adjoint 1.0877e-08 against FD 1.0877e-08. That is self-consistent, but it carries the
F1 factor in J.
Mutants and the base check
- test_0025 on 8f0ed0a (branch tests, base library): 2 failed,
SympifyError: 0 [pascal]. - Mutant, old peels (
mut765_oldpeel.py): 2 failed. Caught. - Mutant,
float(evaluate())(mut765_nond.py): 1 failed (9.79e15 against 9.79e-06). Caught, by the units case only. - Candidate fix (
alt765_raw.py): 2 passed. The test can't tell F1 from correct.
Merge conditions
- Land the _peel_except raises SympifyError on a dimensional expression, killing EVERY row of a sensitivity table #765 half (
_is_leafand the three peels) as it is, with test_0025's viscoplastic case. - Replace
integral()with PETSc's raw value, notnon_dimensionalise. - Add a test that fails on the current branch: a units model whose J is checked against the no-units ND J, and an FD
check through a facet term and a field dual.p_grad_frames.pyis a ready rig. - Make
fault_friction.py -uw_check_only 1pass again. It is the only existing end-to-end FD check in a units model. - F4 and the silent forward failure in F6 get issues. They are not this PR's to fix.
Issue verdicts
- _peel_except raises SympifyError on a dimensional expression, killing EVERY row of a sensitivity table #765: fixed by the leaf half: test_0025's viscoplastic case, and the probes above.
- solver.sensitivity() returns dJ/dm in SI, not in the non-dimensional system: wrong by the reference scales #763: still live. The branch moves the error rather than removing it: from 1e21 to
L_ref^dim, and into
facet terms and field duals.
What we could not test
- H4's
_datum_reads_oftogether with this change: [read] only, since cb23e50 is not built. - TranscriptAdjoint in a units model, e.g.
sinker_transcript. It is the sameintegral()plus raw duals [read], so it
should be inconsistent the same way. Not run. - np 4.
Underworld development team with AI support from Claude Code
The peel found the named expressions to expand with
fn_extract_expressions, which misses a bare named expression (it searches
from the expression's .sym) and any product made only of named ones (its
exhaustion test stops when every atom is a symbol). A misfit written as
uw.expression("m", u_y sin(pi x)), or m*k, kept no field after the peel,
so its dual and dJ/dalpha were silently zero.
The peel now reads the named expressions at each level from the tree's own
Symbol atoms. extract_expressions itself is left alone, with a TODO(BUG):
the JIT and other callers use it.
Found by H4 (a bare named expression in a boundary condition) in the
review of its #787 branch. test_0032 gains both cases; both gave
dJ/dalpha = 0 before. 42 serial adjoint and transcript files: 548 passed.
fault_friction.py -uw_check_only 1: 0.99999 to 1.00000, as before.
Underworld development team with AI support from Claude Code
|
Added 08bc58b: the peel missed a misfit written as a named expression, and gave dJ/dα = 0 for it (pre-existing, found by H4). The description is updated. I have asked H1 to review this commit. Underworld development team with AI support from Claude Code |
Underworld development team with AI support from Claude Code
When #765 was split out of 19fdbc0, the solver file was taken whole from that commit, which predates #1036. That reverted #1036's _release_fac_keys and _free_after_install calls in the solver, and test_1149 failed three ways (FAC keys left in the options database, PETSc error 73). The file is now development's, with only this branch's change: _peel_except delegates to adjoint._peel_except. test_1149 and test_0032: 16 passed. Underworld development team with AI support from Claude Code
|
The red shard 4/6 (test_1149) was this branch's fault. When #765 was split out, the solver file was taken whole from an older commit, which reverted #1036's Underworld development team with AI support from Claude Code |
The peels stopped after eight levels and returned what was left, so a misfit or residual named more deeply kept a name in front of its field, the derivative through it was zero, and the gradient came back silently 0 (H1's review, F2 on #1047). They now run until nothing is left to expand, and raise RecursionError past 64 levels rather than return a half-expanded expression. test_0032 gains a misfit named ten levels deep: dJ/dalpha was 0.0 before. 129 serial adjoint, transcript, Integral and custom-MG files: 1159 passed, none raising RecursionError. Underworld development team with AI support from Claude Code
…esidual (H1 re-check F1-F5) - F1 (high): the upstream check read only the producing solve's residual. A control that reached it through its own boundary conditions slipped past: p in Z's source, with A's datum g = z, gave 2.9e-2 against FD 1.3e-1, and a control field only in A's datum gave 0 against 0.46. _control_reach now marks a solve reached when it reads a parameter or a requested field (in its residual or a condition), or reads the output of a reached solve. It iterates to a fixed point over every solve in the run, and the walk refuses a pending output only if its solve is reached. - F2: an upstream solve that reads only a fixed coefficient field (no solve produces it) is no longer refused; it agrees with FD. - F3: parameters are found per element with _peel_except, after merging development (#1047's peel), so a p behind a bare named expression in an upstream datum refuses. - F4: the near-name test's field now prints as f, so \bar{f} has teeth. - F5: the message names the reading solve, the variables and their kind of condition. It no longer claims the dual "would be exactly zero" in every case, and the parameter line is gone. H1's 19 walk probes (p_walk2.py) against this build: every refusal is one the unguarded walk gets wrong, every computed gradient matches FD. test_0029 grows to 12 tests. The adjoint suite passes. Underworld development team with AI support from Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dimensional value in a residual no longer stops an adjoint gradient, and a misfit written as a named expression gets its gradient. Fixes #765.
What was wrong
The peels expand named expressions so the chain rule has something to hold on to. They kept a named expression as a leaf only when its value was a bare number. A dimensional value is a Pint quantity, not a SymPy object, so substituting it raised
SympifyError. That took out every gradient of a residual holding one, whatever the parameter. Two routes reach it:SympifyError: 1e+22 [pascal * second];ViscoPlasticFlowModel, even in a model with no reference quantities, becauseyield_stress_mindefaults to0 Pa:SympifyError: 0 [pascal].The change
The leaf test was written out three times:
adjoint._peel(the first to fail, throughfield_dualsand_reads_of),adjoint._peel_except, and the solver's_peel_except. One rule,adjoint._is_leaf, now serves all three, and the solver's copy delegates.A dimensional value is a leaf for the same reason a bare number is: the JIT passes it as a run-time constant.
_peelnow also leaves bare numbers in place. Its callers only differentiate with respect to field symbols, and H1 found J and dJ/dα bitwise equal to base in every no-units probe case.A second peel defect (08bc58b, found by H4)
The peel found the named expressions to expand with
fn_extract_expressions. That function misses a bare named expression, because it searches from the expression's.sym. It also misses any product made only of named expressions, because its exhaustion test stops when every atom is a symbol.So a misfit written as
uw.expression("m", u_y sin(pi x)), or asm * k, kept no field after the peel, and its dJ/dα came back silently zero. This was already true on development.The peel now reads each level's named expressions from the tree's own
Symbolatoms.extract_expressionsitself is left alone, with aTODO(BUG), because the JIT and other callers use it.Tests
tests/test_0032_adjoint_peel_keeps_dimensional_leaves.py(tier B) uses dJ/dα = -J/α on linear Stokes for four cases:docs/examples/adjoint/fault_segments/fault_friction.py -uw_check_only 1: adjoint/FD 0.99999 to 1.00000, as on base.Not in this PR
Checklist (UW3 Style Charter)
level_*andtier_*markersUnderworld development team with AI support from Claude Code