ImpactX
Loading...
Searching...
No Matches
Aperture.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_APERTURE_H
11#define IMPACTX_APERTURE_H
12
14#include "mixin/alignment.H"
15#include "mixin/beamoptic.H"
16#include "mixin/thin.H"
18#include "mixin/spintransport.H"
19#include "mixin/named.H"
20#include "mixin/nofinalize.H"
21
22#include <AMReX_Enum.H>
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
31namespace impactx::elements
32{
33 struct Aperture
34 : public mixin::Named,
35 public mixin::BeamOptic<Aperture>,
36 public mixin::LinearTransport<Aperture>,
37 public mixin::Thin,
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 = "Aperture";
45
47 rectangular,
48 elliptical,
49 );
50
52 transmit,
53 absorb,
54 );
55
58 static constexpr bool DEFAULT_shift_odd_x = false;
59 static constexpr Shape DEFAULT_shape = Shape::rectangular;
60 static constexpr Action DEFAULT_action = Action::transmit;
61
78 amrex::ParticleReal aperture_x,
79 amrex::ParticleReal aperture_y,
82 bool shift_odd_x = DEFAULT_shift_odd_x,
83 Shape shape = DEFAULT_shape,
84 Action action = DEFAULT_action,
88 std::optional<std::string> name = DEFAULT_name
89 )
90 : Named(std::move(name)),
91 Alignment(dx, dy, rotation_degree),
92 m_shape(shape), m_action(action), m_repeat_x(repeat_x), m_repeat_y(repeat_y), m_shift_odd_x(shift_odd_x)
93 {
94 using namespace amrex::literals; // for _rt and _prt
95
96 if (aperture_x > 0_prt) {
97 m_inv_aperture_x = 1_prt / aperture_x;
98 } else {
99 throw std::runtime_error("Aperture: aperture_x must be > 0!");
100 }
101
102 if (aperture_y > 0_prt) {
103 m_inv_aperture_y = 1_prt / aperture_y;
104 } else {
105 throw std::runtime_error("Aperture: aperture_y must be > 0!");
106 }
107
108 if (m_repeat_y == 0_prt && m_shift_odd_x) {
109 throw std::runtime_error("Aperture: shift_odd_x can only be used if repeat_y is set, too!");
110 }
111 }
112
114 void reverse () { /* no-op */ }
115
117 using BeamOptic::operator();
118
126 void compute_constants ([[maybe_unused]] RefPart const & refpart)
127 {
128 Alignment::compute_constants(refpart);
129 }
130
143 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
146 T_Real const & AMREX_RESTRICT x,
147 T_Real const & AMREX_RESTRICT y,
148 [[maybe_unused]] T_Real const & AMREX_RESTRICT t,
149 [[maybe_unused]] T_Real const & AMREX_RESTRICT px,
150 [[maybe_unused]] T_Real const & AMREX_RESTRICT py,
151 [[maybe_unused]] T_Real const & AMREX_RESTRICT pt,
152 T_IdCpu & AMREX_RESTRICT idcpu,
153 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
154 ) const
155 {
156 using namespace amrex::literals; // for _rt and _prt
157 namespace stdx = amrex::simd::stdx;
158 using amrex::Math::powi;
159 using std::fmod;
160 using std::abs;
161 using VBool = decltype(x>0_prt);
162
163 // define intermediate variables
164 amrex::ParticleReal const dx = m_repeat_x * 0.5_prt;
165 amrex::ParticleReal const dy = m_repeat_y * 0.5_prt;
166
167 // shift every 2nd row by half of m_repeat_x
168 T_Real sx = x;
169 T_Real const sy = y;
170 VBool const shift_x = m_shift_odd_x == false ? VBool{false} : fmod(floor((y+dy)/m_repeat_y), 2) != T_Real{0};
171#ifdef AMREX_USE_SIMD
172 amrex::simd::stdx::where(shift_x, sx) += dx;
173#else
174 sx += shift_x ? dx : 0_prt;
175#endif
176
177 // if the aperture is periodic, shift sx,sy coordinates to the fundamental domain
178 T_Real u = (m_repeat_x == 0_prt) ? sx : (fmod(abs(sx)+dx, m_repeat_x)-dx);
179 T_Real v = (m_repeat_y == 0_prt) ? sy : (fmod(abs(sy)+dy, m_repeat_y)-dy);
180
181 // scale horizontal and vertical coordinates
182 // performance optimization: we intentionally store the inverse of
183 // the aperture, so we can do a quick multiplication (instead of a
184 // costly division) here.
185 u = u * m_inv_aperture_x;
186 v = v * m_inv_aperture_y;
187
188 // compare against the aperture boundary
189 VBool inside_aperture;
190
191 switch (m_shape)
192 {
193 case Shape::rectangular: // default
194 inside_aperture = (powi<2>(u) <= 1_prt && powi<2>(v) <= 1_prt);
195 break;
196
197 case Shape::elliptical:
198 inside_aperture = (powi<2>(u) + powi<2>(v) <= 1_prt);
199 break;
200
201 default:
202 inside_aperture = VBool{true};
203 break;
204 }
205
206 // For transmit (default): invalidate if outside aperture
207 // For absorb: invalidate if inside aperture
208 auto const invalid_mask = m_action == Action::transmit ? !inside_aperture : inside_aperture;
210 }
211
213 using Thin::operator();
214
231 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
234 T_Real const & AMREX_RESTRICT x,
235 T_Real const & AMREX_RESTRICT y,
236 T_Real const & AMREX_RESTRICT t,
237 T_Real const & AMREX_RESTRICT px,
238 T_Real const & AMREX_RESTRICT py,
239 T_Real const & AMREX_RESTRICT pt,
240 [[maybe_unused]] T_Real const & AMREX_RESTRICT sx,
241 [[maybe_unused]] T_Real const & AMREX_RESTRICT sy,
242 [[maybe_unused]] T_Real const & AMREX_RESTRICT sz,
243 T_IdCpu & AMREX_RESTRICT idcpu,
244 RefPart const & AMREX_RESTRICT refpart
245 ) const
246 {
247 // spin is unaffected
248
249 // phase space push
250 (*this)(x, y, t, px, py, pt, idcpu, refpart);
251 }
252
254 using LinearTransport::operator();
255
266 Map6x6
267 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
268 {
269 // apply the transverse rotation (roll) alignment error
271 }
272
273 Shape m_shape;
274 Action m_action;
275 // performance optimization: we intentionally store the inverse of
276 // the aperture, so we can do a quick multiplication instead of a costly
277 // division in the comparison operation per particle
283
284 };
285
286} // namespace impactx
287
289
290#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: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
__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:42
static constexpr Shape DEFAULT_shape
Definition Aperture.H:59
ImpactXParticleContainer::ParticleType PType
Definition Aperture.H:44
static constexpr amrex::ParticleReal DEFAULT_repeat_y
Definition Aperture.H:57
static constexpr auto type
Definition Aperture.H:43
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:145
static constexpr Action DEFAULT_action
Definition Aperture.H:60
AMREX_ENUM_IN_CLASS(Shape, rectangular, elliptical,)
void compute_constants(RefPart const &refpart)
Definition Aperture.H:126
amrex::ParticleReal m_repeat_x
maximum vertical coordinate
Definition Aperture.H:280
static constexpr amrex::ParticleReal DEFAULT_repeat_x
Definition Aperture.H:56
amrex::ParticleReal m_inv_aperture_y
maximum horizontal coordinate
Definition Aperture.H:279
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Aperture.H:267
Shape m_shape
Definition Aperture.H:273
AMREX_ENUM_IN_CLASS(Action, transmit, absorb,)
amrex::ParticleReal m_inv_aperture_x
action type (transmit, absorb)
Definition Aperture.H:278
Aperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y, amrex::ParticleReal repeat_x=DEFAULT_repeat_x, amrex::ParticleReal repeat_y=DEFAULT_repeat_y, bool shift_odd_x=DEFAULT_shift_odd_x, Shape shape=DEFAULT_shape, Action action=DEFAULT_action, 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 Aperture.H:77
bool m_shift_odd_x
vertical period for repeated masking
Definition Aperture.H:282
Action m_action
aperture type (rectangular, elliptical)
Definition Aperture.H:274
static constexpr bool DEFAULT_shift_odd_x
Definition Aperture.H:58
amrex::ParticleReal m_repeat_y
horizontal period for repeated masking
Definition Aperture.H:281
void reverse()
Definition Aperture.H:114
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:233
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 spintransport.H:36
Definition thin.H:24