Skip to content

Commit e98ef0b

Browse files
authored
Merge branch 'master' into 231-add-particle-species-in-particlebeam
2 parents de73cbd + f37f50e commit e98ef0b

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### 🚀 Features
1010

11-
- `ParticleBeam` now supports importing from and exporting to [openPMD-beamphysics](https://github.com/ChristopherMayes/openPMD-beamphysics) HDF5 files and `ParticleGroup` objects. This allows for easy conversion to and from other file formats supported by openPMD-beamphysics. (see #305) (@cr-xu)
11+
- `ParticleBeam` now supports importing from and exporting to [openPMD-beamphysics](https://github.com/ChristopherMayes/openPMD-beamphysics) HDF5 files and `ParticleGroup` objects. This allows for easy conversion to and from other file formats supported by openPMD-beamphysics. (see #305, #320) (@cr-xu, @Hespe)
1212
- Add the option to choose the particle species `Species` for `Beam` classes (see #276) (@cr-xu)
1313

1414
### 🐛 Bug fixes

cheetah/particles/particle_beam.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import List, Literal, Optional, Tuple, Union
33

44
import numpy as np
5-
import pmd_beamphysics as openpmd
65
import torch
76
from matplotlib import pyplot as plt
87
from scipy import constants
@@ -692,6 +691,14 @@ def from_openpmd_file(
692691
cls, path: str, energy: torch.Tensor, device=None, dtype=None
693692
) -> "ParticleBeam":
694693
"""Load an openPMD particle group HDF5 file as a Cheetah `ParticleBeam`."""
694+
try:
695+
import pmd_beamphysics as openpmd
696+
except ImportError:
697+
raise ImportError(
698+
"""To use the openPMD beam import, openPMD-beamphysics must be
699+
installed."""
700+
)
701+
695702
particle_group = openpmd.ParticleGroup(path)
696703
return cls.from_openpmd_particlegroup(
697704
particle_group, energy, device=device, dtype=dtype
@@ -700,7 +707,7 @@ def from_openpmd_file(
700707
@classmethod
701708
def from_openpmd_particlegroup(
702709
cls,
703-
particle_group: openpmd.ParticleGroup,
710+
particle_group: "openpmd.ParticleGroup", # noqa: F821
704711
energy: torch.Tensor,
705712
device=None,
706713
dtype=None,
@@ -749,7 +756,7 @@ def save_as_openpmd_h5(self, path: str) -> None:
749756
particle_group = self.to_openpmd_particlegroup()
750757
particle_group.write(path)
751758

752-
def to_openpmd_particlegroup(self) -> openpmd.ParticleGroup:
759+
def to_openpmd_particlegroup(self) -> "openpmd.ParticleGroup": # noqa: F821
753760
"""
754761
Convert the `ParticleBeam` to an openPMD `ParticleGroup` object.
755762
@@ -761,6 +768,14 @@ def to_openpmd_particlegroup(self) -> openpmd.ParticleGroup:
761768
762769
:return: openPMD `ParticleGroup` object with the `ParticleBeam`'s particles.
763770
"""
771+
try:
772+
import pmd_beamphysics as openpmd
773+
except ImportError:
774+
raise ImportError(
775+
"""To use the openPMD beam export, openPMD-beamphysics must be
776+
installed."""
777+
)
778+
764779
# For now only support non-batched particles
765780
if len(self.particles.shape) != 2:
766781
raise ValueError("Only non-batched particles are supported.")

test_requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
git+https://github.com/cr-xu/ocelot@update-scipy-compatibility # Ocelot
2+
openpmd-beamphysics
23
pytest
34
pytest-cov

0 commit comments

Comments
 (0)