"plugins/vscode:/vscode.git/clone" did not exist on "e4ca66e458ed562a4affcb80c9ad0a1a632c3ed5"
ReferenceCustomGBIxn.cpp 23.5 KB
Newer Older
1

2
/* Portions copyright (c) 2009-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
 * 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>

28
#include "SimTKOpenMMUtilities.h"
29
30
31
32
33
34
35
36
#include "ReferenceForce.h"
#include "ReferenceCustomGBIxn.h"

using std::map;
using std::set;
using std::string;
using std::stringstream;
using std::vector;
37
using namespace OpenMM;
38
39
40
41
42
43
44

/**---------------------------------------------------------------------------------------

   ReferenceCustomGBIxn constructor

   --------------------------------------------------------------------------------------- */

45
46
47
ReferenceCustomGBIxn::ReferenceCustomGBIxn(const vector<Lepton::CompiledExpression>& valueExpressions,
                     const vector<vector<Lepton::CompiledExpression> > valueDerivExpressions,
                     const vector<vector<Lepton::CompiledExpression> > valueGradientExpressions,
48
                     const vector<vector<Lepton::CompiledExpression> > valueParamDerivExpressions,
49
50
                     const vector<string>& valueNames,
                     const vector<OpenMM::CustomGBForce::ComputationType>& valueTypes,
51
52
53
                     const vector<Lepton::CompiledExpression>& energyExpressions,
                     const vector<vector<Lepton::CompiledExpression> > energyDerivExpressions,
                     const vector<vector<Lepton::CompiledExpression> > energyGradientExpressions,
54
                     const vector<vector<Lepton::CompiledExpression> > energyParamDerivExpressions,
55
56
                     const vector<OpenMM::CustomGBForce::ComputationType>& energyTypes,
                     const vector<string>& parameterNames) :
57
58
            cutoff(false), periodic(false), valueExpressions(valueExpressions), valueDerivExpressions(valueDerivExpressions), valueGradientExpressions(valueGradientExpressions), valueParamDerivExpressions(valueParamDerivExpressions),
            valueTypes(valueTypes), energyExpressions(energyExpressions), energyDerivExpressions(energyDerivExpressions), energyGradientExpressions(energyGradientExpressions), energyParamDerivExpressions(energyParamDerivExpressions),
59
60
61
62
63
64
65
66
67
68
            energyTypes(energyTypes) {

    for (int i = 0; i < this->valueExpressions.size(); i++)
        expressionSet.registerExpression(this->valueExpressions[i]);
    for (int i = 0; i < this->valueDerivExpressions.size(); i++)
        for (int j = 0; j < this->valueDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->valueDerivExpressions[i][j]);
    for (int i = 0; i < this->valueGradientExpressions.size(); i++)
        for (int j = 0; j < this->valueGradientExpressions[i].size(); j++)
            expressionSet.registerExpression(this->valueGradientExpressions[i][j]);
69
70
71
    for (int i = 0; i < this->valueParamDerivExpressions.size(); i++)
        for (int j = 0; j < this->valueParamDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->valueParamDerivExpressions[i][j]);
72
73
74
75
76
77
78
79
    for (int i = 0; i < this->energyExpressions.size(); i++)
        expressionSet.registerExpression(this->energyExpressions[i]);
    for (int i = 0; i < this->energyDerivExpressions.size(); i++)
        for (int j = 0; j < this->energyDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->energyDerivExpressions[i][j]);
    for (int i = 0; i < this->energyGradientExpressions.size(); i++)
        for (int j = 0; j < this->energyGradientExpressions[i].size(); j++)
            expressionSet.registerExpression(this->energyGradientExpressions[i][j]);
80
81
82
    for (int i = 0; i < this->energyParamDerivExpressions.size(); i++)
        for (int j = 0; j < this->energyParamDerivExpressions[i].size(); j++)
            expressionSet.registerExpression(this->energyParamDerivExpressions[i][j]);
83
84
85
86
87
88
    rIndex = expressionSet.getVariableIndex("r");
    xIndex = expressionSet.getVariableIndex("x");
    yIndex = expressionSet.getVariableIndex("y");
    zIndex = expressionSet.getVariableIndex("z");
    for (int i = 0; i < (int) parameterNames.size(); i++) {
        paramIndex.push_back(expressionSet.getVariableIndex(parameterNames[i]));
89
90
        for (int j = 1; j < 3; j++) {
            stringstream name;
91
92
            name << parameterNames[i] << j;
            particleParamIndex.push_back(expressionSet.getVariableIndex(name.str()));
93
94
95
        }
    }
    for (int i = 0; i < (int) valueNames.size(); i++) {
96
        valueIndex.push_back(expressionSet.getVariableIndex(valueNames[i]));
97
98
99
        for (int j = 1; j < 3; j++) {
            stringstream name;
            name << valueNames[i] << j;
100
            particleValueIndex.push_back(expressionSet.getVariableIndex(name.str()));
101
102
103
104
105
106
107
108
109
110
        }
    }
}

