ImpactX
KVdist.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_DISTRIBUTION_KVDIST
11 #define IMPACTX_DISTRIBUTION_KVDIST
12 
14 
15 #include <ablastr/constant.H>
16 
17 #include <AMReX_Random.H>
18 #include <AMReX_REAL.H>
19 
20 #include <cmath>
21 
22 
23 namespace impactx::distribution
24 {
25  struct KVdist
26  {
38  amrex::ParticleReal sigx,
39  amrex::ParticleReal sigy,
40  amrex::ParticleReal sigt,
41  amrex::ParticleReal sigpx,
42  amrex::ParticleReal sigpy,
43  amrex::ParticleReal sigpt,
44  amrex::ParticleReal muxpx=0.0,
45  amrex::ParticleReal muypy=0.0,
46  amrex::ParticleReal mutpt=0.0
47  )
48  : m_sigmaX(sigx), m_sigmaY(sigy), m_sigmaT(sigt), m_sigmaPx(sigpx), m_sigmaPy(sigpy),
49  m_sigmaPt(sigpt), m_muxpx(muxpx), m_muypy(muypy), m_mutpt(mutpt)
50  {
51  }
52 
60  void initialize ([[maybe_unused]] amrex::ParticleReal bunch_charge, [[maybe_unused]] RefPart const & ref)
61  {
62  }
63 
68  void
70  {
71  }
72 
85  amrex::ParticleReal & AMREX_RESTRICT x,
86  amrex::ParticleReal & AMREX_RESTRICT y,
87  amrex::ParticleReal & AMREX_RESTRICT t,
88  amrex::ParticleReal & AMREX_RESTRICT px,
89  amrex::ParticleReal & AMREX_RESTRICT py,
90  amrex::ParticleReal & AMREX_RESTRICT pt,
91  amrex::RandomEngine const & engine
92  ) const
93  {
94  using namespace amrex::literals;
96 
97  amrex::ParticleReal v,phi,r,beta,p,u1,u2,ln1;
98  amrex::ParticleReal root,a1,a2;
99 
100  // Sample and transform to define (x,y):
101  v = amrex::Random(engine);
102  phi = amrex::Random(engine);
103  phi = 2_prt*pi*phi;
104  r = sqrt(v);
105  x = r*cos(phi);
106  y = r*sin(phi);
107 
108  // Sample and transform to define (px,py):
109  beta = amrex::Random(engine);
110  beta = 2_prt*pi*beta;
111  p = sqrt(1_prt-pow(r,2));
112  px = p*cos(beta);
113  py = p*sin(beta);
114 
115  // Sample and transform to define (t,pt):
116  t = amrex::Random(engine);
117  t = 2.0_prt*(t-0.5_prt);
118  u1 = amrex::Random(engine);
119  u2 = amrex::Random(engine);
120  ln1 = sqrt(-2_prt*log(u1));
121  pt = ln1*cos(2_prt*pi*u2);
122 
123  // Scale to produce the identity covariance matrix:
124  amrex::ParticleReal const c = sqrt(3.0_prt);
125  x = 2_prt*x;
126  y = 2_prt*y;
127  t = c*t;
128  px = 2_prt*px;
129  py = 2_prt*py;
130  // pt = pt;
131 
132  // Transform to produce the desired second moments/correlations:
133  root = sqrt(1.0_prt-m_muxpx*m_muxpx);
134  a1 = m_sigmaX*x/root;
135  a2 = m_sigmaPx*(-m_muxpx*x/root+px);
136  x = a1;
137  px = a2;
138  root = sqrt(1.0_prt-m_muypy*m_muypy);
139  a1 = m_sigmaY*y/root;
140  a2 = m_sigmaPy*(-m_muypy*y/root+py);
141  y = a1;
142  py = a2;
143  root = sqrt(1.0_prt-m_mutpt*m_mutpt);
144  a1 = m_sigmaT*t/root;
145  a2 = m_sigmaPt*(-m_mutpt*t/root+pt);
146  t = a1;
147  pt = a2;
148  }
149 
150  private:
151  amrex::ParticleReal m_sigmaX, m_sigmaY, m_sigmaT;
152  amrex::ParticleReal m_sigmaPx, m_sigmaPy, m_sigmaPt;
153  amrex::ParticleReal m_muxpx, m_muypy, m_mutpt;
154  };
155 
156 } // namespace impactx::distribution
157 
158 #endif // IMPACTX_DISTRIBUTION_KVDIST
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
static constexpr amrex::Real pi
constexpr std::enable_if_t< std::is_floating_point< T >::value, T > pi()
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > log(const GpuComplex< T > &a_z) noexcept
Real Random()
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > pow(const GpuComplex< T > &a_z, const T &a_y) noexcept
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
Definition: All.H:27
@ t
fixed t as the independent variable
c
beta
Definition: ReferenceParticle.H:30
Definition: KVdist.H:26
amrex::ParticleReal m_sigmaPy
Definition: KVdist.H:152
KVdist(amrex::ParticleReal sigx, amrex::ParticleReal sigy, amrex::ParticleReal sigt, amrex::ParticleReal sigpx, amrex::ParticleReal sigpy, amrex::ParticleReal sigpt, amrex::ParticleReal muxpx=0.0, amrex::ParticleReal muypy=0.0, amrex::ParticleReal mutpt=0.0)
Definition: KVdist.H:37
amrex::ParticleReal m_sigmaT
Definition: KVdist.H:151
amrex::ParticleReal m_mutpt
Definition: KVdist.H:153
void finalize()
Definition: KVdist.H:69
void initialize([[maybe_unused]] amrex::ParticleReal bunch_charge, [[maybe_unused]] RefPart const &ref)
Definition: KVdist.H:60
amrex::ParticleReal m_sigmaPx
related RMS sizes (length)
Definition: KVdist.H:152
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(amrex::ParticleReal &AMREX_RESTRICT x, amrex::ParticleReal &AMREX_RESTRICT y, amrex::ParticleReal &AMREX_RESTRICT t, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py, amrex::ParticleReal &AMREX_RESTRICT pt, amrex::RandomEngine const &engine) const
Definition: KVdist.H:84
amrex::ParticleReal m_sigmaPt
Definition: KVdist.H:152
amrex::ParticleReal m_muxpx
RMS momentum.
Definition: KVdist.H:153
amrex::ParticleReal m_muypy
Definition: KVdist.H:153
amrex::ParticleReal m_sigmaX
Definition: KVdist.H:151
amrex::ParticleReal m_sigmaY
Definition: KVdist.H:151