libMesh
eigen_sparse_linear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 #ifndef LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
21 #define LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
22 
23 #include "libmesh/libmesh_common.h"
24 
25 #ifdef LIBMESH_HAVE_EIGEN
26 
27 // Eigen includes
28 
29 // Local includes
30 #include "libmesh/linear_solver.h"
31 #include "libmesh/eigen_sparse_vector.h"
32 #include "libmesh/eigen_sparse_matrix.h"
33 
34 // C++ includes
35 
36 namespace libMesh
37 {
38 
47 template <typename T>
48 class EigenSparseLinearSolver : public LinearSolver<T>
49 {
50 public:
55  LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
56 
61 
65  virtual void clear () libmesh_override;
66 
70  virtual void init (const char * name=libmesh_nullptr) libmesh_override;
71 
75  virtual std::pair<unsigned int, Real>
76  solve (SparseMatrix<T> & matrix,
77  NumericVector<T> & solution,
78  NumericVector<T> & rhs,
79  const double tol,
80  const unsigned int m_its) libmesh_override;
81 
85  virtual std::pair<unsigned int, Real>
86  adjoint_solve (SparseMatrix<T> & matrix,
87  NumericVector<T> & solution,
88  NumericVector<T> & rhs,
89  const double tol,
90  const unsigned int m_its) libmesh_override;
91 
95  virtual std::pair<unsigned int, Real>
96  solve (SparseMatrix<T> & matrix,
97  SparseMatrix<T> & pc,
98  NumericVector<T> & solution,
99  NumericVector<T> & rhs,
100  const double tol,
101  const unsigned int m_its) libmesh_override;
102 
106  virtual std::pair<unsigned int, Real>
107  solve (const ShellMatrix<T> & shell_matrix,
108  NumericVector<T> & solution_in,
109  NumericVector<T> & rhs_in,
110  const double tol,
111  const unsigned int m_its) libmesh_override;
112 
118  virtual std::pair<unsigned int, Real>
119  solve (const ShellMatrix<T> & shell_matrix,
120  const SparseMatrix<T> & precond_matrix,
121  NumericVector<T> & solution_in,
122  NumericVector<T> & rhs_in,
123  const double tol,
124  const unsigned int m_its) libmesh_override;
125 
129  virtual LinearConvergenceReason get_converged_reason() const libmesh_override;
130 
131 private:
132 
138 
142  Eigen::ComputationInfo _comp_info;
143 
148  static std::map<Eigen::ComputationInfo, LinearConvergenceReason> _convergence_reasons;
149 
153  static std::map<Eigen::ComputationInfo, LinearConvergenceReason> build_map()
154  {
155  std::map<Eigen::ComputationInfo, LinearConvergenceReason> ret;
156  ret[Eigen::Success] = CONVERGED_ITS;
157  ret[Eigen::NumericalIssue] = DIVERGED_BREAKDOWN;
158  ret[Eigen::NoConvergence] = DIVERGED_ITS;
159  ret[Eigen::InvalidInput] = DIVERGED_NULL;
160  return ret;
161  }
162 };
163 
164 
165 
166 // Call the class-static function to define the class-static member.
167 // Since it's a template class, you actually do this in the header,
168 // not the source file.
169 template <typename T>
170 std::map<Eigen::ComputationInfo, LinearConvergenceReason>
172 
173 
174 
175 template <typename T>
176 inline
178 {
179  this->clear ();
180 }
181 
182 
183 
184 template <typename T>
185 inline
186 std::pair<unsigned int, Real>
188  SparseMatrix<T> &,
191  const double,
192  const unsigned int)
193 {
194  libmesh_error_msg("ERROR: Eigen does not support a user-supplied preconditioner!");
195 
196  std::pair<unsigned int, Real> p;
197  return p;
198 }
199 
200 } // namespace libMesh
201 
202 #endif // #ifdef LIBMESH_HAVE_EIGEN
203 #endif // LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
Eigen::ComputationInfo _comp_info
Store the result of the last solve.
Encapsulates the MPI_Comm object.
Definition: parallel.h:657
This class provides an interface to Eigen iterative solvers that is compatible with the libMesh Linea...
const class libmesh_nullptr_t libmesh_nullptr
Numeric vector.
Definition: dof_map.h:66
The libMesh namespace provides an interface to certain functionality in the library.
PetscErrorCode Vec Mat Mat pc
EigenSparseLinearSolver(const libMesh::Parallel::Communicator &comm_in LIBMESH_CAN_DEFAULT_TO_COMMWORLD)
Constructor.
Generic sparse matrix.
Definition: dof_map.h:65
virtual void clear() libmesh_override
Release all memory and clear data structures.
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &matrix, NumericVector< T > &solution, NumericVector< T > &rhs, const double tol, const unsigned int m_its) libmesh_override
Call the Eigen solver to solve A^T x = b.
static std::map< Eigen::ComputationInfo, LinearConvergenceReason > build_map()
Static function used to initialize _convergence_reasons map.
static std::map< Eigen::ComputationInfo, LinearConvergenceReason > _convergence_reasons
Static map between Eigen ComputationInfo enumerations and libMesh LinearConvergenceReason enumeration...
virtual LinearConvergenceReason get_converged_reason() const libmesh_override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_eigen_preconditioner_type()
Tells Eigen to use the user-specified preconditioner stored in _preconditioner_type.
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags)
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &matrix, NumericVector< T > &solution, NumericVector< T > &rhs, const double tol, const unsigned int m_its) libmesh_override
Call the Eigen solver.
virtual void init(const char *name=libmesh_nullptr) libmesh_override
Initialize data structures if not done so already.