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 <stdexcept>
28 #include <tuple>
29 #include <vector>
30 
31 
32 namespace impactx
33 {
44  {
46  0.1644024074311037,
47  -0.1324009958969339,
48  4.3443060026047219e-002,
49  8.5602654094946495e-002,
50  -0.2433578169042885,
51  0.5297150596779437,
52  0.7164884680963959,
53  -5.2579522442877296e-003,
54  -5.5025369142193678e-002,
55  4.6845673335028933e-002,
56  -2.3279346335638568e-002,
57  4.0800777539657775e-003,
58  4.1378326533752169e-003,
59  -2.5040533340490805e-003,
60  -4.0654981400000964e-003,
61  9.6630592067498289e-003,
62  -8.5275895985990214e-003,
63  -5.8078747006425020e-002,
64  -2.4044337836660403e-002,
65  1.0968240064697212e-002,
66  -3.4461179858301418e-003,
67  -8.1201564869443749e-004,
68  2.1438992904959380e-003,
69  -1.4997753525697276e-003,
70  1.8685171825676386e-004
71  };
72 
74  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76  0, 0, 0
77  };
78  };
79 
86 namespace RFCavityData
87 {
89  static inline int next_id = 0;
90 
92  static inline std::map<int, std::vector<amrex::ParticleReal>> h_cos_coef = {};
94  static inline std::map<int, std::vector<amrex::ParticleReal>> h_sin_coef = {};
95 
97  static inline std::map<int, amrex::Gpu::DeviceVector<amrex::ParticleReal>> d_cos_coef = {};
99  static inline std::map<int, amrex::Gpu::DeviceVector<amrex::ParticleReal>> d_sin_coef = {};
100 
101 } // namespace RFCavityData
102 
103  struct RFCavity
104  : public elements::BeamOptic<RFCavity>,
105  public elements::Thick
106  {
107  static constexpr auto name = "RFCavity";
109 
123  amrex::ParticleReal ds,
124  amrex::ParticleReal escale,
125  amrex::ParticleReal freq,
126  amrex::ParticleReal phase,
127  std::vector<amrex::ParticleReal> cos_coef,
128  std::vector<amrex::ParticleReal> sin_coef,
129  int mapsteps = 1,
130  int nslice = 1
131  )
132  : Thick(ds, nslice),
133  m_escale(escale), m_freq(freq), m_phase(phase), m_mapsteps(mapsteps)
134  {
135  // next created RF cavity has another id for its data
137 
138  // validate sin and cos coefficients are the same length
139  m_ncoef = cos_coef.size();
140  if (m_ncoef != int(sin_coef.size()))
141  throw std::runtime_error("RFCavity: cos and sin coefficients must have same length!");
142 
143  // host data
144  RFCavityData::h_cos_coef[m_id] = cos_coef;
145  RFCavityData::h_sin_coef[m_id] = sin_coef;
148 
149  // device data
153  cos_coef.begin(), cos_coef.end(),
154  RFCavityData::d_cos_coef[m_id].begin());
156  sin_coef.begin(), sin_coef.end(),
157  RFCavityData::d_sin_coef[m_id].begin());
159 
160  // low-level objects we can use on device
163  }
164 
166  using BeamOptic::operator();
167 
179  PType& AMREX_RESTRICT p,
180  amrex::ParticleReal & AMREX_RESTRICT px,
181  amrex::ParticleReal & AMREX_RESTRICT py,
182  amrex::ParticleReal & AMREX_RESTRICT pt,
183  [[maybe_unused]] RefPart const & refpart
184  ) const
185  {
186  using namespace amrex::literals; // for _rt and _prt
187 
188  // access AoS data such as positions and cpu/id
189  amrex::ParticleReal const x = p.pos(RealAoS::x);
190  amrex::ParticleReal const y = p.pos(RealAoS::y);
191  amrex::ParticleReal const t = p.pos(RealAoS::t);
192 
193  // initialize output values of momenta
194  amrex::ParticleReal pxout = px;
195  amrex::ParticleReal pyout = py;
196  amrex::ParticleReal ptout = pt;
197 
198  // get the linear map
200 
201  // symplectic linear map for the RF cavity is computed using the
202  // Hamiltonian formalism as described in:
203  // https://uspas.fnal.gov/materials/09UNM/ComputationalMethods.pdf.
204  // R denotes the transfer matrix in the basis (x,px,y,py,t,pt),
205  // so that, e.g., R(3,4) = dyf/dpyi.
206 
207  // push particles using the linear map
208  p.pos(RealAoS::x) = R(1,1)*x + R(1,2)*px + R(1,3)*y
209  + R(1,4)*py + R(1,5)*t + R(1,6)*pt;
210  pxout = R(2,1)*x + R(2,2)*px + R(2,3)*y
211  + R(2,4)*py + R(2,5)*t + R(2,6)*pt;
212  p.pos(RealAoS::y) = R(3,1)*x + R(3,2)*px + R(3,3)*y
213  + R(3,4)*py + R(3,5)*t + R(3,6)*pt;
214  pyout = R(4,1)*x + R(4,2)*px + R(4,3)*y
215  + R(4,4)*py + R(4,5)*t + R(4,6)*pt;
216  p.pos(RealAoS::t) = R(5,1)*x + R(5,2)*px + R(5,3)*y
217  + R(5,4)*py + R(5,5)*t + R(5,6)*pt;
218  ptout = R(6,1)*x + R(6,2)*px + R(6,3)*y
219  + R(6,4)*py + R(6,5)*t + R(6,6)*pt;
220 
221  // assign updated momenta
222  px = pxout;
223  py = pyout;
224  pt = ptout;
225  }
226 
232  void operator() (RefPart & AMREX_RESTRICT refpart) const
233  {
234  using namespace amrex::literals; // for _rt and _prt
235 
236  // assign input reference particle values
237  amrex::ParticleReal const x = refpart.x;
238  amrex::ParticleReal const px = refpart.px;
239  amrex::ParticleReal const y = refpart.y;
240  amrex::ParticleReal const py = refpart.py;
241  amrex::ParticleReal const z = refpart.z;
242  amrex::ParticleReal const pz = refpart.pz;
243  amrex::ParticleReal const pt = refpart.pt;
244  amrex::ParticleReal const s = refpart.s;
245  amrex::ParticleReal const sedge = refpart.sedge;
246 
247  // initialize linear map (deviation) values
248  for (int i=1; i<7; i++) {
249  for (int j=1; j<7; j++) {
250  if (i == j)
251  refpart.map(i, j) = 1.0_prt;
252  else
253  refpart.map(i, j) = 0.0_prt;
254  }
255  }
256 
257  // length of the current slice
258  amrex::ParticleReal const slice_ds = m_ds / nslice();
259 
260  // compute intial value of beta*gamma
261  amrex::ParticleReal const bgi = sqrt(pow(pt, 2) - 1.0_prt);
262 
263  // call integrator to advance (t,pt)
264  amrex::ParticleReal const zin = s - sedge;
265  amrex::ParticleReal const zout = zin + slice_ds;
266  int const nsteps = m_mapsteps;
267 
268  integrators::symp2_integrate_split3(refpart,zin,zout,nsteps,*this);
269  amrex::ParticleReal const ptf = refpart.pt;
270 
271  // advance position (x,y,z)
272  refpart.x = x + slice_ds*px/bgi;
273  refpart.y = y + slice_ds*py/bgi;
274  refpart.z = z + slice_ds*pz/bgi;
275 
276  // compute final value of beta*gamma
277  amrex::ParticleReal const bgf = sqrt(pow(ptf, 2) - 1.0_prt);
278 
279  // advance momentum (px,py,pz)
280  refpart.px = px*bgf/bgi;
281  refpart.py = py*bgf/bgi;
282  refpart.pz = pz*bgf/bgi;
283 
284  // convert linear map from dynamic to static units
285  amrex::ParticleReal scale_in = 1.0_prt;
286  amrex::ParticleReal scale_fin = 1.0_prt;
287 
288  for (int i=1; i<7; i++) {
289  for (int j=1; j<7; j++) {
290  if( i % 2 == 0)
291  scale_fin = bgf;
292  else
293  scale_fin = 1.0_prt;
294  if( j % 2 == 0)
295  scale_in = bgi;
296  else
297  scale_in = 1.0_prt;
298  refpart.map(i, j) = refpart.map(i, j) * scale_in / scale_fin;
299  }
300  }
301 
302  // advance integrated path length
303  refpart.s = s + slice_ds;
304  }
305 
312  std::tuple<amrex::ParticleReal, amrex::ParticleReal, amrex::ParticleReal>
314  RF_Efield (amrex::ParticleReal const zeval) const
315  {
316  using namespace amrex::literals; // for _rt and _prt
317 
318  // pick the right data depending if we are on the host side
319  // (reference particle push) or device side (particles):
320 #if AMREX_DEVICE_COMPILE
321  amrex::ParticleReal* cos_data = m_cos_d_data;
322  amrex::ParticleReal* sin_data = m_sin_d_data;
323 #else
324  amrex::ParticleReal* cos_data = m_cos_h_data;
325  amrex::ParticleReal* sin_data = m_sin_h_data;
326 #endif
327 
328  // specify constants
330  amrex::ParticleReal const zlen = m_ds;
331  amrex::ParticleReal const zmid = zlen / 2.0_prt;
332 
333  // compute on-axis electric field (z is relative to cavity midpoint)
334  amrex::ParticleReal efield = 0.0;
335  amrex::ParticleReal efieldp = 0.0;
336  amrex::ParticleReal efieldpp = 0.0;
337  amrex::ParticleReal efieldint = 0.0;
338  amrex::ParticleReal const z = zeval - zmid;
339 
340  if (std::abs(z) <= zmid)
341  {
342  efield = 0.5_prt*cos_data[0];
343  efieldint = z*efield;
344  for (int j=1; j < m_ncoef; ++j)
345  {
346  efield = efield + cos_data[j]*cos(j*2*pi*z/zlen) +
347  sin_data[j]*sin(j*2*pi*z/zlen);
348  efieldp = efieldp-j*2*pi*cos_data[j]*sin(j*2*pi*z/zlen)/zlen +
349  j*2*pi*sin_data[j]*cos(j*2*pi*z/zlen)/zlen;
350  efieldpp = efieldpp- pow(j*2*pi*cos_data[j]/zlen,2) *cos(j*2*pi*z/zlen) -
351  pow(j*2*pi*sin_data[j]/zlen,2) *sin(j*2*pi*z/zlen);
352  efieldint = efieldint + zlen*cos_data[j]*sin(j*2*pi*z/zlen)/(j*2*pi) -
353  zlen*sin_data[j]*cos(j*2*pi*z/zlen)/(j*2*pi);
354  }
355  }
356  return std::make_tuple(efield, efieldp, efieldint);
357  }
358 
368  void map3 (amrex::ParticleReal const tau,
369  RefPart & refpart,
370  [[maybe_unused]] amrex::ParticleReal & zeval) const
371  {
372  using namespace amrex::literals; // for _rt and _prt
373 
374  // push the reference particle
375  amrex::ParticleReal const t = refpart.t;
376  amrex::ParticleReal const pt = refpart.pt;
377 
378  if (pt < -1.0_prt) {
379  refpart.t = t + tau/sqrt(1.0_prt - pow(pt, -2));
380  refpart.pt = pt;
381  }
382  else {
383  refpart.t = t;
384  refpart.pt = pt;
385  }
386 
387  // push the linear map equations
389  amrex::ParticleReal const betgam = refpart.beta_gamma();
390 
391  refpart.map(5,5) = R(5,5) + tau*R(6,5)/pow(betgam,3);
392  refpart.map(5,6) = R(5,6) + tau*R(6,6)/pow(betgam,3);
393  }
394 
404  void map2 (amrex::ParticleReal const tau,
405  RefPart & refpart,
406  amrex::ParticleReal & zeval) const
407  {
408  using namespace amrex::literals; // for _rt and _prt
409 
410  amrex::ParticleReal const t = refpart.t;
411  amrex::ParticleReal const pt = refpart.pt;
412 
413  // Define parameters and intermediate constants
416  amrex::ParticleReal const k = (2.0_prt*pi/c)*m_freq;
417  amrex::ParticleReal const phi = m_phase*(pi/180.0_prt);
418  amrex::ParticleReal const E0 = m_escale;
419 
420  // push the reference particle
421  auto [ez, ezp, ezint] = RF_Efield(zeval);
422  amrex::ignore_unused(ez, ezint);
423 
424  refpart.t = t;
425  refpart.pt = pt;
426 
427  // push the linear map equations
429  amrex::ParticleReal const s = tau/refpart.beta_gamma();
430  amrex::ParticleReal const L = E0*ezp*sin(k*t+phi)/(2.0_prt*k);
431 
432  refpart.map(1,1) = (1.0_prt-s*L)*R(1,1) + s*R(2,1);
433  refpart.map(1,2) = (1.0_prt-s*L)*R(1,2) + s*R(2,2);
434  refpart.map(2,1) = -s*pow(L,2)*R(1,1) + (1.0_prt+s*L)*R(2,1);
435  refpart.map(2,2) = -s*pow(L,2)*R(1,2) + (1.0_prt+s*L)*R(2,2);
436 
437  refpart.map(3,3) = (1.0_prt-s*L)*R(3,3) + s*R(4,3);
438  refpart.map(3,4) = (1.0_prt-s*L)*R(3,4) + s*R(4,4);
439  refpart.map(4,3) = -s*pow(L,2)*R(3,3) + (1.0_prt+s*L)*R(4,3);
440  refpart.map(4,4) = -s*pow(L,2)*R(3,4) + (1.0_prt+s*L)*R(4,4);
441  }
442 
452  void map1 (amrex::ParticleReal const tau,
453  RefPart & refpart,
454  amrex::ParticleReal & zeval) const
455  {
456  using namespace amrex::literals; // for _rt and _prt
457 
458  amrex::ParticleReal const t = refpart.t;
459  amrex::ParticleReal const pt = refpart.pt;
460  amrex::ParticleReal const z = zeval;
461 
462  // Define parameters and intermediate constants
465  amrex::ParticleReal const k = (2.0_prt*pi/c)*m_freq;
466  amrex::ParticleReal const phi = m_phase*(pi/180.0_prt);
467  amrex::ParticleReal const E0 = m_escale;
468 
469  // push the reference particle
470  auto [ez, ezp, ezint] = RF_Efield(z);
472  zeval = z + tau;
473  auto [ezf, ezpf, ezintf] = RF_Efield(zeval);
475 
476  refpart.t = t;
477  refpart.pt = pt - E0*(ezintf-ezint)*cos(k*t+phi);
478 
479  // push the linear map equations
481  amrex::ParticleReal const M = E0*(ezintf-ezint)*k*sin(k*t+phi);
482  amrex::ParticleReal const L = E0*(ezpf-ezp)*sin(k*t+phi)/(2.0_prt*k)+M/2.0_prt;
483 
484  refpart.map(2,1) = L*R(1,1) + R(2,1);
485  refpart.map(2,2) = L*R(1,2) + R(2,2);
486 
487  refpart.map(4,3) = L*R(3,3) + R(4,3);
488  refpart.map(4,4) = L*R(3,4) + R(4,4);
489 
490  refpart.map(6,5) = M*R(5,5) + R(6,5);
491  refpart.map(6,6) = M*R(5,6) + R(6,6);
492  }
493 
496  void
498  {
499  // remove from unique data map
504 
509  }
510 
511  private:
512  amrex::ParticleReal m_escale;
513  amrex::ParticleReal m_freq;
514  amrex::ParticleReal m_phase;
516  int m_id;
517 
518  int m_ncoef = 0;
519  amrex::ParticleReal* m_cos_h_data = nullptr;
520  amrex::ParticleReal* m_sin_h_data = nullptr;
521  amrex::ParticleReal* m_cos_d_data = nullptr;
522  amrex::ParticleReal* m_sin_d_data = nullptr;
523  };
524 
525 } // namespace impactx
526 
527 #endif // IMPACTX_RFCAVITY_H
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
static constexpr auto c
static constexpr amrex::Real pi
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
static constexpr HostToDevice hostToDevice
void streamSynchronize() noexcept
constexpr std::enable_if_t< std::is_floating_point< T >::value, T > pi()
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
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
i
static std::map< int, std::vector< amrex::ParticleReal > > h_cos_coef
host: cosine coefficients in Fourier expansion of on-axis electric field Ez
Definition: RFCavity.H:92
static std::map< int, std::vector< amrex::ParticleReal > > h_sin_coef
host: sine coefficients in Fourier expansion of on-axis electric field Ez
Definition: RFCavity.H:94
static std::map< int, amrex::Gpu::DeviceVector< amrex::ParticleReal > > d_sin_coef
device: sine coefficients in Fourier expansion of on-axis electric field Ez
Definition: RFCavity.H:99
static std::map< int, amrex::Gpu::DeviceVector< amrex::ParticleReal > > d_cos_coef
device: cosine coefficients in Fourier expansion of on-axis electric field Ez
Definition: RFCavity.H:97
static int next_id
last used id for a created RF cavity
Definition: RFCavity.H:89
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
Definition: ImpactX.cpp:33
s
c
int nsteps
int count
Definition: RFCavity.H:44
amrex::Vector< amrex::ParticleReal > default_sin_coef
Definition: RFCavity.H:73
amrex::Vector< amrex::ParticleReal > default_cos_coef
Definition: RFCavity.H:45
Definition: RFCavity.H:106
amrex::ParticleReal m_escale
Definition: RFCavity.H:512
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:178
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map2(amrex::ParticleReal const tau, RefPart &refpart, amrex::ParticleReal &zeval) const
Definition: RFCavity.H:404
amrex::ParticleReal * m_cos_h_data
number of Fourier coefficients
Definition: RFCavity.H:519
amrex::ParticleReal m_phase
RF frequency in Hz.
Definition: RFCavity.H:514
RFCavity(amrex::ParticleReal ds, amrex::ParticleReal escale, amrex::ParticleReal freq, amrex::ParticleReal phase, std::vector< amrex::ParticleReal > cos_coef, std::vector< amrex::ParticleReal > sin_coef, int mapsteps=1, int nslice=1)
Definition: RFCavity.H:122
void finalize()
Definition: RFCavity.H:497
amrex::ParticleReal * m_cos_d_data
non-owning pointer to host sine coefficients
Definition: RFCavity.H:521
int m_mapsteps
RF driven phase in deg.
Definition: RFCavity.H:515
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map1(amrex::ParticleReal const tau, RefPart &refpart, amrex::ParticleReal &zeval) const
Definition: RFCavity.H:452
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map3(amrex::ParticleReal const tau, RefPart &refpart, [[maybe_unused]] amrex::ParticleReal &zeval) const
Definition: RFCavity.H:368
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:314
static constexpr auto name
Definition: RFCavity.H:107
amrex::ParticleReal * m_sin_d_data
non-owning pointer to device cosine coefficients
Definition: RFCavity.H:522
ImpactXParticleContainer::ParticleType PType
Definition: RFCavity.H:108
int m_id
number of map integration steps per slice
Definition: RFCavity.H:516
amrex::ParticleReal * m_sin_h_data
non-owning pointer to host cosine coefficients
Definition: RFCavity.H:520
amrex::ParticleReal m_freq
scaling factor for RF electric field
Definition: RFCavity.H:513
int m_ncoef
unique RF cavity id used for data lookup map
Definition: RFCavity.H:518
@ x
position in x [m] (at fixed s OR fixed t)
Definition: ImpactXParticleContainer.H:42
@ y
position in y [m] (at fixed s OR fixed t)
Definition: ImpactXParticleContainer.H:43
@ t
c * time-of-flight [m] (at fixed s)
Definition: ImpactXParticleContainer.H:44
Definition: ReferenceParticle.H:30
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition: ReferenceParticle.H:79
amrex::ParticleReal pt
energy deviation, normalized by rest energy
Definition: ReferenceParticle.H:39
amrex::Array2D< amrex::ParticleReal, 1, 6, 1, 6 > map
linearized map
Definition: ReferenceParticle.H:44
amrex::ParticleReal t
clock time * c in meters
Definition: ReferenceParticle.H:35
Definition: beamoptic.H:135
Definition: thick.H:24
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
amrex::ParticleReal m_ds
Definition: thick.H:56
Thick(amrex::ParticleReal const ds, int const nslice)
Definition: thick.H:30