Skip to content

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

Open
spossann wants to merge 2 commits into
develfrom
fix-stale-scalars-852
Open

spossann wants to merge 2 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>

This branch has not been deployed

No deployments
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

1 participant