/**---------------------------------------------------------------------------------------

   ReferenceCustomGBIxn destructor

   --------------------------------------------------------------------------------------- */

111
ReferenceCustomGBIxn::~ReferenceCustomGBIxn() {
112
113
114
115
116
117
118
119
120
121
122
}

  /**---------------------------------------------------------------------------------------

     Set the force to use a cutoff.

     @param distance            the cutoff distance
     @param neighbors           the neighbor list to use

     --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
123
  void ReferenceCustomGBIxn::setUseCutoff(double distance, const OpenMM::NeighborList& neighbors) {
124
125
126
127
128
129
130
131
132
133
134
135

    cutoff = true;
    cutoffDistance = distance;
    neighborList = &neighbors;
  }

  /**---------------------------------------------------------------------------------------

     Set the force to use periodic boundary conditions.  This requires that a cutoff has
     also been set, and the smallest side of the periodic box is at least twice the cutoff
     distance.

136
     @param vectors    the vectors defining the periodic box
137
138
139

     --------------------------------------------------------------------------------------- */

peastman's avatar
peastman committed
140
  void ReferenceCustomGBIxn::setPeriodic(Vec3* vectors) {
141

142
    if (cutoff) {
143
144
145
        assert(vectors[0][0] >= 2.0*cutoffDistance);
        assert(vectors[1][1] >= 2.0*cutoffDistance);
        assert(vectors[2][2] >= 2.0*cutoffDistance);
146
    }
147
    periodic = true;
148
149
150
    periodicBoxVectors[0] = vectors[0];
    periodicBoxVectors[1] = vectors[1];
    periodicBoxVectors[2] = vectors[2];
151
152
  }

