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/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{
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,
39 public mixin::NoFinalize,
40 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
41 {
42 static constexpr auto type = "Aperture";
44
45 // TODO: make AMREX_ENUM and simplify @see shape_name implementation with it
51 enum Action
52 {
55 };
56
57 static std::string
58 shape_name (Shape const & shape);
59
60 static std::string
61 action_name (Action const & action);
62
79 amrex::ParticleReal aperture_x,
80 amrex::ParticleReal aperture_y,
81 amrex::ParticleReal repeat_x,
82 amrex::ParticleReal repeat_y,
83 bool shift_odd_x,
84 Shape shape,
85 Action action,
86 amrex::ParticleReal dx = 0,
87 amrex::ParticleReal dy = 0,
88 amrex::ParticleReal rotation_degree = 0,
89 std::optional<std::string> name = std::nullopt
90 )
91 : Named(std::move(name)),
92 Alignment(dx, dy, rotation_degree),
93 m_shape(shape), m_action(action), m_repeat_x(repeat_x), m_repeat_y(repeat_y), m_shift_odd_x(shift_odd_x)
94 {
95 using namespace amrex::literals; // for _rt and _prt
96
97 if (aperture_x > 0_prt) {
98 m_inv_aperture_x = 1_prt / aperture_x;
99 } else {
100 throw std::runtime_error("Aperture: aperture_x must be > 0!");
101 }
102
103 if (aperture_y > 0_prt) {
104 m_inv_aperture_y = 1_prt / aperture_y;
105 } else {
106 throw std::runtime_error("Aperture: aperture_y must be > 0!");
107 }
108
109 if (m_repeat_y == 0_prt && m_shift_odd_x) {
110 throw std::runtime_error("Aperture: shift_odd_x can only be used if repeat_y is set, too!");
111 }
112 }
113
115 using BeamOptic::operator();
116
124 void compute_constants ([[maybe_unused]] RefPart const & refpart)
125 {
126 Alignment::compute_constants(refpart);
127 }
128
141 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
144 T_Real & AMREX_RESTRICT x,
145 T_Real & AMREX_RESTRICT y,
146 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
147 T_Real & AMREX_RESTRICT px,
148 T_Real & AMREX_RESTRICT py,
149 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
150 T_IdCpu & AMREX_RESTRICT idcpu,
151 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
152 ) const
153 {
154 using namespace amrex::literals; // for _rt and _prt
155 namespace stdx = amrex::simd::stdx;
156 using amrex::Math::powi;
157 using std::fmod;
158 using std::abs;
159 using VBool = decltype(x>0_prt);
160
161 // shift due to alignment errors of the element
162 shift_in(x, y, px, py);
163
164 // define intermediate variables
165 amrex::ParticleReal const dx = m_repeat_x * 0.5_prt;
166 amrex::ParticleReal const dy = m_repeat_y * 0.5_prt;
167
168 // shift every 2nd row by half of m_repeat_x
169 T_Real sx = x;
170 T_Real const sy = y;
171 VBool const shift_x = m_shift_odd_x == false ? VBool{false} : fmod(floor((y+dy)/m_repeat_y), 2) != T_Real{0};
172#ifdef AMREX_USE_SIMD
173 amrex::simd::stdx::where(shift_x, sx) += dx;
174#else
175 sx += shift_x ? dx : 0_prt;
176#endif
177
178 // if the aperture is periodic, shift sx,sy coordinates to the fundamental domain
179 T_Real u = (m_repeat_x == 0_prt) ? sx : (fmod(abs(sx)+dx, m_repeat_x)-dx);
180 T_Real v = (m_repeat_y == 0_prt) ? sy : (fmod(abs(sy)+dy, m_repeat_y)-dy);
181
182 // scale horizontal and vertical coordinates
183 // performance optimization: we intentionally store the inverse of
184 // the aperture, so we can do a quick multiplication (instead of a
185 // costly division) here.
186 u = u * m_inv_aperture_x;
187 v = v * m_inv_aperture_y;
188
189 // compare against the aperture boundary
190 VBool inside_aperture;
191
192 switch (m_shape)
193 {
194 case Shape::rectangular: // default
195 inside_aperture = (powi<2>(u) <= 1_prt && powi<2>(v) <= 1_prt);
196 break;
197
199 inside_aperture = (powi<2>(u) + powi<2>(v) <= 1_prt);
200 break;
201
202 default:
203 inside_aperture = VBool{true};
204 break;
205 }
206
207 // For transmit (default): invalidate if outside aperture
208 // For absorb: invalidate if inside aperture
209 auto const invalid_mask = m_action == Action::transmit ? !inside_aperture : inside_aperture;
211
212 // undo shift due to alignment errors of the element
213 shift_out(x, y, px, py);
214 }
215
217 using Thin::operator();
218
220 using LinearTransport::operator();
221
228 Map6x6
229 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
230 {
231 throw std::runtime_error(std::string(type) + ": Envelope tracking is not yet implemented!");
232 return Map6x6::Identity();
233 }
234
237 // performance optimization: we intentionally store the inverse of
238 // the aperture, so we can do a quick multiplication instead of a costly
239 // division in the comparison operation per particle
240 amrex::ParticleReal m_inv_aperture_x;
241 amrex::ParticleReal m_inv_aperture_y;
242 amrex::ParticleReal m_repeat_x;
243 amrex::ParticleReal m_repeat_y;
245
246 };
247
248} // namespace impactx
249
250#endif // IMPACTX_APERTURE_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
__host__ __device__ void make_invalid() const noexcept
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:31
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:78
ImpactXParticleContainer::ParticleType PType
Definition Aperture.H:43
static std::string shape_name(Shape const &shape)
Definition Aperture.cpp:17
static constexpr auto type
Definition Aperture.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 Aperture.H:143
void compute_constants(RefPart const &refpart)
Definition Aperture.H:124
amrex::ParticleReal m_repeat_x
maximum vertical coordinate
Definition Aperture.H:242
amrex::ParticleReal m_inv_aperture_y
maximum horizontal coordinate
Definition Aperture.H:241
Action
Definition Aperture.H:52
@ absorb
Definition Aperture.H:54
@ transmit
Definition Aperture.H:53
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Aperture.H:229
Shape m_shape
Definition Aperture.H:235
amrex::ParticleReal m_inv_aperture_x
action type (transmit, absorb)
Definition Aperture.H:240
bool m_shift_odd_x
vertical period for repeated masking
Definition Aperture.H:244
Action m_action
aperture type (rectangular, elliptical)
Definition Aperture.H:236
amrex::ParticleReal m_repeat_y
horizontal period for repeated masking
Definition Aperture.H:243
static std::string action_name(Action const &action)
Definition Aperture.cpp:31
Shape
Definition Aperture.H:47
@ rectangular
Definition Aperture.H:48
@ elliptical
Definition Aperture.H:49
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