BrookShakeAlgorithm.cpp 25.4 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#include <sstream>
#include "BrookShakeAlgorithm.h"
#include "BrookPlatform.h"
#include "OpenMMException.h"
#include "BrookStreamImpl.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
struct ShakeCluster {

   int _centralID;
   int _peripheralID[3];
   int _size;
   float _distance;
   float _centralInvMass, _peripheralInvMass;

   ShakeCluster(){};
   ShakeCluster( int centralID, float invMass) : _centralID(centralID), _centralInvMass(invMass), _size(0) {};

   void addAtom( int id, float dist, float invMass){
      if( _size == 3 ){
          std::stringstream message;
          message << "ShakeCluster::addAtom: " << "atom " << id << " has more than 3 constraints!." << std::endl; 
          throw OpenMMException( message.str() );
      }
      if( _size > 0 && dist != _distance ){
          std::stringstream message;
          message << "ShakeCluster::addAtom: " << "atom " << id << " has different constraint distances: " <<  dist << " and " << _distance << std::endl; 
          throw OpenMMException( message.str() );
      }
      if( _size > 0 && invMass != _peripheralInvMass ){
          std::stringstream message;
          message << "ShakeCluster::addAtom: " << " constrainted atoms associated w/ atom " << id << " have different masses: " <<  invMass << " and " << _peripheralInvMass << std::endl; 
          throw OpenMMException( message.str() );
      }
      _peripheralID[_size++]  = id;
      _distance              = dist;
      _peripheralInvMass     = invMass;
   }
};

Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** 
 *
 * Constructor
 * 
 */

BrookShakeAlgorithm::BrookShakeAlgorithm( ){

// ---------------------------------------------------------------------------------------

   //static const std::string methodName      = "BrookShakeAlgorithm::BrookShakeAlgorithm";

   BrookOpenMMFloat zero                    = (BrookOpenMMFloat)  0.0;
   BrookOpenMMFloat one                     = (BrookOpenMMFloat)  1.0;

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
91
   _numberOfParticles            = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
92
   _numberOfConstraints          = -1;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
93
94
   _maxIterations                = 25;
   _shakeTolerance               = static_cast<BrookOpenMMFloat>(1.0e-04);
Mark Friedrichs's avatar
Mark Friedrichs committed
95
96
97

   // mark stream dimension variables as unset

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
98
99
100
   _shakeParticleStreamWidth     = -1;
   _shakeParticleStreamHeight    = -1;
   _shakeParticleStreamSize      = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

   _shakeConstraintStreamSize    = -1;
   _shakeConstraintStreamWidth   = -1;
   _shakeConstraintStreamHeight  = -1;

   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      _shakeStreams[ii]   = NULL;
   }

   // setup inverse sqrt masses

   _inverseSqrtMasses = NULL;

}   
 
/** 
 * Destructor
 * 
 */

BrookShakeAlgorithm::~BrookShakeAlgorithm( ){

// ---------------------------------------------------------------------------------------

   //static const std::string methodName      = "BrookShakeAlgorithm::~BrookShakeAlgorithm";

// ---------------------------------------------------------------------------------------

   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      delete _shakeStreams[ii];
   }

   delete[] _inverseSqrtMasses;

}

/** 
 * Get number of constraints
 * 
 * @return   number of constraints
 *
 */

