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 
13 #include <AMReX_Random.H>
14 #include <AMReX_REAL.H>
15 
16 #include <cmath>
17 
18 
19 namespace impactx::distribution
20 {
21  struct KVdist
22  {
33  KVdist(amrex::ParticleReal const sigx, amrex::ParticleReal const sigy,
34  amrex::ParticleReal const sigt,amrex::ParticleReal const sigpx,
35  amrex::ParticleReal const sigpy,amrex::ParticleReal const sigpt,
36  amrex::ParticleReal const muxpx=0.0, amrex::ParticleReal const muypy=0.0,
37  amrex::ParticleReal const mutpt=0.0
38  )
39  : m_sigmaX(sigx),m_sigmaY(sigy),m_sigmaT(sigt),m_sigmaPx(sigpx),m_sigmaPy(sigpy),
40  m_sigmaPt(sigpt),m_muxpx(muxpx),m_muypy(muypy),m_mutpt(mutpt)
41  {
42  }
43 
56  amrex::ParticleReal & x,
57  amrex::ParticleReal & y,
58  amrex::ParticleReal & t,
59  amrex::ParticleReal & px,
60  amrex::ParticleReal & py,
61  amrex::ParticleReal & pt,
62  amrex::RandomEngine const& engine) const {
63 
64  using namespace amrex::literals;
65 
66  amrex::ParticleReal v,phi,r,beta,p,u1,u2,ln1;
67  amrex::ParticleReal root,a1,a2;
68 
69  constexpr amrex::ParticleReal pi = 3.14159265358979_prt;
70 
71  // Sample and transform to define (x,y):
72  v = amrex::Random(engine);
73  phi = amrex::Random(engine);
74  phi = 2_prt*pi*phi;
75  r = sqrt(v);
76  x = r*cos(phi);
77  y = r*sin(phi);
78 
79  // Sample and transform to define (px,py):
80  beta = amrex::Random(engine);
81  beta = 2_prt*pi*beta;
82  p = sqrt(1_prt-pow(r,2));
83  px = p*cos(beta);
84  py = p*sin(beta);
85 
86  // Sample and transform to define (t,pt):
87  t = amrex::Random(engine);
88  t = 2.0_prt*(t-0.5_prt);
89  u1 = amrex::Random(engine);
90  u2 = amrex::Random(engine);
91  ln1 = sqrt(-2_prt*log(u1));
92  pt = ln1*cos(2_prt*pi*u2);
93 
94  // Scale to produce the identity covariance matrix:
95  amrex::ParticleReal const c = sqrt(3.0_prt);
96  x = 2_prt*x;
97  y = 2_prt*y;
98  t = c*t;
99  px = 2_prt*px;
100  py = 2_prt*py;
101  // pt = pt;
102 
103  // Transform to produce the desired second moments/correlations:
104  root = sqrt(1.0_prt-m_muxpx*m_muxpx);
105  a1 = m_sigmaX*x/root;
106  a2 = m_sigmaPx*(-m_muxpx*x/root+px);
107  x = a1;
108  px = a2;
109  root = sqrt(1.0_prt-m_muypy*m_muypy);
110  a1 = m_sigmaY*y/root;
111  a2 = m_sigmaPy*(-m_muypy*y/root+py);
112  y = a1;
113  py = a2;
114  root = sqrt(1.0_prt-m_mutpt*m_mutpt);
115  a1 = m_sigmaT*t/root;
116  a2 = m_sigmaPt*(-m_mutpt*t/root+pt);
117  t = a1;
118  pt = a2;
119  }
120  private:
121  amrex::ParticleReal m_sigmaX,m_sigmaY,m_sigmaT;
122  amrex::ParticleReal m_sigmaPx,m_sigmaPy,m_sigmaPt;
123  amrex::ParticleReal m_muxpx,m_muypy,m_mutpt;
124  };
125 
126 } // namespace impactx::distribution
127 
128 #endif // IMPACTX_DISTRIBUTION_KVDIST
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
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:26
c
beta
Definition: KVdist.H:22
amrex::ParticleReal m_sigmaPy
Definition: KVdist.H:122
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: KVdist.H:55
amrex::ParticleReal m_sigmaT
Definition: KVdist.H:121
amrex::ParticleReal m_mutpt
Definition: KVdist.H:123
amrex::ParticleReal m_sigmaPx
related RMS sizes (length)
Definition: KVdist.H:122
KVdist(amrex::ParticleReal const sigx, amrex::ParticleReal const sigy, amrex::ParticleReal const sigt, amrex::ParticleReal const sigpx, amrex::ParticleReal const sigpy, amrex::ParticleReal const sigpt, amrex::ParticleReal const muxpx=0.0, amrex::ParticleReal const muypy=0.0, amrex::ParticleReal const mutpt=0.0)
Definition: KVdist.H:33
amrex::ParticleReal m_sigmaPt
Definition: KVdist.H:122
amrex::ParticleReal m_muxpx
RMS momentum.
Definition: KVdist.H:123
amrex::ParticleReal m_muypy
Definition: KVdist.H:123
amrex::ParticleReal m_sigmaX
Definition: KVdist.H:121
amrex::ParticleReal m_sigmaY
Definition: KVdist.H:121