$extrastylesheet
linear_solver.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 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_convergence_flags.h"
00026 #include "libmesh/enum_solver_package.h"
00027 #include "libmesh/enum_solver_type.h"
00028 #include "libmesh/enum_preconditioner_type.h"
00029 #include "libmesh/enum_subset_solve_mode.h"
00030 #include "libmesh/reference_counted_object.h"
00031 #include "libmesh/libmesh.h"
00032 #include "libmesh/parallel_object.h"
00033 #include "libmesh/auto_ptr.h"
00034 
00035 // C++ includes
00036 #include <cstddef>
00037 #include <vector>
00038 
00039 namespace libMesh
00040 {
00041 
00042 // forward declarations
00043 template <typename T> class SparseMatrix;
00044 template <typename T> class NumericVector;
00045 template <typename T> class ShellMatrix;
00046 template <typename T> class Preconditioner;
00047 class System;
00048 
00057 template <typename T>
00058 class LinearSolver : public ReferenceCountedObject<LinearSolver<T> >,
00059                      public ParallelObject
00060 {
00061 public:
00062 
00066   LinearSolver (const libMesh::Parallel::Communicator &comm_in
00067                 LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
00068 
00072   virtual ~LinearSolver ();
00073 
00078   static UniquePtr<LinearSolver<T> > build(const libMesh::Parallel::Communicator &comm_in,
00079                                            const SolverPackage solver_package = libMesh::default_solver_package());
00080 
00085   bool initialized () const { return _is_initialized; }
00086 
00090   virtual void clear () {}
00091 
00096   virtual void init (const char* name = NULL) = 0;
00097 
00106   virtual void init_names (const System&) {}
00107 
00111   SolverType solver_type () const { return _solver_type; }
00112 
00116   void set_solver_type (const SolverType st)
00117   { _solver_type = st; }
00118 
00122   PreconditionerType preconditioner_type () const;
00123 
00127   void set_preconditioner_type (const PreconditionerType pct);
00128 
00132   void attach_preconditioner(Preconditioner<T> * preconditioner);
00133 
00134   virtual void reuse_preconditioner(bool );
00135 
00136   bool get_same_preconditioner();
00137 
00145   virtual void restrict_solve_to (const std::vector<unsigned int>* const dofs,
00146                                   const SubsetSolveMode subset_solve_mode=SUBSET_ZERO);
00147 
00154   virtual std::pair<unsigned int, Real> solve (SparseMatrix<T>&,  // System Matrix
00155                                                NumericVector<T>&, // Solution vector
00156                                                NumericVector<T>&, // RHS vector
00157                                                const double,      // Stopping tolerance
00158                                                const unsigned int) = 0; // N. Iterations
00159 
00165   virtual std::pair<unsigned int, Real> adjoint_solve (SparseMatrix<T>&,  // System Matrix
00166                                                        NumericVector<T>&, // Solution vector
00167                                                        NumericVector<T>&, // RHS vector
00168                                                        const double,      // Stopping tolerance
00169                                                        const unsigned int); // N. Iterations
00170 
00176   virtual std::pair<unsigned int, Real> solve (SparseMatrix<T>&,  // System Matrix
00177                                                SparseMatrix<T>&,  // Preconditioning Matrix
00178                                                NumericVector<T>&, // Solution vector
00179                                                NumericVector<T>&, // RHS vector
00180                                                const double,      // Stopping tolerance
00181                                                const unsigned int) = 0; // N. Iterations
00182 
00189   std::pair<unsigned int, Real> solve (SparseMatrix<T>& matrix,
00190                                        SparseMatrix<T>* precond_matrix,
00191                                        NumericVector<T>&, // Solution vector
00192                                        NumericVector<T>&, // RHS vector
00193                                        const double,      // Stopping tolerance
00194                                        const unsigned int); // N. Iterations
00195 
00196 
00197 
00201   virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T>& shell_matrix,
00202                                                NumericVector<T>&, // Solution vector
00203                                                NumericVector<T>&, // RHS vector
00204                                                const double,      // Stopping tolerance
00205                                                const unsigned int) = 0; // N. Iterations
00206 
00207 
00208 
00214   virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T>& shell_matrix,
00215                                                const SparseMatrix<T>& precond_matrix,
00216                                                NumericVector<T>&, // Solution vector
00217                                                NumericVector<T>&, // RHS vector
00218                                                const double,      // Stopping tolerance
00219                                                const unsigned int) = 0; // N. Iterations
00220 
00221 
00226   std::pair<unsigned int, Real> solve (const ShellMatrix<T>& matrix,
00227                                        const SparseMatrix<T>* precond_matrix,
00228                                        NumericVector<T>&, // Solution vector
00229                                        NumericVector<T>&, // RHS vector
00230                                        const double,      // Stopping tolerance
00231                                        const unsigned int); // N. Iterations
00232 
00233 
00238   virtual void print_converged_reason() const;
00239 
00243   virtual LinearConvergenceReason get_converged_reason() const = 0;
00244 
00245 protected:
00246 
00247 
00251   SolverType _solver_type;
00252 
00256   PreconditionerType _preconditioner_type;
00257 
00261   bool _is_initialized;
00262 
00266   Preconditioner<T> * _preconditioner;
00267 
00274   bool same_preconditioner;
00275 
00276 };
00277 
00278 
00279 
00280 
00281 /*----------------------- inline functions ----------------------------------*/
00282 template <typename T>
00283 inline
00284 LinearSolver<T>::LinearSolver (const libMesh::Parallel::Communicator &comm_in) :
00285   ParallelObject       (comm_in),
00286   _solver_type         (GMRES),
00287   _preconditioner_type (ILU_PRECOND),
00288   _is_initialized      (false),
00289   _preconditioner      (NULL),
00290   same_preconditioner  (false)
00291 {
00292 }
00293 
00294 
00295 
00296 template <typename T>
00297 inline
00298 LinearSolver<T>::~LinearSolver ()
00299 {
00300   this->clear ();
00301 }
00302 
00303 template <typename T>
00304 inline
00305 bool LinearSolver<T>::get_same_preconditioner()
00306 {
00307   return same_preconditioner;
00308 }
00309 
00310 template <typename T>
00311 inline
00312 std::pair<unsigned int, Real>
00313 LinearSolver<T>::solve (SparseMatrix<T>&   mat,
00314                         SparseMatrix<T>*   pc_mat,
00315                         NumericVector<T>&  sol,
00316                         NumericVector<T>&  rhs,
00317                         const double       tol,
00318                         const unsigned int n_iter)
00319 {
00320   if (pc_mat)
00321     return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
00322   else
00323     return this->solve(mat, sol, rhs, tol, n_iter);
00324 }
00325 
00326 
00327 template <typename T>
00328 inline
00329 std::pair<unsigned int, Real>
00330 LinearSolver<T>::solve (const ShellMatrix<T>&  mat,
00331                         const SparseMatrix<T>* pc_mat,
00332                         NumericVector<T>&      sol,
00333                         NumericVector<T>&      rhs,
00334                         const double           tol,
00335                         const unsigned int     n_iter)
00336 {
00337   if (pc_mat)
00338     return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
00339   else
00340     return this->solve(mat, sol, rhs, tol, n_iter);
00341 }
00342 
00343 } // namespace libMesh
00344 
00345 
00346 #endif // LIBMESH_LINEAR_SOLVER_H