Skip to content

Commit

Permalink
Improve parameter names
Browse files Browse the repository at this point in the history
  • Loading branch information
jank324 committed Sep 3, 2023
1 parent 77ae4df commit e7ef902
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
62 changes: 38 additions & 24 deletions cheetah/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,16 @@ def __repr__(self) -> None:
@dataclass
class Dipole(Element):
"""
Dipole magnet (by default a sector bending magnet)
Dipole magnet (by default a sector bending magnet).
:param length: Length in meters.
:param angle: Deflection angle in rad.
:param e1: The angle of inclination of the entrance face [rad].
:param e2: The angle of inclination of the exit face [rad].
:param tilt: Tilt of the magnet in x-y plane [rad].
:param fint: Fringe field integral (of the enterance face).
:param fintx: (only set if different from `fint`) Fringe field integral of the exit
face.
:param fringe_integral: Fringe field integral (of the enterance face).
:param fringe_integral_exit: (only set if different from `fint`) Fringe field
integral of the exit face.
:param gap: The magnet gap [m], NOTE in MAD and ELEGANT: HGAP = gap/2
:param name: Unique identifier of the element.
"""
Expand All @@ -305,8 +305,8 @@ class Dipole(Element):
e1: float = 0
e2: float = 0
tilt: float = 0
fint: float = 0
fintx: float = 0
fringe_integral: float = 0
fringe_integral_exit: float = 0
gap: float = 0

def __init__(
Expand All @@ -316,8 +316,8 @@ def __init__(
e1: float = 0.0,
e2: float = 0.0,
tilt: float = 0.0,
fint: float = 0.0,
fintx: Optional[float] = None,
fringe_integral: float = 0.0,
fringe_integral_exit: Optional[float] = None,
gap: float = 0.0,
name: Optional[str] = None,
**kwargs,
Expand All @@ -327,8 +327,10 @@ def __init__(
self.gap = gap
self.tilt = tilt
self.name = name
self.fint = fint
self.fintx = fint if fintx is None else fintx
self.fringe_integral = fringe_integral
self.fringe_integral_exit = (
fringe_integral if fringe_integral_exit is None else fringe_integral_exit
)
# Rectangular bend
self.e1 = e1
self.e2 = e2
Expand Down Expand Up @@ -381,11 +383,17 @@ def transfer_map(self, energy: float) -> torch.Tensor:
return R

def _transfer_map_enter(self, energy: float) -> torch.Tensor:
if self.fint == 0:
if self.fringe_integral == 0:
return torch.eye(7, device=self.device)
else:
sec_e = 1.0 / np.cos(self.e1)
phi = self.fint * self.hx * self.gap * sec_e * (1 + np.sin(self.e1) ** 2)
phi = (
self.fringe_integral
* self.hx
* self.gap
* sec_e
* (1 + np.sin(self.e1) ** 2)
)
return torch.tensor(
[
[1, 0, 0, 0, 0, 0, 0],
Expand All @@ -401,11 +409,17 @@ def _transfer_map_enter(self, energy: float) -> torch.Tensor:
)

def _transfer_map_exit(self, energy: float) -> torch.Tensor:
if self.fintx == 0:
if self.fringe_integral_exit == 0:
return torch.eye(7, device=self.device)
else:
sec_e = 1.0 / np.cos(self.e2)
phi = self.fint * self.hx * self.gap * sec_e * (1 + np.sin(self.e2) ** 2)
phi = (
self.fringe_integral
* self.hx
* self.gap
* sec_e
* (1 + np.sin(self.e2) ** 2)
)
return torch.tensor(
[
[1, 0, 0, 0, 0, 0, 0],
Expand All @@ -427,8 +441,8 @@ def __repr__(self):
+ f"e1={self.e1:.2f},"
+ f"e2={self.e2:.2f},"
+ f"tilt={self.tilt:.2f},"
+ f"fint={self.fint:.2f},"
+ f"fintx={self.fintx:.2f},"
+ f"fint={self.fringe_integral:.2f},"
+ f"fintx={self.fringe_integral_exit:.2f},"
+ f"gap={self.gap:.2f},"
+ f'name="{self.name}")'
)
Expand All @@ -437,16 +451,16 @@ def __repr__(self):
@dataclass
class RBend(Dipole):
"""
Rectangular bending magnet
Rectangular bending magnet.
:param length: Length in meters.
:param angle: Deflection angle in rad.
:param e1: The angle of inclination of the entrance face [rad].
:param e2: The angle of inclination of the exit face [rad].
:param tilt: Tilt of the magnet in x-y plane [rad].
:param fint: Fringe field integral (of the enterance face).
:param fintx: (only set if different from `fint`) Fringe field integral of the exit
face.
:param fringe_integral: Fringe field integral (of the enterance face).
:param fringe_integral_exit: (only set if different from `fint`) Fringe field
integral of the exit face.
:param gap: The magnet gap [m], NOTE in MAD and ELEGANT: HGAP = gap/2
:param name: Unique identifier of the element.
"""
Expand All @@ -458,8 +472,8 @@ def __init__(
e1: float = 0.0,
e2: float = 0.0,
tilt: float = 0.0,
fint: float = 0.0,
fintx: Optional[float] = None,
fringe_integral: float = 0.0,
fringe_integral_exit: Optional[float] = None,
gap: float = 0.0,
name: Optional[str] = None,
**kwargs,
Expand All @@ -472,8 +486,8 @@ def __init__(
e1=e1,
e2=e2,
tilt=tilt,
fint=fint,
fintx=fintx,
fringe_integral=fringe_integral,
fringe_integral_exit=fringe_integral_exit,
gap=gap,
name=name,
**kwargs,
Expand Down
4 changes: 2 additions & 2 deletions cheetah/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def parse_cheetah_element(element: acc.Element):
"e2": element.e2,
"gap": element.gap,
"tilt": element.tilt,
"fint": element.fint,
"fintx": element.fintx,
"fint": element.fringe_integral,
"fintx": element.fringe_integral,
}
elif isinstance(element, acc.Quadrupole):
element_class = "Quadrupole"
Expand Down
2 changes: 1 addition & 1 deletion test/test_compare_ocelot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_dipole_with_fringe_field():
incoming_beam = cheetah.ParticleBeam.from_astra(
"benchmark/cheetah/ACHIP_EA1_2021.1351.001"
)
cheetah_dipole = cheetah.Dipole(length=0.1, angle=0.1, fint=0.1, gap=0.2)
cheetah_dipole = cheetah.Dipole(length=0.1, angle=0.1, fringe_integral=0.1, gap=0.2)
outgoing_beam = cheetah_dipole.track(incoming_beam)

# Ocelot
Expand Down

0 comments on commit e7ef902

Please sign in to comment.