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 
75  PType& AMREX_RESTRICT p,
76  amrex::ParticleReal & AMREX_RESTRICT px,
77  amrex::ParticleReal & AMREX_RESTRICT py,
78  amrex::ParticleReal & AMREX_RESTRICT pt,
79  RefPart const & refpart
80  ) const
81  {
82  using namespace amrex::literals; // for _rt and _prt
83 
84  // shift due to alignment errors of the element
85  shift_in(p.pos(RealAoS::x), p.pos(RealAoS::y), px, py);
86 
87  // access AoS data such as positions and cpu/id
88  amrex::ParticleReal const x = p.pos(RealAoS::x);
89  amrex::ParticleReal const y = p.pos(RealAoS::y);
90  amrex::ParticleReal const t = p.pos(RealAoS::t);
91 
92  // length of the current slice
93  amrex::ParticleReal const slice_ds = m_ds / nslice();
94 
95  // access reference particle values to find beta*gamma^2
96  amrex::ParticleReal const pt_ref = refpart.pt;
97  amrex::ParticleReal const betgam2 = pow(pt_ref, 2) - 1.0_prt;
98 
99  // compute phase advance per unit length in s (in rad/m)
100  amrex::ParticleReal const omega = sqrt(std::abs(m_k));
101 
102  // intialize output values of momenta
103  amrex::ParticleReal pxout = px;
104  amrex::ParticleReal pyout = py;
105  amrex::ParticleReal const ptout = pt;
106 
107  if(m_k > 0.0) {
108  // advance position and momentum (focusing quad)
109  p.pos(RealAoS::x) = cos(omega*slice_ds)*x + sin(omega*slice_ds)/omega*px;
110  pxout = -omega*sin(omega*slice_ds)*x + cos(omega*slice_ds)*px;
111 
112  p.pos(RealAoS::y) = cosh(omega*slice_ds)*y + sinh(omega*slice_ds)/omega*py;
113  pyout = omega*sinh(omega*slice_ds)*y + cosh(omega*slice_ds)*py;
114 
115  p.pos(RealAoS::t) = t + (slice_ds/betgam2)*pt;
116  // ptout = pt;
117  } else {
118  // advance position and momentum (defocusing quad)
119  p.pos(RealAoS::x) = cosh(omega*slice_ds)*x + sinh(omega*slice_ds)/omega*px;
120  pxout = omega*sinh(omega*slice_ds)*x + cosh(omega*slice_ds)*px;
121 
122  p.pos(RealAoS::y) = cos(omega*slice_ds)*y + sin(omega*slice_ds)/omega*py;
123  pyout = -omega*sin(omega*slice_ds)*y + cos(omega*slice_ds)*py;
124 
125  p.pos(RealAoS::t) = t + (slice_ds/betgam2)*pt;
126  // ptout = pt;
127  }
128 
129  // assign updated momenta
130  px = pxout;
131  py = pyout;
132  pt = ptout;
133 
134  // undo shift due to alignment errors of the element
135  shift_out(p.pos(RealAoS::x), p.pos(RealAoS::y), px, py);
136  }
137 
143  void operator() (RefPart & AMREX_RESTRICT refpart) const {
144 
145  using namespace amrex::literals; // for _rt and _prt
146 
147  // assign input reference particle values
148  amrex::ParticleReal const x = refpart.x;
149  amrex::ParticleReal const px = refpart.px;
150  amrex::ParticleReal const y = refpart.y;
151  amrex::ParticleReal const py = refpart.py;
152  amrex::ParticleReal const z = refpart.z;
153  amrex::ParticleReal const pz = refpart.pz;
154  amrex::ParticleReal const t = refpart.t;
155  amrex::ParticleReal const pt = refpart.pt;
156  amrex::ParticleReal const s = refpart.s;
157 
158  // length of the current slice
159  amrex::ParticleReal const slice_ds = m_ds / nslice();
160 
161  // assign intermediate parameter
162  amrex::ParticleReal const step = slice_ds / sqrt(pow(pt,2)-1.0_prt);
163 
164  // advance position and momentum (straight element)
165  refpart.x = x + step*px;
166  refpart.y = y + step*py;
167  refpart.z = z + step*pz;
168  refpart.t = t - step*pt;
169 
170  // advance integrated path length
171  refpart.s = s + slice_ds;
172  }
173 
174  private:
175  amrex::ParticleReal m_k;
176  };
177 
178 } // namespace impactx
179 
180 #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:36
@ 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::ParticleReal m_k
Definition: Quad.H:175
static constexpr auto name
Definition: Quad.H:33
ImpactXParticleContainer::ParticleType PType
Definition: Quad.H:34
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, RefPart const &refpart) const
Definition: Quad.H:74
@ x
position in x [m] (at fixed s OR fixed t)
Definition: ImpactXParticleContainer.H:48
@ y
position in y [m] (at fixed s OR fixed t)
Definition: ImpactXParticleContainer.H:49
@ t
c * time-of-flight [m] (at fixed s)
Definition: ImpactXParticleContainer.H:50
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:135
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