ReferenceStochasticDynamics.cpp 7.55 KB
Newer Older
1

2
/* Portions copyright (c) 2006-2016 Stanford University and Simbios.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * Contributors: Pande Group
 *
 * 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.
 */

25
#include <cstring>
26
27
#include <sstream>

28
#include "SimTKOpenMMUtilities.h"
29
#include "ReferenceStochasticDynamics.h"
30
#include "ReferenceVirtualSites.h"
peastman's avatar
peastman committed
31
#include "openmm/OpenMMException.h"
32

33
34
#include <cstdio>

35
using std::vector;
36
using namespace OpenMM;
37

38
39
40
41
42
43
/**---------------------------------------------------------------------------------------

   ReferenceStochasticDynamics constructor

   @param numberOfAtoms  number of atoms
   @param deltaT         delta t for dynamics
44
   @param friction       friction coefficient
45
46
47
48
   @param temperature    temperature

   --------------------------------------------------------------------------------------- */

49
ReferenceStochasticDynamics::ReferenceStochasticDynamics(int numberOfAtoms,
peastman's avatar
peastman committed
50
51
                                                         double deltaT, double friction,
                                                         double temperature) : 
52
           ReferenceDynamics(numberOfAtoms, deltaT, temperature), friction(friction) {
53
54
   xPrime.resize(numberOfAtoms);
   inverseMasses.resize(numberOfAtoms);
55
56
57
58
59
60
61
62
}

/**---------------------------------------------------------------------------------------

   ReferenceStochasticDynamics destructor

   --------------------------------------------------------------------------------------- */

63
ReferenceStochasticDynamics::~ReferenceStochasticDynamics() {
64
65
66
67
}

/**---------------------------------------------------------------------------------------

68
   Get friction coefficient
69
70
71

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
72
double ReferenceStochasticDynamics::getFriction() const {
73
   return friction;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

/**---------------------------------------------------------------------------------------

   First SD update; based on code in update.c do_update_sd() Gromacs 3.1.4

   @param numberOfAtoms       number of atoms
   @param atomCoordinates     atom coordinates
   @param velocities          velocities
   @param forces              forces
   @param inverseMasses       inverse atom masses
   @param xPrime              xPrime

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
89
90
91
92
void ReferenceStochasticDynamics::updatePart1(int numberOfAtoms, vector<Vec3>& atomCoordinates,
                                              vector<Vec3>& velocities,
                                              vector<Vec3>& forces, vector<double>& inverseMasses,
                                              vector<Vec3>& xPrime) {
93
94
   // perform first update

peastman's avatar
peastman committed
95
96
97
98
99
100
   double dt = getDeltaT();
   double friction = getFriction();
   const double vscale = exp(-dt*friction);
   const double fscale = (friction == 0 ? dt : (1-vscale)/friction);
   const double kT = BOLTZ*getTemperature();
   const double noisescale = sqrt(kT*(1-vscale*vscale));
101

102
   for (int ii = 0; ii < numberOfAtoms; ii++) {
103
       if (inverseMasses[ii] != 0.0) {
peastman's avatar
peastman committed
104
           double sqrtInvMass = sqrt(inverseMasses[ii]);
105
106
107
108
           for (int jj = 0; jj < 3; jj++) {
               velocities[ii][jj]  = vscale*velocities[ii][jj] + fscale*inverseMasses[ii]*forces[ii][jj] + noisescale*sqrtInvMass*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
           }
       }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
   }
}

/**---------------------------------------------------------------------------------------

   Second update; based on code in update.c do_update_sd() w/ bFirstHalf = false in Gromacs 3.1.4

   @param numberOfAtoms       number of atoms
   @param atomCoordinates     atom coordinates
   @param velocities          velocities
   @param forces              forces
   @param masses              atom masses

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
124
125
126
127
void ReferenceStochasticDynamics::updatePart2(int numberOfAtoms, vector<Vec3>& atomCoordinates,
                                              vector<Vec3>& velocities,
                                              vector<Vec3>& forces, vector<double>& inverseMasses,
                                              vector<Vec3>& xPrime) {
128
129
   // perform second update

130
   for (int ii = 0; ii < numberOfAtoms; ii++) {
131
       if (inverseMasses[ii] != 0.0)
peastman's avatar
peastman committed
132
            xPrime[ii] = atomCoordinates[ii]+velocities[ii]*getDeltaT();
133
134
135
   }
}

peastman's avatar
peastman committed
136
137
138
139
void ReferenceStochasticDynamics::updatePart3(int numberOfAtoms, vector<Vec3>& atomCoordinates,
                                              vector<Vec3>& velocities, vector<double>& inverseMasses,
                                              vector<Vec3>& xPrime) {
   double invStepSize = 1.0/getDeltaT();
peastman's avatar
peastman committed
140
141
142
143
144
145
146
   for (int i = 0; i < numberOfAtoms; ++i)
       if (inverseMasses[i] != 0) {
            velocities[i] = (xPrime[i]-atomCoordinates[i])*invStepSize;
            atomCoordinates[i] = xPrime[i];
       }
}

147
148
149
150
151
/**---------------------------------------------------------------------------------------

   Update -- driver routine for performing stochastic dynamics update of coordinates
   and velocities

152
   @param system              the System to be integrated
153
154
155
156
157
158
159
   @param atomCoordinates     atom coordinates
   @param velocities          velocities
   @param forces              forces
   @param masses              atom masses

   --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
160
161
void ReferenceStochasticDynamics::update(const OpenMM::System& system, vector<Vec3>& atomCoordinates,
                                          vector<Vec3>& velocities, vector<Vec3>& forces, vector<double>& masses, double tolerance) {
162
163
   // first-time-through initialization

164
   int numberOfAtoms = system.getNumParticles();
165
   if (getTimeStep() == 0) {
166
167
      // invert masses

168
      for (int ii = 0; ii < numberOfAtoms; ii++) {
peastman's avatar
peastman committed
169
170
         if (masses[ii] == 0.0)
             inverseMasses[ii] = 0.0;
171
         else
peastman's avatar
peastman committed
172
             inverseMasses[ii] = 1.0/masses[ii];
173
174
175
176
177
      }
   }

   // 1st update

178
   updatePart1(numberOfAtoms, atomCoordinates, velocities, forces, inverseMasses, xPrime);
179
180
181

   // 2nd update

182
   updatePart2(numberOfAtoms, atomCoordinates, velocities, forces, inverseMasses, xPrime);
183

184
   ReferenceConstraintAlgorithm* referenceConstraintAlgorithm = getReferenceConstraintAlgorithm();
185
   if (referenceConstraintAlgorithm)
186
      referenceConstraintAlgorithm->apply(atomCoordinates, xPrime, inverseMasses, tolerance);
187

188
   // copy xPrime -> atomCoordinates
189

peastman's avatar
peastman committed
190
   updatePart3(numberOfAtoms, atomCoordinates, velocities, inverseMasses, xPrime);
191

192
   ReferenceVirtualSites::computePositions(system, atomCoordinates);
193
194
   incrementTimeStep();
}