Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] Add more tests for AdaptiveBeamForceFieldAndMass #43

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BeamAdapter_test/BeamInterpolation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sofa/helper/BackTrace.h>

#include <sofa/component/statecontainer/MechanicalObject.h>
#include <sofa/core/ExecParams.h>

using sofa::core::topology::BaseMeshTopology ;
using sofa::core::objectmodel::Data ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,29 @@
******************************************************************************/
#include <sofa/testing/BaseSimulationTest.h>

#include <sofa/helper/BackTrace.h>

#include <BeamAdapter/component/forcefield/AdaptiveBeamForceFieldAndMass.h>
#include <sofa/component/statecontainer/MechanicalObject.h>
using sofa::core::topology::BaseMeshTopology ;
using sofa::core::objectmodel::Data ;

#include <sofa/component/topology/container/dynamic/TetrahedronSetTopologyContainer.h>
using sofa::component::topology::container::dynamic::TetrahedronSetTopologyContainer;

using sofa::helper::WriteAccessor ;
using sofa::defaulttype::Rigid3dTypes ;

#include <sofa/simulation/common/SceneLoaderXML.h>
using sofa::simulation::SceneLoaderXML;

#include <sofa/simulation/graph/DAGSimulation.h>
using sofa::simulation::Simulation ;
using sofa::simulation::Node ;
using sofa::simulation::setSimulation ;
using sofa::core::objectmodel::New ;
using sofa::core::objectmodel::BaseData ;
using sofa::component::statecontainer::MechanicalObject ;

#include <BeamAdapter/component/forcefield/AdaptiveBeamForceFieldAndMass.h>
using sofa::component::forcefield::AdaptiveBeamForceFieldAndMass;
#include <sofa/helper/system/thread/CTime.h>

#include <string>
using std::string;

namespace sofa
{
using sofa::component::forcefield::AdaptiveBeamForceFieldAndMass;
using sofa::component::statecontainer::MechanicalObject;
using sofa::helper::system::thread::ctime_t;

using sofa::simulation::Node;
using sofa::simulation::SceneLoaderXML;

struct AdaptiveBeamForceFieldAndMassTest : public sofa::testing::BaseSimulationTest
{
using Rigid3dTypes = defaulttype::Rigid3dTypes;
using VecCoord = MechanicalObject<Rigid3dTypes>::VecCoord;

void simpleSceneTest(){
string scene =
"<?xml version='1.0'?>"
Expand All @@ -76,24 +65,173 @@ struct AdaptiveBeamForceFieldAndMassTest : public sofa::testing::BaseSimulationT
Node::SPtr root = SceneLoaderXML::loadFromMemory ( "test1", scene.c_str(), scene.size());

ASSERT_NE(root.get(), nullptr);
MechanicalObject<defaulttype::Rigid3dTypes>* mechanicalObject = nullptr;
MechanicalObject<Rigid3dTypes>* mechanicalObject = nullptr;
root->getTreeObject(mechanicalObject);

ASSERT_NE(mechanicalObject, nullptr);
EXPECT_TRUE(mechanicalObject->getName() == "DOFs") ;

AdaptiveBeamForceFieldAndMass<defaulttype::Rigid3dTypes>* beamForceFieldMass = nullptr;
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;

root->getTreeObject(beamForceFieldMass);

ASSERT_NE(beamForceFieldMass, nullptr);
ASSERT_NO_THROW(beamForceFieldMass->init());
ASSERT_NO_THROW(beamForceFieldMass->reinit());
}



Node::SPtr createSingleBeam()
{
string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 -9.81 0' dt='0.01'>"
" <RequiredPlugin name='Sofa.Component.ODESolver.Backward' />"
" <RequiredPlugin name='Sofa.Component.LinearSolver.Direct' />"
" <RequiredPlugin name='Sofa.Component.StateContainer' />"
" <RequiredPlugin name='Sofa.Component.Constraint.Projective' />"
" <RequiredPlugin name='Sofa.Component.StateContainer' />"
" <RequiredPlugin name='Sofa.Component.Topology.Container.Constant' />"
" <RequiredPlugin name='Sofa.Component.Topology.Container.Grid' />"
" <RequiredPlugin name='BeamAdapter' />"
" <DefaultAnimationLoop />"
" <DefaultVisualManagerLoop />"
" <Node name='BeamModel'/>"
" <EulerImplicitSolver rayleighStiffness='0.0' rayleighMass='0.0' />"
" <BTDLinearSolver />"
" <RegularGridTopology name='MeshLines' nx='200' ny='1' nz='1' xmax='100' xmin='0' ymin='0' ymax='0' zmax='0' zmin='0'/>"
" <MechanicalObject template='Rigid3d' name='DOFs' />"
" <FixedConstraint indices='0' />"
" <BeamInterpolation name='Interpol' radius='0.1'/>"
" <AdaptiveBeamForceFieldAndMass name='ForceField' interpolation='@Interpol' massDensity='10.0'/>"
" </Node> "
"</Node> ";

Node::SPtr root = SceneLoaderXML::loadFromMemory("singleBeam", scene.c_str(), (unsigned int)(scene.size()));
sofa::simulation::getSimulation()->init(root.get());

return root;
}


void checkCreation()
{
Node::SPtr root = createSingleBeam();

// Search for Beam FF
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;
root->getTreeObject(beamForceFieldMass);
ASSERT_NE(beamForceFieldMass, nullptr);

// Check component state and Data default values

// TODO: activate this test in component init refactoring PR
//ASSERT_EQ(beamForceFieldMass->d_componentState.getValue(), sofa::core::objectmodel::ComponentState::Valid);
ASSERT_EQ(beamForceFieldMass->d_computeMass.getValue(), true);
ASSERT_FLOAT_EQ(beamForceFieldMass->d_massDensity.getValue(), 10.0);
ASSERT_FLOAT_EQ(beamForceFieldMass->rayleighMass.getValue(), 0.0);
ASSERT_FLOAT_EQ(beamForceFieldMass->rayleighStiffness.getValue(), 0.0);
}


void checkValues()
{
int nbrStep = 500;
int nbrGrid = 200;
Node::SPtr root = createSingleBeam();

// Search for Beam FF
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;
root->getTreeObject(beamForceFieldMass);
ASSERT_NE(beamForceFieldMass, nullptr);

// Access mstate
MechanicalObject<Rigid3dTypes>* dofs = nullptr;
root->getTreeObject(dofs);

// Access dofs
const VecCoord& positions = dofs->x.getValue();
ASSERT_EQ(positions.size(), nbrGrid);

// Check position at init
auto id = nbrGrid - 1;
EXPECT_NEAR(positions[id][0], 100, 1e-4);
EXPECT_NEAR(positions[id][1], 0, 1e-4);
EXPECT_NEAR(positions[id][2], 0, 1e-4);

// run some simulation steps
auto simulation = sofa::simulation::getSimulation();
for (int i = 0; i < nbrStep; i++)
{
simulation->animate(root.get(), 0.01);
}

// Check position after simulation
EXPECT_NEAR(positions[id][0], -1.34228, 1e-4);
EXPECT_NEAR(positions[id][1], -110.274221, 1e-4);
EXPECT_NEAR(positions[id][2], 0, 1e-4);
}


void testPerformances()
{
Node::SPtr root = createSingleBeam();

int nbrStep = 1000;
int nbrTest = 10;

double diffTimeMs = 0;
double timeMin = std::numeric_limits<double>::max();
double timeMax = std::numeric_limits<double>::min();

auto simulation = sofa::simulation::getSimulation();
for (int i = 0; i < nbrTest; ++i)
{
ctime_t startTime = sofa::helper::system::thread::CTime::getRefTime();
for (int i = 0; i < nbrStep; i++)
{
simulation->animate(root.get(), 0.01);
}

ctime_t diffTime = sofa::helper::system::thread::CTime::getRefTime() - startTime;
double diffTimed = sofa::helper::system::thread::CTime::toSecond(diffTime);

if (timeMin > diffTimed)
timeMin = diffTimed;
if (timeMax < diffTimed)
timeMax = diffTimed;

diffTimeMs += diffTimed;
simulation->reset(root.get());
}

//std::cout << "timeMean: " << diffTimeMs/nbrTest << std::endl;
//std::cout << "timeMin: " << timeMin << std::endl;
//std::cout << "timeMax: " << timeMax << std::endl;

// Some logs (2022-08-08):
//timeMean: 1.00409
//timeMin : 0.977432
//timeMax : 1.02568

}
};

TEST_F(AdaptiveBeamForceFieldAndMassTest, SimpleScene) {
ASSERT_NO_THROW(this->simpleSceneTest()) ;
}

TEST_F(AdaptiveBeamForceFieldAndMassTest, checkCreation) {
ASSERT_NO_THROW(this->checkCreation());
}

TEST_F(AdaptiveBeamForceFieldAndMassTest, checkValues) {
ASSERT_NO_THROW(this->checkValues());
}

TEST_F(AdaptiveBeamForceFieldAndMassTest, DISABLED_testPerformances) {
ASSERT_NO_THROW(this->testPerformances());
}

}