SolveCG.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _GOSOLVECG_H_
00034 #define _GOSOLVECG_H_
00035 
00036 
00037 //   -----------------------------------------------------------------------
00038 //      Interface file for class SolveCG
00039 //   -----------------------------------------------------------------------
00040 //
00041 //       Solve the equation system Ax=b where A is a symmetric
00042 //       positive definite matrix using Conjugate Gradient Method.
00043 //
00044 //   -----------------------------------------------------------------------
00045 //    Written by: Vibeke Skytt                            09.99
00046 //    Based on  : PrCG.h written by Mike Floater
00047 //   -----------------------------------------------------------------------
00048 
00049 #include <vector>
00050 
00051 namespace Go
00052 {
00055 
00056 
00059 class SolveCG
00060 {
00061 public:
00062 
00064     SolveCG();
00065 
00067     ~SolveCG();
00068 
00076     void attachMatrix(double *gmat, int nn);
00077 
00080     void precondRILU(double relaxfac);
00081 
00088     int solve(double *ex, double *eb, int nn);
00089 
00092     void setTolerance(double tolerance = 1.0e-6)
00093     {tolerance_ = tolerance;}
00094 
00097     void setMaxIterations(int max_iterations)
00098     {max_iterations_ = max_iterations;}
00099 
00100 
00101 private:
00102 
00103     std::vector<double> A_;   // Sparse matrix containing the left side
00104                               // of the equation system.
00105     int nn_;           // Size of equation system, i.e. number of unknowns.
00106     int np_;           // Number of non-zero entries in the equation system.
00107     std::vector<int> irow_;  // The indexes in A_ and jcol_ of the
00108                           // first non-zeros of the nn_ rows.
00109     std::vector<int> jcol_;  // The np_ indexes j of the non-zero elements
00110 
00111     double  tolerance_; // The numerical tolerance deciding if we have reached a solution.
00112     int     max_iterations_; // The maximal number of iterations to be used by solver.
00113 
00114     // Parameters used in RILU preconditioning.
00115 
00116     std::vector<double> M_;  // Preconditioning matrix.
00117     double omega_;        // Relaxation parameter.
00118 
00119     std::vector<int> diagonal_;  // Index of diagonal elements in the jcol
00120     int diagset_; // Whether the index of the diagonal elements has been set.
00121 
00125     template <typename RandomIterator1, typename RandomIterator2>
00126     void matrixProduct(RandomIterator1 sx, RandomIterator2 sy)
00127     {
00128         int kj, ki;
00129         for(kj=0; kj<nn_; kj++) {
00130             sy[kj] = 0.0;
00131             for(ki=irow_[kj]; ki<irow_[kj+1]; ki++) {
00132                 sy[kj] += A_[ki] * sx[jcol_[ki]];
00133             }
00134         }
00135     }
00136 
00138     int getIndex(int ki, int kj);
00139 
00144     void forwBack(double *r, double *s);
00145 
00153     int solveRILU(double *ex, double *eb, int nn);
00154 
00155     // Solve the equation system by conjugate gradient method
00161     int solveStd(double *ex, double *eb, int nn);
00162 
00164     void printPrecond();        // Print LU factorised preconditioning matrix
00165 
00166 };
00167 
00169 } // namespacew Go
00170 
00171 #endif
00172 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1