peastman's avatar
peastman committed
153
154
155
void ReferenceCustomGBIxn::calculateIxn(int numberOfAtoms, vector<Vec3>& atomCoordinates, double** atomParameters,
                                           const vector<set<int> >& exclusions, map<string, double>& globalParameters, vector<Vec3>& forces,
                                           double* totalEnergy, double* energyParamDerivs) {
156
157
    for (map<string, double>::const_iterator iter = globalParameters.begin(); iter != globalParameters.end(); ++iter)
        expressionSet.setVariable(expressionSet.getVariableIndex(iter->first), iter->second);
158
159
160
161
162
163
    
    // Initialize arrays for storing values.
    
    int numValues = valueTypes.size();
    int numDerivs = valueParamDerivExpressions[0].size();
    values.resize(numValues);
peastman's avatar
peastman committed
164
    dEdV.resize(numValues, vector<double>(numberOfAtoms, 0.0));
165
166
    dValuedParam.resize(numValues);
    for (int i = 0; i < numValues; i++)
peastman's avatar
peastman committed
167
        dValuedParam[i].resize(numDerivs, vector<double>(numberOfAtoms, 0.0));
168

169
170
    // First calculate the computed values.

171
172
    for (int valueIndex = 0; valueIndex < numValues; valueIndex++) {
        if (valueTypes[valueIndex] == OpenMM::CustomGBForce::SingleParticle)
173
            calculateSingleParticleValue(valueIndex, numberOfAtoms, atomCoordinates, atomParameters);
174
        else if (valueTypes[valueIndex] == OpenMM::CustomGBForce::ParticlePair)
175
            calculateParticlePairValue(valueIndex, numberOfAtoms, atomCoordinates, atomParameters, exclusions, true);
176
        else
177
            calculateParticlePairValue(valueIndex, numberOfAtoms, atomCoordinates, atomParameters, exclusions, false);
178
    }
179
180
181

    // Now calculate the energy and its derivates.

182
183
    for (int termIndex = 0; termIndex < (int) energyExpressions.size(); termIndex++) {
        if (energyTypes[termIndex] == OpenMM::CustomGBForce::SingleParticle)
184
            calculateSingleParticleEnergyTerm(termIndex, numberOfAtoms, atomCoordinates, atomParameters, forces, totalEnergy, energyParamDerivs);
185
        else if (energyTypes[termIndex] == OpenMM::CustomGBForce::ParticlePair)
186
            calculateParticlePairEnergyTerm(termIndex, numberOfAtoms, atomCoordinates, atomParameters, exclusions, true, forces, totalEnergy, energyParamDerivs);
187
        else
188
            calculateParticlePairEnergyTerm(termIndex, numberOfAtoms, atomCoordinates, atomParameters, exclusions, false, forces, totalEnergy, energyParamDerivs);
189
    }
190
191
192

    // Apply the chain rule to evaluate forces.

193
    calculateChainRuleForces(numberOfAtoms, atomCoordinates, atomParameters, exclusions, forces, energyParamDerivs);
194
195
}

peastman's avatar
peastman committed
196
void ReferenceCustomGBIxn::calculateSingleParticleValue(int index, int numAtoms, vector<Vec3>& atomCoordinates, double** atomParameters) {
197
198
    values[index].resize(numAtoms);
    for (int i = 0; i < numAtoms; i++) {
199
200
201
202
203
        expressionSet.setVariable(xIndex, atomCoordinates[i][0]);
        expressionSet.setVariable(yIndex, atomCoordinates[i][1]);
        expressionSet.setVariable(zIndex, atomCoordinates[i][2]);
        for (int j = 0; j < (int) paramIndex.size(); j++)
            expressionSet.setVariable(paramIndex[j], atomParameters[i][j]);
204
        for (int j = 0; j < index; j++)
205
            expressionSet.setVariable(valueIndex[j], values[j][i]);
peastman's avatar
peastman committed
206
        values[index][i] = valueExpressions[index].evaluate();
207
208
209

        // Calculate derivatives with respect to parameters.

210
211
        for (int j = 0; j < valueParamDerivExpressions[index].size(); j++)
            dValuedParam[index][j][i] += valueParamDerivExpressions[index][j].evaluate();
212
        for (int j = 0; j < index; j++) {
peastman's avatar
peastman committed
213
            double dVdV = valueDerivExpressions[index][j].evaluate();
214
215
216
            for (int k = 0; k < valueParamDerivExpressions[index].size(); k++)
                dValuedParam[index][k][i] += dVdV*dValuedParam[j][k][i];
        }
217
218
219
    }
}

peastman's avatar
peastman committed
220
void ReferenceCustomGBIxn::calculateParticlePairValue(int index, int numAtoms, vector<Vec3>& atomCoordinates, double** atomParameters,
221
        const vector<set<int> >& exclusions, bool useExclusions) {
222
223
    values[index].resize(numAtoms);
    for (int i = 0; i < numAtoms; i++)
peastman's avatar
peastman committed
224
        values[index][i] = 0.0;
225
    if (cutoff) {
226
227
        // Loop over all pairs in the neighbor list.

228
229
230
231
        for (int i = 0; i < (int) neighborList->size(); i++) {
            OpenMM::AtomPair pair = (*neighborList)[i];
            if (useExclusions && exclusions[pair.first].find(pair.second) != exclusions[pair.first].end())
                continue;
232
233
            calculateOnePairValue(index, pair.first, pair.second, atomCoordinates, atomParameters);
            calculateOnePairValue(index, pair.second, pair.first, atomCoordinates, atomParameters);
234
235
236
        }
    }
    else {
237
238
        // Perform an O(N^2) loop over all atom pairs.

239
240
        for (int i = 0; i < numAtoms; i++) {
            for (int j = i+1; j < numAtoms; j++) {
241
242
                if (useExclusions && exclusions[i].find(j) != exclusions[i].end())
                    continue;
243
244
                calculateOnePairValue(index, i, j, atomCoordinates, atomParameters);
                calculateOnePairValue(index, j, i, atomCoordinates, atomParameters);
245
246
247
248
249
           }
        }
    }
}

peastman's avatar
peastman committed
250
251
void ReferenceCustomGBIxn::calculateOnePairValue(int index, int atom1, int atom2, vector<Vec3>& atomCoordinates, double** atomParameters) {
    double deltaR[ReferenceForce::LastDeltaRIndex];
252
    if (periodic)
253
        ReferenceForce::getDeltaRPeriodic(atomCoordinates[atom2], atomCoordinates[atom1], periodicBoxVectors, deltaR);
254
255
    else
        ReferenceForce::getDeltaR(atomCoordinates[atom2], atomCoordinates[atom1], deltaR);
peastman's avatar
peastman committed
256
    double r = deltaR[ReferenceForce::RIndex];
257
258
    if (cutoff && r >= cutoffDistance)
        return;
259
260
261
    for (int i = 0; i < (int) paramIndex.size(); i++) {
        expressionSet.setVariable(particleParamIndex[i*2], atomParameters[atom1][i]);
        expressionSet.setVariable(particleParamIndex[i*2+1], atomParameters[atom2][i]);
262
    }
263
    expressionSet.setVariable(rIndex, r);
264
    for (int i = 0; i < index; i++) {
265
266
        expressionSet.setVariable(particleValueIndex[i*2], values[i][atom1]);
        expressionSet.setVariable(particleValueIndex[i*2+1], values[i][atom2]);
267
    }
peastman's avatar
peastman committed
268
    values[index][atom1] += valueExpressions[index].evaluate();
269
270
271
272
273
    
    // Calculate derivatives with respect to parameters.
    
    for (int i = 0; i < valueParamDerivExpressions[index].size(); i++)
        dValuedParam[index][i][atom1] += valueParamDerivExpressions[index][i].evaluate();
274
275
}

peastman's avatar
peastman committed
276
277
void ReferenceCustomGBIxn::calculateSingleParticleEnergyTerm(int index, int numAtoms, vector<Vec3>& atomCoordinates,
        double** atomParameters, vector<Vec3>& forces, double* totalEnergy, double* energyParamDerivs) {
278
    for (int i = 0; i < numAtoms; i++) {
279
280
281
282
283
284
285
        expressionSet.setVariable(xIndex, atomCoordinates[i][0]);
        expressionSet.setVariable(yIndex, atomCoordinates[i][1]);
        expressionSet.setVariable(zIndex, atomCoordinates[i][2]);
        for (int j = 0; j < (int) paramIndex.size(); j++)
            expressionSet.setVariable(paramIndex[j], atomParameters[i][j]);
        for (int j = 0; j < valueIndex.size(); j++)
            expressionSet.setVariable(valueIndex[j], values[j][i]);
286
287
288
        
        // Compute energy and force.
        
289
        if (totalEnergy != NULL)
peastman's avatar
peastman committed
290
            *totalEnergy += energyExpressions[index].evaluate();
291
        for (int j = 0; j < (int) valueIndex.size(); j++)
peastman's avatar
peastman committed
292
293
294
295
            dEdV[j][i] += energyDerivExpressions[index][j].evaluate();
        forces[i][0] -= energyGradientExpressions[index][0].evaluate();
        forces[i][1] -= energyGradientExpressions[index][1].evaluate();
        forces[i][2] -= energyGradientExpressions[index][2].evaluate();
296
297
298
        
        // Compute derivatives with respect to parameters.
        
299
        for (int k = 0; k < energyParamDerivExpressions[index].size(); k++)
300
            energyParamDerivs[k] += energyParamDerivExpressions[index][k].evaluate();
301
302
303
    }
}

peastman's avatar
peastman committed
304
305
void ReferenceCustomGBIxn::calculateParticlePairEnergyTerm(int index, int numAtoms, vector<Vec3>& atomCoordinates, double** atomParameters,
        const vector<set<int> >& exclusions, bool useExclusions, vector<Vec3>& forces, double* totalEnergy, double* energyParamDerivs) {
306
    if (cutoff) {
307
308
        // Loop over all pairs in the neighbor list.

309
310
311
312
        for (int i = 0; i < (int) neighborList->size(); i++) {
            OpenMM::AtomPair pair = (*neighborList)[i];
            if (useExclusions && exclusions[pair.first].find(pair.second) != exclusions[pair.first].end())
                continue;
313
            calculateOnePairEnergyTerm(index, pair.first, pair.second, atomCoordinates, atomParameters, forces, totalEnergy, energyParamDerivs);
314
315
316
        }
    }
    else {
317
318
        // Perform an O(N^2) loop over all atom pairs.

319
320
        for (int i = 0; i < numAtoms; i++) {
            for (int j = i+1; j < numAtoms; j++) {
321
322
                if (useExclusions && exclusions[i].find(j) != exclusions[i].end())
                    continue;
323
                calculateOnePairEnergyTerm(index, i, j, atomCoordinates, atomParameters, forces, totalEnergy, energyParamDerivs);
324
325
326
327
328
           }
        }
    }
}

peastman's avatar
peastman committed
329
330
void ReferenceCustomGBIxn::calculateOnePairEnergyTerm(int index, int atom1, int atom2, vector<Vec3>& atomCoordinates, double** atomParameters,
        vector<Vec3>& forces, double* totalEnergy, double* energyParamDerivs) {
331
332
    // Compute the displacement.

peastman's avatar
peastman committed
333
    double deltaR[ReferenceForce::LastDeltaRIndex];
334
    if (periodic)
335
        ReferenceForce::getDeltaRPeriodic(atomCoordinates[atom2], atomCoordinates[atom1], periodicBoxVectors, deltaR);
336
337
    else
        ReferenceForce::getDeltaR(atomCoordinates[atom2], atomCoordinates[atom1], deltaR);
peastman's avatar
peastman committed
338
    double r = deltaR[ReferenceForce::RIndex];
339
340
    if (cutoff && r >= cutoffDistance)
        return;
341
342
343

    // Record variables for evaluating expressions.

344
345
346
    for (int i = 0; i < (int) paramIndex.size(); i++) {
        expressionSet.setVariable(particleParamIndex[i*2], atomParameters[atom1][i]);
        expressionSet.setVariable(particleParamIndex[i*2+1], atomParameters[atom2][i]);
347
    }
348
349
350
351
    expressionSet.setVariable(rIndex, r);
    for (int i = 0; i < (int) valueIndex.size(); i++) {
        expressionSet.setVariable(particleValueIndex[i*2], values[i][atom1]);
        expressionSet.setVariable(particleValueIndex[i*2+1], values[i][atom2]);
352
    }
353
354
355

    // Evaluate the energy and its derivatives.

356
    if (totalEnergy != NULL)
peastman's avatar
peastman committed
357
358
        *totalEnergy += energyExpressions[index].evaluate();
    double dEdR = energyDerivExpressions[index][0].evaluate();
359
360
361
362
363
    dEdR *= 1/r;
    for (int i = 0; i < 3; i++) {
       forces[atom1][i] -= dEdR*deltaR[i];
       forces[atom2][i] += dEdR*deltaR[i];
    }
364
    for (int i = 0; i < (int) valueIndex.size(); i++) {
peastman's avatar
peastman committed
365
366
        dEdV[i][atom1] += energyDerivExpressions[index][2*i+1].evaluate();
        dEdV[i][atom2] += energyDerivExpressions[index][2*i+2].evaluate();
367
    }
368
369
370
371
372
        
    // Compute derivatives with respect to parameters.

    for (int i = 0; i < energyParamDerivExpressions[index].size(); i++)
        energyParamDerivs[i] += energyParamDerivExpressions[index][i].evaluate();
373
374
}

peastman's avatar
peastman committed
375
376
void ReferenceCustomGBIxn::calculateChainRuleForces(int numAtoms, vector<Vec3>& atomCoordinates, double** atomParameters,
        const vector<set<int> >& exclusions, vector<Vec3>& forces, double* energyParamDerivs) {
377
378
379
380
381
    if (cutoff) {
        // Loop over all pairs in the neighbor list.

        for (int i = 0; i < (int) neighborList->size(); i++) {
            OpenMM::AtomPair pair = (*neighborList)[i];
382
            bool isExcluded = (exclusions[pair.first].find(pair.second) != exclusions[pair.first].end());
383
384
            calculateOnePairChainRule(pair.first, pair.second, atomCoordinates, atomParameters, forces, isExcluded);
            calculateOnePairChainRule(pair.second, pair.first, atomCoordinates, atomParameters, forces, isExcluded);
385
386
387
388
389
        }
    }
    else {
        // Perform an O(N^2) loop over all atom pairs.

390
391
        for (int i = 0; i < numAtoms; i++) {
            for (int j = i+1; j < numAtoms; j++) {
392
                bool isExcluded = (exclusions[i].find(j) != exclusions[i].end());
393
394
                calculateOnePairChainRule(i, j, atomCoordinates, atomParameters, forces, isExcluded);
                calculateOnePairChainRule(j, i, atomCoordinates, atomParameters, forces, isExcluded);
395
396
397
           }
        }
    }
Peter Eastman's avatar
Peter Eastman committed
398
399
400
401

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

    for (int i = 0; i < numAtoms; i++) {
402
403
404
        expressionSet.setVariable(xIndex, atomCoordinates[i][0]);
        expressionSet.setVariable(yIndex, atomCoordinates[i][1]);
        expressionSet.setVariable(zIndex, atomCoordinates[i][2]);
peastman's avatar
peastman committed
405
406
407
        vector<double> dVdX(valueDerivExpressions.size(), 0.0);
        vector<double> dVdY(valueDerivExpressions.size(), 0.0);
        vector<double> dVdZ(valueDerivExpressions.size(), 0.0);
408
409
410
411
        for (int j = 0; j < (int) paramIndex.size(); j++)
            expressionSet.setVariable(paramIndex[j], atomParameters[i][j]);
        for (int j = 1; j < (int) valueIndex.size(); j++) {
            expressionSet.setVariable(valueIndex[j-1], values[j-1][i]);
412
            for (int k = 1; k < j; k++) {
peastman's avatar
peastman committed
413
                double dVdV = valueDerivExpressions[j][k].evaluate();
414
415
416
                dVdX[j] += dVdV*dVdX[k];
                dVdY[j] += dVdV*dVdY[k];
                dVdZ[j] += dVdV*dVdZ[k];
Peter Eastman's avatar
Peter Eastman committed
417
            }
peastman's avatar
peastman committed
418
419
420
            dVdX[j] += valueGradientExpressions[j][0].evaluate();
            dVdY[j] += valueGradientExpressions[j][1].evaluate();
            dVdZ[j] += valueGradientExpressions[j][2].evaluate();
421
422
423
            forces[i][0] -= dEdV[j][i]*dVdX[j];
            forces[i][1] -= dEdV[j][i]*dVdY[j];
            forces[i][2] -= dEdV[j][i]*dVdZ[j];
Peter Eastman's avatar
Peter Eastman committed
424
425
        }
    }
426
427
428
429
430
431
432
        
    // Compute chain rule terms for derivatives with respect to parameters.

    for (int i = 0; i < numAtoms; i++)
        for (int j = 0; j < (int) valueIndex.size(); j++)
            for (int k = 0; k < dValuedParam[j].size(); k++)
                energyParamDerivs[k] += dEdV[j][i]*dValuedParam[j][k][i];
433
434
}

