Repository navigation
Scalars fetch allocated objects again after each allocation: no stale scalars in a second run (fixes #852) - #876
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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()callsallocate()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 (hasattrchecks in_local_update).Fix
Scalar.allocate(): new hook that fetches the objects created by the allocation;update()calls it once after each allocation.BilinearEnergyFEEC,VolumeFormEnergyFEECandPICScalar(clone config) fetch their objects there instead of caching them in_local_update. TheScalardocstring states the rule.Scalars.reset_allocation(): marks every scalar, including the summands of sums such asen_tot, for a new fetch; called at the end ofSimulation.allocate().post_allocate:_divUvofVariationalMHDandVariationalCompressibleFluid(only fordivdiv_alpha > 0),_f0/_f0_valuesofLinearVlasovAmpereOneSpecies.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 energyen_E:[0, 2.159e-5, 8.467e-5, 1.8439e-4][1.8439e-4, 1.8439e-4, 1.8439e-4, 1.8439e-4][0, 2.159e-5, 8.467e-5, 1.8439e-4]3.131146e-43.131146e-4Tests run locally:
src/struphy/models/tests/test_scalars.py(6 passed),test_variational_divdiv.py(5 passed)🤖 Generated with Claude Code