Skip to content

Optimizing ParticleTracker and cpr.Tracker#2911

Merged
pheuer merged 51 commits intoPlasmaPy:mainfrom
pheuer:streamline_tracker_less_interp
Jan 13, 2025
Merged

Optimizing ParticleTracker and cpr.Tracker#2911
pheuer merged 51 commits intoPlasmaPy:mainfrom
pheuer:streamline_tracker_less_interp

Conversation

@pheuer
Copy link
Member

@pheuer pheuer commented Nov 18, 2024

This PR contains a long list of mostly minor improvements to the ParticleTracker and charged particle radiography Tracker focused on reducing unnecessary repetitive calculations.

Major changes

  • Made all of the axis properties of AbstractGrid cached_properties
  • Added a 'soften edges' method to Grid that can help prepare the grid for particle tracking.

Minor changes (no changelog necessary)

  • Wrapped statements in test_particle_tracker with warnings filter to catch a warning about fields not being zero at the edges of the grids. It's ok -these are just tests.
  • ParticleTracker interpolator now handles quantities that aren't provided on the grids a bit more elegantly - we don't have to add arrays of zeros and then interpolate over them anymore.
  • Cached some masks (e.g. _tracked_particle_mask) and reset the cache only as needed - these were cheap to compute, but they were being recomputed a lot, so this helps reduce that.

@github-actions
Copy link

Thank you for submitting this pull request (PR)! ✨ PlasmaPy depends on contributors like you, so we deeply appreciate it!

The bottom of this page shows several checks that are run for every PR. ✅ Don't worry if something broke! We break stuff all the time. 😅 Click on "Details" to learn why a check didn't pass. Please also feel free to ask for help. We do that all the time as well! 🌸 We invite you to our weekly community meeting (2 pm ET on most Tuesdays) and office hours (3 pm ET on most Thursdays).

After this PR is marked as ready for review, someone should stop by to provide a code review and offer suggestions soon. 👥 If we don't provide a review within a few days, please feel free to send us a reminder. 🎗

PlasmaPy's contributor guide has information on:

Tip

📚 For a documentation preview, click on Details next to docs/readthedocs.org:plasmapy. For cryptic documentation errors, see the documentation troubleshooting guide.

Tip

🧹 Automatically fix most pre-commit.ci failures by commenting pre-commit.ci autofix below. For other failures, please see the pre-commit troubleshooting guide.

We thank you once again! 🌌

@github-actions github-actions bot added plasmapy.plasma Related to the plasmapy.plasma subpackage testing plasmapy.diagnostics Related to the plasmapy.diagnostics subpackage plasmapy.simulation Related to the plasmapy.simulation subpackage python Pull requests that update Python code labels Nov 18, 2024
@codecov
Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 97.48744% with 5 lines in your changes missing coverage. Please review.

Project coverage is 94.43%. Comparing base (9a0b172) to head (8487be7).
Report is 119 commits behind head on main.

Files with missing lines Patch % Lines
...py/simulation/particle_tracker/particle_tracker.py 97.72% 0 Missing and 3 partials ⚠️
...rged_particle_radiography/synthetic_radiography.py 93.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2911      +/-   ##
==========================================
- Coverage   94.75%   94.43%   -0.33%     
==========================================
  Files         108      108              
  Lines        9631     9681      +50     
  Branches     1451     1462      +11     
==========================================
+ Hits         9126     9142      +16     
- Misses        324      346      +22     
- Partials      181      193      +12     

☔ 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.

@pheuer
Copy link
Member Author

pheuer commented Nov 18, 2024

@namurphy I added some additional print output which is causing doctest to fail - how does one reset that to accept the new test output as 'normal'?

@namurphy
Copy link
Member

namurphy commented Nov 21, 2024

@namurphy I added some additional print output which is causing doctest to fail - how does one reset that to accept the new test output as 'normal'?

That's a good question! We can do that by updating the example section of each docstring directly to have the new output rather than the old. So it'd be something like changing a docstring from

>>> simulation.run()
>>> simulation.whatevs()

to

>>> simulation.run()
Awesomely running awesome simulation.
Finished in 4.6 gigaseconds.
>>> simulation.whatevs()

@pheuer
Copy link
Member Author

pheuer commented Nov 21, 2024

That's a good question! We can do that by updating the example section of each docstring directly to have the new output rather than the old. So it'd be something like changing a docstring from

Ah, awesome, I understand how that works now! Thanks!

@pheuer
Copy link
Member Author

pheuer commented Dec 4, 2024

The error in the tests is probably coming from the fact that the test uses the np.reshape(shape = ?) keyword argument, but that keyword argument was only added in numpy 2.1. In the older versions of numpy, shape could only be a positional argument, and maybe on the 3.11 CI tests environment, one of the earlier numpy versions is installed? The better question is, why wasn't this a problem before...

=========================== short test summary info ===========================
FAILED tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py::test_NIST_particle_stopping[ALUMINUM-density0-energy_projected_range_list0] - TypeError: reshape() got an unexpected keyword argument 'shape'
FAILED tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py::test_NIST_particle_stopping[SILICON-density1-energy_projected_range_list1] - TypeError: reshape() got an unexpected keyword argument 'shape'

EDIT: Looks like that worked!

@pheuer
Copy link
Member Author

pheuer commented Jan 2, 2025

@PlasmaPy/plasmapy-reviewers Can we get this merged in the next few weeks? I've left comments trying to explain what was done - please tag me with any further questions.

Copy link
Member

@namurphy namurphy left a comment

Choose a reason for hiding this comment

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

