libMesh
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
libMesh::LinearSolver< T > Class Template Referenceabstract

This base class can be inherited from to provide interfaces to linear solvers from different packages like PETSc and LASPACK. More...

#include <linear_solver.h>

Inheritance diagram for libMesh::LinearSolver< T >:
[legend]

Public Member Functions

 LinearSolver (const libMesh::Parallel::Communicator &comm_in)
 Constructor. More...
 
virtual ~LinearSolver ()
 Destructor. More...
 
bool initialized () const
 
virtual void clear ()
 Release all memory and clear data structures. More...
 
virtual void init (const char *name=nullptr)=0
 Initialize data structures if not done so already. More...
 
virtual void init_names (const System &)
 Apply names to the system to be solved. More...
 
SolverType solver_type () const
 
void set_solver_type (const SolverType st)
 Sets the type of solver to use. More...
 
PreconditionerType preconditioner_type () const
 
void set_preconditioner_type (const PreconditionerType pct)
 Sets the type of preconditioner to use. More...
 
void attach_preconditioner (Preconditioner< T > *preconditioner)
 Attaches a Preconditioner object to be used. More...
 
virtual void reuse_preconditioner (bool)
 Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent solves. More...
 
bool get_same_preconditioner ()
 
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, which must contain local dofs on each processor only and not contain any duplicates. More...
 
