Skip to content

TrappedResonance#2257

Draft
ejpaul wants to merge 219 commits into
PlasmaControl:masterfrom
jlabbate15:ejp-merge-master
Draft

TrappedResonance#2257
ejpaul wants to merge 219 commits into
PlasmaControl:masterfrom
jlabbate15:ejp-merge-master

Conversation

@ejpaul

@ejpaul ejpaul commented Jul 6, 2026

Copy link
Copy Markdown

Development of TrappedResonance objective function for EP optimization.

ejpaul and others added 29 commits March 9, 2026 09:37
… fixes

The earlier merge-conflict resolution accidentally dropped
QuasisymmetryTwoTermNormalized from _omnigenity.py/__init__.py; restore it.
Also drop the local desc/particles.py draft since an equivalent version
landed on upstream DESC master and will be picked up by the upcoming merge.
# Conflicts:
#	desc/batching.py
#	desc/compute/_fast_ion.py
#	desc/compute/_metric.py
#	desc/compute/_neoclassical.py
#	desc/compute/_old.py
#	desc/geometry/surface.py
#	desc/integrals/__init__.py
#	desc/integrals/_bounce_utils.py
#	desc/integrals/_interp_utils.py
#	desc/integrals/basis.py
#	desc/integrals/bounce_integral.py
#	desc/objectives/_fast_ion.py
#	desc/objectives/_neoclassical.py
#	desc/plotting.py
#	desc/utils.py
#	docs/installation.rst
#	docs/notebooks/tutorials/EffectiveRipple.ipynb
#	requirements.txt
#	tests/baseline/test_Gamma_c_Nemov_1D.png
#	tests/baseline/test_Gamma_c_Velasco_1D.png
#	tests/baseline/test_binormal_drift_bounce1d.png
#	tests/baseline/test_binormal_drift_bounce2d_0-False-16.png
#	tests/baseline/test_binormal_drift_bounce2d_0-True-64.png
#	tests/baseline/test_binormal_drift_bounce2d_1e-08-True-64.png
#	tests/baseline/test_bounce1d_checks.png
#	tests/baseline/test_bounce2d_checks.png
#	tests/baseline/test_effective_ripple_1D.png
#	tests/baseline/test_plot_gammac.png
#	tests/benchmarks/benchmark_cpu_small.py
#	tests/benchmarks/benchmark_gpu_small.py
#	tests/benchmarks/memory_funcs.py
#	tests/test_fast_ion.py
#	tests/test_integrals.py
#	tests/test_interp_utils.py
#	tests/test_neoclassical.py
#	tests/test_objective_funs.py
- Format _trapped_resonance.py (compute + objectives) and the
  QuasisymmetryTwoTermNormalized addition with black 22.10.0.
- Fix isort import ordering, docstring formatting (D400/D403), and
  line-length (E501) violations flagged by flake8.
- Drop unused local `pitch_batch_size` and the `imap = jax.lax.map`
  addition in backend.py, both dead code only referenced by a stray
  backup file.
- Give "trapped EP resonance" a truthful grid_requirement so
  test_compute_everything's generic sweep correctly excludes it
  instead of crashing on missing objective-only kwargs.
Drop a macOS .DS_Store, a stray auto-generated VMEC input dump, a
367KB browser page-save HTML export, various one-off .npy debug
dumps, and four notebooks that predate the current TrappedResonance
API entirely (results_01282026.ipynb, test.ipynb,
TrappedResonance_Plotting.ipynb, prelim_optimization.ipynb) and were
never updated to match it. Also drop desc/compute/_neoclassical
old.py, a backup file with a space in its name that isn't part of
the package and is only referenced by itself.

None of this belongs in a PR; keeps trappedres_objopt_example.ipynb,
freq_comparison.ipynb, and TRObj_func.py, which are current and working.
Mirrors the existing EffectiveRipple/GammaC pattern:
- Exclude TrappedResonance from the generic no-nangrad sweep
  (TestObjectiveNaNGrad) and give it a dedicated unit test with
  reduced settings, checking both weight_method="linear" and "bump"
  produce finite gradients.
- Extend _reduced_resolution_objective with small TrappedResonance
  settings for use by that test.
- Exclude TrappedResonance from the resolution-independence sweep
  (TestComputeScalarResolution): unlike Ripple/GammaC's smooth
  surface-averaged integrals, resonance detection converges slower
  with equilibrium grid resolution than the sweep's shared tolerance
  allows within its reduced resolution range.
Mirrors test_objective_against_compute_bounce (EffectiveRipple/GammaC):
verifies TrappedResonance.compute() matches directly computing
"trapped EP resonance" via the same low-level compute_fun/get_transforms/
get_profiles calls the objective itself uses, with an independently
constructed grid/quad/zeta and a single trivial p/q=0/1 resonance to
keep res_arr/q_arr/p_arr reproducible by hand.

Also bump the reduced-resolution settings used by this and the
no-nangrad unit test: num_eta=4/num_transit=2 (the previous settings)
detect zero resonance crossings, making both tests exercise an
identically-zero function. num_eta=8/num_transit=4/knots_per_transit=60
is the smallest configuration found to produce genuine signal.
… test

_resonance_physics's "linear" weight_method previously used a 2-point
secant to find where Omega(rho) crosses a resonance. Add a 4-point
Catmull-Rom cubic Hermite refinement (Newton-polished from the linear
estimate) of the crossing location, falling back to the existing
2-point scheme at domain-boundary intervals or wherever num_rho < 4.
Verified byte-identical output to the prior implementation whenever no
4-point stencil is available (e.g. the num_rho=3 reduced-test setting),
and deterministic (no run-to-run flakiness) where it is available.

This does not fix TrappedResonance's resolution-independence on its
own: measured spread across a [2, 2.5, 3] equilibrium-resolution sweep
stayed non-trivial (~1.7-18%) regardless of stencil order, since the
dominant sensitivity comes from summing over many discrete p/q
resonance crossings across the rho grid, not local interpolation
accuracy. Reducing p_max/q_max from the 10/10 default to 4/4 was the
more effective lever (~16% -> ~8% spread at num_rho=10, num_eta=10).

Update the TrappedResonance-specific settings in
_reduced_resolution_objective to num_rho=10, num_eta=10, p_max=4,
q_max=4 (shared by the no-NaN-grad and resolution-independence tests),
give TrappedResonance a looser 1e-1 rtol in
test_compute_scalar_resolution_others (vs the shared 6e-2), and
re-enable it in that sweep instead of excluding it outright.
Restored by an earlier merge-conflict fix but out of scope for the
trapped-resonance PR; the unrelated notebook API fixes from that same
historical commit are kept. Not referenced anywhere else in the repo.
desc/ResonanceOpt/ (personal notebooks/scripts), island_benchmarks/
(334M of scratch notebooks and .h5/.nc/.npz data files), and stray
.DS_Store files don't belong in the library. Add .DS_Store to
.gitignore so it doesn't come back.
These 7 tests/baseline/*.png files don't match any current test
function or parametrization (and aren't present in upstream DESC
either) — leftover from an earlier state of the branch.
The upstream merge (a736681) left both the old and new wording of
the same two entries (PR PlasmaControl#1826, PlasmaControl#1834) side by side. Drop the
duplicate to match upstream/master.

@dario-panici dario-panici left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good! Main change I'd request is to use the newer Bounce2D functionality for this, and an overall cleanup. The function is pretty complex and there are lots of grids being made within the compute method itself which I think maybe can be made simpler. I will try my hand this week at the Bounce2D version and see if I can think of ways to simplify it.

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.

4 participants