ImpactX
Quad.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_QUAD_H
11 #define IMPACTX_QUAD_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 {
27  struct Quad
28  : public elements::BeamOptic<Quad>,
29  public elements::Thick,
30  public elements::Alignment,
32  {
33  static constexpr auto name = "Quad";
35 
48  Quad (
49  amrex::ParticleReal ds,
50  amrex::ParticleReal k,
51  amrex::ParticleReal dx = 0,
52  amrex::ParticleReal dy = 0,
53  amrex::ParticleReal rotation_degree = 0,
54  int nslice = 1
55  )
56  : Thick(ds, nslice),
57  Alignment(dx, dy, rotation_degree),
58  m_k(k)
59  {
60  }
61 
63  using BeamOptic::operator();
64 
78  amrex::ParticleReal & AMREX_RESTRICT x,
79  amrex::ParticleReal & AMREX_RESTRICT y,
80  amrex::ParticleReal & AMREX_RESTRICT t,
81  amrex::ParticleReal & AMREX_RESTRICT px,
82  amrex::ParticleReal & AMREX_RESTRICT py,
83  amrex::ParticleReal & AMREX_RESTRICT pt,
84  [[maybe_unused]] uint64_t & AMREX_RESTRICT idcpu,
85  RefPart const & refpart
86  ) const
87  {
88  using namespace amrex::literals; // for _rt and _prt
89 
90  // shift due to alignment errors of the element
91  shift_in(x, y, px, py);
92 
93  // length of the current slice
94  amrex::ParticleReal const slice_ds = m_ds / nslice();
95 
96  // access reference particle values to find beta*gamma^2
97  amrex::ParticleReal const pt_ref = refpart.pt;
98  amrex::ParticleReal const betgam2 = pow(pt_ref, 2) - 1.0_prt;
99 
100  // compute phase advance per unit length in s (in rad/m)
101  amrex::ParticleReal const omega = sqrt(std::abs(m_k));
102 
103  // intialize output values
104  amrex::ParticleReal xout = x;
105  amrex::ParticleReal yout = y;
106  amrex::ParticleReal tout = t;
107  amrex::ParticleReal pxout = px;
108  amrex::ParticleReal pyout = py;
109  amrex::ParticleReal const ptout = pt;
110 
111  if(m_k > 0.0) {
112  // advance position and momentum (focusing quad)
113  xout = cos(omega*slice_ds)*x + sin(omega*slice_ds)/omega*px;
114  pxout = -omega*sin(omega*slice_ds)*x + cos(omega*slice_ds)*px;
115 
116  yout = cosh(omega*slice_ds)*y + sinh(omega*slice_ds)/omega*py;
117  pyout = omega*sinh(omega*slice_ds)*y + cosh(omega*slice_ds)*py;
118 
119  tout = t + (slice_ds/betgam2)*pt;
120  // ptout = pt;
121  } else {
122  // advance position and momentum (defocusing quad)
123  xout = cosh(omega*slice_ds)*x + sinh(omega*slice_ds)/omega*px;
124  pxout = omega*sinh(omega*slice_ds)*x + cosh(omega*slice_ds)*px;
125 
126  yout = cos(omega*slice_ds)*y + sin(omega*slice_ds)/omega*py;
127  pyout = -omega*sin(omega*slice_ds)*y + cos(omega*slice_ds)*py;
128 
129  tout = t + (slice_ds/betgam2)*pt;
130  // ptout = pt;
131  }
132 
133  // assign updated values
134  x = xout;
135  y = yout;
136  t = tout;
137  px = pxout;
138  py = pyout;
139  pt = ptout;
140 
141  // undo shift due to alignment errors of the element
142  shift_out(x, y, px, py);
143  }
144 
150  void operator() (RefPart & AMREX_RESTRICT refpart) const {
151 
152  using namespace amrex::literals; // for _rt and _prt
153 
154  // assign input reference particle values
155  amrex::ParticleReal const x = refpart.x;
156  amrex::ParticleReal const px = refpart.px;
157  amrex::ParticleReal const y = refpart.y;
158  amrex::ParticleReal const py = refpart.py;
159  amrex::ParticleReal const z = refpart.z;
160  amrex::ParticleReal const pz = refpart.pz;
161  amrex::ParticleReal const t = refpart.t;
162  amrex::ParticleReal const pt = refpart.pt;
163  amrex::ParticleReal const s = refpart.s;
164 
165  // length of the current slice
166  amrex::ParticleReal const slice_ds = m_ds / nslice();
167 
168  // assign intermediate parameter
169  amrex::ParticleReal const step = slice_ds / sqrt(pow(pt,2)-1.0_prt);
170 
171  // advance position and momentum (straight element)
172  refpart.x = x + step*px;
173  refpart.y = y + step*py;
174  refpart.z = z + step*pz;
175  refpart.t = t - step*pt;
176 
177  // advance integrated path length
178  refpart.s = s + slice_ds;
179  }
180 
181  amrex::ParticleReal m_k;
182  };
183 
184 } // namespace impactx
185 
186 #endif // IMPACTX_QUAD_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
Definition: Quad.H:32
Quad(amrex::ParticleReal ds, amrex::ParticleReal k, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, int nslice=1)
Definition: Quad.H:48
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: Quad.H:77
amrex::ParticleReal m_k
Definition: Quad.H:181
static constexpr auto name
Definition: Quad.H:33
ImpactXParticleContainer::ParticleType PType
Definition: Quad.H:34
Definition: ReferenceParticle.H:30
amrex::ParticleReal pt
energy, normalized by rest energy
Definition: ReferenceParticle.H:39
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