CpuCustomNonbondedForce.cpp 13 KB
Newer Older
1

peastman's avatar
peastman committed
2
/* Portions copyright (c) 2009-2018 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
 * Contributors: Peter Eastman
 *
 * 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 <string.h>
#include <sstream>

#include "SimTKOpenMMUtilities.h"
#include "ReferenceForce.h"
#include "CpuCustomNonbondedForce.h"

using namespace OpenMM;
using namespace std;

35
36
37
CpuCustomNonbondedForce::ThreadData::ThreadData(const Lepton::CompiledExpression& energyExpression, const Lepton::CompiledExpression& forceExpression,
            const vector<string>& parameterNames, const std::vector<Lepton::CompiledExpression> energyParamDerivExpressions) :
            energyExpression(energyExpression), forceExpression(forceExpression), energyParamDerivExpressions(energyParamDerivExpressions) {
38
39
40
    map<string, double*> variableLocations;
    variableLocations["r"] = &r;
    particleParam.resize(2*parameterNames.size());
41
    for (int i = 0; i < (int) parameterNames.size(); i++) {
42
        for (int j = 0; j < 2; j++) {
43
            stringstream name;
44
45
            name << parameterNames[i] << (j+1);
            variableLocations[name.str()] = &particleParam[i*2+j];
46
47
        }
    }
48
    energyParamDerivs.resize(energyParamDerivExpressions.size());
49
50
51
52
    this->energyExpression.setVariableLocations(variableLocations);
    this->forceExpression.setVariableLocations(variableLocations);
    expressionSet.registerExpression(this->energyExpression);
    expressionSet.registerExpression(this->forceExpression);
peastman's avatar
peastman committed
53
54
55
    for (auto& expression : this->energyParamDerivExpressions) {
        expression.setVariableLocations(variableLocations);
        expressionSet.registerExpression(expression);
56
    }
57
58
}

59
CpuCustomNonbondedForce::CpuCustomNonbondedForce(const Lepton::CompiledExpression& energyExpression,
60
61
            const Lepton::CompiledExpression& forceExpression, const vector<string>& parameterNames, const vector<set<int> >& exclusions,
            const std::vector<Lepton::CompiledExpression> energyParamDerivExpressions, ThreadPool& threads) :
62
            cutoff(false), useSwitch(false), periodic(false), useInteractionGroups(false), paramNames(parameterNames), exclusions(exclusions), threads(threads) {
63
    for (int i = 0; i < threads.getNumThreads(); i++)
64
        threadData.push_back(new ThreadData(energyExpression, forceExpression, parameterNames, energyParamDerivExpressions));
65
66
}

67
CpuCustomNonbondedForce::~CpuCustomNonbondedForce() {
peastman's avatar
peastman committed
68
69
    for (auto data : threadData)
        delete data;
70
}
71

peastman's avatar
peastman committed
72
void CpuCustomNonbondedForce::setUseCutoff(double distance, const CpuNeighborList& neighbors) {
73
74
75
76
77
78
    cutoff = true;
    cutoffDistance = distance;
    neighborList = &neighbors;
  }

void CpuCustomNonbondedForce::setInteractionGroups(const vector<pair<set<int>, set<int> > >& groups) {
79
    useInteractionGroups = true;
peastman's avatar
peastman committed
80
81
82
    for (auto& group : groups) {
        const set<int>& set1 = group.first;
        const set<int>& set2 = group.second;
83
84
85
86
87
88
89
90
91
92
        for (set<int>::const_iterator atom1 = set1.begin(); atom1 != set1.end(); ++atom1) {
            for (set<int>::const_iterator atom2 = set2.begin(); atom2 != set2.end(); ++atom2) {
                if (*atom1 == *atom2 || exclusions[*atom1].find(*atom2) != exclusions[*atom1].end())
                    continue; // This is an excluded interaction.
                if (*atom1 > *atom2 && set1.find(*atom2) != set1.end() && set2.find(*atom1) != set2.end())
                    continue; // Both atoms are in both sets, so skip duplicate interactions.
                groupInteractions.push_back(make_pair(*atom1, *atom2));
            }
        }
    }
93
94
}

peastman's avatar
peastman committed
95
void CpuCustomNonbondedForce::setUseSwitchingFunction(double distance) {
96
97
98
99
    useSwitch = true;
    switchingDistance = distance;
}

peastman's avatar
peastman committed
100
void CpuCustomNonbondedForce::setPeriodic(Vec3* periodicBoxVectors) {
101
    assert(cutoff);
102
103
104
    assert(periodicBoxVectors[0][0] >= 2.0*cutoffDistance);
    assert(periodicBoxVectors[1][1] >= 2.0*cutoffDistance);
    assert(periodicBoxVectors[2][2] >= 2.0*cutoffDistance);
105
    periodic = true;
106
107
108
109
110
111
112
113
114
115
116
117
118
    this->periodicBoxVectors[0] = periodicBoxVectors[0];
    this->periodicBoxVectors[1] = periodicBoxVectors[1];
    this->periodicBoxVectors[2] = periodicBoxVectors[2];
    recipBoxSize[0] = (float) (1.0/periodicBoxVectors[0][0]);
    recipBoxSize[1] = (float) (1.0/periodicBoxVectors[1][1]);
    recipBoxSize[2] = (float) (1.0/periodicBoxVectors[2][2]);
    periodicBoxVec4.resize(3);
    periodicBoxVec4[0] = fvec4(periodicBoxVectors[0][0], periodicBoxVectors[0][1], periodicBoxVectors[0][2], 0);
    periodicBoxVec4[1] = fvec4(periodicBoxVectors[1][0], periodicBoxVectors[1][1], periodicBoxVectors[1][2], 0);
    periodicBoxVec4[2] = fvec4(periodicBoxVectors[2][0], periodicBoxVectors[2][1], periodicBoxVectors[2][2], 0);
    triclinic = (periodicBoxVectors[0][1] != 0.0 || periodicBoxVectors[0][2] != 0.0 ||
                 periodicBoxVectors[1][0] != 0.0 || periodicBoxVectors[1][2] != 0.0 ||
                 periodicBoxVectors[2][0] != 0.0 || periodicBoxVectors[2][1] != 0.0);
119
}
120
121


122
123
124
void CpuCustomNonbondedForce::calculatePairIxn(int numberOfAtoms, float* posq, vector<Vec3>& atomCoordinates, vector<vector<double> >& atomParameters,
                                               const map<string, double>& globalParameters, vector<AlignedArray<float> >& threadForce,
                                               bool includeForce, bool includeEnergy, double& totalEnergy, double* energyParamDerivs) {
125
126
127
128
129
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->posq = posq;
    this->atomCoordinates = &atomCoordinates[0];
130
    this->atomParameters = &atomParameters[0];
131
    this->globalParameters = &globalParameters;
132
    this->threadForce = &threadForce;
133
134
    this->includeForce = includeForce;
    this->includeEnergy = includeEnergy;
135
    threadEnergy.resize(threads.getNumThreads());
peastman's avatar
peastman committed
136
    atomicCounter = 0;
137
    
138
    // Signal the threads to start running and wait for them to finish.
139
    
peastman's avatar
peastman committed
140
    threads.execute([&] (ThreadPool& threads, int threadIndex) { threadComputeForce(threads, threadIndex); });
141
    threads.waitForThreads();
142
    
143
144
    // Combine the energies from all the threads.
    
145
    int numThreads = threads.getNumThreads();
146
147
148
149
    if (includeEnergy) {
        for (int i = 0; i < numThreads; i++)
            totalEnergy += threadEnergy[i];
    }
150
151
152
153
154
155
156

    // Combine the energy derivatives from all threads.
    
    int numDerivs = threadData[0]->energyParamDerivs.size();
    for (int i = 0; i < numThreads; i++)
        for (int j = 0; j < numDerivs; j++)
            energyParamDerivs[j] += threadData[i]->energyParamDerivs[j];
157
158
159
160
161
162
163
164
165
166
}

void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int threadIndex) {
    // Compute this thread's subset of interactions.

    int numThreads = threads.getNumThreads();
    threadEnergy[threadIndex] = 0;
    double& energy = threadEnergy[threadIndex];
    float* forces = &(*threadForce)[threadIndex][0];
    ThreadData& data = *threadData[threadIndex];
peastman's avatar
peastman committed
167
168
169
170
    for (auto& param : *globalParameters)
        data.expressionSet.setVariable(data.expressionSet.getVariableIndex(param.first), param.second);
    for (auto& deriv : data.energyParamDerivs)
        deriv = 0.0;
171
172
    fvec4 boxSize(periodicBoxVectors[0][0], periodicBoxVectors[1][1], periodicBoxVectors[2][2], 0);
    fvec4 invBoxSize(recipBoxSize[0], recipBoxSize[1], recipBoxSize[2], 0);
173
    if (useInteractionGroups) {
174
175
        // The user has specified interaction groups, so compute only the requested interactions.
        
176
177
178
        int start = threadIndex*groupInteractions.size()/numThreads;
        int end = (threadIndex+1)*groupInteractions.size()/numThreads;
        for (int i = start; i < end; i++) {
179
180
181
            int atom1 = groupInteractions[i].first;
            int atom2 = groupInteractions[i].second;
            for (int j = 0; j < (int) paramNames.size(); j++) {
182
183
                data.particleParam[j*2] = atomParameters[atom1][j];
                data.particleParam[j*2+1] = atomParameters[atom2][j];
184
            }
185
            calculateOneIxn(atom1, atom2, data, forces, energy, boxSize, invBoxSize);
186
187
188
189
        }
    }
    else if (cutoff) {
        // We are using a cutoff, so get the interactions from the neighbor list.
190
191

        while (true) {
peastman's avatar
peastman committed
192
            int blockIndex = atomicCounter++;
193
194
            if (blockIndex >= neighborList->getNumBlocks())
                break;
195
            const int blockSize = neighborList->getBlockSize();
Daniel Towner's avatar
Daniel Towner committed
196
            const int32_t* blockAtom = &neighborList->getSortedAtoms()[blockSize*blockIndex];
197
            const vector<int>& neighbors = neighborList->getBlockNeighbors(blockIndex);
198
            const auto& exclusions = neighborList->getBlockExclusions(blockIndex);
199
200
            for (int i = 0; i < (int) neighbors.size(); i++) {
                int first = neighbors[i];
201
                for (int j = 0; j < (int) paramNames.size(); j++)
202
                    data.particleParam[j*2] = atomParameters[first][j];
203
                for (int k = 0; k < blockSize; k++) {
204
205
                    if ((exclusions[i] & (1<<k)) == 0) {
                        int second = blockAtom[k];
206
                        for (int j = 0; j < (int) paramNames.size(); j++)
207
                            data.particleParam[j*2+1] = atomParameters[second][j];
208
                        calculateOneIxn(first, second, data, forces, energy, boxSize, invBoxSize);
209
210
211
212
213
214
215
216
                    }
                }
            }
        }
    }
    else {
        // Every particle interacts with every other one.
        
217
        while (true) {
peastman's avatar
peastman committed
218
            int ii = atomicCounter++;
219
220
            if (ii >= numberOfAtoms)
                break;
221
222
223
            for (int jj = ii+1; jj < numberOfAtoms; jj++) {
                if (exclusions[jj].find(ii) == exclusions[jj].end()) {
                    for (int j = 0; j < (int) paramNames.size(); j++) {
224
225
                        data.particleParam[j*2] = atomParameters[ii][j];
                        data.particleParam[j*2+1] = atomParameters[jj][j];
226
                    }
227
                    calculateOneIxn(ii, jj, data, forces, energy, boxSize, invBoxSize);
228
229
230
231
232
233
                }
            }
        }
    }
}

234
235
236
void CpuCustomNonbondedForce::calculateOneIxn(int ii, int jj, ThreadData& data, 
        float* forces, double& totalEnergy, const fvec4& boxSize, const fvec4& invBoxSize) {
    // Get deltaR, R2, and R between 2 atoms
237
238
239
240
241
242
243
244
245

    fvec4 deltaR;
    fvec4 posI(posq+4*ii);
    fvec4 posJ(posq+4*jj);
    float r2;
    getDeltaR(posI, posJ, deltaR, r2, boxSize, invBoxSize);
    if (cutoff && r2 >= cutoffDistance*cutoffDistance)
        return;
    float r = sqrtf(r2);
246
    data.r = r;
247
248
249

    // accumulate forces

250
    double dEdR = (includeForce ? data.forceExpression.evaluate()/r : 0.0);
251
252
253
    double energy = 0.0;
    if (includeEnergy || (useSwitch && r > switchingDistance))
        energy = data.energyExpression.evaluate();
254
    double switchValue = 1.0;
255
256
    if (useSwitch) {
        if (r > switchingDistance) {
257
258
259
            double t = (r-switchingDistance)/(cutoffDistance-switchingDistance);
            switchValue = 1+t*t*t*(-10+t*(15-t*6));
            double switchDeriv = t*t*(-30+t*(60-t*30))/(cutoffDistance-switchingDistance);
260
261
262
263
264
265
266
267
268
269
            dEdR = switchValue*dEdR + energy*switchDeriv/r;
            energy *= switchValue;
        }
    }
    fvec4 result = deltaR*dEdR;
    (fvec4(forces+4*ii)+result).store(forces+4*ii);
    (fvec4(forces+4*jj)-result).store(forces+4*jj);

    // accumulate energies

270
    totalEnergy += energy;
271
272
273
274
275
    
    // Accumulate energy derivatives.

    for (int i = 0; i < data.energyParamDerivExpressions.size(); i++)
        data.energyParamDerivs[i] += switchValue*data.energyParamDerivExpressions[i].evaluate();
276
277
278
279
280
}

void CpuCustomNonbondedForce::getDeltaR(const fvec4& posI, const fvec4& posJ, fvec4& deltaR, float& r2, const fvec4& boxSize, const fvec4& invBoxSize) const {
    deltaR = posJ-posI;
    if (periodic) {
281
282
283
284
285
286
287
288
289
        if (triclinic) {
            deltaR -= periodicBoxVec4[2]*floorf(deltaR[2]*recipBoxSize[2]+0.5f);
            deltaR -= periodicBoxVec4[1]*floorf(deltaR[1]*recipBoxSize[1]+0.5f);
            deltaR -= periodicBoxVec4[0]*floorf(deltaR[0]*recipBoxSize[0]+0.5f);
        }
        else {
            fvec4 base = round(deltaR*invBoxSize)*boxSize;
            deltaR = deltaR-base;
        }
290
291
292
    }
    r2 = dot3(deltaR, deltaR);
}