CpuCustomGBForce.cpp 27.1 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

/* Portions copyright (c) 2009-2014 Stanford University and Simbios.
 * 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 "SimTKOpenMMCommon.h"
#include "SimTKOpenMMUtilities.h"
#include "ReferenceForce.h"
#include "CpuCustomGBForce.h"
peastman's avatar
peastman committed
32
#include "gmx_atomic.h"
33

34
35
using namespace OpenMM;
using namespace std;
36

peastman's avatar
peastman committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class CpuCustomGBForce::ComputeForceTask : public ThreadPool::Task {
public:
    ComputeForceTask(CpuCustomGBForce& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadComputeForce(threads, threadIndex);
    }
    CpuCustomGBForce& owner;
};

CpuCustomGBForce::ThreadData::ThreadData(int numAtoms, int numThreads, int threadIndex,
                      const vector<Lepton::CompiledExpression>& valueExpressions,
                      const vector<vector<Lepton::CompiledExpression> >& valueDerivExpressions,
                      const vector<vector<Lepton::CompiledExpression> >& valueGradientExpressions,
                      const vector<string>& valueNames,
                      const vector<Lepton::CompiledExpression>& energyExpressions,
                      const vector<vector<Lepton::CompiledExpression> >& energyDerivExpressions,
                      const vector<vector<Lepton::CompiledExpression> >& energyGradientExpressions,
                      const vector<string>& parameterNames) :
            valueExpressions(valueExpressions), valueDerivExpressions(valueDerivExpressions), valueGradientExpressions(valueGradientExpressions),
            energyExpressions(energyExpressions), energyDerivExpressions(energyDerivExpressions), energyGradientExpressions(energyGradientExpressions) {
    firstAtom = (threadIndex*(long long) numAtoms)/numThreads;
    lastAtom = ((threadIndex+1)*(long long) numAtoms)/numThreads;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    for (int i = 0; i < (int) valueExpressions.size(); i++)
        expressionSet.registerExpression(this->valueExpressions[i]);
    for (int i = 0; i < (int) valueDerivExpressions.size(); i++)
        for (int j = 0; j < (int) valueDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->valueDerivExpressions[i][j]);
    for (int i = 0; i < (int) valueGradientExpressions.size(); i++)
        for (int j = 0; j < (int) valueGradientExpressions[i].size(); j++)
            expressionSet.registerExpression(this->valueGradientExpressions[i][j]);
    for (int i = 0; i < (int) energyExpressions.size(); i++)
        expressionSet.registerExpression(this->energyExpressions[i]);
    for (int i = 0; i < (int) energyDerivExpressions.size(); i++)
        for (int j = 0; j < (int) energyDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->energyDerivExpressions[i][j]);
    for (int i = 0; i < (int) energyGradientExpressions.size(); i++)
        for (int j = 0; j < (int) energyGradientExpressions[i].size(); j++)
            expressionSet.registerExpression(this->energyGradientExpressions[i][j]);
    xindex = expressionSet.getVariableIndex("x");
    yindex = expressionSet.getVariableIndex("y");
    zindex = expressionSet.getVariableIndex("z");
    rindex = expressionSet.getVariableIndex("r");
peastman's avatar
peastman committed
80
81
    for (int i = 0; i < (int) parameterNames.size(); i++) {
        paramIndex.push_back(expressionSet.getVariableIndex(parameterNames[i]));
82
83
        for (int j = 1; j < 3; j++) {
            stringstream name;
peastman's avatar
peastman committed
84
            name << parameterNames[i] << j;
85
86
87
88
89
90
91
92
93
94
95
            particleParamIndex.push_back(expressionSet.getVariableIndex(name.str()));
        }
    }
    for (int i = 0; i < (int) valueNames.size(); i++) {
        valueIndex.push_back(expressionSet.getVariableIndex(valueNames[i]));
        for (int j = 1; j < 3; j++) {
            stringstream name;
            name << valueNames[i] << j;
            particleValueIndex.push_back(expressionSet.getVariableIndex(name.str()));
        }
    }
peastman's avatar
peastman committed
96
97
98
    value0.resize(numAtoms);
    dEdV.resize(valueNames.size());
    for (int i = 0; i < (int) dEdV.size(); i++)
99
100
101
102
103
104
        dEdV[i].resize(numAtoms);
    dVdX.resize(valueDerivExpressions.size());
    dVdY.resize(valueDerivExpressions.size());
    dVdZ.resize(valueDerivExpressions.size());
    dVdR1.resize(valueDerivExpressions.size());
    dVdR2.resize(valueDerivExpressions.size());
105
106
}

peastman's avatar
peastman committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
CpuCustomGBForce::CpuCustomGBForce(int numAtoms, const std::vector<std::set<int> >& exclusions,
                     const vector<Lepton::CompiledExpression>& valueExpressions,
                     const vector<vector<Lepton::CompiledExpression> >& valueDerivExpressions,
                     const vector<vector<Lepton::CompiledExpression> >& valueGradientExpressions,
                     const vector<string>& valueNames,
                     const vector<CustomGBForce::ComputationType>& valueTypes,
                     const vector<Lepton::CompiledExpression>& energyExpressions,
                     const vector<vector<Lepton::CompiledExpression> >& energyDerivExpressions,
                     const vector<vector<Lepton::CompiledExpression> >& energyGradientExpressions,
                     const vector<CustomGBForce::ComputationType>& energyTypes,
                     const vector<string>& parameterNames, ThreadPool& threads) :
            exclusions(exclusions), cutoff(false), periodic(false), valueNames(valueNames), valueTypes(valueTypes),
            energyTypes(energyTypes), paramNames(parameterNames), threads(threads) {
    for (int i = 0; i < threads.getNumThreads(); i++)
        threadData.push_back(new ThreadData(numAtoms, threads.getNumThreads(), i, valueExpressions, valueDerivExpressions, valueGradientExpressions, valueNames,
                      energyExpressions, energyDerivExpressions, energyGradientExpressions, parameterNames));
    values.resize(valueNames.size());
    dEdV.resize(valueNames.size());
    for (int i = 0; i < (int) values.size(); i++) {
        values[i].resize(numAtoms);
        dEdV[i].resize(numAtoms);
    }
}

131
CpuCustomGBForce::~CpuCustomGBForce() {
peastman's avatar
peastman committed
132
133
    for (int i = 0; i < (int) threadData.size(); i++)
        delete threadData[i];
134
135
}

136
void CpuCustomGBForce::setUseCutoff(float distance, const CpuNeighborList& neighbors) {
137
138
    cutoff = true;
    cutoffDistance = distance;
139
    cutoffDistance2 = distance*distance;
140
    neighborList = &neighbors;
141
  }
142
143
144
145
146
147
148
149
150
151
152
153
154

void CpuCustomGBForce::setPeriodic(RealVec& boxSize) {
    if (cutoff) {
        assert(boxSize[0] >= 2.0*cutoffDistance);
        assert(boxSize[1] >= 2.0*cutoffDistance);
        assert(boxSize[2] >= 2.0*cutoffDistance);
    }
    periodic = true;
    periodicBoxSize[0] = boxSize[0];
    periodicBoxSize[1] = boxSize[1];
    periodicBoxSize[2] = boxSize[2];
  }

155
void CpuCustomGBForce::calculateIxn(int numberOfAtoms, float* posq, RealOpenMM** atomParameters,
peastman's avatar
peastman committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
                                           map<string, double>& globalParameters, vector<AlignedArray<float> >& threadForce,
                                           bool includeForce, bool includeEnergy, double& totalEnergy) {
    // Record the parameters for the threads.
    
    this->numberOfAtoms = numberOfAtoms;
    this->posq = posq;
    this->atomParameters = atomParameters;
    this->globalParameters = &globalParameters;
    this->threadForce = &threadForce;
    this->includeForce = includeForce;
    this->includeEnergy = includeEnergy;
    threadEnergy.resize(threads.getNumThreads());
    gmx_atomic_t counter;
    this->atomicCounter = &counter;

    // Calculate the first computed value.

    ComputeForceTask task(*this);
    gmx_atomic_set(&counter, 0);
    threads.execute(task);
    threads.waitForThreads();
    
    // Calculate the remaining computed values.
    
    threads.resumeThreads();
    threads.waitForThreads();

    // Calculate the energy terms.

    for (int i = 0; i < (int) threadData[0]->energyExpressions.size(); i++) {
        gmx_atomic_set(&counter, 0);
        threads.execute(task);
        threads.waitForThreads();
    }

    // Sum the energy derivatives.

    threads.resumeThreads();
    threads.waitForThreads();
    
    // Apply the chain rule to evaluate forces.

    gmx_atomic_set(&counter, 0);
    threads.resumeThreads();
    threads.waitForThreads();

    // Combine the energies from all the threads.
    
    if (includeEnergy) {
        int numThreads = threads.getNumThreads();
        for (int i = 0; i < numThreads; i++)
            totalEnergy += threadEnergy[i];
    }
}

void CpuCustomGBForce::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];
219
220
    fvec4 boxSize(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2], 0);
    fvec4 invBoxSize((1/periodicBoxSize[0]), (1/periodicBoxSize[1]), (1/periodicBoxSize[2]), 0);
peastman's avatar
peastman committed
221
222
223
224
225
226
227
228
229
230
231
232
233
    for (map<string, double>::const_iterator iter = globalParameters->begin(); iter != globalParameters->end(); ++iter)
        data.expressionSet.setVariable(data.expressionSet.getVariableIndex(iter->first), iter->second);

    // Calculate the first computed value.

    for (int i = 0; i < (int) data.value0.size(); i++)
        data.value0[i] = 0.0f;
    if (valueTypes[0] == CustomGBForce::ParticlePair)
        calculateParticlePairValue(0, data, numberOfAtoms, posq, atomParameters, true, boxSize, invBoxSize);
    else
        calculateParticlePairValue(0, data, numberOfAtoms, posq, atomParameters, false, boxSize, invBoxSize);
    threads.syncThreads();

234
    // Sum the first computed value and calculate the remaining ones.
peastman's avatar
peastman committed
235

236
    int numValues = valueTypes.size();
peastman's avatar
peastman committed
237
238
239
240
241
    for (int atom = data.firstAtom; atom < data.lastAtom; atom++) {
        float sum = 0.0f;
        for (int j = 0; j < (int) threadData.size(); j++)
            sum += threadData[j]->value0[atom];
        values[0][atom] = sum;
242
243
244
245
246
247
248
249
250
        data.expressionSet.setVariable(data.xindex, posq[4*atom]);
        data.expressionSet.setVariable(data.yindex, posq[4*atom+1]);
        data.expressionSet.setVariable(data.zindex, posq[4*atom+2]);
        for (int j = 0; j < (int) paramNames.size(); j++)
            data.expressionSet.setVariable(data.paramIndex[j], atomParameters[atom][j]);
        for (int i = 1; i < numValues; i++) {
            data.expressionSet.setVariable(data.valueIndex[i-1], values[i-1][atom]);
            values[i][atom] = (float) data.valueExpressions[i].evaluate();
        }
peastman's avatar
peastman committed
251
252
    }
    threads.syncThreads();
253
254
255

    // Now calculate the energy and its derivatives.

peastman's avatar
peastman committed
256
257
258
259
    for (int i = 0; i < (int) data.dEdV.size(); i++)
        for (int j = 0; j < (int) data.dEdV[i].size(); j++)
            data.dEdV[i][j] = 0.0;
    for (int termIndex = 0; termIndex < (int) data.energyExpressions.size(); termIndex++) {
260
        if (energyTypes[termIndex] == CustomGBForce::SingleParticle)
peastman's avatar
peastman committed
261
            calculateSingleParticleEnergyTerm(termIndex, data, numberOfAtoms, posq, atomParameters, forces, energy);
262
        else if (energyTypes[termIndex] == CustomGBForce::ParticlePair)
peastman's avatar
peastman committed
263
            calculateParticlePairEnergyTerm(termIndex, data, numberOfAtoms, posq, atomParameters, true, forces, energy, boxSize, invBoxSize);
264
        else
peastman's avatar
peastman committed
265
266
267
268
269
270
271
272
273
274
275
276
277
            calculateParticlePairEnergyTerm(termIndex, data, numberOfAtoms, posq, atomParameters, false, forces, energy, boxSize, invBoxSize);
        threads.syncThreads();
    }

    // Sum the energy derivatives.

    for (int atom = data.firstAtom; atom < data.lastAtom; atom++) {
        for (int i = 0; i < (int) dEdV.size(); i++) {
            float sum = 0.0f;
            for (int j = 0; j < (int) threadData.size(); j++)
                sum += threadData[j]->dEdV[i][atom];
            dEdV[i][atom] = sum;
        }
278
    }
peastman's avatar
peastman committed
279
    threads.syncThreads();
280
281
282

    // Apply the chain rule to evaluate forces.

peastman's avatar
peastman committed
283
    calculateChainRuleForces(data, numberOfAtoms, posq, atomParameters, forces, boxSize, invBoxSize);
284
285
}

peastman's avatar
peastman committed
286
287
void CpuCustomGBForce::calculateParticlePairValue(int index, ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
        bool useExclusions, const fvec4& boxSize, const fvec4& invBoxSize) {
288
    for (int i = 0; i < numAtoms; i++)
289
        values[index][i] = 0.0f;
peastman's avatar
peastman committed
290
    vector<float>& valueArray = (index == 0 ? data.value0 : values[index]);
291
292
293
    if (cutoff) {
        // Loop over all pairs in the neighbor list.

peastman's avatar
peastman committed
294
295
296
297
        while (true) {
            int blockIndex = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (blockIndex >= neighborList->getNumBlocks())
                break;
298
299
300
301
302
303
304
305
306
307
            const int* blockAtom = &neighborList->getSortedAtoms()[4*blockIndex];
            const vector<int>& neighbors = neighborList->getBlockNeighbors(blockIndex);
            const vector<char>& blockExclusions = neighborList->getBlockExclusions(blockIndex);
            for (int i = 0; i < (int) neighbors.size(); i++) {
                int first = neighbors[i];
                for (int k = 0; k < 4; k++) {
                    if ((blockExclusions[i] & (1<<k)) == 0) {
                        int second = blockAtom[k];
                        if (useExclusions && exclusions[first].find(second) != exclusions[first].end())
                            continue;
peastman's avatar
peastman committed
308
309
                        calculateOnePairValue(index, first, second, data, posq, atomParameters, valueArray, boxSize, invBoxSize);
                        calculateOnePairValue(index, second, first, data, posq, atomParameters, valueArray, boxSize, invBoxSize);
310
311
312
                    }
                }
            }
313
314
315
316
317
        }
    }
    else {
        // Perform an O(N^2) loop over all atom pairs.

peastman's avatar
peastman committed
318
319
320
321
        while (true) {
            int i = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (i >= numAtoms)
                break;
322
323
324
            for (int j = i+1; j < numAtoms; j++) {
                if (useExclusions && exclusions[i].find(j) != exclusions[i].end())
                    continue;
peastman's avatar
peastman committed
325
326
                calculateOnePairValue(index, i, j, data, posq, atomParameters, valueArray, boxSize, invBoxSize);
                calculateOnePairValue(index, j, i, data, posq, atomParameters, valueArray, boxSize, invBoxSize);
327
328
329
330
331
           }
        }
    }
}

peastman's avatar
peastman committed
332
333
void CpuCustomGBForce::calculateOnePairValue(int index, int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
        vector<float>& valueArray, const fvec4& boxSize, const fvec4& invBoxSize) {
334
335
336
337
338
    fvec4 deltaR;
    fvec4 pos1(posq+4*atom1);
    fvec4 pos2(posq+4*atom2);
    float r2;
    getDeltaR(pos2, pos1, deltaR, r2, periodic, boxSize, invBoxSize);
339
    if (cutoff && r2 >= cutoffDistance2)
340
        return;
341
    float r = sqrtf(r2);
342
    for (int i = 0; i < (int) paramNames.size(); i++) {
peastman's avatar
peastman committed
343
344
        data.expressionSet.setVariable(data.particleParamIndex[i*2], atomParameters[atom1][i]);
        data.expressionSet.setVariable(data.particleParamIndex[i*2+1], atomParameters[atom2][i]);
345
    }
peastman's avatar
peastman committed
346
    data.expressionSet.setVariable(data.rindex, r);
347
    for (int i = 0; i < index; i++) {
peastman's avatar
peastman committed
348
349
        data.expressionSet.setVariable(data.particleValueIndex[i*2], values[i][atom1]);
        data.expressionSet.setVariable(data.particleValueIndex[i*2+1], values[i][atom2]);
350
    }
peastman's avatar
peastman committed
351
    valueArray[atom1] += (float) data.valueExpressions[index].evaluate();
352
353
}

peastman's avatar
peastman committed
354
355
356
357
358
359
void CpuCustomGBForce::calculateSingleParticleEnergyTerm(int index, ThreadData& data, int numAtoms, float* posq,
        RealOpenMM** atomParameters, float* forces, double& totalEnergy) {
    for (int i = data.firstAtom; i < data.lastAtom; i++) {
        data.expressionSet.setVariable(data.xindex, posq[4*i]);
        data.expressionSet.setVariable(data.yindex, posq[4*i+1]);
        data.expressionSet.setVariable(data.zindex, posq[4*i+2]);
360
        for (int j = 0; j < (int) paramNames.size(); j++)
peastman's avatar
peastman committed
361
            data.expressionSet.setVariable(data.paramIndex[j], atomParameters[i][j]);
362
        for (int j = 0; j < (int) valueNames.size(); j++)
peastman's avatar
peastman committed
363
364
365
            data.expressionSet.setVariable(data.valueIndex[j], values[j][i]);
        if (includeEnergy)
            totalEnergy += (float) data.energyExpressions[index].evaluate();
366
        for (int j = 0; j < (int) valueNames.size(); j++)
peastman's avatar
peastman committed
367
368
369
370
            data.dEdV[j][i] += (float) data.energyDerivExpressions[index][j].evaluate();
        forces[4*i+0] -= (float) data.energyGradientExpressions[index][0].evaluate();
        forces[4*i+1] -= (float) data.energyGradientExpressions[index][1].evaluate();
        forces[4*i+2] -= (float) data.energyGradientExpressions[index][2].evaluate();
371
372
373
    }
}

peastman's avatar
peastman committed
374
375
void CpuCustomGBForce::calculateParticlePairEnergyTerm(int index, ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
        bool useExclusions, float* forces, double& totalEnergy, const fvec4& boxSize, const fvec4& invBoxSize) {
376
377
378
    if (cutoff) {
        // Loop over all pairs in the neighbor list.

peastman's avatar
peastman committed
379
380
381
382
        while (true) {
            int blockIndex = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (blockIndex >= neighborList->getNumBlocks())
                break;
383
384
385
386
387
388
389
390
391
392
            const int* blockAtom = &neighborList->getSortedAtoms()[4*blockIndex];
            const vector<int>& neighbors = neighborList->getBlockNeighbors(blockIndex);
            const vector<char>& blockExclusions = neighborList->getBlockExclusions(blockIndex);
            for (int i = 0; i < (int) neighbors.size(); i++) {
                int first = neighbors[i];
                for (int k = 0; k < 4; k++) {
                    if ((blockExclusions[i] & (1<<k)) == 0) {
                        int second = blockAtom[k];
                        if (useExclusions && exclusions[first].find(second) != exclusions[first].end())
                            continue;
peastman's avatar
peastman committed
393
                        calculateOnePairEnergyTerm(index, first, second, data, posq, atomParameters, forces, totalEnergy, boxSize, invBoxSize);
394
395
396
                    }
                }
            }
397
398
399
400
401
        }
    }
    else {
        // Perform an O(N^2) loop over all atom pairs.

peastman's avatar
peastman committed
402
403
404
405
        while (true) {
            int i = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (i >= numAtoms)
                break;
406
407
408
            for (int j = i+1; j < numAtoms; j++) {
                if (useExclusions && exclusions[i].find(j) != exclusions[i].end())
                    continue;
peastman's avatar
peastman committed
409
                calculateOnePairEnergyTerm(index, i, j, data, posq, atomParameters, forces, totalEnergy, boxSize, invBoxSize);
410
411
412
413
414
           }
        }
    }
}

peastman's avatar
peastman committed
415
416
void CpuCustomGBForce::calculateOnePairEnergyTerm(int index, int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
        float* forces, double& totalEnergy, const fvec4& boxSize, const fvec4& invBoxSize) {
417
418
    // Compute the displacement.

419
420
421
422
423
    fvec4 deltaR;
    fvec4 pos1(posq+4*atom1);
    fvec4 pos2(posq+4*atom2);
    float r2;
    getDeltaR(pos2, pos1, deltaR, r2, periodic, boxSize, invBoxSize);
424
    if (cutoff && r2 >= cutoffDistance2)
425
        return;
426
    float r = sqrtf(r2);
427
428
429
430

    // Record variables for evaluating expressions.

    for (int i = 0; i < (int) paramNames.size(); i++) {
peastman's avatar
peastman committed
431
432
        data.expressionSet.setVariable(data.particleParamIndex[i*2], atomParameters[atom1][i]);
        data.expressionSet.setVariable(data.particleParamIndex[i*2+1], atomParameters[atom2][i]);
433
    }
peastman's avatar
peastman committed
434
    data.expressionSet.setVariable(data.rindex, r);
435
    for (int i = 0; i < (int) valueNames.size(); i++) {
peastman's avatar
peastman committed
436
437
        data.expressionSet.setVariable(data.particleValueIndex[i*2], values[i][atom1]);
        data.expressionSet.setVariable(data.particleValueIndex[i*2+1], values[i][atom2]);
438
439
440
441
    }

    // Evaluate the energy and its derivatives.

peastman's avatar
peastman committed
442
443
444
    if (includeEnergy)
        totalEnergy += (float) data.energyExpressions[index].evaluate();
    float dEdR = (float) data.energyDerivExpressions[index][0].evaluate();
445
    dEdR *= 1/r;
446
447
448
    fvec4 result = deltaR*dEdR;
    (fvec4(forces+4*atom1)-result).store(forces+4*atom1);
    (fvec4(forces+4*atom2)+result).store(forces+4*atom2);
449
    for (int i = 0; i < (int) valueNames.size(); i++) {
peastman's avatar
peastman committed
450
451
        data.dEdV[i][atom1] += (float) data.energyDerivExpressions[index][2*i+1].evaluate();
        data.dEdV[i][atom2] += (float) data.energyDerivExpressions[index][2*i+2].evaluate();
452
453
454
    }
}

peastman's avatar
peastman committed
455
456
void CpuCustomGBForce::calculateChainRuleForces(ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
        float* forces, const fvec4& boxSize, const fvec4& invBoxSize) {
457
458
459
    if (cutoff) {
        // Loop over all pairs in the neighbor list.

peastman's avatar
peastman committed
460
461
462
463
        while (true) {
            int blockIndex = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (blockIndex >= neighborList->getNumBlocks())
                break;
464
465
466
467
468
469
470
471
472
            const int* blockAtom = &neighborList->getSortedAtoms()[4*blockIndex];
            const vector<int>& neighbors = neighborList->getBlockNeighbors(blockIndex);
            const vector<char>& blockExclusions = neighborList->getBlockExclusions(blockIndex);
            for (int i = 0; i < (int) neighbors.size(); i++) {
                int first = neighbors[i];
                for (int k = 0; k < 4; k++) {
                    if ((blockExclusions[i] & (1<<k)) == 0) {
                        int second = blockAtom[k];
                        bool isExcluded = (exclusions[first].find(second) != exclusions[first].end());
peastman's avatar
peastman committed
473
474
                        calculateOnePairChainRule(first, second, data, posq, atomParameters, forces, isExcluded, boxSize, invBoxSize);
                        calculateOnePairChainRule(second, first, data, posq, atomParameters, forces, isExcluded, boxSize, invBoxSize);
475
476
477
                    }
                }
            }
478
479
480
481
482
        }
    }
    else {
        // Perform an O(N^2) loop over all atom pairs.

peastman's avatar
peastman committed
483
484
485
486
        while (true) {
            int i = gmx_atomic_fetch_add(reinterpret_cast<gmx_atomic_t*>(atomicCounter), 1);
            if (i >= numAtoms)
                break;
487
488
            for (int j = i+1; j < numAtoms; j++) {
                bool isExcluded = (exclusions[i].find(j) != exclusions[i].end());
peastman's avatar
peastman committed
489
490
                calculateOnePairChainRule(i, j, data, posq, atomParameters, forces, isExcluded, boxSize, invBoxSize);
                calculateOnePairChainRule(j, i, data, posq, atomParameters, forces, isExcluded, boxSize, invBoxSize);
491
492
493
494
495
496
           }
        }
    }

    // Compute chain rule terms for computed values that depend explicitly on particle coordinates.

peastman's avatar
peastman committed
497
498
499
500
    for (int i = data.firstAtom; i < data.lastAtom; i++) {
        data.expressionSet.setVariable(data.xindex, posq[4*i]);
        data.expressionSet.setVariable(data.yindex, posq[4*i+1]);
        data.expressionSet.setVariable(data.zindex, posq[4*i+2]);
501
        for (int j = 0; j < (int) paramNames.size(); j++)
peastman's avatar
peastman committed
502
            data.expressionSet.setVariable(data.paramIndex[j], atomParameters[i][j]);
503
        for (int j = 1; j < (int) valueNames.size(); j++) {
peastman's avatar
peastman committed
504
505
506
507
            data.expressionSet.setVariable(data.valueIndex[j-1], values[j-1][i]);
            data.dVdX[j] = 0.0;
            data.dVdY[j] = 0.0;
            data.dVdZ[j] = 0.0;
508
            for (int k = 1; k < j; k++) {
peastman's avatar
peastman committed
509
510
511
512
                float dVdV = (float) data.valueDerivExpressions[j][k].evaluate();
                data.dVdX[j] += dVdV*data.dVdX[k];
                data.dVdY[j] += dVdV*data.dVdY[k];
                data.dVdZ[j] += dVdV*data.dVdZ[k];
513
            }
peastman's avatar
peastman committed
514
515
516
517
518
519
            data.dVdX[j] += (float) data.valueGradientExpressions[j][0].evaluate();
            data.dVdY[j] += (float) data.valueGradientExpressions[j][1].evaluate();
            data.dVdZ[j] += (float) data.valueGradientExpressions[j][2].evaluate();
            forces[4*i+0] -= dEdV[j][i]*data.dVdX[j];
            forces[4*i+1] -= dEdV[j][i]*data.dVdY[j];
            forces[4*i+2] -= dEdV[j][i]*data.dVdZ[j];
520
521
522
523
        }
    }
}

peastman's avatar
peastman committed
524
525
void CpuCustomGBForce::calculateOnePairChainRule(int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
        float* forces, bool isExcluded, const fvec4& boxSize, const fvec4& invBoxSize) {
526
527
    // Compute the displacement.

528
529
530
531
532
    fvec4 deltaR;
    fvec4 pos1(posq+4*atom1);
    fvec4 pos2(posq+4*atom2);
    float r2;
    getDeltaR(pos2, pos1, deltaR, r2, periodic, boxSize, invBoxSize);
533
    if (cutoff && r2 >= cutoffDistance2)
534
        return;
535
    float r = sqrtf(r2);
536
537
538
539

    // Record variables for evaluating expressions.

    for (int i = 0; i < (int) paramNames.size(); i++) {
peastman's avatar
peastman committed
540
541
        data.expressionSet.setVariable(data.particleParamIndex[i*2], atomParameters[atom1][i]);
        data.expressionSet.setVariable(data.particleParamIndex[i*2+1], atomParameters[atom2][i]);
542
        data.expressionSet.setVariable(data.paramIndex[i], atomParameters[atom1][i]);
543
    }
544
545
546
547
    data.expressionSet.setVariable(data.valueIndex[0], values[0][atom1]);
    data.expressionSet.setVariable(data.xindex, posq[4*atom1]);
    data.expressionSet.setVariable(data.yindex, posq[4*atom1+1]);
    data.expressionSet.setVariable(data.zindex, posq[4*atom1+2]);
peastman's avatar
peastman committed
548
549
550
    data.expressionSet.setVariable(data.rindex, r);
    data.expressionSet.setVariable(data.particleValueIndex[0], values[0][atom1]);
    data.expressionSet.setVariable(data.particleValueIndex[1], values[0][atom2]);
551
552
553

    // Evaluate the derivative of each parameter with respect to position and apply forces.

554
555
    float rinv = 1/r;
    deltaR *= rinv;
556
    fvec4 f1(0.0f), f2(0.0f);
557
    if (!isExcluded || valueTypes[0] != CustomGBForce::ParticlePair) {
peastman's avatar
peastman committed
558
559
        data.dVdR1[0] = (float) data.valueDerivExpressions[0][0].evaluate();
        data.dVdR2[0] = -data.dVdR1[0];
560
561
        f1 -= deltaR*(dEdV[0][atom1]*data.dVdR1[0]);
        f2 -= deltaR*(dEdV[0][atom1]*data.dVdR2[0]);
562
563
    }
    for (int i = 1; i < (int) valueNames.size(); i++) {
peastman's avatar
peastman committed
564
565
566
        data.expressionSet.setVariable(data.valueIndex[i], values[i][atom1]);
        data.dVdR1[i] = 0.0;
        data.dVdR2[i] = 0.0;
567
        for (int j = 0; j < i; j++) {
peastman's avatar
peastman committed
568
569
570
            float dVdV = (float) data.valueDerivExpressions[i][j].evaluate();
            data.dVdR1[i] += dVdV*data.dVdR1[j];
            data.dVdR2[i] += dVdV*data.dVdR2[j];
571
        }
572
573
        f1 -= deltaR*(dEdV[i][atom1]*data.dVdR1[i]);
        f2 -= deltaR*(dEdV[i][atom1]*data.dVdR2[i]);
574
    }
575
576
    (fvec4(forces+4*atom1)+f1).store(forces+4*atom1);
    (fvec4(forces+4*atom2)+f2).store(forces+4*atom2);
577
578
579
580
581
582
583
}

void CpuCustomGBForce::getDeltaR(const fvec4& posI, const fvec4& posJ, fvec4& deltaR, float& r2, bool periodic, const fvec4& boxSize, const fvec4& invBoxSize) const {
    deltaR = posJ-posI;
    if (periodic) {
        fvec4 base = round(deltaR*invBoxSize)*boxSize;
        deltaR = deltaR-base;
584
    }
585
    r2 = dot3(deltaR, deltaR);
586
}