-
Notifications
You must be signed in to change notification settings - Fork 17
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
add BmadQuadrupole
element
#153
Conversation
As a general comment. I'm not sure I like this quick-and-dirty drop-in way of integrating Bmad-X elements. However, I think it makes sense to test them this way. In the long-term we should think about how this can be clean up better. Some questions we should ask ourselves:
Maybe slightly unrelated but I saw it in this PR:
|
One small thing to keep in mind, bmadx tracking agrees to single precision with bmadx. I was expecting it to agree to double precision, but it is good enough for now. |
cheetah/bmadx_utils.py
Outdated
def cheetah_to_bmad_coords( | ||
cheetah_coords: torch.Tensor, ref_energy: torch.Tensor, mc2: torch.Tensor | ||
) -> torch.Tensor: | ||
"""Transforms Cheetah coordinates to Bmad coordinates. | ||
:param cheetah_coords: 7-dimensional particle vectors in Cheetah coordinates. | ||
:param ref_energy: reference energy in eV. | ||
""" | ||
# initialize Bmad coordinates: | ||
bmad_coords = cheetah_coords[..., :6].clone() | ||
|
||
# Cheetah longitudinal coords: | ||
tau = cheetah_coords[..., 4] | ||
delta = cheetah_coords[..., 5] | ||
|
||
# compute p0c and bmad z, pz | ||
p0c = torch.sqrt(ref_energy**2 - mc2**2) | ||
energy = ref_energy + delta * p0c | ||
p = torch.sqrt(energy**2 - mc2**2) | ||
beta = p / energy | ||
z = -beta * tau | ||
pz = (p - p0c) / p0c | ||
|
||
# Bmad coords: | ||
bmad_coords[..., 4] = z | ||
bmad_coords[..., 5] = pz | ||
|
||
return bmad_coords, p0c | ||
|
||
|
||
def bmad_to_cheetah_coords( | ||
bmad_coords: torch.Tensor, p0c: torch.Tensor, mc2: torch.Tensor | ||
) -> torch.Tensor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to just integrate these two methods also in ParticleBeam
like the other transformations?
I think p0c
can also be provided as a property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking to include the coordinate transforms in here instead: https://github.com/desy-ml/cheetah/blob/master/cheetah/converters/bmad.py
This way it could also be used to transform beams for both Bmad and Bmad-X.
Any thoughts?
@jp-ga, the question came up with @roussel-ryan, if this or #163 should be merged first. I think a main question in that was if the changes in #163 might make conversions in this PR redundant. Do you have an opinion on this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jp-ga Can you have a brief look at the split
function I wrote a comment about?
@roussel-ryan Can you check if the test I added seems to be about the error you ran into?
Otherwise, I think this is ready to merge!
@jank324 I thought about this back when developing Bmad-X, and at the end I decided that it was better to keep the number of steps in the quad different and independent from the number of elements when splitting. For example, if you have a quad with num_steps=3 and you split the element in 5, the code should do 5 quadrupoles with L/5 length, each of which with num_steps=3 (doing 3 drift-kick-drift steps). In the future I would like to implement fringe fields, so the splitting should have entrance fringe at the first element's entrance and exit fringe at the last element's exit, but we can leave this for a future PR. |
@jp-ga So the behaviour you would like to have now is that each of the 5 child quadrupoles has 3 splits, just like their parent had 3 splits? In that case, don't we need to add the line highlighted below in the element = Quadrupole(
torch.min(resolution, remaining),
self.k1,
misalignment=self.misalignment,
tilt=self.tilt,
num_steps=self.num_steps, # <-- Add this line?
dtype=self.length.dtype,
device=self.length.device,
) |
@jank324 I'm not sure if the test you added will raise the issue I ran into, I think my error had to do with the ellipsoid beam generation, but I'm not sure if that test will run into the same issue. I think it may be likely through since I remember that the different methods used the same code |
@jank324 Exactly, I will add it soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One final comment: Is it intended to keep the dev/
folder? If the notebook is useful, shall we move it to some other places, like a documentation page?
Otherwise it's good to go!
I'm also not sure about the On the other hand, I've never actually felt like I needed to rewrite a notebook for some reason and we might not want to clutter up the repository and history like this. |
So ... to document the decision: The |
Description
Add
BmadQuadrupole
element by interfacing with Bmad-X tracking routines.Motivation and Context
Closes #139
Types of changes
Checklist
flake8
(required).pytest
tests pass (required).pytest
on 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.