ImpactX
ExactSbend.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_EXACTSBEND_H
11 #define IMPACTX_EXACTSBEND_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_Math.H>
21 #include <AMReX_REAL.H>
22 #include <AMReX_Print.H> // for PrintToFile
23 
24 #include <cmath>
25 
26 
27 namespace impactx
28 {
29  struct ExactSbend
30  : public elements::BeamOptic<ExactSbend>,
31  public elements::Thick,
32  public elements::Alignment,
34  {
35  static constexpr auto name = "ExactSbend";
36  static constexpr amrex::ParticleReal degree2rad = ablastr::constant::math::pi / 180.0;
38 
60  amrex::ParticleReal ds,
61  amrex::ParticleReal phi,
62  amrex::ParticleReal B,
63  amrex::ParticleReal dx = 0,
64  amrex::ParticleReal dy = 0,
65  amrex::ParticleReal rotation_degree = 0,
66  int nslice = 1
67  )
68  : Thick(ds, nslice),
69  Alignment(dx, dy, rotation_degree),
70  m_phi(phi * degree2rad), m_B(B)
71  {
72  }
73 
75  using BeamOptic::operator();
76 
90  amrex::ParticleReal & AMREX_RESTRICT x,
91  amrex::ParticleReal & AMREX_RESTRICT y,
92  amrex::ParticleReal & AMREX_RESTRICT t,
93  amrex::ParticleReal & AMREX_RESTRICT px,
94  amrex::ParticleReal & AMREX_RESTRICT py,
95  amrex::ParticleReal & AMREX_RESTRICT pt,
96  [[maybe_unused]] uint64_t & AMREX_RESTRICT idcpu,
97  RefPart const & refpart
98  ) const
99  {
100  using namespace amrex::literals; // for _rt and _prt
101 
102  // shift due to alignment errors of the element
103  shift_in(x, y, px, py);
104 
105  // access position data
106  amrex::ParticleReal const xout = x;
107  amrex::ParticleReal const yout = y;
108  amrex::ParticleReal const tout = t;
109 
110  // angle of arc for the current slice
111  amrex::ParticleReal const slice_phi = m_phi / nslice();
112 
113  // access reference particle values to find beta
114  amrex::ParticleReal const bet = refpart.beta();
115 
116  // reference particle's orbital radius
117  amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
118 
119  // intialize output values of momenta
120  amrex::ParticleReal pxout = px;
121  amrex::ParticleReal pyout = py;
122  amrex::ParticleReal ptout = pt;
123 
124  // assign intermediate quantities
125  amrex::ParticleReal const pperp = sqrt(pow(pt,2)-2.0_prt/bet*pt-pow(py,2)+1.0_prt);
126  amrex::ParticleReal const pzi = sqrt(pow(pperp,2)-pow(px,2));
127  amrex::ParticleReal const rho = rc + xout;
128  auto const [sin_phi, cos_phi] = amrex::Math::sincos(slice_phi);
129 
130  // update momenta
131  pxout = px*cos_phi + (pzi - rho/rc)*sin_phi;
132  pyout = py;
133  ptout = pt;
134 
135  // angle of momentum rotation
136  amrex::ParticleReal const pzf = sqrt(pow(pperp,2)-pow(pxout,2));
137  amrex::ParticleReal const theta = slice_phi + asin(px/pperp) - asin(pxout/pperp);
138 
139  // update position coordinates
140  x = -rc + rho*cos_phi + rc*(pzf + px*sin_phi - pzi*cos_phi);
141  y = yout + theta * rc * py;
142  t = tout - theta * rc * (pt - 1.0_prt / bet) - m_phi * rc / bet;
143 
144  // assign updated momenta
145  px = pxout;
146  py = pyout;
147  pt = ptout;
148 
149  // undo shift due to alignment errors of the element
150  shift_out(x, y, px, py);
151  }
152 
158  void operator() (RefPart & AMREX_RESTRICT refpart) const
159  {
160  using namespace amrex::literals; // for _rt and _prt
161 
162  // assign input reference particle values
163  amrex::ParticleReal const x = refpart.x;
164  amrex::ParticleReal const px = refpart.px;
165  amrex::ParticleReal const y = refpart.y;
166  amrex::ParticleReal const py = refpart.py;
167  amrex::ParticleReal const z = refpart.z;
168  amrex::ParticleReal const pz = refpart.pz;
169  amrex::ParticleReal const t = refpart.t;
170  amrex::ParticleReal const pt = refpart.pt;
171  amrex::ParticleReal const s = refpart.s;
172 
173  // length of the current slice
174  amrex::ParticleReal const slice_ds = m_ds / nslice();
175 
176  // assign intermediate parameters
177  amrex::ParticleReal const theta = m_phi / nslice();
178  amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
179  amrex::ParticleReal const B = refpart.beta_gamma() /rc;
180 
181  // calculate expensive terms once
182  auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
183 
184  // advance position and momentum (bend)
185  refpart.px = px*cos_theta - pz*sin_theta;
186  refpart.py = py;
187  refpart.pz = pz*cos_theta + px*sin_theta;
188  refpart.pt = pt;
189 
190  refpart.x = x + (refpart.pz - pz)/B;
191  refpart.y = y + (theta/B)*py;
192  refpart.z = z - (refpart.px - px)/B;
193  refpart.t = t - (theta/B)*pt;
194 
195  // advance integrated path length
196  refpart.s = s + slice_ds;
197 
198  }
199 
200  amrex::ParticleReal m_phi;
201  amrex::ParticleReal m_B;
202  };
203 
204 } // namespace impactx
205 
206 #endif // IMPACTX_EXACTSBEND_H
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
static constexpr amrex::Real pi
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::pair< double, double > sincos(double x)
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: ExactSbend.H:34
amrex::ParticleReal m_B
bend angle in radians
Definition: ExactSbend.H:201
ImpactXParticleContainer::ParticleType PType
Definition: ExactSbend.H:37
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: ExactSbend.H:89
ExactSbend(amrex::ParticleReal ds, amrex::ParticleReal phi, amrex::ParticleReal B, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, int nslice=1)
Definition: ExactSbend.H:59
amrex::ParticleReal m_phi
Definition: ExactSbend.H:200
static constexpr auto name
Definition: ExactSbend.H:35
static constexpr amrex::ParticleReal degree2rad
Definition: ExactSbend.H:36
Definition: ReferenceParticle.H:30
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition: ReferenceParticle.H:169
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition: ReferenceParticle.H:64
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