BrookBrownianDynamics.cpp 28.9 KB
Newer Older
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
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
/* -------------------------------------------------------------------------- *
 *                                   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 "BrookBrownianDynamics.h"
#include "BrookPlatform.h"
#include "OpenMMException.h"
#include "BrookStreamImpl.h"
#include "gpu/kshakeh.h"
#include "gpu/kupdatebd.h"
#include "gpu/kcommon.h"

// use random number generator

#include "../../reference/src/SimTKUtilities/SimTKOpenMMUtilities.h"
#include "../../reference/src/SimTKUtilities/SimTKOpenMMRealType.h"

using namespace OpenMM;
using namespace std;

/** 
 *
 * Constructor
 * 
 */

BrookBrownianDynamics::BrookBrownianDynamics( ){

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

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
67
   _numberOfParticles             = -1;
68
69
70

   // mark stream dimension variables as unset

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
71
72
73
   _bdParticleStreamWidth         = -1;
   _bdParticleStreamHeight        = -1;
   _bdParticleStreamSize          = -1;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

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

   _temperature      = oneMinus;
   _stepSize         = oneMinus;
   _tau              = oneMinus;
   _noiseAmplitude   = zero;
   _forceScale       = zero;

   // setup inverse sqrt masses

   _inverseSqrtMasses = NULL;

   // set randomNumber seed 

   _randomNumberSeed  = 1393;

   //_randomNumberSeed = randomNumberSeed ? randomNumberSeed : 1393;
   //SimTKOpenMMUtilities::setRandomNumberSeed( randomNumberSeed );
}   
 
/** 
 * Destructor
 * 
 */

BrookBrownianDynamics::~BrookBrownianDynamics( ){

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

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

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

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

   delete[] _inverseSqrtMasses;

}

/** 
 * Get tau
 * 
 * @return  tau
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getTau( void ) const {
   return _tau;
}

/** 
 * Get friction
 * 
 * @return  friction
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getFriction( void ) const {
   static const BrookOpenMMFloat zero = (BrookOpenMMFloat) 0.0; 
   static const BrookOpenMMFloat one  = (BrookOpenMMFloat) 1.0; 
   return ( (_tau == zero) ? zero : (one/_tau) );
}

/** 
 * Get temperature
 * 
 * @return  temperature
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getTemperature( void ) const {
   return _temperature;
}

/** 
 * Get stepSize
 * 
 * @return  stepSize
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getStepSize( void ) const {
   return _stepSize;
}

/** 
 * Get force scale
 * 
 * @return  force scale
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getForceScale( void ) const {
   return _forceScale;
}

/** 
 * Get noise amplitude
 * 
 * @return  noise amplitude
 *
 */

BrookOpenMMFloat BrookBrownianDynamics::getNoiseAmplitude( void ) const {
   return _noiseAmplitude;
}

/** 
 * Set tau
 * 
 * @param tau   new tau value
 *
 * @return      DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_setTau( BrookOpenMMFloat tau ){
   _tau = tau;
   _setNoiseAmplitude();
   _setForceScale();
   return DefaultReturnValue;
}

/** 
 * Set friction = 1/tau
 * 
 * @param friction   new friction value
 *
 * @return      DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_setFriction( BrookOpenMMFloat friction ){

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

   //static const std::string methodName      = "BrookBrownianDynamics::_setFriction";

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

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

   _tau   = (BrookOpenMMFloat) ( (friction != zero) ? one/friction : zero);

   _setNoiseAmplitude();
   _setForceScale();

   return DefaultReturnValue;
}

/** 
 * Set temperature
 * 
 * @parameter   temperature
 *
 * @return      DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_setTemperature( BrookOpenMMFloat temperature ){

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

   //static const std::string methodName      = "BrookBrownianDynamics::_setTemperature";

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

   _temperature = temperature;
   _setNoiseAmplitude();
   return DefaultReturnValue;
}

/** 
 * Set stepSize
 * 
 * @param   stepSize
 *
 * @return      DefaultReturnValue
 *
 * @throw OpenMMException, if stepSize <= 0
 *
 */

