MatrixLinearSolver.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
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     // matrix-vector product
00048 #if 1
00049     // new more powerful visitors
00050     parent->propagateDxAndResetDf(x,res);
00051     parent->addMBKdx(res,mFact,bFact,kFact, false); // df = (m M + b B + k K) dx
00052 
00053 #else
00054     parent->propagateDx(x);          // dx = p
00055     parent->computeDf(res);            // q = K p
00056 
00057     if (kFact != 1.0)
00058         res *= kFact; // q = k K p
00059     
00060     // apply global Rayleigh damping 
00061     if (mFact == 1.0)
00062     {
00063         parent->addMdx(res); // no need to propagate p as dx again
00064     }
00065     else if (mFact != 0.0)
00066     {
00067         parent->addMdx(res,SolverImpl::VecId(),mFact); // no need to propagate p as dx again
00068     }
00069     // q = (m M + k K) p
00070     
00072 #endif
00073     
00074     // filter the product to take the constraints into account
00075     //
00076     parent->projectResponse(res);     // q is projected to the constrained space
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 // Force template instantiation
00162 template class MatrixLinearSolver<GraphScatteredMatrix,GraphScatteredVector>;
00163 
00164 } // namespace simulation
00165 
00166 } // namespace sofa

Generated on Sun Nov 23 04:05:46 2008 for Sofa by  doxygen 1.5.7.1