ImpactX
Loading...
Searching...
No Matches
CFbend.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
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_CFBEND_H
11#define IMPACTX_CFBEND_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
31
32namespace impactx::elements
33{
34 struct CFbend
35 : public mixin::Named,
36 public mixin::BeamOptic<CFbend>,
37 public mixin::LinearTransport<CFbend>,
38 public mixin::Thick,
39 public mixin::Alignment,
43 // At least on Intel AVX512, there is a small overhead to vectorize this element, see
44 // https://github.com/BLAST-ImpactX/impactx/pull/1002
45 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
46 {
47 static constexpr auto type = "CFbend";
49
73 amrex::ParticleReal rotation_degree = 0,
76 int nslice = 1,
77 std::optional<std::string> name = std::nullopt
78 )
79 : Named(std::move(name)),
80 Thick(ds, nslice),
81 Alignment(dx, dy, rotation_degree),
83 m_rc(rc), m_k(k)
84 {
85 }
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 amrex::ParticleReal const slice_ds = m_ds / nslice();
109
110 // find beta*gamma^2, beta
111 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
112 amrex::ParticleReal const bet = refpart.beta();
113 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
114 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
115
116 // update horizontal and longitudinal phase space variables
117 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
118 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
119
120 // update vertical phase space variables
121 amrex::ParticleReal const gy = -m_k;
122 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
123
124 // trigonometry
125 amrex::ParticleReal const cx = gx > 0_prt ? std::cos(omega_x * slice_ds) : std::cosh(omega_x * slice_ds);
126 amrex::ParticleReal const sx = gx > 0_prt ? std::sin(omega_x * slice_ds)/omega_x : std::sinh(omega_x * slice_ds)/omega_x;
127 amrex::ParticleReal const cy = gy > 0_prt ? std::cos(omega_y * slice_ds) : std::cosh(omega_y * slice_ds);
128 amrex::ParticleReal const sy = gy > 0_prt ? std::sin(omega_y * slice_ds)/omega_y : std::sinh(omega_y * slice_ds)/omega_y;
129
130 amrex::ParticleReal const ibrc = 1_prt / (bet * m_rc);
131 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
132 amrex::ParticleReal const igb2r2 = 1_prt / (gx * b2rc2);
133
134 // elements of the linear transport matrix
135 // The matrix elements of R are identical with those found in:
136 // F. C. Iselin, Part. Accel. 17, pp. 143-155 (1985), Section 4,
137 // except the longitudinal variables used here (t,pt) have the opposite sign.
138 m_R11 = cx;
139 m_R12 = sx;
140 m_R16 = -(1_prt - cx) * igbrc;
141 m_R21 = -gx * sx;
142 m_R22 = cx;
143 m_R26 = -sx * ibrc;
144 m_R33 = cy;
145 m_R34 = sy;
146 m_R43 = -gy * sy;
147 m_R44 = cy;
148 m_R51 = -m_R26;
149 m_R52 = -m_R16;
150 m_R56 = slice_ds * ibetgam2 + (sx - slice_ds) * igb2r2;
151
152 // access reference particle values for spin calculation
154 amrex::ParticleReal const gamma = refpart.gamma();
155 amrex::ParticleReal const beta = refpart.beta();
156 amrex::ParticleReal const gyro_const1 = 1_prt + G * gamma;
157 amrex::ParticleReal const gyro_const2 = 1_prt + G * gamma * gamma;
158
159 // trigonometry for spin calculation
160 amrex::ParticleReal const h = (m_rc == 0_prt) ? 0_prt : 1_prt/m_rc;
161 auto const [sinG, cosG] = amrex::Math::sincos(G * h * gamma * slice_ds);
162
163 // intermediate constants for spin calculation
164 amrex::ParticleReal const factor = 1_prt / (-gy + powi<2>(G * h * gamma));
165
166 // nonvanishing components of spin rotation at the design point
167 m_lambday = -G * h * gamma * slice_ds;
168
169 // elements of the spin-orbit coupling matrix
170 m_A13 = (G*h*gyro_const2*gy*cy*sinG + gy*(powi<2>(G*h)*(gamma - 1_prt)*gamma - gy*gyro_const1 )*sy*cosG) * factor;
171 m_A14 = (G*h*gyro_const2*gy*sy*sinG + (powi<2>(G*h)*(gamma - 1_prt)*gamma - gy*gyro_const1)*(1_prt - cosG*cy))*factor;
172 m_A21 = -gyro_const1 * gx * sx;
173 m_A22 = -gyro_const1 * (1_prt - cx);
174 m_A26 = G*h*beta*gamma*slice_ds - h*gyro_const1*sx/(beta);
175 m_A33 = (-(-gy*gyro_const1 + powi<2>(G*h)*gamma*(gamma - 1_prt))*gy*sy*sinG -gy*G*h*gyro_const2*(1_prt - cosG*cy))*factor;
176 m_A34 = ((-gy*gyro_const1 + powi<2>(G*h)*gamma*(gamma - 1_prt)) * cy*sinG + G*h*gyro_const2*gy*cosG*sy)*factor;
177 }
178
192 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
195 T_Real & AMREX_RESTRICT x,
196 T_Real & AMREX_RESTRICT y,
197 T_Real & AMREX_RESTRICT t,
198 T_Real & AMREX_RESTRICT px,
199 T_Real & AMREX_RESTRICT py,
200 T_Real const & AMREX_RESTRICT pt,
201 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
202 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
203 ) const
204 {
205 using namespace amrex::literals; // for _rt and _prt
206
207 // initialize output values
208 T_Real xout = x;
209 T_Real yout = y;
210 T_Real tout = t;
211
212 // initialize output values of momenta
213 T_Real pxout = px;
214 T_Real pyout = py;
215 // T_Real ptout = pt;
216
217 // update horizontal and longitudinal phase space variables
218 // advance position and momentum: (de)focusing
219 x = m_R11 * xout + m_R12 * px + m_R16 * pt;
220 pxout = m_R21 * xout + m_R22 * px + m_R26 * pt;
221 t = m_R51 * xout + m_R52 * px + m_R56 * pt + tout;
222 // ptout = pt;
223
224 // update vertical phase space variables
225 // advance position and momentum (de)focusing
226 y = m_R33 * yout + m_R34 * py;
227 pyout = m_R43 * yout + m_R44 * py;
228
229 // assign updated momenta
230 px = pxout;
231 py = pyout;
232 // pt = ptout;
233 }
234
240 void operator() (RefPart & AMREX_RESTRICT refpart) const
241 {
242 using namespace amrex::literals; // for _rt and _prt
243 using amrex::Math::powi;
244
245 // assign input reference particle values
246 amrex::ParticleReal const x = refpart.x;
247 amrex::ParticleReal const px = refpart.px;
248 amrex::ParticleReal const y = refpart.y;
249 amrex::ParticleReal const py = refpart.py;
250 amrex::ParticleReal const z = refpart.z;
251 amrex::ParticleReal const pz = refpart.pz;
252 amrex::ParticleReal const t = refpart.t;
253 amrex::ParticleReal const pt = refpart.pt;
254 amrex::ParticleReal const s = refpart.s;
255
256 // length of the current slice
257 amrex::ParticleReal const slice_ds = m_ds / nslice();
258
259 // assign intermediate parameter
260 amrex::ParticleReal const theta = slice_ds/m_rc;
261 amrex::ParticleReal const B = std::sqrt(powi<2>(pt)-1.0_prt)/m_rc;
262
263 // calculate expensive terms once
264 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
265
266 // advance position and momentum (bend)
267 refpart.px = px*cos_theta - pz*sin_theta;
268 refpart.py = py;
269 refpart.pz = pz*cos_theta + px*sin_theta;
270 refpart.pt = pt;
271
272 refpart.x = x + (refpart.pz - pz)/B;
273 refpart.y = y + (theta/B)*py;
274 refpart.z = z - (refpart.px - px)/B;
275 refpart.t = t - (theta/B)*pt;
276
277 // advance integrated path length
278 refpart.s = s + slice_ds;
279 }
280
281
296 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
299 T_Real & AMREX_RESTRICT x,
300 T_Real & AMREX_RESTRICT y,
301 T_Real & AMREX_RESTRICT t,
302 T_Real & AMREX_RESTRICT px,
303 T_Real & AMREX_RESTRICT py,
304 T_Real const & AMREX_RESTRICT pt,
305 T_Real & AMREX_RESTRICT sx,
306 T_Real & AMREX_RESTRICT sy,
307 T_Real & AMREX_RESTRICT sz,
308 T_IdCpu const & AMREX_RESTRICT idcpu,
309 RefPart const & AMREX_RESTRICT refpart
310 ) const
311 {
312 using namespace amrex::literals; // for _rt and _prt
313 using amrex::Math::powi;
314
315 // initialize the three components of the axis-angle vector
316 T_Real lambdax = 0_prt;
317 T_Real lambday = 0_prt;
318 T_Real lambdaz = 0_prt;
319
320 // set the angle-axis generator based on the phase space variables
321 lambdax = m_A13 * y + m_A14 * py;
322 lambday = m_A21 * x + m_A22 * px + m_A26 * pt;
323 lambdaz = m_A33 * y + m_A34 * py;
324
325 // push the spin vector using the generator just determined
326 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
327
328 // axis-angle vector components generating the reference spin map
329 lambdax = 0.0_prt;
330 lambday = m_lambday;
331 lambdaz = 0.0_prt;
332
333 // push the spin vector using the generator just determined
334 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
335
336 // phase space push
337 (*this)(x, y, t, px, py, pt, idcpu, refpart);
338
339 }
340
341
343 using LinearTransport::operator();
344
351 Map6x6
352 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
353 {
354 using namespace amrex::literals; // for _rt and _prt
355 using amrex::Math::powi;
356
357 // length of the current slice
358 amrex::ParticleReal const slice_ds = m_ds / nslice();
359
360 // find beta*gamma^2, beta
361 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
362 amrex::ParticleReal const bet = refpart.beta();
363 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
364 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
365
366 // update horizontal and longitudinal phase space variables
367 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
368 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
369
370 // update vertical phase space variables
371 amrex::ParticleReal const gy = -m_k;
372 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
373
374 // trigonometry
375 auto const [sinx, cosx] = amrex::Math::sincos(omega_x * slice_ds);
376 amrex::ParticleReal const sinhx = std::sinh(omega_x * slice_ds);
377 amrex::ParticleReal const coshx = std::cosh(omega_x * slice_ds);
378 auto const [siny, cosy] = amrex::Math::sincos(omega_y * slice_ds);
379 amrex::ParticleReal const sinhy = std::sinh(omega_y * slice_ds);
380 amrex::ParticleReal const coshy = std::cosh(omega_y * slice_ds);
381
382 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
383 amrex::ParticleReal const iobrc = 1_prt / (omega_x * bet * m_rc);
384 amrex::ParticleReal const igobr = 1_prt / (gx * omega_x * b2rc2);
385
386 // initialize linear map matrix elements
388
389 R(1,1) = gx > 0_prt ? cosx : coshx;
390 R(1,2) = gx > 0_prt ? sinx / omega_x : sinhx / omega_x;
391 R(1,6) = gx > 0_prt ? -(1_prt - cosx) * igbrc : -(1_prt - coshx) * igbrc;
392 R(2,1) = gx > 0_prt ? -omega_x * sinx : omega_x * sinhx;
393 R(2,2) = gx > 0_prt ? cosx : coshx;
394 R(2,6) = gx > 0_prt ? -sinx * iobrc : -sinhx * iobrc;
395 R(3,3) = gy > 0_prt ? cosy : coshy;
396 R(3,4) = gy > 0_prt ? siny / omega_y : sinhy / omega_y;
397 R(4,3) = gy > 0_prt ? -omega_y * siny : omega_y * sinhy;
398 R(4,4) = gy > 0_prt ? cosy : coshy;
399 R(5,1) = gx > 0_prt ? sinx * iobrc : sinhx * iobrc;
400 R(5,2) = gx > 0_prt ? (1_prt - cosx) * igbrc : (1_prt - coshx) * igbrc;
401 R(5,6) = gx > 0_prt ?
402 slice_ds * ibetgam2 + (sinx - omega_x * slice_ds) * igobr :
403 slice_ds * ibetgam2 + (sinhx - omega_x * slice_ds) * igobr;
404
405 // apply the transverse rotation (roll) alignment error
406 return rotate_aligned_map(R);
407 }
408
411
412 private:
413 // constants that are independent of the individually tracked particle,
414 // see: compute_constants() to refresh
417 };
418
419} // namespace impactx
420
422
423#endif // IMPACTX_CFBEND_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)
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 beta() const
Definition ReferenceParticle.H:151
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:139
Definition CFbend.H:46
amrex::ParticleReal m_R21
Definition CFbend.H:415
amrex::ParticleReal m_A22
Definition CFbend.H:416
void compute_constants(RefPart const &refpart)
Definition CFbend.H:100
void reverse()
Definition CFbend.H:88
amrex::ParticleReal m_A26
Definition CFbend.H:416
amrex::ParticleReal m_R43
Definition CFbend.H:415
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 CFbend.H:298
static constexpr auto type
Definition CFbend.H:47
amrex::ParticleReal m_R11
quadrupole strength in m^(-2)
Definition CFbend.H:415
amrex::ParticleReal m_rc
Definition CFbend.H:409
amrex::ParticleReal m_R44
Definition CFbend.H:415
amrex::ParticleReal m_A21
Definition CFbend.H:416
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 CFbend.H:194
amrex::ParticleReal m_A13
Definition CFbend.H:416
CFbend(amrex::ParticleReal ds, amrex::ParticleReal rc, 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 CFbend.H:67
amrex::ParticleReal m_A33
Definition CFbend.H:416
amrex::ParticleReal m_R56
Definition CFbend.H:415
ImpactXParticleContainer::ParticleType PType
Definition CFbend.H:48
amrex::ParticleReal m_R33
Definition CFbend.H:415
amrex::ParticleReal m_k
bend radius in m
Definition CFbend.H:410
amrex::ParticleReal m_R16
Definition CFbend.H:415
amrex::ParticleReal m_lambday
Definition CFbend.H:416
amrex::ParticleReal m_R52
Definition CFbend.H:415
amrex::ParticleReal m_R26
Definition CFbend.H:415
amrex::ParticleReal m_R51
Definition CFbend.H:415
amrex::ParticleReal m_A34
Definition CFbend.H:416
amrex::ParticleReal m_R34
Definition CFbend.H:415
amrex::ParticleReal m_R12
Definition CFbend.H:415
amrex::ParticleReal m_R22
Definition CFbend.H:415
amrex::ParticleReal m_A14
Definition CFbend.H:416
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition CFbend.H:352
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:189
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:179
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:263
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:39
Definition beamoptic.H:529
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 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