ReferenceRpmdKernels.cpp 17.7 KB
Newer Older
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* -------------------------------------------------------------------------- *
 *                                   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) 2011 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.                                     *
 * -------------------------------------------------------------------------- */

#include "ReferenceRpmdKernels.h"
#include "openmm/internal/ContextImpl.h"
#include "SimTKUtilities/SimTKOpenMMUtilities.h"

using namespace OpenMM;
using namespace std;

static vector<RealVec>& extractPositions(ContextImpl& context) {
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    return *((vector<RealVec>*) data->positions);
}

static vector<RealVec>& extractVelocities(ContextImpl& context) {
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    return *((vector<RealVec>*) data->velocities);
}

static vector<RealVec>& extractForces(ContextImpl& context) {
    ReferencePlatform::PlatformData* data = reinterpret_cast<ReferencePlatform::PlatformData*>(context.getPlatformData());
    return *((vector<RealVec>*) data->forces);
}

ReferenceIntegrateRPMDStepKernel::~ReferenceIntegrateRPMDStepKernel() {
    if (fft != NULL)
        fftpack_destroy(fft);
}
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);
    }
69
    fftpack_init_1d(&fft, numCopies);
70
71
72
    SimTKOpenMMUtilities::setRandomNumberSeed((unsigned int) integrator.getRandomNumberSeed());
}

73
74
75
76
77
78
void ReferenceIntegrateRPMDStepKernel::execute(ContextImpl& context, const RPMDIntegrator& integrator, bool forcesAreValid) {
    const int numCopies = positions.size();
    const int numParticles = positions[0].size();
    const RealOpenMM dt = integrator.getStepSize();
    const RealOpenMM halfdt = 0.5*dt;
    const System& system = context.getSystem();
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    vector<RealVec>& pos = extractPositions(context);
    vector<RealVec>& vel = extractVelocities(context);
    vector<RealVec>& f = extractForces(context);
    if (!hasSetPosition) {
        // Initialize the positions from the context.
        
        for (int i = 0; i < numCopies; i++)
            for (int j = 0; j < numParticles; j++)
                positions[i][j] = pos[j];
        hasSetPosition = true;
    }
    if (!hasSetVelocity) {
        // Initialize the velocities from the context.
        
        for (int i = 0; i < numCopies; i++)
            for (int j = 0; j < numParticles; j++)
                velocities[i][j] = vel[j];
        hasSetVelocity = true;
    }
    
    // Loop over copies and compute the force on each one.
    
101
102
103
104
105
106
107
    if (!forcesAreValid) {
        for (int i = 0; i < numCopies; i++) {
            pos = positions[i];
            vel = velocities[i];
            context.calcForcesAndEnergy(true, false);
            forces[i] = f;
        }
108
    }
109
110

    // Apply the PILE-L thermostat.
111
    
112
113
114
115
116
117
118
119
120
121
122
123
124
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
    vector<t_complex> p(numCopies);
    vector<t_complex> q(numCopies);
    const RealOpenMM hbar = 1.054571628e-34*AVOGADRO/(1000*1e-12);
    const RealOpenMM scale = 1.0/sqrt(numCopies);
    const RealOpenMM nkT = numCopies*BOLTZ*integrator.getTemperature();
    const RealOpenMM twown = 2.0*nkT/hbar;
    const RealOpenMM c1_0 = exp(-halfdt*integrator.getFriction());
    const RealOpenMM c2_0 = sqrt(1.0-c1_0*c1_0);
    for (int particle = 0; particle < numParticles; particle++) {
        const RealOpenMM c3_0 = c2_0*sqrt(system.getParticleMass(particle)*nkT);
        for (int component = 0; component < 3; component++) {
            for (int k = 0; k < numCopies; k++)
                p[k] = t_complex(scale*velocities[k][particle][component]*system.getParticleMass(particle), 0.0);
            fftpack_exec_1d(fft, FFTPACK_FORWARD, &p[0], &p[0]);
            
            // Apply a local Langevin thermostat to the centroid mode.

            p[0].re = p[0].re*c1_0 + c3_0*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();

            // 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);
                const RealOpenMM wk = twown*sin(k*M_PI/numCopies);
                const RealOpenMM c1 = exp(-2.0*wk*halfdt);
                const RealOpenMM c2 = sqrt((1.0-c1*c1)/2) * (isCenter ? sqrt(2.0) : 1.0);
                const RealOpenMM c3 = c2*sqrt(system.getParticleMass(particle)*nkT);
                RealOpenMM rand1 = c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                RealOpenMM rand2 = (isCenter ? 0.0 : c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
                p[k] = p[k]*c1 + t_complex(rand1, rand2);
                if (k < numCopies-k)
                    p[numCopies-k] = p[numCopies-k]*c1 + t_complex(rand1, -rand2);                
            }
            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &p[0], &p[0]);
            for (int k = 0; k < numCopies; k++)
                velocities[k][particle][component] = scale*p[k].re/system.getParticleMass(particle);
        }
    }

151
152
153
154
    // Update velocities.
    
    for (int i = 0; i < numCopies; i++)
        for (int j = 0; j < numParticles; j++)
