Skip to content

Commit

Permalink
Merge pull request #189 from Hespe/179-electron-mass-constants
Browse files Browse the repository at this point in the history
Unify constants for electron mass
  • Loading branch information
jank324 authored Jun 19, 2024
2 parents e2d0a7d + 19f8b41 commit a35fc03
Show file tree
Hide file tree
Showing 20 changed files with 11 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Update reference from arXiv preprint to PRAB publication (see #166) (@jank324)
- Rename converter modules to the respective name of the accelerator code (see #167) (@jank324)
- Added imports to the code example in the README (see #188) (@jank324)
- Refactor definitions of physical constants (see #189) (@hespe)

## [v0.6.3](https://github.com/desy-ml/cheetah/releases/tag/v0.6.3) (2024-03-28)

Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import matplotlib.pyplot as plt
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

from cheetah.particles import Beam, ParticleBeam
Expand All @@ -14,15 +12,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Aperture(Element):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/bpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import matplotlib.pyplot as plt
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size

from cheetah.particles import Beam, ParameterBeam, ParticleBeam
Expand All @@ -15,15 +13,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class BPM(Element):
"""
Expand Down
5 changes: 0 additions & 5 deletions cheetah/accelerator/cavity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/custom_transfer_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import matplotlib.pyplot as plt
import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

from cheetah.particles import Beam
Expand All @@ -13,15 +11,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class CustomTransferMap(Element):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/dipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

from cheetah.track_methods import base_rmatrix, rotation_matrix
Expand All @@ -15,15 +13,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Dipole(Element):
"""
Expand Down
8 changes: 1 addition & 7 deletions cheetah/accelerator/drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import matplotlib.pyplot as plt
import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

Expand All @@ -12,11 +11,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)
Expand Down Expand Up @@ -53,7 +47,7 @@ def transfer_map(self, energy: torch.Tensor) -> torch.Tensor:
device = self.length.device
dtype = self.length.dtype

gamma = energy / rest_energy.to(device=device, dtype=dtype)
gamma = energy / electron_mass_eV.to(device=device, dtype=dtype)
igamma2 = torch.zeros_like(gamma) # TODO: Effect on gradients?
igamma2[gamma != 0] = 1 / gamma[gamma != 0] ** 2
beta = torch.sqrt(1 - igamma2)
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@

import matplotlib.pyplot as plt
import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import nn

from cheetah.particles import Beam, ParameterBeam, ParticleBeam
from cheetah.utils import UniqueNameGenerator

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Element(ABC, nn.Module):
"""
Expand Down
8 changes: 1 addition & 7 deletions cheetah/accelerator/horizontal_corrector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

Expand All @@ -14,11 +13,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)
Expand Down Expand Up @@ -57,7 +51,7 @@ def transfer_map(self, energy: torch.Tensor) -> torch.Tensor:
device = self.length.device
dtype = self.length.dtype

gamma = energy / rest_energy.to(device=device, dtype=dtype)
gamma = energy / electron_mass_eV.to(device=device, dtype=dtype)
igamma2 = torch.zeros_like(gamma) # TODO: Effect on gradients?
igamma2[gamma != 0] = 1 / gamma[gamma != 0] ** 2
beta = torch.sqrt(1 - igamma2)
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import matplotlib.pyplot as plt
import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import Size

from cheetah.particles import Beam
Expand All @@ -13,15 +11,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Marker(Element):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/quadrupole.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

from cheetah.track_methods import base_rmatrix, misalignment_matrix
Expand All @@ -15,15 +13,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Quadrupole(Element):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/rbend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Optional, Union

import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import nn

from cheetah.utils import UniqueNameGenerator
Expand All @@ -11,15 +9,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class RBend(Dipole):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import matplotlib.pyplot as plt
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn
from torch.distributions import MultivariateNormal

Expand All @@ -16,15 +14,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Screen(Element):
"""
Expand Down
11 changes: 0 additions & 11 deletions cheetah/accelerator/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import matplotlib
import matplotlib.pyplot as plt
import torch
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

from cheetah.converters.bmad import convert_bmad_lattice
Expand All @@ -22,15 +20,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)


class Segment(Element):
"""
Expand Down
8 changes: 1 addition & 7 deletions cheetah/accelerator/solenoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import matplotlib.pyplot as plt
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

Expand All @@ -14,11 +13,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)
Expand Down Expand Up @@ -66,7 +60,7 @@ def transfer_map(self, energy: torch.Tensor) -> torch.Tensor:
device = self.length.device
dtype = self.length.dtype

gamma = energy / rest_energy.to(device=device, dtype=dtype)
gamma = energy / electron_mass_eV.to(device=device, dtype=dtype)
c = torch.cos(self.length * self.k)
s = torch.sin(self.length * self.k)

Expand Down
8 changes: 1 addition & 7 deletions cheetah/accelerator/undulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import matplotlib.pyplot as plt
import torch
from matplotlib.patches import Rectangle
from scipy import constants
from scipy.constants import physical_constants
from torch import Size, nn

Expand All @@ -13,11 +12,6 @@

generate_unique_name = UniqueNameGenerator(prefix="unnamed_element")

rest_energy = torch.tensor(
constants.electron_mass
* constants.speed_of_light**2
/ constants.elementary_charge # electron mass
)
electron_mass_eV = torch.tensor(
physical_constants["electron mass energy equivalent in MeV"][0] * 1e6
)
Expand Down Expand Up @@ -53,7 +47,7 @@ def transfer_map(self, energy: torch.Tensor) -> torch.Tensor:
device = self.length.device
dtype = self.length.dtype

gamma = energy / rest_energy.to(device=device, dtype=dtype)
gamma = energy / electron_mass_eV.to(device=device, dtype=dtype)
igamma2 = (
1 / gamma**2
if gamma != 0
Expand Down
Loading

0 comments on commit a35fc03

Please sign in to comment.