Bridge++  Ver. 1.2.x
 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 using std::string;
17 
18 const std::string RandomNumbers_Mseries::class_name = "RandomNumbers_Mseries";
19 const double RandomNumbers_Mseries::Fnorm = 4.656612870908988e-10;
20 
21 //====================================================================
22 void RandomNumbers_Mseries::initset(const int ndelay)
23 {
24  vout.general(m_vl, " Random number init: ndelay =%10d\n", ndelay);
25 
26  int IP = Np;
27  int IQ = Nq;
28 
29  int MACRM = 40;
30  int MACRI = 1;
31  unsigned IB[IP];
32 
33  unsigned IX = MACRI;
34  for (int i = 0; i < IP; ++i) {
35  IX = IX * 69069;
36  IB[i] = IX >> 31;
37  }
38 
39  int JR = 0;
40  int KR = IP - IQ;
41  for (int j = 0; j < IP; ++j) {
42  unsigned iwork = 0;
43 
44  for (int i = 0; i < 32; ++i) {
45  iwork = iwork * 2 + IB[JR];
46  IB[JR] = IB[JR] ^ IB[KR];
47  ++JR;
48  if (JR == IP) JR = 0;
49  ++KR;
50  if (KR == IP) KR = 0;
51  }
52 
53  w[j] = iwork >> 1;
54  }
55 
56  jr = JR;
57  kr = KR;
58 
59  delay3(ndelay);
60 }
61 
62 
63 //====================================================================
64 void RandomNumbers_Mseries::delay3(const int ndelay)
65 {
66  int IP = Np;
67  int IQ = Nq;
68 
69  int LAMBDA = ndelay;
70 
71  int MACRM = 40;
72  int MACRI = 1;
73 
74  unsigned IWK[2 * IP - 1];
75  unsigned C[2 * IP];
76  unsigned IB[IP + 32];
77 
78  int MU = MACRM;
79 
80  for (int i = 0; i < IP; ++i) {
81  IWK[i] = w[i];
82  }
83 
84  for (int i = IP; i < 2 * IP - 1; ++i) {
85  IWK[i] = IWK[i - IP] ^ IWK[i - IQ];
86  }
87 
88  for (int i = 0; i < MU; ++i) {
89  IB[i] = 0;
90  }
91  int M = LAMBDA;
92  int NB = MU - 1;
93 
94 continued220:
95  if (M <= IP - 1) goto continued300;
96  ++NB;
97  IB[NB] = M % 2;
98  M = M / 2;
99  goto continued220;
100 
101 continued300:
102  for (int i = 0; i < IP; ++i) {
103  C[i] = 0;
104  }
105 
106  C[M] = 1;
107  for (int j = NB; j >= 0; --j) {
108  for (int i = IP - 1; i >= 0; --i) {
109  C[2 * i + IB[j]] = C[i];
110  C[2 * i + 1 - IB[j]] = 0;
111  }
112  for (int i = 2 * IP - 1; i >= IP; --i) {
113  C[i - IP] = C[i - IP] ^ C[i];
114  C[i - IQ] = C[i - IQ] ^ C[i];
115  }
116  }
117 
118  for (int j = 0; j < IP; ++j) {
119  unsigned iwork = 0;
120  for (int i = 0; i < IP; ++i) {
121  iwork = iwork ^ (C[i] * IWK[j + i]);
122  }
123  w[j] = iwork;
124  }
125 }
126 
127 
128 //====================================================================
129 void RandomNumbers_Mseries::writefile(const string& filename)
130 {
131  vout.general(m_vl, "%s: write down to file = %s\n", class_name.c_str(), filename.c_str());
132 
133  if (Communicator::is_primary()) {
134  std::ofstream outfile;
135  outfile.open(filename.c_str());
136 
137  if (!outfile) {
138  vout.crucial(m_vl, "%s: unable to open output file.\n", class_name.c_str());
139  return;
140  }
141 
142  outfile << " " << jr << " " << kr << std::endl;
143 
144  for (int i = 0; i < Np; i++) {
145  outfile << " " << w[i] << std::endl;
146  }
147 
148  outfile.close();
149  }
150 }
151 
152 
153 //====================================================================
154 void RandomNumbers_Mseries::readfile(const string& filename)
155 {
156  vout.general(m_vl, "%s: read from file = %s\n", class_name.c_str(), filename.c_str());
157 
158  if (Communicator::is_primary()) {
159  std::ifstream infile;
160  infile.open(filename.c_str());
161 
162  if (!infile) {
163  vout.crucial(m_vl, "%s: unable to open input file.\n", class_name.c_str());
164  abort();
165  }
166 
167  infile >> jr >> kr;
168 
169  for (int i = 0; i < Np; i++) {
170  infile >> w[i];
171  }
172 
173  infile.close();
174  }
175 
177  Communicator::broadcast(1, &jr, 0);
178  Communicator::broadcast(1, &kr, 0);
179 }
180 
181 
182 //====================================================================
183 //============================================================END=====
void readfile(const std::string &)
BridgeIO vout
Definition: bridgeIO.cpp:207
void general(const char *format,...)
Definition: bridgeIO.cpp:38
void delay3(const int ndelay)
Bridge::VerboseLevel m_vl
Definition: randomNumbers.h:46
void initset(const int ndelay)
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
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.
void writefile(const std::string &)
static const std::string class_name