ImpactX
Loading...
Searching...
No Matches
Aperture.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_APERTURE_H
11#define IMPACTX_APERTURE_H
12
13
15#include "mixin/alignment.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thin.H"
19#include "mixin/spintransport.H"
20#include "mixin/named.H"
21#include "mixin/nofinalize.H"
22
23#include <AMReX_Extension.H>
24#include <AMReX_Math.H>
25#include <AMReX_REAL.H>
26#include <AMReX_SIMD.H>
27
28#include <cmath>
29#include <stdexcept>
30
31
32namespace impactx::elements
33{
34 struct Aperture
35 : public mixin::Named,
36 public mixin::BeamOptic<Aperture>,
37 public mixin::LinearTransport<Aperture>,
38 public mixin::Thin,
39 public mixin::Alignment,
41 public mixin::NoFinalize,
42 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
43 {
44 static constexpr auto type = "Aperture";
46
47 // TODO: make AMREX_ENUM and simplify @see shape_name implementation with it
53 enum Action
54 {
57 };
58
59 static std::string
60 shape_name (Shape const & shape);
61
62 static std::string
63 action_name (Action const & action);
64
81 amrex::ParticleReal aperture_x,
82 amrex::ParticleReal aperture_y,
83 amrex::ParticleReal repeat_x,
84 amrex::ParticleReal repeat_y,
85 bool shift_odd_x,
86 Shape shape,
87 Action action,
90 amrex::ParticleReal rotation_degree = 0,
91 std::optional<std::string> name = std::nullopt
92 )
93 : Named(std::move(name)),
94 Alignment(dx, dy, rotation_degree),
95 m_shape(shape), m_action(action), m_repeat_x(repeat_x), m_repeat_y(repeat_y), m_shift_odd_x(shift_odd_x)
96 {
97 using namespace amrex::literals; // for _rt and _prt
98
99 if (aperture_x > 0_prt) {
100 m_inv_aperture_x = 1_prt / aperture_x;
101 } else {
102 throw std::runtime_error("Aperture: aperture_x must be > 0!");
103 }
104
105 if (aperture_y > 0_prt) {
106 m_inv_aperture_y = 1_prt / aperture_y;
107 } else {
108 throw std::runtime_error("Aperture: aperture_y must be > 0!");
109 }
110
111 if (m_repeat_y == 0_prt && m_shift_odd_x) {
112 throw std::runtime_error("Aperture: shift_odd_x can only be used if repeat_y is set, too!");
113 }
114 }
115
117 void reverse () { /* no-op */ }
118
120 using BeamOptic::operator();
121
129 void compute_constants ([[maybe_unused]] RefPart const & refpart)
130 {
131 Alignment::compute_constants(refpart);
132 }
133
146 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
149 T_Real const & AMREX_RESTRICT x,
150 T_Real const & AMREX_RESTRICT y,
151 [[maybe_unused]] T_Real const & AMREX_RESTRICT t,
152 [[maybe_unused]] T_Real const & AMREX_RESTRICT px,
153 [[maybe_unused]] T_Real const & AMREX_RESTRICT py,
154 [[maybe_unused]] T_Real const & AMREX_RESTRICT pt,
155 T_IdCpu & AMREX_RESTRICT idcpu,
156 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
157 ) const
158 {
159 using namespace amrex::literals; // for _rt and _prt
160 namespace stdx = amrex::simd::stdx;
161 using amrex::Math::powi;
162 using std::fmod;
163 using std::abs;
164 using VBool = decltype(x>0_prt);
165
166 // define intermediate variables
167 amrex::ParticleReal const dx = m_repeat_x * 0.5_prt;
168 amrex::ParticleReal const dy = m_repeat_y * 0.5_prt;
169
170 // shift every 2nd row by half of m_repeat_x
171 T_Real sx = x;
172 T_Real const sy = y;
173 VBool const shift_x = m_shift_odd_x == false ? VBool{false} : fmod(floor((y+dy)/m_repeat_y), 2) != T_Real{0};
174#ifdef AMREX_USE_SIMD
175 amrex::simd::stdx::where(shift_x, sx) += dx;
176#else
177 sx += shift_x ? dx : 0_prt;
178#endif
179
180 // if the aperture is periodic, shift sx,sy coordinates to the fundamental domain
181 T_Real u = (m_repeat_x == 0_prt) ? sx : (fmod(abs(sx)+dx, m_repeat_x)-dx);
182 T_Real v = (m_repeat_y == 0_prt) ? sy : (fmod(abs(sy)+dy, m_repeat_y)-dy);
183
184 // scale horizontal and vertical coordinates
185 // performance optimization: we intentionally store the inverse of
186 // the aperture, so we can do a quick multiplication (instead of a
187 // costly division) here.
188 u = u * m_inv_aperture_x;
189 v = v * m_inv_aperture_y;
190
191 // compare against the aperture boundary
192 VBool inside_aperture;
193
194 switch (m_shape)
195 {
196 case Shape::rectangular: // default
197 inside_aperture = (powi<2>(u) <= 1_prt && powi<2>(v) <= 1_prt);
198 break;
199
201 inside_aperture = (powi<2>(u) + powi<2>(v) <= 1_prt);
202 break;
203
204 default:
205 inside_aperture = VBool{true};
206 break;
207 }
208
209 // For transmit (default): invalidate if outside aperture
210 // For absorb: invalidate if inside aperture
211 auto const invalid_mask = m_action == Action::transmit ? !inside_aperture : inside_aperture;
213 }
214
216 using Thin::operator();
217
234 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
237 T_Real const & AMREX_RESTRICT x,
238 T_Real const & AMREX_RESTRICT y,
239 T_Real const & AMREX_RESTRICT t,
240 T_Real const & AMREX_RESTRICT px,
241 T_Real const & AMREX_RESTRICT py,
242 T_Real const & AMREX_RESTRICT pt,
243 [[maybe_unused]] T_Real const & AMREX_RESTRICT sx,
244 [[maybe_unused]] T_Real const & AMREX_RESTRICT sy,
245 [[maybe_unused]] T_Real const & AMREX_RESTRICT sz,
246 T_IdCpu & AMREX_RESTRICT idcpu,
247 RefPart const & AMREX_RESTRICT refpart
248 ) const
249 {
250 // spin is unaffected
251
252 // phase space push
253 (*this)(x, y, t, px, py, pt, idcpu, refpart);
254 }
255
257 using LinearTransport::operator();
258
269 Map6x6
270 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
271 {
272 // apply the transverse rotation (roll) alignment error
274 }
275
278 // performance optimization: we intentionally store the inverse of
279 // the aperture, so we can do a quick multiplication instead of a costly
280 // division in the comparison operation per particle
286
287 };
288
289} // namespace impactx
290
292
293#endif // IMPACTX_APERTURE_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__ detail::where_expression< T > where(bool const mask, T &value)
__host__ __device__ T abs(const GpuComplex< T > &a_z) noexcept
Definition All.H:56
@ 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
Definition Aperture.H:43
Aperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y, amrex::ParticleReal repeat_x, amrex::ParticleReal repeat_y, bool shift_odd_x, Shape shape, Action action, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, std::optional< std::string > name=std::nullopt)
Definition Aperture.H:80
ImpactXParticleContainer::ParticleType PType
Definition Aperture.H:45
static std::string shape_name(Shape const &shape)
Definition Aperture.cpp:17
static constexpr auto type
Definition Aperture.H:44
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(T_Real const &AMREX_RESTRICT x, T_Real const &AMREX_RESTRICT y, T_Real const &AMREX_RESTRICT t, T_Real const &AMREX_RESTRICT px, T_Real const &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Aperture.H:148
void compute_constants(RefPart const &refpart)
Definition Aperture.H:129
amrex::ParticleReal m_repeat_x
maximum vertical coordinate
Definition Aperture.H:283
amrex::ParticleReal m_inv_aperture_y
maximum horizontal coordinate
Definition Aperture.H:282
Action
Definition Aperture.H:54
@ absorb
Definition Aperture.H:56
@ transmit
Definition Aperture.H:55
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Aperture.H:270
Shape m_shape
Definition Aperture.H:276
amrex::ParticleReal m_inv_aperture_x
action type (transmit, absorb)
Definition Aperture.H:281
bool m_shift_odd_x
vertical period for repeated masking
Definition Aperture.H:285
Action m_action
aperture type (rectangular, elliptical)
Definition Aperture.H:277
amrex::ParticleReal m_repeat_y
horizontal period for repeated masking
Definition Aperture.H:284
void reverse()
Definition Aperture.H:117
static std::string action_name(Action const &action)
Definition Aperture.cpp:31
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void spin_and_phasespace_push(T_Real const &AMREX_RESTRICT x, T_Real const &AMREX_RESTRICT y, T_Real const &AMREX_RESTRICT t, T_Real const &AMREX_RESTRICT px, T_Real const &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_Real const &AMREX_RESTRICT sx, T_Real const &AMREX_RESTRICT sy, T_Real const &AMREX_RESTRICT sz, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Aperture.H:236
Shape
Definition Aperture.H:49
@ rectangular
Definition Aperture.H:50
@ elliptical
Definition Aperture.H:51
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:189
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:179
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:263
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:39
Definition beamoptic.H:529
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 spintransport.H:36
Definition thin.H:24