| 1 |
/* |
| 2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
* |
| 4 |
* The University of Notre Dame grants you ("Licensee") a |
| 5 |
* non-exclusive, royalty free, license to use, modify and |
| 6 |
* redistribute this software in source and binary code form, provided |
| 7 |
* that the following conditions are met: |
| 8 |
* |
| 9 |
* 1. Acknowledgement of the program authors must be made in any |
| 10 |
* publication of scientific results based in part on use of the |
| 11 |
* program. An acceptable form of acknowledgement is citation of |
| 12 |
* the article in which the program was described (Matthew |
| 13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
| 14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
| 15 |
* Parallel Simulation Engine for Molecular Dynamics," |
| 16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
| 17 |
* |
| 18 |
* 2. Redistributions of source code must retain the above copyright |
| 19 |
* notice, this list of conditions and the following disclaimer. |
| 20 |
* |
| 21 |
* 3. Redistributions in binary form must reproduce the above copyright |
| 22 |
* notice, this list of conditions and the following disclaimer in the |
| 23 |
* documentation and/or other materials provided with the |
| 24 |
* distribution. |
| 25 |
* |
| 26 |
* This software is provided "AS IS," without a warranty of any |
| 27 |
* kind. All express or implied conditions, representations and |
| 28 |
* warranties, including any implied warranty of merchantability, |
| 29 |
* fitness for a particular purpose or non-infringement, are hereby |
| 30 |
* excluded. The University of Notre Dame and its licensors shall not |
| 31 |
* be liable for any damages suffered by licensee as a result of |
| 32 |
* using, modifying or distributing the software or its |
| 33 |
* derivatives. In no event will the University of Notre Dame or its |
| 34 |
* licensors be liable for any lost revenue, profit or data, or for |
| 35 |
* direct, indirect, special, consequential, incidental or punitive |
| 36 |
* damages, however caused and regardless of the theory of liability, |
| 37 |
* arising out of the use of or inability to use software, even if the |
| 38 |
* University of Notre Dame has been advised of the possibility of |
| 39 |
* such damages. |
| 40 |
*/ |
| 41 |
|
| 42 |
#ifndef MATH_OOPSERANDNUMGEN_HPP |
| 43 |
#define MATH_OOPSERANDNUMGEN_HPP |
| 44 |
|
| 45 |
#include <vector> |
| 46 |
#include "MersenneTwister.hpp" |
| 47 |
#include "utils/simError.h" |
| 48 |
|
| 49 |
namespace oopse { |
| 50 |
|
| 51 |
class OOPSERandNumGen{ |
| 52 |
public: |
| 53 |
typedef unsigned long uint32; |
| 54 |
|
| 55 |
OOPSERandNumGen( const uint32& oneSeed); |
| 56 |
OOPSERandNumGen(); |
| 57 |
|
| 58 |
~OOPSERandNumGen() { |
| 59 |
delete mtRand_; |
| 60 |
} |
| 61 |
|
| 62 |
/** Returns a real number in [0,1] */ |
| 63 |
double rand() { |
| 64 |
return mtRand_->rand(); |
| 65 |
} |
| 66 |
|
| 67 |
/** Returns a real number in [0, n] */ |
| 68 |
double rand( const double& n ) { |
| 69 |
return mtRand_->rand(n); |
| 70 |
} |
| 71 |
|
| 72 |
/** Returns a real number in [0, 1) */ |
| 73 |
double randExc() { |
| 74 |
return mtRand_->randExc(); |
| 75 |
} |
| 76 |
|
| 77 |
/** Returns a real number in [0, n) */ |
| 78 |
double randExc( const double& n ) { |
| 79 |
return mtRand_->randExc(n); |
| 80 |
} |
| 81 |
|
| 82 |
/** Returns a real number in (0, 1) */ |
| 83 |
double randDblExc() { |
| 84 |
return mtRand_->randDblExc(); |
| 85 |
} |
| 86 |
|
| 87 |
/** Returns a real number in (0, n) */ |
| 88 |
double randDblExc( const double& n ) { |
| 89 |
return mtRand_->randDblExc(n); |
| 90 |
} |
| 91 |
|
| 92 |
/** Returns aninteger in [0,2^32-1] */ |
| 93 |
uint32 randInt() { |
| 94 |
return mtRand_->randInt(); |
| 95 |
} |
| 96 |
|
| 97 |
/** Returns aninteger in [0, n] for n < 2^32 */ |
| 98 |
uint32 randInt( const uint32& n ) { |
| 99 |
return mtRand_->randInt(n); |
| 100 |
} |
| 101 |
|
| 102 |
/** Returns a 53-bitreal number in [0,1) (capacity of IEEE double precision) */ |
| 103 |
double rand53() { |
| 104 |
return mtRand_->rand53(); |
| 105 |
} |
| 106 |
|
| 107 |
/** Access to nonuniform random number distributions */ |
| 108 |
double randNorm( const double& mean, const double& variance) { |
| 109 |
return mtRand_->randNorm(mean, variance); |
| 110 |
} |
| 111 |
|
| 112 |
// Re-seeding functions with same behavior as initializers |
| 113 |
void seed( const uint32 oneSeed ); |
| 114 |
|
| 115 |
void seed(); |
| 116 |
|
| 117 |
private: |
| 118 |
|
| 119 |
MTRand* mtRand_; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
inline OOPSERandNumGen::OOPSERandNumGen( const uint32& oneSeed) { |
| 124 |
#ifndef IS_MPI |
| 125 |
mtRand_ = new MTRand(oneSeed); |
| 126 |
#else |
| 127 |
int nProcessors; |
| 128 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
| 129 |
mtRand_ = new MTRand(oneSeed, nProcessors, worldRank); |
| 130 |
#endif |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
inline OOPSERandNumGen::OOPSERandNumGen() { |
| 135 |
#ifndef IS_MPI |
| 136 |
mtRand_ = new MTRand(); |
| 137 |
#else |
| 138 |
std::vector<uint32> bigSeed; |
| 139 |
const int masterNode = 0; |
| 140 |
int nProcessors; |
| 141 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
| 142 |
mtRand_ = new MTRand(nProcessors, worldRank); |
| 143 |
|
| 144 |
seed(); |
| 145 |
#endif |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
// Re-seeding functions with same behavior as initializers |
| 150 |
inline void OOPSERandNumGen::seed( const uint32 oneSeed ) { |
| 151 |
#ifndef IS_MPI |
| 152 |
mtRand_->seed(oneSeed); |
| 153 |
#else |
| 154 |
const int masterNode = 0; |
| 155 |
int seed = oneSeed; |
| 156 |
MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
| 157 |
|
| 158 |
if (seed != oneSeed) { |
| 159 |
sprintf(painCave.errMsg, |
| 160 |
"Using different seed to initialize OOPSERandNumGen.\n"); |
| 161 |
painCave.isFatal = 1;; |
| 162 |
simError(); |
| 163 |
} |
| 164 |
|
| 165 |
#endif |
| 166 |
} |
| 167 |
|
| 168 |
inline void OOPSERandNumGen::seed() { |
| 169 |
#ifndef IS_MPI |
| 170 |
mtRand_->seed(); |
| 171 |
#else |
| 172 |
|
| 173 |
std::vector<uint32> bigSeed; |
| 174 |
int size; |
| 175 |
const int masterNode = 0; |
| 176 |
if (worldRank == masterNode) { |
| 177 |
bigSeed = mtRand_->generateSeeds(); |
| 178 |
size = bigSeed.size(); |
| 179 |
MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 180 |
MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
| 181 |
|
| 182 |
}else { |
| 183 |
MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 184 |
bigSeed.resize(size); |
| 185 |
MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
| 186 |
} |
| 187 |
|
| 188 |
if (bigSeed.size() == 1) { |
| 189 |
mtRand_->seed(bigSeed[0]); |
| 190 |
} else { |
| 191 |
mtRand_->seed(&bigSeed[0], bigSeed.size()); |
| 192 |
} |
| 193 |
|
| 194 |
#endif |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
#endif |