SimTKOpenMMUtilities.cpp 10.6 KB
Newer Older
1

2
/* Portions copyright (c) 2006-2009 Stanford University and Simbios.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * Contributors: Pande Group
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

// class of shared, static utility methods

27
#include "openmm/internal/OSRngSeed.h"
28
#include "SimTKOpenMMUtilities.h"
29
#include "sfmt/SFMT.h"
30
31
32

// fabs(), ...

33
34
#include <cmath>
#include <cstdio>
35
#include <string.h>
Peter Eastman's avatar
Peter Eastman committed
36
#include <iostream>
37

38
39
using namespace OpenMM;

40
41
uint32_t SimTKOpenMMUtilities::_randomNumberSeed = 0;
bool SimTKOpenMMUtilities::_randomInitialized = false;
42
bool SimTKOpenMMUtilities::nextGaussianIsValid = false;
peastman's avatar
peastman committed
43
double SimTKOpenMMUtilities::nextGaussian = 0;
44
OpenMM_SFMT::SFMT SimTKOpenMMUtilities::sfmt;
45

46
47
/* ---------------------------------------------------------------------------------------

peastman's avatar
peastman committed
48
   Allocate 1D double array (Simbios)
49
50
51
52
53
54
55
56
57
58
59
60
61

   array[i]

   @param iSize                i-dimension
   @param array1D              array (if null on entry allocated)
   @param initialize           if true, then initialize array
   @param initialValue         intitial value
   @param idString             id string

   @return allocated array

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
62
63
double* SimTKOpenMMUtilities::allocateOneDRealOpenMMArray(int iSize, double* array1D, 
                                                              int initialize, double initialValue,
64
65
                                                              const std::string& idString) {
   if (array1D == NULL) {
66

peastman's avatar
peastman committed
67
      array1D = new double[iSize];
68
69
70

   }

71
   if (initialize) {
peastman's avatar
peastman committed
72
73
      if (initialValue == 0.0) {
         memset(array1D, 0, iSize*sizeof(double));
74
      } else {
75
         for (int ii = 0; ii < iSize; ii++) {
76
77
78
79
80
81
82
83
84
85
            array1D[ii] = initialValue;
         }
      }
   }

   return array1D;
}

/* ---------------------------------------------------------------------------------------

peastman's avatar
peastman committed
86
   Allocate 2D double array (Simbios)
87
88
89
90
91
92
93
94
95
96
97
98
99
100

   array[i][j]

   @param iSize                i-dimension
   @param jSize                j-dimension
   @param array2D              array (if null on entry allocated)
   @param initialize           if true, then initialize array
   @param initialValue         intitial value
   @param idString             id string

   @return allocated array

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
101
102
double** SimTKOpenMMUtilities::allocateTwoDRealOpenMMArray(int iSize, int jSize, double** array2D, 
                                                               int initialize, double initialValue,
103
104
                                                               const std::string& idString) {
   if (array2D == NULL) {
105

peastman's avatar
peastman committed
106
      array2D = new double*[iSize];
107
      std::string blockString = idString;
108
      blockString.append("Block");
109

peastman's avatar
peastman committed
110
      double* block = new double[jSize*iSize];
111

112
      for (int ii = 0; ii < iSize; ii++) {
113
114
115
116
117
         array2D[ii]  = block;
         block       += jSize;
      }    
   }

118
119
   if (initialize) {
      initialize2DRealOpenMMArray(iSize, jSize, array2D, initialValue);
120
121
122
123
124
125
126
   }

   return array2D;
}

/* ---------------------------------------------------------------------------------------

peastman's avatar
peastman committed
127
   Free 2D double array (Simbios)
128
129
130
131
132
133
134
135

   array[i][j]

   @param array2D              array (if null on entry allocated)
   @param idString             id string

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
136
void SimTKOpenMMUtilities::freeTwoDRealOpenMMArray(double** array2D, const std::string& idString) {
137
   if (array2D != NULL) {
138
139

      std::string blockString = idString;
140
      blockString.append("Block");
141

Peter Eastman's avatar
Peter Eastman committed
142
143
      delete[] array2D[0];
      delete[] array2D;
144
145
146
147
148
   }
}

/* ---------------------------------------------------------------------------------------

peastman's avatar
peastman committed
149
   Free 1D double array (Simbios)
150
151
152
153
154
155
156
157

   array[i]

   @param array1D              array (if null on entry allocated)
   @param idString             id string

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
158
void SimTKOpenMMUtilities::freeOneDRealOpenMMArray(double* array1D, const std::string& idString) {
159
   if (array1D != NULL) {
Peter Eastman's avatar
Peter Eastman committed
160
      delete[] array1D;
161
162
163
164
165
   }
}

/* ---------------------------------------------------------------------------------------

peastman's avatar
peastman committed
166
   Initialize 2D double array (Simbios)
167
168
169
170
171
172
173
174
175
176

   array[i][j]

   @param iSize                i-dimension
   @param jSize                j-dimension
   @param array2D              array (if null on entry allocated)
   @param initialValue         intitial value

   --------------------------------------------------------------------------------------- */

177
void SimTKOpenMMUtilities::initialize2DRealOpenMMArray(int iSize, int jSize,
peastman's avatar
peastman committed
178
179
                                                       double** array2D,
                                                       double initialValue) {
180
181
182
   bool useMemset;
   bool useMemsetSingleBlock;

183
   if (initialValue == 0.0f) {
184
      useMemset = true;
185
      if (jSize > 1 && (array2D[0] + jSize) == array2D[1]) {  
186
187
188
189
190
191
192
193
194
         useMemsetSingleBlock = true;
      } else {
         useMemsetSingleBlock = false;
      }

   } else {
      useMemset = false;
   }

195
196
   if (useMemset) {
      if (useMemsetSingleBlock) {
peastman's avatar
peastman committed
197
         memset(array2D[0], 0, iSize*jSize*sizeof(double));
198
      } else {
199
         for (int ii = 0; ii < iSize; ii++) {
peastman's avatar
peastman committed
200
            memset(array2D[ii], 0, jSize*sizeof(double));
201
202
203
         }
      }
   } else {
204
205
      for (int ii = 0; ii < iSize; ii++) {
         for (int jj = 0; jj < jSize; jj++) {
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
            array2D[ii][jj] = initialValue;
         }
      }
   }
}

/* ---------------------------------------------------------------------------------------

   Compute cross product of two 3-vectors and place in 3rd vector  -- helper method

   vectorZ = vectorX x vectorY

   @param vectorX             x-vector
   @param vectorY             y-vector
   @param vectorZ             z-vector

   @return vector is vectorZ

   --------------------------------------------------------------------------------------- */
     
peastman's avatar
peastman committed
226
227
228
void SimTKOpenMMUtilities::crossProductVector3(double* vectorX,
                                               double* vectorY,
                                               double* vectorZ) {
229
230
231
232
233
234
235
   vectorZ[0]  = vectorX[1]*vectorY[2] - vectorX[2]*vectorY[1];
   vectorZ[1]  = vectorX[2]*vectorY[0] - vectorX[0]*vectorY[2];
   vectorZ[2]  = vectorX[0]*vectorY[1] - vectorX[1]*vectorY[0];

   return;
}

236
237
238
239
240
241
242
243
/**---------------------------------------------------------------------------------------

   Get normally distributed random number

   @return random value

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
244
double SimTKOpenMMUtilities::getNormallyDistributedRandomNumber() {
245
246
247
    if (nextGaussianIsValid) {
        nextGaussianIsValid = false;
        return nextGaussian;
248
249
    }
    if (!_randomInitialized) {
250
        init_gen_rand(_randomNumberSeed, sfmt);
251
        _randomInitialized = true;
252
        nextGaussianIsValid = false;
253
254
255
256
    }
    
    // Use the polar form of the Box-Muller transformation to generate two Gaussian random numbers.
    
peastman's avatar
peastman committed
257
    double x, y, r2;
258
    do {
peastman's avatar
peastman committed
259
260
        x = 2.0 * genrand_real2(sfmt) - 1.0;
        y = 2.0 * genrand_real2(sfmt) - 1.0;
261
262
        r2 = x*x + y*y;
    } while (r2 >= 1.0 || r2 == 0.0);
peastman's avatar
peastman committed
263
    double multiplier = sqrt((-2.0*log(r2))/r2);
264
265
    nextGaussian = y*multiplier;
    nextGaussianIsValid = true;
266
267
268
269
270
271
272
273
274
275
276
    return x*multiplier;
}

/**---------------------------------------------------------------------------------------

   Get uniformly distributed random number in the range [0, 1)

   @return random value

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
277
double SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber() {
278
    if (!_randomInitialized) {
279
        init_gen_rand(_randomNumberSeed, sfmt);
280
        _randomInitialized = true;
281
        nextGaussianIsValid = false;
282
    }
peastman's avatar
peastman committed
283
    double value = genrand_real2(sfmt);
284
    return value;
285
286
287
288
289
290
291
292
293
294
}

/**---------------------------------------------------------------------------------------

   Get random number seed

   @return random number seed

   --------------------------------------------------------------------------------------- */

295
uint32_t SimTKOpenMMUtilities::getRandomNumberSeed() {
296
297
298
299
300
301
302
303
304
305
306
   return _randomNumberSeed;
}

/**---------------------------------------------------------------------------------------

   Set random number seed

   @param seed    new seed value

   --------------------------------------------------------------------------------------- */

307
void SimTKOpenMMUtilities::setRandomNumberSeed(uint32_t seed) {
308
    // If the seed is 0, use a unique seed
309
310
311
312
    if (seed == 0)
        _randomNumberSeed = (uint32_t) osrngseed();
    else
        _randomNumberSeed = seed;
313
   _randomInitialized = false;
Peter Eastman's avatar
Peter Eastman committed
314
   nextGaussianIsValid = false;
315
}
Peter Eastman's avatar
Peter Eastman committed
316
317
318
319
320
321

void SimTKOpenMMUtilities::createCheckpoint(std::ostream& stream) {
    stream.write((char*) &_randomNumberSeed, sizeof(uint32_t));
    stream.write((char*) &_randomInitialized, sizeof(bool));
    if (_randomInitialized) {
        stream.write((char*) &nextGaussianIsValid, sizeof(bool));
peastman's avatar
peastman committed
322
        stream.write((char*) &nextGaussian, sizeof(double));
Peter Eastman's avatar
Peter Eastman committed
323
324
325
326
327
328
        sfmt.createCheckpoint(stream);
    }
}

void SimTKOpenMMUtilities::loadCheckpoint(std::istream& stream) {
    stream.read((char*) &_randomNumberSeed, sizeof(uint32_t));
329
    bool prevInitialized = _randomInitialized;
Peter Eastman's avatar
Peter Eastman committed
330
331
    stream.read((char*) &_randomInitialized, sizeof(bool));
    if (_randomInitialized) {
332
333
        if (!prevInitialized)
            init_gen_rand(0, sfmt);
Peter Eastman's avatar
Peter Eastman committed
334
        stream.read((char*) &nextGaussianIsValid, sizeof(bool));
peastman's avatar
peastman committed
335
        stream.read((char*) &nextGaussian, sizeof(double));
Peter Eastman's avatar
Peter Eastman committed
336
337
338
        sfmt.loadCheckpoint(stream);
    }
}