BrookIntegrateLangevinStepKernel.cpp 7.08 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
/* -------------------------------------------------------------------------- *
 *                                   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: Peter Eastman                                                     *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
32
#include "BrookIntegrateLangevinStepKernel.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
35
36
37
#include "BrookStreamInternal.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mark Friedrichs committed
38
39
40
41
42
43
44
45
      /** 
       * BrookIntegrateLangevinStepKernel constructor
       * 
       * @param name        name of the stream to create
       * @param platform    platform
       *
       */

Mark Friedrichs's avatar
Mark Friedrichs committed
46
BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel( std::string name, const Platform& platform ) :
Mark Friedrichs's avatar
Mark Friedrichs committed
47
                                  IntegrateLangevinStepKernel( name, platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
48
49
50

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

Mark Friedrichs's avatar
Mark Friedrichs committed
51
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
52
53

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
54
55
56

   _brookStochasticDynamics        = NULL;
   _brookShakeAlgorithm            = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
57
58
   _brookRandomNumberGenerator     = NULL;

Mark Friedrichs's avatar
Mark Friedrichs committed
59
60
}

Mark Friedrichs's avatar
Mark Friedrichs committed
61
62
63
64
65
/** 
 * BrookIntegrateVerletStepKernel destructor
 * 
 */
  
Mark Friedrichs's avatar
Mark Friedrichs committed
66
BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
67
68
69

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

Mark Friedrichs's avatar
Mark Friedrichs committed
70
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
71
72
73

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
74
   delete _brookStochasticDynamics;
Mark Friedrichs's avatar
Mark Friedrichs committed
75
   delete _brookShakeAlgorithm;
Mark Friedrichs's avatar
Mark Friedrichs committed
76
   delete _brookRandomNumberGenerator;
Mark Friedrichs's avatar
Mark Friedrichs committed
77

Mark Friedrichs's avatar
Mark Friedrichs committed
78
79
}

Mark Friedrichs's avatar
Mark Friedrichs committed
80
81
82
83
84
85
86
87
88
/** 
 * Initialize the kernel, setting up all parameters related to integrator.
 * 
 * @param masses             the mass of each atom
 * @param constraintIndices  each element contains the indices of two atoms whose distance should be constrained
 * @param constraintLengths  the required distance between each pair of constrained atoms
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
89
void BrookIntegrateLangevinStepKernel::initialize( const vector<double>& masses,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
90
91
                                                   const vector<vector<int> >& constraintIndices,
                                                   const vector<double>& constraintLengths ){
Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94

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

Mark Friedrichs's avatar
Mark Friedrichs committed
95
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::initialize";
Mark Friedrichs's avatar
Mark Friedrichs committed
96
97
98

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
99
   _brookStochasticDynamics      = new BrookStochasticDynamics( );
Mark Friedrichs's avatar
Mark Friedrichs committed
100
101
   _brookStochasticDynamics->setup( masses, getPlatform() );

Mark Friedrichs's avatar
Mark Friedrichs committed
102
   _brookShakeAlgorithm          = new BrookShakeAlgorithm( );
Mark Friedrichs's avatar
Mark Friedrichs committed
103
   _brookShakeAlgorithm->setup( masses, constraintIndices, constraintLengths, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
104

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
105
106
   // assert( (_brookShakeAlgorithm->getNumberOfConstraints() > 0) );

Mark Friedrichs's avatar
Mark Friedrichs committed
107
108
   _brookRandomNumberGenerator   = new BrookRandomNumberGenerator( );
   _brookRandomNumberGenerator->setup( (int) masses.size(), getPlatform() );
109
   _brookRandomNumberGenerator->setVerbosity( 1 );
Mark Friedrichs's avatar
Mark Friedrichs committed
110
111
}

Mark Friedrichs's avatar
Mark Friedrichs committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** 
 * Execute kernel
 * 
 * @param positions          atom coordinates
 * @param velocities         atom velocities
 * @param forces             atom forces
 * @param temperature        heat bath temperature
 * @param friction           friction coefficient coupling the system to the heat bath
 * @param stepSize           integration step size
 *
 */

void BrookIntegrateLangevinStepKernel::execute( Stream& positions, Stream& velocities,
                                                const Stream& forces, double temperature,
                                                double friction, double stepSize ){

Mark Friedrichs's avatar
Mark Friedrichs committed
128
129
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
130
   double epsilon = 1.0e-04;
131
   static const std::string methodName      = "BrookIntegrateLangevinStepKernel::execute";
Mark Friedrichs's avatar
Mark Friedrichs committed
132
133
134

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
135
136
137
138
   // first time through initialize _brookStochasticDynamics

   // for each subsequent call, check if parameters need to be updated due to a change
   // in T, gamma, or the step size
Mark Friedrichs's avatar
Mark Friedrichs committed
139

Mark Friedrichs's avatar
Mark Friedrichs committed
140
141
   // take step

Mark Friedrichs's avatar
Mark Friedrichs committed
142
143
144
145
   double differences[3];
   differences[0] = temperature - (double) _brookStochasticDynamics->getTemperature();
   differences[1] = friction    - (double) _brookStochasticDynamics->getFriction();
   differences[2] = stepSize    - (double) _brookStochasticDynamics->getStepSize();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
146
   if( fabs( differences[0] ) > epsilon || fabs( differences[1] ) > epsilon || fabs( differences[2] ) > epsilon ){
147
//printf( "%s calling updateParameters\n", methodName.c_str() );
Mark Friedrichs's avatar
Mark Friedrichs committed
148
      _brookStochasticDynamics->updateParameters( temperature, friction, stepSize );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
149
150
151
   } else {
//printf( "%s NOT calling updateParameters\n", methodName.c_str() );
}
152
153

   _brookStochasticDynamics->update( positions, velocities, forces, *_brookShakeAlgorithm, *_brookRandomNumberGenerator );
Mark Friedrichs's avatar
Mark Friedrichs committed
154
155

}