Conversation
…ent transfer map caching might look like
| if self._cached_first_order_transfer_map is None: | ||
| R = drift_matrix(length=self.length, energy=energy, species=species) | ||
|
|
||
| self._cached_first_order_transfer_map = R | ||
|
|
||
| return self._cached_first_order_transfer_map |
There was a problem hiding this comment.
There was a problem hiding this comment.
Which other properties of Beam or Element are expensive to calculate and read often to justify caching? Especially for Beam, I would most properties to change on every tracking, so in which scenario would we benefit from caching some of its computed properties? With transfer_map, that should be constant for most elements along the lattice.
There was a problem hiding this comment.
It might be more effective to cache some intermediate computation artifacts, e.g. in the TWISS parameters. Since if you are polling
|
Partial PR for #538. |
There was a problem hiding this comment.
Pull Request Overview
This PR implements caching for first-order and second-order transfer maps in the accelerator elements, aiming to achieve significant speed-ups by avoiding redundant calculations.
- Refactors the
first_order_transfer_mapandsecond_order_transfer_mapmethods to become caching wrappers - Renames the actual computation methods to
_compute_first_order_transfer_mapand_compute_second_order_transfer_map - Updates all test files to use the new
.track()method syntax instead of calling elements directly
Reviewed Changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cheetah/accelerator/element.py | Implements the caching infrastructure with cache invalidation when defining features change |
| cheetah/accelerator/*.py | Updates all element classes to rename their transfer map methods to the private _compute_* variants |
| tests/test_quadrupole.py | Updates test syntax from direct element calls to .track() method calls and improves variable naming |
| CHANGELOG.md | Documents the performance improvement feature |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Hespe
left a comment
There was a problem hiding this comment.
Looks good to me in principle. Only question would be if we need tests for the caching (or rather proper cache invalidation, I guess) but I am not sure how to do that in a systematic fashion.




Description
Add functionality to elements to cache transfer maps and save on compute time.
Motivation and Context
One of Cheetah's stated goals is speed.
Types of changes
Checklist
flake8(required).pytesttests pass (required).pyteston a machine with a CUDA GPU and made sure all tests pass (required).Note: We are using a maximum length of 88 characters per line.