QMCPACK
RandomSeqGenerator.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef QMCPLUSPLUS_RANDOMSEQUENCEGENERATOR_H
17 #define QMCPLUSPLUS_RANDOMSEQUENCEGENERATOR_H
18 #include <algorithm>
19 #include <type_traits>
20 #include "OhmmsPETE/OhmmsMatrix.h"
22 #include "Particle/MCCoords.hpp"
23 #include "config/stdlib/Constants.h"
24 
25 /*!\fn template<class T> void assignGaussRand(T* restrict a, unsigned n)
26  *\param a the starting pointer
27  *\param n the number of type T to be assigned
28  *\brief Assign Gaussian distributed random numbers using Box-Mueller algorithm. Called by overloaded funtions makeGaussRandom
29  */
30 namespace qmcplusplus
31 {
32 template<class T, class RG>
33 inline void assignGaussRand(T* restrict a, unsigned n, RG& rng)
34 {
35  OHMMS_PRECISION_FULL slightly_less_than_one = 1.0 - std::numeric_limits<OHMMS_PRECISION_FULL>::epsilon();
36  int nm1 = n - 1;
37  OHMMS_PRECISION_FULL temp1, temp2;
38  for (int i = 0; i < nm1; i += 2)
39  {
40  temp1 = std::sqrt(-2.0 * std::log(1.0 - slightly_less_than_one * rng()));
41  temp2 = 2.0 * M_PI * rng();
42  a[i] = temp1 * std::cos(temp2);
43  a[i + 1] = temp1 * std::sin(temp2);
44  }
45  if (n % 2 == 1)
46  {
47  temp1 = std::sqrt(-2.0 * std::log(1.0 - slightly_less_than_one * rng()));
48  temp2 = 2.0 * M_PI * rng();
49  a[nm1] = temp1 * std::cos(temp2);
50  }
51 }
52 
53 /*!\fn template<class T> void assignUniformRand(T* restrict a, unsigned n)
54  *\param a the starting pointer
55  *\param n the number of type T to be assigned
56  *\brief Assign unifor distributed random numbers [0,1)
57  */
58 //template<class T>
59 //inline void assignUniformRand(T* restrict a, unsigned n) {
60 // for(int i=0; i<n;i++) a[i] = Random();
61 // //generate(a,a+n,Random);
62 //}
63 template<class T, class RG>
64 inline void assignUniformRand(T* restrict a, unsigned n, RG& rng)
65 {
66  for (int i = 0; i < n; i++)
67  a[i] = rng();
68 }
69 
70 template<typename T, unsigned D, class RG>
72 {
73  assignGaussRand(&(a[0][0]), a.size() * D, rng);
74 }
75 
76 template<typename T, unsigned D, class RG>
77 inline void makeGaussRandomWithEngine(std::vector<TinyVector<T, D>>& a, RG& rng)
78 {
79  assignGaussRand(&(a[0][0]), a.size() * D, rng);
80 }
81 
82 template<typename T, class RG>
83 inline void makeGaussRandomWithEngine(std::vector<T>& a, RG& rng)
84 {
85  static_assert(std::is_floating_point<T>::value,
86  "makeGaussRandomWithEngine(std::vector<T>...) only implemented for floating point T");
87  assignGaussRand(&(a[0]), a.size(), rng);
88 }
89 
90 template<typename T, class RG>
92 {
93  assignGaussRand(&(a[0]), a.size(), rng);
94 }
95 
96 template<CoordsType CT, class RG>
97 inline void makeGaussRandomWithEngine(MCCoords<CT>& a, RG& rng)
98 {
99  makeGaussRandomWithEngine(a.positions, rng);
100  if constexpr (CT == CoordsType::POS_SPIN)
101  makeGaussRandomWithEngine(a.spins, rng);
102 }
103 
104 } // namespace qmcplusplus
105 #endif
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
MakeReturn< UnaryNode< FnSin, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sin(const Vector< T1, C1 > &l)
Attaches a unit to a Vector for IO.
void assignGaussRand(T *restrict a, unsigned n, RG &rng)
size_type size() const
return the current size
Definition: OhmmsVector.h:162
MakeReturn< UnaryNode< FnCos, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t cos(const Vector< T1, C1 > &l)
void assignUniformRand(T *restrict a, unsigned n, RG &rng)
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
Declaraton of ParticleAttrib<T> derived from Vector.
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
void makeGaussRandomWithEngine(ParticleAttrib< TinyVector< T, D >> &a, RG &rng)