Skip to content

refactor: use DiffResults for single-pass derivative calculations#453

Merged
henry2004y merged 2 commits intomasterfrom
refactor-diffresults
Feb 4, 2026
Merged

refactor: use DiffResults for single-pass derivative calculations#453
henry2004y merged 2 commits intomasterfrom
refactor-diffresults

Conversation

@henry2004y
Copy link
Owner

This PR integrates DiffResults to optimize field calculations by computing jacobians and function values in a single pass, eliminating redundant field evaluations.

The optimization eliminates redundant field evaluations in three critical functions:

  • get_B_parameters: Used in guiding center equations for computing magnetic field properties
  • get_E_parameters: Used for computing electric field properties
  • _get_B_jacobian: Helper function used by get_magnetic_properties and get_curvature

@gemini-code-assist
Copy link

Summary of Changes

Hello @henry2004y, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant optimization to the field calculation routines by leveraging the DiffResults package. By enabling single-pass computation of both Jacobian matrices and function values, it effectively removes redundant field evaluations in critical functions responsible for determining magnetic and electric field properties, thereby enhancing overall performance and efficiency.

Highlights

  • DiffResults Integration: The DiffResults package has been integrated into the project dependencies to facilitate optimized derivative calculations.
  • Single-Pass Derivative Calculation: Jacobian matrices and function values are now computed in a single pass using DiffResults and ForwardDiff.jacobian!, replacing separate calls for Jacobian and value.
  • Performance Optimization: Redundant field evaluations have been eliminated in get_B_parameters, get_E_parameters, and _get_B_jacobian, leading to improved efficiency for magnetic and electric field property computations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • Project.toml
    • Added DiffResults to the [deps] section.
    • Added DiffResults to the [compat] section.
  • src/TestParticle.jl
    • Added import DiffResults to the module's imports.
  • src/equations.jl
    • Modified get_B_parameters to use DiffResults.JacobianResult, ForwardDiff.jacobian!, DiffResults.jacobian, and DiffResults.value for single-pass derivative calculation.
    • Modified get_E_parameters to use DiffResults.JacobianResult, ForwardDiff.jacobian!, DiffResults.jacobian, and DiffResults.value for single-pass derivative calculation.
  • src/utility/utility.jl
    • Modified _get_B_jacobian to use DiffResults.JacobianResult, ForwardDiff.jacobian!, DiffResults.jacobian, and DiffResults.value for single-pass derivative calculation.
    • Removed a TODO comment related to DiffResults integration.
Activity
  • No specific activity (comments, reviews, etc.) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

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 refactors several functions to use DiffResults.jl for more efficient derivative calculations. By computing the function value and its Jacobian in a single pass, it successfully eliminates redundant function evaluations in get_B_parameters, get_E_parameters, and _get_B_jacobian. The implementation is correct and achieves the intended performance optimization. I've added a few minor suggestions to improve the code's idiomatic style by removing redundant variable assignments from in-place function calls.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Benchmark Results (Julia v1)

