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 
87  PType& AMREX_RESTRICT p,
88  amrex::ParticleReal & AMREX_RESTRICT px,
89  amrex::ParticleReal & AMREX_RESTRICT py,
90  amrex::ParticleReal & AMREX_RESTRICT pt,
91  RefPart const & refpart) const {
92 
93  using namespace amrex::literals; // for _rt and _prt
94 
95  // shift due to alignment errors of the element
96  shift_in(p.pos(RealAoS::x), p.pos(RealAoS::y), px, py);
97 
98  // access AoS data such as positions and cpu/id
99  amrex::ParticleReal const x = p.pos(RealAoS::x);
100  amrex::ParticleReal const y = p.pos(RealAoS::y);
101  amrex::ParticleReal const t = p.pos(RealAoS::t);
102 
103  // angle of arc for the current slice
104  amrex::ParticleReal const slice_phi = m_phi / nslice();
105 
106  // access reference particle values to find beta
107  amrex::ParticleReal const bet = refpart.beta();
108 
109  // reference particle's orbital radius
110  amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
111 
112  // intialize output values of momenta
113  amrex::ParticleReal pxout = px;
114  amrex::ParticleReal pyout = py;
115  amrex::ParticleReal ptout = pt;
116 
117  // assign intermediate quantities
118  amrex::ParticleReal const pperp = sqrt(pow(pt,2)-2.0_prt/bet*pt-pow(py,2)+1.0_prt);
119  amrex::ParticleReal const pzi = sqrt(pow(pperp,2)-pow(px,2));
120  amrex::ParticleReal const rho = rc + x;
121  auto const [sin_phi, cos_phi] = amrex::Math::sincos(slice_phi);
122 
123  // update momenta
124  pxout = px*cos_phi + (pzi - rho/rc)*sin_phi;
125  pyout = py;
126  ptout = pt;
127 
128  // angle of momentum rotation
129  amrex::ParticleReal const pzf = sqrt(pow(pperp,2)-pow(pxout,2));
130  amrex::ParticleReal const theta = slice_phi + asin(px/pperp) - asin(pxout/pperp);
131 
132  // update position coordinates
133  p.pos(RealAoS::x) = -rc + rho*cos_phi + rc*(pzf + px*sin_phi - pzi*cos_phi);
134  p.pos(RealAoS::y) = y + theta*rc*py;
135  p.pos(RealAoS::t) = t - theta*rc*(pt - 1.0_prt/bet) - m_phi*rc/bet;
136 
137  // assign updated momenta
138  px = pxout;
139  py = pyout;
140  pt = ptout;
141 
142  // undo shift due to alignment errors of the element
143  shift_out(p.pos(RealAoS::x), p.pos(RealAoS::y), px, py);
144  }
145 
151  void operator() (RefPart & AMREX_RESTRICT refpart) const {
152 
153  using namespace amrex::literals; // for _rt and _prt
154 
155  // assign input reference particle values
156  amrex::ParticleReal const x = refpart.x;
157  amrex::ParticleReal const px = refpart.px;
158  amrex::ParticleReal const y = refpart.y;
159  amrex::ParticleReal const py = refpart.py;
160  amrex::ParticleReal const z = refpart.z;
161  amrex::ParticleReal const pz = refpart.pz;
162  amrex::ParticleReal const t = refpart.t;
163  amrex::ParticleReal const pt = refpart.pt;
164  amrex::ParticleReal const s = refpart.s;
165 
166  // length of the current slice
167  amrex::ParticleReal const slice_ds = m_ds / nslice();
168 
169  // assign intermediate parameters
170  amrex::ParticleReal const theta = m_phi / nslice();
171  amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
172  amrex::ParticleReal const B = refpart.beta_gamma() /rc;
173 
174  // calculate expensive terms once
175  auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
176 
177  // advance position and momentum (bend)
178  refpart.px = px*cos_theta - pz*sin_theta;
179  refpart.py = py;
180  refpart.pz = pz*cos_theta + px*sin_theta;
181  refpart.pt = pt;
182 
183  refpart.x = x + (refpart.pz - pz)/B;
184  refpart.y = y + (theta/B)*py;
185  refpart.z = z - (refpart.px - px)/B;
186  refpart.t = t - (theta/B)*pt;
187 
188  // advance integrated path length
189  refpart.s = s + slice_ds;
190 
191  }
192 
193  private:
194  amrex::ParticleReal m_phi;
195  amrex::ParticleReal m_B;
196  };
197 
198 } // namespace impactx
199 
200 #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:36
@ t
fixed t as the independent variable
s
Definition: ExactSbend.H:34
amrex::ParticleReal m_B
bend angle in radians
Definition: ExactSbend.H:195
ImpactXParticleContainer::ParticleType PType
Definition: ExactSbend.H:37
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: ExactSbend.H:86
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:194
static constexpr auto name
Definition: ExactSbend.H:35
static constexpr amrex::ParticleReal degree2rad
Definition: ExactSbend.H:36
@ 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_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: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