ReferenceRpmdKernels.cpp 18.2 KB
Newer Older
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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
9
 * Portions copyright (c) 2011-2022 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#include "ReferenceRpmdKernels.h"
33
#include "openmm/OpenMMException.h"
34
#include "openmm/internal/ContextImpl.h"
35
#include "SimTKOpenMMUtilities.h"
Peter Eastman's avatar
Peter Eastman committed
36
37
38
39
40
#ifdef _MSC_VER
  #define POCKETFFT_NO_VECTORS
#endif
#include "pocketfft_hdronly.h"
#include <complex>
41
42
43
44

using namespace OpenMM;
using namespace std;

peastman's avatar
peastman committed
45
static vector<Vec3>& extractPositions(ContextImpl& context) {
46
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
47
    return *data->positions;
48
49
}

peastman's avatar
peastman committed
50
static vector<Vec3>& extractVelocities(ContextImpl& context) {
51
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
52
    return *data->velocities;
53
54
}

peastman's avatar
peastman committed
55
static vector<Vec3>& extractForces(ContextImpl& context) {
56
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
57
    return *data->forces;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
}

void ReferenceIntegrateRPMDStepKernel::initialize(const System& system, const RPMDIntegrator& integrator) {
    int numCopies = integrator.getNumCopies();
    int numParticles = system.getNumParticles();
    positions.resize(numCopies);
    velocities.resize(numCopies);
    forces.resize(numCopies);
    for (int i = 0; i < numCopies; i++) {
        positions[i].resize(numParticles);
        velocities[i].resize(numParticles);
        forces[i].resize(numParticles);
    }
    SimTKOpenMMUtilities::setRandomNumberSeed((unsigned int) integrator.getRandomNumberSeed());
72
73
74
75
76
77
    
    // Build a list of contractions.
    
    groupsNotContracted = -1;
    const map<int, int>& contractions = integrator.getContractions();
    int maxContractedCopies = 0;
peastman's avatar
peastman committed
78
79
80
    for (auto& c : contractions) {
        int group = c.first;
        int copies = c.second;
81
82
83
84
85
86
87
88
89
90
91
92
        if (group < 0 || group > 31)
            throw OpenMMException("RPMDIntegrator: Force group must be between 0 and 31");
        if (copies < 0 || copies > numCopies)
            throw OpenMMException("RPMDIntegrator: Number of copies for contraction cannot be greater than the total number of copies being simulated");
        if (copies != numCopies) {
            if (groupsByCopies.find(copies) == groupsByCopies.end()) {
                groupsByCopies[copies] = 1<<group;
                if (copies > maxContractedCopies)
                    maxContractedCopies = copies;
            }
            else
                groupsByCopies[copies] |= 1<<group;
peastman's avatar
peastman committed
93
            groupsNotContracted -= 1<<group;
94
95
        }
    }
96
    groupsNotContracted &= integrator.getIntegrationForceGroups();
97
98
99
100
101
102
103
104
105
    
    // Create workspace for doing contractions.
    
    contractedPositions.resize(maxContractedCopies);
    contractedForces.resize(maxContractedCopies);
    for (int i = 0; i < maxContractedCopies; i++) {
        contractedPositions[i].resize(numParticles);
        contractedForces[i].resize(numParticles);
    }
106
107
}

