Skip to content

Scalars fetch allocated objects again after each allocation: no stale scalars in a second run (fixes #852) - #876

Merged
spossann merged 3 commits into
develfrom
fix-stale-scalars-852
Nov 3, 2026
Merged

spossann merged 3 commits into
develfrom
fix-stale-scalars-852

Conversation

@spossann

@spossann spossann commented Nov 2, 2026

Copy link
Copy Markdown
Member

Fixes #852.

Problem

A second Simulation.run() on the same object (the restart in the model tests, utils_testing.call_test, or a notebook that runs twice) saved stale scalar quantities: every value equal to the last value of the first run. run() calls allocate() again, which creates new spline vectors, mass operators and particles, but the scalars had cached the objects of the first run on their first update (hasattr checks in _local_update).

Fix

  • Scalar.allocate(): new hook that fetches the objects created by the allocation; update() calls it once after each allocation. BilinearEnergyFEEC, VolumeFormEnergyFEEC and PICScalar (clone config) fetch their objects there instead of caching them in _local_update. The Scalar docstring states the rule.
  • Scalars.reset_allocation(): marks every scalar, including the summands of sums such as en_tot, for a new fetch; called at the end of Simulation.allocate().
  • Model-side caches with the same pattern move to post_allocate: _divUv of VariationalMHD and VariationalCompressibleFluid (only for divdiv_alpha > 0), _f0/_f0_values of LinearVlasovAmpereOneSpecies.
  • New unit test test_scalars_follow_a_new_allocation.

No baseline changes: the regression data is taken before the restart, and the first run is unchanged.

Verification

Maxwell, default parameters (Tend = 0.03), electric energy en_E:

values
first run [0, 2.159e-5, 8.467e-5, 1.8439e-4]
second run, same object (before) [1.8439e-4, 1.8439e-4, 1.8439e-4, 1.8439e-4]
second run, same object (after) [0, 2.159e-5, 8.467e-5, 1.8439e-4]
restart to t = 0.04, last value (after) 3.131146e-4
straight run to t = 0.04, last value 3.131146e-4

Tests run locally:

  • src/struphy/models/tests/test_scalars.py (6 passed), test_variational_divdiv.py (5 passed)
  • regression tests, serial: Maxwell, VariationalMHD, VariationalCompressibleFluid, LinearVlasovAmpereOneSpecies, LinearVlasovMaxwellOneSpecies, VlasovAmpereOneSpecies, GuidingCenter
  • regression tests, 2 ranks: Maxwell, VlasovAmpereOneSpecies, LinearVlasovAmpereOneSpecies

🤖 Generated with Claude Code

spossann and others added 2 commits November 2, 2026 16:31
A second Simulation.run() on the same object (e.g. the restart in the model
tests) re-allocates spline vectors, mass operators and particles, but the
scalars had cached the objects of the first run on their first update and
saved stale values.

- Scalar.allocate() hook, called by update() once after each allocation;
  BilinearEnergyFEEC, VolumeFormEnergyFEEC and PICScalar fetch their objects
  there instead of caching them in _local_update.
- Scalars.reset_allocation(), called at the end of Simulation.allocate().
- Model-side caches (_divUv of the variational models, _f0/_f0_values of
  LinearVlasovAmpereOneSpecies) move to post_allocate.
- Unit test for scalars following a new allocation.

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

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.

🟡 Changes recommended

The test does not protect recursive allocation resets because its only allocation-dependent scalar is registered directly rather than reachable solely through a nested sum.

1 open issue
What changed in this PR

Fixes stale scalar diagnostics when running or restarting the same Simulation object more than once, addressing #852 by refreshing allocation-dependent caches.

Changes:

  • Adds scalar allocation hooks and recursive resets for summed diagnostics.
  • Refreshes model-specific caches in post_allocate.
  • Adds a unit test for scalar updates after reallocation.
File Description
src/​struphy/​simulation/​sim.py Resets scalar allocation state after simulation allocation.
src/​struphy/​models/​variational_mhd.py Recreates div-div energy caches after allocation.
src/​struphy/​models/​variational_compressible_fluid.py Refreshes div-div operators and work buffers after allocation.
src/​struphy/​models/​tests/​test_scalars.py Tests scalar values after replacing allocated objects.
src/​struphy/​models/​scalars.py Adds allocation hooks, recursive resets, and lifecycle documentation.
src/​struphy/​models/​linear_vlasov_ampere_one_species.py Refreshes background references and marker buffers after allocation.

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@spossann
spossann merged commit 5dd317c into devel Nov 3, 2026
30 checks passed
@spossann
spossann deleted the fix-stale-scalars-852 branch November 3, 2026 07:45
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.

Second Simulation.run() (restart in model tests) saves stale scalar quantities

2 participants