21#include "libmesh/libmesh_logging.h"
22#include "libmesh/linear_solver.h"
23#include "libmesh/laspack_linear_solver.h"
24#include "libmesh/eigen_sparse_linear_solver.h"
25#include "libmesh/petsc_linear_solver.h"
26#include "libmesh/trilinos_aztec_linear_solver.h"
27#include "libmesh/preconditioner.h"
28#include "libmesh/sparse_matrix.h"
29#include "libmesh/enum_to_string.h"
30#include "libmesh/solver_configuration.h"
31#include "libmesh/enum_solver_package.h"
32#include "libmesh/enum_preconditioner_type.h"
33#include "libmesh/enum_solver_type.h"
48 _is_initialized (false),
49 _preconditioner (nullptr),
50 same_preconditioner (false),
51 _solver_configuration(nullptr)
58std::unique_ptr<LinearSolver<T>>
66 switch (solver_package)
68#ifdef LIBMESH_HAVE_LASPACK
70 return std::make_unique<LaspackLinearSolver<T>>(comm);
74#ifdef LIBMESH_HAVE_PETSC
76 return std::make_unique<PetscLinearSolver<T>>(comm);
80#ifdef LIBMESH_TRILINOS_HAVE_AZTECOO
82 return std::make_unique<AztecLinearSolver<T>>(comm);
86#ifdef LIBMESH_HAVE_EIGEN
88 return std::make_unique<EigenSparseLinearSolver<T>>(comm);
92 libmesh_error_msg(
"ERROR: Unrecognized solver package: " << solver_package);
95 return std::unique_ptr<LinearSolver<T>>();
103 return _preconditioner->type();
105 return _preconditioner_type;
113 _preconditioner->set_type(pct);
115 _preconditioner_type = pct;
122 libmesh_error_msg_if(this->_is_initialized,
123 "Preconditioner must be attached before the solver is initialized!");
126 _preconditioner = preconditioner;
133 same_preconditioner = reuse_flag;
142 libmesh_not_implemented();
150 const std::optional<double> tol,
151 const std::optional<unsigned int> n_its)
154 LOG_SCOPE(
"adjoint_solve()",
"LinearSolver");
162 const std::pair<unsigned int, Real> totalrval = this->solve (mat, sol, rhs, tol, n_its);
181 _solver_configuration = &solver_configuration;
186 const std::optional<double> & setting,
187 const std::optional<double> default_value)
189 if (setting.has_value())
190 return setting.value();
191 else if (_solver_configuration)
193 if (
const auto it = this->_solver_configuration->real_valued_data.find(setting_name);
194 it != this->_solver_configuration->real_valued_data.end())
195 return double(it->second);
197 else if (default_value.has_value())
198 return default_value.value();
200 libmesh_error_msg(
"Iteration configuration parameter to the linear solver should either be supplied through input arguments or a SolverConfiguration object!");
207 const std::optional<int> & setting,
208 const std::optional<int> default_value)
210 if (setting.has_value())
211 return setting.value();
212 else if (_solver_configuration)
214 if (
const auto it = this->_solver_configuration->int_valued_data.find(setting_name);
215 it != this->_solver_configuration->int_valued_data.end())
218 else if (default_value.has_value())
219 return default_value.value();
221 libmesh_error_msg(
"Iteration configuration parameter to the linear solver should either be supplied through input arguments or a SolverConfiguration object!");
This base class can be inherited from to provide interfaces to linear solvers from different packages...
virtual void restrict_solve_to(const std::vector< unsigned int > *const dofs, const SubsetSolveMode subset_solve_mode=SUBSET_ZERO)
After calling this method, all successive solves will be restricted to the given set of dofs,...
int get_int_solver_setting(const std::string &setting_name, const std::optional< int > &setting, const std::optional< int > default_value=std::nullopt)
Get solver settings based on optional parameters and the solver configuration object.
PreconditionerType preconditioner_type() const
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a LinearSolver using the linear solver package specified by solver_package.
double get_real_solver_setting(const std::string &setting_name, const std::optional< double > &setting, const std::optional< double > default_value=std::nullopt)
Get solver settings based on optional parameters and the solver configuration object.
virtual void print_converged_reason() const
Prints a useful message about why the latest linear solve con(di)verged.
void set_preconditioner_type(const PreconditionerType pct)
Sets the type of preconditioner to use.
LinearSolver(const libMesh::Parallel::Communicator &comm_in)
Constructor.
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used.
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const std::optional< double > tol=std::nullopt, const std::optional< unsigned int > m_its=std::nullopt)
Function to solve the adjoint system.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
An object whose state is distributed along a set of processors.
This class provides a uniform interface for preconditioners.
This class stores solver configuration data, e.g.
virtual void close()=0
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
virtual void get_transpose(SparseMatrix< T > &dest) const =0
Copies the transpose of the matrix into dest, which may be *this.
std::string enum_to_string(const T e)
The libMesh namespace provides an interface to certain functionality in the library.
SubsetSolveMode
defines an enum for the question what happens to the dofs outside the given subset when a system is s...
void libmesh_ignore(const Args &...)
SolverPackage
Defines an enum for various linear solver packages.
PreconditionerType
Defines an enum for preconditioner types.
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).