int BrookShakeAlgorithm::getNumberOfConstraints( void ) const {
   return _numberOfConstraints;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/** 
 * Get max iterations
 * 
 * @return  max iterations
 *
 */
         
int BrookShakeAlgorithm::getMaxIterations( void ) const {
   return _maxIterations;
}
         
/** 
 * Set max iterations
 * 
 * @param  max iterations
 *
 * @return DefaultReturnValue
 *
 */
         
int BrookShakeAlgorithm::setMaxIterations( int maxIterations ){
   _maxIterations = maxIterations;
   return DefaultReturnValue;
}
    
/** 
 * Get SHAKE tolerance
 * 
 * @return  SHAKE tolerance  
 *
 */
         
BrookOpenMMFloat BrookShakeAlgorithm::getShakeTolerance( void ) const {
   return _shakeTolerance;
}
         
/** 
 * Set SHAKE tolerance
 * 
 * @param  SHAKE tolerance  
 *
 * @return DefaultReturnValue
 *
 */
         
int BrookShakeAlgorithm::setShakeTolerance( BrookOpenMMFloat tolerance ){
   _shakeTolerance = tolerance;
   return DefaultReturnValue;
}
    
Mark Friedrichs's avatar
Mark Friedrichs committed
198
/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
199
 * Get Particle stream size
Mark Friedrichs's avatar
Mark Friedrichs committed
200
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
201
 * @return  Particle stream size
Mark Friedrichs's avatar
Mark Friedrichs committed
202
203
204
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
205
206
int BrookShakeAlgorithm::getShakeParticleStreamSize( void ) const {
   return _shakeParticleStreamSize;
Mark Friedrichs's avatar
Mark Friedrichs committed
207
208
209
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
210
 * Get particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
211
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
212
 * @return  particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
213
214
215
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
216
217
int BrookShakeAlgorithm::getShakeParticleStreamWidth( void ) const {
   return _shakeParticleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
218
219
220
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
221
 * Get particle stream height
Mark Friedrichs's avatar
Mark Friedrichs committed
222
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
223
 * @return particle stream height
Mark Friedrichs's avatar
Mark Friedrichs committed
224
225
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
226
227
int BrookShakeAlgorithm::getShakeParticleStreamHeight( void ) const {
   return _shakeParticleStreamHeight;
Mark Friedrichs's avatar
Mark Friedrichs committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
}

/** 
 * Get Constraint stream size
 *
 * @return  Constraint stream size
 *
 */

int BrookShakeAlgorithm::getShakeConstraintStreamSize( void ) const {
   return _shakeConstraintStreamSize;
}

/** 
 * Get constraint stream width
 *
 * @return  constraint stream width
 *
 */

int BrookShakeAlgorithm::getShakeConstraintStreamWidth( void ) const {
   return _shakeConstraintStreamWidth;
}

/** 
 * Get constraint stream height
 *
 * @return constraint stream height
 */

int BrookShakeAlgorithm::getShakeConstraintStreamHeight( void ) const {
   return _shakeConstraintStreamHeight;
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
263
 * Get Shake particle indices stream 
Mark Friedrichs's avatar
Mark Friedrichs committed
264
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
265
 * @return  Shake particle indices stream
Mark Friedrichs's avatar
Mark Friedrichs committed
266
267
268
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
269
270
BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeParticleIndicesStream( void ) const {
   return _shakeStreams[ShakeParticleIndicesStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
271
272
273
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
274
 * Get Shake particle parameter stream
Mark Friedrichs's avatar
Mark Friedrichs committed
275
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
276
 * @return  Shake particle parameter sStream
Mark Friedrichs's avatar
Mark Friedrichs committed
277
278
279
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
280
281
BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeParticleParameterStream( void ) const {
   return _shakeStreams[ShakeParticleParameterStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
}

/** 
 * Get Shake XCons0 stream
 *
 * @return  XCons0 stream
 *
 */

BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeXCons0Stream( void ) const {
   return _shakeStreams[ShakeXCons0Stream];
}

/** 
 * Get Shake XCons1 stream
 *
 * @return  XCons1 stream
 *
 */

BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeXCons1Stream( void ) const {
   return _shakeStreams[ShakeXCons1Stream];
}

/** 
 * Get Shake XCons2 stream
 *
 * @return  XCons2 stream
 *
 */

BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeXCons2Stream( void ) const {
   return _shakeStreams[ShakeXCons2Stream];
}

/** 
 * Get Shake XCons3 stream
 *
 * @return  XCons3 stream
 *
 */

BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeXCons3Stream( void ) const {
   return _shakeStreams[ShakeXCons3Stream];
}

/** 
 * Get Shake inverse map stream
 *
 * @return  Shake inverse map stream
 *
 */

BrookFloatStreamInternal* BrookShakeAlgorithm::getShakeInverseMapStream( void ) const {
   return _shakeStreams[ShakeInverseMapStream];
}

/** 
 * Initialize stream dimensions
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
342
 * @param numberOfParticles         number of particles
Mark Friedrichs's avatar
Mark Friedrichs committed
343
344
345
346
347
348
349
 * @param numberOfConstraints       number of constraints
 * @param platform                  platform
 *      
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
350
int BrookShakeAlgorithm::_initializeStreamSizes( int numberOfParticles, int numberOfConstraints,
Mark Friedrichs's avatar
Mark Friedrichs committed
351
352
353
354
355
356
357
358
                                                 const Platform& platform ){

// ---------------------------------------------------------------------------------------

   //static const std::string methodName      = "BrookShakeAlgorithm::_initializeStreamSizes";

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
359
360
361
   _shakeParticleStreamSize           = getParticleStreamSize( platform );
   _shakeParticleStreamWidth          = getParticleStreamWidth( platform );
   _shakeParticleStreamHeight         = getParticleStreamHeight( platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

   // get constraint stream width & height, and then set stream size to the product

   BrookCommon::getStreamDimensions( numberOfConstraints, &_shakeConstraintStreamWidth, &_shakeConstraintStreamHeight );
   _shakeConstraintStreamSize     = _shakeConstraintStreamWidth*_shakeConstraintStreamHeight;

   return DefaultReturnValue;
}

/** 
 * Initialize streams
 * 
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

int BrookShakeAlgorithm::_initializeStreams( const Platform& platform ){

// ---------------------------------------------------------------------------------------

   //static const std::string methodName      = "BrookShakeAlgorithm::_initializeStreams";

   BrookOpenMMFloat dangleValue              = (BrookOpenMMFloat) 0.0;

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
390
391
   int shakeParticleStreamSize               = getShakeParticleStreamSize();
   int shakeParticleStreamWidth              = getShakeParticleStreamWidth();
Mark Friedrichs's avatar
Mark Friedrichs committed
392
393
394
395

   int shakeConstraintStreamSize             = getShakeConstraintStreamSize();
   int shakeConstraintStreamWidth            = getShakeConstraintStreamWidth();

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
396
397
    _shakeStreams[ShakeParticleIndicesStream]
                                             = new BrookFloatStreamInternal( BrookCommon::ShakeParticleIndicesStream,
Mark Friedrichs's avatar
Mark Friedrichs committed
398
399
400
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
                                                                             BrookStreamInternal::Float4, dangleValue );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
401
402
    _shakeStreams[ShakeParticleParameterStream]  
                                             = new BrookFloatStreamInternal( BrookCommon::ShakeParticleParameterStream,
Mark Friedrichs's avatar
Mark Friedrichs committed
403
404
405
406
407
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
                                                                             BrookStreamInternal::Float4, dangleValue );

    _shakeStreams[ShakeXCons0Stream]         = new BrookFloatStreamInternal( BrookCommon::ShakeXCons0Stream,
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
408
                                                                             BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
409
410
411

    _shakeStreams[ShakeXCons1Stream]         = new BrookFloatStreamInternal( BrookCommon::ShakeXCons1Stream,
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
412
                                                                             BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
413
414
415

    _shakeStreams[ShakeXCons2Stream]         = new BrookFloatStreamInternal( BrookCommon::ShakeXCons2Stream,
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
416
                                                                             BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
417
418
419

    _shakeStreams[ShakeXCons3Stream]         = new BrookFloatStreamInternal( BrookCommon::ShakeXCons3Stream,
                                                                             shakeConstraintStreamSize, shakeConstraintStreamWidth,
420
                                                                             BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
421
422

    _shakeStreams[ShakeInverseMapStream]     = new BrookFloatStreamInternal( BrookCommon::ShakeInverseMapStream,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
423
                                                                             shakeParticleStreamSize, shakeParticleStreamWidth,
Mark Friedrichs's avatar
Mark Friedrichs committed
424
425
426
427
428
429
430
431
432
                                                                             BrookStreamInternal::Float2, dangleValue );

   return DefaultReturnValue;
}
 
/*  
 * Set Shake streams
 *
 * @param masses                masses
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
433
 * @param constraintIndices     constraint particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
434
 * @param constraintLengths     constraint lengths
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
435
 * @param platform              platform reference
Mark Friedrichs's avatar
Mark Friedrichs committed
436
437
438
439
440
441
442
443
 *
 * @return ErrorReturnValue if error
 *
 * @throw OpenMMException if constraintIndices.size() != constraintLengths.size()
 *
 */
    
int BrookShakeAlgorithm::_setShakeStreams( const std::vector<double>& masses, const std::vector< std::vector<int> >& constraintIndices,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
444
                                           const std::vector<double>& constraintLengths, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
445
446
447
448
449
450
451
452
453
454
    
// ---------------------------------------------------------------------------------------

   BrookOpenMMFloat one                      = (BrookOpenMMFloat)  1.0;
   BrookOpenMMFloat half                     = (BrookOpenMMFloat)  0.5;

   static const std::string methodName       = "BrookShakeAlgorithm::_updateSdStreams";

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
455
   FILE* log = getLog();
Mark Friedrichs's avatar
Mark Friedrichs committed
456
457
458
459
460
461
462
463
464
   // check that number of constraints for two input vectors is consistent

   if( constraintIndices.size() != constraintLengths.size() ){
      std::stringstream message;
      message << methodName << " constraintIndices size=" << constraintIndices.size() << " does not equal constraintLengths size=" << constraintLengths.size();
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }   

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
465
   // get constraint count for each atom
Mark Friedrichs's avatar
Mark Friedrichs committed
466

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
467
   vector<int> constraintCount( masses.size(), 0);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
468
469
   std::vector< std::vector<int> >::const_iterator particleIterator = constraintIndices.begin();
   while( particleIterator != constraintIndices.end() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
470

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
471
      std::vector<int> particleVector = *particleIterator;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
472
473
      int atomI                       = particleVector[0]; 
      int atomJ                       = particleVector[1]; 
Mark Friedrichs's avatar
Mark Friedrichs committed
474

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
475
      // check that atom indices are not out of range
Mark Friedrichs's avatar
Mark Friedrichs committed
476

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
477
      if( atomI < 0 || atomI > (int) masses.size() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
478
         std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
479
         message << methodName << " constraint I-index=" << atomI << " is  out of range [0, " << masses.size() << "]" << std::endl;
Mark Friedrichs's avatar
Mark Friedrichs committed
480
481
         throw OpenMMException( message.str() );
         return ErrorReturnValue;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
482
483
484
      }

      if( atomJ < 0 || atomJ > (int) masses.size() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
485
         std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
486
         message << methodName << " constraint J-index=" << atomJ << " is out of range [0, " << masses.size() << "]" << std::endl;
Mark Friedrichs's avatar
Mark Friedrichs committed
487
488
489
490
         throw OpenMMException( message.str() );
         return ErrorReturnValue;
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
491
492
493
494
495
496
497
498
499
500
501
502
      constraintCount[atomI]++;
      constraintCount[atomJ]++;

      particleIterator++;
   }
   
   // Find clusters consisting of a central atom with up to three peripheral atoms.
   
   map<int, ShakeCluster> clusters;
   particleIterator                                     = constraintIndices.begin();
   std::vector<double>::const_iterator distanceIterator = constraintLengths.begin();
   while( particleIterator != constraintIndices.end() ){
503

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
      float distance                  = static_cast<float>( *distanceIterator );
      std::vector<int> particleVector = *particleIterator;
      int atomI                       = particleVector[0]; 
      int atomJ                       = particleVector[1]; 

      // Determine the central atom.
       
      bool firstIsCentral;
      if( constraintCount[atomI] > 1 ){
         firstIsCentral = true;
      } else if (constraintCount[atomJ] > 1 ){
         firstIsCentral = false;
      } else if( atomI < atomJ ){
         firstIsCentral = true;
      } else {
         firstIsCentral = false;
520
      }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
      int centralID, peripheralID;
      float centralInvMass, peripheralInvMass;
      if( firstIsCentral ){
         centralID         = atomI;
         peripheralID      = atomJ;
         centralInvMass    = static_cast<float>( masses[atomI] );
         peripheralInvMass = static_cast<float>( masses[atomJ] );
      } else {
         centralID         = atomJ;
         peripheralID      = atomI;
         centralInvMass    = static_cast<float>( masses[atomJ] );
         peripheralInvMass = static_cast<float>( masses[atomI] );
      }
      if( constraintCount[peripheralID] != 1 ){
          throw OpenMMException("Only bonds to hydrogens may be constrained");
      }
       
      // Add it to the cluster
       
      if( clusters.find(centralID) == clusters.end() ){
         clusters[centralID] = ShakeCluster( centralID, centralInvMass );
      }
      clusters[centralID].addAtom( peripheralID, distance, peripheralInvMass );
 
      particleIterator++;
      distanceIterator++;
   }
    
   // allocate space for streams
550

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
551
552
553
554
555
   _initializeStreamSizes( (int) masses.size(), (int) clusters.size(), platform );
   _initializeStreams( platform );

   int shakeParticleStreamSize               = getShakeParticleStreamSize();
   int shakeConstraintStreamSize             = getShakeConstraintStreamSize();
556

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
557
   // allocate arrays to be read down to board and initialize
Mark Friedrichs's avatar
Mark Friedrichs committed
558

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
559
560
561
562
563
564
565
   BrookOpenMMFloat* particleIndices  = new BrookOpenMMFloat[4*shakeConstraintStreamSize];
   BrookOpenMMFloat* shakeParameters  = new BrookOpenMMFloat[4*shakeConstraintStreamSize];
   BrookOpenMMFloat minusOne          = static_cast<BrookOpenMMFloat>(-1.0);
   for( int ii = 0; ii < 4*shakeConstraintStreamSize; ii++ ){
      particleIndices[ii] = minusOne;
   }
   memset( shakeParameters, 0, 4*shakeConstraintStreamSize*sizeof( BrookOpenMMFloat ) ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
566

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
567
   // load indices & parameters
Mark Friedrichs's avatar
Mark Friedrichs committed
568

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
569
570
571
   int constraintIndex                   = 0;
   _numberOfConstraints                  = static_cast<int>(clusters.size());
   for( map<int, ShakeCluster>::const_iterator iter = clusters.begin(); iter != clusters.end(); ++iter ){
Mark Friedrichs's avatar
Mark Friedrichs committed
572

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
573
      const ShakeCluster& cluster        = iter->second;
Mark Friedrichs's avatar
Mark Friedrichs committed
574

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
575
576
577
578
      particleIndices[constraintIndex]   = static_cast<BrookOpenMMFloat>( cluster._centralID );
      for( int ii = 0; ii < cluster._size; ii++ ){
         particleIndices[constraintIndex+ii+1] = static_cast<BrookOpenMMFloat>( cluster._peripheralID[ii] );
      }
Mark Friedrichs's avatar
Mark Friedrichs committed
579

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
580
581
582
583
      shakeParameters[constraintIndex]    = one/( static_cast<BrookOpenMMFloat>( cluster._centralInvMass ) );
      shakeParameters[constraintIndex+1]  = half/( static_cast<BrookOpenMMFloat>(cluster._centralInvMass + cluster._peripheralInvMass) );
      shakeParameters[constraintIndex+2]  = static_cast<BrookOpenMMFloat>( cluster._distance*cluster._distance );
      shakeParameters[constraintIndex+3]  = one/( static_cast<BrookOpenMMFloat>( cluster._peripheralInvMass ) );
Mark Friedrichs's avatar
Mark Friedrichs committed
584
585
586
587
588
589

      constraintIndex += 4;
   }

   // write entries to board

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
590
591
   _shakeStreams[ShakeParticleIndicesStream]->loadFromArray( particleIndices );
   _shakeStreams[ShakeParticleParameterStream]->loadFromArray( shakeParameters );
Mark Friedrichs's avatar
Mark Friedrichs committed
592
593
594
595
596

   delete[] shakeParameters;

   // initialize inverse map

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
597
598
   BrookOpenMMFloat* inverseMap = new BrookOpenMMFloat[2*shakeParticleStreamSize];
   for( int ii = 0; ii < shakeParticleStreamSize*2; ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
599
600
601
602
603
604
605
606
      inverseMap[ii] = -1;
   }
   
   // build inverse map
 
   for( int ii = 0; ii < shakeConstraintStreamSize; ii++ ){
      int ii4 = ii << 2;
      for( int jj = 0; jj < 4; jj++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
607
608
609
610
         if( particleIndices[ii4+jj] != -1 ){
            int particleIndex             = (int) (particleIndices[ii4+jj] + 0.001);
            inverseMap[particleIndex*2]   = (float) ii;
            inverseMap[particleIndex*2+1] = (float) jj;
Mark Friedrichs's avatar
Mark Friedrichs committed
611
612
613
614
615
616
         }
      }
   }
   
   _shakeStreams[ShakeInverseMapStream]->loadFromArray( inverseMap );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
617
   delete[] particleIndices;
Mark Friedrichs's avatar
Mark Friedrichs committed
618
619
620
621
622
623
624
625
626
627
   delete[] inverseMap;

   return DefaultReturnValue;

}
 
/*  
 * Setup of Shake parameters
 *
 * @param masses                masses
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
628
 * @param constraintIndices     constraint particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
 * @param constraintLengths     constraint lengths
 * @param platform              Brook platform
 *
 * @return ErrorReturnValue if error
 *
 */
    
int BrookShakeAlgorithm::setup( const std::vector<double>& masses, const std::vector<std::vector<int> >& constraintIndices,
                                const std::vector<double>& constraintLengths, const Platform& platform ){
    
// ---------------------------------------------------------------------------------------

   //static const std::string methodName      = "BrookShakeAlgorithm::setup";

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
645
646
   int numberOfParticles  = (int) masses.size();
   setNumberOfParticles( numberOfParticles );
Mark Friedrichs's avatar
Mark Friedrichs committed
647

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
648
   _setShakeStreams( masses, constraintIndices, constraintLengths, platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683

   return DefaultReturnValue;
}

/* 
 * Get contents of object
 *
 * @param level   level of dump
 *
 * @return string containing contents
 *
 * */

std::string BrookShakeAlgorithm::getContentsString( int level ) const {

// ---------------------------------------------------------------------------------------

   static const std::string methodName      = "BrookShakeAlgorithm::getContentsString";

   static const unsigned int MAX_LINE_CHARS = 256;
   char value[MAX_LINE_CHARS];
   static const char* Set                   = "Set";
   static const char* NotSet                = "Not set";

// ---------------------------------------------------------------------------------------

   std::stringstream message;
   std::string tab   = "   ";

#ifdef WIN32
#define LOCAL_SPRINTF(a,b,c) sprintf_s( (a), MAX_LINE_CHARS, (b), (c) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#endif

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
684
685
686
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfConstraints() );
   message << _getLine( tab, "Number of constraints:", value ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
687
688
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParticles() );
   message << _getLine( tab, "Number of particles:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
689

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
690
691
692
693
694
695
   (void) LOCAL_SPRINTF( value, "%d", getMaxIterations() );
   message << _getLine( tab, "Max iterations:", value ); 

   (void) LOCAL_SPRINTF( value, "%2e", getShakeTolerance() );
   message << _getLine( tab, "Tolerance:", value ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
696
697
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamWidth() );
   message << _getLine( tab, "Particle stream width:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
698

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
699
700
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamHeight() );
   message << _getLine( tab, "Particle stream height:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
701

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
702
703
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamSize() );
   message << _getLine( tab, "Particle stream size:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
704
705
706
707
708
709
710
711
712
713
714
715

   (void) LOCAL_SPRINTF( value, "%d", getShakeConstraintStreamWidth() );
   message << _getLine( tab, "Constraint stream width:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getShakeConstraintStreamHeight() );
   message << _getLine( tab, "Constraint stream height:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getShakeConstraintStreamSize() );
   message << _getLine( tab, "Constraint stream size:", value ); 

   message << _getLine( tab, "Log:",                  (getLog()                          ? Set : NotSet) ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
716
717
   message << _getLine( tab, "ParticleIndices:",      (getShakeParticleIndicesStream()   ? Set : NotSet) ); 
   message << _getLine( tab, "ParticleParameters:",   (getShakeParticleParameterStream() ? Set : NotSet) ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
   message << _getLine( tab, "XCons0:",               (getShakeXCons0Stream()            ? Set : NotSet) ); 
   message << _getLine( tab, "XCons1:",               (getShakeXCons1Stream()            ? Set : NotSet) ); 
   message << _getLine( tab, "XCons2:",               (getShakeXCons2Stream()            ? Set : NotSet) ); 
   message << _getLine( tab, "XCons3:",               (getShakeXCons3Stream()            ? Set : NotSet) ); 
   message << _getLine( tab, "InverseMap:",           (getShakeInverseMapStream()        ? Set : NotSet) ); 
 
   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      message << std::endl;
      if( _shakeStreams[ii] ){
         message << _shakeStreams[ii]->getContentsString( );
      }
   }

#undef LOCAL_SPRINTF

   return message.str();
}