int BrookBrownianDynamics::_setStepSize( BrookOpenMMFloat stepSize ){

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

   static const std::string methodName      = "BrookBrownianDynamics::_setStepSize";

   BrookOpenMMFloat zero                    = (BrookOpenMMFloat)  0.0;
   BrookOpenMMFloat two                     = (BrookOpenMMFloat)  2.0;

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

   if( stepSize <= zero ){
      std::stringstream message;
      message << methodName << " step size=" << stepSize << " is invalid.";
      throw OpenMMException( message.str() );
   }    

   _stepSize = stepSize;
   _setNoiseAmplitude();
   _setForceScale();

   return DefaultReturnValue;
}

/** 
 * Set noise amplitude
 * 
 * @return  DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_setNoiseAmplitude( void ){

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

   //static const std::string methodName      = "BrookBrownianDynamics::_setNoiseAmplitude";

   BrookOpenMMFloat zero                    = (BrookOpenMMFloat)  0.0;
   BrookOpenMMFloat two                     = (BrookOpenMMFloat)  2.0;

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

   _noiseAmplitude =  _temperature*_stepSize*_tau;

   // skip sqrt if value is negative -- presumably not all values updated

   if( _noiseAmplitude > zero ){
      RealOpenMM intermediate = (RealOpenMM) BOLTZ*( (RealOpenMM) (two*_noiseAmplitude) );
      _noiseAmplitude         = (BrookOpenMMFloat) SQRT( intermediate );
   }
   return DefaultReturnValue;
}

/** 
 * Set force scale
 * 
 * @return  DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_setForceScale( void ){

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

   //static const std::string methodName      = "BrookBrownianDynamics::_setForceScale";

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

   _forceScale = _stepSize*_tau;
   return DefaultReturnValue;
}

/** 
 * Update parameters -- only way parameters can be set
 * 
 * @param  temperature     temperature
 * @param  friction        friction
 * @param  step size       step size
 *
 * @return   solute dielectric
 *
 */

int BrookBrownianDynamics::updateParameters( double temperature, double friction, double stepSize ){

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

   static int showUpdate         = 1;
   static int maxShowUpdate      = 3;
   static const char* methodName = "\nBrookBrownianDynamics::updateParameters";

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

   _setStepSize(    (BrookOpenMMFloat)  stepSize );
   _setFriction(    (BrookOpenMMFloat)  friction );
   //_setTau(    (BrookOpenMMFloat)  friction );
   _setTemperature( (BrookOpenMMFloat)  temperature );

   _updateStreams( );

   // show update

   if( showUpdate && getLog() && (showUpdate++ < maxShowUpdate) ){
      std::string contents = getContentsString( );
      (void) fprintf( getLog(), "%s contents\n%s", methodName, contents.c_str() );
      (void) fflush( getLog() );

   }

   return DefaultReturnValue;

};

/** 
 * Update
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
379
380
381
 * @param  positions                   particle positions
 * @param  velocities                  particle velocities
 * @param  forces                      particle forces
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
 * @param  brookShakeAlgorithm         BrookShakeAlgorithm reference
 * @param  brookRandomNumberGenerator  BrookRandomNumberGenerator reference
 *
 * @return  DefaultReturnValue
 *
 */

