linear_solver.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // C++ includes
21 
22 // Local Includes
24 #include "libmesh/auto_ptr.h"
25 #include "libmesh/linear_solver.h"
30 #include "libmesh/preconditioner.h"
31 #include "libmesh/sparse_matrix.h"
32 
33 namespace libMesh
34 {
35 
36 //------------------------------------------------------------------
37 // LinearSolver members
38 template <typename T>
39 AutoPtr<LinearSolver<T> >
41  const SolverPackage solver_package)
42 {
43  // Build the appropriate solver
44  switch (solver_package)
45  {
46 
47 
48 #ifdef LIBMESH_HAVE_LASPACK
49  case LASPACK_SOLVERS:
50  {
52  return ap;
53  }
54 #endif
55 
56 
57 #ifdef LIBMESH_HAVE_PETSC
58  case PETSC_SOLVERS:
59  {
61  return ap;
62  }
63 #endif
64 
65 
66 #ifdef LIBMESH_HAVE_TRILINOS
67  case TRILINOS_SOLVERS:
68  {
70  return ap;
71  }
72 #endif
73 
74 
75 #ifdef LIBMESH_HAVE_EIGEN
76  case EIGEN_SOLVERS:
77  {
79  return ap;
80  }
81 #endif
82 
83  default:
84  libMesh::err << "ERROR: Unrecognized solver package: "
85  << solver_package
86  << std::endl;
87  libmesh_error();
88  }
89 
90  AutoPtr<LinearSolver<T> > ap(NULL);
91  return ap;
92 }
93 
94 template <typename T>
97 {
98  if(_preconditioner)
99  return _preconditioner->type();
100 
101  return _preconditioner_type;
102 }
103 
104 template <typename T>
105 void
107 {
108  if(_preconditioner)
109  _preconditioner->set_type(pct);
110  else
111  _preconditioner_type = pct;
112 }
113 
114 template <typename T>
115 void
117 {
118  if(this->_is_initialized)
119  {
120  libMesh::err<<"Preconditioner must be attached before the solver is initialized!"<<std::endl;
121  libmesh_error();
122  }
123 
124  _preconditioner_type = SHELL_PRECOND;
125  _preconditioner = preconditioner;
126 }
127 
128  template <typename T>
129 void
131  {
132  same_preconditioner = reuse_flag;
133  }
134 
135 template <typename T>
136 void
137 LinearSolver<T>::restrict_solve_to(const std::vector<unsigned int>* const dofs,
138  const SubsetSolveMode /*subset_solve_mode*/)
139 {
140  if(dofs!=NULL)
141  {
142  libmesh_not_implemented();
143  }
144 }
145 
146 
147  template <typename T>
148  std::pair<unsigned int, Real> LinearSolver<T>::adjoint_solve (SparseMatrix<T> & mat,
149  NumericVector<T>& sol,
150  NumericVector<T>& rhs,
151  const double tol,
152  const unsigned int n_iter)
153  {
154  // Log how long the linear solve takes.
155  START_LOG("adjoint_solve()", "LinearSolver");
156 
157  // Take the discrete adjoint
158  mat.close();
159  mat.get_transpose(mat);
160 
161  // Call the solve function for the relevant linear algebra library and
162  // solve the transpose matrix
163  const std::pair<unsigned int, Real> totalrval = this->solve (mat, sol, rhs, tol, n_iter);
164 
165  // Now transpose back and restore the original matrix
166  // by taking the discrete adjoint
167  mat.get_transpose(mat);
168 
169  // Stop logging the nonlinear solve
170  STOP_LOG("adjoint_solve()", "LinearSolver");
171 
172  return totalrval;
173 
174  }
175 
176 
177 //------------------------------------------------------------------
178 // Explicit instantiations
179 template class LinearSolver<Number>;
180 
181 
182 
183 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo