Skip to content

GuidingCenter en_fB sums over holes and ghosts; test_scalars en_fB tests fail on devel #727

Open

Description

@max-models

Two tests in src/struphy/models/tests/test_scalars.py fail on devel (25688c5):

FAILED src/struphy/models/tests/test_scalars.py::test_en_fB_ignores_ghost_markers
  AssertionError: GuidingCenter: en_fB = 8.75 with ghosts, 2.0 without
FAILED src/struphy/models/tests/test_scalars.py::test_guiding_center_en_fB_does_not_divide_by_Np
  AttributeError: '_GuidingCenterMarkers' object has no attribute 'save_magnetic_background_energy'

The failures were first seen while testing #724, but they also happen on unchanged devel (checked locally in a clean worktree of origin/devel, macOS, python -m pytest src/struphy/models/tests/test_scalars.py: 2 failed, 4 passed). CI does not flag them.

1. GuidingCenter._compute_en_fB uses an inverted mask (a real bug). src/struphy/models/guiding_center.py:120-121:

energy = particles.markers[~particles.valid_mks, 5].dot(
    particles.markers[~particles.valid_mks, 8],
)

~valid_mks selects the holes and ghost markers instead of the valid markers, so en_fB of GuidingCenter sums over the wrong rows. #631 (fix for #563) meant to change ~particles.holes to particles.valid_mks. It did so in LinearMHDDriftkineticCC._compute_en_fB (linear_mhd_driftkinetic_cc.py:211-212), but in guiding_center.py it wrote ~particles.valid_mks. test_en_fB_ignores_ghost_markers catches this.

2. The mock in test_guiding_center_en_fB_does_not_divide_by_Np is outdated. _GuidingCenterMarkers (test_scalars.py:169, added in #477) has no save_magnetic_background_energy() and no valid_mks, which GuidingCenter._compute_en_fB uses.

Suggested fix:

  • guiding_center.py: index with particles.valid_mks (without ~).
  • test_scalars.py: give _GuidingCenterMarkers a no-op save_magnetic_background_energy() and a valid_mks attribute (~self.holes).
  • Check why CI did not run these tests. The unit-test job uses --testmon, so it may have skipped them.

Found while working on #724 (part of #697, #650).

🤖 Generated with Claude Code

Activity

  1. added
    bugSomething isn't working
    mediumMedium-severity bug from the #416 audit
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AIbugSomething isn't workingmediumMedium-severity bug from the #416 audit

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions