void CustomIntegrator::initialize(ContextImpl& contextRef) { if (owner != NULL && &contextRef.getOwner() != owner) throw OpenMMException("This Integrator is already bound to a context"); vector<std::string> variableList; set<std::string> variableSet; variableList.insert(variableList.end(), globalNames.begin(), globalNames.end()); variableList.insert(variableList.end(), perDofNames.begin(), perDofNames.end()); for (int i = 0; i < (int) variableList.size(); i++) { string& name = variableList[i]; if (variableSet.find(name) != variableSet.end()) throw OpenMMException("The Integrator defines two variables with the same name: "+name); variableSet.insert(name); if (contextRef.getParameters().find(name) != contextRef.getParameters().end()) throw OpenMMException("The Integrator defines a variable with the same name as a Context parameter: "+name); } set<std::string> globalTargets; globalTargets.insert(globalNames.begin(), globalNames.end()); globalTargets.insert("dt"); for (map<string, double>::const_iterator iter = contextRef.getParameters().begin(); iter != contextRef.getParameters().end(); ++iter) globalTargets.insert(iter->first); for (int i = 0; i < computations.size(); i++) { if (computations[i].type == ComputeGlobal && globalTargets.find(computations[i].variable) == globalTargets.end()) throw OpenMMException("Unknown global variable: "+computations[i].variable); } context = &contextRef; owner = &contextRef.getOwner(); kernel = context->getPlatform().createKernel(IntegrateCustomStepKernel::Name(), contextRef); kernel.getAs<IntegrateCustomStepKernel>().initialize(contextRef.getSystem(), *this); kernel.getAs<IntegrateCustomStepKernel>().setGlobalVariables(contextRef, globalValues); for (int i = 0; i < (int) perDofValues.size(); i++) { if (perDofValues[i].size() == 1) perDofValues[i].resize(context->getSystem().getNumParticles(), perDofValues[i][0]); kernel.getAs<IntegrateCustomStepKernel>().setPerDofVariable(contextRef, i, perDofValues[i]); } }
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; for (int i = 0; i < (int) node.getChildren().size(); i++) children.push_back(replaceDerivFunctions(node.getChildren()[i], context)); return ExpressionTreeNode(op.clone(), children); } }
void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, vector<RealVec>& atomCoordinates, vector<RealVec>& velocities, vector<RealVec>& forces, vector<RealOpenMM>& masses, map<string, RealOpenMM>& globals, vector<vector<RealVec> >& perDof, bool& forcesAreValid, RealOpenMM tolerance) { if (invalidatesForces.size() == 0) initialize(context, masses, globals); int numSteps = stepType.size(); globals.insert(context.getParameters().begin(), context.getParameters().end()); for (map<string, RealOpenMM>::const_iterator iter = globals.begin(); iter != globals.end(); ++iter) expressionSet.setVariable(expressionSet.getVariableIndex(iter->first), iter->second); oldPos = atomCoordinates; // Loop over steps and execute them. for (int step = 0; step < numSteps; ) { if ((needsForces[step] || needsEnergy[step]) && (!forcesAreValid || context.getLastForceGroups() != forceGroupFlags[step])) { // Recompute forces and/or energy. bool computeForce = needsForces[step] || computeBothForceAndEnergy[step]; bool computeEnergy = needsEnergy[step] || computeBothForceAndEnergy[step]; recordChangedParameters(context, globals); RealOpenMM e = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroupFlags[step]); if (computeEnergy) { energy = e; context.getEnergyParameterDerivatives(energyParamDerivs); } forcesAreValid = true; } // Execute the step. int nextStep = step+1; switch (stepType[step]) { case CustomIntegrator::ComputeGlobal: { uniform = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber(); gaussian = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(); RealOpenMM result = stepExpressions[step][0].evaluate(); globals[stepVariable[step]] = result; expressionSet.setVariable(stepVariableIndex[step], result); break; } case CustomIntegrator::ComputePerDof: { vector<RealVec>* results = NULL; if (stepVariableIndex[step] == xIndex) results = &atomCoordinates; else if (stepVariableIndex[step] == vIndex) results = &velocities; else { for (int j = 0; j < integrator.getNumPerDofVariables(); j++) if (stepVariableIndex[step] == perDofVariableIndex[j]) results = &perDof[j]; } if (results == NULL) throw OpenMMException("Illegal per-DOF output variable: "+stepVariable[step]); computePerDof(numberOfAtoms, *results, atomCoordinates, velocities, forces, masses, perDof, stepExpressions[step][0]); break; } case CustomIntegrator::ComputeSum: { computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, forces, masses, perDof, stepExpressions[step][0]); RealOpenMM sum = 0.0; for (int j = 0; j < numberOfAtoms; j++) if (masses[j] != 0.0) sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2]; globals[stepVariable[step]] = sum; expressionSet.setVariable(stepVariableIndex[step], sum); break; } case CustomIntegrator::ConstrainPositions: { getReferenceConstraintAlgorithm()->apply(oldPos, atomCoordinates, inverseMasses, tolerance); oldPos = atomCoordinates; break; } case CustomIntegrator::ConstrainVelocities: { getReferenceConstraintAlgorithm()->applyToVelocities(oldPos, velocities, inverseMasses, tolerance); break; } case CustomIntegrator::UpdateContextState: { recordChangedParameters(context, globals); context.updateContextState(); globals.insert(context.getParameters().begin(), context.getParameters().end()); for (map<string, RealOpenMM>::const_iterator iter = globals.begin(); iter != globals.end(); ++iter) expressionSet.setVariable(expressionSet.getVariableIndex(iter->first), iter->second); break; } case CustomIntegrator::IfBlockStart: { if (!evaluateCondition(step)) nextStep = blockEnd[step]+1; break; } case CustomIntegrator::WhileBlockStart: { if (!evaluateCondition(step)) nextStep = blockEnd[step]+1; break; } case CustomIntegrator::BlockEnd: { if (blockEnd[step] != -1) nextStep = blockEnd[step]; // Return to the start of a while block. break; } } if (invalidatesForces[step]) forcesAreValid = false; step = nextStep; } ReferenceVirtualSites::computePositions(context.getSystem(), atomCoordinates); incrementTimeStep(); recordChangedParameters(context, globals); }
void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, vector<RealVec>& atomCoordinates, vector<RealVec>& velocities, vector<RealVec>& forces, vector<RealOpenMM>& masses, map<string, RealOpenMM>& globals, vector<vector<RealVec> >& perDof, bool& forcesAreValid, RealOpenMM tolerance) { int numSteps = stepType.size(); globals.insert(context.getParameters().begin(), context.getParameters().end()); oldPos = atomCoordinates; if (invalidatesForces.size() == 0) { // 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 this method is called. vector<int> forceGroup; vector<vector<Lepton::ParsedExpression> > expressions; CustomIntegratorUtilities::analyzeComputations(context, integrator, expressions, comparisons, blockEnd, invalidatesForces, needsForces, needsEnergy, computeBothForceAndEnergy, forceGroup); stepExpressions.resize(expressions.size()); for (int i = 0; i < numSteps; i++) { for (int j = 0; j < (int) expressions[i].size(); j++) stepExpressions[i].push_back(expressions[i][j].createProgram()); if (stepType[i] == CustomIntegrator::BeginWhileBlock) blockEnd[blockEnd[i]] = i; // Record where to branch back to. } // Record the variable names and flags for the force and energy in each step. forceGroupFlags.resize(numSteps, -1); forceName.resize(numSteps, "f"); energyName.resize(numSteps, "energy"); vector<string> forceGroupName; vector<string> energyGroupName; for (int i = 0; i < 32; i++) { stringstream fname; fname << "f" << i; forceGroupName.push_back(fname.str()); stringstream ename; ename << "energy" << i; energyGroupName.push_back(ename.str()); } for (int i = 0; i < numSteps; i++) { if (needsForces[i] && forceGroup[i] > -1) forceName[i] = forceGroupName[forceGroup[i]]; if (needsEnergy[i] && forceGroup[i] > -1) energyName[i] = energyGroupName[forceGroup[i]]; if (forceGroup[i] > -1) forceGroupFlags[i] = 1<<forceGroup[i]; } // Build the list of inverse masses. 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]; } } // Loop over steps and execute them. for (int step = 0; step < numSteps; ) { if ((needsForces[step] || needsEnergy[step]) && (!forcesAreValid || context.getLastForceGroups() != forceGroupFlags[step])) { // Recompute forces and/or energy. bool computeForce = needsForces[step] || computeBothForceAndEnergy[step]; bool computeEnergy = needsEnergy[step] || computeBothForceAndEnergy[step]; recordChangedParameters(context, globals); RealOpenMM e = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroupFlags[step]); if (computeEnergy) energy = e; forcesAreValid = true; } globals[energyName[step]] = energy; // Execute the step. int nextStep = step+1; switch (stepType[step]) { case CustomIntegrator::ComputeGlobal: { map<string, RealOpenMM> variables = globals; variables["uniform"] = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber(); variables["gaussian"] = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(); globals[stepVariable[step]] = stepExpressions[step][0].evaluate(variables); break; } case CustomIntegrator::ComputePerDof: { vector<RealVec>* results = NULL; if (stepVariable[step] == "x") results = &atomCoordinates; else if (stepVariable[step] == "v") results = &velocities; else { for (int j = 0; j < integrator.getNumPerDofVariables(); j++) if (stepVariable[step] == integrator.getPerDofVariableName(j)) results = &perDof[j]; } if (results == NULL) throw OpenMMException("Illegal per-DOF output variable: "+stepVariable[step]); computePerDof(numberOfAtoms, *results, atomCoordinates, velocities, forces, masses, globals, perDof, stepExpressions[step][0], forceName[step]); break; } case CustomIntegrator::ComputeSum: { computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, forces, masses, globals, perDof, stepExpressions[step][0], forceName[step]); RealOpenMM sum = 0.0; for (int j = 0; j < numberOfAtoms; j++) if (masses[j] != 0.0) sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2]; globals[stepVariable[step]] = sum; break; } case CustomIntegrator::ConstrainPositions: { getReferenceConstraintAlgorithm()->apply(oldPos, atomCoordinates, inverseMasses, tolerance); oldPos = atomCoordinates; break; } case CustomIntegrator::ConstrainVelocities: { getReferenceConstraintAlgorithm()->applyToVelocities(oldPos, velocities, inverseMasses, tolerance); break; } case CustomIntegrator::UpdateContextState: { recordChangedParameters(context, globals); context.updateContextState(); globals.insert(context.getParameters().begin(), context.getParameters().end()); break; } case CustomIntegrator::BeginIfBlock: { if (!evaluateCondition(step, globals)) nextStep = blockEnd[step]+1; break; } case CustomIntegrator::BeginWhileBlock: { if (!evaluateCondition(step, globals)) nextStep = blockEnd[step]+1; break; } case CustomIntegrator::EndBlock: { if (blockEnd[step] != -1) nextStep = blockEnd[step]; // Return to the start of a while block. break; } } if (invalidatesForces[step]) forcesAreValid = false; step = nextStep; } ReferenceVirtualSites::computePositions(context.getSystem(), atomCoordinates); incrementTimeStep(); recordChangedParameters(context, globals); }
void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, vector<RealVec>& atomCoordinates, vector<RealVec>& velocities, vector<RealVec>& forces, vector<RealOpenMM>& masses, map<string, RealOpenMM>& globals, vector<vector<RealVec> >& perDof, bool& forcesAreValid, RealOpenMM tolerance){ int numSteps = stepType.size(); globals.insert(context.getParameters().begin(), context.getParameters().end()); oldPos = atomCoordinates; if (invalidatesForces.size() == 0) { // The first time this is called, work out when to recompute forces and energy. First build a // list of every step that invalidates the forces. invalidatesForces.resize(numSteps, false); needsForces.resize(numSteps, false); needsEnergy.resize(numSteps, false); forceGroup.resize(numSteps, -2); forceName.resize(numSteps, "f"); energyName.resize(numSteps, "energy"); set<string> affectsForce; affectsForce.insert("x"); for (vector<ForceImpl*>::const_iterator iter = context.getForceImpls().begin(); iter != context.getForceImpls().end(); ++iter) { const map<string, double> params = (*iter)->getDefaultParameters(); for (map<string, double>::const_iterator param = params.begin(); param != params.end(); ++param) affectsForce.insert(param->first); } for (int i = 0; i < numSteps; i++) invalidatesForces[i] = (stepType[i] == CustomIntegrator::ConstrainPositions || affectsForce.find(stepVariable[i]) != affectsForce.end()); // Make a list of which steps require valid forces or energy to be known. vector<string> forceGroupName; vector<string> energyGroupName; for (int i = 0; i < 32; i++) { stringstream fname; fname << "f" << i; forceGroupName.push_back(fname.str()); stringstream ename; ename << "energy" << i; energyGroupName.push_back(ename.str()); } for (int i = 0; i < numSteps; i++) { if (stepType[i] == CustomIntegrator::ComputeGlobal || stepType[i] == CustomIntegrator::ComputePerDof || stepType[i] == CustomIntegrator::ComputeSum) { for (int j = 0; j < stepExpression[i].getNumOperations(); j++) { const Lepton::Operation& op = stepExpression[i].getOperation(j); if (op.getId() == Lepton::Operation::VARIABLE) { if (op.getName() == "energy") { if (forceGroup[i] != -2) throw OpenMMException("A single computation step cannot depend on multiple force groups"); needsEnergy[i] = true; forceGroup[i] = -1; } else if (op.getName().substr(0, 6) == "energy") { for (int k = 0; k < (int) energyGroupName.size(); k++) if (op.getName() == energyGroupName[k]) { if (forceGroup[i] != -2) throw OpenMMException("A single computation step cannot depend on multiple force groups"); needsForces[i] = true; forceGroup[i] = 1<<k; energyName[i] = energyGroupName[k]; break; } } else if (op.getName() == "f") { if (forceGroup[i] != -2) throw OpenMMException("A single computation step cannot depend on multiple force groups"); needsForces[i] = true; forceGroup[i] = -1; } else if (op.getName()[0] == 'f') { for (int k = 0; k < (int) forceGroupName.size(); k++) if (op.getName() == forceGroupName[k]) { if (forceGroup[i] != -2) throw OpenMMException("A single computation step cannot depend on multiple force groups"); needsForces[i] = true; forceGroup[i] = 1<<k; forceName[i] = forceGroupName[k]; break; } } } } } } // Build the list of inverse masses. 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]; } } // Loop over steps and execute them. for (int i = 0; i < numSteps; i++) { if ((needsForces[i] || needsEnergy[i]) && (!forcesAreValid || context.getLastForceGroups() != forceGroup[i])) { // Recompute forces and or energy. Figure out what is actually needed // between now and the next time they get invalidated again. bool computeForce = false, computeEnergy = false; for (int j = i; ; j++) { if (needsForces[j]) computeForce = true; if (needsEnergy[j]) computeEnergy = true; if (invalidatesForces[j]) break; if (j == numSteps-1) j = -1; if (j == i-1) break; } recordChangedParameters(context, globals); RealOpenMM e = context.calcForcesAndEnergy(computeForce, computeEnergy, forceGroup[i]); if (computeEnergy) energy = e; forcesAreValid = true; } globals[energyName[i]] = energy; // Execute the step. switch (stepType[i]) { case CustomIntegrator::ComputeGlobal: { map<string, RealOpenMM> variables = globals; variables["uniform"] = SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber(); variables["gaussian"] = SimTKOpenMMUtilities::getNormallyDistributedRandomNumber(); globals[stepVariable[i]] = stepExpression[i].evaluate(variables); break; } case CustomIntegrator::ComputePerDof: { vector<RealVec>* results = NULL; if (stepVariable[i] == "x") results = &atomCoordinates; else if (stepVariable[i] == "v") results = &velocities; else { for (int j = 0; j < integrator.getNumPerDofVariables(); j++) if (stepVariable[i] == integrator.getPerDofVariableName(j)) results = &perDof[j]; } if (results == NULL) throw OpenMMException("Illegal per-DOF output variable: "+stepVariable[i]); computePerDof(numberOfAtoms, *results, atomCoordinates, velocities, forces, masses, globals, perDof, stepExpression[i], forceName[i]); break; } case CustomIntegrator::ComputeSum: { computePerDof(numberOfAtoms, sumBuffer, atomCoordinates, velocities, forces, masses, globals, perDof, stepExpression[i], forceName[i]); RealOpenMM sum = 0.0; for (int j = 0; j < numberOfAtoms; j++) if (masses[j] != 0.0) sum += sumBuffer[j][0]+sumBuffer[j][1]+sumBuffer[j][2]; globals[stepVariable[i]] = sum; break; } case CustomIntegrator::ConstrainPositions: { getReferenceConstraintAlgorithm()->apply(oldPos, atomCoordinates, inverseMasses, tolerance); oldPos = atomCoordinates; break; } case CustomIntegrator::ConstrainVelocities: { getReferenceConstraintAlgorithm()->applyToVelocities(oldPos, velocities, inverseMasses, tolerance); break; } case CustomIntegrator::UpdateContextState: { recordChangedParameters(context, globals); context.updateContextState(); globals.insert(context.getParameters().begin(), context.getParameters().end()); } } if (invalidatesForces[i]) forcesAreValid = false; } ReferenceVirtualSites::computePositions(context.getSystem(), atomCoordinates); incrementTimeStep(); recordChangedParameters(context, globals); }