ImpactX
Loading...
Searching...
No Matches
QuadEdge.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_QUADEDGE_H
11#define IMPACTX_QUADEDGE_H
12
14#include "mixin/alignment.H"
15#include "mixin/beamoptic.H"
16#include "mixin/thin.H"
18#include "mixin/named.H"
19#include "mixin/nofinalize.H"
20
21#include <AMReX_Extension.H>
22#include <AMReX_Math.H>
23#include <AMReX_REAL.H>
24#include <AMReX_SIMD.H>
25
26#include <cmath>
27#include <stdexcept>
28
29
30namespace impactx::elements
31{
32 struct QuadEdge
33 : public mixin::Named,
34 public mixin::BeamOptic<QuadEdge>,
35 public mixin::LinearTransport<QuadEdge>,
36 public mixin::Thin,
37 public mixin::Alignment,
38 public mixin::NoFinalize,
39 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
40 {
41 static constexpr auto type = "QuadEdge";
43
45 {
46 entry = 1,
47 exit = -1
48 };
49
75 amrex::ParticleReal k,
76 int unit,
77 Location flag,
78 amrex::ParticleReal dx = 0,
79 amrex::ParticleReal dy = 0,
80 amrex::ParticleReal rotation_degree = 0,
81 std::optional<std::string> name = std::nullopt
82 )
83 : Named(std::move(name)),
84 Alignment(dx, dy, rotation_degree),
85 m_k(k), m_unit(unit), m_flag(flag)
86 {
87 }
88
90 using BeamOptic::operator();
91
99 void compute_constants ([[maybe_unused]] RefPart const & refpart)
100 {
101 using namespace amrex::literals; // for _rt and _prt
102
103 Alignment::compute_constants(refpart);
104
105 // access reference particle values to find beta and gamma
106 m_beta = refpart.beta();
107 m_gamma = refpart.gamma();
108
109 // normalize quad units to MAD-X convention if needed
110 m_g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
111
112 // the global scale constant; sign depends on entry or exit
113 m_a = static_cast<amrex::ParticleReal>(m_flag) * (-1_prt * m_g / 12.0_prt);
114 }
115
130 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
133 T_Real & AMREX_RESTRICT x,
134 T_Real & AMREX_RESTRICT y,
135 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
136 T_Real & AMREX_RESTRICT px,
137 T_Real & AMREX_RESTRICT py,
138 T_Real & AMREX_RESTRICT pt,
139 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
140 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
141 ) const
142 {
143
144 using namespace amrex::literals; // for _rt and _prt
145 using amrex::Math::powi;
146 using namespace std; // for cmath(float)
147
148 // shift due to alignment errors of the element
149 shift_in(x, y, px, py);
150
151 // compute particle momentum deviation delta + 1
152 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
153 T_Real a = m_a / delta1;
154
155 // initialize output values
156 T_Real xout = x;
157 T_Real yout = y;
158 T_Real tout = t;
159 T_Real pxout = px;
160 T_Real pyout = py;
161
162 // intermediate quantities to be used below
163 T_Real x2my2 = x*x - y*y;
164 T_Real x2py2 = x*x + y*y;
165 T_Real denominator = 1_prt - 9_prt*powi<2>(a)*powi<2>(x2my2);
166
167 // apply edge focusing (transverse phase space)
168 xout = x - a * (powi<3>(x) + 3_prt * x * powi<2>(y));
169 yout = y + a * (3_prt * powi<2>(x) * y + powi<3>(y));
170
171 pxout = (px + 3_prt*a*px*x2py2 - 6_prt*a*py*x*y) / denominator;
172 pyout = (py - 3_prt*a*py*x2py2 + 6_prt*a*px*x*y) / denominator;
173
174 // apply edge focusing (longitudinal phase space)
175 T_Real f4 = a * ( pxout * (powi<3>(x) + 3_prt*x*powi<2>(y))
176 - pyout * (powi<3>(y) + 3_prt*y*powi<2>(x)) );
177 tout = t + f4 * (pt - 1_prt/m_beta) / powi<2>(delta1);
178
179 // update output values
180 x = xout;
181 y = yout;
182 t = tout;
183 px = pxout;
184 py = pyout;
185
186 // undo shift due to alignment errors of the element
187 shift_out(x, y, px, py);
188 }
189
191 using Thin::operator();
192
194 using LinearTransport::operator();
195
202 Map6x6
203 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
204 {
205 using namespace amrex::literals; // for _rt and _prt
206
207 // initialize linear map matrix elements
209
210 return R;
211 }
212
213 amrex::ParticleReal m_k;
214 int m_unit;
216
217 private:
218 // constants that are independent of the individually tracked particle,
219 // see: compute_constants() to refresh
220 amrex::ParticleReal m_g;
221 amrex::ParticleReal m_a;
222 amrex::ParticleReal m_beta;
223 amrex::ParticleReal m_gamma;
224 };
225
226} // namespace impactx
227
228#endif // IMPACTX_QUADEDGE_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
constexpr T powi(T x) noexcept
Definition All.H:54
@ 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
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:31
amrex::ParticleReal m_a
Definition QuadEdge.H:221
ImpactXParticleContainer::ParticleType PType
Definition QuadEdge.H:42
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 QuadEdge.H:132
amrex::ParticleReal m_k
Definition QuadEdge.H:213
int m_unit
quadrupole focusing strength
Definition QuadEdge.H:214
void compute_constants(RefPart const &refpart)
Definition QuadEdge.H:99
static constexpr auto type
Definition QuadEdge.H:41
amrex::ParticleReal m_beta
Definition QuadEdge.H:222
QuadEdge(amrex::ParticleReal k, int unit, Location flag, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, std::optional< std::string > name=std::nullopt)
Definition QuadEdge.H:74
amrex::ParticleReal m_gamma
Definition QuadEdge.H:223
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition QuadEdge.H:203
amrex::ParticleReal m_g
+1 for entry, or -1 for exit
Definition QuadEdge.H:220
Location m_flag
unit specification for quad strength
Definition QuadEdge.H:215
Location
Definition QuadEdge.H:45
@ exit
for leading (entry) fringe field
Definition QuadEdge.H:47
@ entry
Definition QuadEdge.H:46
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:219
Definition lineartransport.H:29
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 thin.H:24