ImpactX
Loading...
Searching...
No Matches
Triangle.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: Ji Qiang, Axel Huebl
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_DISTRIBUTION_TRIANGLE
11#define IMPACTX_DISTRIBUTION_TRIANGLE
12
14
15#include <ablastr/constant.H>
16
17#include <AMReX_Random.H>
18#include <AMReX_REAL.H>
19
20#include <cmath>
21
22
24{
25 struct Triangle
26 {
42 amrex::ParticleReal const lambdax, amrex::ParticleReal const lambday,
43 amrex::ParticleReal const lambdat, amrex::ParticleReal const lambdapx,
44 amrex::ParticleReal const lambdapy, amrex::ParticleReal const lambdapt,
45 amrex::ParticleReal const muxpx=0.0, amrex::ParticleReal const muypy=0.0,
46 amrex::ParticleReal const mutpt=0.0, amrex::ParticleReal const meanx=0.0,
47 amrex::ParticleReal const meany=0.0, amrex::ParticleReal const meant=0.0,
48 amrex::ParticleReal const meanpx=0.0, amrex::ParticleReal const meanpy=0.0,
49 amrex::ParticleReal const meanpt=0.0, amrex::ParticleReal const dispx=0.0,
50 amrex::ParticleReal const disppx=0.0, amrex::ParticleReal const dispy=0.0,
51 amrex::ParticleReal const disppy=0.0
52 )
53 : m_lambdaX(lambdax), m_lambdaY(lambday), m_lambdaT(lambdat), m_lambdaPx(lambdapx), m_lambdaPy(lambdapy),
54 m_lambdaPt(lambdapt), m_muxpx(muxpx), m_muypy(muypy), m_mutpt(mutpt), m_meanx(meanx), m_meany(meany),
55 m_meant(meant), m_meanpx(meanpx), m_meanpy(meanpy), m_meanpt(meanpt), m_dispx(dispx), m_disppx(disppx),
56 m_dispy(dispy), m_disppy(disppy)
57 {
58 }
59
67 void initialize ([[maybe_unused]] amrex::ParticleReal bunch_charge, [[maybe_unused]] RefPart const & ref)
68 {
69 }
70
75 void
77 {
78 }
79
92 amrex::ParticleReal & x,
93 amrex::ParticleReal & y,
94 amrex::ParticleReal & t,
95 amrex::ParticleReal & px,
96 amrex::ParticleReal & py,
97 amrex::ParticleReal & pt,
98 amrex::RandomEngine const& engine
99 ) const
100 {
101
102 using namespace amrex::literals;
104
105 amrex::ParticleReal ln1, norm, u0, u1, u2;
106 amrex::ParticleReal g1, g2, g3, g4, g5;
107 amrex::ParticleReal d, root, a1, a2;
108
109 // Sample the t coordinate for a ramped triangular profile (unit
110 // variance):
111 u0 = amrex::Random(engine);
112 t = std::sqrt(2_prt)*(2_prt-3_prt*std::sqrt(u0));
113
114 // Generate five standard normal random variables using Box-Muller:
115 u1 = amrex::Random(engine);
116 u2 = amrex::Random(engine);
117 ln1 = std::sqrt(-2_prt*std::log(u1));
118 g1 = ln1 * std::cos(2_prt*pi*u2);
119 g2 = ln1 * std::sin(2_prt*pi*u2);
120 u1 = amrex::Random(engine);
121 u2 = amrex::Random(engine);
122 ln1 = std::sqrt(-2_prt*std::log(u1));
123 g3 = ln1 * std::cos(2_prt*pi*u2);
124 g4 = ln1 * std::sin(2_prt*pi*u2);
125 u1 = amrex::Random(engine);
126 u2 = amrex::Random(engine);
127 ln1 = std::sqrt(-2_prt*std::log(u1));
128 g5 = ln1 * std::cos(2_prt*pi*u2);
129
130 // Use one of these normal random variables for pt:
131 pt = g5;
132
133 // Normalize the rest to produce uniform samples on the unit sphere:
134 norm = std::sqrt(g1*g1+g2*g2+g3*g3+g4*g4);
135 g1 /= norm;
136 g2 /= norm;
137 g3 /= norm;
138 g4 /= norm;
139
140 // Scale to produce uniform samples in a 4D ball (unit variance):
141 d = 4_prt; // unit ball dimension
142 u1 = amrex::Random(engine); // uniform sample
143 u2 = std::sqrt(d+2_prt) * std::pow(u1,1_prt/d);
144 x = g1*u2;
145 y = g2*u2;
146 px = g3*u2;
147 py = g4*u2;
148
149 // Transform to produce the desired second moments/correlations:
150 root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
151 a1 = m_lambdaX * x / root;
152 a2 = m_lambdaPx * (-m_muxpx * x / root + px);
153 x = a1;
154 px = a2;
155 root = std::sqrt(1.0_prt-m_muypy*m_muypy);
156 a1 = m_lambdaY * y / root;
157 a2 = m_lambdaPy * (-m_muypy * y / root + py);
158 y = a1;
159 py = a2;
160 root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
161 a1 = m_lambdaT * t / root;
162 a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
163 t = a1;
164 pt = a2;
165
166 // Apply dispersive correlations
167 x = x - m_dispx * pt;
168 px = px - m_disppx * pt;
169 y = y - m_dispy * pt;
170 py = py - m_disppy * pt;
171
172 // Apply centroid translation
173 x = x + m_meanx;
174 px = px + m_meanpx;
175 y = y + m_meany;
176 py = py + m_meanpy;
177 t = t + m_meant;
178 pt = pt + m_meanpt;
179 }
180
181 amrex::ParticleReal m_lambdaX, m_lambdaY, m_lambdaT;
182 amrex::ParticleReal m_lambdaPx, m_lambdaPy, m_lambdaPt;
183 amrex::ParticleReal m_muxpx, m_muypy, m_mutpt;
184 amrex::ParticleReal m_meanx, m_meany, m_meant;
185 amrex::ParticleReal m_meanpx, m_meanpy, m_meanpt;
186 amrex::ParticleReal m_dispx, m_disppx, m_dispy, m_disppy;
187 };
188
189} // namespace impactx::distribution
190
191#endif // IMPACTX_DISTRIBUTION_TRIANGLE
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
static constexpr amrex::Real pi
Real Random()
Definition All.H:27
@ t
fixed t as the independent variable
Definition ImpactXParticleContainer.H:38
Definition ReferenceParticle.H:31
Triangle(amrex::ParticleReal const lambdax, amrex::ParticleReal const lambday, amrex::ParticleReal const lambdat, amrex::ParticleReal const lambdapx, amrex::ParticleReal const lambdapy, amrex::ParticleReal const lambdapt, amrex::ParticleReal const muxpx=0.0, amrex::ParticleReal const muypy=0.0, amrex::ParticleReal const mutpt=0.0, amrex::ParticleReal const meanx=0.0, amrex::ParticleReal const meany=0.0, amrex::ParticleReal const meant=0.0, amrex::ParticleReal const meanpx=0.0, amrex::ParticleReal const meanpy=0.0, amrex::ParticleReal const meanpt=0.0, amrex::ParticleReal const dispx=0.0, amrex::ParticleReal const disppx=0.0, amrex::ParticleReal const dispy=0.0, amrex::ParticleReal const disppy=0.0)
Definition Triangle.H:41
amrex::ParticleReal m_lambdaPy
Definition Triangle.H:182
amrex::ParticleReal m_lambdaPx
related position axis intercepts (length) of the phase space ellipse
Definition Triangle.H:182
amrex::ParticleReal m_disppx
Definition Triangle.H:186
void initialize(amrex::ParticleReal bunch_charge, RefPart const &ref)
Definition Triangle.H:67
amrex::ParticleReal m_meanpt
Definition Triangle.H:185
amrex::ParticleReal m_meant
Definition Triangle.H:184
amrex::ParticleReal m_lambdaT
Definition Triangle.H:181
amrex::ParticleReal m_meanx
correlation length-momentum
Definition Triangle.H:184
amrex::ParticleReal m_lambdaX
Definition Triangle.H:181
amrex::ParticleReal m_mutpt
Definition Triangle.H:183
amrex::ParticleReal m_dispy
Definition Triangle.H:186
amrex::ParticleReal m_dispx
momentum coordinates of centroid offset
Definition Triangle.H:186
amrex::ParticleReal m_lambdaY
Definition Triangle.H:181
amrex::ParticleReal m_disppy
Definition Triangle.H:186
amrex::ParticleReal m_muypy
Definition Triangle.H:183
amrex::ParticleReal m_lambdaPt
Definition Triangle.H:182
amrex::ParticleReal m_meanpy
Definition Triangle.H:185
amrex::ParticleReal m_meany
Definition Triangle.H:184
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(amrex::ParticleReal &x, amrex::ParticleReal &y, amrex::ParticleReal &t, amrex::ParticleReal &px, amrex::ParticleReal &py, amrex::ParticleReal &pt, amrex::RandomEngine const &engine) const
Definition Triangle.H:91
amrex::ParticleReal m_meanpx
spatial coordinates of centroid offset
Definition Triangle.H:185
void finalize()
Definition Triangle.H:76
amrex::ParticleReal m_muxpx
related momentum axis intercepts of the phase space ellipse
Definition Triangle.H:183