Skip to content
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

Beam get item method #291

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ build
distributions

docs/_build

*.idea
dev*
26 changes: 26 additions & 0 deletions cheetah/particles/particle_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,32 @@ def momenta(self) -> torch.Tensor:
"""Momenta of the individual particles."""
return torch.sqrt(self.energies**2 - electron_mass_eV**2)

def __getitem__(self, item):
# check if energy is batched
if len(self.energy.shape) == 0:
energy = self.energy
else:
# if energy is batched then make sure the batch dimension matches the
# batch dimension of the particle distribution
if not self.energy.shape == self.particles.shape[:-2]:
raise RuntimeError(
"particle energy batch shape does not match " "particle batch shape"
)

energy = self.energy[item]

# check if particle charges is batched
if len(self.particle_charges.shape) > 1:
particle_charges = self.particle_charges[item]
else:
particle_charges = self.particle_charges

return ParticleBeam(
self.particles[item],
energy,
particle_charges,
)

def __repr__(self) -> str:
return (
f"{self.__class__.__name__}(n={repr(self.num_particles)},"
Expand Down
32 changes: 32 additions & 0 deletions tests/test_particle_beam.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest
import torch

import cheetah
from cheetah import ParticleBeam


Expand Down Expand Up @@ -144,3 +146,33 @@ def test_generate_uniform_ellipsoid_vectorized():
assert torch.allclose(beam.sigma_p, sigma_p)
assert torch.allclose(beam.energy, energy)
assert torch.allclose(beam.total_charge, total_charge)


def test_indexing():
# test batching with beamline parameters
quadrupole = cheetah.Quadrupole(
length=torch.tensor(0.2).unsqueeze(0), k1=torch.rand((5, 2))
)
incoming = cheetah.ParticleBeam.from_parameters(sigma_x=torch.tensor(1e-5))

outgoing = quadrupole.track(incoming)

sub_beam = outgoing[:2]
assert sub_beam.beta_x.shape == torch.Size([2, 2])
assert torch.equal(sub_beam.particle_charges, incoming.particle_charges)
assert torch.equal(sub_beam.energy, incoming.energy)

# test batching with energy
incoming = cheetah.ParticleBeam.from_parameters(sigma_x=torch.tensor(1e-5))
incoming.energy.data = torch.rand((5, 2))

outgoing = quadrupole.track(incoming)
sub_beam = outgoing[:2]

assert sub_beam.beta_x.shape == torch.Size([2, 2])
assert torch.equal(sub_beam.particle_charges, incoming.particle_charges)
assert torch.equal(sub_beam.energy, incoming.energy[:2])

with pytest.raises(RuntimeError):
outgoing.energy.data = torch.rand((4, 2))
outgoing[:2]