linear_solver.h
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 00020 #ifndef LIBMESH_LINEAR_SOLVER_H 00021 #define LIBMESH_LINEAR_SOLVER_H 00022 00023 // Local includes 00024 #include "libmesh/libmesh_common.h" 00025 #include "libmesh/enum_solver_package.h" 00026 #include "libmesh/enum_solver_type.h" 00027 #include "libmesh/enum_preconditioner_type.h" 00028 #include "libmesh/enum_subset_solve_mode.h" 00029 #include "libmesh/reference_counted_object.h" 00030 #include "libmesh/libmesh.h" 00031 00032 // C++ includes 00033 #include <cstddef> 00034 #include <vector> 00035 00036 namespace libMesh 00037 { 00038 00039 // forward declarations 00040 template <typename T> class AutoPtr; 00041 template <typename T> class SparseMatrix; 00042 template <typename T> class NumericVector; 00043 template <typename T> class ShellMatrix; 00044 template <typename T> class Preconditioner; 00045 00046 00055 template <typename T> 00056 class LinearSolver : public ReferenceCountedObject<LinearSolver<T> > 00057 { 00058 public: 00059 00063 LinearSolver (); 00064 00068 virtual ~LinearSolver (); 00069 00074 static AutoPtr<LinearSolver<T> > build(const SolverPackage solver_package = 00075 libMesh::default_solver_package()); 00076 00081 bool initialized () const { return _is_initialized; } 00082 00086 virtual void clear () {} 00087 00091 virtual void init () = 0; 00092 00096 SolverType solver_type () const { return _solver_type; } 00097 00101 void set_solver_type (const SolverType st) 00102 { _solver_type = st; } 00103 00107 PreconditionerType preconditioner_type () const; 00108 00112 void set_preconditioner_type (const PreconditionerType pct); 00113 00117 void attach_preconditioner(Preconditioner<T> * preconditioner); 00118 00119 virtual void reuse_preconditioner(bool ); 00120 00121 bool get_same_preconditioner(); 00122 00130 virtual void restrict_solve_to (const std::vector<unsigned int>* const dofs, 00131 const SubsetSolveMode subset_solve_mode=SUBSET_ZERO); 00132 00139 virtual std::pair<unsigned int, Real> solve (SparseMatrix<T>&, // System Matrix 00140 NumericVector<T>&, // Solution vector 00141 NumericVector<T>&, // RHS vector 00142 const double, // Stopping tolerance 00143 const unsigned int) = 0; // N. Iterations 00144 00150 virtual std::pair<unsigned int, Real> adjoint_solve (SparseMatrix<T>&, // System Matrix 00151 NumericVector<T>&, // Solution vector 00152 NumericVector<T>&, // RHS vector 00153 const double, // Stopping tolerance 00154 const unsigned int); // N. Iterations 00155 00161 virtual std::pair<unsigned int, Real> solve (SparseMatrix<T>&, // System Matrix 00162 SparseMatrix<T>&, // Preconditioning Matrix 00163 NumericVector<T>&, // Solution vector 00164 NumericVector<T>&, // RHS vector 00165 const double, // Stopping tolerance 00166 const unsigned int) = 0; // N. Iterations 00167 00174 std::pair<unsigned int, Real> solve (SparseMatrix<T>& matrix, 00175 SparseMatrix<T>* precond_matrix, 00176 NumericVector<T>&, // Solution vector 00177 NumericVector<T>&, // RHS vector 00178 const double, // Stopping tolerance 00179 const unsigned int); // N. Iterations 00180 00181 00182 00186 virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T>& shell_matrix, 00187 NumericVector<T>&, // Solution vector 00188 NumericVector<T>&, // RHS vector 00189 const double, // Stopping tolerance 00190 const unsigned int) = 0; // N. Iterations 00191 00192 00193 00199 virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T>& shell_matrix, 00200 const SparseMatrix<T>& precond_matrix, 00201 NumericVector<T>&, // Solution vector 00202 NumericVector<T>&, // RHS vector 00203 const double, // Stopping tolerance 00204 const unsigned int) = 0; // N. Iterations 00205 00206 00211 std::pair<unsigned int, Real> solve (const ShellMatrix<T>& matrix, 00212 const SparseMatrix<T>* precond_matrix, 00213 NumericVector<T>&, // Solution vector 00214 NumericVector<T>&, // RHS vector 00215 const double, // Stopping tolerance 00216 const unsigned int); // N. Iterations 00217 00218 00223 virtual void print_converged_reason() = 0; 00224 00225 00226 protected: 00227 00228 00232 SolverType _solver_type; 00233 00237 PreconditionerType _preconditioner_type; 00238 00242 bool _is_initialized; 00243 00247 Preconditioner<T> * _preconditioner; 00248 00255 bool same_preconditioner; 00256 00257 }; 00258 00259 00260 00261 00262 /*----------------------- inline functions ----------------------------------*/ 00263 template <typename T> 00264 inline 00265 LinearSolver<T>::LinearSolver () : 00266 _solver_type (GMRES), 00267 _preconditioner_type (ILU_PRECOND), 00268 _is_initialized (false), 00269 _preconditioner (NULL), 00270 same_preconditioner (false) 00271 { 00272 } 00273 00274 00275 00276 template <typename T> 00277 inline 00278 LinearSolver<T>::~LinearSolver () 00279 { 00280 this->clear (); 00281 } 00282 00283 template <typename T> 00284 inline 00285 bool LinearSolver<T>::get_same_preconditioner() 00286 { 00287 return same_preconditioner; 00288 } 00289 00290 template <typename T> 00291 inline 00292 std::pair<unsigned int, Real> 00293 LinearSolver<T>::solve (SparseMatrix<T>& mat, 00294 SparseMatrix<T>* pc_mat, 00295 NumericVector<T>& sol, 00296 NumericVector<T>& rhs, 00297 const double tol, 00298 const unsigned int n_iter) 00299 { 00300 if (pc_mat) 00301 return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter); 00302 else 00303 return this->solve(mat, sol, rhs, tol, n_iter); 00304 } 00305 00306 00307 template <typename T> 00308 inline 00309 std::pair<unsigned int, Real> 00310 LinearSolver<T>::solve (const ShellMatrix<T>& mat, 00311 const SparseMatrix<T>* pc_mat, 00312 NumericVector<T>& sol, 00313 NumericVector<T>& rhs, 00314 const double tol, 00315 const unsigned int n_iter) 00316 { 00317 if (pc_mat) 00318 return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter); 00319 else 00320 return this->solve(mat, sol, rhs, tol, n_iter); 00321 } 00322 00323 } // namespace libMesh 00324 00325 00326 #endif // LIBMESH_LINEAR_SOLVER_H
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: