/// After simulation compare the positions of points to the theoretical positions. bool compareSimulatedToTheoreticalPositions( double h, double tolerancePosition, double toleranceEnergy = 1e-13, double checkEnergyConservation=false) { int i = 0; // Init simulation sofa::simulation::getSimulation()->init(root.get()); double time = root->getTime(); // Get mechanical object simulation::Node::SPtr massNode = root->getChild("MassNode"); typename MechanicalObject::SPtr dofs = massNode->get<MechanicalObject>(root->SearchDown); // Animate do { // Record the mass position Coord p0=dofs.get()->read(sofa::core::ConstVecCoordId::position())->getValue()[0]; double absoluteError = fabs(p0[1]-positionsArray[i]); // Compare mass position to the theoretical position if( absoluteError > tolerancePosition ) { ADD_FAILURE() << "Position of mass at time " << time << " is wrong: " << std::endl <<" expected Position is " << positionsArray[i] << std::endl <<" actual Position is " << p0[1] << std::endl << "absolute error = " << absoluteError << std::endl; return false; } //Animate sofa::simulation::getSimulation()->animate(root.get(),h); time = root->getTime(); // Check if hamiltonian energy is constant when there is no damping if(checkEnergyConservation && fabs(variationalSolver->f_hamiltonianEnergy.getValue() -totalEnergy) > toleranceEnergy ) { ADD_FAILURE() << "Hamiltonian energy at time " << time << " is wrong: " << std::endl <<" expected Energy is " << totalEnergy << std::endl <<" actual Energy is " << variationalSolver->f_hamiltonianEnergy.getValue() << std::endl << "absolute error = " << fabs(variationalSolver->f_hamiltonianEnergy.getValue() -totalEnergy) << std::endl; return false; } // Iterate i++; } while (time < 2); return true; }
/// After simulation compare the positions of points to the theoretical positions. bool compareSimulatedToTheoreticalPositions(double tolerance) { // Init simulation sofa::simulation::getSimulation()->init(root.get()); double time = root->getTime(); double stiffnessSpring = 100; double mass = 10; double w = sqrt(stiffnessSpring/mass); // Get mechanical object simulation::Node::SPtr massNode = root->getChild("MassNode"); typename MechanicalObject::SPtr dofs = massNode->get<MechanicalObject>(root->SearchDown); // Animate do { // Record the mass position Coord p0=dofs.get()->read(sofa::core::ConstVecCoordId::position())->getValue()[0]; // Absolute error double absoluteError = fabs(p0[1]-(cos(w*time))); // Compare mass position to the theoretical position if( absoluteError > tolerance ) { ADD_FAILURE() << "Position of mass at time " << time << " is wrong: " << std::endl <<" expected Position is " << cos(sqrt(stiffnessSpring/mass)*time) << std::endl <<" actual Position is " << p0[1] << std::endl << "absolute error = " << absoluteError << std::endl; return false; } //Animate sofa::simulation::getSimulation()->animate(root.get(),0.001); time = root->getTime(); } while (time < 2); return true; }