-
Notifications
You must be signed in to change notification settings - Fork 30
Sextupole implementation #406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 10 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
358c999
Add tests for `Sextupole` implementation
jank324 dc37cff
Add sextupole sekelton
jank324 75a0f98
Rearrange contents of `track_methods.py`
jank324 7cbe9e0
Implement method to generate second order transfer map
jank324 747ceb1
Fix sextupole into working state
jank324 f8be117
Fix test failures resulting from minor oversights
jank324 9d51d00
First draft of `ParameterBeam` implementation for `Sextupole`
jank324 00da660
Add a test for `ParameterBeam` tracking through a `Sextupole`
jank324 96ee1a0
Add changelog entry
jank324 2951aa0
Implement `ParameterBeam` in `Sextupole` with first order effects only
jank324 214fc7c
Rename method for computing T to reflect that T is a tensor
jank324 142fca7
Add vectorization and dtype tests for sextupole
Hespe 2061e24
Add clone test for sextupole
Hespe 7a1c5fd
Slightly cleaer docstring for `rotation_matrix` method
jank324 3311f1d
Merge branch 'sextupole' of github.com:desy-ml/cheetah into sextupole
jank324 23a6125
Address (mostly) the comments from Copilot review
jank324 9ebf1b7
Add unit to docstring
jank324 d27f710
Fix precedence that I ignored when originally combining the two steps…
jank324 c99215d
Merge branch 'master' into sextupole
jank324 49980f5
Use realistic sextupole values in test similar to those in EuXFEL lat…
jank324 578e8d2
Merge branch 'sextupole' of github.com:desy-ml/cheetah into sextupole
jank324 4650997
Address the fact the `ParameterBeam` and `ParticleBeam` comparison on…
jank324 b9e9d48
Add docs entry for `Sextupole`
jank324 2fede43
Add vectorised sextupole test
jank324 3645c62
Fix bug discovered in sextupole vectorisation
jank324 0b6c720
Test with vectorisation in first order as well
jank324 e1ed559
Add dependency to speed up einsum operations in `torch`
jank324 f285e3c
Presumed minor speed up in first order titlt by replacing `einsum`
jank324 3d3eea7
Fix `Sextupole.defining_features`
jank324 5971ad0
Clean up matrix multiplications across Cheetah replacing `matmul` wit…
jank324 504e18c
Merge branch 'master' into sextupole
jank324 894dbd5
Add sentence to docstring that MAD convention is used
jank324 64ac13c
Replace `einsum` by matrix multiplications
jank324 e741aca
Add further suggestions by @Hespe
jank324 b05f4d0
Merge branch 'sextupole' of github.com:desy-ml/cheetah into sextupole
jank324 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| RBend, | ||
| Screen, | ||
| Segment, | ||
| Sextupole, | ||
| Solenoid, | ||
| SpaceChargeKick, | ||
| TransverseDeflectingCavity, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import matplotlib.pyplot as plt | ||
| import torch | ||
|
|
||
| from cheetah.accelerator.element import Element | ||
| from cheetah.particles import Beam, ParameterBeam, ParticleBeam, Species | ||
| from cheetah.track_methods import base_rmatrix, base_tmatrix, misalignment_matrix | ||
| from cheetah.utils import verify_device_and_dtype | ||
|
|
||
|
|
||
| class Sextupole(Element): | ||
| """ | ||
| A sextupole element in a particle accelerator. | ||
|
|
||
| :param length: Length in meters. | ||
| :param k2: TODO | ||
| :param misalignment: TODO | ||
| :param tilt: TODO | ||
| :param name: Unique identifier of the element. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| length: torch.Tensor, | ||
| k2: torch.Tensor | None = None, | ||
| misalignment: torch.Tensor | None = None, | ||
| tilt: torch.Tensor | None = None, | ||
| name: str | None = None, | ||
| device: torch.device | None = None, | ||
| dtype: torch.dtype | None = None, | ||
| ) -> None: | ||
| device, dtype = verify_device_and_dtype([length, k2], device, dtype) | ||
| factory_kwargs = {"device": device, "dtype": dtype} | ||
| super().__init__(name=name, **factory_kwargs) | ||
|
|
||
| self.length = torch.as_tensor(length, **factory_kwargs) | ||
|
|
||
| self.register_buffer_or_parameter( | ||
| "k2", torch.as_tensor(k2 if k2 is not None else 0.0, **factory_kwargs) | ||
| ) | ||
| self.register_buffer_or_parameter( | ||
| "misalignment", | ||
| torch.as_tensor( | ||
| misalignment if misalignment is not None else (0.0, 0.0), | ||
| **factory_kwargs, | ||
| ), | ||
| ) | ||
| self.register_buffer_or_parameter( | ||
| "tilt", torch.as_tensor(tilt if tilt is not None else 0.0, **factory_kwargs) | ||
| ) | ||
|
|
||
| def transfer_map(self, energy: torch.Tensor, species: Species) -> torch.Tensor: | ||
| R = base_rmatrix( | ||
| length=self.length, | ||
| k1=torch.zeros_like(self.length), | ||
| hx=torch.zeros_like(self.length), | ||
| species=species, | ||
| tilt=self.tilt, | ||
| energy=energy, | ||
| ) | ||
|
|
||
| if torch.all(self.misalignment == 0): | ||
| return R | ||
| else: | ||
| R_entry, R_exit = misalignment_matrix(self.misalignment) | ||
| R = torch.einsum("...ij,...jk,...kl->...il", R_exit, R, R_entry) | ||
|
jank324 marked this conversation as resolved.
Outdated
|
||
| return R | ||
|
|
||
| def track(self, incoming: Beam) -> Beam: | ||
| """ | ||
| Track the beam through the sextupole element. | ||
|
|
||
| :param incoming: Beam entering the element. | ||
| :return: Beam exiting the element. | ||
| """ | ||
| first_order_tm = self.transfer_map(incoming.energy, incoming.species) | ||
| second_order_tm = base_tmatrix( | ||
| length=self.length, | ||
| k1=torch.zeros_like(self.length), | ||
| k2=self.k2, | ||
| hx=torch.zeros_like(self.length), | ||
| species=incoming.species, | ||
| tilt=self.tilt, | ||
| energy=incoming.energy, | ||
| ) | ||
|
|
||
| if isinstance(incoming, ParameterBeam): | ||
| # For ParameterBeam, only first-order effects are applied | ||
| return super().track(incoming) | ||
| elif isinstance(incoming, ParticleBeam): | ||
| # Apply the transfer map to the incoming particles | ||
| first_order_particles = torch.matmul( | ||
| incoming.particles, first_order_tm.transpose(-2, -1) | ||
| ) | ||
| second_order_particles = torch.einsum( | ||
| "...ijk,...j,...k->...i", | ||
| second_order_tm, | ||
| incoming.particles, | ||
| incoming.particles, | ||
| ) | ||
| outgoing_particles = second_order_particles + first_order_particles | ||
|
|
||
| return ParticleBeam( | ||
| particles=outgoing_particles, | ||
| energy=incoming.energy, | ||
| particle_charges=incoming.particle_charges, | ||
| survival_probabilities=incoming.survival_probabilities, | ||
| species=incoming.species, | ||
| ) | ||
| else: | ||
| raise TypeError( | ||
| f"Unsupported beam type: {type(incoming)}. Expected ParameterBeam or " | ||
| "ParticleBeam." | ||
| ) | ||
|
|
||
| @property | ||
| def is_skippable(self) -> bool: | ||
| return False | ||
|
|
||
| @property | ||
| def is_active(self) -> bool: | ||
| return torch.any(self.k2 != 0.0).item() | ||
|
|
||
| def split(self, resolution: torch.Tensor) -> list[Element]: | ||
| raise NotImplementedError | ||
|
|
||
| def plot(self, ax: plt.Axes, s: float, vector_idx: tuple | None = None) -> None: | ||
| raise NotImplementedError | ||
|
|
||
| def defining_features(self) -> list[str]: | ||
| return super().defining_features() + ["length", "k2"] | ||
|
|
||
| def __repr__(self) -> str: | ||
| return ( | ||
| f"{self.__class__.__name__}(length={repr(self.length)}, " | ||
| f"k2={repr(self.k2)}, " | ||
| f"name={repr(self.name)})" | ||
|
jank324 marked this conversation as resolved.
|
||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.