ReferenceCustomDynamics.cpp 21.5 KB
Newer Older
1

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

25
#include "SimTKOpenMMUtilities.h"
26
#include "ReferenceVirtualSites.h"
27
#include "ReferenceCustomDynamics.h"
28
#include "ReferenceTabulatedFunction.h"
29
#include "openmm/OpenMMException.h"
30
31
32
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/ForceImpl.h"
#include "lepton/Operation.h"
33
34
#include "lepton/ParsedExpression.h"
#include "lepton/Parser.h"
35
#include <set>
36
#include <sstream>
37
38
39

using namespace std;
using namespace OpenMM;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using namespace Lepton;

class ReferenceCustomDynamics::DerivFunction : public CustomFunction {
public:
    DerivFunction(map<string, double>& energyParamDerivs, const string& param) : energyParamDerivs(energyParamDerivs), param(param) {
    }
    int getNumArguments() const {
        return 0;
    }
    double evaluate(const double* arguments) const {
        return energyParamDerivs[param];
    }
    double evaluateDerivative(const double* arguments, const int* derivOrder) const {
        return 0;
    }
    CustomFunction* clone() const {
        return new DerivFunction(energyParamDerivs, param);
    }
private:
    map<string, double>& energyParamDerivs;
    string param;
};
62

63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * Determine whether a parsed expression involves any vector functions.
 */
static bool isVectorExpression(const ExpressionTreeNode& node) {
    const Lepton::Operation& op = node.getOperation();
    if (op.getId() == Lepton::Operation::CUSTOM)
        if (op.getName() == "dot" || op.getName() == "cross" || op.getName() == "vector" || op.getName() == "_x" || op.getName() == "_y" || op.getName() == "_z")
            return true;
    for (auto& child : node.getChildren())
        if (isVectorExpression(child))
            return true;
    return false;
}

77
78
79
80
81
82
83
84
85
86
87
/**---------------------------------------------------------------------------------------

   ReferenceCustomDynamics constructor

   @param numberOfAtoms  number of atoms
   @param integrator     the integrator definition to use

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

ReferenceCustomDynamics::ReferenceCustomDynamics(int numberOfAtoms, const CustomIntegrator& integrator) : 
           ReferenceDynamics(numberOfAtoms, integrator.getStepSize(), 0.0), integrator(integrator) {
88
    sumBuffer.resize(numberOfAtoms);
89
    oldPos.resize(numberOfAtoms);
90
91
92
93
94
    stepType.resize(integrator.getNumComputations());
    stepVariable.resize(integrator.getNumComputations());
    for (int i = 0; i < integrator.getNumComputations(); i++) {
        string expression;
        integrator.getComputationStep(i, stepType[i], stepVariable[i], expression);
95
    }
96
97
98
99
100
101
102
103
104
105
106
}

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

   ReferenceCustomDynamics destructor

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

ReferenceCustomDynamics::~ReferenceCustomDynamics() {
}

peastman's avatar
peastman committed
107
void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<double>& masses, map<string, double>& globals) {
108
109
110
    // Some initialization can't be done in the constructor, since we need a ContextImpl from which to get the list of
    // Context parameters.  Instead, we do it the first time update() or computeKineticEnergy() is called.

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    std::map<std::string, double*> variableLocations;
    variableLocations["x"] = &x;
    variableLocations["v"] = &v;
    variableLocations["m"] = &m;
    variableLocations["f"] = &f;
    variableLocations["energy"] = &energy;
    variableLocations["gaussian"] = &gaussian;
    variableLocations["uniform"] = &uniform;
    perDofVariable.resize(integrator.getNumPerDofVariables());
    for (int i = 0; i < integrator.getNumPerDofVariables(); i++)
        variableLocations[integrator.getPerDofVariableName(i)] = &perDofVariable[i];
    for (int i = 0; i < 32; i++) {
        stringstream fname;
        fname << "f" << i;
        variableLocations[fname.str()] = &f;
        stringstream ename;
        ename << "energy" << i;
        variableLocations[ename.str()] = &energy;
    }
    
131
132
133
134
135
136
    // Create custom functions for the tabulated functions.

    map<string, Lepton::CustomFunction*> functions;
    for (int i = 0; i < integrator.getNumTabulatedFunctions(); i++)
        functions[integrator.getTabulatedFunctionName(i)] = createReferenceTabulatedFunction(integrator.getTabulatedFunction(i));
    
137
138
    // Parse the expressions.
    
139
140
    int numSteps = stepType.size();
    vector<int> forceGroup;
141
    vector<vector<ParsedExpression> > expressions;
142
    CustomIntegratorUtilities::analyzeComputations(context, integrator, expressions, comparisons, blockEnd, invalidatesForces, needsForces, needsEnergy, computeBothForceAndEnergy, forceGroup, functions);
143
    stepExpressions.resize(expressions.size());
144
    stepVectorExpressions.resize(expressions.size());
145
146
147
    for (int i = 0; i < numSteps; i++) {
        stepExpressions[i].resize(expressions[i].size());
        for (int j = 0; j < (int) expressions[i].size(); j++) {
148
149
150
151
152
153
154
155
            ParsedExpression parsed(replaceDerivFunctions(expressions[i][j].getRootNode(), context));
            if (isVectorExpression(parsed.getRootNode()))
                stepVectorExpressions[i].push_back(VectorExpression(parsed));
            else {
                stepExpressions[i][j] = parsed.createCompiledExpression();
                stepExpressions[i][j].setVariableLocations(variableLocations);
                expressionSet.registerExpression(stepExpressions[i][j]);
            }
156
157
158
159
        }
        if (stepType[i] == CustomIntegrator::WhileBlockStart)
            blockEnd[blockEnd[i]] = i; // Record where to branch back to.
    }
160
161
162
163
164
165
    kineticEnergyExpression = Parser::parse(integrator.getKineticEnergyExpression()).optimize().createCompiledExpression();
    kineticEnergyExpression.setVariableLocations(variableLocations);
    expressionSet.registerExpression(kineticEnergyExpression);
    kineticEnergyNeedsForce = false;
    if (kineticEnergyExpression.getVariables().find("f") != kineticEnergyExpression.getVariables().end())
        kineticEnergyNeedsForce = true;
166

167
168
169
170
171
    // Delete the custom functions.

    for (auto& function : functions)
        delete function.second;

172
    // Record the force group flags for each step.
173

174
    forceGroupFlags.resize(numSteps, integrator.getIntegrationForceGroups());
175
    for (int i = 0; i < numSteps; i++)
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        if (forceGroup[i] > -1)
            forceGroupFlags[i] = 1<<forceGroup[i];

    // Build the list of inverse masses.

    int numberOfAtoms = masses.size();
    inverseMasses.resize(numberOfAtoms);
    for (int i = 0; i < numberOfAtoms; i++) {
        if (masses[i] == 0.0)
            inverseMasses[i] = 0.0;
        else
            inverseMasses[i] = 1.0/masses[i];
    }

190
    // Record indices of variables.
191
192
193
194
195
196
197
198
199

    xIndex = expressionSet.getVariableIndex("x");
    vIndex = expressionSet.getVariableIndex("v");
    for (int i = 0; i < integrator.getNumPerDofVariables(); i++)
        perDofVariableIndex.push_back(expressionSet.getVariableIndex(integrator.getPerDofVariableName(i)));
    for (int i = 0; i < stepVariable.size(); i++)
        stepVariableIndex.push_back(expressionSet.getVariableIndex(stepVariable[i]));
}

200
201
202
203
204
205
206
207
208
209
ExpressionTreeNode ReferenceCustomDynamics::replaceDerivFunctions(const ExpressionTreeNode& node, ContextImpl& context) {
    const Operation& op = node.getOperation();
    if (op.getId() == Operation::CUSTOM && op.getName() == "deriv") {
        string param = node.getChildren()[1].getOperation().getName();
        if (context.getParameters().find(param) == context.getParameters().end())
            throw OpenMMException("The second argument to deriv() must be a context parameter");
        return ExpressionTreeNode(new Operation::Custom("deriv", new DerivFunction(energyParamDerivs, param)));
    }
    else {
        vector<ExpressionTreeNode> children;
peastman's avatar
peastman committed
210
211
        for (auto& child : node.getChildren())
            children.push_back(replaceDerivFunctions(child, context));
212
213
214
215
        return ExpressionTreeNode(op.clone(), children);
    }
}

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**---------------------------------------------------------------------------------------

   Update -- driver routine for performing Custom dynamics update of coordinates
   and velocities

   @param context             the context this integrator is updating
   @param numberOfAtoms       number of atoms
   @param atomCoordinates     atom coordinates
   @param velocities          velocities
   @param forces              forces
   @param masses              atom masses
   @param globals             a map containing values of global variables
   @param forcesAreValid      whether the current forces are valid or need to be recomputed

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

peastman's avatar
peastman committed
232
233
234
void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, vector<Vec3>& atomCoordinates,
                                     vector<Vec3>& velocities, vector<Vec3>& forces, vector<double>& masses,
                                     map<string, double>& globals, vector<vector<Vec3> >& perDof, bool& forcesAreValid, double tolerance) {
235
236
    if (invalidatesForces.size() == 0)
        initialize(context, masses, globals);
237
238
    int numSteps = stepType.size();
    globals.insert(context.getParameters().begin(), context.getParameters().end());
peastman's avatar
peastman committed
239
240
    for (auto& global : globals)
        expressionSet.setVariable(expressionSet.getVariableIndex(global.first), global.second);
241
    oldPos = atomCoordinates;
242
243
244
245
    map<int, double> groupEnergy;
    map<int, vector<Vec3> > groupForces;
    if (forcesAreValid)
        groupForces[context.getLastForceGroups()] = forces;
246
247
248
    
    // Loop over steps and execute them.
    
249
    for (int step = 0; step < numSteps; ) {
250
251
        int flags = forceGroupFlags[step];
        if ((needsForces[step] && groupForces.find(flags) == groupForces.end()) || (needsEnergy[step] && groupEnergy.find(flags) == groupEnergy.end())) {
252
            // Recompute forces and/or energy.
253
            
254
255
            bool computeForce = needsForces[step] || computeBothForceAndEnergy[step];
            bool computeEnergy = needsEnergy[step] || computeBothForceAndEnergy[step];
256
            recordChangedParameters(context, globals);
peastman's avatar
peastman committed
257
            double e = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroupFlags[step]);
258
259
            if (computeForce)
                groupForces[flags] = forces;
260
            if (computeEnergy) {
261
                groupEnergy[flags] = e;
262
263
                context.getEnergyParameterDerivatives(energyParamDerivs);
            }
264
265
266
267
            forcesAreValid = true;
        }
        
        // Execute the step.
268

269
270
        energy = (needsEnergy[step] ? groupEnergy[flags] : 0);
        vector<Vec3>& stepForces = (needsForces[step] ? groupForces[flags] : forces);
271
        int nextStep = step+1;
272
        bool stepInvalidatesForces = invalidatesForces[step];
273
        switch (stepType[step]) {
274
            case CustomIntegrator::ComputeGlobal: {
275
276
                uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber();
                gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
peastman's avatar
peastman committed
277
                double result = stepExpressions[step][0].evaluate();
278
279
                globals[stepVariable[step]] = result;
                expressionSet.setVariable(stepVariableIndex[step], result);
280
281
282
                break;
            }
            case CustomIntegrator::ComputePerDof: {
peastman's avatar
peastman committed
283
                vector<Vec3>* results = NULL;
284
                if (stepVariableIndex[step] == xIndex)
285
                    results = &atomCoordinates;
286
                else if (stepVariableIndex[step] == vIndex)
287
288
289
                    results = &velocities;
                else {
                    for (int j = 0; j < integrator.getNumPerDofVariables(); j++)
290
                        if (stepVariableIndex[step] == perDofVariableIndex[j])
291
292
293
                            results = &perDof[j];
                }
                if (results == NULL)
294
                    throw OpenMMException("Illegal per-DOF output variable: "+stepVariable[step]);
295
296
297
298
                if (stepVectorExpressions[step].size() > 0)
                    computePerParticle(numberOfAtoms, *results, atomCoordinates, velocities, stepForces, masses, perDof, globals, stepVectorExpressions[step][0]);
                else
                    computePerDof(numberOfAtoms, *results, atomCoordinates, velocities, stepForces, masses, perDof, stepExpressions[step][0]);
299
300
301
                break;
            }
            case CustomIntegrator::ComputeSum: {
302
303
304
305
                if (stepVectorExpressions[step].size() > 0)
                    computePerParticle(numberOfAtoms, sumBuffer, atomCoordinates, velocities, stepForces, masses, perDof, globals, stepVectorExpressions[step][0]);
                else
                    computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, stepForces, masses, perDof, stepExpressions[step][0]);
peastman's avatar
peastman committed
306
                double sum = 0.0;
307
                for (int j = 0; j < numberOfAtoms; j++)
308
309
                    if (masses[j] != 0.0)
                        sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2];
310
                globals[stepVariable[step]] = sum;
311
                expressionSet.setVariable(stepVariableIndex[step], sum);
312
313
314
                break;
            }
            case CustomIntegrator::ConstrainPositions: {
315
                getReferenceConstraintAlgorithm()->apply(oldPos, atomCoordinates, inverseMasses, tolerance);
316
                oldPos = atomCoordinates;
317
318
319
                break;
            }
            case CustomIntegrator::ConstrainVelocities: {
320
                getReferenceConstraintAlgorithm()->applyToVelocities(oldPos, velocities, inverseMasses, tolerance);
321
                break;
322
323
324
            }
            case CustomIntegrator::UpdateContextState: {
                recordChangedParameters(context, globals);
325
                stepInvalidatesForces = context.updateContextState();
326
                globals.insert(context.getParameters().begin(), context.getParameters().end());
peastman's avatar
peastman committed
327
328
                for (auto& global : globals)
                    expressionSet.setVariable(expressionSet.getVariableIndex(global.first), global.second);
329
330
                break;
            }
331
            case CustomIntegrator::IfBlockStart: {
332
                if (!evaluateCondition(step))
333
334
335
                    nextStep = blockEnd[step]+1;
                break;
            }
336
            case CustomIntegrator::WhileBlockStart: {
337
                if (!evaluateCondition(step))
338
339
340
                    nextStep = blockEnd[step]+1;
                break;
            }
341
            case CustomIntegrator::BlockEnd: {
342
343
344
                if (blockEnd[step] != -1)
                    nextStep = blockEnd[step]; // Return to the start of a while block.
                break;
345
346
            }
        }
347
        if (stepInvalidatesForces) {
348
            forcesAreValid = false;
349
350
351
            groupForces.clear();
            groupEnergy.clear();
        }
352
        step = nextStep;
353
    }
354
    ReferenceVirtualSites::computePositions(context.getSystem(), atomCoordinates);
355
356
357
358
    incrementTimeStep();
    recordChangedParameters(context, globals);
}

peastman's avatar
peastman committed
359
360
361
void ReferenceCustomDynamics::computePerDof(int numberOfAtoms, vector<Vec3>& results, const vector<Vec3>& atomCoordinates,
              const vector<Vec3>& velocities, const vector<Vec3>& forces, const vector<double>& masses,
              const vector<vector<Vec3> >& perDof, const CompiledExpression& expression) {
362
    // Loop over all degrees of freedom.
363

364
    for (int i = 0; i < numberOfAtoms; i++) {
365
        if (masses[i] != 0.0) {
366
            m = masses[i];
367
368
369
            for (int j = 0; j < 3; j++) {
                // Compute the expression.

370
371
372
373
374
                x = atomCoordinates[i][j];
                v = velocities[i][j];
                f = forces[i][j];
                uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber();
                gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
375
                for (int k = 0; k < (int) perDof.size(); k++)
376
                    perDofVariable[k] = perDof[k][i][j];
377
                results[i][j] = expression.evaluate();
378
            }
379
380
381
382
        }
    }
}

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
void ReferenceCustomDynamics::computePerParticle(int numberOfAtoms, vector<Vec3>& results, const vector<Vec3>& atomCoordinates,
              const vector<Vec3>& velocities, const vector<Vec3>& forces, const vector<double>& masses,
              const vector<vector<Vec3> >& perDof, const map<string, double>& globals, const VectorExpression& expression) {
    // Loop over all degrees of freedom.

    map<string, Vec3> variables;
    for (auto& entry : globals)
        variables[entry.first] = Vec3(entry.second, entry.second, entry.second);
    for (int i = 0; i < numberOfAtoms; i++) {
        if (masses[i] != 0.0) {
            variables["m"] = Vec3(masses[i], masses[i], masses[i]);
            variables["x"] = atomCoordinates[i];
            variables["v"] = velocities[i];
            variables["f"] = forces[i];
            variables["uniform"] = Vec3(SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber(),
                    SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber(), SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber());
            variables["gaussian"] = Vec3(SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(),
                    SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(), SimTKOpenMMUtilities::getNormallyDistributedRandomNumber());
            for (int j = 0; j < perDof.size(); j++)
                variables[integrator.getPerDofVariableName(j)] = perDof[j][i];
            results[i] = expression.evaluate(variables);
        }
    }
}

408
bool ReferenceCustomDynamics::evaluateCondition(int step) {
409
410
    uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber();
    gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
411
412
    double lhs = stepExpressions[step][0].evaluate();
    double rhs = stepExpressions[step][1].evaluate();
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    switch (comparisons[step]) {
        case CustomIntegratorUtilities::EQUAL:
            return (lhs == rhs);
        case CustomIntegratorUtilities::LESS_THAN:
            return (lhs < rhs);
        case CustomIntegratorUtilities::GREATER_THAN:
            return (lhs > rhs);
        case CustomIntegratorUtilities::NOT_EQUAL:
            return (lhs != rhs);
        case CustomIntegratorUtilities::LESS_THAN_OR_EQUAL:
            return (lhs <= rhs);
        case CustomIntegratorUtilities::GREATER_THAN_OR_EQUAL:
            return (lhs >= rhs);
    }
    throw OpenMMException("ReferenceCustomDynamics: Invalid comparison operator");
}

430
431
432
/**
 * Check which context parameters have changed and register them with the context.
 */
peastman's avatar
peastman committed
433
void ReferenceCustomDynamics::recordChangedParameters(OpenMM::ContextImpl& context, std::map<std::string, double>& globals) {
peastman's avatar
peastman committed
434
435
    for (auto& param : context.getParameters()) {
        string name = param.first;
436
        double value = globals[name];
peastman's avatar
peastman committed
437
        if (value != param.second)
438
439
440
            context.setParameter(name, globals[name]);
    }
}
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

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

   Compute the kinetic energy of the system.

   @param context             the context this integrator is updating
   @param numberOfAtoms       number of atoms
   @param atomCoordinates     atom coordinates
   @param velocities          velocities
   @param forces              forces
   @param masses              atom masses
   @param globals             a map containing values of global variables
   @param perDof              the values of per-DOF variables
   @param forcesAreValid      whether the current forces are valid or need to be recomputed

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

peastman's avatar
peastman committed
458
459
460
double ReferenceCustomDynamics::computeKineticEnergy(OpenMM::ContextImpl& context, int numberOfAtoms, std::vector<OpenMM::Vec3>& atomCoordinates,
        std::vector<OpenMM::Vec3>& velocities, std::vector<OpenMM::Vec3>& forces, std::vector<double>& masses,
        std::map<std::string, double>& globals, std::vector<std::vector<OpenMM::Vec3> >& perDof, bool& forcesAreValid) {
461
462
    if (invalidatesForces.size() == 0)
        initialize(context, masses, globals);
463
    globals.insert(context.getParameters().begin(), context.getParameters().end());
peastman's avatar
peastman committed
464
465
    for (auto& global : globals)
        expressionSet.setVariable(expressionSet.getVariableIndex(global.first), global.second);
466
    computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, forces, masses, perDof, kineticEnergyExpression);
peastman's avatar
peastman committed
467
    double sum = 0.0;
468
469
470
471
472
    for (int j = 0; j < numberOfAtoms; j++)
        if (masses[j] != 0.0)
            sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2];
    return sum;
}