ImpactX
Loading...
Searching...
No Matches
ChrQuad.H
Go to the documentation of this file.
1/* Copyright 2022-2026 The Regents of the University of California, through Lawrence
2 * Berkeley National Laboratory (subject to receipt of any required
3 * approvals from the U.S. Dept. of Energy). All rights reserved.
4 *
5 * This file is part of ImpactX.
6 *
7 * Authors: Chad Mitchell, Axel Huebl
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_CHRQUAD_H
11#define IMPACTX_CHRQUAD_H
12
14#include "mixin/alignment.H"
15#include "mixin/pipeaperture.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thick.H"
19#include "mixin/spintransport.H"
20#include "mixin/named.H"
21#include "mixin/nofinalize.H"
22
23#include <AMReX_Extension.H>
24#include <AMReX_Math.H>
25#include <AMReX_REAL.H>
26#include <AMReX_SIMD.H>
27
28#include <cmath>
29#include <stdexcept>
30
31namespace impactx::elements
32{
33 struct ChrQuad
34 : public mixin::Named,
35 public mixin::BeamOptic<ChrQuad>,
36 public mixin::LinearTransport<ChrQuad>,
37 public mixin::Thick,
38 public mixin::Alignment,
41 public mixin::NoFinalize,
42 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
43 {
44 static constexpr auto type = "ChrQuad";
46
47 static constexpr int DEFAULT_unit = 0;
48
90
92 void reverse () { Thick::reverse(); }
93
95 using BeamOptic::operator();
96
104 void compute_constants (RefPart const & refpart)
105 {
106 using namespace amrex::literals; // for _rt and _prt
107 using amrex::Math::powi;
108
109 Alignment::compute_constants(refpart);
110
111 // length of the current slice
112 m_slice_ds = m_ds / nslice();
113
114 // access reference particle values to find beta and gamma
115 m_beta = refpart.beta();
116 amrex::ParticleReal const gamma = refpart.gamma();
117
118 // normalize quad units to MAD-X convention if needed
119 m_g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
120
121 m_const1 = 1_prt / (2_prt * powi<3>(m_beta) * powi<2>(gamma));
122 }
123
135 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
138 T_Real & AMREX_RESTRICT x,
139 T_Real & AMREX_RESTRICT y,
140 T_Real & AMREX_RESTRICT t,
141 T_Real & AMREX_RESTRICT px,
142 T_Real & AMREX_RESTRICT py,
143 T_Real const & AMREX_RESTRICT pt,
144 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
145 RefPart const & AMREX_RESTRICT refpart
146 ) const
147 {
148 using namespace amrex::literals; // for _rt and _prt
149 using amrex::Math::powi;
150 using namespace std; // for cmath(float)
151
152 // access position data
153 T_Real const xout = x;
154 T_Real const yout = y;
155 T_Real const tout = t;
156
157 // compute particle momentum deviation delta + 1
158 T_Real const delta1 = sqrt(1_prt - 2_prt * pt / m_beta + powi<2>(pt));
159 T_Real const delta = delta1 - 1_prt;
160
161 // compute phase advance per unit length in s (in rad/m)
162 // chromatic dependence on delta is included
163 T_Real const omega = sqrt(abs(m_g) / delta1);
164
165 // initialize output values of momenta
166 T_Real pxout = px;
167 T_Real pyout = py;
168 // T_Real const ptout = pt;
169
170 if (m_g != 0_prt)
171 {
172 // Advance transverse position and momentum.
173
174 // Focusing/defocusing quad:
175 // The two cases differ only by which plane is oscillatory
176 // vs. hyperbolic; the m_g sign is uniform across particles and
177 // delta1 > 0 always, so we select the trig factors branch-free.
178 bool const focusing = m_g > 0_prt;
179 // raw off-diagonal trig: oscillatory (sin) for the focusing plane,
180 // hyperbolic (sinh) for the defocusing plane (delta1 > 0 always).
181 T_Real const sx = focusing ? sin(omega * m_slice_ds) : sinh(omega * m_slice_ds);
182 T_Real const sy = focusing ? sinh(omega * m_slice_ds) : sin(omega * m_slice_ds);
183 // sign on the px,py kick: -1 (x focusing) / +1 (x defocusing)
184 T_Real const ax = focusing ? -1_prt : 1_prt;
185
186 // per-particle 2x2 transfer-map blocks. omega is chromatic (it depends
187 // on delta1), so unlike our linear Quad these cannot be cached in compute_constants.
188 T_Real const w = omega * delta1;
189 T_Real const R11 = focusing ? cos(omega * m_slice_ds) : cosh(omega * m_slice_ds);
190 T_Real const R12 = sx / w;
191 T_Real const R21 = ax * w * sx;
192 T_Real const R22 = R11;
193 T_Real const R33 = focusing ? cosh(omega * m_slice_ds) : cos(omega * m_slice_ds);
194 T_Real const R34 = sy / w;
195 T_Real const R43 = -ax * w * sy;
196 T_Real const R44 = R33;
197
198 x = R11 * xout + R12 * px;
199 pxout = R21 * xout + R22 * px;
200 y = R33 * yout + R34 * py;
201 pyout = R43 * yout + R44 * py;
202
203 // For the longitudinal update below, (q1,p1) is the oscillatory
204 // plane and (q2,p2) the hyperbolic plane. For focusing that is
205 // (x,px) and (y,py), for defocusing the planes swap.
206 T_Real const q1 = focusing ? xout : yout;
207 T_Real const q2 = focusing ? yout : xout;
208 T_Real const p1 = focusing ? px : py;
209 T_Real const p2 = focusing ? py : px;
210
211 // the corresponding symplectic update to t (nonzero strength)
212 T_Real const term = pt + delta / m_beta;
213 T_Real const t0 = tout - term * m_slice_ds / delta1;
214
215 T_Real const term1 = -(powi<2>(p2) + powi<2>(q2) * powi<2>(w)) * sinh(2_prt * m_slice_ds * omega);
216 T_Real const term2 = -(powi<2>(p1) - powi<2>(q1) * powi<2>(w)) * sin(2_prt * m_slice_ds * omega);
217 T_Real const term3 = -2_prt * q2 * p2 * w * cosh(2_prt * m_slice_ds * omega);
218 T_Real const term4 = -2_prt * q1 * p1 * w * cos(2_prt * m_slice_ds * omega);
219 T_Real const term5 = 2_prt * omega * (
220 q1 * p1 * delta1 + q2 * p2 * delta1
221 -(powi<2>(p1) + powi<2>(p2)) * m_slice_ds
222 -(powi<2>(q1) - powi<2>(q2)) * powi<2>(w) * m_slice_ds
223 );
224 t = t0 + (-1_prt + m_beta * pt)
225 / (8_prt * m_beta * powi<3>(delta1) * omega)
226 * (term1 + term2 + term3 + term4 + term5);
227 // ptout = pt;
228
229 } else // drift
230 {
231 // advance transverse position and momentum (zero focusing strength = drift)
232 x = xout + m_slice_ds * px / delta1;
233 // pxout = px;
234 y = yout + m_slice_ds * py / delta1;
235 // pyout = py;
236
237 // advance longitudinal position and momentum
238 // the corresponding symplectic update to t (zero strength = drift)
239 T_Real term = 2_prt * powi<2>(pt) + powi<2>(px) + powi<2>(py);
240 term = 2_prt - 4_prt * m_beta * pt + powi<2>(m_beta) * term;
241 term = -2_prt + powi<2>(refpart.gamma()) * term;
242 term = (-1_prt + m_beta * pt) * term;
243 term = term * m_const1;
244 t = tout - m_slice_ds * (1_prt / m_beta + term / powi<3>(delta1));
245 // ptout = pt;
246 }
247
248 // assign updated momenta
249 px = pxout;
250 py = pyout;
251 // pt = ptout;
252 }
253
259 void operator() (RefPart & AMREX_RESTRICT refpart) const
260 {
261 using namespace amrex::literals; // for _rt and _prt
262 using amrex::Math::powi;
263
264 // assign input reference particle values
265 amrex::ParticleReal const x = refpart.x;
266 amrex::ParticleReal const px = refpart.px;
267 amrex::ParticleReal const y = refpart.y;
268 amrex::ParticleReal const py = refpart.py;
269 amrex::ParticleReal const z = refpart.z;
270 amrex::ParticleReal const pz = refpart.pz;
271 amrex::ParticleReal const t = refpart.t;
272 amrex::ParticleReal const pt = refpart.pt;
273 amrex::ParticleReal const s = refpart.s;
274
275 // length of the current slice
276 amrex::ParticleReal const slice_ds = m_ds / nslice();
277
278 // assign intermediate parameter
279 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
280
281 // advance position and momentum (straight element)
282 refpart.x = x + step * px;
283 refpart.y = y + step * py;
284 refpart.z = z + step * pz;
285 refpart.t = t - step * pt;
286
287 // advance integrated path length
288 refpart.s = s + slice_ds;
289 }
290
305 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
308 T_Real & AMREX_RESTRICT x,
309 T_Real & AMREX_RESTRICT y,
310 T_Real & AMREX_RESTRICT t,
311 T_Real & AMREX_RESTRICT px,
312 T_Real & AMREX_RESTRICT py,
313 T_Real const & AMREX_RESTRICT pt,
314 T_Real & AMREX_RESTRICT sx,
315 T_Real & AMREX_RESTRICT sy,
316 T_Real & AMREX_RESTRICT sz,
317 T_IdCpu const & AMREX_RESTRICT idcpu,
318 RefPart const & AMREX_RESTRICT refpart
319 ) const
320 {
321 using namespace amrex::literals; // for _rt and _prt
322 using amrex::Math::powi;
323 using namespace std; // for cmath(float)
324
325 // compute particle momentum deviation delta + 1
326 T_Real const delta1 = sqrt(1_prt - 2_prt * pt / m_beta + powi<2>(pt));
327 T_Real const inv_delta1 = 1_prt / delta1;
328
329 // compute particle relativistic gamma
330 T_Real const gamma = refpart.gamma() * (1_prt - m_beta * pt);
331 T_Real const gyro_const = 1_prt + refpart.gyromagnetic_anomaly * gamma;
332
333 // compute phase advance per unit length in s (in rad/m)
334 // chromatic dependence on delta is included
335 T_Real const omega = sqrt(abs(m_g) * inv_delta1);
336 T_Real const omega_ds = omega * m_slice_ds;
337
338 // compute trigonometric quantities
339 auto const [sin_omega_ds, cos_omega_ds] = amrex::Math::sincos(omega_ds);
340 T_Real const sinh_omega_ds = sinh(omega_ds);
341 T_Real const cosh_omega_ds = cosh(omega_ds);
342
343 // The focusing/defocusing/drift cases differ only by which plane is
344 // oscillatory vs. hyperbolic and by an overall sign. omega is real
345 // (delta1 > 0 always) and the m_g sign is uniform across particles,
346 // so we select the per-plane factors branch-free.
347 // For m_g == 0, omega == 0 makes all of (1-cos),
348 // sin, (cosh-1), sinh vanish, so the generator is identically zero
349 // (drift) with no division by omega anywhere.
350 bool const focusing = m_g > 0.0_prt;
351 // "1 - cos"-type factors multiplying p*inv_delta1, per plane
352 T_Real const fac_x = focusing ? (1_prt - cos_omega_ds) : (cosh_omega_ds - 1_prt);
353 T_Real const fac_y = focusing ? (cosh_omega_ds - 1_prt) : (1_prt - cos_omega_ds);
354 // raw "sin"-type trig multiplying q*omega, per plane
355 T_Real const sin_x = focusing ? sin_omega_ds : sinh_omega_ds;
356 T_Real const sin_y = focusing ? sinh_omega_ds : sin_omega_ds;
357 // overall sign: -gyro for focusing, +gyro for defocusing/drift
358 T_Real const sgn = focusing ? -gyro_const : gyro_const;
359
360 T_Real const lambdax = sgn * (py * inv_delta1 * fac_y + y * omega * sin_y);
361 T_Real const lambday = sgn * (px * inv_delta1 * fac_x + x * omega * sin_x);
362 T_Real const lambdaz = 0_prt;
363
364 // push the spin vector using the generator just determined
365 rotate_spin(lambdax, lambday, lambdaz, sx, sy, sz);
366
367 // phase space push
368 (*this)(x, y, t, px, py, pt, idcpu, refpart);
369 }
370
372 using LinearTransport::operator();
373
380 Map6x6
381 transport_map (RefPart const & AMREX_RESTRICT refpart) const
382 {
383 using namespace amrex::literals; // for _rt and _prt
384 using amrex::Math::powi;
385
386 // length of the current slice
387 amrex::ParticleReal const slice_ds = m_ds / nslice();
388
389 // access reference particle values to find beta*gamma^2
390 amrex::ParticleReal const pt_ref = refpart.pt;
391 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1_prt;
392
393 // normalize quad units to MAD-X convention if needed
394 amrex::ParticleReal const g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
395
396 // compute phase advance per unit length in s (in rad/m)
397 amrex::ParticleReal const omega = std::sqrt(std::abs(g));
398
399 // initialize linear map matrix elements
401
402 if (g > 0.0) {
403 R(1,1) = std::cos(omega*slice_ds);
404 R(1,2) = std::sin(omega*slice_ds)/omega;
405 R(2,1) = -omega*std::sin(omega*slice_ds);
406 R(2,2) = std::cos(omega*slice_ds);
407 R(3,3) = std::cosh(omega*slice_ds);
408 R(3,4) = std::sinh(omega*slice_ds)/omega;
409 R(4,3) = omega*std::sinh(omega*slice_ds);
410 R(4,4) = std::cosh(omega*slice_ds);
411 R(5,6) = slice_ds/betgam2;
412 } else if (g < 0.0) {
413 R(1,1) = std::cosh(omega*slice_ds);
414 R(1,2) = std::sinh(omega*slice_ds)/omega;
415 R(2,1) = omega*std::sinh(omega*slice_ds);
416 R(2,2) = std::cosh(omega*slice_ds);
417 R(3,3) = std::cos(omega*slice_ds);
418 R(3,4) = std::sin(omega*slice_ds)/omega;
419 R(4,3) = -omega*std::sin(omega*slice_ds);
420 R(4,4) = std::cos(omega*slice_ds);
421 R(5,6) = slice_ds/betgam2;
422 } else {
423 R(1,2) = m_slice_ds;
424 R(3,4) = m_slice_ds;
425 R(5,6) = m_slice_ds / betgam2;
426 }
427
428 // apply the transverse rotation (roll) alignment error
429 return rotate_aligned_map(R);
430 }
431
433 int m_unit;
434
435 private:
436 // constants that are independent of the individually tracked particle,
437 // see: compute_constants() to refresh
442 };
443
444} // namespace impactx
445
447
448#endif // IMPACTX_CHRQUAD_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
#define IMPACTX_PUSH_EXTERN_TEMPLATE(ElementType)
Definition PushAll.H:78
amrex_particle_real ParticleReal
constexpr T powi(T x) noexcept
__host__ __device__ std::pair< double, double > sincos(double x)
__host__ __device__ T abs(const GpuComplex< T > &a_z) noexcept
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Definition All.H:55
@ s
fixed s as the independent variable
Definition ImpactXParticleContainer.H:37
@ t
fixed t as the independent variable
Definition ImpactXParticleContainer.H:38
amrex::SmallMatrix< amrex::ParticleReal, 6, 6, amrex::Order::F, 1 > Map6x6
Definition CovarianceMatrix.H:20
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition ReferenceParticle.H:260
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:151
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:139
Definition ChrQuad.H:43
int m_unit
quadrupole strength in 1/m^2 (or T/m)
Definition ChrQuad.H:433
amrex::ParticleReal m_const1
Definition ChrQuad.H:441
amrex::ParticleReal m_slice_ds
unit specification for quad strength
Definition ChrQuad.H:438
ImpactXParticleContainer::ParticleType PType
Definition ChrQuad.H:45
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ChrQuad.H:381
void compute_constants(RefPart const &refpart)
Definition ChrQuad.H:104
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT t, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ChrQuad.H:137
static constexpr int DEFAULT_unit
Definition ChrQuad.H:47
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void spin_and_phasespace_push(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT t, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ChrQuad.H:307
amrex::ParticleReal m_k
Definition ChrQuad.H:432
static constexpr auto type
Definition ChrQuad.H:44
ChrQuad(amrex::ParticleReal ds, amrex::ParticleReal k, int unit=DEFAULT_unit, amrex::ParticleReal dx=DEFAULT_dx, amrex::ParticleReal dy=DEFAULT_dy, amrex::ParticleReal rotation_degree=DEFAULT_rotation_degree, amrex::ParticleReal aperture_x=DEFAULT_aperture_x, amrex::ParticleReal aperture_y=DEFAULT_aperture_y, int nslice=DEFAULT_nslice, std::optional< std::string > name=DEFAULT_name)
Definition ChrQuad.H:71
void reverse()
Definition ChrQuad.H:92
amrex::ParticleReal m_beta
m_ds / nslice();
Definition ChrQuad.H:439
amrex::ParticleReal m_g
Definition ChrQuad.H:440
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:193
static constexpr amrex::ParticleReal DEFAULT_dy
Definition alignment.H:34
static constexpr amrex::ParticleReal DEFAULT_dx
Definition alignment.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:183
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:267
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:43
static constexpr amrex::ParticleReal DEFAULT_rotation_degree
Definition alignment.H:35
Definition beamoptic.H:567
Definition lineartransport.H:50
Definition named.H:29
static constexpr std::nullopt_t DEFAULT_name
Definition named.H:30
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:59
AMREX_FORCE_INLINE std::string name() const
Definition named.H:124
Definition nofinalize.H:22
Definition pipeaperture.H:26
static constexpr amrex::ParticleReal DEFAULT_aperture_x
Definition pipeaperture.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_x() const
Definition pipeaperture.H:93
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_y() const
Definition pipeaperture.H:104
static constexpr amrex::ParticleReal DEFAULT_aperture_y
Definition pipeaperture.H:28
PipeAperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y)
Definition pipeaperture.H:35
Definition spintransport.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void rotate_spin(T_Real const &AMREX_RESTRICT lambdax, T_Real const &AMREX_RESTRICT lambday, T_Real const &AMREX_RESTRICT lambdaz, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz) const
Definition spintransport.H:48
Definition thick.H:24
static constexpr int DEFAULT_nslice
Definition thick.H:25
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:32
amrex::ParticleReal m_ds
Definition thick.H:70
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal ds() const
Definition thick.H:55
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int nslice() const
Definition thick.H:45