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/spintransport.H"
22#include "mixin/thin.H"
23#include "mixin/TrackedVector.H"
24
25#include <ablastr/constant.H>
26
27#include <AMReX_Extension.H>
28#include <AMReX_Math.H>
29#include <AMReX_REAL.H>
30#include <AMReX_SIMD.H>
31
32#include <cmath>
33#include <map>
34#include <memory>
35#include <stdexcept>
36#include <vector>
37
38
39namespace impactx::elements
40{
41
52
54 : public mixin::Named,
55 public mixin::BeamOptic<PolygonAperture>,
56 public mixin::LinearTransport<PolygonAperture>,
57 public mixin::Thin,
58 public mixin::Alignment,
61 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
62 {
63 static constexpr auto type = "PolygonAperture";
65
67
68 enum Action
69 {
72 };
73
74 static std::string
75 action_name (Action const & action);
76
94 std::vector< amrex::ParticleReal> vertices_x,
95 std::vector< amrex::ParticleReal> vertices_y,
96 amrex::ParticleReal min_radius2,
97 amrex::ParticleReal repeat_x,
98 amrex::ParticleReal repeat_y,
99 bool shift_odd_x,
100 Action action,
103 amrex::ParticleReal rotation_degree = 0,
104 std::optional<std::string> name = std::nullopt
105 )
106 : Named(std::move(name)),
107 Alignment(dx, dy, rotation_degree),
108 m_action(action), m_repeat_x(repeat_x), m_repeat_y(repeat_y), m_shift_odd_x(shift_odd_x),
109 m_id(DynamicData::allocate_id()),
110 m_min_radius2(min_radius2)
111 {
112 using namespace amrex::literals; // for _rt and _prt
113
114 if (m_repeat_y == 0_prt && m_shift_odd_x) {
115 throw std::runtime_error("PolygonAperture: shift_odd_x can only be used if repeat_y is set, too!");
116 }
117
118 m_nvert = vertices_x.size();
119 if (m_nvert != vertices_y.size()) {
120 throw std::runtime_error("PolygonAperture: horizontal and vertical vertices arrays must have same length!");
121 }
122
123 if ((vertices_x[0] != vertices_x[m_nvert-1]) ||
124 (vertices_y[0] != vertices_y[m_nvert-1])) {
125 throw std::runtime_error("PolygonAperture: first and last vertex must be exactly equal!");
126 }
127
128 auto& vert = DynamicData::emplace(
129 m_id,
130 std::move(vertices_x),
131 std::move(vertices_y)
132 );
133 m_vertices_x_h_data = vert.x.host_const().data();
134 m_vertices_y_h_data = vert.y.host_const().data();
135 }
136
138 void reverse () { /* no-op */ }
139
141 using BeamOptic::operator();
142
150 void compute_constants ([[maybe_unused]] RefPart const & refpart)
151 {
152 Alignment::compute_constants(refpart);
153
154 // Ensure dynamic vertex data is on GPU and we have fresh pointers to it
155 auto const & vert = *DynamicData::get(m_id);
156 m_vertices_x_d_data = vert.x.device_const().data();
157 m_vertices_y_d_data = vert.y.device_const().data();
158 }
159
172 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
175 T_Real const & AMREX_RESTRICT x,
176 T_Real const & AMREX_RESTRICT y,
177 [[maybe_unused]] T_Real const & AMREX_RESTRICT t,
178 [[maybe_unused]] T_Real const & AMREX_RESTRICT px,
179 [[maybe_unused]] T_Real const & AMREX_RESTRICT py,
180 [[maybe_unused]] T_Real const & AMREX_RESTRICT pt,
181 T_IdCpu & AMREX_RESTRICT idcpu,
182 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
183 ) const
184 {
185 // a complex type with two T_Real
187
188 using namespace amrex::literals; // for _rt and _prt
189 namespace stdx = amrex::simd::stdx;
190 using amrex::Math::powi;
191 using std::fmod;
192 using std::abs;
193 using amrex::arg;
194 using VBool = decltype(x>0_prt);
195
196 // define intermediate variables
197 amrex::ParticleReal const dx = m_repeat_x * 0.5_prt;
198 amrex::ParticleReal const dy = m_repeat_y * 0.5_prt;
199
200 // shift every 2nd row by half of m_repeat_x
201 T_Real sx = x;
202 T_Real const sy = y;
203 VBool const shift_x = m_shift_odd_x == false ? VBool{false} : fmod(floor((y+dy)/m_repeat_y), 2) != T_Real{0};
204#ifdef AMREX_USE_SIMD
205 amrex::simd::stdx::where(shift_x, sx) += dx;
206#else
207 sx += shift_x ? dx : 0_prt;
208#endif
209
210 // if the aperture is periodic, shift sx,sy coordinates to the fundamental domain
211 T_Real const u = (m_repeat_x == 0_prt) ? sx : (fmod(abs(sx)+dx, m_repeat_x)-dx);
212 T_Real const v = (m_repeat_y == 0_prt) ? sy : (fmod(abs(sy)+dy, m_repeat_y)-dy);
213
214 // compare against the aperture boundary
215 VBool inside_aperture;
216
217 // calculate whether point is inside polygon or not
218 T_Real const r2 = powi<2>(u) + powi<2>(v);
219 // are we inside the minimum radius?
220 if (r2 < m_min_radius2) {
221 inside_aperture = VBool(true); // yes!
222 } else{
223
224#if AMREX_DEVICE_COMPILE
225 amrex::ParticleReal const * vertices_x = m_vertices_x_d_data;
226 amrex::ParticleReal const * vertices_y = m_vertices_y_d_data;
227#else
228 amrex::ParticleReal const * vertices_x = m_vertices_x_h_data;
229 amrex::ParticleReal const * vertices_y = m_vertices_y_h_data;
230#endif
231
232 // no, we have to do the polygon calculation
233
234 /*
235 * The algorithms works as follows:
236 *
237 * You have a polygon (in 2D) described by points V_1, ..., V_n arranged
238 * counter-clockwise. You have a particle with coordinates w = (x,y).
239 * Get the difference vector from the particle to each of
240 * the vertices in turn expressed as a complex number z_k, k=1,n. Now the angle
241 * between the particle and vertex i and vertex j is
242 *
243 * theta_{ij} = arg(\bar{z_i} z_j)
244 * This is easily demonstrated by expressing the complex numbers in exponential
245 * polar notation and also the angle is positive in the counter-clockwise direction.
246 *
247 * Take the sum of all the angles. The angle sum for particle inside the polygon will
248 * be 2 pi. The sum of angles of particle outside of the polygon will be 0.
249 */
250 T_Real thetasum = 0.0;
251 Complex v1conj, v2;
252
253 for (size_t i = 0; i < m_nvert-1; ++i) {
254 v1conj = Complex(u - vertices_x[i], -(v-vertices_y[i]));
255 v2 = Complex(u - vertices_x[i+1], v-vertices_y[i+1]);
256 thetasum += arg(v2 * v1conj);
257 }
258 inside_aperture = abs(thetasum/(2*ablastr::constant::math::pi)) > 1.0e-12_prt;
259 }
260
261 // For transmit (default): invalidate if outside aperture
262 // For absorb: invalidate if inside aperture
263 auto const invalid_mask = m_action == Action::transmit ? !inside_aperture : inside_aperture;
265 }
266
268 using Thin::operator();
269
286 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
289 T_Real const & AMREX_RESTRICT x,
290 T_Real const & AMREX_RESTRICT y,
291 T_Real const & AMREX_RESTRICT t,
292 T_Real const & AMREX_RESTRICT px,
293 T_Real const & AMREX_RESTRICT py,
294 T_Real const & AMREX_RESTRICT pt,
295 [[maybe_unused]] T_Real const & AMREX_RESTRICT sx,
296 [[maybe_unused]] T_Real const & AMREX_RESTRICT sy,
297 [[maybe_unused]] T_Real const & AMREX_RESTRICT sz,
298 T_IdCpu & AMREX_RESTRICT idcpu,
299 RefPart const & AMREX_RESTRICT refpart
300 ) const
301 {
302 // spin is unaffected
303
304 // phase space push
305 (*this)(x, y, t, px, py, pt, idcpu, refpart);
306 }
307
309 using LinearTransport::operator();
310
321 Map6x6
322 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
323 {
324 // apply the transverse rotation (roll) alignment error
326 }
327
332
333 int m_id;
334
335 amrex::ParticleReal m_min_radius2; // minimum radius in which all pass squared
336
337 std::vector<double>::size_type m_nvert = 0;
342
343 };
344
345} // namespace impactx
346
349
350#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__ detail::where_expression< T > where(bool const mask, T &value)
__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:62
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition PolygonAperture.H:322
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 PolygonAperture.H:174
amrex::ParticleReal const * m_vertices_y_h_data
non-owning pointer to host x vertices
Definition PolygonAperture.H:339
amrex::ParticleReal const * m_vertices_y_d_data
non-owning pointer to device x vertices
Definition PolygonAperture.H:341
amrex::ParticleReal m_repeat_y
horizontal period for repeated masking
Definition PolygonAperture.H:330
amrex::ParticleReal const * m_vertices_x_d_data
non-owning pointer to host vertices
Definition PolygonAperture.H:340
std::vector< double >::size_type m_nvert
Definition PolygonAperture.H:337
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 PolygonAperture.H:288
void compute_constants(RefPart const &refpart)
Definition PolygonAperture.H:150
void reverse()
Definition PolygonAperture.H:138
Action m_action
Definition PolygonAperture.H:328
int m_id
for hexagonal/triangular mask patterns: horizontal shift of every 2nd (odd) vertical period by m_repe...
Definition PolygonAperture.H:333
static constexpr auto type
Definition PolygonAperture.H:63
bool m_shift_odd_x
vertical period for repeated masking
Definition PolygonAperture.H:331
amrex::ParticleReal m_min_radius2
unique PolygonAperture id used for data lookup map
Definition PolygonAperture.H:335
amrex::ParticleReal const * m_vertices_x_h_data
number of vertices
Definition PolygonAperture.H:338
Action
Definition PolygonAperture.H:69
@ absorb
Definition PolygonAperture.H:71
@ transmit
Definition PolygonAperture.H:70
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:93
static std::string action_name(Action const &action)
Definition PolygonAperture.cpp:18
ImpactXParticleContainer::ParticleType PType
Definition PolygonAperture.H:64
amrex::ParticleReal m_repeat_x
action type (transmit, absorb)
Definition PolygonAperture.H:329
mixin::GPUDataRegistry< PolygonVertices > DynamicData
Definition PolygonAperture.H:66
Definition PolygonAperture.H:48
mixin::TrackedVector< amrex::ParticleReal > y
Definition PolygonAperture.H:50
mixin::TrackedVector< amrex::ParticleReal > x
Definition PolygonAperture.H:49
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
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 spintransport.H:36
Definition thin.H:24
Definition TrackedVector.H:49