155
            velocities[i][j] += forces[i][j]*(halfdt/system.getParticleMass(j));
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    
    // Evolve the free ring polymer by transforming to the frequency domain.

    for (int particle = 0; particle < numParticles; particle++) {
        for (int component = 0; component < 3; component++) {
            for (int k = 0; k < numCopies; k++) {
                q[k] = t_complex(scale*positions[k][particle][component], 0.0);
                p[k] = t_complex(scale*velocities[k][particle][component]*system.getParticleMass(particle), 0.0);
            }
            fftpack_exec_1d(fft, FFTPACK_FORWARD, &q[0], &q[0]);
            fftpack_exec_1d(fft, FFTPACK_FORWARD, &p[0], &p[0]);
            q[0] += p[0]*(dt/system.getParticleMass(particle));
            for (int k = 1; k < numCopies; k++) {
                const RealOpenMM wk = twown*sin(k*M_PI/numCopies);
                const RealOpenMM wt = wk*dt;
171
172
                const RealOpenMM coswt = cos(wt);
                const RealOpenMM sinwt = sin(wt);
173
                const RealOpenMM wm = wk*system.getParticleMass(particle);
174
175
                const t_complex pprime = p[k]*coswt - q[k]*(wm*sinwt); // Advance momentum from t to t+dt
                q[k] = p[k]*(sinwt/wm) + q[k]*coswt; // Advance position from t to t+dt
176
177
178
179
180
181
182
183
184
185
                p[k] = pprime;
            }
            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &q[0], &q[0]);
            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &p[0], &p[0]);
            for (int k = 0; k < numCopies; k++) {
                positions[k][particle][component] = scale*q[k].re;
                velocities[k][particle][component] = scale*p[k].re/system.getParticleMass(particle);
            }
        }
    }
186
187
188
189
190
191
192
193
194
195
196
197
    
    // Calculate forces based on the updated positions.
    
    for (int i = 0; i < numCopies; i++) {
        pos = positions[i];
        vel = velocities[i];
        context.updateContextState();
        positions[i] = pos;
        velocities[i] = vel;
        context.calcForcesAndEnergy(true, false);
        forces[i] = f;
    }
198

199
200
201
202
203
204
205
206
    // Update velocities.
    
    for (int i = 0; i < numCopies; i++)
        for (int j = 0; j < numParticles; j++)
            velocities[i][j] += forces[i][j]*(halfdt/system.getParticleMass(j));


    // Apply the PILE-L thermostat again.
207
208
    
    for (int particle = 0; particle < numParticles; particle++) {
209
        const RealOpenMM c3_0 = c2_0*sqrt(system.getParticleMass(particle)*nkT);
210
211
212
213
214
215
216
        for (int component = 0; component < 3; component++) {
            for (int k = 0; k < numCopies; k++)
                p[k] = t_complex(scale*velocities[k][particle][component]*system.getParticleMass(particle), 0.0);
            fftpack_exec_1d(fft, FFTPACK_FORWARD, &p[0], &p[0]);
            
            // Apply a local Langevin thermostat to the centroid mode.

217
            p[0].re = p[0].re*c1_0 + c3_0*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
218
219
220

            // Use critical damping white noise for the remaining modes.
            
221
            for (int k = 1; k <= numCopies/2; k++) {
222
223
                const bool isCenter = (numCopies%2 == 0 && k == numCopies/2);
                const RealOpenMM wk = twown*sin(k*M_PI/numCopies);
224
                const RealOpenMM c1 = exp(-2.0*wk*halfdt);
225
226
227
228
229
230
231
232
233
234
235
236
237
                const RealOpenMM c2 = sqrt((1.0-c1*c1)/2) * (isCenter ? sqrt(2.0) : 1.0);
                const RealOpenMM c3 = c2*sqrt(system.getParticleMass(particle)*nkT);
                RealOpenMM rand1 = c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                RealOpenMM rand2 = (isCenter ? 0.0 : c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
                p[k] = p[k]*c1 + t_complex(rand1, rand2);
                if (k < numCopies-k)
                    p[numCopies-k] = p[numCopies-k]*c1 + t_complex(rand1, -rand2);                
            }
            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &p[0], &p[0]);
            for (int k = 0; k < numCopies; k++)
                velocities[k][particle][component] = scale*p[k].re/system.getParticleMass(particle);
        }
    }
238
239
240
241
    
    // Update the time.
    
    context.setTime(context.getTime()+dt);
