ReferenceCustomDynamics::ReferenceCustomDynamics(int numberOfAtoms, const CustomIntegrator& integrator) : ReferenceDynamics(numberOfAtoms, integrator.getStepSize(), 0.0), integrator(integrator) { sumBuffer.resize(numberOfAtoms); oldPos.resize(numberOfAtoms); 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); } kineticEnergyExpression = Lepton::Parser::parse(integrator.getKineticEnergyExpression()).optimize().createProgram(); kineticEnergyNeedsForce = false; for (int i = 0; i < kineticEnergyExpression.getNumOperations(); i++) { const Lepton::Operation& op = kineticEnergyExpression.getOperation(i); if (op.getId() == Lepton::Operation::VARIABLE && op.getName() == "f") kineticEnergyNeedsForce = true; } }
void testSerializeCustomIntegrator() { CustomIntegrator *intg = new CustomIntegrator(0.002234); intg->addPerDofVariable("temp",0); vector<Vec3> initialValues(123); for(int i = 0; i < 123; i++) initialValues[i] = Vec3(i+0.1, i+0.2, i+0.3); intg->setPerDofVariable(0, initialValues); intg->addPerDofVariable("oldx", 0); intg->addComputePerDof("v", "v+dt*f/m"); intg->addComputePerDof("oldx", "x"); intg->addComputePerDof("x", "x+dt*v"); intg->addConstrainPositions(); intg->addComputePerDof("v", "(x-oldx)/dt"); intg->addUpdateContextState(); intg->addConstrainVelocities(); intg->addComputeSum("summand", "x*x+v*v"); intg->addPerDofVariable("outf", 0); intg->addPerDofVariable("outf1", 0); intg->addPerDofVariable("outf2", 0); intg->addGlobalVariable("oute", 0); intg->addGlobalVariable("oute1", 0); intg->addGlobalVariable("oute2", 0); intg->addComputePerDof("outf", "f"); intg->addComputePerDof("outf1", "f1"); intg->addComputePerDof("outf2", "f2"); intg->addComputeGlobal("oute", "energy"); intg->addComputeGlobal("oute1", "energy1"); intg->addComputeGlobal("oute2", "energy2"); intg->addUpdateContextState(); intg->addConstrainVelocities(); intg->addComputeSum("summand2", "v*v+f*f"); intg->setConstraintTolerance(1e-5); intg->setKineticEnergyExpression("m*v1*v1/2; v1=v+0.5*dt*f/m"); stringstream ss; XmlSerializer::serialize<Integrator>(intg, "CustomIntegrator", ss); CustomIntegrator *intg2 = dynamic_cast<CustomIntegrator*>(XmlSerializer::deserialize<Integrator>(ss)); ASSERT_EQUAL(intg->getNumGlobalVariables(), intg->getNumGlobalVariables()); for (int i = 0; i < intg->getNumGlobalVariables(); i++) { ASSERT_EQUAL(intg->getGlobalVariable(i), intg2->getGlobalVariable(i)); ASSERT_EQUAL(intg->getGlobalVariableName(i), intg2->getGlobalVariableName(i)); } ASSERT_EQUAL(intg->getNumPerDofVariables(), intg2->getNumPerDofVariables()); for(int i = 0; i < intg->getNumPerDofVariables(); i++) { vector<Vec3> vars1; intg->getPerDofVariable(i, vars1); vector<Vec3> vars2; intg2->getPerDofVariable(i, vars2); ASSERT_EQUAL(vars1.size(),vars2.size()); for (int j = 0; j < (int) vars1.size(); j++) { ASSERT_EQUAL(vars1[j][0], vars2[j][0]); ASSERT_EQUAL(vars1[j][1], vars2[j][1]); ASSERT_EQUAL(vars1[j][2], vars2[j][2]); } } ASSERT_EQUAL(intg->getNumComputations(), intg2->getNumComputations()); for(int i=0; i<intg->getNumComputations(); i++) { CustomIntegrator::ComputationType type1, type2; string variable1, variable2; string expression1, expression2; intg->getComputationStep(i, type1, variable1, expression1); intg2->getComputationStep(i, type2, variable2, expression2); ASSERT_EQUAL(type1, type2); ASSERT_EQUAL(variable1, variable2); ASSERT_EQUAL(expression1, expression2); } ASSERT_EQUAL(intg->getKineticEnergyExpression(), intg2->getKineticEnergyExpression()); ASSERT_EQUAL(intg->getRandomNumberSeed(), intg2->getRandomNumberSeed()); ASSERT_EQUAL(intg->getStepSize(), intg2->getStepSize()); ASSERT_EQUAL(intg->getConstraintTolerance(), intg2->getConstraintTolerance()); delete intg; delete intg2; }