108
109
110
void ReferenceIntegrateRPMDStepKernel::execute(ContextImpl& context, const RPMDIntegrator& integrator, bool forcesAreValid) {
    const int numCopies = positions.size();
    const int numParticles = positions[0].size();
peastman's avatar
peastman committed
111
112
    const double dt = integrator.getStepSize();
    const double halfdt = 0.5*dt;
113
    const System& system = context.getSystem();
peastman's avatar
peastman committed
114
115
116
    vector<Vec3>& pos = extractPositions(context);
    vector<Vec3>& vel = extractVelocities(context);
    vector<Vec3>& f = extractForces(context);
117
118
119
    
    // Loop over copies and compute the force on each one.
    
120
121
    if (!forcesAreValid)
        computeForces(context, integrator);
122
123

    // Apply the PILE-L thermostat.
124
    
Peter Eastman's avatar
Peter Eastman committed
125
126
    vector<complex<double>> v(numCopies);
    vector<complex<double>> q(numCopies);
peastman's avatar
peastman committed
127
128
129
130
131
132
    const double hbar = 1.054571628e-34*AVOGADRO/(1000*1e-12);
    const double scale = 1.0/sqrt((double) numCopies);
    const double nkT = numCopies*BOLTZ*integrator.getTemperature();
    const double twown = 2.0*nkT/hbar;
    const double c1_0 = exp(-halfdt*integrator.getFriction());
    const double c2_0 = sqrt(1.0-c1_0*c1_0);
133
134
135
136
    if (integrator.getApplyThermostat()) {
        for (int particle = 0; particle < numParticles; particle++) {
            if (system.getParticleMass(particle) == 0.0)
                continue;
peastman's avatar
peastman committed
137
            const double c3_0 = c2_0*sqrt(nkT/system.getParticleMass(particle));
138
139
            for (int component = 0; component < 3; component++) {
                for (int k = 0; k < numCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
140
141
                    v[k] = complex<double>(scale*velocities[k][particle][component], 0.0);
                pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, v.data(), v.data(), 1.0, 0);
142

143
                // Apply a local Langevin thermostat to the centroid mode.
144

Peter Eastman's avatar
Peter Eastman committed
145
                v[0].real(v[0].real()*c1_0 + c3_0*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
146
147
148
149
150

                // Use critical damping white noise for the remaining modes.

                for (int k = 1; k <= numCopies/2; k++) {
                    const bool isCenter = (numCopies%2 == 0 && k == numCopies/2);
peastman's avatar
peastman committed
151
152
153
154
155
156
                    const double wk = twown*sin(k*M_PI/numCopies);
                    const double c1 = exp(-2.0*wk*halfdt);
                    const double c2 = sqrt((1.0-c1*c1)/2) * (isCenter ? sqrt(2.0) : 1.0);
                    const double c3 = c2*sqrt(nkT/system.getParticleMass(particle));
                    double rand1 = c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                    double rand2 = (isCenter ? 0.0 : c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
Peter Eastman's avatar
Peter Eastman committed
157
                    v[k] = v[k]*c1 + complex<double>(rand1, rand2);
158
                    if (k < numCopies-k)
Peter Eastman's avatar
Peter Eastman committed
159
                        v[numCopies-k] = v[numCopies-k]*c1 + complex<double>(rand1, -rand2);
160
                }
Peter Eastman's avatar
Peter Eastman committed
161
                pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, v.data(), v.data(), 1.0, 0);
162
                for (int k = 0; k < numCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
163
                    velocities[k][particle][component] = scale*v[k].real();
164
165
166
167
            }
        }
    }

168
169
170
171
    // Update velocities.
    
    for (int i = 0; i < numCopies; i++)
        for (int j = 0; j < numParticles; j++)
Peter Eastman's avatar
Peter Eastman committed
172
173
            if (system.getParticleMass(j) != 0.0)
                velocities[i][j] += forces[i][j]*(halfdt/system.getParticleMass(j));
174
175
176
177
    
    // Evolve the free ring polymer by transforming to the frequency domain.

    for (int particle = 0; particle < numParticles; particle++) {
Peter Eastman's avatar
Peter Eastman committed
178
179
        if (system.getParticleMass(particle) == 0.0)
            continue;
180
181
        for (int component = 0; component < 3; component++) {
            for (int k = 0; k < numCopies; k++) {
Peter Eastman's avatar
Peter Eastman committed
182
183
                q[k] = complex<double>(scale*positions[k][particle][component], 0.0);
                v[k] = complex<double>(scale*velocities[k][particle][component], 0.0);
184
            }
Peter Eastman's avatar
Peter Eastman committed
185
186
            pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, q.data(), q.data(), 1.0, 0);
            pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, v.data(), v.data(), 1.0, 0);
Peter Eastman's avatar
Peter Eastman committed
187
            q[0] += v[0]*dt;
188
            for (int k = 1; k < numCopies; k++) {
peastman's avatar
peastman committed
189
190
191
192
193
                const double wk = twown*sin(k*M_PI/numCopies);
                const double wt = wk*dt;
                const double coswt = cos(wt);
                const double sinwt = sin(wt);
                const double wm = wk*system.getParticleMass(particle);
Peter Eastman's avatar
Peter Eastman committed
194
                const complex<double> vprime = v[k]*coswt - q[k]*(wk*sinwt); // Advance velocity from t to t+dt
Peter Eastman's avatar
Peter Eastman committed
195
196
                q[k] = v[k]*(sinwt/wk) + q[k]*coswt; // Advance position from t to t+dt
                v[k] = vprime;
197
            }
Peter Eastman's avatar
Peter Eastman committed
198
199
            pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, q.data(), q.data(), 1.0, 0);
            pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, v.data(), v.data(), 1.0, 0);
200
            for (int k = 0; k < numCopies; k++) {
Peter Eastman's avatar
Peter Eastman committed
201
202
                positions[k][particle][component] = scale*q[k].real();
                velocities[k][particle][component] = scale*v[k].real();
203
204
205
            }
        }
    }
