Skip to content

Collapse duplicated loss APIs - #206

Merged
eduardolneto merged 3 commits into
mainfrom
refactor/single-loss-api
Nov 13, 2026
Merged

eduardolneto merged 3 commits into
mainfrom
refactor/single-loss-api

Conversation

@rogeriojorge

@rogeriojorge rogeriojorge commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Replaces the four Tracing loss methods with one and the seven loss_particle_* functions with a shared trace helper.

dynamics.py: loss_fraction_BioSavart, loss_fraction_BioSavart_collisions, loss_fraction and loss_fraction_collisions become loss_fraction(boundary=None, r_max=1.0). With boundary=None a particle is lost at s >= r_max; with a SurfaceClassifier it is lost when the signed distance goes negative. Collision models return the two extra arrays (energy and position at loss), decided from self.model.

objective_functions.py: One private _orbit does the trace and the axis offsets; each loss_particle_* keeps its name and is a few lines. Trace options (timestep, maxtime, num_steps, trace_tolerance, model, boundary) are now keyword-only through **trace, and target_iota in loss_particle_iota moved to the second positional slot. No example imports these functions (the two optimization examples define their own), so nothing else changed except trace_particles_interpolated_dipoles.py, which now calls loss_fraction(classifier).

API changes: The three removed Tracing methods are gone; loss_fraction(r_max) positional use must become loss_fraction(r_max=...).

Equivalence: Before/after run on main vs this branch (QA VMEC and QH coils + classifier, plain and collision models, several r_max, all seven objectives plus a non-default target_iota): 51 output arrays, all bitwise identical. Tests pass with JAX_ENABLE_X64=1 pytest -n 2 tests.

External callers: gh search code --owner uwplasma for loss_fraction_BioSavart, loss_fraction_collisions and loss_fraction( finds only essos/dynamics.py and trace_particles_interpolated_dipoles.py (both updated here) in the whole organisation; no other uwplasma repository uses them. No other searched source calls them.

Analytic test: test_loss_fraction_of_analytic_gyro_orbits builds gyro-orbits of radius rho about guiding centres at distance d from the origin and checks loss_fraction against a sphere of radius R: a particle is lost iff d + rho > R, one starting outside is lost at t = 0 (first time, not -1), and a particle with d + rho < R stays confined; the r_max branch is checked on the same orbits.

Overlap: #193 also edits dynamics.py and objective_functions.py, but I found no changes to the loss methods or loss_particle_* in its diff, so I expect at most a trivial textual conflict.

Diff: 82 additions, 252 deletions.

Reviewer commands: JAX_ENABLE_X64=1 python -m pytest -q -n 2 tests (391 passed, 1 xfailed on Python 3.13; 386 passed, 5 skipped, 1 xfailed on Python 3.10 with JAX 0.6.2) and pytest tests/test_dynamics.py -k analytic_gyro. Limit: the equivalence script compares against main, so it only covers the configurations listed above.

@rogeriojorge
rogeriojorge marked this pull request as ready for review November 11, 2026 02:40
@rogeriojorge
rogeriojorge marked this pull request as draft November 11, 2026 02:40
@rogeriojorge
rogeriojorge marked this pull request as ready for review November 11, 2026 03:01

@eduardolneto eduardolneto 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.

This seems correct and reducing number of lines.

@eduardolneto
eduardolneto merged commit 7b4422c into main Nov 13, 2026
5 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