242
243
}

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//void ReferenceIntegrateRPMDStepKernel::execute(ContextImpl& context, const RPMDIntegrator& integrator, bool forcesAreValid) {
//    int numCopies = positions.size();
//    int numParticles = positions[0].size();
//    vector<RealVec>& pos = extractPositions(context);
//    vector<RealVec>& vel = extractVelocities(context);
//    vector<RealVec>& f = extractForces(context);
//    if (!hasSetPosition) {
//        // Initialize the positions from the context.
//        
//        for (int i = 0; i < numCopies; i++)
//            for (int j = 0; j < numParticles; j++)
//                positions[i][j] = pos[j];
//        hasSetPosition = true;
//    }
//    if (!hasSetVelocity) {
//        // Initialize the velocities from the context.
//        
//        for (int i = 0; i < numCopies; i++)
//            for (int j = 0; j < numParticles; j++)
//                velocities[i][j] = vel[j];
//        hasSetVelocity = true;
//    }
//    
//    // Loop over copies and compute the force on each one.
//    
//    for (int i = 0; i < numCopies; i++) {
//        pos = positions[i];
//        vel = velocities[i];
//        context.calcForcesAndEnergy(true, false);
//        forces[i] = f;
//    }
//
//    // Update velocities.
//    
//    const RealOpenMM dt = integrator.getStepSize();
//    const System& system = context.getSystem();
//    for (int i = 0; i < numCopies; i++)
//        for (int j = 0; j < numParticles; j++)
//            velocities[i][j] += forces[i][j]*(dt/system.getParticleMass(j));
//    
//    // Evolve the free ring polymer by transforming to the frequency domain.
//
//    vector<t_complex> p(numCopies);
//    vector<t_complex> q(numCopies);
//    const RealOpenMM scale = 1.0/sqrt(numCopies);
//    const RealOpenMM hbar = 1.054571628e-34*AVOGADRO/(1000*1e-12);
//    const RealOpenMM nkT = numCopies*BOLTZ*integrator.getTemperature();
//    const RealOpenMM twown = 2.0*nkT/hbar;
//    for (int particle = 0; particle < numParticles; particle++) {
//        for (int component = 0; component < 3; component++) {
//            for (int k = 0; k < numCopies; k++) {
//                q[k] = t_complex(scale*positions[k][particle][component], 0.0);
//                p[k] = t_complex(scale*velocities[k][particle][component]*system.getParticleMass(particle), 0.0);
//            }
//            fftpack_exec_1d(fft, FFTPACK_FORWARD, &q[0], &q[0]);
//            fftpack_exec_1d(fft, FFTPACK_FORWARD, &p[0], &p[0]);
//            q[0] += p[0]*(dt/system.getParticleMass(particle));
//            for (int k = 1; k < numCopies; k++) {
//                const RealOpenMM wk = twown*sin(k*M_PI/numCopies);
//                const RealOpenMM wt = wk*dt;
//                const RealOpenMM sinwt2 = sin(wt/2);
//                const RealOpenMM wm = wk*system.getParticleMass(particle);
//                const t_complex pprime = p[k] - q[k]*(2.0*wm*sinwt2); // Advance momentum from t-dt/2 to t+dt/2
//                q[k] = p[k]*(2.0*sinwt2/wm) + q[k]*(1.0-4.0*sinwt2*sinwt2); // Advance position from t to t+dt
//                p[k] = pprime;
//            }
//            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &q[0], &q[0]);
//            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &p[0], &p[0]);
//            for (int k = 0; k < numCopies; k++) {
//                positions[k][particle][component] = scale*q[k].re;
//                velocities[k][particle][component] = scale*p[k].re/system.getParticleMass(particle);
//            }
//        }
//    }
//
//    // Apply the PILE-L thermostat.
//    
//    const RealOpenMM c1_0 = exp(-dt*integrator.getFriction());
//    const RealOpenMM c2_0 = sqrt(1.0-c1_0*c1_0);
//    for (int particle = 0; particle < numParticles; particle++) {
//        const RealOpenMM c3_0 = c2_0*sqrt(system.getParticleMass(particle)*nkT);
//        for (int component = 0; component < 3; component++) {
//            for (int k = 0; k < numCopies; k++)
//                p[k] = t_complex(scale*velocities[k][particle][component]*system.getParticleMass(particle), 0.0);
//            fftpack_exec_1d(fft, FFTPACK_FORWARD, &p[0], &p[0]);
//            
//            // Apply a local Langevin thermostat to the centroid mode.
//
//            p[0].re = p[0].re*c1_0 + c3_0*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
//
//            // 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);
//                const RealOpenMM wk = twown*sin(k*M_PI/numCopies);
//                const RealOpenMM c1 = exp(-2.0*wk*dt);
//                const RealOpenMM c2 = sqrt((1.0-c1*c1)/2) * (isCenter ? sqrt(2.0) : 1.0);
//                const RealOpenMM c3 = c2*sqrt(system.getParticleMass(particle)*nkT);
//                RealOpenMM rand1 = c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
//                RealOpenMM rand2 = (isCenter ? 0.0 : c3*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
//                p[k] = p[k]*c1 + t_complex(rand1, rand2);
//                if (k < numCopies-k)
//                    p[numCopies-k] = p[numCopies-k]*c1 + t_complex(rand1, -rand2);                
//            }
//            fftpack_exec_1d(fft, FFTPACK_BACKWARD, &p[0], &p[0]);
//            for (int k = 0; k < numCopies; k++)
//                velocities[k][particle][component] = scale*p[k].re/system.getParticleMass(particle);
//        }
//    }
//}

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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];
    hasSetPosition = true;
}

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];
    hasSetVelocity = true;
}

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