SOFA API  4ecb558f
Open source framework for multi-physics simuation
sofa::ForceField_test< _ForceFieldType > Struct Template Reference

#include <ForceFieldTestCreation.h>

Helper for writing ForceField tests. The constructor creates a root node and adds it a State and a ForceField (of the paremeter type of this template class). Pointers to node, state and force are available. Deriving the ForceField test from this class makes it easy to write: just call function run_test with positions, velocities and the corresponding expected forces. This function automatically checks not only the forces (function addForce), but also the stiffness (methods addDForce and addKToMatrix), using finite differences. More...

Inheritance diagram for sofa::ForceField_test< _ForceFieldType >:

Detailed Description

template<typename _ForceFieldType>
struct sofa::ForceField_test< _ForceFieldType >

Helper for writing ForceField tests. The constructor creates a root node and adds it a State and a ForceField (of the paremeter type of this template class). Pointers to node, state and force are available. Deriving the ForceField test from this class makes it easy to write: just call function run_test with positions, velocities and the corresponding expected forces. This function automatically checks not only the forces (function addForce), but also the stiffness (methods addDForce and addKToMatrix), using finite differences.

Author
Fran├žois Faure, 2014

Public Attributes

Scene elements

{

DOF::SPtr dof
 
ForceField::SPtr force
 
simulation::Node::SPtr node
 
Precision and control parameters

}

{

SReal errorMax
 tolerance in precision test. The actual value is this one times the epsilon of the Real numbers (typically float or double) More...
 
SReal errorFactorPotentialEnergy
 The test for potential energy is successfull if the (infinite norm of the) difference is less than errorFactorPotentialEnergy * errorMax *epsilon (default = 1) More...
 
std::pair< Real, RealdeltaRange
 Minimum/Maximum amplitudes of the random perturbation used to check the stiffness using finite differences. More...
 
bool checkStiffness
 If false, stops the test after checking the force, without checking the stiffness. Default value is true. More...
 
bool debug
 
- Public Attributes inherited from sofa::testing::BaseTest
sofa::testing::MessageAsTestFailure m_fatal
 
sofa::testing::MessageAsTestFailure m_error
 

Tested API

}

{

unsigned char flags
 
static const unsigned char TEST_POTENTIAL_ENERGY = 1
 testing getPotentialEnergy function. The tests will only work with conservative forces (if dissipative forces such as viscosity or damping are computed, the test is wrong) More...
 
static const unsigned char TEST_ALL = UCHAR_MAX
 testing everything More...
 
 ForceField_test ()
 } More...
 
 ForceField_test (std::string filename)
 
void run_test (const VecCoord &x, const VecDeriv &v, const VecDeriv &ef, bool initScene=true)
 Given positions and velocities, checks that the expected forces are obtained, and that a small change of positions generates the corresponding change of forces. More...
 

Additional Inherited Members

- Static Public Attributes inherited from sofa::testing::BaseTest
static int seed = (unsigned int)time(nullptr)
 Seed value. More...
 
- Public Member Functions inherited from sofa::testing::BaseSimulationTest
 BaseSimulationTest ()
 
bool importPlugin (const std::string &name)
 
- Public Member Functions inherited from sofa::testing::BaseTest
 BaseTest ()
 Initialize Sofa and the random number generator. More...
 
 ~BaseTest () override
 
virtual void onSetUp ()
 
virtual void onTearDown ()
 
- Public Member Functions inherited from sofa::testing::NumericTest< _ForceFieldType::DataTypes::Real >
 NumericTest ()
 
Real vectorMaxDiff (const Container1 &c1, const Container2 &c2)
 Return the maximum difference between two containers. Issues a failure if sizes are different. More...
 
Real vectorMaxAbs (const Container &c)
 Return the maximum absolute value of a container. More...
 
- Static Public Member Functions inherited from sofa::testing::NumericTest< _ForceFieldType::DataTypes::Real >
static Real matrixMaxDiff (const Matrix1 &m1, const Matrix2 &m2)
 return the maximum difference between corresponding entries, or the infinity if the matrices have different sizes More...
 
static Real matrixMaxDiff (const sofa::type::Mat< M, N, Real > &m1, const Matrix2 &m2)
 Return the maximum difference between corresponding entries, or the infinity if the matrices have different sizes. More...
 
static Real epsilon ()
 the smallest real number More...
 
static Real infinity ()
 Infinity. More...
 
static bool isSmall (Real r, Real ratio=1.)
 true if the magnitude of r is less than ratio*epsilon More...
 
static Real vectorMaxDiff (const sofa::type::Vec< N, Real > &m1, const Vector2 &m2)
 return the maximum difference between corresponding entries, or the infinity if the vectors have different sizes More...
 
static Real vectorMaxDiff (const sofa::type::Vec< N, Real > &m1, const sofa::type::Vec< N, Real > &m2)
 return the maximum difference between corresponding entries More...
 
- Static Protected Member Functions inherited from sofa::testing::NumericTest< _ForceFieldType::DataTypes::Real >
static float norm (float a)
 
static double norm (double a)
 
static Real norm (T a)
 

Attribute details

◆ checkStiffness

template<typename _ForceFieldType >
bool sofa::ForceField_test< _ForceFieldType >::checkStiffness

If false, stops the test after checking the force, without checking the stiffness. Default value is true.

◆ debug

template<typename _ForceFieldType >
bool sofa::ForceField_test< _ForceFieldType >::debug

Print debug messages. Default is false.

◆ deltaRange

template<typename _ForceFieldType >
std::pair<Real,Real> sofa::ForceField_test< _ForceFieldType >::deltaRange

Minimum/Maximum amplitudes of the random perturbation used to check the stiffness using finite differences.

Warning
Should be more than errorMax/stiffness. This is not checked automatically.

◆ dof

template<typename _ForceFieldType >
DOF::SPtr sofa::ForceField_test< _ForceFieldType >::dof

◆ errorFactorPotentialEnergy

template<typename _ForceFieldType >
SReal sofa::ForceField_test< _ForceFieldType >::errorFactorPotentialEnergy

The test for potential energy is successfull if the (infinite norm of the) difference is less than errorFactorPotentialEnergy * errorMax *epsilon (default = 1)

◆ errorMax

template<typename _ForceFieldType >
SReal sofa::ForceField_test< _ForceFieldType >::errorMax

tolerance in precision test. The actual value is this one times the epsilon of the Real numbers (typically float or double)

◆ flags

template<typename _ForceFieldType >
unsigned char sofa::ForceField_test< _ForceFieldType >::flags

testing options. (all by default). To be used with precaution.

◆ force

template<typename _ForceFieldType >
ForceField::SPtr sofa::ForceField_test< _ForceFieldType >::force

◆ node

template<typename _ForceFieldType >
simulation::Node::SPtr sofa::ForceField_test< _ForceFieldType >::node

◆ TEST_ALL

template<typename _ForceFieldType >
const unsigned char sofa::ForceField_test< _ForceFieldType >::TEST_ALL = UCHAR_MAX
static

testing everything

◆ TEST_POTENTIAL_ENERGY

template<typename _ForceFieldType >
const unsigned char sofa::ForceField_test< _ForceFieldType >::TEST_POTENTIAL_ENERGY = 1
static

testing getPotentialEnergy function. The tests will only work with conservative forces (if dissipative forces such as viscosity or damping are computed, the test is wrong)

Constructor details

◆ ForceField_test() [1/2]

template<typename _ForceFieldType >
sofa::ForceField_test< _ForceFieldType >::ForceField_test ( )
inline

}

Create a scene with a node, a state and a forcefield.;

◆ ForceField_test() [2/2]

template<typename _ForceFieldType >
sofa::ForceField_test< _ForceFieldType >::ForceField_test ( std::string  filename)
inline

Create a scene from a xml file.

Function details

◆ run_test()

template<typename _ForceFieldType >
void sofa::ForceField_test< _ForceFieldType >::run_test ( const VecCoord x,
const VecDeriv v,
const VecDeriv ef,
bool  initScene = true 
)
inline

Given positions and velocities, checks that the expected forces are obtained, and that a small change of positions generates the corresponding change of forces.

Parameters
xpositions
vvelocities
efexpected forces This function first checks that the expected forces are obtained. Then, it checks getPotentialEnergy. And then, it checks the stiffness, unless member checkStiffness is set to false. A new position is created using a small random change, and the new force is computed. The change of potential energy is compared to the dot product between displacement and force. The change of force is compared to the change computed by function addDForce, and to the product of the position change with the stiffness matrix.