ImpactX
ChrPlasmaLens.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_CHRPLASMALENS_H
11 #define IMPACTX_CHRPLASMALENS_H
12 
14 #include "mixin/alignment.H"
15 #include "mixin/beamoptic.H"
16 #include "mixin/thick.H"
17 #include "mixin/nofinalize.H"
18 
19 #include <AMReX_Extension.H>
20 #include <AMReX_REAL.H>
21 
22 #include <cmath>
23 
24 
25 namespace impactx
26 {
28  : public elements::BeamOptic<ChrPlasmaLens>,
29  public elements::Thick,
30  public elements::Alignment,
32  {
33  static constexpr auto name = "ChrPlasmaLens";
35 
54  amrex::ParticleReal ds,
55  amrex::ParticleReal k,
56  int unit,
57  amrex::ParticleReal dx = 0,
58  amrex::ParticleReal dy = 0,
59  amrex::ParticleReal rotation_degree = 0,
60  int nslice = 1
61  )
62  : Thick(ds, nslice),
63  Alignment(dx, dy, rotation_degree),
64  m_k(k), m_unit(unit)
65  {
66  }
67 
69  using BeamOptic::operator();
70 
84  amrex::ParticleReal & AMREX_RESTRICT x,
85  amrex::ParticleReal & AMREX_RESTRICT y,
86  amrex::ParticleReal & AMREX_RESTRICT t,
87  amrex::ParticleReal & AMREX_RESTRICT px,
88  amrex::ParticleReal & AMREX_RESTRICT py,
89  amrex::ParticleReal & AMREX_RESTRICT pt,
90  [[maybe_unused]] uint64_t & AMREX_RESTRICT idcpu,
91  RefPart const & refpart
92  ) const
93  {
94  using namespace amrex::literals; // for _rt and _prt
95 
96  // shift due to alignment errors of the element
97  shift_in(x, y, px, py);
98 
99  // length of the current slice
100  amrex::ParticleReal const slice_ds = m_ds / nslice();
101 
102  // access reference particle values to find beta
103  amrex::ParticleReal const bet = refpart.beta();
104 
105  // normalize focusing strength units to MAD-X convention if needed
106  amrex::ParticleReal g = m_k;
107  if (m_unit == 1) {
108  g = m_k / refpart.rigidity_Tm();
109  }
110 
111  // compute particle momentum deviation delta + 1
112  amrex::ParticleReal delta1;
113  delta1 = sqrt(1_prt - 2_prt*pt/bet + pow(pt,2));
114  amrex::ParticleReal const delta = delta1 - 1_prt;
115 
116  // compute phase advance per unit length in s (in rad/m)
117  // chromatic dependence on delta is included
118  amrex::ParticleReal const omega = sqrt(std::abs(g)/delta1);
119 
120  // initialize output values
121  amrex::ParticleReal xout = x;
122  amrex::ParticleReal yout = y;
123 
124  // intialize output values of momenta
125  amrex::ParticleReal pxout = px;
126  amrex::ParticleReal pyout = py;
127  amrex::ParticleReal const ptout = pt;
128 
129  // paceholder variables
130  amrex::ParticleReal q1 = x;
131  amrex::ParticleReal q2 = y;
132  amrex::ParticleReal p1 = px;
133  amrex::ParticleReal p2 = py;
134 
135  auto const [sin_ods, cos_ods] = amrex::Math::sincos(omega*slice_ds);
136 
137  // advance transverse position and momentum (focusing)
138  xout = cos_ods * x + sin_ods / (omega * delta1) * px;
139  pxout = -omega * delta1 * sin_ods * x + cos_ods * px;
140 
141  yout = cos_ods * y + sin_ods / (omega * delta1) * py;
142  pyout = -omega * delta1 * sin_ods * y + cos_ods * py;
143 
144  // advance longitudinal position and momentum
145 
146  // the corresponding symplectic update to t
147  amrex::ParticleReal const term = pt + delta/bet;
148  amrex::ParticleReal const t0 = t - term*slice_ds/delta1;
149 
150  amrex::ParticleReal const w = omega*delta1;
151  amrex::ParticleReal const term1 = -(pow(p2,2)-pow(q2,2)*pow(w,2))*sin(2_prt*slice_ds*omega);
152  amrex::ParticleReal const term2 = -(pow(p1,2)-pow(q1,2)*pow(w,2))*sin(2_prt*slice_ds*omega);
153  amrex::ParticleReal const term3 = -2_prt*q2*p2*w*cos(2_prt*slice_ds*omega);
154  amrex::ParticleReal const term4 = -2_prt*q1*p1*w*cos(2_prt*slice_ds*omega);
155  amrex::ParticleReal const term5 = 2_prt*omega*(q1*p1*delta1 + q2*p2*delta1
156  -(pow(p1,2)+pow(p2,2))*slice_ds - (pow(q1,2)+pow(q2,2))*pow(w,2)*slice_ds);
157  t = t0 + (-1_prt+bet*pt)/(8_prt*bet*pow(delta1,3)*omega)
158  *(term1+term2+term3+term4+term5);
159 
160  // ptout = pt;
161 
162  // assign updated position & momenta
163  x = xout;
164  y = yout;
165  px = pxout;
166  py = pyout;
167  pt = ptout;
168 
169  // undo shift due to alignment errors of the element
170  shift_out(x, y, px, py);
171  }
172 
178  void operator() (RefPart & AMREX_RESTRICT refpart) const
179  {
180  using namespace amrex::literals; // for _rt and _prt
181 
182  // assign input reference particle values
183  amrex::ParticleReal const x = refpart.x;
184  amrex::ParticleReal const px = refpart.px;
185  amrex::ParticleReal const y = refpart.y;
186  amrex::ParticleReal const py = refpart.py;
187  amrex::ParticleReal const z = refpart.z;
188  amrex::ParticleReal const pz = refpart.pz;
189  amrex::ParticleReal const t = refpart.t;
190  amrex::ParticleReal const pt = refpart.pt;
191  amrex::ParticleReal const s = refpart.s;
192 
193  // length of the current slice
194  amrex::ParticleReal const slice_ds = m_ds / nslice();
195 
196  // assign intermediate parameter
197  amrex::ParticleReal const step = slice_ds / sqrt(pow(pt,2)-1.0_prt);
198 
199  // advance position and momentum (straight element)
200  refpart.x = x + step*px;
201  refpart.y = y + step*py;
202  refpart.z = z + step*pz;
203  refpart.t = t - step*pt;
204 
205  // advance integrated path length
206  refpart.s = s + slice_ds;
207  }
208 
209  amrex::ParticleReal m_k;
210  int m_unit;
211  };
212 
213 } // namespace impactx
214 
215 #endif // IMPACTX_CHRPLASMALENS_H
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::pair< double, double > sincos(double x)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > pow(const GpuComplex< T > &a_z, const T &a_y) noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Definition: ImpactX.cpp:33
@ t
fixed t as the independent variable
s
tuple w
Definition: ChrPlasmaLens.H:32
static constexpr auto name
Definition: ChrPlasmaLens.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(amrex::ParticleReal &AMREX_RESTRICT x, amrex::ParticleReal &AMREX_RESTRICT y, amrex::ParticleReal &AMREX_RESTRICT t, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py, amrex::ParticleReal &AMREX_RESTRICT pt, [[maybe_unused]] uint64_t &AMREX_RESTRICT idcpu, RefPart const &refpart) const
Definition: ChrPlasmaLens.H:83
ImpactXParticleContainer::ParticleType PType
Definition: ChrPlasmaLens.H:34
int m_unit
focusing strength in 1/m^2 (or T/m)
Definition: ChrPlasmaLens.H:210
ChrPlasmaLens(amrex::ParticleReal ds, amrex::ParticleReal k, int unit, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, int nslice=1)
Definition: ChrPlasmaLens.H:53
amrex::ParticleReal m_k
Definition: ChrPlasmaLens.H:209
Definition: ReferenceParticle.H:30
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition: ReferenceParticle.H:169
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition: ReferenceParticle.H:64
Definition: alignment.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_out(amrex::ParticleReal &AMREX_RESTRICT x, amrex::ParticleReal &AMREX_RESTRICT y, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py) const
Definition: alignment.H:91
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition: alignment.H:120
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_in(amrex::ParticleReal &AMREX_RESTRICT x, amrex::ParticleReal &AMREX_RESTRICT y, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py) const
Definition: alignment.H:61
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition: alignment.H:130
Definition: beamoptic.H:149
Definition: nofinalize.H:22
Definition: thick.H:24
Thick(amrex::ParticleReal ds, int nslice)
Definition: thick.H:30
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
amrex::ParticleReal m_ds
Definition: thick.H:58