Example #1
0
void apply(const std::string& /*directory*/, std::vector<std::string>& files, bool /*reinit*/)
{


    sofa::simulation::Simulation* simulation = sofa::simulation::getSimulation();

    //Launch the comparison for each scenes
    for (unsigned int i = 0; i < files.size(); ++i)
    {

        const std::string& currentFile = files[i];
        GNode::SPtr groot = sofa::core::objectmodel::SPtr_dynamic_cast<GNode> (simulation->load(currentFile.c_str()));
        if (groot == NULL)
        {
            std::cerr << "CANNOT open " << currentFile << " !" << std::endl;
            continue;
        }
        std::cout << "sec. Loading " << currentFile << std::endl;

        //Save the initial time
        clock_t curtime = clock();
        simulation->init(groot.get());
        double t = static_cast<double>(clock() - curtime) / (float)CLOCKS_PER_SEC;
        std::cout << "Init time " << t << " sec." << std::endl;

        //Clear and prepare for next scene
        simulation->unload(groot);
        groot.reset();
    }
}
Example #2
0
void apply(const std::string& directory, std::vector<std::string>& files,
        unsigned int iterations, bool reinit, bool useTopology)
{

    sofa::component::init(); // ensures all components are initialized, also introduce a dependency to all libraries, avoiding problems with -Wl,--as-needed flag

    sofa::simulation::Simulation* simulation = sofa::simulation::getSimulation();

    //Launch the comparison for each scenes
    for (unsigned int i = 0; i < files.size(); ++i)
    {

        const std::string& currentFile = files[i];
        GNode::SPtr groot = sofa::core::objectmodel::SPtr_dynamic_cast<GNode> (simulation->load(currentFile.c_str()));
        if (groot == NULL)
        {
            std::cerr << "CANNOT open " << currentFile << " !" << std::endl;
            continue;
        }

        simulation->init(groot.get());

        //Filename where the simulation of the current scene will be saved (in Sofa/applications/projects/sofaVerification/simulation/)
        std::string refFile;
        if(directory.empty())
        {
            refFile += SetDirectory::GetParentDir(DataRepository.getFirstPath().c_str());
            refFile += "/applications/projects/sofaVerification/simulation/";
            refFile += SetDirectory::GetFileName(currentFile.c_str());
        }
        else
        {
            refFile += directory;
            refFile += '/';
            refFile += SetDirectory::GetFileName(currentFile.c_str());
        }

        //If we initialize the system, we add only WriteState components, to store the reference states
        if (reinit)
        {
            if (useTopology)
            {
                sofa::component::misc::WriteTopologyCreator writeVisitor(sofa::core::ExecParams::defaultInstance());

                writeVisitor.setCreateInMapping(true);
                writeVisitor.setSceneName(refFile);
                writeVisitor.execute(groot.get());

                sofa::component::misc::WriteTopologyActivator v_write(sofa::core::ExecParams::defaultInstance() /* PARAMS FIRST */, true);
                v_write.execute(groot.get());
            }
            else
            {
                sofa::component::misc::WriteStateCreator writeVisitor(sofa::core::ExecParams::defaultInstance());

                writeVisitor.setCreateInMapping(true);
                writeVisitor.setSceneName(refFile);
                writeVisitor.execute(groot.get());

                sofa::component::misc::WriteStateActivator v_write(sofa::core::ExecParams::defaultInstance() /* PARAMS FIRST */, true);
                v_write.execute(groot.get());
            }

        }
        else
        {
            if (useTopology)
            {
                //We add CompareTopology components: as it derives from the ReadTopology, we use the ReadTopologyActivator to enable them.
                sofa::component::misc::CompareTopologyCreator compareVisitor(sofa::core::ExecParams::defaultInstance());
                compareVisitor.setCreateInMapping(true);
                compareVisitor.setSceneName(refFile);
                compareVisitor.execute(groot.get());

                sofa::component::misc::ReadTopologyActivator v_read(sofa::core::ExecParams::defaultInstance(),true);
                v_read.execute(groot.get());
            }
            else
            {
                //We add CompareState components: as it derives from the ReadState, we use the ReadStateActivator to enable them.
                sofa::component::misc::CompareStateCreator compareVisitor(sofa::core::ExecParams::defaultInstance());
                compareVisitor.setCreateInMapping(true);
                compareVisitor.setSceneName(refFile);
                compareVisitor.execute(groot.get());

                sofa::component::misc::ReadStateActivator v_read(sofa::core::ExecParams::defaultInstance() /* PARAMS FIRST */, true);
                v_read.execute(groot.get());
            }
        }

        //Do as many iterations as specified in entry of the program. At each step, the compare state will compare the computed states to the recorded states
        std::cout << "Computing " << iterations << " for " << currentFile << std::endl;

        //Save the initial time
        clock_t curtime = clock();
        for (unsigned int i = 0; i < iterations; i++)
        {
            simulation->animate(groot.get());
        }
        double t = static_cast<double>(clock() - curtime) / CLOCKS_PER_SEC;

        std::cout << "ITERATIONS " << iterations << " TIME " << t << " seconds ";

        //We read the final error: the summation of all the error made at each time step
        if (!reinit)
        {
            if (useTopology)
            {
                sofa::component::misc::CompareTopologyResult result(sofa::core::ExecParams::defaultInstance());
                result.execute(groot.get());
                std::cout << "ERROR " << result.getTotalError() << ' ';

                const std::vector<unsigned int>& listResult = result.getErrors();
                if (listResult.size() != 5)
                {
                    std::cerr
                            << "ERROR while reading detail of errors by topological element."
                                    << std::endl;
                    break;
                }

                std::cout
                        << "ERROR by element type "
                                << " EDGES "      << static_cast<double>(listResult[0])
                                / result.getNumCompareTopology()
                                << " TRIANGLES "  << static_cast<double>(listResult[1])
                                / result.getNumCompareTopology()
                                << " QUADS "      << static_cast<double>(listResult[2])
                                / result.getNumCompareTopology()
                                << " TETRAHEDRA " << static_cast<double>(listResult[3])
                                / result.getNumCompareTopology()
                                << " HEXAHEDRA "  << static_cast<double>(listResult[4])
                                / result.getNumCompareTopology();
            }
            else
            {
                sofa::component::misc::CompareStateResult result(sofa::core::ExecParams::defaultInstance());
                result.execute(groot.get());
                std::cout
                        << "ERROR " << result.getTotalError() << " ERRORBYDOF "
                                << static_cast<double>(result.getErrorByDof())
                                / result.getNumCompareState() << std::endl;
            }
        }
        std::cout << std::endl;

        //Clear and prepare for next scene
        simulation->unload(groot.get());
        groot.reset();
    }
}