206
207
208
    
    // Calculate forces based on the updated positions.
    
209
    computeForces(context, integrator);
210

211
212
213
214
    // Update velocities.
    
    for (int i = 0; i < numCopies; i++)
        for (int j = 0; j < numParticles; j++)
Peter Eastman's avatar
Peter Eastman committed
215
216
            if (system.getParticleMass(j) != 0.0)
                velocities[i][j] += forces[i][j]*(halfdt/system.getParticleMass(j));
217
218

    // Apply the PILE-L thermostat again.
219
    
220
221
222
223
    if (integrator.getApplyThermostat()) {
        for (int particle = 0; particle < numParticles; particle++) {
            if (system.getParticleMass(particle) == 0.0)
                continue;
peastman's avatar
peastman committed
224
            const double c3_0 = c2_0*sqrt(nkT/system.getParticleMass(particle));
225
226
            for (int component = 0; component < 3; component++) {
                for (int k = 0; k < numCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
227
228
                    v[k] = complex<double>(scale*velocities[k][particle][component], 0.0);
                pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, v.data(), v.data(), 1.0, 0);
229

230
                // Apply a local Langevin thermostat to the centroid mode.
231

Peter Eastman's avatar
Peter Eastman committed
232
                v[0].real(v[0].real()*c1_0 + c3_0*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
233
234
235
236
237

                // Use critical damping white noise for the remaining modes.

                for (int k = 1; k <= numCopies/2; k++) {
                    const bool isCenter = (numCopies%2 == 0 && k == numCopies/2);
peastman's avatar
peastman committed
238
239
240
241
242
243
                    const double wk = twown*sin(k*M_PI/numCopies);
                    const double c1 = exp(-2.0*wk*halfdt);
                    const double c2 = sqrt((1.0-c1*c1)/2) * (isCenter ? sqrt(2.0) : 1.0);
                    const double c3 = c2*sqrt(nkT/system.getParticleMass(particle));
                    double rand1 = c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                    double rand2 = (isCenter ? 0.0 : c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
Peter Eastman's avatar
Peter Eastman committed
244
                    v[k] = v[k]*c1 + complex<double>(rand1, rand2);
245
                    if (k < numCopies-k)
Peter Eastman's avatar
Peter Eastman committed
246
                        v[numCopies-k] = v[numCopies-k]*c1 + complex<double>(rand1, -rand2);
247
                }
Peter Eastman's avatar
Peter Eastman committed
248
                pocketfft::c2c({(size_t) numCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, v.data(), v.data(), 1.0, 0);
249
                for (int k = 0; k < numCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
250
                    velocities[k][particle][component] = scale*v[k].real();
251
252
253
            }
        }
    }
254
255
256
257
    
    // Update the time.
    
    context.setTime(context.getTime()+dt);
258
259
}

260
261
262
void ReferenceIntegrateRPMDStepKernel::computeForces(ContextImpl& context, const RPMDIntegrator& integrator) {
    const int totalCopies = positions.size();
    const int numParticles = positions[0].size();
peastman's avatar
peastman committed
263
264
265
    vector<Vec3>& pos = extractPositions(context);
    vector<Vec3>& vel = extractVelocities(context);
    vector<Vec3>& f = extractForces(context);
266
267
268
269
270
271
272
    
    // Compute forces from all groups that didn't have a specified contraction.
    
    for (int i = 0; i < totalCopies; i++) {
        pos = positions[i];
        vel = velocities[i];
        context.computeVirtualSites();
273
274
        Vec3 initialBox[3];
        context.getPeriodicBoxVectors(initialBox[0], initialBox[1], initialBox[2]);
275
        context.updateContextState();
276
277
        Vec3 finalBox[3];
        context.getPeriodicBoxVectors(finalBox[0], finalBox[1], finalBox[2]);
278
279
        if (initialBox[0] != finalBox[0] || initialBox[1] != finalBox[1] || initialBox[2] != finalBox[2])
            throw OpenMMException("Standard barostats cannot be used with RPMDIntegrator.  Use RPMDMonteCarloBarostat instead.");
280
281
282
283
284
285
286
287
        positions[i] = pos;
        velocities[i] = vel;
        context.calcForcesAndEnergy(true, false, groupsNotContracted);
        forces[i] = f;
    }
    
    // Now loop over contractions and compute forces from them.
    
peastman's avatar
peastman committed
288
289
290
    for (auto& g : groupsByCopies) {
        int copies = g.first;
        int groupFlags = g.second;
291
292
293
        
        // Find the contracted positions.
        
Peter Eastman's avatar
Peter Eastman committed
294
        vector<complex<double>> q(totalCopies);
peastman's avatar
peastman committed
295
        const double scale1 = 1.0/totalCopies;
296
297
298
299
300
        for (int particle = 0; particle < numParticles; particle++) {
            for (int component = 0; component < 3; component++) {
                // Transform to the frequency domain, set high frequency components to zero, and transform back.
                
                for (int k = 0; k < totalCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
301
302
                    q[k] = complex<double>(positions[k][particle][component], 0.0);
                pocketfft::c2c({(size_t) totalCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, q.data(), q.data(), 1.0, 0);
303
304
305
306
307
                if (copies > 1) {
                    int start = (copies+1)/2;
                    int end = totalCopies-copies+start;
                    for (int k = end; k < totalCopies; k++)
                        q[k-(totalCopies-copies)] = q[k];
Peter Eastman's avatar
Peter Eastman committed
308
                    pocketfft::c2c({(size_t) copies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, q.data(), q.data(), 1.0, 0);
309
310
                }
                for (int k = 0; k < copies; k++)
Peter Eastman's avatar
Peter Eastman committed
311
                    contractedPositions[k][particle][component] = scale1*q[k].real();
312
313
314
315
316
317
318
319
320
321
322
323
324
325
            }
        }
        
        // Compute forces.

        for (int i = 0; i < copies; i++) {
            pos = contractedPositions[i];
            context.computeVirtualSites();
            context.calcForcesAndEnergy(true, false, groupFlags);
            contractedForces[i] = f;
        }
        
        // Apply the forces to the original copies.
        
peastman's avatar
peastman committed
326
        const double scale2 = 1.0/copies;
327
328
329
330
331
        for (int particle = 0; particle < numParticles; particle++) {
            for (int component = 0; component < 3; component++) {
                // Transform to the frequency domain, pad with zeros, and transform back.
                
                for (int k = 0; k < copies; k++)
Peter Eastman's avatar
Peter Eastman committed
332
                    q[k] = complex<double>(contractedForces[k][particle][component], 0.0);
333
                if (copies > 1)
Peter Eastman's avatar
Peter Eastman committed
334
                    pocketfft::c2c({(size_t) copies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, true, q.data(), q.data(), 1.0, 0);
335
336
337
338
339
                int start = (copies+1)/2;
                int end = totalCopies-copies+start;
                for (int k = end; k < totalCopies; k++)
                    q[k] = q[k-(totalCopies-copies)];
                for (int k = start; k < end; k++)
Peter Eastman's avatar
Peter Eastman committed
340
341
                    q[k] = complex<double>(0, 0);
                pocketfft::c2c({(size_t) totalCopies}, {sizeof(complex<double>)}, {sizeof(complex<double>)}, {0}, false, q.data(), q.data(), 1.0, 0);
342
                for (int k = 0; k < totalCopies; k++)
Peter Eastman's avatar
Peter Eastman committed
343
                    forces[k][particle][component] += scale2*q[k].real();
344
345
346
347
348
            }
        }
    }
}

349
double ReferenceIntegrateRPMDStepKernel::computeKineticEnergy(ContextImpl& context, const RPMDIntegrator& integrator) {
350
    const System& system = context.getSystem();
351
    int numParticles = system.getNumParticles();
peastman's avatar
peastman committed
352
    vector<Vec3>& velData = extractVelocities(context);
353
354
355
356
    double energy = 0.0;
    for (int i = 0; i < numParticles; ++i) {
        double mass = system.getParticleMass(i);
        if (mass > 0) {
peastman's avatar
peastman committed
357
            Vec3 v = velData[i];
358
359
360
361
362
363
            energy += mass*(v.dot(v));
        }
    }
    return 0.5*energy;
}

364
365
366
367
368
369
370
371
372
373
374
375
void ReferenceIntegrateRPMDStepKernel::setPositions(int copy, const vector<Vec3>& pos) {
    int numParticles = positions[copy].size();
    for (int i = 0; i < numParticles; i++)
        positions[copy][i] = pos[i];
}

void ReferenceIntegrateRPMDStepKernel::setVelocities(int copy, const vector<Vec3>& vel) {
    int numParticles = velocities[copy].size();
    for (int i = 0; i < numParticles; i++)
        velocities[copy][i] = vel[i];
}

376
void ReferenceIntegrateRPMDStepKernel::copyToContext(int copy, ContextImpl& context) {
377
378
379
    extractPositions(context) = positions[copy];
    extractVelocities(context) = velocities[copy];
}