Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
randomNumbers_Mseries.cpp
Go to the documentation of this file.
1 
14 #include "randomNumbers_Mseries.h"
15 
16 const std::string RandomNumbers_Mseries::class_name = "RandomNumbers_Mseries";
17 const double RandomNumbers_Mseries::Fnorm = 4.656612870908988e-10;
18 
19 //====================================================================
20 void RandomNumbers_Mseries::reset(unsigned long seed)
21 {
22  initset((int)seed);
23 }
24 
25 
26 //====================================================================
27 void RandomNumbers_Mseries::initset(const int ndelay)
28 {
29  vout.general(m_vl, " Random number init: ndelay =%10d\n", ndelay);
30 
31  int IP = Np;
32  int IQ = Nq;
33 
34  int MACRM = 40;
35  int MACRI = 1;
36  unsigned IB[IP];
37 
38  unsigned IX = MACRI;
39  for (int i = 0; i < IP; ++i) {
40  IX = IX * 69069;
41  IB[i] = IX >> 31;
42  }
43 
44  int JR = 0;
45  int KR = IP - IQ;
46  for (int j = 0; j < IP; ++j) {
47  unsigned iwork = 0;
48 
49  for (int i = 0; i < 32; ++i) {
50  iwork = iwork * 2 + IB[JR];
51  IB[JR] = IB[JR] ^ IB[KR];
52  ++JR;
53  if (JR == IP) JR = 0;
54  ++KR;
55  if (KR == IP) KR = 0;
56  }
57 
58  w[j] = iwork >> 1;
59  }
60 
61  jr = JR;
62  kr = KR;
63 
64  delay3(ndelay);
65 }
66 
67 
68 //====================================================================
69 void RandomNumbers_Mseries::delay3(const int ndelay)
70 {
71  int IP = Np;
72  int IQ = Nq;
73 
74  int LAMBDA = ndelay;
75 
76  int MACRM = 40;
77  int MACRI = 1;
78 
79  unsigned IWK[2 * IP - 1];
80  unsigned C[2 * IP];
81  unsigned IB[IP + 32];
82 
83  int MU = MACRM;
84 
85  for (int i = 0; i < IP; ++i) {
86  IWK[i] = w[i];
87  }
88 
89  for (int i = IP; i < 2 * IP - 1; ++i) {
90  IWK[i] = IWK[i - IP] ^ IWK[i - IQ];
91  }
92 
93  for (int i = 0; i < MU; ++i) {
94  IB[i] = 0;
95  }
96  int M = LAMBDA;
97  int NB = MU - 1;
98 
99 continued220:
100  if (M <= IP - 1) goto continued300;
101  ++NB;
102  IB[NB] = M % 2;
103  M = M / 2;
104  goto continued220;
105 
106 continued300:
107  for (int i = 0; i < IP; ++i) {
108  C[i] = 0;
109  }
110 
111  C[M] = 1;
112  for (int j = NB; j >= 0; --j) {
113  for (int i = IP - 1; i >= 0; --i) {
114  C[2 * i + IB[j]] = C[i];
115  C[2 * i + 1 - IB[j]] = 0;
116  }
117  for (int i = 2 * IP - 1; i >= IP; --i) {
118  C[i - IP] = C[i - IP] ^ C[i];
119  C[i - IQ] = C[i - IQ] ^ C[i];
120  }
121  }
122 
123  for (int j = 0; j < IP; ++j) {
124  unsigned iwork = 0;
125  for (int i = 0; i < IP; ++i) {
126  iwork = iwork ^ (C[i] * IWK[j + i]);
127  }
128  w[j] = iwork;
129  }
130 }
131 
132 
133 //====================================================================
134 void RandomNumbers_Mseries::write_file(const std::string& filename)
135 {
136  vout.general(m_vl, "%s: write down to file = %s\n", class_name.c_str(), filename.c_str());
137 
138  if (Communicator::is_primary()) {
139  std::ofstream outfile;
140  outfile.open(filename.c_str());
141 
142  if (!outfile) {
143  vout.crucial(m_vl, "Error at %s: unable to open output file.\n", class_name.c_str());
144  exit(EXIT_FAILURE);
145  }
146 
147  outfile << " " << jr << " " << kr << std::endl;
148 
149  for (int i = 0; i < Np; i++) {
150  outfile << " " << w[i] << std::endl;
151  }
152 
153  outfile.close();
154  }
155 }
156 
157 
158 //====================================================================
159 void RandomNumbers_Mseries::read_file(const std::string& filename)
160 {
161  vout.general(m_vl, "%s: read from file = %s\n", class_name.c_str(), filename.c_str());
162 
163  if (Communicator::is_primary()) {
164  std::ifstream infile;
165  infile.open(filename.c_str());
166 
167  if (!infile) {
168  vout.crucial(m_vl, "Error at %s: unable to open input file.\n", class_name.c_str());
169  exit(EXIT_FAILURE);
170  }
171 
172  infile >> jr >> kr;
173 
174  for (int i = 0; i < Np; i++) {
175  infile >> w[i];
176  }
177 
178  infile.close();
179  }
180 
182  Communicator::broadcast(1, &jr, 0);
183  Communicator::broadcast(1, &kr, 0);
184 }
185 
186 
187 //====================================================================
188 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:495
void read_file(const std::string &)
save and load random number status.
void general(const char *format,...)
Definition: bridgeIO.cpp:195
void delay3(const int ndelay)
Bridge::VerboseLevel m_vl
Definition: randomNumbers.h:42
void initset(const int ndelay)
void reset(unsigned long seed)
reset state with new seed.
void write_file(const std::string &)
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static const double Fnorm
initialized in .cpp file.
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
static bool is_primary()
check if the present node is primary in small communicator.
static const std::string class_name