ImpactX
Loading...
Searching...
No Matches
Quad.H
Go to the documentation of this file.
1/* Copyright 2022-2023 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
30
31namespace impactx::elements
32{
33 struct Quad
34 : public mixin::Named,
35 public mixin::BeamOptic<Quad>,
36 public mixin::LinearTransport<Quad>,
37 public mixin::Thick,
38 public mixin::Alignment,
42 // At least on Intel AVX512, there is a small overhead to vectorize this element for DP and a gain for SP, see
43 // https://github.com/BLAST-ImpactX/impactx/pull/1002
44#ifdef AMREX_SINGLE_PRECISION_PARTICLES
45 , public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
46#endif
47 {
48 static constexpr auto type = "Quad";
50
71 amrex::ParticleReal rotation_degree = 0,
74 int nslice = 1,
75 std::optional<std::string> name = std::nullopt
76 )
77 : Named(std::move(name)),
78 Thick(ds, nslice),
79 Alignment(dx, dy, rotation_degree),
81 m_k(k)
82 {
83 }
84
86 void reverse () { Thick::reverse(); }
87
89 using BeamOptic::operator();
90
98 void compute_constants (RefPart const & refpart)
99 {
100 using namespace amrex::literals; // for _rt and _prt
101 using amrex::Math::powi;
102
103 Alignment::compute_constants(refpart);
104
105 // length of the current slice
106 m_slice_ds = m_ds / nslice();
107
108 amrex::ParticleReal const pt_ref = refpart.pt;
109 // find beta*gamma^2
110 m_betgam2 = powi<2>(pt_ref) - 1.0_prt;
112
113 // compute phase advance per unit length in s (in rad/m)
114 m_omega = std::sqrt(std::abs(m_k));
119
120 // gyromagnetic constant (used during spin push)
122 m_gyro_const = 1_prt + G * refpart.gamma();
123 }
124
138 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
141 T_Real & AMREX_RESTRICT x,
142 T_Real & AMREX_RESTRICT y,
143 T_Real & AMREX_RESTRICT t,
144 T_Real & AMREX_RESTRICT px,
145 T_Real & AMREX_RESTRICT py,
146 T_Real & AMREX_RESTRICT pt,
147 T_IdCpu & AMREX_RESTRICT idcpu,
148 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
149 ) const
150 {
151 using namespace amrex::literals; // for _rt and _prt
152
153 // shift due to alignment errors of the element
154 shift_in(x, y, px, py);
155
156 // initialize output values
157 T_Real xout = x;
158 T_Real yout = y;
159 T_Real tout = t;
160 T_Real pxout = px;
161 T_Real pyout = py;
162 T_Real const ptout = pt;
163
164 if (m_k > 0.0_prt)
165 {
166 // advance position and momentum (focusing quad)
168 pxout = -m_omega*m_sin_omega_ds*x + m_cos_omega_ds*px;
169
172
173 tout = t + (m_slice_bg)*pt;
174 // ptout = pt;
175 } else if (m_k < 0.0_prt)
176 {
177 // advance position and momentum (defocusing quad)
180
182 pyout = -m_omega*m_sin_omega_ds*y + m_cos_omega_ds*py;
183
184 tout = t + m_slice_bg*pt;
185 // ptout = pt;
186 } else {
187 // advance position and momentum (zero strength = drift)
188 xout = x + m_slice_ds * px;
189 // pxout = px;
190 yout = y + m_slice_ds * py;
191 // pyout = py;
192 tout = t + m_slice_bg * pt;
193 // ptout = pt;
194 }
195
196 // assign updated values
197 x = xout;
198 y = yout;
199 t = tout;
200 px = pxout;
201 py = pyout;
202 pt = ptout;
203
204 // apply transverse aperture
205 apply_aperture(x, y, idcpu);
206
207 // undo shift due to alignment errors of the element
208 shift_out(x, y, px, py);
209 }
210
216 void operator() (RefPart & AMREX_RESTRICT refpart) const { // TODO: update as well, but needs more careful placement of calc_constants
217
218 using namespace amrex::literals; // for _rt and _prt
219 using amrex::Math::powi;
220
221 // assign input reference particle values
222 amrex::ParticleReal const x = refpart.x;
223 amrex::ParticleReal const px = refpart.px;
224 amrex::ParticleReal const y = refpart.y;
225 amrex::ParticleReal const py = refpart.py;
226 amrex::ParticleReal const z = refpart.z;
227 amrex::ParticleReal const pz = refpart.pz;
228 amrex::ParticleReal const t = refpart.t;
229 amrex::ParticleReal const pt = refpart.pt;
230 amrex::ParticleReal const s = refpart.s;
231
232 // length of the current slice
233 amrex::ParticleReal const slice_ds = m_ds / nslice();
234
235 // assign intermediate parameter
236 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt)-1.0_prt);
237
238 // advance position and momentum (straight element)
239 refpart.x = x + step*px;
240 refpart.y = y + step*py;
241 refpart.z = z + step*pz;
242 refpart.t = t - step*pt;
243
244 // advance integrated path length
245 refpart.s = s + slice_ds;
246 }
247
253 Map6x6
254 transport_map (RefPart const & AMREX_RESTRICT refpart) const // TODO: update as well, but needs more careful placement of calc_constants
255 {
256 using namespace amrex::literals; // for _rt and _prt
257 using amrex::Math::powi;
258
259 // length of the current slice
260 amrex::ParticleReal const slice_ds = m_ds / nslice();
261
262 // access reference particle values to find beta*gamma^2
263 amrex::ParticleReal const pt_ref = refpart.pt;
264 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1.0_prt;
265
266 // compute phase advance per unit length in s (in rad/m)
267 amrex::ParticleReal const omega = std::sqrt(std::abs(m_k));
268
269 // initialize linear map matrix elements
271
272 if (m_k > 0.0) {
273 R(1,1) = std::cos(omega*slice_ds);
274 R(1,2) = std::sin(omega*slice_ds)/omega;
275 R(2,1) = -omega*std::sin(omega*slice_ds);
276 R(2,2) = std::cos(omega*slice_ds);
277 R(3,3) = std::cosh(omega*slice_ds);
278 R(3,4) = std::sinh(omega*slice_ds)/omega;
279 R(4,3) = omega*std::sinh(omega*slice_ds);
280 R(4,4) = std::cosh(omega*slice_ds);
281 R(5,6) = slice_ds/betgam2;
282 } else if (m_k < 0.0) {
283 R(1,1) = std::cosh(omega*slice_ds);
284 R(1,2) = std::sinh(omega*slice_ds)/omega;
285 R(2,1) = omega*std::sinh(omega*slice_ds);
286 R(2,2) = std::cosh(omega*slice_ds);
287 R(3,3) = std::cos(omega*slice_ds);
288 R(3,4) = std::sin(omega*slice_ds)/omega;
289 R(4,3) = -omega*std::sin(omega*slice_ds);
290 R(4,4) = std::cos(omega*slice_ds);
291 R(5,6) = slice_ds/betgam2;
292 } else {
293 R(1,2) = slice_ds;
294 R(3,4) = slice_ds;
295 R(5,6) = slice_ds / betgam2;
296 }
297
298 return R;
299 }
300
315 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
318 [[maybe_unused]] T_Real & AMREX_RESTRICT x,
319 [[maybe_unused]] T_Real & AMREX_RESTRICT y,
320 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
321 [[maybe_unused]] T_Real & AMREX_RESTRICT px,
322 [[maybe_unused]] T_Real & AMREX_RESTRICT py,
323 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
324 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
325 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
326 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
327 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
328 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
329 ) const
330 {
331 using namespace amrex::literals; // for _rt and _prt
332
333 // initialize the three components of the axis-angle vector
334 T_Real lambdax = 0_prt;
335 T_Real lambday = 0_prt;
336 T_Real lambdaz = 0_prt;
337
338 if (m_k > 0.0_prt)
339 {
340
341 // horizontally focusing quad case
342 lambdax = -m_gyro_const * ( py*(m_cosh_omega_ds - 1_prt) + y*m_omega*m_sinh_omega_ds );
343 lambday = -m_gyro_const * ( px*(1_prt - m_cos_omega_ds) + x*m_omega*m_sin_omega_ds );
344
345 } else if (m_k < 0.0_prt)
346 {
347
348 // horizontally defocusing quad case
349 lambdax = m_gyro_const * ( py*(1_prt - m_cos_omega_ds) + y*m_omega*m_sin_omega_ds );
350 lambday = m_gyro_const * ( px*(m_cosh_omega_ds - 1_prt) + x*m_omega*m_sinh_omega_ds );
351
352 } else {
353 // treat as a drift
354 }
355
356 // push the spin vector using the generator just determined
357 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
358
359 // phase space push
360 (*this)(x, y, t, px, py, pt, idcpu, refpart);
361 }
362
363
365 using LinearTransport::operator();
366
368
369 private:
370 // constants that are independent of the individually tracked particle,
371 // see: compute_constants() to refresh
381
382 };
383
384} // namespace impactx
385
387
388#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:56
@ 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:140
Definition Quad.H:47
amrex::ParticleReal m_cosh_omega_ds
std::sinh(omega*slice_ds)
Definition Quad.H:379
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 &AMREX_RESTRICT pt, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Quad.H:317
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 &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Quad.H:140
amrex::ParticleReal m_k
Definition Quad.H:367
static constexpr auto type
Definition Quad.H:48
amrex::ParticleReal m_slice_ds
quadrupole strength in 1/m
Definition Quad.H:372
amrex::ParticleReal m_cos_omega_ds
std::sin(omega*slice_ds)
Definition Quad.H:377
amrex::ParticleReal m_gyro_const
std::cosh(omega*slice_ds)
Definition Quad.H:380
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Quad.H:254
void compute_constants(RefPart const &refpart)
Definition Quad.H:98
amrex::ParticleReal m_slice_bg
beta*gamma^2
Definition Quad.H:374
ImpactXParticleContainer::ParticleType PType
Definition Quad.H:49
amrex::ParticleReal m_sin_omega_ds
std::sqrt(std::abs(m_k)) compute phase advance per unit length in s (in rad/m)
Definition Quad.H:376
void reverse()
Definition Quad.H:86
Quad(amrex::ParticleReal ds, amrex::ParticleReal k, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, amrex::ParticleReal aperture_x=0, amrex::ParticleReal aperture_y=0, int nslice=1, std::optional< std::string > name=std::nullopt)
Definition Quad.H:66
amrex::ParticleReal m_sinh_omega_ds
std::cos(omega*slice_ds)
Definition Quad.H:378
amrex::ParticleReal m_betgam2
m_ds / nslice();
Definition Quad.H:373
amrex::ParticleReal m_omega
m_slice_ds / m_betgam2
Definition Quad.H:375
Definition alignment.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_out(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:109
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:146
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:136
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_in(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:78
Definition beamoptic.H:436
Definition lineartransport.H:50
Definition named.H:29
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:57
AMREX_FORCE_INLINE std::string name() const
Definition named.H:122
Definition nofinalize.H:22
Definition pipeaperture.H:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void apply_aperture(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_IdCpu &AMREX_RESTRICT idcpu) const
Definition pipeaperture.H:59
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_x() const
Definition pipeaperture.H:90
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_y() const
Definition pipeaperture.H:101
PipeAperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y)
Definition pipeaperture.H:32
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
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:30
amrex::ParticleReal m_ds
Definition thick.H:68
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal ds() const
Definition thick.H:53
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int nslice() const
Definition thick.H:43