Skip to content

Option to use proper normalization for QS objectives - #2304

Open
YigitElma wants to merge 10 commits into
masterfrom
yge/qs-modes
Open

Option to use proper normalization for QS objectives#2304
YigitElma wants to merge 10 commits into
masterfrom
yge/qs-modes

Conversation

@YigitElma

Copy link
Copy Markdown
Collaborator

During stage-1 optimization, if the volume or the average magnetic field is not constrained, the strength of the magnetic field can be reduced by inflating the equilibrium (while still having the same Psi). The actual definition of these objectives (for example, in Landreman&Paul 2022) already includes these normalizations. Our precomputed normalizations cannot prevent these kinds of "cheats" in some cases.

This PR adds the option to choose which equation to use by the new mode argument. The default keeps the old behavior.

@YigitElma YigitElma self-assigned this Aug 27, 2026
@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Memory benchmark result

|               Test Name                |      %Δ      |    Master (MB)     |      PR (MB)       |    Δ (MB)    |    Time PR (s)     |  Time Master (s)   |
| -------------------------------------- | ------------ | ------------------ | ------------------ | ------------ | ------------------ | ------------------ |
  test_objective_jac_w7x                 |   -0.16 %    |     4.244e+03      |     4.237e+03      |    -6.91     |       32.28        |       28.45        |
  test_proximal_jac_w7x_with_eq_update   |   -0.62 %    |     6.851e+03      |     6.809e+03      |    -42.39    |       157.68       |       152.10       |
  test_proximal_freeb_jac                |   -0.07 %    |     1.355e+04      |     1.354e+04      |    -9.63     |       82.53        |       80.79        |
  test_proximal_freeb_jac_blocked        |   -0.45 %    |     7.904e+03      |     7.869e+03      |    -35.26    |       69.71        |       69.13        |
  test_proximal_freeb_jac_batched        |   -0.21 %    |     7.894e+03      |     7.877e+03      |    -16.41    |       69.42        |       68.23        |
  test_proximal_jac_ripple               |   -1.24 %    |     3.800e+03      |     3.753e+03      |    -47.14    |       53.98        |       53.18        |
  test_proximal_jac_ripple_bounce1d      |    0.74 %    |     3.767e+02      |     3.795e+02      |     2.80     |        3.74        |        3.45        |
  test_eq_solve                          |   -0.14 %    |     1.831e+03      |     1.829e+03      |    -2.51     |       53.44        |       52.29        |
  test_objective_quadratic_flux_jac      |    0.58 %    |     1.880e+03      |     1.890e+03      |    10.86     |       35.04        |       34.70        |

For the memory plots, go to the summary of Memory Benchmarks workflow and download the artifact.

@YigitElma YigitElma added the run_benchmarks Run timing benchmarks on this PR against current master branch label Aug 27, 2026
@YigitElma
YigitElma marked this pull request as ready for review August 27, 2026 06:07
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.34%. Comparing base (fe84bd1) to head (5398a8d).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2304   +/-   ##
=======================================
  Coverage   94.33%   94.34%           
=======================================
  Files         101      101           
  Lines       29055    29080   +25     
