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/beamoptic.H"
15 #include "mixin/thick.H"
16 #include "mixin/nofinalize.H"
17 
18 #include <AMReX_Extension.H>
19 #include <AMReX_REAL.H>
20 
21 #include <cmath>
22 
23 
24 namespace impactx
25 {
26  struct Quad
27  : public elements::BeamOptic<Quad>,
28  public elements::Thick,
30  {
31  static constexpr auto name = "Quad";
33 
43  Quad( amrex::ParticleReal const ds, amrex::ParticleReal const k,
44  int const nslice )
45  : Thick(ds, nslice), m_k(k)
46  {
47  }
48 
50  using BeamOptic::operator();
51 
62  PType& AMREX_RESTRICT p,
63  amrex::ParticleReal & AMREX_RESTRICT px,
64  amrex::ParticleReal & AMREX_RESTRICT py,
65  amrex::ParticleReal & AMREX_RESTRICT pt,
66  RefPart const & refpart) const {
67 
68  using namespace amrex::literals; // for _rt and _prt
69 
70  // access AoS data such as positions and cpu/id
71  amrex::ParticleReal const x = p.pos(RealAoS::x);
72  amrex::ParticleReal const y = p.pos(RealAoS::y);
73  amrex::ParticleReal const t = p.pos(RealAoS::t);
74 
75  // length of the current slice
76  amrex::ParticleReal const slice_ds = m_ds / nslice();
77 
78  // access reference particle values to find beta*gamma^2
79  amrex::ParticleReal const pt_ref = refpart.pt;
80  amrex::ParticleReal const betgam2 = pow(pt_ref, 2) - 1.0_prt;
81 
82  // compute phase advance per unit length in s (in rad/m)
83  amrex::ParticleReal const omega = sqrt(std::abs(m_k));
84 
85  // intialize output values of momenta
86  amrex::ParticleReal pxout = px;
87  amrex::ParticleReal pyout = py;
88  amrex::ParticleReal const ptout = pt;
89 
90  if(m_k > 0.0) {
91  // advance position and momentum (focusing quad)
92  p.pos(RealAoS::x) = cos(omega*slice_ds)*x + sin(omega*slice_ds)/omega*px;
93  pxout = -omega*sin(omega*slice_ds)*x + cos(omega*slice_ds)*px;
94 
95  p.pos(RealAoS::y) = cosh(omega*slice_ds)*y + sinh(omega*slice_ds)/omega*py;
96  pyout = omega*sinh(omega*slice_ds)*y + cosh(omega*slice_ds)*py;
97 
98  p.pos(RealAoS::t) = t + (slice_ds/betgam2)*pt;
99  // ptout = pt;
100  } else {
101  // advance position and momentum (defocusing quad)
102  p.pos(RealAoS::x) = cosh(omega*slice_ds)*x + sinh(omega*slice_ds)/omega*px;
103  pxout = omega*sinh(omega*slice_ds)*x + cosh(omega*slice_ds)*px;
104 
105  p.pos(RealAoS::y) = cos(omega*slice_ds)*y + sin(omega*slice_ds)/omega*py;
106  pyout = -omega*sin(omega*slice_ds)*y + cos(omega*slice_ds)*py;
107 
108  p.pos(RealAoS::t) = t + (slice_ds/betgam2)*pt;
109  // ptout = pt;
110  }
111 
112  // assign updated momenta
113  px = pxout;
114  py = pyout;
115  pt = ptout;
116 
117  }
118 
124  void operator() (RefPart & AMREX_RESTRICT refpart) const {
125 
126  using namespace amrex::literals; // for _rt and _prt
127 
128  // assign input reference particle values
129  amrex::ParticleReal const x = refpart.x;
130  amrex::ParticleReal const px = refpart.px;
131  amrex::ParticleReal const y = refpart.y;
132  amrex::ParticleReal const py = refpart.py;
133  amrex::ParticleReal const z = refpart.z;
134  amrex::ParticleReal const pz = refpart.pz;
135  amrex::ParticleReal const t = refpart.t;
136  amrex::ParticleReal const pt = refpart.pt;
137  amrex::ParticleReal const s = refpart.s;
138 
139  // length of the current slice
140  amrex::ParticleReal const slice_ds = m_ds / nslice();
141 
142  // assign intermediate parameter
143  amrex::ParticleReal const step = slice_ds / sqrt(pow(pt,2)-1.0_prt);
144 
145  // advance position and momentum (straight element)
146  refpart.x = x + step*px;
147  refpart.y = y + step*py;
148  refpart.z = z + step*pz;
149  refpart.t = t - step*pt;
150 
151  // advance integrated path length
152  refpart.s = s + slice_ds;
153  }
154 
155  private:
156  amrex::ParticleReal m_k;
157  };
158 
159 } // namespace impactx
160 
161 #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:34
s
Definition: Quad.H:30
amrex::ParticleReal m_k
Definition: Quad.H:156
Quad(amrex::ParticleReal const ds, amrex::ParticleReal const k, int const nslice)
Definition: Quad.H:43
static constexpr auto name
Definition: Quad.H:31
ImpactXParticleContainer::ParticleType PType
Definition: Quad.H:32
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:61
@ 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::ParticleReal pt
energy, normalized by rest energy
Definition: ReferenceParticle.H:39
Definition: beamoptic.H:135
Definition: nofinalize.H:22
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