BrookIntegrateLangevinStepKernel.cpp 7.94 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
Mods  
Mark Friedrichs committed
38
39
40
41
42
43
44
45
46
/** 
 * BrookIntegrateLangevinStepKernel constructor
 * 
 * @param name                  name of the stream to create
 * @param platform              platform
 * @param openMMBrookInterface  OpenMMBrookInterface reference
 * @param system                System reference  
 *
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
47

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
48
49
50
BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel( std::string name, const Platform& platform,
                                  OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                                  IntegrateLangevinStepKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
Mark Friedrichs's avatar
Mark Friedrichs committed
51
52
53

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

Mark Friedrichs's avatar
Mark Friedrichs committed
54
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
55
56

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
57

58
   _brookLangevinDynamics        = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
59
   _brookShakeAlgorithm            = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
60
61
   _brookRandomNumberGenerator     = NULL;

Mark Friedrichs's avatar
Mark Friedrichs committed
62
63
}

Mark Friedrichs's avatar
Mark Friedrichs committed
64
65
66
67
68
/** 
 * BrookIntegrateVerletStepKernel destructor
 * 
 */
  
Mark Friedrichs's avatar
Mark Friedrichs committed
69
BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
72

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

Mark Friedrichs's avatar
Mark Friedrichs committed
73
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
76

// ---------------------------------------------------------------------------------------
   
77
   delete _brookLangevinDynamics;
Mark Friedrichs's avatar
Mark Friedrichs committed
78
   delete _brookShakeAlgorithm;
Mark Friedrichs's avatar
Mark Friedrichs committed
79
   delete _brookRandomNumberGenerator;
Mark Friedrichs's avatar
Mark Friedrichs committed
80

Mark Friedrichs's avatar
Mark Friedrichs committed
81
82
}

Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85
/** 
 * Initialize the kernel, setting up all parameters related to integrator.
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
86
87
 * @param system                System reference  
 * @param integrator            LangevinIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
88
89
90
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
91
void BrookIntegrateLangevinStepKernel::initialize( const System& system, const LangevinIntegrator& integrator ){
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
Mods  
Mark Friedrichs committed
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
   int numberOfParticles = system.getNumParticles();

   // masses

   std::vector<double> masses;
   masses.resize( numberOfParticles );

   for( int ii = 0; ii < numberOfParticles; ii++ ){
      masses[ii] = static_cast<RealOpenMM>(system.getParticleMass(ii));
   }

   // constraints

   int numberOfConstraints = system.getNumConstraints();

   std::vector<std::vector<int> > constraintIndicesVector;
   constraintIndicesVector.resize( numberOfConstraints );

   std::vector<double> constraintLengths;
   constraintLengths.resize( numberOfConstraints );

   for( int ii = 0; ii < numberOfConstraints; ii++ ){

      int particle1, particle2;
      double distance;

      system.getConstraintParameters( ii, particle1, particle2, distance );
      std::vector<int> constraintIndices;
      constraintIndicesVector[ii]  = constraintIndices;

      constraintIndices[0]         = particle1;
      constraintIndices[1]         = particle2;
      constraintLengths[ii]        = static_cast<RealOpenMM>(distance);
   }

   _brookLangevinDynamics        = new BrookLangevinDynamics( );
135
   _brookLangevinDynamics->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
136

Mark Friedrichs's avatar
Mark Friedrichs committed
137
   _brookShakeAlgorithm          = new BrookShakeAlgorithm( );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
138
   _brookShakeAlgorithm->setup( masses, constraintIndicesVector, constraintLengths, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
139

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

Mark Friedrichs's avatar
Mark Friedrichs committed
142
143
   _brookRandomNumberGenerator   = new BrookRandomNumberGenerator( );
   _brookRandomNumberGenerator->setup( (int) masses.size(), getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
144
//   _brookRandomNumberGenerator->setVerbosity( 1 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
145

Mark Friedrichs's avatar
Mark Friedrichs committed
146
147
}

Mark Friedrichs's avatar
Mark Friedrichs committed
148
149
150
/** 
 * Execute kernel
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
151
152
 * @param context            OpenMMContextImpl reference
 * @param integrator         LangevinIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
153
154
155
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
156
void BrookIntegrateLangevinStepKernel::execute( OpenMMContextImpl& context, const LangevinIntegrator& integrator ){
Mark Friedrichs's avatar
Mark Friedrichs committed
157

Mark Friedrichs's avatar
Mark Friedrichs committed
158
159
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
160
   double epsilon                           = 1.0e-04;
161
   static const std::string methodName      = "BrookIntegrateLangevinStepKernel::execute";
Mark Friedrichs's avatar
Mark Friedrichs committed
162
163
164

// ---------------------------------------------------------------------------------------
   
165
   // first time through initialize _brookLangevinDynamics
Mark Friedrichs's avatar
Mark Friedrichs committed
166
167
168

   // 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
169

Mark Friedrichs's avatar
Mark Friedrichs committed
170
171
   // take step

Mark Friedrichs's avatar
Mark Friedrichs committed
172
   double differences[3];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
173
174
175
   differences[0] = integrator.getTemperature() - (double) _brookLangevinDynamics->getTemperature();
   differences[1] = integrator.getFriction()    - (double) _brookLangevinDynamics->getFriction();
   differences[2] = integrator.getStepSize()    - (double) _brookLangevinDynamics->getStepSize();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
176
   if( fabs( differences[0] ) > epsilon || fabs( differences[1] ) > epsilon || fabs( differences[2] ) > epsilon ){
177
//printf( "%s calling updateParameters\n", methodName.c_str() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
178
      _brookLangevinDynamics->updateParameters( integrator.getTemperature(), integrator.getFriction(), integrator.getStepSize() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
179
180
181
   } else {
//printf( "%s NOT calling updateParameters\n", methodName.c_str() );
}
182

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
183
184
   _brookLangevinDynamics->update( *(_openMMBrookInterface.getParticlePositions()), *(_openMMBrookInterface.getParticleVelocities()), 
                                   *(_openMMBrookInterface.getParticleForces()), *_brookShakeAlgorithm, *_brookRandomNumberGenerator );
Mark Friedrichs's avatar
Mark Friedrichs committed
185
186

}