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