ImpactX
Loading...
Searching...
No Matches
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/pipeaperture.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thick.H"
19#include "mixin/named.H"
20#include "mixin/nofinalize.H"
21
22#include <AMReX_Extension.H>
23#include <AMReX_Math.H>
24#include <AMReX_REAL.H>
25#include <AMReX_SIMD.H>
26
27#include <cmath>
28#include <stdexcept>
29
30
31namespace impactx::elements
32{
34 : public mixin::Named,
35 public mixin::BeamOptic<ExactSbend>,
36 public mixin::LinearTransport<ExactSbend>,
37 public mixin::Thick,
38 public mixin::Alignment,
40 public mixin::NoFinalize,
41 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
42 {
43 static constexpr auto type = "ExactSbend";
46
76 amrex::ParticleReal rotation_degree = 0,
79 int nslice = 1,
80 std::optional<std::string> name = std::nullopt
81 )
82 : Named(std::move(name)),
83 Thick(ds, nslice),
84 Alignment(dx, dy, rotation_degree),
86 m_phi(phi * degree2rad), m_B(B)
87 {
88 }
89
92 rc (RefPart const & refpart) const
93 {
94 using namespace amrex::literals; // for _rt and _prt
95
96 return m_B != 0_prt ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
97 }
98
100 void reverse () { Thick::reverse(); m_phi = -m_phi; }
101
103 using BeamOptic::operator();
104
105
113 void compute_constants (RefPart const & refpart)
114 {
115 using namespace amrex::literals; // for _rt and _prt
116 using amrex::Math::powi;
117
118 Alignment::compute_constants(refpart);
119
120 // reference particle's orbital radius
121 m_rc = this->rc(refpart);
122
123 // arc length and angle of the current slice
124 m_slice_ds = m_ds / nslice();
125 amrex::ParticleReal const bend_sign = m_ds * m_rc / std::abs(m_ds * m_rc);
126 m_slice_phi = bend_sign * std::abs(m_phi) / nslice(); // the sign is determined by rc/ds
127
128 // trigonometric evaluations
129 auto const [sin_phi, cos_phi] = amrex::Math::sincos(m_slice_phi);
130 m_sin_phi = sin_phi;
131 m_cos_phi = cos_phi;
132
133 // access reference particle values to find relativistic factors
134 m_ibeta = 1_prt / refpart.beta();
135 m_ibetagam2 = 1_prt / powi<2>(refpart.beta_gamma());
136
137 }
138
150 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
153 T_Real & AMREX_RESTRICT x,
154 T_Real & AMREX_RESTRICT y,
155 T_Real & AMREX_RESTRICT t,
156 T_Real & AMREX_RESTRICT px,
157 T_Real & AMREX_RESTRICT py,
158 T_Real & AMREX_RESTRICT pt,
159 T_IdCpu & AMREX_RESTRICT idcpu,
160 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
161 ) const
162 {
163 using namespace amrex::literals; // for _rt and _prt
164 using amrex::Math::powi;
165 using namespace std; // for cmath(float)
166 namespace stdx = amrex::simd::stdx;
167
168 // shift due to alignment errors of the element
169 shift_in(x, y, px, py);
170
171 // access position data
172 T_Real const xout = x;
173 T_Real const yout = y;
174 T_Real const tout = t;
175
176 // initialize output values of momenta
177 T_Real pxout = px;
178 T_Real pyout = py;
179 T_Real ptout = pt;
180
181 // treat the special case of zero field
182 if (m_phi==0_prt && m_B==0_prt) {
183 // compute the radical in the denominator (= pz):
184 T_Real const inv_pzden = 1_prt / sqrt(
185 powi<2>(pt - m_ibeta) -
187 powi<2>(px) -
188 powi<2>(py)
189 );
190
191 // advance position and momentum (exact drift)
192 x = xout + m_slice_ds * px * inv_pzden;
193 // pxout = px;
194 y = yout + m_slice_ds * py * inv_pzden;
195 // pyout = py;
196 t = tout - m_slice_ds * (m_ibeta + (pt - m_ibeta) * inv_pzden);
197 // ptout = pt;
198
199 // assign updated momenta
200 px = pxout;
201 py = pyout;
202 pt = ptout;
203
204 } else {
205
206 // assign intermediate quantities
207 T_Real const pperp2 = powi<2>(pt)-2.0_prt * m_ibeta * pt - powi<2>(py)+1.0_prt;
208 T_Real const px2 = powi<2>(px);
209 // determine if particle lies within the domain of map definition
210 auto const mask = pperp2 <= px2;
212 {
213 T_Real const pperp = sqrt(pperp2);
214 T_Real const pzi = sqrt(powi<2>(pperp) - powi<2>(px));
215 T_Real const rho = m_rc + xout;
216
217 // update momenta
218 pxout = px * m_cos_phi + (pzi - rho / m_rc) * m_sin_phi;
219 pyout = py;
220 ptout = pt;
221
222 // angle of momentum rotation
223 T_Real const px2f = powi<2>(pxout);
224 // determine if particle lies within domain of map definition
225 auto const mask2 = pperp2 <= px2f;
227 {
228 T_Real const pzf = sqrt(powi<2>(pperp)-powi<2>(pxout));
229 T_Real const theta = m_slice_phi + asin(px/pperp) - asin(pxout/pperp);
230
231 // update position coordinates
232 x = -m_rc + rho*m_cos_phi + m_rc*(pzf + px*m_sin_phi - pzi*m_cos_phi);
233 y = yout + theta * m_rc * py;
234 t = tout - theta * m_rc * (pt - 1.0_prt * m_ibeta) - m_slice_phi * m_rc * m_ibeta;
235
236 // assign updated momenta
237 px = pxout;
238 py = pyout;
239 pt = ptout;
240 }
241 }
242 }
243
244 // apply transverse aperture
245 apply_aperture(x, y, idcpu);
246
247 // undo shift due to alignment errors of the element
248 shift_out(x, y, px, py);
249 }
250
256 void operator() (RefPart & AMREX_RESTRICT refpart) const
257 {
258 using namespace amrex::literals; // for _rt and _prt
259 using amrex::Math::powi;
260
261 // assign input reference particle values
262 amrex::ParticleReal const x = refpart.x;
263 amrex::ParticleReal const px = refpart.px;
264 amrex::ParticleReal const y = refpart.y;
265 amrex::ParticleReal const py = refpart.py;
266 amrex::ParticleReal const z = refpart.z;
267 amrex::ParticleReal const pz = refpart.pz;
268 amrex::ParticleReal const t = refpart.t;
269 amrex::ParticleReal const pt = refpart.pt;
270 amrex::ParticleReal const s = refpart.s;
271
272 // length of the current slice
273 amrex::ParticleReal const slice_ds = m_ds / nslice();
274
275 // special case of zero field = an exact drift
276 if (m_phi==0_prt && m_B==0_prt) {
277 // advance position and momentum (drift)
278 amrex::ParticleReal const step = slice_ds /std::sqrt(powi<2>(pt)-1.0_prt);
279 refpart.x = x + step*px;
280 refpart.y = y + step*py;
281 refpart.z = z + step*pz;
282 refpart.t = t - step*pt;
283
284 } else {
285
286 // assign intermediate parameters
287 amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
288 amrex::ParticleReal const B = refpart.beta_gamma() /rc;
289 amrex::ParticleReal const bend_sign = m_ds * rc / std::abs(m_ds * rc);
290 amrex::ParticleReal const theta = bend_sign * std::abs(m_phi) / nslice(); // sign is determined by rc*ds
291
292 // calculate expensive terms once
293 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
294
295 // advance position and momentum (bend)
296 refpart.px = px*cos_theta - pz*sin_theta;
297 refpart.py = py;
298 refpart.pz = pz*cos_theta + px*sin_theta;
299 refpart.pt = pt;
300
301 refpart.x = x + (refpart.pz - pz)/B;
302 refpart.y = y + (theta/B)*py;
303 refpart.z = z - (refpart.px - px)/B;
304 refpart.t = t - (theta/B)*pt;
305
306 }
307
308 // advance integrated path length
309 refpart.s = s + slice_ds;
310 }
311
313 using LinearTransport::operator();
314
321 Map6x6
322 transport_map (RefPart const & AMREX_RESTRICT refpart) const
323 {
324 using namespace amrex::literals; // for _rt and _prt
325 using amrex::Math::powi;
326
327 // length of the current slice
328 amrex::ParticleReal const slice_ds = m_ds / nslice();
329
330 // access reference particle values to find beta*gamma^2
331 amrex::ParticleReal const pt_ref = refpart.pt;
332 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1_prt;
333 amrex::ParticleReal const bet = refpart.beta();
334
335 // initialize linear map matrix elements
337
338 // treat the special case of zero field
339 if (m_rc==0.0_prt) {
340 R(1,2) = slice_ds;
341 R(3,4) = slice_ds;
342 R(5,6) = slice_ds / betgam2;
343
344 } else {
345
346 // calculate expensive terms once
347 amrex::ParticleReal const theta = slice_ds/m_rc;
348 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
349
350 // assign linear map matrix elements
351 R(1,1) = cos_theta;
352 R(1,2) = m_rc * sin_theta;
353 R(1,6) = - ( m_rc / bet) * (1_prt - cos_theta);
354 R(2,1) = -sin_theta / m_rc;
355 R(2,2) = cos_theta;
356 R(2,6) = - sin_theta / bet;
357 R(3,4) = m_rc * theta;
358 R(5,1) = sin_theta / bet;
359 R(5,2) = m_rc / bet * (1_prt - cos_theta);
360 R(5,6) = m_rc * (-theta + sin_theta / (bet * bet));
361 }
362
363 return R;
364 }
365
368
369 private:
370 // constants that are independent of the individually tracked particle,
371 // see: compute_constants() to refresh
373 };
374
375} // namespace impactx
376
378
379#endif // IMPACTX_EXACTSBEND_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
Array4< int const > mask
#define IMPACTX_PUSH_EXTERN_TEMPLATE(ElementType)
Definition PushAll.H:78
amrex_particle_real ParticleReal
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Definition All.H:56
@ s
fixed s as the independent variable
Definition ImpactXParticleContainer.H:37
@ t
fixed t as the independent variable
Definition ImpactXParticleContainer.H:38
amrex::SmallMatrix< amrex::ParticleReal, 6, 6, amrex::Order::F, 1 > Map6x6
Definition CovarianceMatrix.H:20
__host__ __device__ void make_invalid() const noexcept
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:168
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition ReferenceParticle.H:261
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:152
Definition ExactSbend.H:42
amrex::ParticleReal m_rc
Definition ExactSbend.H:372
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ExactSbend.H:322
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rc(RefPart const &refpart) const
Definition ExactSbend.H:92
amrex::ParticleReal m_cos_phi
Definition ExactSbend.H:372
amrex::ParticleReal m_slice_ds
magnetic field in T
Definition ExactSbend.H:372
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT t, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py, T_Real &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ExactSbend.H:152
void reverse()
Definition ExactSbend.H:100
amrex::ParticleReal m_sin_phi
Definition ExactSbend.H:372
ImpactXParticleContainer::ParticleType PType
Definition ExactSbend.H:45
ExactSbend(amrex::ParticleReal ds, amrex::ParticleReal phi, amrex::ParticleReal B, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, amrex::ParticleReal aperture_x=0, amrex::ParticleReal aperture_y=0, int nslice=1, std::optional< std::string > name=std::nullopt)
Definition ExactSbend.H:70
static constexpr amrex::ParticleReal degree2rad
Definition ExactSbend.H:44
amrex::ParticleReal m_ibetagam2
Definition ExactSbend.H:372
amrex::ParticleReal m_ibeta
Definition ExactSbend.H:372
void compute_constants(RefPart const &refpart)
Definition ExactSbend.H:113
amrex::ParticleReal m_slice_phi
Definition ExactSbend.H:372
static constexpr auto type
Definition ExactSbend.H:43
amrex::ParticleReal m_B
bend angle in radians
Definition ExactSbend.H:367
amrex::ParticleReal m_phi
Definition ExactSbend.H:366
Definition alignment.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_out(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:109
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:146
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:136
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_in(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:78
Definition beamoptic.H:436
Definition lineartransport.H:50
Definition named.H:29
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:57
AMREX_FORCE_INLINE std::string name() const
Definition named.H:122
Definition nofinalize.H:22
Definition pipeaperture.H:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void apply_aperture(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_IdCpu &AMREX_RESTRICT idcpu) const
Definition pipeaperture.H:59
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_x() const
Definition pipeaperture.H:90
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_y() const
Definition pipeaperture.H:101
PipeAperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y)
Definition pipeaperture.H:32
Definition thick.H:24
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:30
amrex::ParticleReal m_ds
Definition thick.H:68
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