CpuLangevinDynamics.cpp 6.15 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
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
 * 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 "SimTKOpenMMUtilities.h"
#include "CpuLangevinDynamics.h"

using namespace OpenMM;
using namespace std;

class CpuLangevinDynamics::Update1Task : public ThreadPool::Task {
public:
    Update1Task(CpuLangevinDynamics& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadUpdate1(threadIndex);
    }
    CpuLangevinDynamics& owner;
};

class CpuLangevinDynamics::Update2Task : public ThreadPool::Task {
public:
    Update2Task(CpuLangevinDynamics& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadUpdate2(threadIndex);
    }
    CpuLangevinDynamics& owner;
};

peastman's avatar
peastman committed
52
53
54
55
56
57
58
59
60
61
class CpuLangevinDynamics::Update3Task : public ThreadPool::Task {
public:
    Update3Task(CpuLangevinDynamics& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadUpdate3(threadIndex);
    }
    CpuLangevinDynamics& owner;
};

peastman's avatar
peastman committed
62
CpuLangevinDynamics::CpuLangevinDynamics(int numberOfAtoms, double deltaT, double friction, double temperature, ThreadPool& threads, CpuRandom& random) : 
63
           ReferenceStochasticDynamics(numberOfAtoms, deltaT, friction, temperature), threads(threads), random(random) {
64
65
66
67
68
}

CpuLangevinDynamics::~CpuLangevinDynamics() {
}

peastman's avatar
peastman committed
69
70
void CpuLangevinDynamics::updatePart1(int numberOfAtoms, vector<Vec3>& atomCoordinates, vector<Vec3>& velocities,
                                      vector<Vec3>& forces, vector<double>& inverseMasses, vector<Vec3>& xPrime) {
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->atomCoordinates = &atomCoordinates[0];
    this->velocities = &velocities[0];
    this->forces = &forces[0];
    this->inverseMasses = &inverseMasses[0];
    this->xPrime = &xPrime[0];
    
    // Signal the threads to start running and wait for them to finish.
    
    Update1Task task(*this);
    threads.execute(task);
    threads.waitForThreads();
}

peastman's avatar
peastman committed
87
88
void CpuLangevinDynamics::updatePart2(int numberOfAtoms, vector<Vec3>& atomCoordinates, vector<Vec3>& velocities,
                                      vector<Vec3>& forces, vector<double>& inverseMasses, vector<Vec3>& xPrime) {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->atomCoordinates = &atomCoordinates[0];
    this->velocities = &velocities[0];
    this->forces = &forces[0];
    this->inverseMasses = &inverseMasses[0];
    this->xPrime = &xPrime[0];
    
    // Signal the threads to start running and wait for them to finish.
    
    Update2Task task(*this);
    threads.execute(task);
    threads.waitForThreads();
}

peastman's avatar
peastman committed
105
106
void CpuLangevinDynamics::updatePart3(int numberOfAtoms, vector<Vec3>& atomCoordinates, vector<Vec3>& velocities,
                                       vector<double>& inverseMasses, vector<Vec3>& xPrime) {
peastman's avatar
peastman committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->atomCoordinates = &atomCoordinates[0];
    this->velocities = &velocities[0];
    this->inverseMasses = &inverseMasses[0];
    this->xPrime = &xPrime[0];
    
    // Signal the threads to start running and wait for them to finish.
    
    Update3Task task(*this);
    threads.execute(task);
    threads.waitForThreads();
}

122
void CpuLangevinDynamics::threadUpdate1(int threadIndex) {
peastman's avatar
peastman committed
123
124
125
126
127
128
    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));
129
130
131
132
133
    int start = threadIndex*numberOfAtoms/threads.getNumThreads();
    int end = (threadIndex+1)*numberOfAtoms/threads.getNumThreads();

    for (int i = start; i < end; i++) {
        if (inverseMasses[i] != 0.0) {
peastman's avatar
peastman committed
134
135
            double sqrtInvMass = sqrt(inverseMasses[i]);
            Vec3 noise(random.getGaussianRandom(threadIndex), random.getGaussianRandom(threadIndex), random.getGaussianRandom(threadIndex));
136
137
138
139
140
141
            velocities[i]  = velocities[i]*vscale + forces[i]*(fscale*inverseMasses[i]) + noise*(noisescale*sqrtInvMass);
        }
   }
}

void CpuLangevinDynamics::threadUpdate2(int threadIndex) {
peastman's avatar
peastman committed
142
    const double dt = getDeltaT();
143
144
145
146
147
    int start = threadIndex*numberOfAtoms/threads.getNumThreads();
    int end = (threadIndex+1)*numberOfAtoms/threads.getNumThreads();

    for (int i = start; i < end; i++) {
        if (inverseMasses[i] != 0.0) {
peastman's avatar
peastman committed
148
            double sqrtInvMass = sqrt(inverseMasses[i]);
149
150
151
152
            xPrime[i] = atomCoordinates[i]+velocities[i]*dt;
        }
   }
}
peastman's avatar
peastman committed
153
154

void CpuLangevinDynamics::threadUpdate3(int threadIndex) {
peastman's avatar
peastman committed
155
   const double invStepSize = 1.0/getDeltaT();
peastman's avatar
peastman committed
156
157
158
159
160
161
162
163
164
165
    int start = threadIndex*numberOfAtoms/threads.getNumThreads();
    int end = (threadIndex+1)*numberOfAtoms/threads.getNumThreads();

   for (int i = start; i < end; ++i)
       if (inverseMasses[i] != 0.0) {
            velocities[i] = (xPrime[i]-atomCoordinates[i])*invStepSize;
            atomCoordinates[i] = xPrime[i];
       }
}