Skip to content

Refactor Boris solvers - #541

Open
henry2004y wants to merge 17 commits into
masterfrom
refactor-OrdinaryDiffEqBoris
Open

Refactor Boris solvers#541
henry2004y wants to merge 17 commits into
masterfrom
refactor-OrdinaryDiffEqBoris

Conversation

@henry2004y

Copy link
Copy Markdown
Owner

This is the first attempt to develop a subpackage OrdinaryDiffEqBoris as a substitute to the native implementation in TestParticle.jl. To handle the saving logic and in consideration for efficiency, we are using a Position-Verlet (Drift-Kick-Drift) leapfrog structure instead of a Velocity-Verlet (Kick-Drift-Kick) style in TestParticle's original implementation.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces the OrdinaryDiffEqBoris package, which implements the Boris method and its multistep/hyper variants (orders 2, 4, and 6) for particle pushing in electric and magnetic fields. The implementation includes support for both constant and mutable caches, as well as adaptive time-stepping based on the gyroperiod. Feedback was provided regarding potential division-by-zero errors in the adaptive step size calculation when the particle charge-to-mass ratio or the magnetic field magnitude is zero, which could lead to numerical instability or NaN values.

Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
@codecov

codecov Bot commented Apr 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.39785% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.08%. Comparing base (50deaa4) to head (be80053).

Files with missing lines Patch % Lines
src/boris.jl 91.39% 16 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #541      +/-   ##
==========================================
- Coverage   90.45%   90.08%   -0.38%     
==========================================
  Files          18       17       -1     
  Lines        2075     1977      -98     
==========================================
- Hits         1877     1781      -96     
+ Misses        198      196       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@henry2004y

Copy link
Copy Markdown
Owner Author

/gemini review

@henry2004y henry2004y changed the title Refactor ordinary diff eq boris Refactor Boris solvers Apr 17, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces the OrdinaryDiffEqBoris package, which provides OrdinaryDiffEq-compatible implementations of standard and multistep Boris solvers. It also refactors the existing AdaptiveBoris into a unified Boris algorithm with an optional safety parameter for adaptive time-stepping across the library. The review feedback identifies several critical issues in the new implementation, specifically the need to incorporate the integration direction (tdir) into adaptive time-step calculations to support backward integration. Additionally, the reviewer noted that adaptive logic was omitted from the mutable cache version of the solver and suggested adding validation to ensure a time step is provided when running in fixed-step mode.

Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl
Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
Comment thread lib/OrdinaryDiffEqBoris/src/boris_perform_step.jl Outdated
Comment thread src/boris.jl Outdated
@github-actions

