libMesh
Loading...
Searching...
No Matches
eigen_sparse_linear_solver.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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#include "libmesh/enum_convergence_flags.h" // The build_map() function uses these.
34
35
36namespace libMesh
37{
38
47template <typename T>
49{
50public:
55
60
64 virtual void clear () override;
65
69 virtual void init (const char * name=nullptr) override;
70
74 virtual std::pair<unsigned int, Real>
75 solve (SparseMatrix<T> & matrix,
76 NumericVector<T> & solution,
77 NumericVector<T> & rhs,
78 const std::optional<double> tol = std::nullopt,
79 const std::optional<unsigned int> m_its = std::nullopt) override;
80
84 virtual std::pair<unsigned int, Real>
86 NumericVector<T> & solution,
87 NumericVector<T> & rhs,
88 const std::optional<double> tol = std::nullopt,
89 const std::optional<unsigned int> m_its = std::nullopt) override;
90
94 virtual std::pair<unsigned int, Real>
95 solve (SparseMatrix<T> & matrix,
96 SparseMatrix<T> & pc,
97 NumericVector<T> & solution,
98 NumericVector<T> & rhs,
99 const std::optional<double> tol = std::nullopt,
100 const std::optional<unsigned int> m_its = std::nullopt) override;
101
105 virtual std::pair<unsigned int, Real>
106 solve (const ShellMatrix<T> & shell_matrix,
107 NumericVector<T> & solution_in,
108 NumericVector<T> & rhs_in,
109 const std::optional<double> tol = std::nullopt,
110 const std::optional<unsigned int> m_its = std::nullopt) override;
111
117 virtual std::pair<unsigned int, Real>
118 solve (const ShellMatrix<T> & shell_matrix,
119 const SparseMatrix<T> & precond_matrix,
120 NumericVector<T> & solution_in,
121 NumericVector<T> & rhs_in,
122 const std::optional<double> tol = std::nullopt,
123 const std::optional<unsigned int> m_its = std::nullopt) override;
124
128 virtual LinearConvergenceReason get_converged_reason() const override;
129
130private:
131
137
141 Eigen::ComputationInfo _comp_info;
142
147 static std::map<Eigen::ComputationInfo, LinearConvergenceReason> _convergence_reasons;
148
152 static std::map<Eigen::ComputationInfo, LinearConvergenceReason> build_map()
153 {
154 std::map<Eigen::ComputationInfo, LinearConvergenceReason> ret;
155 ret[Eigen::Success] = CONVERGED_ITS;
156 ret[Eigen::NumericalIssue] = DIVERGED_BREAKDOWN;
157 ret[Eigen::NoConvergence] = DIVERGED_ITS;
158 ret[Eigen::InvalidInput] = DIVERGED_NULL;
159 return ret;
160 }
161};
162
163
164
165// Call the class-static function to define the class-static member.
166// Since it's a template class, you actually do this in the header,
167// not the source file.
168template <typename T>
169std::map<Eigen::ComputationInfo, LinearConvergenceReason>
171
172
173
174template <typename T>
175inline
180
181
182
183template <typename T>
184inline
185std::pair<unsigned int, Real>
190 const std::optional<double>,
191 const std::optional<unsigned int>)
192{
193 libmesh_error_msg("ERROR: Eigen does not support a user-supplied preconditioner!");
194
195 return std::pair<unsigned int, Real>();
196}
197
198} // namespace libMesh
199
200#endif // #ifdef LIBMESH_HAVE_EIGEN
201#endif // LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
This class provides an interface to Eigen iterative solvers that is compatible with the libMesh Linea...
virtual LinearConvergenceReason get_converged_reason() const override
Eigen::ComputationInfo _comp_info
Store the result of the last solve.
virtual void init(const char *name=nullptr) override
Initialize data structures if not done so already.
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &matrix, NumericVector< T > &solution, NumericVector< T > &rhs, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt) override
Call the Eigen solver.
static std::map< Eigen::ComputationInfo, LinearConvergenceReason > build_map()
Static function used to initialize _convergence_reasons map.
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &matrix, NumericVector< T > &solution, NumericVector< T > &rhs, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt) override
Call the Eigen solver to solve A^T x = b.
void set_eigen_preconditioner_type()
Tells Eigen to use the user-specified preconditioner stored in _preconditioner_type.
virtual void clear() override
Release all memory and clear data structures.
static std::map< Eigen::ComputationInfo, LinearConvergenceReason > _convergence_reasons
Static map between Eigen ComputationInfo enumerations and libMesh LinearConvergenceReason enumeration...
This base class can be inherited from to provide interfaces to linear solvers from different packages...
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Generic shell matrix, i.e.
Generic sparse matrix.
The libMesh namespace provides an interface to certain functionality in the library.
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).