ImpactX
Loading...
Searching...
No Matches
ChrPlasmaLens.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 Sjobak
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_CHRPLASMALENS_H
11#define IMPACTX_CHRPLASMALENS_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{
34 : public mixin::Named,
35 public mixin::BeamOptic<ChrPlasmaLens>,
36 public mixin::LinearTransport<ChrPlasmaLens>,
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 = "ChrPlasmaLens";
46
47 static constexpr int DEFAULT_unit = 0;
48
88
90 void reverse () { Thick::reverse(); }
91
93 using BeamOptic::operator();
94
102 void compute_constants (RefPart const & refpart)
103 {
104 using namespace amrex::literals; // for _rt and _prt
105 using amrex::Math::powi;
106
107 Alignment::compute_constants(refpart);
108
109 // length of the current slice
110 m_slice_ds = m_ds / nslice();
111
112 // access reference particle values to find beta and gamma
113 m_beta = refpart.beta();
114 amrex::ParticleReal const gamma = refpart.gamma();
115
116 // normalize focusing strength units to MAD-X convention if needed
117 m_g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
118
119 //For m_g=0 time propagation
120 m_const1 = 1_prt / (2_prt * powi<3>(m_beta) * powi<2>(gamma));
121 }
122
134 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
137 T_Real & AMREX_RESTRICT x,
138 T_Real & AMREX_RESTRICT y,
139 T_Real & AMREX_RESTRICT t,
140 T_Real & AMREX_RESTRICT px,
141 T_Real & AMREX_RESTRICT py,
142 T_Real const & AMREX_RESTRICT pt,
143 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
144 RefPart const & AMREX_RESTRICT refpart
145 ) const
146 {
147 using namespace amrex::literals; // for _rt and _prt
148 using amrex::Math::powi;
149 using namespace std; // for cmath(float)
150
151 // compute particle momentum deviation delta + 1
152 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
153 T_Real const delta = delta1 - 1_prt;
154
155 // compute phase advance per unit length in s (in rad/m)
156 // chromatic dependence on delta is included
157 T_Real const omega = sqrt(abs(m_g)/delta1);
158
159 // initialize output values
160 T_Real xout = x;
161 T_Real yout = y;
162 T_Real tout = t;
163
164 // initialize output values of momenta
165 T_Real pxout = px;
166 T_Real pyout = py;
167 // T_Real const ptout = pt;
168
169 // placeholder variables -- canonical position and momentum
170 T_Real const q1 = x;
171 T_Real const q2 = y;
172 T_Real const p1 = px;
173 T_Real const p2 = py;
174
175 if (m_g > 0_prt)
176 {
177 auto const [sin_ods, cos_ods] = amrex::Math::sincos(omega*m_slice_ds);
178
179 // advance transverse position and momentum (focusing)
180 xout = cos_ods * x + sin_ods / (omega * delta1) * px;
181 pxout = -omega * delta1 * sin_ods * x + cos_ods * px;
182
183 yout = cos_ods * y + sin_ods / (omega * delta1) * py;
184 pyout = -omega * delta1 * sin_ods * y + cos_ods * py;
185
186 // the corresponding symplectic update to t (focusing both x and y)
187 T_Real const term = pt + delta/m_beta;
188 T_Real const t0 = t - term*m_slice_ds/delta1;
189
190 T_Real const w = omega*delta1;
191 T_Real const term1 = -(powi<2>(p2) - powi<2>(q2) * powi<2>(w)) * sin(2_prt*m_slice_ds*omega);
192 T_Real const term2 = -(powi<2>(p1) - powi<2>(q1) * powi<2>(w)) * sin(2_prt*m_slice_ds*omega);
193 T_Real const term3 = -2_prt * q2 * p2 * w * cos(2_prt*m_slice_ds*omega);
194 T_Real const term4 = -2_prt * q1 * p1 * w * cos(2_prt*m_slice_ds*omega);
195 T_Real const term5 = 2_prt * omega * (q1*p1*delta1 + q2*p2*delta1
196 -(powi<2>(p1) + powi<2>(p2))*m_slice_ds - (powi<2>(q1) + powi<2>(q2)) * powi<2>(w)*m_slice_ds);
197 tout = t0 + (-1_prt+m_beta*pt)
198 / (8_prt*m_beta * powi<3>(delta1)*omega)
199 * (term1+term2+term3+term4+term5);
200
201 }
202 else if (m_g < 0_prt)
203 {
204 auto const sinh_ods = sinh(omega*m_slice_ds);
205 auto const cosh_ods = cosh(omega*m_slice_ds);
206
207 // advance transverse position and momentum (defocusing)
208 xout = cosh_ods * x + sinh_ods / (omega * delta1) * px;
209 pxout = +omega * delta1 * sinh_ods * x + cosh_ods * px;
210
211 yout = cosh_ods * y + sinh_ods / (omega * delta1) * py;
212 pyout = +omega * delta1 * sinh_ods * y + cosh_ods * py;
213
214 // the corresponding symplectic update to t (defocusing both x and y)
215 T_Real const term = pt + delta/m_beta;
216 T_Real const t0 = t - term*m_slice_ds/delta1;
217
218 T_Real const w = omega*delta1;
219 T_Real const term1 = -(powi<2>(p2) + powi<2>(q2) * powi<2>(w)) * sinh(2_prt*m_slice_ds*omega);
220 T_Real const term2 = -(powi<2>(p1) + powi<2>(q1) * powi<2>(w)) * sinh(2_prt*m_slice_ds*omega);
221 T_Real const term3 = -2_prt * q2 * p2 * w * cosh(2_prt*m_slice_ds*omega);
222 T_Real const term4 = -2_prt * q1 * p1 * w * cosh(2_prt*m_slice_ds*omega);
223 T_Real const term5 = 2_prt * omega * (q1*p1*delta1 + q2*p2*delta1
224 -(powi<2>(p1) + powi<2>(p2))*m_slice_ds - (powi<2>(q1) + powi<2>(q2)) * powi<2>(w)*m_slice_ds);
225 tout = t0 + (-1_prt+m_beta*pt)
226 / (8_prt*m_beta * powi<3>(delta1)*omega)
227 * (term1+term2+term3+term4+term5);
228 }
229 else {
230 xout = x + px*m_slice_ds / delta1;
231 pxout = px;
232
233 yout = y + py*m_slice_ds / delta1;
234 pyout = py;
235
236 // the corresponding symplectic update to t (zero strength = drift),
237 // avoiding division by 0
238 T_Real term = 2_prt * powi<2>(pt) + powi<2>(px) + powi<2>(py);
239 term = 2_prt - 4_prt * m_beta * pt + powi<2>(m_beta) * term;
240 term = -2_prt + powi<2>(refpart.gamma())*term;
241 term = (-1_prt + m_beta * pt) * term;
242 term = term * m_const1;
243 tout = t - m_slice_ds * (1_prt / m_beta + term / powi<3>(delta1));
244 }
245 // advance longitudinal position and momentum
246
247 // assign updated position & momenta
248 x = xout;
249 y = yout;
250 t = tout;
251 px = pxout;
252 py = pyout;
253 // pt = ptout;
254 }
255
261 void operator() (RefPart & AMREX_RESTRICT refpart) const
262 {
263 using namespace amrex::literals; // for _rt and _prt
264 using amrex::Math::powi;
265
266 // assign input reference particle values
267 amrex::ParticleReal const x = refpart.x;
268 amrex::ParticleReal const px = refpart.px;
269 amrex::ParticleReal const y = refpart.y;
270 amrex::ParticleReal const py = refpart.py;
271 amrex::ParticleReal const z = refpart.z;
272 amrex::ParticleReal const pz = refpart.pz;
273 amrex::ParticleReal const t = refpart.t;
274 amrex::ParticleReal const pt = refpart.pt;
275 amrex::ParticleReal const s = refpart.s;
276
277 // length of the current slice
278 amrex::ParticleReal const slice_ds = m_ds / nslice();
279
280 // assign intermediate parameter
281 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt)-1.0_prt);
282
283 // advance position and momentum (straight element)
284 refpart.x = x + step*px;
285 refpart.y = y + step*py;
286 refpart.z = z + step*pz;
287 refpart.t = t - step*pt;
288
289 // advance integrated path length
290 refpart.s = s + slice_ds;
291 }
292
293
308 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
311 T_Real & AMREX_RESTRICT x,
312 T_Real & AMREX_RESTRICT y,
313 T_Real & AMREX_RESTRICT t,
314 T_Real & AMREX_RESTRICT px,
315 T_Real & AMREX_RESTRICT py,
316 T_Real const & AMREX_RESTRICT pt,
317 T_Real & AMREX_RESTRICT sx,
318 T_Real & AMREX_RESTRICT sy,
319 T_Real & AMREX_RESTRICT sz,
320 T_IdCpu const & AMREX_RESTRICT idcpu,
321 RefPart const & AMREX_RESTRICT refpart
322 ) const
323 {
324 using namespace amrex::literals; // for _rt and _prt
325 using namespace std; // for cmath(float)
326 using amrex::Math::powi;
327
328 // initialize the three components of the axis-angle vector
329 T_Real lambdax = 0_prt;
330 T_Real lambday = 0_prt;
331 T_Real lambdaz = 0_prt;
332
333 // compute particle momentum deviation delta + 1
334 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
335 T_Real const inv_delta1 = 1_prt / delta1;
336
337 // compute particle relativistic gamma
338 T_Real const gamma = refpart.gamma() * (1_prt - m_beta*pt);
339 T_Real const gyro_const = 1_prt + refpart.gyromagnetic_anomaly * gamma;
340
341 // compute phase advance per unit length in s (in rad/m)
342 // chromatic dependence on delta is included
343 T_Real const omega = sqrt(abs(m_g)*inv_delta1);
344
345 // compute trigonometric quantities
346 auto const [sin_omega_ds, cos_omega_ds] = amrex::Math::sincos(omega*m_slice_ds);
347 T_Real const sinh_omega_ds = sinh(omega*m_slice_ds);
348 T_Real const cosh_omega_ds = cosh(omega*m_slice_ds);
349
350 if (m_g > 0.0_prt)
351 {
352
353 // horizontally focusing quad case
354 lambdax = +gyro_const * ( py*inv_delta1*(1_prt - cos_omega_ds) + y*omega*sin_omega_ds );
355 lambday = -gyro_const * ( px*inv_delta1*(1_prt - cos_omega_ds) + x*omega*sin_omega_ds );
356
357 } else if (m_g < 0.0_prt)
358 {
359
360 // horizontally defocusing quad case
361 lambdax = -gyro_const * ( py*inv_delta1*(cosh_omega_ds - 1_prt) + y*omega*sinh_omega_ds );
362 lambday = +gyro_const * ( px*inv_delta1*(cosh_omega_ds - 1_prt) + x*omega*sinh_omega_ds );
363
364 } else {
365 // treat as a drift
366 }
367
368 // push the spin vector using the generator just determined
369 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
370
371 // phase space push
372 (*this)(x, y, t, px, py, pt, idcpu, refpart);
373
374 }
375
376
378 using LinearTransport::operator();
379
386 Map6x6
387 transport_map (RefPart const & AMREX_RESTRICT refpart) const
388 {
389 using namespace amrex::literals; // for _rt and _prt
390 using amrex::Math::powi;
391
392 // length of the current slice
393 amrex::ParticleReal const slice_ds = m_ds / nslice();
394
395 // access reference particle values to find beta*gamma^2
396 amrex::ParticleReal const pt_ref = refpart.pt;
397 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1_prt;
398
399 // compute phase advance per unit length in s (in rad/m)
400 amrex::ParticleReal const omega = std::sqrt(std::abs(m_k));
401
402 // initialize linear map matrix elements
404
405 if (m_k > 0.0) {
406 R(1,1) = std::cos(omega*slice_ds);
407 R(1,2) = std::sin(omega*slice_ds)/omega;
408 R(2,1) = -omega*std::sin(omega*slice_ds);
409 R(2,2) = std::cos(omega*slice_ds);
410 R(3,3) = std::cos(omega*slice_ds);
411 R(3,4) = std::sin(omega*slice_ds)/omega;
412 R(4,3) = -omega*std::sin(omega*slice_ds);
413 R(4,4) = std::cos(omega*slice_ds);
414 R(5,6) = slice_ds/betgam2;
415 } else if (m_k < 0.0) {
416 R(1,1) = std::cosh(omega*slice_ds);
417 R(1,2) = std::sinh(omega*slice_ds)/omega;
418 R(2,1) = omega*std::sinh(omega*slice_ds);
419 R(2,2) = std::cosh(omega*slice_ds);
420 R(3,3) = std::cosh(omega*slice_ds);
421 R(3,4) = std::sinh(omega*slice_ds)/omega;
422 R(4,3) = omega*std::sinh(omega*slice_ds);
423 R(4,4) = std::cosh(omega*slice_ds);
424 R(5,6) = slice_ds/betgam2;
425 } else {
426 R(1,2) = slice_ds;
427 R(3,4) = slice_ds;
428 R(5,6) = slice_ds / betgam2;
429 }
430
431 // apply the transverse rotation (roll) alignment error
432 return rotate_aligned_map(R);
433 }
434
436 int m_unit;
437
438 private:
439 // constants that are independent of the individually tracked particle,
440 // see: compute_constants() to refresh
445 };
446
447} // namespace impactx
448
450
451#endif // IMPACTX_CHRPLASMALENS_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 ChrPlasmaLens.H:43
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 ChrPlasmaLens.H:136
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 ChrPlasmaLens.H:310
int m_unit
focusing strength in 1/m^2 (or T/m)
Definition ChrPlasmaLens.H:436
static constexpr auto type
Definition ChrPlasmaLens.H:44
void compute_constants(RefPart const &refpart)
Definition ChrPlasmaLens.H:102
static constexpr int DEFAULT_unit
Definition ChrPlasmaLens.H:47
amrex::ParticleReal m_beta
m_ds / nslice();
Definition ChrPlasmaLens.H:442
amrex::ParticleReal m_k
Definition ChrPlasmaLens.H:435
amrex::ParticleReal m_const1
Definition ChrPlasmaLens.H:444
amrex::ParticleReal m_g
Definition ChrPlasmaLens.H:443
ChrPlasmaLens(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 ChrPlasmaLens.H:69
amrex::ParticleReal m_slice_ds
unit specification for focusing strength
Definition ChrPlasmaLens.H:441
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ChrPlasmaLens.H:387
void reverse()
Definition ChrPlasmaLens.H:90
ImpactXParticleContainer::ParticleType PType
Definition ChrPlasmaLens.H:45
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