ImpactX
Loading...
Searching...
No Matches
ChrQuad.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_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
31
32namespace impactx::elements
33{
34 struct ChrQuad
35 : public mixin::Named,
36 public mixin::BeamOptic<ChrQuad>,
37 public mixin::LinearTransport<ChrQuad>,
38 public mixin::Thick,
39 public mixin::Alignment,
42 public mixin::NoFinalize,
43 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
44 {
45 static constexpr auto type = "ChrQuad";
47
73 int unit,
76 amrex::ParticleReal rotation_degree = 0,
79 int nslice = 1,
80 std::optional<std::string> name = std::nullopt
81 )
82 : Named(std::move(name)),
83 Thick(ds, nslice),
84 Alignment(dx, dy, rotation_degree),
86 m_k(k), m_unit(unit)
87 {
88 }
89
91 void reverse () { Thick::reverse(); }
92
94 using BeamOptic::operator();
95
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 m_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>(m_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 & AMREX_RESTRICT pt,
144 T_IdCpu & AMREX_RESTRICT idcpu,
145 [[maybe_unused]] 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 // shift due to alignment errors of the element
153 shift_in(x, y, px, py);
154
155 // access position data
156 T_Real const xout = x;
157 T_Real const yout = y;
158 T_Real const tout = t;
159
160 // compute particle momentum deviation delta + 1
161 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
162 T_Real const delta = delta1 - 1_prt;
163
164 // compute phase advance per unit length in s (in rad/m)
165 // chromatic dependence on delta is included
166 T_Real const omega = sqrt(abs(m_g)/delta1);
167
168 // initialize output values of momenta
169 T_Real pxout = px;
170 T_Real pyout = py;
171 T_Real const ptout = pt;
172
173 // paceholder variables
174 T_Real q1 = xout;
175 T_Real q2 = yout;
176 T_Real p1 = px;
177 T_Real p2 = py;
178
179 if (m_g > 0_prt)
180 {
181 // advance transverse position and momentum (focusing quad)
182 x = cos(omega*m_slice_ds) * xout +
183 sin(omega*m_slice_ds) / (omega*delta1)*px;
184 pxout = -omega * delta1 * sin(omega*m_slice_ds) * xout + cos(omega * m_slice_ds) * px;
185
186 y = cosh(omega*m_slice_ds) * yout +
187 sinh(omega*m_slice_ds) / (omega*delta1)*py;
188 pyout = omega * delta1 * sinh(omega*m_slice_ds) * yout + cosh(omega * m_slice_ds) * py;
189
190 } else if (m_g < 0_prt)
191 {
192 // advance transverse position and momentum (defocusing quad)
193 x = cosh(omega*m_slice_ds) * xout +
194 sinh(omega*m_slice_ds) / (omega*delta1)*px;
195 pxout = omega * delta1 * sinh(omega*m_slice_ds) * xout + cosh(omega * m_slice_ds) * px;
196
197 y = cos(omega*m_slice_ds) * yout +
198 sin(omega*m_slice_ds) / (omega*delta1) * py;
199 pyout = -omega * delta1 * sin(omega*m_slice_ds) * yout + cos(omega * m_slice_ds) * py;
200
201 q1 = yout;
202 q2 = xout;
203 p1 = py;
204 p2 = px;
205 } else
206 {
207 // advance transverse position and momentum (zero focusing strength = drift)
208 x = xout + m_slice_ds * px / delta1;
209 // pxout = px;
210 y = yout + m_slice_ds * py / delta1;
211 // pyout = py;
212 }
213
214
215 // advance longitudinal position and momentum
216
217 if (m_g == 0.0)
218 {
219 // the corresponding symplectic update to t (zero strength = drift)
220 T_Real term = 2_prt * powi<2>(pt) + powi<2>(px) + powi<2>(py);
221 term = 2_prt - 4_prt * m_beta * pt + powi<2>(m_beta) * term;
222 term = -2_prt + powi<2>(m_gamma)*term;
223 term = (-1_prt + m_beta * pt) * term;
224 term = term * m_const1;
225 t = tout - m_slice_ds * (1_prt / m_beta + term / powi<3>(delta1));
226 // ptout = pt;
227
228 } else
229 {
230 // the corresponding symplectic update to t (nonzero strength)
231 T_Real const term = pt + delta / m_beta;
232 T_Real const t0 = tout - term * m_slice_ds / delta1;
233
234 T_Real const w = omega * delta1;
235 T_Real const term1 = -(powi<2>(p2) + powi<2>(q2) * powi<2>(w)) * sinh(2_prt*m_slice_ds*omega);
236 T_Real const term2 = -(powi<2>(p1) - powi<2>(q1) * powi<2>(w)) * sin(2_prt*m_slice_ds*omega);
237 T_Real const term3 = -2_prt * q2 * p2 * w * cosh(2_prt*m_slice_ds*omega);
238 T_Real const term4 = -2_prt * q1 * p1 * w * cos(2_prt*m_slice_ds*omega);
239 T_Real const term5 = 2_prt * omega * (q1*p1*delta1 + q2*p2*delta1
240 -(powi<2>(p1) + powi<2>(p2))*m_slice_ds - (powi<2>(q1)-powi<2>(q2)) * powi<2>(w)*m_slice_ds);
241 t = t0 + (-1_prt+m_beta*pt)
242 / (8_prt*m_beta * powi<3>(delta1)*omega)
243 * (term1+term2+term3+term4+term5);
244 // ptout = pt;
245 }
246
247 // assign updated momenta
248 px = pxout;
249 py = pyout;
250 pt = ptout;
251
252 // apply transverse aperture
253 apply_aperture(x, y, idcpu);
254
255 // undo shift due to alignment errors of the element
256 shift_out(x, y, px, py);
257 }
258
264 void operator() (RefPart & AMREX_RESTRICT refpart) const
265 {
266 using namespace amrex::literals; // for _rt and _prt
267 using amrex::Math::powi;
268
269 // assign input reference particle values
270 amrex::ParticleReal const x = refpart.x;
271 amrex::ParticleReal const px = refpart.px;
272 amrex::ParticleReal const y = refpart.y;
273 amrex::ParticleReal const py = refpart.py;
274 amrex::ParticleReal const z = refpart.z;
275 amrex::ParticleReal const pz = refpart.pz;
276 amrex::ParticleReal const t = refpart.t;
277 amrex::ParticleReal const pt = refpart.pt;
278 amrex::ParticleReal const s = refpart.s;
279
280 // length of the current slice
281 amrex::ParticleReal const slice_ds = m_ds / nslice();
282
283 // assign intermediate parameter
284 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt)-1.0_prt);
285
286 // advance position and momentum (straight element)
287 refpart.x = x + step*px;
288 refpart.y = y + step*py;
289 refpart.z = z + step*pz;
290 refpart.t = t - step*pt;
291
292 // advance integrated path length
293 refpart.s = s + slice_ds;
294 }
295
296
297
312 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
315 [[maybe_unused]] T_Real & AMREX_RESTRICT x,
316 [[maybe_unused]] T_Real & AMREX_RESTRICT y,
317 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
318 [[maybe_unused]] T_Real & AMREX_RESTRICT px,
319 [[maybe_unused]] T_Real & AMREX_RESTRICT py,
320 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
321 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
322 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
323 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
324 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
325 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
326 ) const
327 {
328 using namespace amrex::literals; // for _rt and _prt
329 using amrex::Math::powi;
330
331 // initialize the three components of the axis-angle vector
332 T_Real lambdax = 0_prt;
333 T_Real lambday = 0_prt;
334 T_Real lambdaz = 0_prt;
335
336 // compute particle momentum deviation delta + 1
337 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
338 T_Real const inv_delta1 = 1_prt / delta1;
339
340 // compute particle relativistic gamma
341 T_Real const gamma = m_gamma * (1_prt - m_beta*pt);
342 T_Real const gyro_const = 1_prt + refpart.gyromagnetic_anomaly * gamma;
343
344 // compute phase advance per unit length in s (in rad/m)
345 // chromatic dependence on delta is included
346 T_Real const omega = sqrt(abs(m_g)*inv_delta1);
347
348 // compute trigonometric quantities
349 auto const [sin_omega_ds, cos_omega_ds] = amrex::Math::sincos(omega*m_slice_ds);
350 T_Real const sinh_omega_ds = sinh(omega*m_slice_ds);
351 T_Real const cosh_omega_ds = cosh(omega*m_slice_ds);
352
353 if (m_g > 0.0_prt)
354 {
355
356 // horizontally focusing quad case
357 lambdax = -gyro_const * ( py/delta1*(cosh_omega_ds - 1_prt) + y*omega*sinh_omega_ds );
358lambdax = -gyro_const * ( py*inv_delta1*(cosh_omega_ds - 1_prt) + y*omega*sinh_omega_ds );
359lambday = -gyro_const * ( px*inv_delta1*(1_prt - cos_omega_ds) + x*omega*sin_omega_ds );
360
361 } else if (m_g < 0.0_prt)
362 {
363
364 // horizontally defocusing quad case
365 lambdax = gyro_const * ( py*inv_delta1*(1_prt - cos_omega_ds) + y*omega*sin_omega_ds );
366 lambday = gyro_const * ( px*inv_delta1*(cosh_omega_ds - 1_prt) + x*omega*sinh_omega_ds );
367
368 } else {
369 // treat as a drift
370 }
371
372 // push the spin vector using the generator just determined
373 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
374
375 // phase space push
376 (*this)(x, y, t, px, py, pt, idcpu, refpart);
377
378 }
379
380
382 using LinearTransport::operator();
383
390 Map6x6
391 transport_map (RefPart const & AMREX_RESTRICT refpart) const
392 {
393 using namespace amrex::literals; // for _rt and _prt
394 using amrex::Math::powi;
395
396 // length of the current slice
397 amrex::ParticleReal const slice_ds = m_ds / nslice();
398
399 // access reference particle values to find beta*gamma^2
400 amrex::ParticleReal const pt_ref = refpart.pt;
401 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1_prt;
402
403 // normalize quad units to MAD-X convention if needed
404 amrex::ParticleReal const g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
405
406 // compute phase advance per unit length in s (in rad/m)
407 amrex::ParticleReal const omega = std::sqrt(std::abs(g));
408
409 // initialize linear map matrix elements
411
412 if (g > 0.0) {
413 R(1,1) = std::cos(omega*slice_ds);
414 R(1,2) = std::sin(omega*slice_ds)/omega;
415 R(2,1) = -omega*std::sin(omega*slice_ds);
416 R(2,2) = std::cos(omega*slice_ds);
417 R(3,3) = std::cosh(omega*slice_ds);
418 R(3,4) = std::sinh(omega*slice_ds)/omega;
419 R(4,3) = omega*std::sinh(omega*slice_ds);
420 R(4,4) = std::cosh(omega*slice_ds);
421 R(5,6) = slice_ds/betgam2;
422 } else if (g < 0.0) {
423 R(1,1) = std::cosh(omega*slice_ds);
424 R(1,2) = std::sinh(omega*slice_ds)/omega;
425 R(2,1) = omega*std::sinh(omega*slice_ds);
426 R(2,2) = std::cosh(omega*slice_ds);
427 R(3,3) = std::cos(omega*slice_ds);
428 R(3,4) = std::sin(omega*slice_ds)/omega;
429 R(4,3) = -omega*std::sin(omega*slice_ds);
430 R(4,4) = std::cos(omega*slice_ds);
431 R(5,6) = slice_ds/betgam2;
432 } else {
433 R(1,2) = m_slice_ds;
434 R(3,4) = m_slice_ds;
435 R(5,6) = m_slice_ds / betgam2;
436 }
437
438 return R;
439 }
440
442 int m_unit;
443
444 private:
445 // constants that are independent of the individually tracked particle,
446 // see: compute_constants() to refresh
452 };
453
454} // namespace impactx
455
457
458#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
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
__host__ __device__ T abs(const GpuComplex< T > &a_z) noexcept
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) 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_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition ReferenceParticle.H:261
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:152
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:140
Definition ChrQuad.H:44
int m_unit
quadrupole strength in 1/m^2 (or T/m)
Definition ChrQuad.H:442
amrex::ParticleReal m_const1
Definition ChrQuad.H:451
amrex::ParticleReal m_slice_ds
unit specification for quad strength
Definition ChrQuad.H:447
ImpactXParticleContainer::ParticleType PType
Definition ChrQuad.H:46
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ChrQuad.H:391
void compute_constants(RefPart const &refpart)
Definition ChrQuad.H:104
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 ChrQuad.H:314
amrex::ParticleReal m_gamma
Definition ChrQuad.H:449
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 ChrQuad.H:137
ChrQuad(amrex::ParticleReal ds, amrex::ParticleReal k, int unit, 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 ChrQuad.H:70
amrex::ParticleReal m_k
Definition ChrQuad.H:441
static constexpr auto type
Definition ChrQuad.H:45
void reverse()
Definition ChrQuad.H:91
amrex::ParticleReal m_beta
m_ds / nslice();
Definition ChrQuad.H:448
amrex::ParticleReal m_g
Definition ChrQuad.H:450
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