-
Notifications
You must be signed in to change notification settings - Fork 6
/
pmd_utils.py
263 lines (212 loc) · 6.34 KB
/
pmd_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from bmadx.constants import C_LIGHT, M_ELECTRON, E_CHARGE
from bmadx.structures import Particle
from bmadx.bmad_torch.track_torch import Beam, particle_to_beam
import numpy as np
import torch
import sys
from pmd_beamphysics import ParticleGroup
def openpmd_to_bmadx_coords(
pmd_particle: ParticleGroup,
p0c
):
"""
Transforms openPMD-beamphysics ParticleGroup to
bmad phase-space coordinates.
Parameters:
pmd_particle (pmd_beamphysics.ParticleGroup): openPMD-beamphysics ParticleGroup
p0c (float): reference momentum in eV
Returns:
bmad_coods (list): list of bmad coords (x, px, y, py, z, pz)
"""
x = pmd_particle.x
px = pmd_particle.px / p0c
y = pmd_particle.y
py = pmd_particle.py / p0c
z = - pmd_particle.beta * C_LIGHT * pmd_particle.t
pz = pmd_particle.p / p0c - 1.0
bmad_coords = (x, px, y, py, z, pz)
return bmad_coords
def openpmd_to_bmadx_particles(
pmd_particle: ParticleGroup,
p0c: float,
s : float = 0.0,
mc2 : float = M_ELECTRON
):
"""
Transforms openPMD-beamphysics ParticleGroup to
bmad phase-space Particle named tuple.
Parameters:
pmd_particle (pmd_beamphysics.ParticleGroup): openPMD-beamphysics ParticleGroup
p0c (float): reference momentum in eV
Returns:
Bmadx Particle
"""
coords = openpmd_to_bmadx_coords(pmd_particle, p0c)
particle = Particle(
*coords,
s = s,
p0c = p0c,
mc2 = mc2)
return particle
def openpmd_to_bmadx_beam(
pmd_particle: ParticleGroup,
p0c,
s = torch.tensor(0.0, dtype=torch.float32),
mc2 = torch.tensor(M_ELECTRON, dtype=torch.float32)
):
"""
Transforms openPMD-beamphysics ParticleGroup to
bmad phase-space Particle named tuple.
Parameters:
pmd_particle (pmd_beamphysics.ParticleGroup): openPMD-beamphysics ParticleGroup
p0c (float): reference momentum in eV
Returns:
Bmadx torch Beam
"""
particle = openpmd_to_bmadx_particles(pmd_particle, p0c, s, mc2)
beam = particle_to_beam(particle)
return beam
def bmadx_particles_to_openpmd(particle: Particle):
"""
Transforms bmadx Particle to openPMD-beamphysics ParticleGroup.
Parameters
----------
particle: bmax Particle
particle to transform.
Returns
-------
pmd_beamphysics.ParticleGroup
"""
lib = sys.modules[type(particle.x).__module__]
if lib == np:
x = particle.x
px = particle.px
y = particle.y
py = particle.py
z = particle.z
pz = particle.pz
elif lib == torch:
x = particle.x.detach().numpy()
px = particle.px.detach().numpy()
y = particle.y.detach().numpy()
py = particle.py.detach().numpy()
z = particle.z.detach().numpy()
pz = particle.pz.detach().numpy()
else:
raise ValueError('Only numpy and torch Particles are supported as of now')
dat = {}
dat['x'] = x
dat['px'] = px * particle.p0c
dat['y'] = y
dat['py'] = py * particle.p0c
dat['z'] = pz * 0.0
dat['pz'] = particle.p0c * ( (pz + 1.0)**2 - px**2 - py**2 )**0.5
p = (1 + pz ) * particle.p0c
beta = (
(p / M_ELECTRON)**2 /
( 1 + (p / M_ELECTRON)**2 )
)**0.5
dat['t'] = - z / (C_LIGHT * beta)
dat['status'] = np.ones_like(x, dtype=int)
dat['weight'] = np.ones_like(x) * E_CHARGE
if np.isclose(particle.mc2, M_ELECTRON):
dat['species'] = 'electron'
else:
raise ValueError('only electrons are supported as of now')
return ParticleGroup(data=dat)
def bmadx_beam_to_openpmd(beam: Beam):
"""
Transforms bmadx torch Beam to openPMD-beamphysics ParticleGroup.
Parameters
----------
beam: bmax torch Beam to transform.
Returns
-------
pmd_beamphysics.ParticleGroup
"""
particle = beam.numpy_particles()
pmd_particle = bmadx_particles_to_openpmd(particle)
return pmd_particle
def save_particles_as_h5(particle: Particle, fname: str):
"""
Saves bmadx Particle as h5 file in openPMD-beamphysics
ParticleGroup standard.
Parameters
----------
particle: bmax Particle
particle to transform.
fname: str
file name
Returns
-------
None
"""
pmd_particle = bmadx_particles_to_openpmd(particle)
pmd_particle.write(fname)
def save_beam_as_h5(beam: Beam, fname: str):
"""
Saves bmadx torch Beam as h5 file in openPMD-beamphysics
ParticleGroup standard.
Parameters
----------
beam: bmax torch Beam to transform.
fname: str
file name
Returns
-------
None
"""
pmd_particle = bmadx_beam_to_openpmd(beam)
pmd_particle.write(fname)
def opal_data_to_bmadx_particle(
opal_data_file: str,
p0c: float = None,
mc2: float = M_ELECTRON
):
"""
Transforms OPAL particle coordinates in a data file to
Bmad-X Particle beam.
Parameters
----------
opal_data_file (str): OPAL data file with particle coordinates
p0c (float): design momentum times c in eV as defined in Bmad coords
mc2 (float): particle rest mass energy in eV
Returns
-------
particle (bmadx.Particle): Bmad-X Particle beam
"""
data = np.genfromtxt(opal_data_file, skip_header=1)
pc = mc2 * np.sqrt(data[:,1]**2 + data[:,3]**2 + data[:,5]**2)
# if not provided, reference momentum is avg p
if p0c is None:
p0c = pc.mean()
# initial transforms
x = data[:,0]
y = data[:,2]
px = mc2 * data[:,1] / p0c
py = mc2 * data[:,3] / p0c
pz = pc / p0c - 1.0
# drift to z_avg (so that s value is the same)
z0 = data[:,4].mean()
dt = (z0 - data[:,4]) / data[:,5]
x = x + data[:,1] * dt
y = y + data[:,3] * dt
# transform z coord
beta = np.sqrt(
(pc / mc2)**2 /
( 1 + (pc / mc2)**2 )
)
z = - beta * C_LIGHT * dt
# return bmadx particle
particle = Particle(
x = x,
px = px,
y = y,
py = py,
z = z,
pz = pz,
s = 0.0,
p0c = p0c,
mc2 = mc2
)
return particle