QMCPACK
test_StdRandom.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) 2023 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 // Steven Hahn, hahnse@ornl.gov, Oak Ridge National lab
9 //
10 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 
16 #include "Utilities/StdRandom.h"
17 
18 #include <vector>
19 
20 namespace qmcplusplus
21 {
22 
23 TEST_CASE("StdRandom mt19937 determinism", "[utilities]")
24 {
25  // Verify StdRandom (MT19937 internally) generates a fixed sequence given an fixed initial seed
26  // This is not guaranteed by Boost but appears to be the case
27  StdRandom<double> our_rng(13);
28  std::vector<double> expected = {0.7777024102, 0.6073413305, 0.237541216};
29  for (auto i = 0; i < expected.size(); ++i)
30  CHECK(our_rng() == Approx(expected[i]));
31 }
32 
33 TEST_CASE("StdRandom save and load", "[utilities]")
34 {
35  using DoubleRNG = StdRandom<double>;
36  DoubleRNG rng;
37 
38  rng.init(111);
39 
40  std::vector<double> rng_doubles(100,0.0);
41  for(auto& elem : rng_doubles)
42  elem = rng();
43 
44  std::vector<DoubleRNG::uint_type> state;
45 
46  rng.save(state);
47 
48  CHECK(state.size() == rng.state_size());
49 
50  DoubleRNG rng2;
51  rng2.init(110);
52  rng2.load(state);
53 
54  CHECK(rng2() == rng());
55 }
56 
57 TEST_CASE("StdRandom clone", "[utilities]")
58 {
59  using DoubleRNG = StdRandom<double>;
60  DoubleRNG rng;
61 
62  rng.init(111);
63  std::vector<double> rng_doubles(100, 0.0);
64  for (auto& elem : rng_doubles)
65  elem = rng();
66 
67  std::vector<DoubleRNG::uint_type> state1;
68  rng.save(state1);
69 
70  std::stringstream stream1;
71  rng.write(stream1);
72 
73  auto rng2 = rng.makeClone();
74  std::vector<DoubleRNG::uint_type> state2;
75  rng2->save(state2);
76 
77  CHECK(state1.size() == state2.size());
78  CHECK(state1 == state2);
79 
80  std::stringstream stream2;
81  rng2->write(stream2);
82 
83  CHECK(stream1.str() == stream2.str());
84 
85  CHECK((*rng2)() == rng());
86  CHECK((*rng2)() == rng());
87 }
88 
89 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
A minimally functional wrapper for the since c++11 <random>
void init(int iseed_in) override
Definition: StdRandom.h:64
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))