Skip to content

SwarmVariable reads a non-symmetric matrix back in the order it was written; global_evaluate stops transposing tensors - #988

Merged
lmoresi merged 5 commits into
developmentfrom
bugfix/remap-sym-tensor
Nov 12, 2026
Merged

lmoresi merged 5 commits into
developmentfrom
bugfix/remap-sym-tensor

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

SwarmVariable now reads back a non-symmetric MATRIX or TENSOR in the order it was written. That was why global_evaluate returned such tensors transposed. Part of #977. Stacked on #982, so merge #959 and #982 first.

Scope after the development merge. When this PR was opened it also wrote remapped values in each variable's own layout and reordered the limiter's bounds. Development now has both, through #1002's _evaluated_in_storage_layout. The merge keeps development's helper and drops this PR's _values_in_data_layout, since keeping both would permute twice. The source change is now swarm.py alone (+2 −1).

What was wrong

global_evaluate carries its answer on a MATRIX SwarmVariable, which packed by rows and unpacked by columns, so array did not read back what was written. Write [[1,2],[3,4]] and you read [[1,3],[2,4]], in serial too. On a MATRIX mesh variable the transposition cancelled against the C-order reshape, which is why it went unnoticed.

The change

SwarmVariable._pack_array_to_data_format follows _data_layout. A single row or column keeps the plain reshape.

Tests

Who could be affected

Any code that called global_evaluate on a non-symmetric TENSOR expression got a transposed answer. I found no caller that compensates for it. Non-square MATRIX shapes other than a single row or column were not run.

Not in this PR

Mesh.remesh, the main subject of #977, and pack_raw_data_to_petsc are untouched, so #977 stays open.

Checklist (UW3 Style Charter)

  • Read the Style Charter and followed it over the surrounding code
  • No drive-by refactors, renames or cleanups
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers
  • No hedging names and no commented-out code
  • No new exception swallows
  • New data access uses .array / mesh.X.coords
  • Parallel safety checked at np 2 and 4
  • No dependency changes
  • AI-assisted work carries the attribution line

Underworld development team with AI support from Claude Code

…d-trips

global_evaluate returns the full array per node. A symmetric tensor stores only its
independent components, so the remap's reshape raised and left the move part done.
A MATRIX SwarmVariable packed by rows and unpacked by columns, so it did not read back
what was written, and global_evaluate, which carries its answer on one, returned every
non-symmetric tensor transposed. Part of #977.

Underworld development team with AI support from Claude Code
… follow the layout

The two errors that cancelled for a square matrix also cancelled in _refine_prolongate,
_refine_restrict and the monotone limit, so those read a matrix transposed once
global_evaluate was right. A single row or column still takes the plain reshape, which
keeps assigning (N, dim, 1) to a vector.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Adversarial review

Two independent reviews, by reading code; neither ran anything. The first read the original commit; the second read it stacked with the #979 work. The follow-up commit was not re-reviewed.

Fixed

  • _refine_prolongate and _refine_restrict read a square MATRIX transposed once global_evaluate was right. They now use the layout helper.
  • _apply_monotone_limit bounded matrix components against the wrong columns for the same reason. Fixed, with a test shown to fail without it.
  • Assigning (N, dim, 1) to a vector would have raised. The plain reshape is kept for a single row or column.
  • The test docstring wrongly said a MATRIX was silently transposed. It says now that those two errors cancelled.

Known and left

Attacks that found nothing

  • Callers compensating for the transposition: none near any global_evaluate call, and _write_evaluated follows layout.
  • Shapes (1,n), (n,1), (1,1): layout equals C order.
  • read_timestep and existing files: the saved variable is (1,n), and on-disk canonical data is unchanged.
  • The empty-partition guard; var.shape is always a 2-tuple, including enhanced and integration-point variables; the off-diagonal convention has no Mandel or factor of two across array, data and evaluate.

Issue verdicts: #977 still live, keep it open.

Underworld development team with AI support from Claude Code

Development's _evaluated_in_storage_layout (7bfdfaa) does what this
branch's _values_in_data_layout did, with units, so the conflicts take
development's helper, and the limiter takes development's per-entry bounds
(this branch's reorder would now permute twice). What remains here is the
swarm view of a full matrix and the symmetric-tensor tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lmoresi

lmoresi commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Rebased on #982 and development 526edb9 (c4d31f9). Most of this PR is now on development. 7bfdfaa (2026-11-08, "global_evaluate returns a MATRIX in its components, and deform and refinement store every shape in its slot") added _evaluated_in_storage_layout. It does what this branch's _values_in_data_layout did, and handles units as well. The merge therefore takes development's helper in remesh.py and discretisation_mesh.py, and development's per-entry limiter bounds. This branch's reorder in _apply_monotone_limit would have permuted a MATRIX twice, so it's gone.

What remains:

  • SwarmVariable views a full matrix by its layout, not by a flat row-major reshape (swarm.py).
  • test_0769, the symmetric-tensor and matrix remap test, serial and np 2.

Locally: test_0769 and test_0768 pass, 9 tests; test_0769 at np 2 passes. The title overstates the PR now, so I'll rename it when it's reviewed again.

Underworld development team with AI support from Claude Code

lmoresi pushed a commit that referenced this pull request Nov 10, 2026
The two conflicts combine both sides: the evaluation stays at the
non-dimensional coordinates the DM holds (this branch), and the result is
laid out by development's _evaluated_in_storage_layout, which also reduces a
dimensional evaluation to the non-dimensional values var.data holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lmoresi lmoresi changed the title A remapped variable is written in its own layout; global_evaluate stops transposing tensors SwarmVariable reads a non-symmetric matrix back in the order it was written; global_evaluate stops transposing tensors Nov 11, 2026
@lmoresi
lmoresi changed the base branch from bugfix/remap-no-clamp to development November 12, 2026 00:22
@lmoresi lmoresi closed this Nov 12, 2026
@lmoresi lmoresi reopened this Nov 12, 2026
@lmoresi
lmoresi merged commit c7fc6d7 into development Nov 12, 2026
17 of 19 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