ReferenceVariableStochasticDynamics.cpp 7.73 KB
Newer Older
1

2
/* Portions copyright (c) 2006-2024 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
25
26
 * 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.
 */

#include <cstring>
#include <sstream>
27
#include <algorithm>
28

29
#include "SimTKOpenMMUtilities.h"
30
#include "ReferenceVariableStochasticDynamics.h"
31
#include "ReferenceVirtualSites.h"
peastman's avatar
peastman committed
32
#include "openmm/OpenMMException.h"
33
34
35

#include <cstdio>

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

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

   ReferenceVariableStochasticDynamics constructor

   @param numberOfAtoms  number of atoms
   @param deltaT         delta t for dynamics
45
   @param friction       friction coefficient
46
47
48
49
50
   @param temperature    temperature
   @param accuracy       required accuracy

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

51
ReferenceVariableStochasticDynamics::ReferenceVariableStochasticDynamics(int numberOfAtoms,
peastman's avatar
peastman committed
52
53
                                                          double friction, double temperature,
                                                          double accuracy) :
54
           ReferenceDynamics(numberOfAtoms, 0.0f, temperature), friction(friction), _accuracy(accuracy) {
55
   xPrime.resize(numberOfAtoms);
56
   oldx.resize(numberOfAtoms);
57
   inverseMasses.resize(numberOfAtoms);
58
59
60
61
62
63
64
65
}

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

   ReferenceVariableStochasticDynamics destructor

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

66
ReferenceVariableStochasticDynamics::~ReferenceVariableStochasticDynamics() {
67
68
69
70
71
72
73
74
75
76
}

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

   Get the required accuracy

   @return accuracy

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

peastman's avatar
peastman committed
77
double ReferenceVariableStochasticDynamics::getAccuracy() const {
78
79
80
81
82
83
84
85
86
    return _accuracy;
}

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

   Set the required accuracy

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

peastman's avatar
peastman committed
87
void ReferenceVariableStochasticDynamics::setAccuracy(double accuracy) {
88
89
90
91
92
    _accuracy = accuracy;
}

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

93
   Get friction coefficient
94
95
96

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

peastman's avatar
peastman committed
97
double ReferenceVariableStochasticDynamics::getFriction() const {
98
   return friction;
99
100
}

101
void ReferenceVariableStochasticDynamics::updatePart1(int numberOfAtoms, vector<Vec3>& velocities, vector<Vec3>& forces, vector<double>& inverseMasses, double maxStepSize) {
102
   // Select the step size to use
peastman's avatar
peastman committed
103
    double error = 0;
104
105
    for (int i = 0; i < numberOfAtoms; ++i) {
        for (int j = 0; j < 3; ++j) {
peastman's avatar
peastman committed
106
            double xerror = inverseMasses[i]*forces[i][j];
107
108
109
            error += xerror*xerror;
        }
    }
peastman's avatar
peastman committed
110
    error = sqrt(error/(numberOfAtoms*3));
111
    double dt = sqrt(getAccuracy()/error);
112
    if (getDeltaT() > 0.0f)
113
114
115
116
117
118
        dt = std::min(dt, getDeltaT()*2.0f); // For safety, limit how quickly dt can increase.
    if (dt > getDeltaT() && dt < 1.2f*getDeltaT())
        dt = getDeltaT(); // Keeping dt constant between steps improves the behavior of the integrator.
    if (dt > maxStepSize)
        dt = maxStepSize;
    setDeltaT(dt);
119
120
121
 
    // perform first update

122
123
124
    for (int i = 0; i < numberOfAtoms; i++)
        if (inverseMasses[i] != 0.0)
            velocities[i] += (dt*inverseMasses[i])*forces[i];
125
126
}

peastman's avatar
peastman committed
127
void ReferenceVariableStochasticDynamics::updatePart2(int numberOfAtoms, vector<Vec3>& atomCoordinates,
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                                         vector<Vec3>& velocities, vector<double>& inverseMasses,
                                         vector<Vec3>& xPrime) {
    const double halfdt = 0.5*getDeltaT();
    const double kT = BOLTZ*getTemperature();
    const double friction = getFriction();
    const double vscale = exp(-getDeltaT()*friction);
    const double noisescale = sqrt(1-vscale*vscale);

    for (int i = 0; i < numberOfAtoms; i++) {
        if (inverseMasses[i] != 0.0) {
            xPrime[i] = atomCoordinates[i] + velocities[i]*halfdt;
            velocities[i] = vscale*velocities[i] + noisescale*sqrt(kT*inverseMasses[i])*Vec3(
                    SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(),
                    SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(),
                    SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
            xPrime[i] = xPrime[i] + velocities[i]*halfdt;
            oldx[i] = xPrime[i];
        }
    }
147
148
149
150
151
152
153
}

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

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

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

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

peastman's avatar
peastman committed
162
163
164
void ReferenceVariableStochasticDynamics::update(const OpenMM::System& system, vector<Vec3>& atomCoordinates,
                                          vector<Vec3>& velocities,
                                          vector<Vec3>& forces, vector<double>& masses, double maxStepSize, double tolerance) {
165
166
167
168
169
170
171
172
173
174
175
176
    int numberOfAtoms = system.getNumParticles();
    ReferenceConstraintAlgorithm* referenceConstraintAlgorithm = getReferenceConstraintAlgorithm();
    if (getTimeStep() == 0) {
        // Invert masses

        for (int ii = 0; ii < numberOfAtoms; ii++) {
            if (masses[ii] == 0.0)
                inverseMasses[ii] = 0.0;
            else
                inverseMasses[ii] = 1/masses[ii];
        }
    }
177

178
    // 1st update
179

180
181
182
    updatePart1(numberOfAtoms, velocities, forces, inverseMasses, maxStepSize);
    if (referenceConstraintAlgorithm)
        referenceConstraintAlgorithm->applyToVelocities(atomCoordinates, velocities, inverseMasses, tolerance);
183

184
    // 2nd update
185

186
187
188
    updatePart2(numberOfAtoms, atomCoordinates, velocities, inverseMasses, xPrime);
    if (referenceConstraintAlgorithm)
        referenceConstraintAlgorithm->apply(atomCoordinates, xPrime, inverseMasses, tolerance);
189

190
    // copy xPrime -> atomCoordinates
191

192
193
194
195
196
197
198
    double invStepSize = 1.0/getDeltaT();
    for (int i = 0; i < numberOfAtoms; i++) {
        if (masses[i] != 0.0) {
            velocities[i] += (xPrime[i]-oldx[i])*invStepSize;
            atomCoordinates[i] = xPrime[i];
        }
    }
199

200
201
    getVirtualSites().computePositions(system, atomCoordinates);
    incrementTimeStep();
202
}