Skip to content

Tensor_Projection's work variable takes the tensor's degree; Stokes.tau decodes a log-conformation history - #1210

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/tensor-projection-takes-the-tensor-degree
Nov 16, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/tensor-projection-takes-the-tensor-degree

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

A log-conformation run could not read its own stress. No issue was filed: this PR is the report. Found while writing a test for #1209.

The defect

On development 1b601be, serial, macOS (12-core Apple M4):

  • Mesh: a StructuredQuadBox 4 x 2.
  • Constitutive model: ViscoElasticPlasticFlowModel(order=1, integrator="etd", objective_rate="upper_convected", stress_history="log_conformation").
  • Boundaries: shear walls. One solve(timestep=0.1).
  • Then stokes.tau raises: ValueError: could not broadcast input array from shape (15,) into shape (45,).

The probe is issue-sweep/sweep-2026-11-15/p_tau_decode.py in the campaigns repository.

Cause

Stokes.tau decodes the history by projecting the carried stress G (exp(ψ*) − I) onto a tensor variable of the history's degree, through Tensor_Projection. That class projects one component at a time through a scalar work variable, copying each component in and out node for node. It built the work variable at degree 2, whatever the tensor's degree. A degree-1 tensor has 15 nodes on this box; a degree-2 scalar has 45.

Fix

  • The work variable takes the tensor field's degree unless the caller gives a degree. Callers that pass their own work variable or a degree are unchanged: the two shipped examples do, and so do test_0505 and test_multicomponent_projection.
  • The components are copied node for node, so a work variable whose degree or continuity differs from the tensor's is refused when the projection is built, with a message naming both. Before, it failed with a broadcast error inside solve(). Every caller in src, tests and the examples passes a matching pair.
  • Stokes.tau projects the decoded stress at a solver tolerance of 1e-10. The projection's defaults (snes_rtol 1e-4) made the accuracy of a public return value incidental.

Tolerances and coordinate matches

In the library: the decode's solver tolerance, 1e-10. Test bounds:

  • the decoded stress against the one-step closed form, at rel 1e-6 (abs 1e-9);
  • a linear tensor projected onto degree 1, at 1e-10, with a projection solver tolerance of 1e-12. At the default tolerance the solve stops about 1e-6 away.

Tests

Three new tests in tests/test_1065_log_conformation.py:

  • test_the_decoded_stress_is_the_one_step_closed_form: one ETD-1 deformation step from rest in simple shear, against G(c − I) from the file's _deformation_step, for xx, xy and yy. It fails on development.
  • test_tensor_projection_works_on_a_degree_one_tensor: a linear tensor, all three entries. It fails on development.
  • test_a_work_variable_that_does_not_match_the_tensor_is_refused: new behaviour, not run against development. From reading, development builds that projection and fails later, in solve().

Related tests pass, serially: 69, in test_1065, test_0505, test_multicomponent_projection, test_1059 and test_0070.

The two-solver decoded-stress test for #1209's tau_decoded_<n> naming will be added once both PRs are merged.

Review

One round of adversarial review; its record is posted below. Its findings are fixed in 3d6eca7.

Left for a follow-up

  • A refused construction leaves its scalar work variable on the mesh, because the work variable is made before the check.
  • A discontinuous tensor with the default work variable is refused rather than given a discontinuous work variable.

Underworld development team with AI support from Claude Code

…au decodes a log-conformation history

Stokes.tau decodes a log-conformation stress history by projecting the
carried stress G (exp(psi*) - I) onto a tensor variable of the history's
degree, through Tensor_Projection. Tensor_Projection projects component by
component through a scalar work variable, copying each component in and out
node for node, and built that variable at degree 2 whatever the tensor's
degree. On a degree-1 history the copy raised: "could not broadcast input
array from shape (15,) into shape (45,)" on a 4 x 2 quad box. So a
log-conformation run could not read its own stress.

The work variable now takes the tensor field's degree unless a degree is
given (2 when there is no tensor field). Callers that pass their own work
variable or a degree are unchanged.

Tests in tests/test_1065_log_conformation.py, both failing on development
1b601be: the decoded stress equals the carried stress on a uniform Maxwell
shear (rel 1e-8), and a linear tensor projects onto degree 1 exactly
(1e-10, at a solver tolerance of 1e-12).

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 16, 2026 03:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

