MatrixLinearSolver.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 #include <sofa/simulation/common/MatrixLinearSolver.h>
00026 #include <sofa/simulation/common/MechanicalVisitor.h>
00027 #include <sofa/simulation/common/MechanicalMatrixVisitor.h>
00028 #include <sofa/simulation/common/MechanicalVPrintVisitor.h>
00029 #include <sofa/simulation/common/VelocityThresholdVisitor.h>
00030 #include <sofa/core/componentmodel/behavior/LinearSolver.h>
00031
00032 #include <stdlib.h>
00033 #include <math.h>
00034
00035 namespace sofa
00036 {
00037
00038 namespace simulation
00039 {
00040
00041
00042 using sofa::core::componentmodel::behavior::LinearSolver;
00043 using sofa::core::objectmodel::BaseContext;
00044
00045 void GraphScatteredMatrix::apply(GraphScatteredVector& res, GraphScatteredVector& x)
00046 {
00047
00048 #if 1
00049
00050 parent->propagateDxAndResetDf(x,res);
00051 parent->addMBKdx(res,mFact,bFact,kFact, false);
00052
00053 #else
00054 parent->propagateDx(x);
00055 parent->computeDf(res);
00056
00057 if (kFact != 1.0)
00058 res *= kFact;
00059
00060
00061 if (mFact == 1.0)
00062 {
00063 parent->addMdx(res);
00064 }
00065 else if (mFact != 0.0)
00066 {
00067 parent->addMdx(res,SolverImpl::VecId(),mFact);
00068 }
00069
00070
00072 #endif
00073
00074
00075
00076 parent->projectResponse(res);
00077 }
00078
00079 template<>
00080 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::resetSystem()
00081 {
00082 if (!systemMatrix) systemMatrix = new GraphScatteredMatrix(this);
00083 if (!systemRHVector) systemRHVector = new GraphScatteredVector(this,VecId());
00084 if (!systemLHVector) systemLHVector = new GraphScatteredVector(this,VecId());
00085 systemRHVector->reset();
00086 systemLHVector->reset();
00087 solutionVecId = VecId();
00088 needInvert = true;
00089 }
00090
00091 template<>
00092 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::resizeSystem(int)
00093 {
00094 resetSystem();
00095 }
00096
00097 template<>
00098 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::setSystemMBKMatrix(double mFact, double bFact, double kFact)
00099 {
00100 resetSystem();
00101 systemMatrix->setMBKFacts(mFact, bFact, kFact);
00102 }
00103
00104 template<>
00105 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::setSystemRHVector(VecId v)
00106 {
00107 systemRHVector->set(v);
00108 }
00109
00110 template<>
00111 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::setSystemLHVector(VecId v)
00112 {
00113 solutionVecId = v;
00114 systemLHVector->set(v);
00115 }
00116
00117 template<>
00118 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::solveSystem()
00119 {
00120 if (needInvert)
00121 {
00122 this->invert(*systemMatrix);
00123 needInvert = false;
00124 }
00125 this->solve(*systemMatrix, *systemLHVector, *systemRHVector);
00126 }
00127
00128 template<>
00129 GraphScatteredMatrix* MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::createMatrix()
00130 {
00131 return new GraphScatteredMatrix(this);
00132 }
00133
00134 template<>
00135 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::deleteMatrix(GraphScatteredMatrix* v)
00136 {
00137 delete v;
00138 }
00139
00140 template<>
00141 GraphScatteredVector* MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::createVector()
00142 {
00143 return new GraphScatteredVector(this);
00144 }
00145
00146 template<>
00147 void MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::deleteVector(GraphScatteredVector* v)
00148 {
00149 delete v;
00150 }
00151
00152 template<>
00153 defaulttype::BaseMatrix* MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::getSystemBaseMatrix() { return NULL; }
00154
00155 template<>
00156 defaulttype::BaseVector* MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::getSystemRHBaseVector() { return NULL; }
00157
00158 template<>
00159 defaulttype::BaseVector* MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>::getSystemLHBaseVector() { return NULL; }
00160
00161
00162 template class MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>;
00163
00164 }
00165
00166 }