Time benchmarks
master 7461ce7... master / 7461ce7...
interpolation/cartesian 0.121 ± 0.01 μs 0.121 ± 0.01 μs 1 ± 0.12
interpolation/spherical 0.411 ± 0.02 μs 0.411 ± 0.01 μs 1 ± 0.054
interpolation/time-dependent 4.17 ± 3.2 μs 4.25 ± 3.1 μs 0.981 ± 1.1
trace/GC/DiffEq Vern6 5.71 ± 1.5 μs 5.72 ± 1.4 μs 0.998 ± 0.36
trace/GC/Native RK4 0.136 ± 0.00012 s 0.136 ± 0.0001 s 1 ± 0.0011
trace/GC/Native RK45 3.35 ± 0.12 μs 3.35 ± 0.12 μs 1 ± 0.051
trace/Hybrid/Sheared 3.67 ± 0.09 μs 3.64 ± 0.1 μs 1.01 ± 0.037
trace/analytic field/in place 0.0561 ± 0.026 ms 0.0558 ± 0.026 ms 1.01 ± 0.67
trace/analytic field/in place relativistic 0.0861 ± 0.03 ms 0.0856 ± 0.033 ms 1.01 ± 0.52
trace/analytic field/out of place 0.0453 ± 0.031 ms 0.0466 ± 0.032 ms 0.972 ± 0.95
trace/normalized/out of place 15.9 ± 7.5 μs 15.8 ± 7.7 μs 1.01 ± 0.68
trace/numerical field/Adaptive Boris 4.32 ± 0.071 ms 4.3 ± 0.07 ms 1 ± 0.023
trace/numerical field/Boris 7.67 ± 0.07 μs 7.61 ± 0.09 μs 1.01 ± 0.015
trace/numerical field/Boris ensemble 15.3 ± 0.12 μs 15.2 ± 0.15 μs 1.01 ± 0.013
trace/numerical field/Boris kernel 0.138 ± 0.046 ms 0.137 ± 0.046 ms 1.01 ± 0.48
trace/numerical field/Boris with fields 8.31 ± 0.07 μs 8.27 ± 0.08 μs 1 ± 0.013
trace/numerical field/Multistep Boris 10.9 ± 0.071 μs 10.9 ± 0.079 μs 1 ± 0.0098
trace/numerical field/in place 27.4 ± 4.9 μs 27.2 ± 4.9 μs 1.01 ± 0.26
trace/numerical field/out of place 19.2 ± 4.6 μs 19.1 ± 4.7 μs 1.01 ± 0.35
trace/time-dependent field/in place 0.137 ± 0.0044 ms 0.138 ± 0.0039 ms 0.996 ± 0.043
trace/time-dependent field/out of place 0.109 ± 0.005 ms 0.11 ± 0.0039 ms 0.994 ± 0.058
time_to_load 2.31 ± 0.018 s 2.37 ± 0.024 s 0.971 ± 0.012
Memory benchmarks
master 7461ce7... master / 7461ce7...
interpolation/cartesian 2 allocs: 0.234 kB 2 allocs: 0.234 kB 1
interpolation/spherical 2 allocs: 0.203 kB 2 allocs: 0.203 kB 1
interpolation/time-dependent 0.044 k allocs: 9.62 kB 0.044 k allocs: 9.62 kB 1
trace/GC/DiffEq Vern6 0.177 k allocs: 12.2 kB 0.177 k allocs: 12.2 kB 1
trace/GC/Native RK4 13 allocs: 0.382 MB 13 allocs: 0.382 MB 1
trace/GC/Native RK45 16 allocs: 2.44 kB 16 allocs: 2.44 kB 1
trace/Hybrid/Sheared 8 allocs: 2.98 kB 8 allocs: 2.98 kB 1
trace/analytic field/in place 2.07 k allocs: 0.0883 MB 2.07 k allocs: 0.0883 MB 1
trace/analytic field/in place relativistic 2.07 k allocs: 0.0883 MB 2.07 k allocs: 0.0883 MB 1
trace/analytic field/out of place 2.03 k allocs: 0.0866 MB 2.03 k allocs: 0.0866 MB 1
trace/normalized/out of place 0.753 k allocs: 0.0321 MB 0.753 k allocs: 0.0321 MB 1
trace/numerical field/Adaptive Boris 27 allocs: 1.5 MB 27 allocs: 1.5 MB 1
trace/numerical field/Boris 6 allocs: 1.16 kB 6 allocs: 1.16 kB 1
trace/numerical field/Boris ensemble 10 allocs: 2.28 kB 10 allocs: 2.28 kB 1
trace/numerical field/Boris kernel 2.03 k allocs: 0.113 MB 2.03 k allocs: 0.113 MB 1
trace/numerical field/Boris with fields 6 allocs: 1.52 kB 6 allocs: 1.52 kB 1
trace/numerical field/Multistep Boris 6 allocs: 1.16 kB 6 allocs: 1.16 kB 1
trace/numerical field/in place 0.432 k allocs: 20.8 kB 0.432 k allocs: 20.8 kB 1
trace/numerical field/out of place 0.393 k allocs: 18.6 kB 0.393 k allocs: 18.6 kB 1
trace/time-dependent field/in place 3.97 k allocs: 0.177 MB 3.97 k allocs: 0.177 MB 1
trace/time-dependent field/out of place 3.93 k allocs: 0.175 MB 3.93 k allocs: 0.175 MB 1
time_to_load 0.145 k allocs: 11 kB 0.145 k allocs: 11 kB 1

@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.65%. Comparing base (9dc631e) to head (7461ce7).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #453      +/-   ##
==========================================
+ Coverage   81.40%   81.65%   +0.24%     
==========================================
  Files          20       20              
  Lines        2044     2050       +6     
==========================================
+ Hits         1664     1674      +10     
+ Misses        380      376       -4     

☔ 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 henry2004y merged commit e337a8f into master Feb 4, 2026
8 checks passed
@henry2004y henry2004y deleted the refactor-diffresults branch February 4, 2026 17:15
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

Comments