… form; a mismatched work variable is refused (review of #1210)

From the adversarial review:
- Stokes.tau's decode kept the projection's default tolerances (snes_rtol
  1e-4), so the accuracy of a public return value was incidental. It now
  projects at 1e-10.
- The test compared the decoded stress with the function it projects. It now
  checks one ETD-1 deformation step from rest in simple shear against the
  closed form G (c - I) of _deformation_step (rel 1e-6), which a wrong
  modulus or decode would fail. The degree-1 projection test checks the yy
  entry too.
- The components are copied node for node, so a work variable whose degree or
  continuity differs from the tensor's (a discontinuous tensor with the default
  work variable, or a scalar_Field of another degree) now raises when the
  projection is built, naming both, where it raised a broadcast error in
  solve(). Every caller in src, tests and the examples passes a matching pair.
- The docstring says degree 0 has no smoothing effect.

69 related tests pass, serially.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Adversarial review record

One round, by a fresh reviewer that read the code through git without running it. The measurements below were made by the author, serially, on macOS (12-core Apple M4).

Findings, all fixed in 3d6eca7

  • The decoded stress was only as accurate as the projection's default tolerances (snes_rtol 1e-4). It passed at rel 1e-8 only because the box is small. Stokes.tau now projects at 1e-10.
  • The first test compared the decoded stress with the function it projects, so a wrong modulus or a wrong decode would have passed. It now checks one ETD-1 deformation step from rest in simple shear against the closed form G(c − I) from _deformation_step, at rel 1e-6.
  • A work variable with other nodes than the tensor's still failed with a broadcast error in solve(). That is a discontinuous tensor with the default work variable, or a scalar_Field of another degree. It is now refused when the projection is built, with a message naming both variables. Every caller in src, tests and the examples passes a matching pair; checked one by one.
  • The degree-1 projection test skipped the yy entry. It is added.
  • The docstring did not say that smoothing has no effect at degree 0. It now does.

Attacks that found nothing

  • No caller relied on the old default of 2: only Stokes.tau used the default work variable.
  • Nothing rank-dependent is added.

Tests

69 related tests pass. The two decode tests failed on development 1b601be. The refusal test is new behaviour, and it was not run against development. From reading, development builds that projection without complaint and fails later, in solve().

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Verdict on head 3d6eca7: ready to merge on green from this review. No correctness findings. Three low items are below; none blocks the merge.

Findings

Low 1: a refused construction leaves its work variable on the mesh

The check runs after super().__init__, and by then SNES_Scalar has already made the default work variable Us<n>. So every refusal leaves an unused field on the mesh's DM.

  • Case: on one mesh, build Tensor_Projection three times: a discontinuous tensor with defaults (refused), a degree-2 tensor with degree=3 (refused), and a degree-2 tensor with defaults (built).
  • Evidence: mesh.vars ends up as ['Us10', 'Us15', 'Us20', 't2', 'td', 'w1', 'w1d']. Two of the three Us fields belong to no solver. This is the same on every rank at np 1, 2 and 3, and the mesh still works afterwards: a later discontinuous projection matches to 8e-16.
  • Suggested fix: decide the work variable's degree and continuity first (from scalar_Field, or from degree with continuous=True), and refuse before calling super().__init__.

Low 2: a discontinuous tensor with default arguments is refused

Asked for nothing else, the default work variable could take the tensor's continuity as it already takes its degree. Today a discontinuous tensor with defaults is refused, and the caller has to build a matching scalar. The message says exactly what to pass, so this is a usability point only.

Low 3: the PR body no longer matches the branch

  • The body names test_the_decoded_stress_is_the_carried_stress with bound rel 1e-8 (abs 1e-12). The test on the branch is test_the_decoded_stress_is_the_one_step_closed_form, with rel 1e-6 and abs 1e-9.
  • The body says "two new tests". There are three: the refusal test is the third.
  • The body says "Callers that pass their own work variable or a degree are unchanged". Those callers are now refused when the work variable does not have the tensor's nodes. Every shipped caller does match (listed below), so nothing changes for them, but the refusal belongs in the body as a behaviour change.

Build note

Development's merge base 1b601be differs in compiled source from the build used here, in petsc_generic_snes_solvers.pyx. The change comes from #1200 and #1202:

  • set_active_region now raises;
  • the index sets in Stokes_Constrained take every unknown per point.

Neither is on the Tensor_Projection or log-conformation path. So the runs below lay the PR's Python on that build without a rebuild.

