Skip to content

_peel_except raises SympifyError on a dimensional expression, killing EVERY row of a sensitivity table #765

Open

Description

@lmoresi

What

_peel_except substitutes each named expression by its definition so the chain rule has
something to hold on to, and leaves a named expression alone when its value is a bare
number (the JIT passes those as run-time constants, so substituting them would recompile
the kernel on every sensitivity evaluation — four compiles per evaluation on the fault
example).

The leaf test was is_Number. A dimensional expression's .sym is a Pint quantity, which
is not a sympy Number, so it fell through to subs and raised

SympifyError: 2700 [kilogram / meter ** 3]

for uw.expression(r"\rho", uw.quantity(2700, "kg/m**3")).

Why it is worse than it looks

The failure is in the shared residual peel, not in the parameter being differentiated
against. So a single dimensional expression anywhere in the residual — the density — took
out every row of a parameter table, including rows for parameters that are themselves
perfectly ordinary numbers.

Fix

Treat a dimensional value as a leaf for exactly the same reason a bare number is one: the
JIT passes it as a run-time constant. wrt itself is still excluded from the peel, which
is what the chain rule needs.

def _is_leaf(e):
    v = getattr(e, "sym", None)
    if getattr(v, "is_Number", False):
        return True
    return not isinstance(v, sympy.Basic)

Consequence, and the separate bug it exposes

Keeping dimensional values as leaves means a dimensional factor can now survive into
dR/dm
when the parameter differentiated against is dimensionless. That is correct
symbolically, and it is what exposed #763 (the sensitivity was then read out of a
re-dimensionalised integral with float()).

Activity

  1. lmoresi commented on Sep 25, 2026

    @lmoresi
    MemberAuthor

    A second route into the same failure, without any user-supplied dimensional value: ViscoPlasticFlowModel's default yield_stress_min is 0 with units="Pa", so stokes.gradient(J, parameters=[model.Parameters.shear_viscosity_min]) on a stock viscoplastic model dies in _peel_except with SympifyError: 0 [pascal] — reproduced on scratch/adjoint-merge (82e697b) and feature/discrete-adjoint (e6e4e61). Any model that leaves the tension cutoff at its default is affected, whatever parameter is asked for.

    Workaround used to check the adjoint in the meantime: model.Parameters.yield_stress_min = 0.0 (a bare number is left as a constant by the peel). With that, the sensitivity to the viscosity floor matches central differences to 4e-8 and the dual on a P0 viscosity field to 1e-7 per cell (tolerance 1e-11, softmin yield).

  2. added a commit that references this issue on Oct 28, 2026
  3. lmoresi commented on Nov 10, 2026

    @lmoresi
    MemberAuthor

    Still live on development 8f0ed0a (H3 units re-verification), on both routes:

    • a dimensional parameter (eta = uw.expression(..., uw.quantity(1e22, "Pa*s"))): SympifyError: 1e+22 [pascal * second];
    • a stock ViscoPlasticFlowModel in a model with no reference quantities. Its default yield_stress_min is 0 Pa, so stokes.gradient(J, parameters=[alpha]) raises SympifyError: 0 [pascal].

    There is a third site that the fix has to cover. The first failure comes from adjoint._peel, through gradient -> field_duals -> _reads_of, before any sensitivity is computed. The is_Number leaf test is in three places: adjoint._peel, adjoint._peel_except, and the solver's own _peel_except in petsc_generic_snes_solvers.pyx, which is a copy of the second. With the leaf rule from the issue in all three, the no-units sensitivity matches central differences to 1.5e-4, and the units case then shows #763.

    Script: runs/2026-11-10-h3/probes/h3_adjoint.py 765 on hyperion.

    Underworld development team with AI support from Claude Code

  4. added
    fixed-in-PR
    on Nov 10, 2026
  5. removed
    fixed-in-PR
    on Nov 11, 2026
  6. added a commit that references this issue on Nov 11, 2026
  7. added
    fixed-in-development
    on Nov 11, 2026
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

    fixed-in-development

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions