ImpactX
Loading...
Searching...
No Matches
PolygonAperture.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: Eric G. Stern, Chad Mitchell, Axel Huebl
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_POLYGONAPERTURE_H
11#define IMPACTX_POLYGONAPERTURE_H
12
13
15#include "mixin/alignment.H"
16#include "mixin/beamoptic.H"
17#include "mixin/dynamicdata.H"
19#include "mixin/named.H"
20#include "mixin/nofinalize.H"
21#include "mixin/thin.H"
22#include "mixin/TrackedVector.H"
23
24#include <ablastr/constant.H>
25
26#include <AMReX_Extension.H>
27#include <AMReX_Math.H>
28#include <AMReX_REAL.H>
29#include <AMReX_SIMD.H>
30
31#include <cmath>
32#include <map>
33#include <memory>
34#include <stdexcept>
35#include <vector>
36
37
38namespace impactx::elements
39{
40
51
53 : public mixin::Named,
54 public mixin::BeamOptic<PolygonAperture>,
55 public mixin::LinearTransport<PolygonAperture>,
56 public mixin::Thin,
57 public mixin::Alignment,
59 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
60 {
61 static constexpr auto type = "PolygonAperture";
63
65
66 enum Action
67 {
70 };
71
72 static std::string
73 action_name (Action const & action);
74
92 std::vector< amrex::ParticleReal> vertices_x,
93 std::vector< amrex::ParticleReal> vertices_y,
94 amrex::ParticleReal min_radius2,
95 amrex::ParticleReal repeat_x,
96 amrex::ParticleReal repeat_y,
97 bool shift_odd_x,
98 Action action,
101 amrex::ParticleReal rotation_degree = 0,
102 std::optional<std::string> name = std::nullopt
103 )
104 : Named(std::move(name)),
105 Alignment(dx, dy, rotation_degree),
106 m_action(action), m_repeat_x(repeat_x), m_repeat_y(repeat_y), m_shift_odd_x(shift_odd_x),
107 m_id(DynamicData::allocate_id()),
108 m_min_radius2(min_radius2)
109 {
110 using namespace amrex::literals; // for _rt and _prt
111
112 if (m_repeat_y == 0_prt && m_shift_odd_x) {
113 throw std::runtime_error("PolygonAperture: shift_odd_x can only be used if repeat_y is set, too!");
114 }
115
116 m_nvert = vertices_x.size();
117 if (m_nvert != vertices_y.size()) {
118 throw std::runtime_error("PolygonAperture: horizontal and vertical vertices arrays must have same length!");
119 }
120
121 if ((vertices_x[0] != vertices_x[m_nvert-1]) ||
122 (vertices_y[0] != vertices_y[m_nvert-1])) {
123 throw std::runtime_error("PolygonAperture: first and last vertex must be exactly equal!");
124 }
125
126 auto& vert = DynamicData::emplace(
127 m_id,
128 std::move(vertices_x),
129 std::move(vertices_y)
130 );
131 m_vertices_x_h_data = vert.x.host_const().data();
132 m_vertices_y_h_data = vert.y.host_const().data();
133 }
134
136 void reverse () { /* no-op */ }
137
139 using BeamOptic::operator();
140
148 void compute_constants ([[maybe_unused]] RefPart const & refpart)
149 {
150 Alignment::compute_constants(refpart);
151
152 // Ensure dynamic vertex data is on GPU and we have fresh pointers to it
153 auto const & vert = *DynamicData::get(m_id);
154 m_vertices_x_d_data = vert.x.device_const().data();
155 m_vertices_y_d_data = vert.y.device_const().data();
156 }
157
170 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
173 T_Real & AMREX_RESTRICT x,
174 T_Real & AMREX_RESTRICT y,
175 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
176 T_Real & AMREX_RESTRICT px,
177 T_Real & AMREX_RESTRICT py,
178 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
179 T_IdCpu & AMREX_RESTRICT idcpu,
180 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
181 ) const
182 {
183 // a complex type with two T_Real
185
186 using namespace amrex::literals; // for _rt and _prt
187 namespace stdx = amrex::simd::stdx;
188 using amrex::Math::powi;
189 using std::fmod;
190 using std::abs;
191 using amrex::arg;
192 using VBool = decltype(x>0_prt);
193
194 // shift due to alignment errors of the element
195 shift_in(x, y, px, py);
196
197 // define intermediate variables
198 amrex::ParticleReal const dx = m_repeat_x * 0.5_prt;
199 amrex::ParticleReal const dy = m_repeat_y * 0.5_prt;
200
201 // shift every 2nd row by half of m_repeat_x
202 T_Real sx = x;
203 T_Real const sy = y;
204 VBool const shift_x = m_shift_odd_x == false ? VBool{false} : fmod(floor((y+dy)/m_repeat_y), 2) != T_Real{0};
205#ifdef AMREX_USE_SIMD
206 amrex::simd::stdx::where(shift_x, sx) += dx;
207#else
208 sx += shift_x ? dx : 0_prt;
209#endif
210
211 // if the aperture is periodic, shift sx,sy coordinates to the fundamental domain
212 T_Real u = (m_repeat_x == 0_prt) ? sx : (fmod(abs(sx)+dx, m_repeat_x)-dx);
213 T_Real v = (m_repeat_y == 0_prt) ? sy : (fmod(abs(sy)+dy, m_repeat_y)-dy);
214
215 // compare against the aperture boundary
216 VBool inside_aperture;
217
218 // calculate whether point is inside polygon or not
219 T_Real r2 = powi<2>(u) + powi<2>(v);
220 // are we inside the minimum radius?
221 if (r2 < m_min_radius2) {
222 inside_aperture = VBool(true); // yes!
223 } else{
224
225#if AMREX_DEVICE_COMPILE
226 amrex::ParticleReal const * vertices_x = m_vertices_x_d_data;
227 amrex::ParticleReal const * vertices_y = m_vertices_y_d_data;
228#else
229 amrex::ParticleReal const * vertices_x = m_vertices_x_h_data;
230 amrex::ParticleReal const * vertices_y = m_vertices_y_h_data;
231#endif
232
233 // no, we have to do the polygon calculation
234
235 /*
236 * The algorithms works as follows:
237 *
238 * You have a polygon (in 2D) described by points V_1, ..., V_n arranged
239 * counter-clockwise. You have a particle with coordinates w = (x,y).
240 * Get the difference vector from the particle to each of
241 * the vertices in turn expressed as a complex number z_k, k=1,n. Now the angle
242 * between the particle and vertex i and vertex j is
243 *
244 * theta_{ij} = arg(\bar{z_i} z_j)
245 * This is easily demonstrated by expressing the complex numbers in exponential
246 * polar notation and also the angle is positive in the counter-clockwise direction.
247 *
248 * Take the sum of all the angles. The angle sum for particle inside the polygon will
249 * be 2 pi. The sum of angles of particle outside of the polygon will be 0.
250 */
251 T_Real thetasum = 0.0;
252 Complex v1conj, v2;
253
254 for (size_t i = 0; i < m_nvert-1; ++i) {
255 v1conj = Complex(u - vertices_x[i], -(v-vertices_y[i]));
256 v2 = Complex(u - vertices_x[i+1], v-vertices_y[i+1]);
257 thetasum += arg(v2 * v1conj);
258 }
259 inside_aperture = abs(thetasum/(2*ablastr::constant::math::pi)) > 1.0e-12_prt;
260 }
261
262 // For transmit (default): invalidate if outside aperture
263 // For absorb: invalidate if inside aperture
264 auto const invalid_mask = m_action == Action::transmit ? !inside_aperture : inside_aperture;
266
267 // undo shift due to alignment errors of the element
268 shift_out(x, y, px, py);
269 }
270
272 using Thin::operator();
273
275 using LinearTransport::operator();
276
287 Map6x6
288 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
289 {
290 return Map6x6::Identity();
291 }
292
297
298 int m_id;
299
300 amrex::ParticleReal m_min_radius2; // minimum radius in which all pass squared
301
302 std::vector<double>::size_type m_nvert = 0;
307
308 };
309
310} // namespace impactx
311
314
315#endif // IMPACTX_POLYGONAPERTURE_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::GpuComplex< amrex::Real > Complex
#define IMPACTX_GPUDATA_EXTERN(ElementType)
Definition dynamicdata.H:169
amrex_particle_real ParticleReal
constexpr T powi(T x) noexcept
__host__ __device__ T arg(const GpuComplex< T > &a_z) noexcept
__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 PolygonAperture.H:60
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition PolygonAperture.H:288
amrex::ParticleReal const * m_vertices_y_h_data
non-owning pointer to host x vertices
Definition PolygonAperture.H:304
amrex::ParticleReal const * m_vertices_y_d_data
non-owning pointer to device x vertices
Definition PolygonAperture.H:306
amrex::ParticleReal m_repeat_y
horizontal period for repeated masking
Definition PolygonAperture.H:295
amrex::ParticleReal const * m_vertices_x_d_data
non-owning pointer to host vertices
Definition PolygonAperture.H:305
std::vector< double >::size_type m_nvert
Definition PolygonAperture.H:302
void compute_constants(RefPart const &refpart)
Definition PolygonAperture.H:148
void reverse()
Definition PolygonAperture.H:136
Action m_action
Definition PolygonAperture.H:293
int m_id
for hexagonal/triangular mask patterns: horizontal shift of every 2nd (odd) vertical period by m_repe...
Definition PolygonAperture.H:298
static constexpr auto type
Definition PolygonAperture.H:61
bool m_shift_odd_x
vertical period for repeated masking
Definition PolygonAperture.H:296
amrex::ParticleReal m_min_radius2
unique PolygonAperture id used for data lookup map
Definition PolygonAperture.H:300
amrex::ParticleReal const * m_vertices_x_h_data
number of vertices
Definition PolygonAperture.H:303
Action
Definition PolygonAperture.H:67
@ absorb
Definition PolygonAperture.H:69
@ transmit
Definition PolygonAperture.H:68
PolygonAperture(std::vector< amrex::ParticleReal > vertices_x, std::vector< amrex::ParticleReal > vertices_y, amrex::ParticleReal min_radius2, amrex::ParticleReal repeat_x, amrex::ParticleReal repeat_y, bool shift_odd_x, Action action, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, std::optional< std::string > name=std::nullopt)
Definition PolygonAperture.H:91
static std::string action_name(Action const &action)
Definition PolygonAperture.cpp:18
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 PolygonAperture.H:172
ImpactXParticleContainer::ParticleType PType
Definition PolygonAperture.H:62
amrex::ParticleReal m_repeat_x
action type (transmit, absorb)
Definition PolygonAperture.H:294
mixin::GPUDataRegistry< PolygonVertices > DynamicData
Definition PolygonAperture.H:64
Definition PolygonAperture.H:47
mixin::TrackedVector< amrex::ParticleReal > y
Definition PolygonAperture.H:49
mixin::TrackedVector< amrex::ParticleReal > x
Definition PolygonAperture.H:48
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:436
static std::shared_ptr< PolygonVertices > const & get(int id)
Definition dynamicdata.H:98
static PolygonVertices & emplace(int id, Args &&... args)
Definition dynamicdata.H:125
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 thin.H:24
Definition TrackedVector.H:49