virtual std::pair< unsigned int, Realsolve (SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
 This function calls the solver _solver_type preconditioned with the _preconditioner_type preconditioner. More...
 
virtual std::pair< unsigned int, Realadjoint_solve (SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
 Function to solve the adjoint system. More...
 
virtual std::pair< unsigned int, Realsolve (SparseMatrix< T > &, SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
 This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" preconditioner. More...
 
std::pair< unsigned int, Realsolve (SparseMatrix< T > &matrix, SparseMatrix< T > *precond_matrix, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
 This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" preconditioner. More...
 
virtual std::pair< unsigned int, Realsolve (const ShellMatrix< T > &shell_matrix, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
 This function solves a system whose matrix is a shell matrix. More...
 
virtual std::pair< unsigned int, Realsolve (const ShellMatrix< T > &shell_matrix, const SparseMatrix< T > &precond_matrix, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
 This function solves a system whose matrix is a shell matrix, but a sparse matrix is used as preconditioning matrix, this allowing other preconditioners than JACOBI. More...
 
std::pair< unsigned int, Realsolve (const ShellMatrix< T > &matrix, const SparseMatrix< T > *precond_matrix, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
 This function solves a system whose matrix is a shell matrix, but an optional sparse matrix may be used as preconditioning matrix. More...
 
virtual void print_converged_reason () const
 Prints a useful message about why the latest linear solve con(di)verged. More...
 
virtual LinearConvergenceReason get_converged_reason () const =0
 
void set_solver_configuration (SolverConfiguration &solver_configuration)
 Set the solver configuration object. More...
 
const Parallel::Communicatorcomm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

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. More...
 
static std::string get_info ()
 Gets a string containing the reference information. More...
 
static void print_info (std::ostream &out=libMesh::out)
 Prints the reference information, by default to libMesh::out. More...
 
static unsigned int n_objects ()
 Prints the number of outstanding (created, but not yet destroyed) objects. More...
 
static void enable_print_counter_info ()
 Methods to enable/disable the reference counter output from print_info() More...
 
static void disable_print_counter_info ()
 

Protected Types

typedef std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
 Data structure to log the information. More...
 

Protected Member Functions

void increment_constructor_count (const std::string &name)
 Increments the construction counter. More...
 
void increment_destructor_count (const std::string &name)
 Increments the destruction counter. More...
 

Protected Attributes

SolverType _solver_type
 Enum stating which type of iterative solver to use. More...
 
PreconditionerType _preconditioner_type
 Enum stating with type of preconditioner to use. More...
 
bool _is_initialized
 Flag indicating if the data structures have been initialized. More...
 
Preconditioner< T > * _preconditioner
 Holds the Preconditioner object to be used for the linear solves. More...
 
bool same_preconditioner
 Boolean flag to indicate whether we want to use an identical preconditioner to the previous solve. More...
 
SolverConfiguration_solver_configuration
 Optionally store a SolverOptions object that can be used to set parameters like solver type, tolerances and iteration limits. More...
 
const Parallel::Communicator_communicator
 

Static Protected Attributes

static Counts _counts
 Actually holds the data. More...
 
static Threads::atomic< unsigned int > _n_objects
 The number of objects. More...
 
static Threads::spin_mutex _mutex
 Mutual exclusion object to enable thread-safe reference counting. More...
 
static bool _enable_print_counter
 Flag to control whether reference count information is printed when print_info is called. More...
 

Detailed Description

template<typename T>
class libMesh::LinearSolver< T >

This base class can be inherited from to provide interfaces to linear solvers from different packages like PETSc and LASPACK.

Author
Benjamin Kirk
Date
2003

Definition at line 70 of file linear_solver.h.

Member Typedef Documentation

◆ Counts

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > libMesh::ReferenceCounter::Counts
protectedinherited

Data structure to log the information.

The log is identified by the class name.

Definition at line 117 of file reference_counter.h.

Constructor & Destructor Documentation

◆ LinearSolver()

template<typename T>
libMesh::LinearSolver< T >::LinearSolver ( const libMesh::Parallel::Communicator comm_in)

Constructor.

Initializes Solver data structures

◆ ~LinearSolver()

template<typename T >
libMesh::LinearSolver< T >::~LinearSolver ( )
virtual

Destructor.

Definition at line 315 of file linear_solver.h.

316 {
317  this->clear ();
318 }
virtual void clear()
Release all memory and clear data structures.

Member Function Documentation

◆ adjoint_solve()

template<typename T>
virtual std::pair<unsigned int, Real> libMesh::LinearSolver< T >::adjoint_solve ( SparseMatrix< T > &  ,
NumericVector< T > &  ,
NumericVector< T > &  ,
const double  ,
const unsigned  int 
)
virtual

Function to solve the adjoint system.

Note
This method will compute the preconditioner from the system matrix. This is not a pure virtual function and is defined linear_solver.C

Reimplemented in libMesh::PetscLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ attach_preconditioner()

template<typename T>
void libMesh::LinearSolver< T >::attach_preconditioner ( Preconditioner< T > *  preconditioner)

Attaches a Preconditioner object to be used.

◆ build()

template<typename T>
static std::unique_ptr<LinearSolver<T> > libMesh::LinearSolver< T >::build ( const libMesh::Parallel::Communicator comm_in,
const SolverPackage  solver_package = libMesh::default_solver_package() 
)
static

Builds a LinearSolver using the linear solver package specified by solver_package.

◆ clear()

template<typename T>
virtual void libMesh::LinearSolver< T >::clear ( )
virtual

Release all memory and clear data structures.

Reimplemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

Definition at line 101 of file linear_solver.h.

101 {}

◆ comm()

const Parallel::Communicator& libMesh::ParallelObject::comm ( ) const
inherited

◆ disable_print_counter_info()

static void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

◆ enable_print_counter_info()

static void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

Methods to enable/disable the reference counter output from print_info()

◆ get_converged_reason()

template<typename T>
virtual LinearConvergenceReason libMesh::LinearSolver< T >::get_converged_reason ( ) const
pure virtual

◆ get_info()

static std::string libMesh::ReferenceCounter::get_info ( )
staticinherited

Gets a string containing the reference information.

◆ get_same_preconditioner()

template<typename T >
bool libMesh::LinearSolver< T >::get_same_preconditioner ( )
Returns
same_preconditioner, which indicates if we reuse the same preconditioner for subsequent solves.

Definition at line 322 of file linear_solver.h.

323 {
324  return same_preconditioner;
325 }
bool same_preconditioner
Boolean flag to indicate whether we want to use an identical preconditioner to the previous solve...

◆ increment_constructor_count()

void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name)
protectedinherited

Increments the construction counter.

Should be called in the constructor of any derived class that will be reference counted.

Definition at line 181 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::ReferenceCountedObject().

182 {
183  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
184  std::pair<unsigned int, unsigned int> & p = _counts[name];
185 
186  p.first++;
187 }
std::string name(const ElemQuality q)
static Counts _counts
Actually holds the data.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.

◆ increment_destructor_count()

void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name)
protectedinherited

Increments the destruction counter.

Should be called in the destructor of any derived class that will be reference counted.

Definition at line 194 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::~ReferenceCountedObject().

195 {
196  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
197  std::pair<unsigned int, unsigned int> & p = _counts[name];
198 
199  p.second++;
200 }
std::string name(const ElemQuality q)
static Counts _counts
Actually holds the data.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.

◆ init()

template<typename T>
virtual void libMesh::LinearSolver< T >::init ( const char *  name = nullptr)
pure virtual

Initialize data structures if not done so already.

May assign a name to the solver in some implementations

Implemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ init_names()

template<typename T>
virtual void libMesh::LinearSolver< T >::init_names ( const System )
virtual

Apply names to the system to be solved.

For most packages this is a no-op; for PETSc this sets an option prefix from the system name and sets field names from the system's variable names.

Since field names are applied to DoF numberings, this method must be called again after any System reinit.

Reimplemented in libMesh::PetscLinearSolver< T >.

Definition at line 117 of file linear_solver.h.

117 {}

◆ initialized()

template<typename T>
bool libMesh::LinearSolver< T >::initialized ( ) const
Returns
true if the data structures are initialized, false otherwise.

Definition at line 96 of file linear_solver.h.

96 { return _is_initialized; }
bool _is_initialized
Flag indicating if the data structures have been initialized.

◆ n_objects()

static unsigned int libMesh::ReferenceCounter::n_objects ( )
staticinherited

Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 83 of file reference_counter.h.

References libMesh::ReferenceCounter::_n_objects.

84  { return _n_objects; }
static Threads::atomic< unsigned int > _n_objects
The number of objects.

◆ n_processors()

processor_id_type libMesh::ParallelObject::n_processors ( ) const
inherited
Returns
The number of processors in the group.

Definition at line 93 of file parallel_object.h.

References libMesh::ParallelObject::_communicator, and libMesh::Parallel::Communicator::size().

Referenced by libMesh::MeshBase::partition().

94  { return cast_int<processor_id_type>(_communicator.size()); }
processor_id_type size() const
Definition: communicator.h:175
const Parallel::Communicator & _communicator

◆ preconditioner_type()

template<typename T>
PreconditionerType libMesh::LinearSolver< T >::preconditioner_type ( ) const
Returns
The type of preconditioner to use.

◆ print_converged_reason()

template<typename T>
virtual void libMesh::LinearSolver< T >::print_converged_reason ( ) const
virtual

Prints a useful message about why the latest linear solve con(di)verged.

Reimplemented in libMesh::AztecLinearSolver< T >, and libMesh::LaspackLinearSolver< T >.

◆ print_info()

static void libMesh::ReferenceCounter::print_info ( std::ostream &  out = libMesh::out)
staticinherited

Prints the reference information, by default to libMesh::out.

◆ processor_id()

processor_id_type libMesh::ParallelObject::processor_id ( ) const
inherited

◆ restrict_solve_to()

template<typename T>
virtual void libMesh::LinearSolver< T >::restrict_solve_to ( const std::vector< unsigned int > *const  dofs,
const SubsetSolveMode  subset_solve_mode = SUBSET_ZERO 
)
virtual

After calling this method, all successive solves will be restricted to the given set of dofs, which must contain local dofs on each processor only and not contain any duplicates.

This mode can be disabled by calling this method with dofs being a nullptr.

Reimplemented in libMesh::PetscLinearSolver< T >.

◆ reuse_preconditioner()

template<typename T>
virtual void libMesh::LinearSolver< T >::reuse_preconditioner ( bool  )
virtual

Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent solves.

◆ set_preconditioner_type()

template<typename T>
void libMesh::LinearSolver< T >::set_preconditioner_type ( const PreconditionerType  pct)

Sets the type of preconditioner to use.

◆ set_solver_configuration()

template<typename T>
void libMesh::LinearSolver< T >::set_solver_configuration ( SolverConfiguration solver_configuration)

Set the solver configuration object.

◆ set_solver_type()

template<typename T>
void libMesh::LinearSolver< T >::set_solver_type ( const SolverType  st)

Sets the type of solver to use.

Definition at line 127 of file linear_solver.h.

128  { _solver_type = st; }
SolverType _solver_type
Enum stating which type of iterative solver to use.

◆ solve() [1/6]

template<typename T>
virtual std::pair<unsigned int, Real> libMesh::LinearSolver< T >::solve ( SparseMatrix< T > &  ,
NumericVector< T > &  ,
NumericVector< T > &  ,
const double  ,
const unsigned  int 
)
pure virtual

This function calls the solver _solver_type preconditioned with the _preconditioner_type preconditioner.

Note
This method will compute the preconditioner from the system matrix.

Implemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ solve() [2/6]

template<typename T>
virtual std::pair<unsigned int, Real> libMesh::LinearSolver< T >::solve ( SparseMatrix< T > &  ,
SparseMatrix< T > &  ,
NumericVector< T > &  ,
NumericVector< T > &  ,
const double  ,
const unsigned  int 
)
pure virtual

This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" preconditioner.

Implemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ solve() [3/6]

template<typename T>
std::pair< unsigned int, Real > libMesh::LinearSolver< T >::solve ( SparseMatrix< T > &  matrix,
SparseMatrix< T > *  precond_matrix,
NumericVector< T > &  sol,
NumericVector< T > &  rhs,
const double  tol,
const unsigned int  n_iter 
)

This function calls the solver "_solver_type" preconditioned with the "_preconditioner_type" preconditioner.

The preconditioning matrix is used if it is provided, or the system matrix is used if precond_matrix is null

Definition at line 330 of file linear_solver.h.

336 {
337  if (pc_mat)
338  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
339  else
340  return this->solve(mat, sol, rhs, tol, n_iter);
341 }
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
This function calls the solver _solver_type preconditioned with the _preconditioner_type precondition...

◆ solve() [4/6]

template<typename T>
virtual std::pair<unsigned int, Real> libMesh::LinearSolver< T >::solve ( const ShellMatrix< T > &  shell_matrix,
NumericVector< T > &  ,
NumericVector< T > &  ,
const double  ,
const unsigned  int 
)
pure virtual

This function solves a system whose matrix is a shell matrix.

Implemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ solve() [5/6]

template<typename T>
virtual std::pair<unsigned int, Real> libMesh::LinearSolver< T >::solve ( const ShellMatrix< T > &  shell_matrix,
const SparseMatrix< T > &  precond_matrix,
NumericVector< T > &  ,
NumericVector< T > &  ,
const double  ,
const unsigned  int 
)
pure virtual

This function solves a system whose matrix is a shell matrix, but a sparse matrix is used as preconditioning matrix, this allowing other preconditioners than JACOBI.

Implemented in libMesh::PetscLinearSolver< T >, libMesh::AztecLinearSolver< T >, libMesh::LaspackLinearSolver< T >, and libMesh::EigenSparseLinearSolver< T >.

◆ solve() [6/6]

template<typename T>
std::pair< unsigned int, Real > libMesh::LinearSolver< T >::solve ( const ShellMatrix< T > &  matrix,
const SparseMatrix< T > *  precond_matrix,
NumericVector< T > &  sol,
NumericVector< T > &  rhs,
const double  tol,
const unsigned int  n_iter 
)

This function solves a system whose matrix is a shell matrix, but an optional sparse matrix may be used as preconditioning matrix.

Definition at line 347 of file linear_solver.h.

353 {
354  if (pc_mat)
355  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
356  else
357  return this->solve(mat, sol, rhs, tol, n_iter);
358 }
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
This function calls the solver _solver_type preconditioned with the _preconditioner_type precondition...

◆ solver_type()

template<typename T>
SolverType libMesh::LinearSolver< T >::solver_type ( ) const
Returns
The type of solver to use.

Definition at line 122 of file linear_solver.h.

122 { return _solver_type; }
SolverType _solver_type
Enum stating which type of iterative solver to use.

Member Data Documentation

◆ _communicator

const Parallel::Communicator& libMesh::ParallelObject::_communicator
protectedinherited

◆ _counts

Counts libMesh::ReferenceCounter::_counts
staticprotectedinherited

◆ _enable_print_counter

bool libMesh::ReferenceCounter::_enable_print_counter
staticprotectedinherited

Flag to control whether reference count information is printed when print_info is called.

Definition at line 141 of file reference_counter.h.

◆ _is_initialized

template<typename T>
bool libMesh::LinearSolver< T >::_is_initialized
protected

Flag indicating if the data structures have been initialized.

Definition at line 287 of file linear_solver.h.

Referenced by libMesh::LinearSolver< Number >::initialized().

◆ _mutex

Threads::spin_mutex libMesh::ReferenceCounter::_mutex
staticprotectedinherited

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 135 of file reference_counter.h.

◆ _n_objects

Threads::atomic<unsigned int> libMesh::ReferenceCounter::_n_objects
staticprotectedinherited

The number of objects.

Print the reference count information when the number returns to 0.

Definition at line 130 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::n_objects(), libMesh::ReferenceCounter::ReferenceCounter(), and libMesh::ReferenceCounter::~ReferenceCounter().

◆ _preconditioner

template<typename T>
Preconditioner<T>* libMesh::LinearSolver< T >::_preconditioner
protected

Holds the Preconditioner object to be used for the linear solves.

Definition at line 292 of file linear_solver.h.

◆ _preconditioner_type

template<typename T>
PreconditionerType libMesh::LinearSolver< T >::_preconditioner_type
protected

Enum stating with type of preconditioner to use.

Definition at line 282 of file linear_solver.h.

◆ _solver_configuration

template<typename T>
SolverConfiguration* libMesh::LinearSolver< T >::_solver_configuration
protected

Optionally store a SolverOptions object that can be used to set parameters like solver type, tolerances and iteration limits.

Definition at line 306 of file linear_solver.h.

◆ _solver_type

template<typename T>
SolverType libMesh::LinearSolver< T >::_solver_type
protected

Enum stating which type of iterative solver to use.

Definition at line 277 of file linear_solver.h.

Referenced by libMesh::LinearSolver< Number >::set_solver_type(), and libMesh::LinearSolver< Number >::solver_type().

◆ same_preconditioner

template<typename T>
bool libMesh::LinearSolver< T >::same_preconditioner
protected

Boolean flag to indicate whether we want to use an identical preconditioner to the previous solve.

This can save substantial work in the cases where the system matrix is the same for successive solves.

Definition at line 300 of file linear_solver.h.


The documentation for this class was generated from the following file: