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
/**---------------------------------------------------------------------------------------

   ReferenceCustomDynamics constructor

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

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

ReferenceCustomDynamics::ReferenceCustomDynamics(int numberOfAtoms, const CustomIntegrator& integrator) : 
87
           ReferenceDynamics(numberOfAtoms, integrator.getStepSize(), 0.0), initialized(false), 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

    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]));
198
199

    initialized = true;
200
201
}

202
203
204
205
206
207
208
209
210
211
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
212
213
        for (auto& child : node.getChildren())
            children.push_back(replaceDerivFunctions(child, context));
214
215
216
217
        return ExpressionTreeNode(op.clone(), children);
    }
}

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

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

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

peastman's avatar
peastman committed
361
362
363
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) {
364
    // Loop over all degrees of freedom.
365

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

372
373
374
375
376
                x = atomCoordinates[i][j];
                v = velocities[i][j];
                f = forces[i][j];
                uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber();
                gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
377
                for (int k = 0; k < (int) perDof.size(); k++)
378
                    perDofVariable[k] = perDof[k][i][j];
379
                results[i][j] = expression.evaluate();
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
408
409
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);
        }
    }
}

410
bool ReferenceCustomDynamics::evaluateCondition(int step) {
411
412
    uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber();
    gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
413
414
    double lhs = stepExpressions[step][0].evaluate();
    double rhs = stepExpressions[step][1].evaluate();
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    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");
}

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

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

   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
460
461
462
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) {
463
    if (!initialized)
464
        initialize(context, masses, globals);
465
    globals.insert(context.getParameters().begin(), context.getParameters().end());
peastman's avatar
peastman committed
466
467
    for (auto& global : globals)
        expressionSet.setVariable(expressionSet.getVariableIndex(global.first), global.second);
468
    computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, forces, masses, perDof, kineticEnergyExpression);
peastman's avatar
peastman committed
469
    double sum = 0.0;
470
471
472
473
474
    for (int j = 0; j < numberOfAtoms; j++)
        if (masses[j] != 0.0)
            sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2];
    return sum;
}