Skip to content

Refactor boris kernel#451

Merged
henry2004y merged 3 commits intomasterfrom
refactor-boris-kernel
Feb 3, 2026
Merged

Refactor boris kernel#451
henry2004y merged 3 commits intomasterfrom
refactor-boris-kernel

Conversation

@henry2004y
Copy link
Owner

  • Fix the output time mismatch with the native Boris implementation
  • Refactor the saving logic

@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 addresses a critical issue where the output times of the Boris kernel implementation did not precisely match the native version, alongside an improvement to the data saving process. By introducing a dedicated function for output velocity correction and refining the saving workflow, the changes ensure that the simulation results are both numerically accurate and consistently recorded. The ability to apply much stricter test tolerances confirms the successful resolution of these discrepancies.

Highlights

  • Output Time Mismatch Fix: Introduced a new helper function _leapfrog_to_output to correctly adjust particle velocities for output, ensuring consistency with the native Boris implementation by applying a half-step velocity push.
  • Refactored Saving Logic: The data saving mechanism within the solve function has been refactored to utilize the new _leapfrog_to_output function and ensure accurate time stamping of saved data for both save_everystep and save_end scenarios.
  • Velocity Push Timing Adjustment: The initial half-step velocity push (vback_kernel!) has been relocated within the solve function to align with the corrected output logic, ensuring proper initialization before the main time-stepping loop.
  • Tightened Test Tolerances: The test tolerances for comparing GPU and CPU Boris kernel solutions have been significantly tightened from rtol = 0.05, atol = 1.0e-4 to rtol = 1.0e-10, atol = 1.0e-10, indicating improved numerical equivalence and correctness of the implementation.

🧠 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
  • src/boris_kernel.jl
    • Added _leapfrog_to_output function to adjust particle velocities to v^n for output.
    • Moved the initial vback_kernel! call to occur after initial solution saving and before the main time-stepping loop.
    • Modified save_everystep and save_end blocks to use _leapfrog_to_output and explicitly calculate t_current for saved data.
  • test/test_boris_kernel.jl
    • Tightened rtol and atol for the sol_gpu[1].u[end] ≈ sol_cpu[1].u[end] test from 0.05 and 1.0e-4 to 1.0e-10 and 1.0e-10 respectively, reflecting improved algorithmic equivalence.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
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 significantly improves the Boris kernel by addressing a critical time mismatch in the output. The introduction of the _leapfrog_to_output function correctly adjusts the particle's velocity for output, ensuring algorithmic equivalence with the native Boris implementation. The reordering of the velocity_back_kernel! call further refines the initialization process. The updated unit tests with tighter tolerances confirm the enhanced accuracy and consistency of the solver. Overall, these changes represent a substantial improvement in the correctness and reliability of the Boris kernel.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Benchmark Results (Julia v1)

Time benchmarks
master c9ca25b... master / c9ca25b...
interpolation/cartesian 0.12 ± 0.001 μs 0.12 ± 0.001 μs 1 ± 0.012
interpolation/spherical 0.411 ± 0.009 μs 0.411 ± 0.01 μs 1 ± 0.033
interpolation/time-dependent 4.27 ± 3.2 μs 4.5 ± 3.1 μs 0.948 ± 0.96
trace/GC/DiffEq Vern6 5.92 ± 1.4 μs 5.96 ± 1.4 μs 0.993 ± 0.33
trace/GC/Native RK4 0.137 ± 0.00016 s 0.136 ± 0.00018 s 1.01 ± 0.0018
trace/GC/Native RK45 3.39 ± 0.14 μs 3.39 ± 0.14 μs 1 ± 0.059
trace/Hybrid/Sheared 3.78 ± 0.14 μs 3.67 ± 0.11 μs 1.03 ± 0.049
trace/analytic field/in place 0.0613 ± 0.032 ms 0.0565 ± 0.026 ms 1.09 ± 0.75
trace/analytic field/in place relativistic 0.0814 ± 0.026 ms 0.0875 ± 0.031 ms 0.93 ± 0.45
trace/analytic field/out of place 0.0461 ± 0.031 ms 0.0457 ± 0.031 ms 1.01 ± 0.97
trace/normalized/out of place 16.1 ± 7.4 μs 16.2 ± 7.6 μs 0.998 ± 0.66
trace/numerical field/Adaptive Boris 4.31 ± 0.093 ms 4.33 ± 0.072 ms 0.997 ± 0.027
trace/numerical field/Boris 7.63 ± 0.081 μs 7.65 ± 0.071 μs 0.997 ± 0.014
trace/numerical field/Boris ensemble 15.2 ± 0.14 μs 15.2 ± 0.13 μs 1 ± 0.013
trace/numerical field/Boris kernel 0.137 ± 0.047 ms 0.138 ± 0.047 ms 0.987 ± 0.47
trace/numerical field/Boris with fields 8.29 ± 0.081 μs 8.3 ± 0.09 μs 0.999 ± 0.015
trace/numerical field/Multistep Boris 10.9 ± 0.099 μs 10.9 ± 0.09 μs 1 ± 0.012
trace/numerical field/in place 27.1 ± 5 μs 27.5 ± 5 μs 0.986 ± 0.25
trace/numerical field/out of place 19.5 ± 4.5 μs 19.4 ± 4.6 μs 1 ± 0.33
trace/time-dependent field/in place 0.137 ± 0.0053 ms 0.136 ± 0.0048 ms 1 ± 0.052
trace/time-dependent field/out of place 0.109 ± 0.0071 ms 0.109 ± 0.0045 ms 1 ± 0.077
time_to_load 2.35 ± 0.048 s 2.3 ± 0.098 s 1.02 ± 0.048
Memory benchmarks
master c9ca25b... master / c9ca25b...
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 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.40%. Comparing base (8345a25) to head (c9ca25b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #451      +/-   ##
==========================================
+ Coverage   81.25%   81.40%   +0.15%     
==========================================
  Files          20       20              
  Lines        2027     2044      +17     
==========================================
+ Hits         1647     1664      +17     
  Misses        380      380              

☔ 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 9dc631e into master Feb 3, 2026
6 checks passed
@henry2004y henry2004y deleted the refactor-boris-kernel branch February 3, 2026 16:52
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