ImpactX
RFCavity.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_RFCAVITY_H
11 #define IMPACTX_RFCAVITY_H
12 
15 #include "mixin/beamoptic.H"
16 #include "mixin/thick.H"
17 
18 #include <ablastr/constant.H>
19 
20 #include <AMReX.H>
21 #include <AMReX_Array.H>
22 #include <AMReX_Extension.H>
23 #include <AMReX_REAL.H>
24 
25 #include <array>
26 #include <cmath>
27 #include <tuple>
28 
29 
30 namespace impactx
31 {
32  struct RFCavity
33  : public elements::BeamOptic<RFCavity>,
34  public elements::Thick
35  {
36  static constexpr auto name = "RFCavity";
38 
50  amrex::ParticleReal const ds,
51  amrex::ParticleReal const escale,
52  amrex::ParticleReal const freq,
53  amrex::ParticleReal const phase,
54  int const mapsteps,
55  int const nslice
56  )
57  : Thick(ds, nslice),
58  m_escale(escale), m_freq(freq), m_phase(phase), m_mapsteps(mapsteps)
59  {
60  }
61 
63  using BeamOptic::operator();
64 
76  PType& AMREX_RESTRICT p,
77  amrex::ParticleReal & AMREX_RESTRICT px,
78  amrex::ParticleReal & AMREX_RESTRICT py,
79  amrex::ParticleReal & AMREX_RESTRICT pt,
80  [[maybe_unused]] RefPart const & refpart
81  ) const
82  {
83  using namespace amrex::literals; // for _rt and _prt
84 
85  // access AoS data such as positions and cpu/id
86  amrex::ParticleReal const x = p.pos(0);
87  amrex::ParticleReal const y = p.pos(1);
88  amrex::ParticleReal const t = p.pos(2);
89 
90  // initialize output values of momenta
91  amrex::ParticleReal pxout = px;
92  amrex::ParticleReal pyout = py;
93  amrex::ParticleReal ptout = pt;
94 
95  // get the linear map
97 
98  // symplectic linear map for the RF cavity is computed using the
99  // Hamiltonian formalism as described in:
100  // https://uspas.fnal.gov/materials/09UNM/ComputationalMethods.pdf.
101  // R denotes the transfer matrix in the basis (x,px,y,py,t,pt),
102  // so that, e.g., R(3,4) = dyf/dpyi.
103 
104  // push particles using the linear map
105  p.pos(0) = R(1,1)*x + R(1,2)*px + R(1,3)*y
106  + R(1,4)*py + R(1,5)*t + R(1,6)*pt;
107  pxout = R(2,1)*x + R(2,2)*px + R(2,3)*y
108  + R(2,4)*py + R(2,5)*t + R(2,6)*pt;
109  p.pos(1) = R(3,1)*x + R(3,2)*px + R(3,3)*y
110  + R(3,4)*py + R(3,5)*t + R(3,6)*pt;
111  pyout = R(4,1)*x + R(4,2)*px + R(4,3)*y
112  + R(4,4)*py + R(4,5)*t + R(4,6)*pt;
113  p.pos(2) = R(5,1)*x + R(5,2)*px + R(5,3)*y
114  + R(5,4)*py + R(5,5)*t + R(5,6)*pt;
115  ptout = R(6,1)*x + R(6,2)*px + R(6,3)*y
116  + R(6,4)*py + R(6,5)*t + R(6,6)*pt;
117 
118  // assign updated momenta
119  px = pxout;
120  py = pyout;
121  pt = ptout;
122  }
123 
129  void operator() (RefPart & AMREX_RESTRICT refpart) const
130  {
131  using namespace amrex::literals; // for _rt and _prt
132 
133  // assign input reference particle values
134  amrex::ParticleReal const x = refpart.x;
135  amrex::ParticleReal const px = refpart.px;
136  amrex::ParticleReal const y = refpart.y;
137  amrex::ParticleReal const py = refpart.py;
138  amrex::ParticleReal const z = refpart.z;
139  amrex::ParticleReal const pz = refpart.pz;
140  amrex::ParticleReal const pt = refpart.pt;
141  amrex::ParticleReal const s = refpart.s;
142  amrex::ParticleReal const sedge = refpart.sedge;
143 
144  // initialize linear map (deviation) values
145  for (int i=1; i<7; i++) {
146  for (int j=1; j<7; j++) {
147  if (i == j)
148  refpart.map(i, j) = 1.0_prt;
149  else
150  refpart.map(i, j) = 0.0_prt;
151  }
152  }
153 
154  // length of the current slice
155  amrex::ParticleReal const slice_ds = m_ds / nslice();
156 
157  // compute intial value of beta*gamma
158  amrex::ParticleReal const bgi = sqrt(pow(pt, 2) - 1.0_prt);
159 
160  // call integrator to advance (t,pt)
161  amrex::ParticleReal const zin = s - sedge;
162  amrex::ParticleReal const zout = zin + slice_ds;
163  int const nsteps = m_mapsteps;
164 
165  integrators::symp2_integrate_split3(refpart,zin,zout,nsteps,*this);
166  amrex::ParticleReal const ptf = refpart.pt;
167 
168  // advance position (x,y,z)
169  refpart.x = x + slice_ds*px/bgi;
170  refpart.y = y + slice_ds*py/bgi;
171  refpart.z = z + slice_ds*pz/bgi;
172 
173  // compute final value of beta*gamma
174  amrex::ParticleReal const bgf = sqrt(pow(ptf, 2) - 1.0_prt);
175 
176  // advance momentum (px,py,pz)
177  refpart.px = px*bgf/bgi;
178  refpart.py = py*bgf/bgi;
179  refpart.pz = pz*bgf/bgi;
180 
181  // convert linear map from dynamic to static units
182  amrex::ParticleReal scale_in = 1.0_prt;
183  amrex::ParticleReal scale_fin = 1.0_prt;
184 
185  for (int i=1; i<7; i++) {
186  for (int j=1; j<7; j++) {
187  if( i % 2 == 0)
188  scale_fin = bgf;
189  else
190  scale_fin = 1.0_prt;
191  if( j % 2 == 0)
192  scale_in = bgi;
193  else
194  scale_in = 1.0_prt;
195  refpart.map(i, j) = refpart.map(i, j) * scale_in / scale_fin;
196  }
197  }
198 
199  // advance integrated path length
200  refpart.s = s + slice_ds;
201  }
202 
209  std::tuple<amrex::ParticleReal, amrex::ParticleReal, amrex::ParticleReal>
211  RF_Efield (amrex::ParticleReal const zeval) const
212  {
213  using namespace amrex::literals; // for _rt and _prt
214 
215  // specify constants
217  constexpr amrex::ParticleReal zlen = 1.31879807_prt;
218  constexpr amrex::ParticleReal zmid = zlen / 2.0_prt;
219  constexpr int ncoef = 25;
220 
221  // specify Fourier coefficients:
222  // Fourier coefficients for the on-axis longitudinal
223  // electric field Ez of the 9-cell TESLA superconducting
224  // cavity (default cavity model):
225  // https://journals.aps.org/prab/pdf/10.1103/PhysRevSTAB.3.092001.
226  // May be superseded by user-provided input values (to do).
227  constexpr std::array<amrex::ParticleReal, ncoef> cos_coef = {
228  0.1644024074311037,
229  -0.1324009958969339,
230  4.3443060026047219e-002,
231  8.5602654094946495e-002,
232  -0.2433578169042885,
233  0.5297150596779437,
234  0.7164884680963959,
235  -5.2579522442877296e-003,
236  -5.5025369142193678e-002,
237  4.6845673335028933e-002,
238  -2.3279346335638568e-002,
239  4.0800777539657775e-003,
240  4.1378326533752169e-003,
241  -2.5040533340490805e-003,
242  -4.0654981400000964e-003,
243  9.6630592067498289e-003,
244  -8.5275895985990214e-003,
245  -5.8078747006425020e-002,
246  -2.4044337836660403e-002,
247  1.0968240064697212e-002,
248  -3.4461179858301418e-003,
249  -8.1201564869443749e-004,
250  2.1438992904959380e-003,
251  -1.4997753525697276e-003,
252  1.8685171825676386e-004
253  };
254  constexpr std::array<amrex::ParticleReal, ncoef> sin_coef = {}; // all zero
255 
256  // compute on-axis electric field (z is relative to cavity midpoint)
257  amrex::ParticleReal efield = 0.0;
258  amrex::ParticleReal efieldp = 0.0;
259  amrex::ParticleReal efieldpp = 0.0;
260  amrex::ParticleReal efieldint = 0.0;
261  amrex::ParticleReal const z = zeval - zmid;
262 
263  if (abs(z)<=zmid)
264  {
265  efield = 0.5_prt*cos_coef[0];
266  efieldint = z*efield;
267  for (int j=1; j < ncoef; ++j)
268  {
269  efield = efield + cos_coef[j]*cos(j*2*pi*z/zlen) +
270  sin_coef[j]*sin(j*2*pi*z/zlen);
271  efieldp = efieldp-j*2*pi*cos_coef[j]*sin(j*2*pi*z/zlen)/zlen +
272  j*2*pi*sin_coef[j]*cos(j*2*pi*z/zlen)/zlen;
273  efieldpp = efieldpp- pow(j*2*pi*cos_coef[j]/zlen,2) *cos(j*2*pi*z/zlen) -
274  pow(j*2*pi*sin_coef[j]/zlen,2) *sin(j*2*pi*z/zlen);
275  efieldint = efieldint + zlen*cos_coef[j]*sin(j*2*pi*z/zlen)/(j*2*pi) -
276  zlen*sin_coef[j]*cos(j*2*pi*z/zlen)/(j*2*pi);
277  }
278  }
279  return std::make_tuple(efield, efieldp, efieldint);
280  }
281 
291  void map3 (amrex::ParticleReal const tau,
292  RefPart & refpart,
293  [[maybe_unused]] amrex::ParticleReal & zeval) const
294  {
295  using namespace amrex::literals; // for _rt and _prt
296 
297  // push the reference particle
298  amrex::ParticleReal const t = refpart.t;
299  amrex::ParticleReal const pt = refpart.pt;
300 
301  if (pt < -1.0_prt) {
302  refpart.t = t + tau/sqrt(1.0_prt - pow(pt, -2));
303  refpart.pt = pt;
304  }
305  else {
306  refpart.t = t;
307  refpart.pt = pt;
308  }
309 
310  // push the linear map equations
312  amrex::ParticleReal const betgam = refpart.beta_gamma();
313 
314  refpart.map(5,5) = R(5,5) + tau*R(6,5)/pow(betgam,3);
315  refpart.map(5,6) = R(5,6) + tau*R(6,6)/pow(betgam,3);
316  }
317 
327  void map2 (amrex::ParticleReal const tau,
328  RefPart & refpart,
329  amrex::ParticleReal & zeval) const
330  {
331  using namespace amrex::literals; // for _rt and _prt
332 
333  amrex::ParticleReal const t = refpart.t;
334  amrex::ParticleReal const pt = refpart.pt;
335 
336  // Define parameters and intermediate constants
339  amrex::ParticleReal const k = (2.0_prt*pi/c)*m_freq;
340  amrex::ParticleReal const phi = m_phase*(pi/180.0_prt);
341  amrex::ParticleReal const E0 = m_escale;
342 
343  // push the reference particle
344  auto [ez, ezp, ezint] = RF_Efield(zeval);
345  amrex::ignore_unused(ez, ezint);
346 
347  refpart.t = t;
348  refpart.pt = pt;
349 
350  // push the linear map equations
352  amrex::ParticleReal const s = tau/refpart.beta_gamma();
353  amrex::ParticleReal const L = E0*ezp*sin(k*t+phi)/(2.0_prt*k);
354 
355  refpart.map(1,1) = (1.0_prt-s*L)*R(1,1) + s*R(2,1);
356  refpart.map(1,2) = (1.0_prt-s*L)*R(1,2) + s*R(2,2);
357  refpart.map(2,1) = -s*pow(L,2)*R(1,1) + (1.0_prt+s*L)*R(2,1);
358  refpart.map(2,2) = -s*pow(L,2)*R(1,2) + (1.0_prt+s*L)*R(2,2);
359 
360  refpart.map(3,3) = (1.0_prt-s*L)*R(3,3) + s*R(4,3);
361  refpart.map(3,4) = (1.0_prt-s*L)*R(3,4) + s*R(4,4);
362  refpart.map(4,3) = -s*pow(L,2)*R(3,3) + (1.0_prt+s*L)*R(4,3);
363  refpart.map(4,4) = -s*pow(L,2)*R(3,4) + (1.0_prt+s*L)*R(4,4);
364  }
365 
375  void map1 (amrex::ParticleReal const tau,
376  RefPart & refpart,
377  amrex::ParticleReal & zeval) const
378  {
379  using namespace amrex::literals; // for _rt and _prt
380 
381  amrex::ParticleReal const t = refpart.t;
382  amrex::ParticleReal const pt = refpart.pt;
383  amrex::ParticleReal const z = zeval;
384 
385  // Define parameters and intermediate constants
388  amrex::ParticleReal const k = (2.0_prt*pi/c)*m_freq;
389  amrex::ParticleReal const phi = m_phase*(pi/180.0_prt);
390  amrex::ParticleReal const E0 = m_escale;
391 
392  // push the reference particle
393  auto [ez, ezp, ezint] = RF_Efield(z);
395  zeval = z + tau;
396  auto [ezf, ezpf, ezintf] = RF_Efield(zeval);
398 
399  refpart.t = t;
400  refpart.pt = pt - E0*(ezintf-ezint)*cos(k*t+phi);
401 
402  // push the linear map equations
404  amrex::ParticleReal const M = E0*(ezintf-ezint)*k*sin(k*t+phi);
405  amrex::ParticleReal const L = E0*(ezpf-ezp)*sin(k*t+phi)/(2.0_prt*k)+M/2.0_prt;
406 
407  refpart.map(2,1) = L*R(1,1) + R(2,1);
408  refpart.map(2,2) = L*R(1,2) + R(2,2);
409 
410  refpart.map(4,3) = L*R(3,3) + R(4,3);
411  refpart.map(4,4) = L*R(3,4) + R(4,4);
412 
413  refpart.map(6,5) = M*R(5,5) + R(6,5);
414  refpart.map(6,6) = M*R(5,6) + R(6,6);
415  }
416 
417  private:
418  amrex::ParticleReal m_escale;
419  amrex::ParticleReal m_freq;
420  amrex::ParticleReal m_phase;
422  };
423 
424 } // namespace impactx
425 
426 #endif // IMPACTX_RFCAVITY_H
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void symp2_integrate_split3(RefPart &refpart, amrex::ParticleReal const zin, amrex::ParticleReal const zout, int const nsteps, T_Element const &element)
Definition: Integrators.H:53
constexpr std::enable_if_t< std::is_floating_point< T >::value, T > pi()
int m_mapsteps
RF driven phase in deg.
Definition: RFCavity.H:421
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(PType &AMREX_RESTRICT p, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py, amrex::ParticleReal &AMREX_RESTRICT pt, [[maybe_unused]] RefPart const &refpart) const
Definition: RFCavity.H:75
static constexpr auto c
Definition: ImpactX.cpp:31
RFCavity(amrex::ParticleReal const ds, amrex::ParticleReal const escale, amrex::ParticleReal const freq, amrex::ParticleReal const phase, int const mapsteps, int const nslice)
Definition: RFCavity.H:49
amrex::ParticleReal m_phase
RF frequency in Hz.
Definition: RFCavity.H:420
Definition: beamoptic.H:131
c
amrex::ParticleReal pt
energy deviation, normalized by rest energy
Definition: ReferenceParticle.H:39
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
amrex::ParticleReal t
clock time * c in meters
Definition: ReferenceParticle.H:35
int nsteps
i
Definition: thick.H:23
amrex::Array2D< amrex::ParticleReal, 1, 6, 1, 6 > map
linearized map
Definition: ReferenceParticle.H:44
int ez
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T abs(const GpuComplex< T > &a_z) noexcept
Thick(amrex::ParticleReal const ds, int const nslice)
Definition: thick.H:30
Definition: ReferenceParticle.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > pow(const GpuComplex< T > &a_z, const T &a_y) noexcept
amrex::ParticleReal m_escale
Definition: RFCavity.H:418
static constexpr auto name
Definition: RFCavity.H:36
s
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition: ReferenceParticle.H:79
std::tuple< amrex::ParticleReal, amrex::ParticleReal, amrex::ParticleReal > AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE RF_Efield(amrex::ParticleReal const zeval) const
Definition: RFCavity.H:211
static constexpr amrex::Real pi
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal ds() const
Definition: thick.H:50
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int nslice() const
Definition: thick.H:40
Definition: RFCavity.H:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map2(amrex::ParticleReal const tau, RefPart &refpart, amrex::ParticleReal &zeval) const
Definition: RFCavity.H:327
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map3(amrex::ParticleReal const tau, RefPart &refpart, [[maybe_unused]] amrex::ParticleReal &zeval) const
Definition: RFCavity.H:291
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map1(amrex::ParticleReal const tau, RefPart &refpart, amrex::ParticleReal &zeval) const
Definition: RFCavity.H:375
amrex::ParticleReal m_ds
Definition: thick.H:56
amrex::ParticleReal m_freq
scaling factor for RF electric field
Definition: RFCavity.H:419