peastman's avatar
peastman committed
435
436
void ReferenceCustomGBIxn::calculateOnePairChainRule(int atom1, int atom2, vector<Vec3>& atomCoordinates, double** atomParameters,
        vector<Vec3>& forces, bool isExcluded) {
437
438
    // Compute the displacement.

peastman's avatar
peastman committed
439
    double deltaR[ReferenceForce::LastDeltaRIndex];
440
    if (periodic)
441
        ReferenceForce::getDeltaRPeriodic(atomCoordinates[atom2], atomCoordinates[atom1], periodicBoxVectors, deltaR);
442
443
    else
        ReferenceForce::getDeltaR(atomCoordinates[atom2], atomCoordinates[atom1], deltaR);
peastman's avatar
peastman committed
444
    double r = deltaR[ReferenceForce::RIndex];
445
446
447
448
449
    if (cutoff && r >= cutoffDistance)
        return;

    // Record variables for evaluating expressions.

450
451
452
    for (int i = 0; i < (int) paramIndex.size(); i++) {
        expressionSet.setVariable(particleParamIndex[i*2], atomParameters[atom1][i]);
        expressionSet.setVariable(particleParamIndex[i*2+1], atomParameters[atom2][i]);
453
    }
454
455
456
    expressionSet.setVariable(rIndex, r);
    expressionSet.setVariable(particleValueIndex[0], values[0][atom1]);
    expressionSet.setVariable(particleValueIndex[1], values[0][atom2]);
457
458
459

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

peastman's avatar
peastman committed
460
    double rinv = 1/r;
Peter Eastman's avatar
Peter Eastman committed
461
462
463
    deltaR[0] *= rinv;
    deltaR[1] *= rinv;
    deltaR[2] *= rinv;
peastman's avatar
peastman committed
464
465
    vector<double> dVdR1(valueDerivExpressions.size(), 0.0);
    vector<double> dVdR2(valueDerivExpressions.size(), 0.0);
466
    if (!isExcluded || valueTypes[0] != OpenMM::CustomGBForce::ParticlePair) {
peastman's avatar
peastman committed
467
        dVdR1[0] = valueDerivExpressions[0][0].evaluate();
Peter Eastman's avatar
Peter Eastman committed
468
        dVdR2[0] = -dVdR1[0];
469
        for (int i = 0; i < 3; i++) {
Peter Eastman's avatar
Peter Eastman committed
470
471
            forces[atom1][i] -= dEdV[0][atom1]*dVdR1[0]*deltaR[i];
            forces[atom2][i] -= dEdV[0][atom1]*dVdR2[0]*deltaR[i];
472
        }
473
    }
474
475
476
477
478
479
480
481
    for (int i = 0; i < (int) paramIndex.size(); i++)
        expressionSet.setVariable(paramIndex[i], atomParameters[atom1][i]);
    expressionSet.setVariable(valueIndex[0], values[0][atom1]);
    for (int i = 1; i < (int) valueIndex.size(); i++) {
        expressionSet.setVariable(valueIndex[i], values[i][atom1]);
        expressionSet.setVariable(xIndex, atomCoordinates[atom1][0]);
        expressionSet.setVariable(yIndex, atomCoordinates[atom1][1]);
        expressionSet.setVariable(zIndex, atomCoordinates[atom1][2]);
482
        for (int j = 0; j < i; j++) {
peastman's avatar
peastman committed
483
            double dVdV = valueDerivExpressions[i][j].evaluate();
Peter Eastman's avatar
Peter Eastman committed
484
485
            dVdR1[i] += dVdV*dVdR1[j];
            dVdR2[i] += dVdV*dVdR2[j];
486
487
        }
        for (int k = 0; k < 3; k++) {
Peter Eastman's avatar
Peter Eastman committed
488
489
            forces[atom1][k] -= dEdV[i][atom1]*dVdR1[i]*deltaR[k];
            forces[atom2][k] -= dEdV[i][atom1]*dVdR2[i]*deltaR[k];
490
        }
491
492
    }
}