BrookIntegrateVerletStepKernel.cpp 7.62 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
10
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
11
12
 * Contributors:                                                              *
 *                                                                            *
13
14
15
16
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
17
 *                                                                            *
18
19
20
21
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
22
 *                                                                            *
23
24
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
Mark Friedrichs's avatar
Mark Friedrichs committed
25
26
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
27
#include "BrookIntegrateVerletStepKernel.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
28
29
30
31
32
#include "BrookStreamInternal.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
35
/** 
 * BrookIntegrateVerletStepKernel constructor
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
36
37
38
39
 * @param name                  name of the kernel
 * @param platform              platform
 * @param openMMBrookInterface  OpenMMBrookInterface reference
 * @param system                System reference  
Mark Friedrichs's avatar
Mark Friedrichs committed
40
41
42
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
43
44
45
BrookIntegrateVerletStepKernel::BrookIntegrateVerletStepKernel( std::string name, const Platform& platform,
                                  OpenMMBrookInterface& openMMBrookInterface, System& system ) : 
                                  IntegrateVerletStepKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
Mark Friedrichs's avatar
Mark Friedrichs committed
46
47
48
49
50
51
52

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

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

// ---------------------------------------------------------------------------------------
   
53
54
   _brookVerletDynamics               = NULL;
   _brookShakeAlgorithm               = NULL;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
55

56
   const BrookPlatform& brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
57
58
59
60
61
62
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   } else {
      _log                = NULL;
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
63
64
}

Mark Friedrichs's avatar
Mark Friedrichs committed
65
66
67
68
69
/** 
 * BrookIntegrateVerletStepKernel destructor
 * 
 */
  
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
72
73
74
75
76
BrookIntegrateVerletStepKernel::~BrookIntegrateVerletStepKernel( ){

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

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

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
77
78
79

   delete _brookVerletDynamics;
   delete _brookShakeAlgorithm;
Mark Friedrichs's avatar
Mark Friedrichs committed
80
81
82
   
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
/** 
 * Get log file reference
 * 
 * @return  log file reference
 *
 */

FILE* BrookIntegrateVerletStepKernel::getLog( void ) const {
   return _log;
}

/** 
 * Set log file reference
 * 
 * @param  log file reference
 *
 * @return  DefaultReturnValue
 *
 */

int BrookIntegrateVerletStepKernel::setLog( FILE* log ){
   _log = log;
   return DefaultReturnValue;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
108
109
110
/** 
 * Initialize the kernel, setting up all parameters related to integrator.
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
111
112
 * @param system                System reference  
 * @param integrator            VerletIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
113
114
115
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
116
void BrookIntegrateVerletStepKernel::initialize(  const System& system, const VerletIntegrator& integrator ){
Mark Friedrichs's avatar
Mark Friedrichs committed
117
118
119

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

Mark Friedrichs's avatar
Mark Friedrichs committed
120
   int printOn                              = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
121
   static const std::string methodName      = "BrookIntegrateVerletStepKernel::initialize";
Mark Friedrichs's avatar
Mark Friedrichs committed
122
123
124

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
   FILE* log             = getLog();
   int numberOfParticles = system.getNumParticles();

   // masses

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

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

   // constraints

   int numberOfConstraints = system.getNumConstraints();

   std::vector<std::vector<int> > constraintIndicesVector;
   constraintIndicesVector.resize( numberOfConstraints );
   std::vector<double> constraintLengths;
   for( int ii = 0; ii < numberOfConstraints; ii++ ){

      int particle1, particle2;
      double distance;

      system.getConstraintParameters( ii, particle1, particle2, distance );

      constraintIndicesVector[ii].push_back( particle1 );
      constraintIndicesVector[ii].push_back( particle2 );
      constraintLengths.push_back( distance );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
156
157
   _brookVerletDynamics          = new BrookVerletDynamics( );
   _brookVerletDynamics->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
158
   _brookVerletDynamics->setLog( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
159
160

   _brookShakeAlgorithm          = new BrookShakeAlgorithm( );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
161
   _brookShakeAlgorithm->setLog( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
162
163
   _brookShakeAlgorithm->setup( masses, constraintIndicesVector, constraintLengths, getPlatform() );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
164
   BrookOpenMMFloat tolerance    = static_cast<BrookOpenMMFloat>( integrator.getConstraintTolerance() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
165
   _brookShakeAlgorithm->setShakeTolerance( tolerance );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
166
   _brookShakeAlgorithm->setMaxIterations( 40 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
167
168
169
170
171

   if( printOn && log ){
      (void) fprintf( log, "%s done w/ setup: particles=%d const=%d\n", methodName.c_str(), numberOfParticles, numberOfConstraints );
      (void) fflush( log );
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
172

Mark Friedrichs's avatar
Mark Friedrichs committed
173
174
}

Mark Friedrichs's avatar
Mark Friedrichs committed
175
176
177
/** 
 * Execute kernel
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
178
179
 * @param context            OpenMMContextImpl reference
 * @param integrator         VerletIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
180
181
182
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
183
void BrookIntegrateVerletStepKernel::execute( OpenMMContextImpl& context, const VerletIntegrator& integrator ){
Mark Friedrichs's avatar
Mark Friedrichs committed
184

Mark Friedrichs's avatar
Mark Friedrichs committed
185
186
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
187
188
   double epsilon                           = 1.0e-04;
   static const std::string methodName      = "BrookIntegrateVerletStepKernel::execute";
Mark Friedrichs's avatar
Mark Friedrichs committed
189
190

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
191
192
193
194
195
196

   // for each subsequent call, check if parameters need to be updated due to a change
   // in the step size

   // take step

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
197
   double stepSize   = integrator.getStepSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
198
199
200
201
202
   double difference = stepSize - (double) _brookVerletDynamics->getStepSize();
   if( fabs( difference ) > epsilon ){
      _brookVerletDynamics->updateParameters( stepSize );
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
203
204
   _brookVerletDynamics->update( *(_openMMBrookInterface.getParticlePositions()), *(_openMMBrookInterface.getParticleVelocities()),
                                 *(_openMMBrookInterface.getParticleForces()), *_brookShakeAlgorithm );
205
   _openMMBrookInterface.setTime(_openMMBrookInterface.getTime());
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
206
}