CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testVectorSave.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: testVectorSave.cc,v 1.3 2010/06/16 17:24:53 garren Exp $
3 // ----------------------------------------------------------------------
4 #include "CLHEP/Random/Randomize.h"
5 #include "CLHEP/Random/NonRandomEngine.h"
6 #include "CLHEP/Random/defs.h"
7 #include <iostream>
8 #include <iomanip>
9 #include <vector>
10 
11 #define CLEAN_OUTPUT
12 #ifdef CLEAN_OUTPUT
13  std::ofstream output("testVectorSave.cout");
14 #else
15  std::ostream & output = std::cout;
16 #endif
17 
18 // Normally on for routine validation:
19 
20 #ifdef TURNOFF
21 #endif
22 
23 #define TEST_VECTOR_ENGINE_RESTORE
24 
25 #define VERBOSER
26 #define VERBOSER2
27 
28 using namespace CLHEP;
29 
30 // Absolutely Safe Equals Without Registers Screwing Us Up
31 bool equals01(const std::vector<double> &ab) {
32  return ab[1]==ab[0];
33 }
34 bool equals(double a, double b) {
35  std::vector<double> ab(2);
36  ab[0]=a; ab[1]=b;
37  return (equals01(ab));
38 }
39 
40 std::vector<double> aSequence(int n) {
41  std::vector<double> v;
42  DualRand e(13542);
43  RandFlat f(e);
44  for (int i=0; i<n; i++) {
45  v.push_back(f());
46  }
47  return v;
48 }
49 
50 
51 // ----------- Vector restore of engines -----------
52 
53 // test the problem reported in bug #44156
54 // Note that this test always works on a 32bit machine
55 template <class E>
56 int vectorTest64(int n) {
57  output << "Vector restore 64bit test for " << E::engineName() << "\n";
58 
59  E e;
60  double x = 0;
61  for (int i=0; i<n; i++) x += e.flat();
62  std::vector<unsigned long> v = e.put();
63  x = e.flat();
64  output << "x = " << x << std::endl;
65 
66  E f;
67  v[0] &= 0xffffffffUL;
68  f.get(v);
69  double y = f.flat();
70  output << "y = " << y << std::endl;
71  if( x != y ) return n;
72 
73  return 0;
74 }
75 // special case for NonRandomEngine
76 template <>
78  output << "Vector restore 64bit test for " << NonRandomEngine::engineName() << "\n";
79 
80  std::vector<double> nonRand = aSequence(500);
81  NonRandomEngine e;
82  e.setRandomSequence(&nonRand[0], nonRand.size());
83 
84  double x = 0;
85  for (int i=0; i<n; i++) x += e.flat();
86  std::vector<unsigned long> v = e.put();
87  x = e.flat();
88  output << "x = " << x << std::endl;
89 
91  f.setRandomSequence(&nonRand[0], nonRand.size());
92 
93  v[0] &= 0xffffffffUL;
94  f.get(v);
95  double y = f.flat();
96  output << "y = " << y << std::endl;
97  if( x != y ) return n;
98 
99  return 0;
100 }
101 
102 template <class E>
103 std::vector<unsigned long> vectorRestore1(int n, std::vector<double> & v) {
104  output << "Vector restore for " << E::engineName() << "\n";
105  E e(97538466);
106  double r=0;
107  for (int i=0; i<n; i++) r += e.flat();
108  std::vector<unsigned long> state = e.put();
109  for (int j=0; j<25; j++) v.push_back(e.flat());
110 #ifdef VERBOSER2
111  output << "First four of v are: "
112  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
113 #endif
114  return state;
115 }
116 
117 template <>
118 std::vector<unsigned long>
119 vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
120 #ifdef VERBOSER2
121  output << "Vector restore for " << NonRandomEngine::engineName() << "\n";
122 #endif
123  std::vector<double> nonRand = aSequence(500);
124  NonRandomEngine e;
125  e.setRandomSequence(&nonRand[0], nonRand.size());
126  double r=0;
127  for (int i=0; i<n; i++) r += e.flat();
128  std::vector<unsigned long> state = e.put();
129  for (int j=0; j<25; j++) v.push_back(e.flat());
130 #ifdef VERBOSER2
131  output << "First four of v are: "
132  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
133 #endif
134  return state;
135 }
136 
137 template <class E>
138 int vectorRestore2(const std::vector<unsigned long> state,
139  const std::vector<double> & v) {
140  int stat = 0;
141  std::vector<double> k;
142  HepRandomEngine * a;
144  if (!a) {
145  std::cout << "???? could not restore engine state from vector for "
146  << E::engineName() << "\n";
147  #ifdef CLEAN_OUTPUT
148  output << "???? could not restore engine state from vector for "
149  << E::engineName() << "\n";
150  #endif
151  stat |= 1048576;
152  return stat;
153  }
154  if (a->name() != E::engineName()) {
155  #ifdef CLEAN_OUTPUT
156  std::cout << "???? restored engine state from vector for "
157  << E::engineName() << "to different type of engine: "
158  << a->name() << "\n"
159  << "There is probably a clash in CRC hashes for these two names!\n";
160  output << "???? restored engine state from vector for "
161  << E::engineName() << "to different type of engine: "
162  << a->name() << "\n"
163  << "There is probably a clash in CRC hashes for these two names!\n";
164  #endif
165  stat |= 1048576;
166  return stat;
167  }
168  for (int j=0; j<25; j++) k.push_back(a->flat());
169  delete a;
170 #ifdef VERBOSER2
171  output << "First four of k are: "
172  << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
173 #endif
174  for (int m=0; m<25; m++) {
175  if ( v[m] != k[m] ) {
176  std::cout << "???? Incorrect vector restored value for anonymous engine: "
177  << E::engineName() << "\n";
178  #ifdef CLEAN_OUTPUT
179  output << "???? Incorrect vector restored value for anonymous engine: "
180  << E::engineName() << "\n";
181  #endif
182  stat |= 1048576;
183  return stat;
184  }
185  }
186  return stat;
187 }
188 
189 
190 template <class E>
191 int vectorRestore(int n) {
192  std::vector<double> v;
193  int status1 = vectorTest64<E>(n);
194  std::vector<unsigned long> state = vectorRestore1<E>(n,v);
195  int status2 = vectorRestore2<E>(state, v);
196  return (status1 | status2);
197 }
198 
199 
200 
201 // ---------------------------------------------
202 // ---------------------------------------------
203 // ---------------------------------------------
204 
205 
206 int main() {
207  int stat = 0;
208 
209 #ifdef TEST_VECTOR_ENGINE_RESTORE
210  output << "\n=================================\n";
211  output << " Part IX \n";
212  output << "Save/restore of engines to vectors\n";
213  output << "=================================\n\n";
214 
215  stat |= vectorRestore<DualRand>(113);
216  stat |= vectorRestore<DRand48Engine>(114);
217  stat |= vectorRestore<Hurd160Engine>(115);
218  stat |= vectorRestore<Hurd288Engine>(116);
219  stat |= vectorRestore<HepJamesRandom>(117);
220  stat |= vectorRestore<MTwistEngine>(118);
221  stat |= vectorRestore<RanecuEngine>(139);
222  stat |= vectorRestore<Ranlux64Engine>(119);
223  stat |= vectorRestore<RanluxEngine>(120);
224  stat |= vectorRestore<RanshiEngine>(121);
225  stat |= vectorRestore<TripleRand>(122);
226  stat |= vectorRestore<NonRandomEngine>(123);
227  stat |= vectorRestore<RandEngine>(129);
228 #endif
229 
230  output << "\n=============================================\n\n";
231 
232  if (stat != 0) {
233  std::cout << "One or more problems detected: stat = " << stat << "\n";
234  output << "One or more problems detected: stat = " << stat << "\n";
235  } else {
236  output << "ranRestoreTest passed with no problems detected.\n";
237  }
238 
239  if (stat == 0) return 0;
240  if (stat > 0) return -(stat|1);
241  return stat|1;
242 }
243 
CLHEP::NonRandomEngine
Definition: Matrix/CLHEP/Random/NonRandomEngine.h:40
a
@ a
Definition: testCategories.cc:125
CLHEP::HepRandomEngine
Definition: Matrix/CLHEP/Random/RandomEngine.h:55
main
int main()
Definition: testVectorSave.cc:206
aSequence
std::vector< double > aSequence(int n)
Definition: testVectorSave.cc:40
b
@ b
Definition: testCategories.cc:125
CLHEP::HepRandomEngine::newEngine
static HepRandomEngine * newEngine(std::istream &is)
Definition: RandomEngine.cc:90
CLHEP::NonRandomEngine::put
virtual std::ostream & put(std::ostream &os) const
Definition: NonRandomEngine.cc:101
equals
bool equals(double a, double b)
Definition: testVectorSave.cc:34
state
ought always be logged and essentially never be ignored ZMexPROBLEM The software has reached a logically impossible internal state
Definition: mechanics_ZMx.txt:159
vectorRestore
int vectorRestore(int n)
Definition: testVectorSave.cc:191
CLHEP::detail::n
n
Definition: Ranlux64Engine.cc:85
output
std::ofstream output("testVectorSave.cout")
vectorRestore1< NonRandomEngine >
std::vector< unsigned long > vectorRestore1< NonRandomEngine >(int n, std::vector< double > &v)
Definition: testVectorSave.cc:119
equals01
bool equals01(const std::vector< double > &ab)
Definition: testVectorSave.cc:31
f
void f(void g())
Definition: excDblThrow.cc:38
CLHEP
Definition: ClhepVersion.h:13
v
they are gone ZOOM Features Discontinued The following features of the ZOOM package were felt to be extreme overkill These have been after checking that no existing user code was utilizing as in SpaceVector v
Definition: keyMergeIssues.doc:324
CLHEP::NonRandomEngine::engineName
static std::string engineName()
Definition: Matrix/CLHEP/Random/NonRandomEngine.h:72
vectorRestore1
std::vector< unsigned long > vectorRestore1(int n, std::vector< double > &v)
Definition: testVectorSave.cc:103
CLHEP::NonRandomEngine::flat
double flat()
Definition: NonRandomEngine.cc:68
j
long j
Definition: JamesRandomSeeding.txt:28
CLHEP::DualRand
Definition: Matrix/CLHEP/Random/DualRand.h:51
i
long i
Definition: JamesRandomSeeding.txt:27
vectorTest64
int vectorTest64(int n)
Definition: testVectorSave.cc:56
CLHEP::RandFlat
Definition: Matrix/CLHEP/Random/RandFlat.h:42
x
any side effects of that construction would occur twice The semantics of throw x
Definition: whyZMthrowRethrows.txt:37
k
long k
Definition: JamesRandomSeeding.txt:29
CLHEP::NonRandomEngine::setRandomSequence
void setRandomSequence(double *s, int n)
Definition: NonRandomEngine.cc:52
vectorRestore2
int vectorRestore2(const std::vector< unsigned long > state, const std::vector< double > &v)
Definition: testVectorSave.cc:138
vectorTest64< NonRandomEngine >
int vectorTest64< NonRandomEngine >(int n)
Definition: testVectorSave.cc:77