github-actions Bot commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master be80053... master / be80053...
flux/multi detector 3.69 ± 0.03 μs 3.74 ± 0.02 μs 0.987 ± 0.0096
flux/single detector 1.28 ± 0.01 μs 1.3 ± 0.021 μs 0.985 ± 0.018
interpolation/cartesian/cubic 0.491 ± 0.01 μs 0.471 ± 0.01 μs 1.04 ± 0.031
interpolation/cartesian/linear 0.05 ± 0.01 μs 0.05 ± 0.01 μs 1 ± 0.28
interpolation/spherical 0.13 ± 0.01 μs 0.13 ± 0.01 μs 1 ± 0.11
interpolation/time-dependent 2.94 ± 3.2 μs 2.79 ± 3.4 μs 1.05 ± 1.7
trace/GC/DiffEq Vern6 6.54 ± 0.55 μs 6.6 ± 0.56 μs 0.991 ± 0.12
trace/GC/Native RK4 0.722 ± 0.0076 ms 0.698 ± 0.0078 ms 1.04 ± 0.016
trace/GC/Native RK45 2.19 ± 0.011 μs 2.19 ± 0.02 μs 1 ± 0.01
trace/analytic field/in place 0.0592 ± 0.033 ms 0.0597 ± 0.033 ms 0.992 ± 0.77
trace/analytic field/in place relativistic 0.089 ± 0.035 ms 0.0807 ± 0.028 ms 1.1 ± 0.58
trace/analytic field/out of place 0.0467 ± 0.033 ms 0.0472 ± 0.033 ms 0.989 ± 0.97
trace/normalized/out of place 17.4 ± 8.8 μs 17.3 ± 9.1 μs 1.01 ± 0.74
trace/numerical field/Adaptive Boris 5.07 ± 3 μs 4.92 ± 2 μs 1.03 ± 0.74
trace/numerical field/Boris 5.45 ± 0.04 μs 5.7 ± 0.05 μs 0.956 ± 0.011
trace/numerical field/Boris ensemble 10.8 ± 0.14 μs 11.4 ± 0.14 μs 0.954 ± 0.017
trace/numerical field/Boris kernel 7.94 ± 0.15 μs 7.9 ± 0.15 μs 1 ± 0.027
trace/numerical field/Boris with fields 5.68 ± 0.053 μs 6.14 ± 0.05 μs 0.925 ± 0.011
trace/numerical field/Hyper Boris (n=2, N=4) 9.81 ± 0.06 μs 10.3 ± 0.071 μs 0.949 ± 0.0087
trace/numerical field/Hyper Boris (n=2, N=6) 10 ± 0.061 μs 10.6 ± 0.07 μs 0.941 ± 0.0084
trace/numerical field/Multistep Boris 8.91 ± 0.059 μs 9.46 ± 0.08 μs 0.942 ± 0.01
trace/numerical field/in place 24.4 ± 4.9 μs 24.4 ± 4.9 μs 0.998 ± 0.28
trace/numerical field/out of place 16.4 ± 4.4 μs 16 ± 4.6 μs 1.02 ± 0.4
trace/time-dependent field/in place 0.136 ± 0.0048 ms 0.139 ± 0.0055 ms 0.976 ± 0.052
trace/time-dependent field/out of place 0.11 ± 0.0053 ms 0.111 ± 0.0082 ms 0.989 ± 0.087
time_to_load 1.97 ± 0.0039 s 1.98 ± 0.04 s 0.993 ± 0.02
Memory benchmarks
master be80053... master / be80053...
flux/multi detector 6 allocs: 0.266 kB 6 allocs: 0.266 kB 1
flux/single detector 0 allocs: 0 B 0 allocs: 0 B
interpolation/cartesian/cubic 0 allocs: 0 B 0 allocs: 0 B
interpolation/cartesian/linear 0 allocs: 0 B 0 allocs: 0 B
interpolation/spherical 0 allocs: 0 B 0 allocs: 0 B
interpolation/time-dependent 16 allocs: 8.94 kB 16 allocs: 8.94 kB 1
trace/GC/DiffEq Vern6 0.175 k allocs: 12.1 kB 0.175 k allocs: 12.1 kB 1
trace/GC/Native RK4 6 allocs: 2.45 kB 6 allocs: 2.45 kB 1
trace/GC/Native RK45 8 allocs: 0.844 kB 8 allocs: 0.844 kB 1
trace/analytic field/in place 2.07 k allocs: 0.091 MB 2.07 k allocs: 0.091 MB 1
trace/analytic field/in place relativistic 2.07 k allocs: 0.091 MB 2.07 k allocs: 0.091 MB 1
trace/analytic field/out of place 2.04 k allocs: 0.0894 MB 2.04 k allocs: 0.0894 MB 1
trace/normalized/out of place 0.756 k allocs: 0.0332 MB 0.756 k allocs: 0.0332 MB 1
trace/numerical field/Adaptive Boris 8 allocs: 0.0543 MB 8 allocs: 0.0543 MB 1
trace/numerical field/Boris 6 allocs: 1.34 kB 6 allocs: 1.34 kB 1
trace/numerical field/Boris ensemble 10 allocs: 2.67 kB 10 allocs: 2.67 kB 1
trace/numerical field/Boris kernel 0.038 k allocs: 2.31 kB 0.038 k allocs: 2.31 kB 1
trace/numerical field/Boris with fields 6 allocs: 1.7 kB 6 allocs: 1.7 kB 1
trace/numerical field/Hyper Boris (n=2, N=4) 6 allocs: 1.34 kB 6 allocs: 1.34 kB 1
trace/numerical field/Hyper Boris (n=2, N=6) 6 allocs: 1.34 kB 6 allocs: 1.34 kB 1
trace/numerical field/Multistep Boris 6 allocs: 1.34 kB 6 allocs: 1.34 kB 1
trace/numerical field/in place 0.412 k allocs: 20.7 kB 0.412 k allocs: 20.7 kB 1
trace/numerical field/out of place 0.378 k allocs: 18.6 kB 0.378 k allocs: 18.6 kB 1
trace/time-dependent field/in place 3.95 k allocs: 0.181 MB 3.95 k allocs: 0.181 MB 1
trace/time-dependent field/out of place 3.92 k allocs: 0.179 MB 3.92 k allocs: 0.179 MB 1
time_to_load 0.149 k allocs: 11.1 kB 0.149 k allocs: 11.1 kB 1

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.

1 participant