Thank you for doing this! And also thank you for your patience while I was proposal writing and taking a month off of work. I have a few suggestions, and overall it's looking good.

For soften_edges, there are two things I'm wondering about.

  1. If I'm understanding this correctly, the erf formulation for the mask in soften_edges will change all of the values on the grid. At the center of the grid, the original values would be multiplied by a value that is really close to, but slightly less than, one. Is there a mask we could apply that doesn't affect quantities toward the center of the grid? Or alternatively, should we add an admonition to the docstring that mentions this?

  2. If this mask is applied to the magnetic field, I believe this would cause the resulting magnetic field to violate the divergence constraint unless $\mathbf{B} · ∇ f = 0$. Should we add an admonition to the docstring that mentions that the divergence constraint will not be preserved?

    I got the condition using the vector identity that $∇ · \left(f \mathbf{B}\right) ≡ f ∇ · \mathbf{B} + \mathbf{B} · ∇f$, and noting that $∇ · \mathbf{B} = 0$ so the first term goes away. My first vector calculus of 2025! 😹

Thank you again!


return Tmin < Tmax

def soften_edges(self, width: u.Quantity | None = None) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

If the grid has a sharp edge, that gradient can mess up the tracking results, so this is a nice pre-filter to run before particle tracing.

This is helpful information! If this information isn't already included somewhere in the documentation, it would be good to add it. I'm not sure if it would go best here and/or somewhere else.

@pheuer
Copy link
Member Author

pheuer commented Jan 9, 2025

@namurphy I'm working on addressing these, but I think one of the recent changes has broken pre-commit on windows. It works for me on old branches (not up to date with main), but on main now I get:

PS C:\Users\pvheu\GitHub\PlasmaPy> pre-commit run --all-files
[INFO] Installing environment for https://github.com/mattlqx/pre-commit-search-and-replace.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('rbenv', 'init', '-')
return code: 1
stdout:
    Executable `bash` not found
stderr: (none)
Check the log at C:\Users\pvheu\.cache\pre-commit\pre-commit.log

With the unhelpful log:

### version information

pre-commit version: 4.0.1
git --version: git version 2.31.0.windows.1
sys.version:
3.12.7 (main, Oct 16 2024, 00:21:24) [MSC v.1929 64 bit (AMD64)]
sys.executable: C:\Users\pvheu.virtualenvs\dev312\Scripts\python.exe
os.name: nt
sys.platform: win32


### error information

An unexpected error has occurred: CalledProcessError: command: ('rbenv', 'init', '-')
return code: 1
stdout:
Executable bash not found
stderr: (none)


Traceback (most recent call last):
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\error_handler.py", line 73, in error_handler
yield
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\main.py", line 417, in main
return run(args.config, store, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\commands\run.py", line 442, in run
install_hook_envs(to_install, store)
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\repository.py", line 229, in install_hook_envs
_hook_install(hook)
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\repository.py", line 85, in _hook_install
lang.install_environment(
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\languages\ruby.py", line 125, in install_environment
lang_base.setup_cmd(prefix, ('rbenv', 'init', '-'))
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\lang_base.py", line 86, in setup_cmd
cmd_output_b(*cmd, cwd=prefix.prefix_dir, **kwargs)
File "C:\Users\pvheu.virtualenvs\dev312\Lib\site-packages\pre_commit\util.py", line 111, in cmd_output_b
raise CalledProcessError(returncode, cmd, stdout_b, stderr_b)
pre_commit.util.CalledProcessError: command: ('rbenv', 'init', '-')
return code: 1
stdout:
Executable bash not found
stderr: (none)

@pheuer
Copy link
Member Author

pheuer commented Jan 9, 2025

pre-commit.ci autofix

@pheuer
Copy link
Member Author

pheuer commented Jan 9, 2025

Hi @namurphy

  1. If I'm understanding this correctly, the erf formulation for the mask in soften_edges will change all of the values on the grid. At the center of the grid, the original values would be multiplied by a value that is really close to, but slightly less than, one. Is there a mask we could apply that doesn't affect quantities toward the center of the grid? Or alternatively, should we add an admonition to the docstring that mentions this?

Yes, I don't see a clean way to fix this and it's a very minor problem since the ERF is very close to one away from the edge, so I've just mentioned it in the docstring.

  1. If this mask is applied to the magnetic field, I believe this would cause the resulting magnetic field to violate the divergence constraint

Yes, good point, I've added a note to that effect.

@pheuer
Copy link
Member Author

pheuer commented Jan 10, 2025

Ok, I think all the comments are addressed - my pre-commit is still broken locally, but I was able to run it through the comment bot, so it's passing now.

Copy link
Member

@namurphy namurphy left a comment

Choose a reason for hiding this comment

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

I have one final optional suggestion for one of the tests which you can consider, and I'd say it's ready to go! Thank you for doing this!

@pheuer
Copy link
Member Author

pheuer commented Jan 13, 2025

pre-commit.ci autofix

@pheuer pheuer enabled auto-merge (squash) January 13, 2025 22:36
@pheuer pheuer merged commit 2ea3d24 into PlasmaPy:main Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change For breaking changes, excluding deprecations and planned removals CI Related to continuous integration feature For new functionality, excluding breaking changes maintenance General updates to package infrastructure plasmapy.diagnostics Related to the plasmapy.diagnostics subpackage plasmapy.plasma Related to the plasmapy.plasma subpackage plasmapy.simulation Related to the plasmapy.simulation subpackage plasmapy.utils Related to the plasmapy.utils subpackage python Pull requests that update Python code static type checking testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants