EulerImplicitSolver.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <sofa/component/odesolver/EulerImplicitSolver.h>
00029 #include <sofa/simulation/common/MechanicalVisitor.h>
00030 #include <sofa/core/ObjectFactory.h>
00031 #include <math.h>
00032 #include <iostream>
00033 #include "sofa/helper/system/thread/CTime.h"
00034
00035 using std::cerr;
00036 using std::endl;
00037
00038 namespace sofa
00039 {
00040
00041 namespace component
00042 {
00043
00044 namespace odesolver
00045 {
00046
00047 using namespace sofa::defaulttype;
00048 using namespace core::componentmodel::behavior;
00049
00050 EulerImplicitSolver::EulerImplicitSolver()
00051 : f_rayleighStiffness( initData(&f_rayleighStiffness,0.1,"rayleighStiffness","Rayleigh damping coefficient related to stiffness") )
00052 , f_rayleighMass( initData(&f_rayleighMass,0.1,"rayleighMass","Rayleigh damping coefficient related to mass"))
00053 , f_velocityDamping( initData(&f_velocityDamping,0.,"vdamping","Velocity decay coefficient (no decay if null)") )
00054 , f_verbose( initData(&f_verbose,false,"verbose","Dump system state at each iteration") )
00055 {
00056 }
00057
00058 void EulerImplicitSolver::solve(double dt)
00059 {
00060 MultiVector pos(this, VecId::position());
00061 MultiVector vel(this, VecId::velocity());
00062 MultiVector f(this, VecId::force());
00063 MultiVector b(this, VecId::V_DERIV);
00064
00065
00066
00067
00068 MultiVector x(this, VecId::V_DERIV);
00069
00070 double h = dt;
00071
00072 const bool verbose = f_verbose.getValue();
00073
00074 addSeparateGravity(dt);
00075
00076
00077
00078 #if 1
00079
00080
00081
00082 computeForce(b, true, false);
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 addMBKv(b, (f_rayleighMass.getValue() == 0.0 ? 0.0 : -f_rayleighMass.getValue()), 0, h+f_rayleighStiffness.getValue(), false, true);
00095
00096 #else
00097
00098
00099 computeForce(b);
00100
00101
00102
00103 computeDfV(f);
00104 b.peq(f,h+f_rayleighStiffness.getValue());
00105
00106 if (f_rayleighMass.getValue() != 0.0)
00107 {
00108
00109
00110
00111
00112 addMdx(b,vel,-f_rayleighMass.getValue());
00113 }
00114
00115 #endif
00116
00117 b.teq(h);
00118
00119 if( verbose )
00120 cerr<<"EulerImplicitSolver, f0 = "<< b <<endl;
00121
00122 projectResponse(b);
00123
00124 if( verbose )
00125 cerr<<"EulerImplicitSolver, projected f0 = "<< b <<endl;
00126
00127 MultiMatrix matrix(this);
00128 matrix = MechanicalMatrix::K * (-h*(h+f_rayleighStiffness.getValue())) + MechanicalMatrix::M * (1+h*f_rayleighMass.getValue());
00129
00130
00131
00132
00133 matrix.solve(x, b);
00134
00135
00136
00137
00138 #ifdef SOFA_NO_VMULTIOP // unoptimized version
00139 vel.peq( x );
00140 pos.peq( vel, h );
00141 #else // single-operation optimization
00142 {
00143 simulation::MechanicalVMultiOpVisitor vmop;
00144 vmop.ops.resize(2);
00145 vmop.ops[0].first = (VecId)vel;
00146 vmop.ops[0].second.push_back(std::make_pair((VecId)vel,1.0));
00147 vmop.ops[0].second.push_back(std::make_pair((VecId)x,1.0));
00148 vmop.ops[1].first = (VecId)pos;
00149 vmop.ops[1].second.push_back(std::make_pair((VecId)pos,1.0));
00150 vmop.ops[1].second.push_back(std::make_pair((VecId)vel,h));
00151 vmop.execute(this->getContext());
00152 }
00153 #endif
00154 if (f_velocityDamping.getValue()!=0.0)
00155 vel *= exp(-h*f_velocityDamping.getValue());
00156
00157 if( verbose )
00158 {
00159 cerr<<"EulerImplicitSolver, final x = "<< pos <<endl;
00160 cerr<<"EulerImplicitSolver, final v = "<< vel <<endl;
00161 }
00162 }
00163
00164 SOFA_DECL_CLASS(EulerImplicitSolver)
00165
00166 int EulerImplicitSolverClass = core::RegisterObject("Implicit time integrator using backward Euler scheme")
00167 .add< EulerImplicitSolver >()
00168 .addAlias("EulerImplicit");
00169 ;
00170
00171 }
00172
00173 }
00174
00175 }
00176