Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bounding Box to Field Line Integration #908

Merged
merged 14 commits into from
Mar 1, 2024
Merged

Conversation

dpanici
Copy link
Collaborator

@dpanici dpanici commented Feb 28, 2024

  • Adds a bounding box to the field_line_integrate defined by bounds_R and bounds_Z keyword arguments, which form a hollow cylindrical bounding box. If the field line trajectory exits these bounds, the RHS will be multiplied by exp(-(R**2+Z**2)) to stop the trajectory and prevent tracing the field line out to infinity, which is both costly and unnecessary when making a Poincare plot, which is the principle purpose of the function.

In one example I had when tracing the precise_QA with a coilset that had some diverging field lines, it took 1/3 of the time compared to without using bounds, so seems to work fine. Defaults to no bounds.

@ddudt this PR is not addressing anything that happens when a SplineMagneticField is used and the field lines exit its domain of validity when extrap=False unless you specify the bounding box to lie within its domain of validity, in which case it should stop the integration soon after it exits the bounding box and possibly before it exits the field's interpolation domain. If there are ideas of how to treat that then we can make an issue and a separate PR

…that box, an exponential damping factor multiplied with the RHS to avoid integrating the lines out to infinity
@dpanici dpanici requested review from ddudt and f0uriest February 28, 2024 19:18
desc/magnetic_fields.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Feb 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.05%. Comparing base (664a562) to head (7e48dbc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #908   +/-   ##
=======================================
  Coverage   95.05%   95.05%           
=======================================
  Files          83       83           
  Lines       20966    20970    +4     
=======================================
+ Hits        19929    19934    +5     
+ Misses       1037     1036    -1     
Files Coverage Δ
desc/magnetic_fields.py 96.97% <100.00%> (+0.01%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Contributor

github-actions bot commented Feb 28, 2024

|             benchmark_name             |         dt(%)          |         dt(s)          |        t_new(s)        |        t_old(s)        | 
| -------------------------------------- | ---------------------- | ---------------------- | ---------------------- | ---------------------- |
 test_build_transform_fft_lowres         |     -0.41 +/- 2.09     | -5.21e-05 +/- 2.66e-04 |  1.27e-02 +/- 1.2e-04  |  1.28e-02 +/- 2.4e-04  |
 test_build_transform_fft_midres         |     +1.36 +/- 1.45     | +1.25e-03 +/- 1.34e-03 |  9.38e-02 +/- 8.9e-04  |  9.25e-02 +/- 1.0e-03  |
 test_build_transform_fft_highres        |     +1.34 +/- 0.85     | +6.19e-03 +/- 3.91e-03 |  4.68e-01 +/- 3.3e-03  |  4.62e-01 +/- 2.1e-03  |
 test_equilibrium_init_lowres            |     -0.23 +/- 2.45     | -1.36e-03 +/- 1.48e-02 |  6.03e-01 +/- 5.4e-03  |  6.04e-01 +/- 1.4e-02  |
 test_equilibrium_init_medres            |     +0.10 +/- 2.07     | +1.25e-03 +/- 2.59e-02 |  1.25e+00 +/- 1.5e-02  |  1.25e+00 +/- 2.1e-02  |
 test_equilibrium_init_highres           |     -0.23 +/- 1.02     | -9.66e-03 +/- 4.28e-02 |  4.17e+00 +/- 3.1e-02  |  4.17e+00 +/- 3.0e-02  |
 test_objective_compile_dshape_current   |     -1.00 +/- 2.43     | -4.69e-02 +/- 1.13e-01 |  4.63e+00 +/- 5.7e-02  |  4.68e+00 +/- 9.8e-02  |
 test_objective_compile_atf              |     -0.93 +/- 2.63     | -8.64e-02 +/- 2.45e-01 |  9.21e+00 +/- 1.9e-01  |  9.30e+00 +/- 1.5e-01  |
 test_objective_compute_dshape_current   |     -0.67 +/- 1.71     | -4.38e-05 +/- 1.11e-04 |  6.46e-03 +/- 7.4e-05  |  6.50e-03 +/- 8.4e-05  |
 test_objective_compute_atf              |     +7.63 +/- 5.06     | +1.01e-03 +/- 6.72e-04 |  1.43e-02 +/- 4.9e-04  |  1.33e-02 +/- 4.6e-04  |
 test_objective_jac_dshape_current       |     +5.75 +/- 7.04     | +2.54e-03 +/- 3.11e-03 |  4.68e-02 +/- 1.5e-03  |  4.42e-02 +/- 2.7e-03  |
 test_objective_jac_atf                  |     +1.77 +/- 3.76     | +3.52e-02 +/- 7.47e-02 |  2.02e+00 +/- 2.8e-02  |  1.98e+00 +/- 6.9e-02  |
 test_perturb_1                          |     -2.79 +/- 2.49     | -3.91e-01 +/- 3.48e-01 |  1.36e+01 +/- 3.3e-01  |  1.40e+01 +/- 1.1e-01  |
 test_perturb_2                          |     +1.81 +/- 1.94     | +3.63e-01 +/- 3.89e-01 |  2.05e+01 +/- 3.1e-01  |  2.01e+01 +/- 2.3e-01  |
 test_proximal_jac_atf                   |     -0.09 +/- 1.05     | -5.87e-03 +/- 7.14e-02 |  6.80e+00 +/- 4.8e-02  |  6.81e+00 +/- 5.3e-02  |
 test_proximal_freeb_compute             |     +0.41 +/- 0.67     | +5.08e-04 +/- 8.18e-04 |  1.23e-01 +/- 5.7e-04  |  1.23e-01 +/- 5.8e-04  |
 test_proximal_freeb_jac                 |     +2.50 +/- 4.17     | +2.04e-01 +/- 3.40e-01 |  8.35e+00 +/- 1.9e-01  |  8.15e+00 +/- 2.8e-01  |

…ponent of the radial and vertical distance from the boundary
desc/magnetic_fields.py Outdated Show resolved Hide resolved
desc/magnetic_fields.py Outdated Show resolved Hide resolved
# this will hit the R bound
# (there is no Z bound, and R would go to 10.0 if not bounded)
r, z = field_line_integrate(r0, z0, phis, field, bounds_R=(10.05, np.inf))
np.testing.assert_allclose(r[-1], 10.05, rtol=3e-4)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we don't expect the integration to stop immediately after the bound is exited, but it should stop soon after depending on how fast the RHS decays. I made the decay very fast so we can test it, but if for some reason we should lessen the decay rates, then we will need to make these test tols here less tight

f0uriest
f0uriest previously approved these changes Feb 29, 2024
@dpanici dpanici requested review from ddudt and removed request for ddudt February 29, 2024 15:36
desc/magnetic_fields.py Outdated Show resolved Hide resolved
desc/magnetic_fields.py Outdated Show resolved Hide resolved
desc/magnetic_fields.py Outdated Show resolved Hide resolved
tests/test_magnetic_fields.py Show resolved Hide resolved
@dpanici dpanici requested review from ddudt and f0uriest March 1, 2024 15:36
f0uriest
f0uriest previously approved these changes Mar 1, 2024
tests/test_magnetic_fields.py Show resolved Hide resolved
@dpanici dpanici merged commit 4698f41 into master Mar 1, 2024
17 checks passed
@dpanici dpanici deleted the dp/field-trace-bounds branch March 1, 2024 21:56
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.

3 participants