Confirmed correct

  • The closed form. L = [[0, 1], [0, 0]] is nilpotent, so F = I + dt L is exactly exp(dt L). The ETD-1 step in _deformation_step is then the scheme's own one-step value:

    • c_xy - 0 = 0.0951626, which is also the exact upper-convected Maxwell value 1 - e^-0.1;
    • c_xx - 1 = 0.0092784, the scheme's discrete value (the continuum value is 0.0093581);
    • c_yy - 1 = 0.

    The test pins the scheme, not the continuum, as its docstring says. The same recurrence is checked independently by the existing test_log_conformation_history_follows_the_recurrence.

  • The new tests fail on development. All three, at the merge base:

    • could not broadcast input array from shape (15,) into shape (45,);
    • (25,) into (81,);
    • DID NOT RAISE.

    All three pass at the head.

  • Degrees 1, 2 and 3 project correctly. A polynomial tensor of the tensor's own degree, projected onto a symmetric tensor of that degree, comes back with the largest nodal error:

    run largest nodal error
    serial 7e-14 to 1.3e-13
    np 2 1.2e-13
    np 3, empty-rank triangle fixture (cells [8, 8, 0]) 1.3e-13
    np 3, gmsh box with the last rank emptied by a shell partition (cells [35, 31, 0]) 1.2e-13

    At the merge base, degree 1 and degree 3 raise the broadcast error.

  • The refusal is collective. Three mismatches are refused, with an identical outcome on every rank at np 1, 2 and 3, including the rank with no cells:

    • a discontinuous tensor with defaults;
    • a scalar work variable of another degree;
    • a degree argument that differs from the tensor's.

    A matching discontinuous pair, and a default continuous tensor, both build. Everything in the check is variable metadata that every rank holds alike, so no rank can decide otherwise.

  • Stokes.tau decodes a log-conformation history. On the 4 x 2 shear box, evaluated at three points against G(c - I):

    run error at three points largest nodal error per rank
    np 2 3.5e-13 5.5e-13
    np 3 3.5e-13
    np 3 with an empty rank (cells [4, 4, 0]) 3.5e-13 5.5e-13 on both rank-holding ranks, nothing on the empty one
  • Other callers. In-library, only Stokes.tau's decode uses Tensor_Projection. The base tau uses MultiComponent_Projection and is not affected. Every other caller passes a work variable that matches the tensor, so none is refused and none changes its answer:

    • test_0505, degree 2 with degree 2;
    • test_multicomponent_projection, three sites, degree 2 with degree 2 and degree=2;
    • Ex_Sheared_Layer_Elastic, degree 2 with degree 2;
    • Ex_Sheared_Layer_Test, degree 1 discontinuous with degree 1 discontinuous;
    • scripts/sessions/run_ve_*, degree 2 with degree 2.
  • With A re-declared mesh variable refuses to change; helper variables named after their owner; uw.unwrap expands completely (#1118, #824) #1209. The two branches merge cleanly. On the merged code, two log-conformation solvers on one mesh decode into tau_decoded_7 and tau_decoded_42. Each matches its own closed form to 3.5e-13 at np 1, 2 and 3 (the np 3 run with an empty rank), and each equals the same solver on its own mesh exactly. This is the two-solver test the PR body defers.

What was run

  • New tests: test_1065 new tests (3) serially, at the head (3 passed) and at the merge base (3 failed, as above).
  • Probes, serially and at np 2: degrees 1 to 3, the refusal cases, and the Stokes.tau decode.
  • Probes at np 3 with an empty rank, by two routes: the fixture in tests/parallel/empty_rank_fixtures.py, and a shell partition hook on gmsh meshes. The hook was given one guard, so that it does not run inside the JIT's build subprocess.
  • Neighbours at np 2: test_1065 (the new tests, the closed-form extension cases and the linear step): 12 passed. test_0505_projection_smoothing_length, test_multicomponent_projection, test_0504_projections, test_0070_ve_stress_history_on_particles and test_1064_stress_history_units: 34 passed.
  • Neighbours serially: test_1065 (all 20), test_0505_projection_smoothing_length, test_multicomponent_projection, test_0504_projections, test_0005_project_discontinuous_to_continuous, test_1059_stress_transport, test_0070, test_1064, test_0020_restore_continuation, test_1051_VE_shear_box, test_1063_stress_history_restart and test_1018_rotated_freeslip_every_rheology: 128 passed.

Every parallel run went under the MPI supervisor. Logs are kept by the reviewer.

Not run:

  • the full suite;
  • 3-D tensors;
  • the two shipped examples themselves (their constructor arguments were checked by reading);
  • the neighbour test files at np 3.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit eccae6d into development Nov 16, 2026
10 checks passed
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.

2 participants