linear_solver.C
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 // C++ includes 00021 00022 // Local Includes 00023 #include "libmesh/libmesh_logging.h" 00024 #include "libmesh/auto_ptr.h" 00025 #include "libmesh/linear_solver.h" 00026 #include "libmesh/laspack_linear_solver.h" 00027 #include "libmesh/petsc_linear_solver.h" 00028 #include "libmesh/trilinos_aztec_linear_solver.h" 00029 #include "libmesh/preconditioner.h" 00030 #include "libmesh/sparse_matrix.h" 00031 00032 namespace libMesh 00033 { 00034 00035 //------------------------------------------------------------------ 00036 // LinearSolver members 00037 template <typename T> 00038 AutoPtr<LinearSolver<T> > 00039 LinearSolver<T>::build(const SolverPackage solver_package) 00040 { 00041 // Build the appropriate solver 00042 switch (solver_package) 00043 { 00044 00045 00046 #ifdef LIBMESH_HAVE_LASPACK 00047 case LASPACK_SOLVERS: 00048 { 00049 AutoPtr<LinearSolver<T> > ap(new LaspackLinearSolver<T>); 00050 return ap; 00051 } 00052 #endif 00053 00054 00055 #ifdef LIBMESH_HAVE_PETSC 00056 case PETSC_SOLVERS: 00057 { 00058 AutoPtr<LinearSolver<T> > ap(new PetscLinearSolver<T>); 00059 return ap; 00060 } 00061 #endif 00062 00063 00064 #ifdef LIBMESH_HAVE_TRILINOS 00065 case TRILINOS_SOLVERS: 00066 { 00067 AutoPtr<LinearSolver<T> > ap(new AztecLinearSolver<T>); 00068 return ap; 00069 } 00070 #endif 00071 00072 default: 00073 libMesh::err << "ERROR: Unrecognized solver package: " 00074 << solver_package 00075 << std::endl; 00076 libmesh_error(); 00077 } 00078 00079 AutoPtr<LinearSolver<T> > ap(NULL); 00080 return ap; 00081 } 00082 00083 template <typename T> 00084 PreconditionerType 00085 LinearSolver<T>::preconditioner_type () const 00086 { 00087 if(_preconditioner) 00088 return _preconditioner->type(); 00089 00090 return _preconditioner_type; 00091 } 00092 00093 template <typename T> 00094 void 00095 LinearSolver<T>::set_preconditioner_type (const PreconditionerType pct) 00096 { 00097 if(_preconditioner) 00098 _preconditioner->set_type(pct); 00099 else 00100 _preconditioner_type = pct; 00101 } 00102 00103 template <typename T> 00104 void 00105 LinearSolver<T>::attach_preconditioner(Preconditioner<T> * preconditioner) 00106 { 00107 if(this->_is_initialized) 00108 { 00109 libMesh::err<<"Preconditioner must be attached before the solver is initialized!"<<std::endl; 00110 libmesh_error(); 00111 } 00112 00113 _preconditioner_type = SHELL_PRECOND; 00114 _preconditioner = preconditioner; 00115 } 00116 00117 template <typename T> 00118 void 00119 LinearSolver<T>::reuse_preconditioner(bool reuse_flag) 00120 { 00121 same_preconditioner = reuse_flag; 00122 } 00123 00124 template <typename T> 00125 void 00126 LinearSolver<T>::restrict_solve_to(const std::vector<unsigned int>* const dofs, 00127 const SubsetSolveMode /*subset_solve_mode*/) 00128 { 00129 if(dofs!=NULL) 00130 { 00131 libmesh_not_implemented(); 00132 } 00133 } 00134 00135 00136 template <typename T> 00137 std::pair<unsigned int, Real> LinearSolver<T>::adjoint_solve (SparseMatrix<T> & mat, 00138 NumericVector<T>& sol, 00139 NumericVector<T>& rhs, 00140 const double tol, 00141 const unsigned int n_iter) 00142 { 00143 // Log how long the linear solve takes. 00144 START_LOG("adjoint_solve()", "LinearSolver"); 00145 00146 // Take the discrete adjoint 00147 mat.close(); 00148 mat.get_transpose(mat); 00149 00150 // Call the solve function for the relevant linear algebra library and 00151 // solve the transpose matrix 00152 const std::pair<unsigned int, Real> totalrval = this->solve (mat, sol, rhs, tol, n_iter); 00153 00154 // Now transpose back and restore the original matrix 00155 // by taking the discrete adjoint 00156 mat.get_transpose(mat); 00157 00158 // Stop logging the nonlinear solve 00159 STOP_LOG("adjoint_solve()", "LinearSolver"); 00160 00161 return totalrval; 00162 00163 } 00164 00165 00166 //------------------------------------------------------------------ 00167 // Explicit instantiations 00168 template class LinearSolver<Number>; 00169 00170 00171 00172 } // namespace libMesh 00173 00174 00175
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: