<commit_before>#include <plugins/Compliant/numericalsolver/MinresSolver.h>
#include <plugins/Compliant/numericalsolver/LDLTSolver.h>
#include <plugins/Compliant/Compliant_test/Compliant_test.h>
#include <plugins/Compliant/odesolver/AssembledSolver.h>
#include <sofa/component/odesolver/EulerSolver.h>
#include <plugins/SceneCreator/SceneCreator.h>
using namespace sofa::modeling;


struct AssembledSolver_test : public CompliantSolver_test
{

    /** @defgroup AssembledSolver_Unit_Tests AssembledSolver basic tests.
     *
     * The scene is composed of two particles connected by a spring. One particle is fixed, while the other has an initial velocity.
     * The solver is set to backward Euler: alpha = beta = 1.
     */
    ///@{

    /** Test in the linear case, with velocity parallel to the spring.
      Convergence should occur in one iteration.
      Integrate using backward Euler (alpha=1, beta=1).
      Post-condition: an explicit Euler step with step -dt brings the system back to the original state.
      */
    void testLinearOneFixedOneStiffnessSpringV100(bool debug)
    {
        SReal dt=0.1; // currently, this must be set in the root node AND passed to the animate function
        Node::SPtr root = clearScene();
        root->setGravity( Vec3(0,0,0) );
        root->setDt(dt);

        // The solver
        using odesolver::AssembledSolver;
        AssembledSolver::SPtr complianceSolver = addNew<AssembledSolver>(getRoot());
        complianceSolver->debug.setValue(debug);
        complianceSolver->alpha.setValue(1.0);
        complianceSolver->beta.setValue(1.0);
        SReal precision = 1.0e-6;

        linearsolver::LDLTSolver::SPtr linearSolver = addNew<linearsolver::LDLTSolver>(getRoot());
        linearSolver->debug.setValue(debug);

        // The string
        ParticleString  string1( root, Vec3(0,0,0), Vec3(1,0,0), 2, 1.0*2 ); // two particles
        string1.compliance->isCompliance.setValue(false); // handle it as a stiffness
        string1.compliance->compliance.setValue(1.0e-3);

        FixedConstraint3::SPtr fixed = addNew<FixedConstraint3>(string1.string_node,"fixedConstraint");
        fixed->addConstraint(0);      // attach first particle

        // velocity parallel to the spring
        {
        MechanicalObject3::WriteVecCoord v = string1.DOF->writeVelocities();
        v[1] = Vec3(1,0,0);
        }


        //**************************************************
        sofa::simulation::getSimulation()->init(root.get());
        //**************************************************

        // initial state
        Vector x0 = getVector( core::VecId::position() );
        Vector v0 = getVector( core::VecId::velocity() );

        //**************************************************
        sofa::simulation::getSimulation()->animate(root.get(),dt);
        //**************************************************

        Vector x1 = getVector( core::VecId::position() );
        Vector v1 = getVector( core::VecId::velocity() );

        // Replace the backward (i.e. implicit) Euler solver with a forward (i.e. explicit) Euler solver.
        // An integration step using the opposite dt should bring us back to the initial state
        getRoot()->removeObject(complianceSolver);
        odesolver::EulerSolver::SPtr eulerSolver = New<odesolver::EulerSolver>();
        getRoot()->addObject(eulerSolver);
        eulerSolver->symplectic.setValue(false);
        sofa::simulation::getSimulation()->animate(root.get(),-dt);

        Vector x2 = getVector( core::VecId::position() );
        Vector v2 = getVector( core::VecId::velocity() );

//        cerr<<"AssembledSolver_test, initial positions : " << x0.transpose() << endl;
//        cerr<<"AssembledSolver_test, initial velocities: " << v0.transpose() << endl;
//        cerr<<"AssembledSolver_test, new positions : " << x1.transpose() << endl;
//        cerr<<"AssembledSolver_test, new velocities: " << v1.transpose() << endl;
//        cerr<<"AssembledSolver_test, new positions  after backward integration: " << x2.transpose() << endl;
//        cerr<<"AssembledSolver_test, new velocities after backward integration: " << v2.transpose() << endl;

        ASSERT_TRUE( (x2-x0).lpNorm<Eigen::Infinity>() < precision );
        ASSERT_TRUE( (v2-v0).lpNorm<Eigen::Infinity>() < precision );
    }

    /// Same as @testLinearOneFixedOneStiffnessSpringV100, with a compliant spring instead of a stiff spring
    void testLinearOneFixedOneComplianceSpringV100( bool debug )
    {
        SReal dt=0.1; // currently, this must be set in the root node AND passed to the animate function
        Node::SPtr root = clearScene();
        root->setGravity( Vec3(0,0,0) );
        root->setDt(dt);

        // The solver
        using odesolver::AssembledSolver;
        AssembledSolver::SPtr complianceSolver = addNew<AssembledSolver>(root);
        complianceSolver->debug.setValue( debug );
        complianceSolver->alpha.setValue(1.0);
        complianceSolver->beta.setValue(1.0);
        SReal precision = 1.0e-6;

        linearsolver::LDLTSolver::SPtr linearSolver = addNew<linearsolver::LDLTSolver>(root);
        linearSolver->debug.setValue(debug);

        // The string
        ParticleString  string1( root, Vec3(0,0,0), Vec3(1,0,0), 2, 1.0*2 ); // two particles
        string1.compliance->isCompliance.setValue(true);
        string1.compliance->compliance.setValue(1.0e-3);
<commit_msg>r11173/sofa : Fix test failure<commit_after>#include <plugins/Compliant/numericalsolver/MinresSolver.h>
#include <plugins/Compliant/numericalsolver/LDLTSolver.h>
#include <plugins/Compliant/Compliant_test/Compliant_test.h>
#include <plugins/Compliant/odesolver/AssembledSolver.h>
#include <sofa/component/odesolver/EulerSolver.h>
#include <plugins/SceneCreator/SceneCreator.h>
using namespace sofa::modeling;


struct AssembledSolver_test : public CompliantSolver_test
{

    /** @defgroup AssembledSolver_Unit_Tests AssembledSolver basic tests.
     *
     * The scene is composed of two particles connected by a spring. One particle is fixed, while the other has an initial velocity.
     * The solver is set to backward Euler: alpha = beta = 1.
     */
    ///@{

    /** Test in the linear case, with velocity parallel to the spring.
      Convergence should occur in one iteration.
      Integrate using backward Euler (alpha=1, beta=1).
      Post-condition: an explicit Euler step with step -dt brings the system back to the original state.
      */
    void testLinearOneFixedOneStiffnessSpringV100(bool debug)
    {
        SReal dt=0.1; // currently, this must be set in the root node AND passed to the animate function
        Node::SPtr root = clearScene();
        root->setGravity( Vec3(0,0,0) );
        root->setDt(dt);

        // The solver
        using odesolver::AssembledSolver;
        AssembledSolver::SPtr complianceSolver = addNew<AssembledSolver>(getRoot());
        complianceSolver->debug.setValue(debug);
        complianceSolver->alpha.setValue(1.0);
        complianceSolver->beta.setValue(1.0);
        SReal precision = 1.0e-6;

        linearsolver::LDLTSolver::SPtr linearSolver = addNew<linearsolver::LDLTSolver>(getRoot());
        linearSolver->debug.setValue(debug);

        // The string
        ParticleString  string1( root, Vec3(0,0,0), Vec3(1,0,0), 2, 1.0*2 ); // two particles
        string1.compliance->isCompliance.setValue(false); // handle it as a stiffness
        string1.compliance->compliance.setValue(1.0e-3);

        FixedConstraint3::SPtr fixed = addNew<FixedConstraint3>(string1.string_node,"fixedConstraint");
        fixed->addConstraint(0);      // attach first particle

        // velocity parallel to the spring
        {
        MechanicalObject3::WriteVecCoord v = string1.DOF->writeVelocities();
        v[1] = Vec3(1,0,0);
        }


        //**************************************************
        sofa::simulation::getSimulation()->init(root.get());
        //**************************************************

        // initial state
        Vector x0 = getVector( core::VecId::position() );
        Vector v0 = getVector( core::VecId::velocity() );

        //**************************************************
        sofa::simulation::getSimulation()->animate(root.get(),dt);
        //**************************************************

        Vector x1 = getVector( core::VecId::position() );
        Vector v1 = getVector( core::VecId::velocity() );

        // An explicit integration step using the opposite dt should bring us back to the initial state
        string1.compliance->isCompliance.setValue(false); string1.compliance->reinit(); // switch the spring as stiffness
        core::MechanicalParams mparams;
        simulation::common::MechanicalOperations mop (&mparams,getRoot()->getContext());
        mop.computeForce( 0+dt, core::VecId::force(), core::VecId::position(), core::VecId::velocity(), false );
        Vector f1 = modeling::getVector( core::VecId::force() );
        Vector v2 = v1 - f1 * dt;
        Vector x2 = x1 - v1 * dt;

        if( debug ){
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, time step : " << dt << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, initial positions : " << x0.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, initial velocities: " << v0.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, new positions : " << x1.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, new velocities: " << v1.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, new forces: " << f1.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, new positions  after backward integration: " << x2.transpose() << endl;
            cerr<<"AssembledSolver_test::testLinearOneFixedOneStiffnessSpringV100, new velocities after backward integration: " << v2.transpose() << endl;
        }

        ASSERT_TRUE( (x2-x0).lpNorm<Eigen::Infinity>() < precision );
        ASSERT_TRUE( (v2-v0).lpNorm<Eigen::Infinity>() < precision );
    }

    /// Same as @testLinearOneFixedOneStiffnessSpringV100, with a compliant spring instead of a stiff spring
    void testLinearOneFixedOneComplianceSpringV100( bool debug )
    {
        SReal dt=0.1; // currently, this must be set in the root node AND passed to the animate function
        Node::SPtr root = clearScene();
        root->setGravity( Vec3(0,0,0) );
        root->setDt(dt);

        // The solver
        using odesolver::AssembledSolver;
        AssembledSolver::SPtr complianceSolver = addNew<AssembledSolver>(root);
        complianceSolver->debug.setValue( debug );
        complianceSolver->alpha.setValue(1.0);
        complianceSolver->beta.setValue(1.0);
        SReal precision = 1.0e-6;

        linearsolver::LDLTSolver::SPtr linearSolver = addNew<linearsolver::LDLTSolver>(root);
        linearSolver->debug.setValue(debug);

        // The string
        ParticleString  string1( root, Vec3(0,0,0), Vec3(1,0,0), 2, 1.0*2 ); // two particles
        string1.compliance->isCompliance.setValue(true);
        string1.compliance->compliance.setValue(1.0e-3);

{'fraction_non_alphanumeric': 0.15520869870220974, 'fraction_numerical': 0.02104524728165556, 'mean_word_length': 3.359709480122324, 'pattern_counts': {'":': 0, '<': 118, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3317043', 'n_tokens_mistral': 3449, 'n_tokens_neox': 3278, 'n_words': 717}