=======================================
+ Hits        27410    27436   +26     
+ Misses       1645     1644    -1     
Files with missing lines Coverage Δ
desc/objectives/_omnigenity.py 97.57% <100.00%> (+0.52%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor
|             benchmark_name             |         dt(%)          |         dt(s)          |        t_new(s)        |        t_old(s)        | 
| -------------------------------------- | ---------------------- | ---------------------- | ---------------------- | ---------------------- |
 test_build_transform_fft_midres         |     +3.86 +/- 3.76     | +3.48e-02 +/- 3.39e-02 |  9.36e-01 +/- 2.4e-02  |  9.01e-01 +/- 2.4e-02  |
 test_build_transform_fft_highres        |     +2.02 +/- 2.30     | +2.41e-02 +/- 2.74e-02 |  1.22e+00 +/- 5.6e-03  |  1.19e+00 +/- 2.7e-02  |
 test_equilibrium_init_medres            |     +2.31 +/- 3.71     | +1.63e-01 +/- 2.62e-01 |  7.21e+00 +/- 1.6e-01  |  7.05e+00 +/- 2.1e-01  |
 test_objective_compile_dshape_current   |     +2.57 +/- 2.04     | +1.06e-01 +/- 8.42e-02 |  4.23e+00 +/- 8.0e-02  |  4.12e+00 +/- 2.7e-02  |
 test_objective_compute_dshape_current   |     -0.75 +/- 18.33    | -5.40e-06 +/- 1.32e-04 |  7.14e-04 +/- 1.1e-04  |  7.19e-04 +/- 7.1e-05  |
 test_objective_jac_dshape_current       |     +8.40 +/- 24.58    | +1.92e-03 +/- 5.62e-03 |  2.48e-02 +/- 4.5e-03  |  2.29e-02 +/- 3.4e-03  |
 test_perturb_2                          |     -0.19 +/- 3.75     | -3.01e-02 +/- 5.95e-01 |  1.58e+01 +/- 4.7e-01  |  1.59e+01 +/- 3.7e-01  |
 test_proximal_jac_atf_with_eq_update    |     -0.49 +/- 1.36     | -5.84e-02 +/- 1.64e-01 |  1.20e+01 +/- 7.3e-02  |  1.20e+01 +/- 1.5e-01  |
 test_proximal_freeb_jac                 |     +1.11 +/- 3.99     | +5.28e-02 +/- 1.89e-01 |  4.80e+00 +/- 1.8e-01  |  4.74e+00 +/- 6.0e-02  |
 test_solve_fixed_iter_compiled          |     -1.11 +/- 2.26     | -6.62e-02 +/- 1.34e-01 |  5.89e+00 +/- 1.1e-01  |  5.96e+00 +/- 8.2e-02  |
 test_objective_grad_ripple              |     +1.19 +/- 3.10     | +1.01e-02 +/- 2.65e-02 |  8.64e-01 +/- 1.9e-02  |  8.54e-01 +/- 1.8e-02  |
 test_objective_quadratic_flux_jac       |     -2.21 +/- 1.82     | -4.98e-02 +/- 4.11e-02 |  2.20e+00 +/- 1.9e-02  |  2.25e+00 +/- 3.6e-02  |
 test_build_transform_fft_lowres         |     +2.52 +/- 3.87     | +2.11e-02 +/- 3.24e-02 |  8.60e-01 +/- 2.1e-02  |  8.39e-01 +/- 2.5e-02  |
 test_equilibrium_init_lowres            |     -0.03 +/- 3.52     | -1.78e-03 +/- 2.25e-01 |  6.40e+00 +/- 1.8e-01  |  6.40e+00 +/- 1.4e-01  |
 test_objective_compile_atf              |     -1.42 +/- 2.12     | -8.79e-02 +/- 1.32e-01 |  6.11e+00 +/- 1.0e-01  |  6.19e+00 +/- 8.0e-02  |
 test_objective_compute_atf              |     -0.46 +/- 8.81     | -9.73e-06 +/- 1.88e-04 |  2.12e-03 +/- 1.3e-04  |  2.13e-03 +/- 1.4e-04  |
 test_objective_jac_atf                  |     +0.58 +/- 4.15     | +8.95e-03 +/- 6.43e-02 |  1.56e+00 +/- 4.9e-02  |  1.55e+00 +/- 4.2e-02  |
 test_perturb_1                          |     +0.69 +/- 2.25     | +8.17e-02 +/- 2.67e-01 |  1.20e+01 +/- 2.5e-01  |  1.19e+01 +/- 1.0e-01  |
 test_proximal_jac_atf                   |     -0.36 +/- 1.36     | -1.87e-02 +/- 7.04e-02 |  5.17e+00 +/- 5.5e-02  |  5.18e+00 +/- 4.3e-02  |
 test_proximal_freeb_compute             |     -1.26 +/- 2.81     | -2.11e-03 +/- 4.71e-03 |  1.65e-01 +/- 2.7e-03  |  1.67e-01 +/- 3.8e-03  |
 test_solve_fixed_iter                   |     +0.47 +/- 2.71     | +1.13e-01 +/- 6.55e-01 |  2.43e+01 +/- 4.1e-01  |  2.42e+01 +/- 5.1e-01  |
 test_LinearConstraintProjection_build   |     +1.26 +/- 4.75     | +8.51e-02 +/- 3.22e-01 |  6.85e+00 +/- 2.5e-01  |  6.77e+00 +/- 2.1e-01  |
 test_objective_compute_ripple           |     +0.81 +/- 4.77     | +1.93e-03 +/- 1.13e-02 |  2.40e-01 +/- 7.0e-03  |  2.38e-01 +/- 8.9e-03  |
 test_objective_quadratic_flux_compute   |     -0.24 +/- 1.55     | -5.21e-05 +/- 3.34e-04 |  2.16e-02 +/- 1.8e-04  |  2.16e-02 +/- 2.8e-04  |

Github CI performance can be noisy. When evaluating the benchmarks, developers should take this into account.

@dpanici dpanici left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to just silently ignore normalize=True when the hatted metric is requested, as technically it IS normalized. Just set the normalization in the build to 1 in those cases

@YigitElma
YigitElma requested a review from dpanici August 27, 2026 15:43
Comment thread desc/objectives/_omnigenity.py Outdated
Comment thread desc/objectives/_omnigenity.py Outdated
Comment thread desc/objectives/_omnigenity.py Outdated

@YigitElma YigitElma left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Also add the equations in the docstring

@YigitElma
YigitElma marked this pull request as draft August 28, 2026 05:19
@YigitElma
YigitElma marked this pull request as ready for review August 28, 2026 15:58
@YigitElma
YigitElma requested review from a team, IssraAli, ddudt, f0uriest, lkadz, rahulgaur104 and singh-jaydeep and removed request for a team August 28, 2026 15:58
Comment thread tests/test_objective_funs.py Outdated
# the harmonics on each surface are all scaled by the same factor, and the
# norm of the residuals on each surface should be between 0 and 1
ratio = (f["fb_hat"] / f["fb"]).reshape((rho.size, -1))
np.testing.assert_allclose(ratio / ratio[:, :1], 1, rtol=1e-12)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

question
I don't understand what this is testing

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That was testing if the normalization is constant per surface but yeah it is not necessary and a weird way to test. I changed it to just checking between 0 and 1

@YigitElma
YigitElma marked this pull request as draft September 1, 2026 06:16
@YigitElma
YigitElma marked this pull request as ready for review September 4, 2026 04:24
@YigitElma
YigitElma requested a review from ddudt September 4, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run_benchmarks Run timing benchmarks on this PR against current master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants