QMCPACK
test_random_seq.cpp
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: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
8 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
9 //
10 // File created by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 
16 #include "Utilities/FakeRandom.h"
17 #include "Utilities/StdRandom.h"
18 #include "Message/Communicate.h"
19 #include "OhmmsPETE/OhmmsMatrix.h"
20 #include "OhmmsPETE/TinyVector.h"
21 #include <vector>
22 
23 using std::cerr;
24 using std::endl;
25 using std::vector;
27 
28 #include <stdio.h>
29 #include <string>
30 
31 using std::string;
32 
33 namespace qmcplusplus
34 {
35 TEST_CASE("gaussian random array length 1", "[particle_base]")
36 {
37  FakeRandom rg;
38  std::vector<double> a(2);
39  assignGaussRand(a.data(), 1, rg);
40 
41  // assuming RNG input is 0.5
42  CHECK(a[0] == Approx(-1.1774100224305424));
43  CHECK(a[1] == Approx(0.0)); // ensure no overflow
44 }
45 
46 TEST_CASE("gaussian random array length 2", "[particle_base]")
47 {
48  FakeRandom rg;
49  std::vector<double> a(3);
50  assignGaussRand(a.data(), 2, rg);
51 
52  // assuming RNG input is 0.5
53  CHECK(a[0] == Approx(-1.1774100224305424));
54  CHECK(a[1] == Approx(1.4419114152535772e-16));
55  CHECK(a[2] == Approx(0.0)); // ensure no overflow
56 }
57 
58 TEST_CASE("gaussian random array length 3", "[particle_base]")
59 {
60  FakeRandom rg;
61  std::vector<double> a(4);
62  assignGaussRand(a.data(), 3, rg);
63 
64  // assuming RNG input is 0.5
65  CHECK(a[0] == Approx(-1.1774100224305424));
66  CHECK(a[1] == Approx(1.4419114152535772e-16));
67  CHECK(a[2] == Approx(-1.1774100224305424));
68  CHECK(a[3] == Approx(0.0)); // ensure no overflow
69 }
70 
71 TEST_CASE("gaussian random particle attrib array length 1", "[particle_base]")
72 {
73  FakeRandom rg;
75  PA.resize(1);
77 
78  // assuming RNG input is 0.5
79  CHECK(PA[0][0] == Approx(-1.1774100224305424));
80 }
81 
82 TEST_CASE("gaussian random input one", "[particle_base]")
83 {
84  FakeRandom rg;
85  rg.set_value(1.0);
86  std::vector<double> a(2);
87  assignGaussRand(a.data(), 2, rg);
88 
89  // uniform RNG input is 1.0
90  // most uniform RNGs do not produce 1.0 exactly (open interval),
91  // but the code is there to prevent it, so a test.
92  CHECK(a[0] == Approx(8.49042441685));
93  CHECK(a[1] == Approx(0.0)); // ensure no overflow
94 }
95 
96 TEST_CASE("gaussian random input zero", "[particle_base]")
97 {
98  FakeRandom rg;
99  rg.set_value(0.0);
100  std::vector<double> a(2);
101  assignGaussRand(a.data(), 2, rg);
102 
103  // uniform RNG input is 0.0
104  CHECK(a[0] == Approx(0.0));
105  CHECK(a[1] == Approx(0.0));
106 }
107 
108 TEST_CASE("makeGaussRandomWithEngine(MCCoords...)", "[particle_base]")
109 {
110  int size_test = 7;
111  std::vector<double> gauss_random_vals(size_test * 3 + (size_test * 3) % 2 + size_test);
112  {
113  StdRandom<QMCTraits::FullPrecRealType> rng;
115  }
116 
117  auto checkRs = [&](auto& rs) {
118  for (int i = 0; i < size_test; ++i)
119  {
120  CHECK(Approx(gauss_random_vals[3 * i]) == rs[i][0]);
121  CHECK(Approx(gauss_random_vals[3 * i + 1]) == rs[i][1]);
122  CHECK(Approx(gauss_random_vals[3 * i + 2]) == rs[i][2]);
123  }
124  };
125 
126  MCCoords<CoordsType::POS> mc_coords_rs(size_test);
127  {
128  StdRandom<QMCTraits::FullPrecRealType> rng;
131  }
132  MCCoords<CoordsType::POS_SPIN> mc_coords_rsspins(size_test);
133  {
134  StdRandom<QMCTraits::FullPrecRealType> rng;
137  // Mod 2 is result of how gaussianDistribution is generated.
138  int offset_for_rs = (3 * size_test) + (3 * size_test) % 2;
139  for (int i = 0; i < size_test; ++i)
141  }
142 }
143 
144 
145 } // namespace qmcplusplus
void resize(size_type n, Type_t val=Type_t())
Resize the container.
Definition: OhmmsVector.h:166
std::vector< QMCTraits::PosType > positions
Definition: MCCoords.hpp:60
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
Attaches a unit to a Vector for IO.
std::vector< QMCTraits::FullPrecRealType > spins
Definition: MCCoords.hpp:61
std::vector< QMCTraits::PosType > positions
Definition: MCCoords.hpp:45
void assignGaussRand(T *restrict a, unsigned n, RG &rng)
std::vector< double > gauss_random_vals(size_test *3+(size_test *3) % 2+size_test)
MCCoords< CoordsType::POS > mc_coords_rs(size_test)
MCCoords< CoordsType::POS_SPIN > mc_coords_rsspins(size_test)
A minimally functional wrapper for the since c++11 <random>
void set_value(double val)
Definition: FakeRandom.cpp:21
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void makeGaussRandomWithEngine(ParticleAttrib< TinyVector< T, D >> &a, RG &rng)