int BrookBrownianDynamics::update( Stream& positions, Stream& velocities,
                                   const Stream& forces,
                                   BrookShakeAlgorithm& brookShakeAlgorithm,
                                   BrookRandomNumberGenerator& brookRandomNumberGenerator ){

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

   // unused Shake parameter

   float omega                   = 1.0f;
   float one                     = 1.0f;

   static const char* methodName = "\nBrookBrownianDynamics::update";

   static const int PrintOn      = 0;

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

   BrookStreamImpl& positionStream                     = dynamic_cast<BrookStreamImpl&>       (positions.getImpl());
   BrookStreamImpl& velocityStream                     = dynamic_cast<BrookStreamImpl&>       (velocities.getImpl());
   const BrookStreamImpl& forceStreamC                 = dynamic_cast<const BrookStreamImpl&> (forces.getImpl());
   BrookStreamImpl& forceStream                        = const_cast<BrookStreamImpl&> (forceStreamC);

   if( (1 || PrintOn) && getLog() ){

      static int showAux = 1;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
416
      if( showAux ){
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
         (void) fprintf( getLog(), "%s shake=%d\n", methodName, brookShakeAlgorithm.getNumberOfConstraints() );
         (void) fflush( getLog() );
      }

      // show update
   
      if( showAux ){
         showAux = 0;

         std::string contents             = brookRandomNumberGenerator.getContentsString( );
         (void) fprintf( getLog(), "%s RNG contents\n%s", methodName, contents.c_str() );
         // brookRandomNumberGenerator.getRandomNumberStream( brookRandomNumberGenerator.getRvStreamIndex() )->printToFile( getLog() ); 

         contents             = brookShakeAlgorithm.getContentsString( );
         (void) fprintf( getLog(), "%s Shake contents\n%s", methodName, contents.c_str() );

         (void) fflush( getLog() );
      }
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
   // diagnostics

   if( (1 || PrintOn) ){
      (void) fprintf( getLog(), "\nPre kintegrate_bd: %d rngStrW=%3d rngOff=%5d "
                                "ForceScale=%12.5e NoiseAmplitude=%12.5e\n",
                                getBrownianDynamicsParticleStreamWidth(),
                                brookRandomNumberGenerator.getRandomNumberStreamWidth(),
                                brookRandomNumberGenerator.getRvStreamOffset(),
                                getForceScale(),  getNoiseAmplitude() );

      // (void) fprintf( getLog(), "\nInverseMassStream\n" );
      //getInverseMassStream()->printToFile( getLog() );

      //StreamImpl& positionStreamImpl               = positionStream.getImpl();
      //const BrookStreamImpl brookPositions         = dynamic_cast<BrookStreamImpl&> (positionStreamImpl);
/*
      BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
      (void) fprintf( getLog(), "\nPositionStream\n" );
      brookStreamInternalPos->printToFile( getLog() );
*/

      double forceSum[3];
      BrookStreamInternal* brookStreamInternalFF       = forceStream.getBrookStreamImpl();
      BrookFloatStreamInternal* brookStreamInternalF   = dynamic_cast<BrookFloatStreamInternal*> (brookStreamInternalFF);
      brookStreamInternalF->sumByDimension( getNumberOfParticles(), forceSum );
      (void) fprintf( getLog(), "\nForceStream [%18.10e %18.10e %18.10e]\n", forceSum[0], forceSum[1], forceSum[2] );
      brookStreamInternalF->printToFile( getLog() );
/*

      (void) fprintf( getLog(), "\nXPrimeStream\n" );
      getXPrimeStream()->printToFile( getLog() ); 

      (void) fprintf( getLog(), "\nRvStreamIndex=%d\n", brookRandomNumberGenerator.getRvStreamIndex() );
*/
      // brookRandomNumberGenerator.getRandomNumberStream( brookRandomNumberGenerator.getRvStreamIndex() )->printToFile( getLog() ); 

         BrookStreamInternal* brookStreamInternalVel  = velocityStream.getBrookStreamImpl();
         (void) fprintf( getLog(), "\nVelocityStream\n" );
         brookStreamInternalVel->printToFile( getLog() ); 
   }   

478
479
480
   // integration step -- deltas returned in XPrime

   kintegrate_bd(
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
481
         (float) getBrownianDynamicsParticleStreamWidth(),
482
483
484
485
486
487
488
489
490
491
492
493
         (float) brookRandomNumberGenerator.getRandomNumberStreamWidth(),
         (float) brookRandomNumberGenerator.getRvStreamOffset(),
         getForceScale(), getNoiseAmplitude(),
         brookRandomNumberGenerator.getRandomNumberStream( brookRandomNumberGenerator.getRvStreamIndex() )->getBrookStream(),
         positionStream.getBrookStream(), forceStream.getBrookStream(),
         getXPrimeStream()->getBrookStream() );
 
   // diagnostics

   if( PrintOn ){
      (void) fprintf( getLog(), "\nPost kintegrate_bd: %d rngStrW=%3d rngOff=%5d "
                                "ForceScale=%12.5e NoiseAmplitude=%12.5e\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
494
                                getBrownianDynamicsParticleStreamWidth(),
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
                                brookRandomNumberGenerator.getRandomNumberStreamWidth(),
                                brookRandomNumberGenerator.getRvStreamOffset(),
                                getForceScale(),  getNoiseAmplitude() );

      // (void) fprintf( getLog(), "\nInverseMassStream\n" );
      //getInverseMassStream()->printToFile( getLog() );

      //StreamImpl& positionStreamImpl               = positionStream.getImpl();
      //const BrookStreamImpl brookPositions         = dynamic_cast<BrookStreamImpl&> (positionStreamImpl);
      BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
      (void) fprintf( getLog(), "\nPositionStream\n" );
      brookStreamInternalPos->printToFile( getLog() );

      (void) fprintf( getLog(), "\nForceStream\n" );
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
      brookStreamInternalF->printToFile( getLog() );

      (void) fprintf( getLog(), "\nXPrimeStream\n" );
      getXPrimeStream()->printToFile( getLog() ); 

      (void) fprintf( getLog(), "\nRvStreamIndex=%d\n", brookRandomNumberGenerator.getRvStreamIndex() );
      // brookRandomNumberGenerator.getRandomNumberStream( brookRandomNumberGenerator.getRvStreamIndex() )->printToFile( getLog() ); 
   }   

   // advance random number cursor

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
521
   brookRandomNumberGenerator.advanceGVCursor( getNumberOfParticles() );
522
523
524
525

   // Shake

   if( brookShakeAlgorithm.getNumberOfConstraints() > 0 ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
526
/*
527
      kshakeh_fix1( 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
528
                    (float) brookShakeAlgorithm.getMaxIterations(),
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
529
                    (float) getBrownianDynamicsParticleStreamWidth(),
530
                    brookShakeAlgorithm.getInverseHydrogenMass(),
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
531
                    brookShakeAlgorithm.getShakeTolerance(),
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
532
                    brookShakeAlgorithm.getShakeParticleIndicesStream()->getBrookStream(),
533
534
                    positionStream.getBrookStream(),
                    getXPrimeStream()->getBrookStream(),
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
535
                    brookShakeAlgorithm.getShakeParticleParameterStream()->getBrookStream(),
536
537
538
539
540
541
542
543
                    brookShakeAlgorithm.getShakeXCons0Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons1Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons2Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons3Stream()->getBrookStream() );
   
      // second Shake gather
   
      kshakeh_update2_fix1( 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
544
                    (float) getBrownianDynamicsParticleStreamWidth(),
545
546
547
548
549
550
551
552
553
                    brookShakeAlgorithm.getShakeInverseMapStream()->getBrookStream(),
                    positionStream.getBrookStream(),
                    getXPrimeStream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons0Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons1Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons2Stream()->getBrookStream(),
                    brookShakeAlgorithm.getShakeXCons3Stream()->getBrookStream(),
                    positionStream.getBrookStream() );
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
554
555
*/
fprintf( stderr, "\nBrownian shake off!!\n" );
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
      // diagnostics
   
      if( PrintOn ){
   
         (void) fprintf( getLog(), "\nPost kupdate_bd:\n" );
   
         BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
         (void) fprintf( getLog(), "\nPositionStream\n" );
         brookStreamInternalPos->printToFile( getLog() );
   
         (void) fprintf( getLog(), "\nXPrimeStream\n" );
         getXPrimeStream()->printToFile( getLog() ); 
   
         BrookStreamInternal* brookStreamInternalVel  = velocityStream.getBrookStreamImpl();
         (void) fprintf( getLog(), "\nVelocityStream\n" );
         brookStreamInternalVel->printToFile( getLog() ); 
   
      }   
   
   } else {

      // update step
   
      float velocityScale = (float) (one/getStepSize());
      kupdate_bd2( velocityScale, getXPrimeStream()->getBrookStream(),
                   positionStream.getBrookStream(), velocityStream.getBrookStream(), positionStream.getBrookStream() );

      // diagnostics
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
585
      if( (1 || PrintOn) ){
586
587
   
         (void) fprintf( getLog(), "\nPost kupdate_bd2: velocityScale=%12.5e\n", velocityScale );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
588

589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
         (void) fprintf( getLog(), "\nXPrimeStream\n" );
         getXPrimeStream()->printToFile( getLog() ); 

         BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
         (void) fprintf( getLog(), "\nPositionStream\n" );
         brookStreamInternalPos->printToFile( getLog() );
   
         BrookStreamInternal* brookStreamInternalVel  = velocityStream.getBrookStreamImpl();
         (void) fprintf( getLog(), "\nVelocityStream\n" );
         brookStreamInternalVel->printToFile( getLog() ); 
   
      }   
   
   }
 
   return DefaultReturnValue;

};

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
609
 * Get Particle stream size
610
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
611
 * @return  Particle stream size
612
613
614
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
615
616
int BrookBrownianDynamics::getBrownianDynamicsParticleStreamSize( void ) const {
   return _bdParticleStreamSize;
617
618
619
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
620
 * Get particle stream width
621
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
622
 * @return  particle stream width
623
624
625
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
626
627
int BrookBrownianDynamics::getBrownianDynamicsParticleStreamWidth( void ) const {
   return _bdParticleStreamWidth;
628
629
630
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
631
 * Get particle stream height
632
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
633
 * @return particle stream height
634
635
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
636
637
int BrookBrownianDynamics::getBrownianDynamicsParticleStreamHeight( void ) const {
   return _bdParticleStreamHeight;
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
}

/** 
 * Get XPrime stream 
 *
 * @return  Xprime stream
 *
 */

BrookFloatStreamInternal* BrookBrownianDynamics::getXPrimeStream( void ) const {
   return _streams[XPrimeStream];
}

/** 
 * Get InverseMass stream 
 *
 * @return  inverse mass stream
 *
 */

BrookFloatStreamInternal* BrookBrownianDynamics::getInverseMassStream( void ) const {
   return _streams[InverseMassStream];
}

/** 
 * Initialize stream dimensions
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
665
 * @param numberOfParticles             number of particles
666
667
668
669
670
671
 * @param platform                  platform
 *      
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
672
int BrookBrownianDynamics::_initializeStreamSizes( int numberOfParticles, const Platform& platform ){
673
674
675
676
677
678
679

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
680
681
682
   _bdParticleStreamSize     = getParticleStreamSize( platform );
   _bdParticleStreamWidth    = getParticleStreamWidth( platform );
   _bdParticleStreamHeight   = getParticleStreamHeight( platform );
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705

   return DefaultReturnValue;
}

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

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

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

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

   BrookOpenMMFloat dangleValue            = (BrookOpenMMFloat) 0.0;

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
706
707
   int sdParticleStreamSize             = getBrownianDynamicsParticleStreamSize();
   int sdParticleStreamWidth            = getBrownianDynamicsParticleStreamWidth();
708
709

    _streams[VPrimeStream]          = new BrookFloatStreamInternal( BrookCommon::VPrimeStream,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
710
                                                                    sdParticleStreamSize, sdParticleStreamWidth,
711
712
713
                                                                    BrookStreamInternal::Float3, dangleValue );

    _streams[XPrimeStream]          = new BrookFloatStreamInternal( BrookCommon::XPrimeStream,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
714
                                                                    sdParticleStreamSize, sdParticleStreamWidth,
715
716
717
                                                                    BrookStreamInternal::Float3, dangleValue );

    _streams[InverseMassStream]     = new BrookFloatStreamInternal( BrookCommon::InverseMassStream,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
718
                                                                    sdParticleStreamSize, sdParticleStreamWidth,
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
                                                                    BrookStreamInternal::Float, dangleValue );

   return DefaultReturnValue;
}

/** 
 * Update streams -- called after parameters change
 * 
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

int BrookBrownianDynamics::_updateStreams( void ){

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

   //static const std::string methodName       = "BrookBrownianDynamics::_updateStreams";

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
739
   //int particleStreamSize                        = getBrownianDynamicsParticleStreamSize();
740
741
742
743
744
745
746
747

   return DefaultReturnValue;

}

/** 
 * Set masses 
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
748
 * @param masses             particle masses
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
 *
 */

int BrookBrownianDynamics::_setInverseSqrtMasses( const std::vector<double>& masses ){

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

   //static const std::string methodName      = "BrookBrownianDynamics::_setInverseSqrtMasses";

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

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

   // setup inverse sqrt masses

   _inverseSqrtMasses = new BrookOpenMMFloat[masses.size()];
   int index          = 0;
   for( std::vector<double>::const_iterator ii = masses.begin(); ii != masses.end(); ii++, index++ ){
      if( *ii != 0.0 ){
         BrookOpenMMFloat value    = static_cast<BrookOpenMMFloat>(*ii);
         _inverseSqrtMasses[index] = ( SQRT( one/value ) );
      } else {
         _inverseSqrtMasses[index] = zero;
      }
   }

   return DefaultReturnValue;
}   
 
/*  
 * Setup of BrownianDynamics parameters
 *
 * @param masses                masses
 * @param platform              Brook platform
 *
 * @return nonzero value if error
 *
 * */
    
int BrookBrownianDynamics::setup( const std::vector<double>& masses, const Platform& platform ){
    
// ---------------------------------------------------------------------------------------

   static const std::string methodName      = "BrookBrownianDynamics::setup";

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

   const BrookPlatform brookPlatform            = dynamic_cast<const BrookPlatform&> (platform);
   setLog( brookPlatform.getLog() );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
800
801
   int numberOfParticles  = (int) masses.size();
   setNumberOfParticles( numberOfParticles );
802
803
804

   // set stream sizes and then create streams

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
805
   _initializeStreamSizes( numberOfParticles, platform );
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
   _initializeStreams( platform );

   _setInverseSqrtMasses( masses );

   return DefaultReturnValue;
}

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

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

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

   static const std::string methodName      = "BrookBrownianDynamics::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
844
845
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParticles() );
   message << _getLine( tab, "Number of particles:", value ); 
846

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
853
854
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamSize() );
   message << _getLine( tab, "Particle stream size:", value ); 
855
856
857
858
859
860
861
862
863
864

   (void) LOCAL_SPRINTF( value, "%.5f", getTau() );
   message << _getLine( tab, "Tau:", value ); 

   (void) LOCAL_SPRINTF( value, "%.5f", getTemperature() );
   message << _getLine( tab, "Temperature:", value ); 

   (void) LOCAL_SPRINTF( value, "%.5f", getStepSize() );
   message << _getLine( tab, "Step size:", value ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
865
   (void) LOCAL_SPRINTF( value, "%.5e", getForceScale() );
866
867
   message << _getLine( tab, "Force scale:", value ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
868
   (void) LOCAL_SPRINTF( value, "%.5e", getNoiseAmplitude() );
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
   message << _getLine( tab, "Noise amplitude:", value ); 

   (void) LOCAL_SPRINTF( value, "%.5f", getTemperature() );
   message << _getLine( tab, "Temperature:", value ); 

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

   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      message << std::endl;
      if( _streams[ii] ){
         message << _streams[ii]->getContentsString( );
      }
   }

#undef LOCAL_SPRINTF

   return message.str();
}