ImpactX
Loading...
Searching...
No Matches
Quad.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, Kyrre Ness Sjobak
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_QUAD_H
11#define IMPACTX_QUAD_H
12
14#include "mixin/alignment.H"
15#include "mixin/pipeaperture.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thick.H"
18#include "mixin/named.H"
19#include "mixin/nofinalize.H"
21#include "mixin/spintransport.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
30namespace impactx::elements
31{
32 struct Quad
33 : public mixin::Named,
34 public mixin::BeamOptic<Quad>,
35 public mixin::LinearTransport<Quad>,
36 public mixin::Thick,
37 public mixin::Alignment,
40 public mixin::NoFinalize,
41 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
42 {
43 static constexpr auto type = "Quad";
45
46 // The light no-spin push in double precision does not benefit from SIMD,
47 // see https://github.com/BLAST-ImpactX/impactx/pull/1002
48 // This overwrites in mixin::BeamOptic the amrex::simd::Vectorized flag.
49#ifndef AMREX_SINGLE_PRECISION_PARTICLES
50 static constexpr bool simd_ps = false;
51#endif
52
86
88 void reverse () { Thick::reverse(); }
89
91 using BeamOptic::operator();
92
100 void compute_constants (RefPart const & refpart)
101 {
102 using namespace amrex::literals; // for _rt and _prt
103 using amrex::Math::powi;
104
105 Alignment::compute_constants(refpart);
106
107 // length of the current slice
108 m_slice_ds = m_ds / nslice();
109
110 amrex::ParticleReal const pt_ref = refpart.pt;
111 // find beta*gamma^2
112 m_betgam2 = powi<2>(pt_ref) - 1.0_prt;
114
115 // gyromagnetic constant (used during spin push)
117 m_gyro_const = 1_prt + G * refpart.gamma();
118
119 // Resolve the focusing/defocusing/drift branch here, once per
120 // element, so the per-particle operator() and spin push are
121 // branch-free. omega = sqrt(|k|) is independent of the tracked
122 // particle, so all trigonometric quantities can be cached.
123 // The transverse 6x6 transfer-map blocks are:
124 // xout = m_R11*x + m_R12*px ; pxout = m_R21*x + m_R22*px
125 // yout = m_R33*y + m_R34*py ; pyout = m_R43*y + m_R44*py
126 // and the spin generator is:
127 // lambdax = m_spin_py*py + m_spin_y*y
128 // lambday = m_spin_px*px + m_spin_x*x
129 amrex::ParticleReal const omega = std::sqrt(std::abs(m_k));
130 amrex::ParticleReal const sin_omega_ds = std::sin(omega * m_slice_ds);
131 amrex::ParticleReal const cos_omega_ds = std::cos(omega * m_slice_ds);
132 amrex::ParticleReal const sinh_omega_ds = std::sinh(omega * m_slice_ds);
133 amrex::ParticleReal const cosh_omega_ds = std::cosh(omega * m_slice_ds);
134
135 if (m_k > 0.0_prt)
136 {
137 // horizontally focusing quad (x focusing, y defocusing)
138 m_R11 = cos_omega_ds;
139 m_R12 = sin_omega_ds / omega;
140 m_R21 = -omega * sin_omega_ds;
141 m_R22 = cos_omega_ds;
142 m_R33 = cosh_omega_ds;
143 m_R34 = sinh_omega_ds / omega;
144 m_R43 = omega * sinh_omega_ds;
145 m_R44 = cosh_omega_ds;
146
147 m_spin_py = -m_gyro_const * (cosh_omega_ds - 1_prt);
148 m_spin_y = -m_gyro_const * omega * sinh_omega_ds;
149 m_spin_px = -m_gyro_const * (1_prt - cos_omega_ds);
150 m_spin_x = -m_gyro_const * omega * sin_omega_ds;
151 }
152 else if (m_k < 0.0_prt)
153 {
154 // horizontally defocusing quad (x defocusing, y focusing)
155 m_R11 = cosh_omega_ds;
156 m_R12 = sinh_omega_ds / omega;
157 m_R21 = omega * sinh_omega_ds;
158 m_R22 = cosh_omega_ds;
159 m_R33 = cos_omega_ds;
160 m_R34 = sin_omega_ds / omega;
161 m_R43 = -omega * sin_omega_ds;
162 m_R44 = cos_omega_ds;
163
164 m_spin_py = m_gyro_const * (1_prt - cos_omega_ds);
165 m_spin_y = m_gyro_const * omega * sin_omega_ds;
166 m_spin_px = m_gyro_const * (cosh_omega_ds - 1_prt);
167 m_spin_x = m_gyro_const * omega * sinh_omega_ds;
168 }
169 else
170 {
171 // zero strength = drift
172 m_R11 = 1.0_prt;
174 m_R21 = 0.0_prt;
175 m_R22 = 1.0_prt;
176 m_R33 = 1.0_prt;
178 m_R43 = 0.0_prt;
179 m_R44 = 1.0_prt;
180
181 m_spin_py = 0.0_prt;
182 m_spin_y = 0.0_prt;
183 m_spin_px = 0.0_prt;
184 m_spin_x = 0.0_prt;
185 }
186 }
187
201 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
204 T_Real & AMREX_RESTRICT x,
205 T_Real & AMREX_RESTRICT y,
206 T_Real & AMREX_RESTRICT t,
207 T_Real & AMREX_RESTRICT px,
208 T_Real & AMREX_RESTRICT py,
209 T_Real const & AMREX_RESTRICT pt,
210 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
211 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
212 ) const
213 {
214 using namespace amrex::literals; // for _rt and _prt
215
216 // copies of original input values
217 T_Real const xin = x;
218 T_Real const yin = y;
219
220 // advance position and momentum
221 x = m_R11 * xin + m_R12 * px;
222 px = m_R21 * xin + m_R22 * px;
223
224 y = m_R33 * yin + m_R34 * py;
225 py = m_R43 * yin + m_R44 * py;
226
227 // R56 = m_slice_bg ; R55 = R66 = 1
228 t = t + m_slice_bg * pt;
229 // pt unchanged
230 }
231
237 void operator() (RefPart & AMREX_RESTRICT refpart) const
238 {
239 using namespace amrex::literals; // for _rt and _prt
240 using amrex::Math::powi;
241
242 // assign input reference particle values
243 amrex::ParticleReal const x = refpart.x;
244 amrex::ParticleReal const px = refpart.px;
245 amrex::ParticleReal const y = refpart.y;
246 amrex::ParticleReal const py = refpart.py;
247 amrex::ParticleReal const z = refpart.z;
248 amrex::ParticleReal const pz = refpart.pz;
249 amrex::ParticleReal const t = refpart.t;
250 amrex::ParticleReal const pt = refpart.pt;
251 amrex::ParticleReal const s = refpart.s;
252
253 // length of the current slice
254 amrex::ParticleReal const slice_ds = m_ds / nslice();
255
256 // assign intermediate parameter
257 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
258
259 // advance position and momentum (straight element)
260 refpart.x = x + step*px;
261 refpart.y = y + step*py;
262 refpart.z = z + step*pz;
263 refpart.t = t - step*pt;
264
265 // advance integrated path length
266 refpart.s = s + slice_ds;
267 }
268
274 Map6x6
275 transport_map (RefPart const & AMREX_RESTRICT refpart) const
276 {
277 using namespace amrex::literals; // for _rt and _prt
278 using amrex::Math::powi;
279
280 // length of the current slice
281 amrex::ParticleReal const slice_ds = m_ds / nslice();
282
283 // access reference particle values to find beta*gamma^2
284 amrex::ParticleReal const pt_ref = refpart.pt;
285 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1.0_prt;
286
287 // compute phase advance per unit length in s (in rad/m)
288 amrex::ParticleReal const omega = std::sqrt(std::abs(m_k));
289
290 // initialize linear map matrix elements
292
293 if (m_k > 0.0) {
294 R(1,1) = std::cos(omega*slice_ds);
295 R(1,2) = std::sin(omega*slice_ds)/omega;
296 R(2,1) = -omega*std::sin(omega*slice_ds);
297 R(2,2) = std::cos(omega*slice_ds);
298 R(3,3) = std::cosh(omega*slice_ds);
299 R(3,4) = std::sinh(omega*slice_ds)/omega;
300 R(4,3) = omega*std::sinh(omega*slice_ds);
301 R(4,4) = std::cosh(omega*slice_ds);
302 R(5,6) = slice_ds/betgam2;
303 } else if (m_k < 0.0) {
304 R(1,1) = std::cosh(omega*slice_ds);
305 R(1,2) = std::sinh(omega*slice_ds)/omega;
306 R(2,1) = omega*std::sinh(omega*slice_ds);
307 R(2,2) = std::cosh(omega*slice_ds);
308 R(3,3) = std::cos(omega*slice_ds);
309 R(3,4) = std::sin(omega*slice_ds)/omega;
310 R(4,3) = -omega*std::sin(omega*slice_ds);
311 R(4,4) = std::cos(omega*slice_ds);
312 R(5,6) = slice_ds/betgam2;
313 } else {
314 R(1,2) = slice_ds;
315 R(3,4) = slice_ds;
316 R(5,6) = slice_ds / betgam2;
317 }
318
319 // apply the transverse rotation (roll) alignment error
320 return rotate_aligned_map(R);
321 }
322
337 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
340 T_Real & AMREX_RESTRICT x,
341 T_Real & AMREX_RESTRICT y,
342 T_Real & AMREX_RESTRICT t,
343 T_Real & AMREX_RESTRICT px,
344 T_Real & AMREX_RESTRICT py,
345 T_Real const & AMREX_RESTRICT pt,
346 T_Real & AMREX_RESTRICT sx,
347 T_Real & AMREX_RESTRICT sy,
348 T_Real & AMREX_RESTRICT sz,
349 T_IdCpu const & AMREX_RESTRICT idcpu,
350 RefPart const & AMREX_RESTRICT refpart
351 ) const
352 {
353 using namespace amrex::literals; // for _rt and _prt
354
355 // the axis-angle vector
356 // The focusing/defocusing/drift selection is fully resolved in
357 // compute_constants(), see the m_spin_* coefficients there.
358 T_Real const lambdax = m_spin_py * py + m_spin_y * y;
359 T_Real const lambday = m_spin_px * px + m_spin_x * x;
360 T_Real const lambdaz = 0_prt;
361
362 // push the spin vector using the generator just determined
363 rotate_spin(lambdax, lambday, lambdaz, sx, sy, sz);
364
365 // phase space push
366 (*this)(x, y, t, px, py, pt, idcpu, refpart);
367 }
368
370 using LinearTransport::operator();
371
373
374 private:
375 // constants that are independent of the individually tracked particle,
376 // see: compute_constants() to refresh
381 // pre-resolved 6x6 transfer-map elements (transverse blocks), see compute_constants()
384 // branch-free spin generator coefficients, see compute_constants()
389
390 };
391
392} // namespace impactx
393
395
396#endif // IMPACTX_QUAD_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
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::ParticleReal pt
energy, normalized by rest energy
Definition ReferenceParticle.H:42
amrex::ParticleReal gyromagnetic_anomaly
anomalous magnetic moment [unitless]
Definition ReferenceParticle.H:45
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:139
Definition Quad.H:42
amrex::ParticleReal m_k
Definition Quad.H:372
amrex::ParticleReal m_R43
Definition Quad.H:383
static constexpr auto type
Definition Quad.H:43
amrex::ParticleReal m_spin_y
lambdax += m_spin_py*py
Definition Quad.H:388
static constexpr bool simd_ps
Definition Quad.H:50
amrex::ParticleReal m_slice_ds
quadrupole strength in 1/m
Definition Quad.H:377
amrex::ParticleReal m_spin_x
lambday += m_spin_px*px
Definition Quad.H:386
amrex::ParticleReal m_R34
Definition Quad.H:383
amrex::ParticleReal m_gyro_const
R56 = m_slice_ds / m_betgam2.
Definition Quad.H:380
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Quad.H:275
void compute_constants(RefPart const &refpart)
Definition Quad.H:100
amrex::ParticleReal m_slice_bg
beta*gamma^2
Definition Quad.H:379
ImpactXParticleContainer::ParticleType PType
Definition Quad.H:44
amrex::ParticleReal m_R21
Definition Quad.H:382
amrex::ParticleReal m_R33
x–px block
Definition Quad.H:383
amrex::ParticleReal m_spin_py
lambday += m_spin_x*x
Definition Quad.H:387
void reverse()
Definition Quad.H:88
amrex::ParticleReal m_R11
(1 + G*gamma) for spin calculation
Definition Quad.H:382
Quad(amrex::ParticleReal ds, amrex::ParticleReal k, 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 Quad.H:68
amrex::ParticleReal m_R44
Definition Quad.H:383
amrex::ParticleReal m_R12
Definition Quad.H:382
amrex::ParticleReal m_R22
Definition Quad.H:382
amrex::ParticleReal m_betgam2
m_ds / nslice();
Definition Quad.H:378
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 Quad.H:339
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 Quad.H:203
amrex::ParticleReal m_spin_px
y–py block
Definition Quad.H:385
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