Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

itl_bicgstab.cpp

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 // Disable warnings about identifier truncation
00024 // with Microsoft Visual C++ (R).
00025 #pragma warning(disable : 4786)
00026 
00027 #include "itl_bicgstab.h"   // include first
00028 #include "itl/interface/mtl.h"
00029 #include "itl/krylov/bicgstab.h"
00030 #include "itl/preconditioner/diagonal.h"
00031 #include "itl/preconditioner/ssor.h"
00032 
00033 #include "mtl/matrix.h"
00034 #include "mtl/mtl.h"
00035 #include "mtl/utils.h"
00036 
00037 #include "real.h"
00038 
00039 #include <cassert>
00040 #include <iostream>
00041 
00042 using namespace itl;
00043 using namespace mtl;
00044 
00045 using sheep::Real;
00046 
00047 typedef matrix<Real, rectangle<>, array<compressed<> >, row_major>::type mtl_matrix;
00048 
00049 int sheep::itl_bicgstab(const sheep::Matrix &a, const sheep::Vector &b, sheep::Vector *x) {
00050     assert(b.GetDimension() == a.GetColumns());
00051     assert(x);
00052     assert(x->GetDimension() == a.GetColumns());
00053 
00054     // Build the MTL version of the 'a' matrix.
00055 
00056     mtl_matrix mtl_a(a.GetRows(), a.GetColumns());
00057 
00058     for(int i = 0; i < mtl_a.nrows(); i++) {
00059         for(int j = 0; j < mtl_a.ncols(); j++) {
00060             mtl_a(i, j) = a(i, j);
00061         }
00062     }
00063 
00064 #ifdef VERBOSE
00065     std::cout << "A:" << std::endl;
00066     for(int i = 0; i < mtl_a.nrows(); i++) {
00067         for(int j = 0; j < mtl_a.ncols(); j++) {
00068             std::cout << mtl_a(i, j) << "\t";
00069         }
00070         std::cout << std::endl;
00071     }
00072     std::cout << std::endl;
00073 #endif  // VERBOSE
00074 
00075     // Build the MTL version of the 'b' vector.
00076 
00077     dense1D<Real> mtl_b(mtl_a.ncols());
00078 
00079     int idx = 0;
00080 
00081     for(dense1D<Real>::iterator i = mtl_b.begin(); i != mtl_b.end(); i++) {
00082         *i = b[idx++];
00083     }
00084 
00085 #ifdef VERBOSE
00086     std::cout << "B:" << std::endl;
00087     for(dense1D<Real>::iterator i = mtl_b.begin(); i != mtl_b.end(); i++) {
00088         std::cout << *i << "\t";
00089     }
00090     std::cout << std::endl;
00091 #endif  // VERBOSE
00092 
00093     // Run the Stabilized Biconjugate Gradient Solver.
00094 
00095     dense1D<Real> mtl_x(mtl_a.nrows(), Real(0));
00096 
00097 //  SSOR<mtl_matrix> precond(mtl_a);
00098     diagonal_precond<mtl_matrix> precond(mtl_a);
00099 //  identity_preconditioner precond;
00100 
00101     basic_iteration<Real> iter(mtl_b, 50, 1e-6);
00102 
00103     bicgstab(mtl_a, mtl_x, mtl_b, precond(), iter);
00104 
00105 #ifdef VERBOSE
00106     std::cout << "X:" << std::endl;
00107     for(dense1D<Real>::iterator i = mtl_x.begin(); i != mtl_x.end(); i++) {
00108         std::cout << *i << "\t";
00109     }
00110     std::cout << std::endl;
00111 #endif  // VERBOSE
00112 
00113     // Copy back the solution vector 'mtl_x' to 'x'.
00114 
00115     idx = 0;
00116 
00117     for(dense1D<Real>::iterator i = mtl_x.begin(); i != mtl_x.end(); i++) {
00118         (*x)[idx++] = *i;
00119     }
00120 
00121     return 0;
00122 }

Generated on Tue May 11 01:31:50 2004 for toxic by doxygen 1.3.6