ImpactX
Loading...
Searching...
No Matches
QuadEdge.H
Go to the documentation of this file.
1/* Copyright 2022-2026 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_Enum.H>
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
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 entry = 1,
46 exit = -1,
47 );
48
49 static constexpr int DEFAULT_unit = 0;
50 static constexpr Location DEFAULT_flag = Location::entry;
51
78 int unit = DEFAULT_unit,
79 Location flag = DEFAULT_flag,
83 std::optional<std::string> name = DEFAULT_name
84 )
85 : Named(std::move(name)),
86 Alignment(dx, dy, rotation_degree),
87 m_k(k), m_unit(unit), m_flag(flag)
88 {
89 }
90
92 void reverse ()
93 {
94 m_flag = (m_flag == Location::entry)
95 ? Location::exit
96 : Location::entry;
97 }
98
100 using BeamOptic::operator();
101
109 void compute_constants ([[maybe_unused]] RefPart const & refpart)
110 {
111 using namespace amrex::literals; // for _rt and _prt
112
113 Alignment::compute_constants(refpart);
114
115 // access reference particle values to find beta
116 m_beta = refpart.beta();
117
118 // normalize quad units to MAD-X convention if needed
119 m_g = m_unit == 1 ? m_k / refpart.rigidity_Tm() : m_k;
120
121 // the global scale constant; sign depends on entry or exit
122 m_a = static_cast<amrex::ParticleReal>(m_flag) * (-1_prt * m_g / 12.0_prt);
123 }
124
139 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
142 T_Real & AMREX_RESTRICT x,
143 T_Real & AMREX_RESTRICT y,
144 T_Real & AMREX_RESTRICT t,
145 T_Real & AMREX_RESTRICT px,
146 T_Real & AMREX_RESTRICT py,
147 T_Real const & AMREX_RESTRICT pt,
148 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
149 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
150 ) const
151 {
152
153 using namespace amrex::literals; // for _rt and _prt
154 using amrex::Math::powi;
155 using namespace std; // for cmath(float)
156
157 // compute particle momentum deviation delta + 1
158 T_Real const delta1 = sqrt(1_prt - 2_prt*pt/m_beta + powi<2>(pt));
159 T_Real const a = m_a / delta1;
160
161 // initialize output values
162 T_Real xout = x;
163 T_Real yout = y;
164 T_Real tout = t;
165 T_Real pxout = px;
166 T_Real pyout = py;
167
168 // intermediate quantities to be used below
169 T_Real const x2my2 = x*x - y*y;
170 T_Real const x2py2 = x*x + y*y;
171 T_Real const denominator = 1_prt - 9_prt*powi<2>(a)*powi<2>(x2my2);
172
173 // apply edge focusing (transverse phase space)
174 xout = x - a * (powi<3>(x) + 3_prt * x * powi<2>(y));
175 yout = y + a * (3_prt * powi<2>(x) * y + powi<3>(y));
176
177 pxout = (px + 3_prt*a*px*x2py2 - 6_prt*a*py*x*y) / denominator;
178 pyout = (py - 3_prt*a*py*x2py2 + 6_prt*a*px*x*y) / denominator;
179
180 // apply edge focusing (longitudinal phase space)
181 T_Real const f4 = a * ( pxout * (powi<3>(x) + 3_prt*x*powi<2>(y))
182 - pyout * (powi<3>(y) + 3_prt*y*powi<2>(x)) );
183 tout = t + f4 * (pt - 1_prt/m_beta) / powi<2>(delta1);
184
185 // update output values
186 x = xout;
187 y = yout;
188 t = tout;
189 px = pxout;
190 py = pyout;
191 }
192
194 using Thin::operator();
195
197 using LinearTransport::operator();
198
205 Map6x6
206 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
207 {
208 using namespace amrex::literals; // for _rt and _prt
209
210 // initialize linear map matrix elements
212
213 // apply the transverse rotation (roll) alignment error
214 return rotate_aligned_map(R);
215 }
216
218 int m_unit;
219 Location m_flag;
220
221 private:
222 // constants that are independent of the individually tracked particle,
223 // see: compute_constants() to refresh
227 };
228
229} // namespace impactx
230
232
233#endif // IMPACTX_QUADEDGE_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
#define IMPACTX_PUSH_EXTERN_TEMPLATE(ElementType)
Definition PushAll.H:78
amrex_particle_real ParticleReal
constexpr T powi(T x) noexcept
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Definition All.H:55
@ 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:33
Definition QuadEdge.H:40
amrex::ParticleReal m_a
Definition QuadEdge.H:225
ImpactXParticleContainer::ParticleType PType
Definition QuadEdge.H:42
amrex::ParticleReal m_k
Definition QuadEdge.H:217
int m_unit
quadrupole focusing strength
Definition QuadEdge.H:218
void compute_constants(RefPart const &refpart)
Definition QuadEdge.H:109
static constexpr auto type
Definition QuadEdge.H:41
amrex::ParticleReal m_beta
Definition QuadEdge.H:226
AMREX_ENUM_IN_CLASS(Location, entry=1, exit=-1,)
void reverse()
Definition QuadEdge.H:92
static constexpr int DEFAULT_unit
Definition QuadEdge.H:49
QuadEdge(amrex::ParticleReal k, int unit=DEFAULT_unit, Location flag=DEFAULT_flag, amrex::ParticleReal dx=DEFAULT_dx, amrex::ParticleReal dy=DEFAULT_dy, amrex::ParticleReal rotation_degree=DEFAULT_rotation_degree, std::optional< std::string > name=DEFAULT_name)
Definition QuadEdge.H:76
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 const &AMREX_RESTRICT pt, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition QuadEdge.H:141
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition QuadEdge.H:206
amrex::ParticleReal m_g
+1 for entry, or -1 for exit
Definition QuadEdge.H:224
static constexpr Location DEFAULT_flag
Definition QuadEdge.H:50
Location m_flag
unit specification for quad strength
Definition QuadEdge.H:219
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:193
static constexpr amrex::ParticleReal DEFAULT_dy
Definition alignment.H:34
static constexpr amrex::ParticleReal DEFAULT_dx
Definition alignment.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:183
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:267
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:43
static constexpr amrex::ParticleReal DEFAULT_rotation_degree
Definition alignment.H:35
Definition beamoptic.H:567
Definition lineartransport.H:50
Definition named.H:29
static constexpr std::nullopt_t DEFAULT_name
Definition named.H:30
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:59
AMREX_FORCE_INLINE std::string name() const
Definition named.H:124
Definition nofinalize.H:22
Definition thin.H:24