"vscode:/vscode.git/clone" did not exist on "2a0b3d7a5da23037b25c31ce36efba1565705106"
BrookIntegrateVerletStepKernel.cpp 7.92 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 "BrookIntegrateVerletStepKernel.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
/** 
 * BrookIntegrateVerletStepKernel constructor
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
41
42
43
44
 * @param name                  name of the kernel
 * @param platform              platform
 * @param openMMBrookInterface  OpenMMBrookInterface reference
 * @param system                System reference  
Mark Friedrichs's avatar
Mark Friedrichs committed
45
46
47
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
48
49
50
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
51
52
53
54
55
56
57

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

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

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
58
59
   _brookVerletDynamics   = NULL;
   _brookShakeAlgorithm   = NULL;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
60
61
62
63
64
65
66
67

   const BrookPlatform brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   } else {
      _log                = NULL;
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
68
69
}

Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
72
73
74
/** 
 * BrookIntegrateVerletStepKernel destructor
 * 
 */
  
Mark Friedrichs's avatar
Mark Friedrichs committed
75
76
77
78
79
80
81
BrookIntegrateVerletStepKernel::~BrookIntegrateVerletStepKernel( ){

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

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

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
82
83
84

   delete _brookVerletDynamics;
   delete _brookShakeAlgorithm;
Mark Friedrichs's avatar
Mark Friedrichs committed
85
86
87
   
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
/** 
 * 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
113
114
115
/** 
 * Initialize the kernel, setting up all parameters related to integrator.
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
116
117
 * @param system                System reference  
 * @param integrator            VerletIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
121
void BrookIntegrateVerletStepKernel::initialize(  const System& system, const VerletIntegrator& integrator ){
Mark Friedrichs's avatar
Mark Friedrichs committed
122
123
124

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
125
126
   int printOn                              = 1;
   static const std::string methodName      = "BrookIntegrateVerletStepKernel::initialize";
Mark Friedrichs's avatar
Mark Friedrichs committed
127
128
129

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
   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
161
162
   _brookVerletDynamics          = new BrookVerletDynamics( );
   _brookVerletDynamics->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
163
   _brookVerletDynamics->setLog( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
164
165

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
169
   BrookOpenMMFloat tolerance    = static_cast<BrookOpenMMFloat>( integrator.getConstraintTolerance() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
170
   _brookShakeAlgorithm->setShakeTolerance( tolerance );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
171
   _brookShakeAlgorithm->setMaxIterations( 40 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
172
173
174
175
176

   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
177

Mark Friedrichs's avatar
Mark Friedrichs committed
178
179
}

Mark Friedrichs's avatar
Mark Friedrichs committed
180
181
182
/** 
 * Execute kernel
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
183
184
 * @param context            OpenMMContextImpl reference
 * @param integrator         VerletIntegrator reference
Mark Friedrichs's avatar
Mark Friedrichs committed
185
186
187
 *
 */

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

Mark Friedrichs's avatar
Mark Friedrichs committed
190
191
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
192
193
   double epsilon                           = 1.0e-04;
   static const std::string methodName      = "BrookIntegrateVerletStepKernel::execute";
Mark Friedrichs's avatar
Mark Friedrichs committed
194
195

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
196
197
198
199
200
201

   // 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
202
   double stepSize   = integrator.getStepSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
203
204
205
206
207
   double difference = stepSize - (double) _brookVerletDynamics->getStepSize();
   if( fabs( difference ) > epsilon ){
      _brookVerletDynamics->updateParameters( stepSize );
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
208
209
   _brookVerletDynamics->update( *(_openMMBrookInterface.getParticlePositions()), *(_openMMBrookInterface.getParticleVelocities()),
                                 *(_openMMBrookInterface.getParticleForces()), *_brookShakeAlgorithm );
Mark Friedrichs's avatar
Mark Friedrichs committed
210

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
211
212

}