Template Numerical Library version\ main:d08c4dec
Loading...
Searching...
No Matches
TNL::Solvers Namespace Reference

Namespace for solvers. More...

Namespaces

namespace  Linear
 Namespace for linear system solvers.
namespace  ODE
 Namespace for solvers of ordinary differential equations.

Classes

struct  ConfigTagDevice
struct  ConfigTagExplicitSolver
struct  ConfigTagIndex
struct  ConfigTagIndex< FastBuildConfigTag, long int >
struct  ConfigTagIndex< FastBuildConfigTag, short int >
struct  ConfigTagMeshResolve
struct  ConfigTagReal
struct  ConfigTagReal< FastBuildConfigTag, float >
struct  ConfigTagReal< FastBuildConfigTag, long double >
struct  ConfigTagTimeDiscretisation
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, ExplicitTimeDiscretisationTag >
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, ImplicitTimeDiscretisationTag >
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, SemiImplicitTimeDiscretisationTag >
class  DefaultBuildConfigTag
class  DirectSolver
 Base class for direct solvers. More...
class  ExplicitEulerSolverTag
class  ExplicitMersonSolverTag
class  ExplicitTimeDiscretisationTag
class  FastBuildConfigTag
class  GinkgoConvergenceLoggerMonitor
 A Ginkgo Convergence logger with a TNL iterative solver monitor. More...
class  ImplicitTimeDiscretisationTag
class  IterativeSolver
 Base class for iterative solvers. More...
class  IterativeSolverMonitor
 Object for monitoring convergence of iterative solvers. More...
class  SemiImplicitTimeDiscretisationTag
struct  Solver
struct  SolverConfig
class  SolverInitiator
class  SolverInitiatorDeviceResolver
class  SolverInitiatorDeviceResolver< ProblemSetter, Real, Device, ConfigTag, false >
class  SolverInitiatorDeviceResolver< ProblemSetter, Real, Device, ConfigTag, true >
class  SolverInitiatorIndexResolver
class  SolverInitiatorIndexResolver< ProblemSetter, Real, Device, Index, ConfigTag, false >
class  SolverInitiatorIndexResolver< ProblemSetter, Real, Device, Index, ConfigTag, true >
class  SolverInitiatorMeshResolver
class  SolverInitiatorMeshResolver< ProblemSetter, Real, Device, Index, ConfigTag, false >
class  SolverInitiatorMeshResolver< ProblemSetter, Real, Device, Index, ConfigTag, true >
class  SolverInitiatorRealResolver
class  SolverInitiatorRealResolver< ProblemSetter, Real, ConfigTag, false >
class  SolverInitiatorRealResolver< ProblemSetter, Real, ConfigTag, true >
class  SolverMonitor
 Base class for solver monitors. More...
class  SolverMonitorThread
 A RAII wrapper for launching the SolverMonitor's main loop in a separate thread. More...
class  SolverStarter
class  SolverStarterExplicitSolverSetter
class  SolverStarterExplicitSolverSetter< Problem, ExplicitSolverTag, ConfigTag, false >
class  SolverStarterExplicitSolverSetter< Problem, ExplicitSolverTag, ConfigTag, true >
class  SolverStarterTimeDiscretisationSetter
class  SolverStarterTimeDiscretisationSetter< Problem, ExplicitTimeDiscretisationTag, ConfigTag, true >
class  SolverStarterTimeDiscretisationSetter< Problem, ImplicitTimeDiscretisationTag, ConfigTag, true >
class  SolverStarterTimeDiscretisationSetter< Problem, SemiImplicitTimeDiscretisationTag, ConfigTag, true >
class  SolverStarterTimeDiscretisationSetter< Problem, TimeDiscretisationTag, ConfigTag, false >
class  StaticIterativeSolver
 Base class for iterative solvers. More...
class  TimeDependencyResolver
class  TimeDependencyResolver< Problem, ConfigTag, false >
class  TimeDependencyResolver< Problem, ConfigTag, true >
class  UserDefinedTimeDiscretisationSetter
class  UserDefinedTimeDiscretisationSetter< Problem, ConfigTag, void >

Functions

template<typename MatrixType>
std::shared_ptr< Linear::LinearSolver< MatrixType > > getLinearSolver (const std::string &name)
 Function returning shared pointer with linear solver given by its name in a form of a string.
std::vector< std::stringgetLinearSolverOptions ()
 Function returning available linear solvers.
template<typename MatrixType>
std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > getPreconditioner (const std::string &name)
 Function returning shared pointer with linear preconditioner given by its name in a form of a string.
std::vector< std::stringgetPreconditionerOptions ()
 Function returning available linear preconditioners.

Detailed Description

Namespace for solvers.

Function Documentation

◆ getLinearSolver()

template<typename MatrixType>
std::shared_ptr< Linear::LinearSolver< MatrixType > > TNL::Solvers::getLinearSolver ( const std::string & name)

Function returning shared pointer with linear solver given by its name in a form of a string.

Template Parameters
MatrixTypeis a type of matrix defining the system of linear equations.
Parameters
nameof the linear solver. The name can be one of the following:
  1. jacobi - for the Jacobi solver - TNL::Solvers::Linear::Jacobi.
  2. sor - for SOR solver - TNL::Solvers::Linear::SOR.
  3. cg - for CG solver - TNL::Solvers::Linear::CG.
  4. bicgstab - for BICGStab solver - TNL::Solvers::Linear::BICGStab.
  5. bicgstabl - for BICGStab(l) solver - TNL::Solvers::Linear::BICGStabL.
  6. gmres - for GMRES solver - TNL::Solvers::Linear::GMRES.
  7. tfqmr - for TFQMR solver - TNL::Solvers::Linear::TFQMR.
  8. idrs - for IDR(s) solver - TNL::Solvers::Linear::IDRs.
Returns
shared pointer with given linear solver.

The following example shows how to use this function:

1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/LinearSolverTypeResolver.h>
7
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12 /***
13 * Set the following matrix (dots represent zero matrix elements):
14 *
15 * / 2.5 -1 . . . \
16 * | -1 2.5 -1 . . |
17 * | . -1 2.5 -1. . |
18 * | . . -1 2.5 -1 |
19 * \ . . . -1 2.5 /
20 */
23 const int size( 5 );
24 auto matrix_ptr = std::make_shared< MatrixType >();
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
28 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
33 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
37 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
41 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
42 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
43 }
44 };
45
46 /***
47 * Set the matrix elements.
48 */
49 matrix_ptr->forAllRows( f );
50 std::cout << *matrix_ptr << std::endl;
51
52 /***
53 * Set the right-hand side vector.
54 */
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
59 std::cout << "Vector b = " << b << std::endl;
60
61 /***
62 * Solve the linear system using diagonal (Jacobi) preconditioner.
63 */
64 auto solver_ptr = TNL::Solvers::getLinearSolver< MatrixType >( "tfqmr" );
65 auto preconditioner_ptr = TNL::Solvers::getPreconditioner< MatrixType >( "diagonal" );
66 preconditioner_ptr->update( matrix_ptr );
67 solver_ptr->setMatrix( matrix_ptr );
68 solver_ptr->setPreconditioner( preconditioner_ptr );
69 solver_ptr->setConvergenceResidue( 1.0e-6 );
70 solver_ptr->solve( b, x );
71 std::cout << "Vector x = " << x << std::endl;
72}
73
74int
75main( int argc, char* argv[] )
76{
77 std::cout << "Solving linear system on host: " << std::endl;
78 iterativeLinearSolverExample< TNL::Devices::Sequential >();
79
80#ifdef __CUDACC__
81 std::cout << "Solving linear system on CUDA device: " << std::endl;
82 iterativeLinearSolverExample< TNL::Devices::Cuda >();
83#endif
84}
#define __cuda_callable__
Definition Macros.h:49
Vector extends Array with algebraic operations.
Definition Vector.h:36
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:55
T endl(T... args)
T make_shared(T... args)
std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > getPreconditioner(const std::string &name)
Function returning shared pointer with linear preconditioner given by its name in a form of a string.
Definition LinearSolverTypeResolver.h:138
std::shared_ptr< Linear::LinearSolver< MatrixType > > getLinearSolver(const std::string &name)
Function returning shared pointer with linear solver given by its name in a form of a string.
Definition LinearSolverTypeResolver.h:84

The result looks as follows:

Solving linear system on host:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Vector x = [ 1, 1, 1, 1, 1 ]
Solving linear system on CUDA device:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Vector x = [ 1, 1, 1, 1, 1 ]

◆ getLinearSolverOptions()

std::vector< std::string > TNL::Solvers::getLinearSolverOptions ( )
inline

Function returning available linear solvers.

Returns
container with names of available linear solvers.

◆ getPreconditioner()

template<typename MatrixType>
std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > TNL::Solvers::getPreconditioner ( const std::string & name)

Function returning shared pointer with linear preconditioner given by its name in a form of a string.

Template Parameters
MatrixTypeis a type of matrix defining the system of linear equations.
Parameters
nameof the linear preconditioner. The name can be one of the following:
  1. none - for none preconditioner
  2. diagonal - for diagonal/Jacobi preconditioner - TNL::Solvers::Linear::Preconditioners::Diagonal
  3. ilu0 - for ILU(0) preconditioner - TNL::Solvers::Linear::Preconditioners::ILU0
  4. ilut - for ILU with thresholding preconditioner - TNL::Solvers::Linear::Preconditioners::ILUT
Returns
shared pointer with given linear preconditioner.
1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/LinearSolverTypeResolver.h>
7
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12 /***
13 * Set the following matrix (dots represent zero matrix elements):
14 *
15 * / 2.5 -1 . . . \
16 * | -1 2.5 -1 . . |
17 * | . -1 2.5 -1. . |
18 * | . . -1 2.5 -1 |
19 * \ . . . -1 2.5 /
20 */
23 const int size( 5 );
24 auto matrix_ptr = std::make_shared< MatrixType >();
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
28 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
33 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
37 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
41 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
42 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
43 }
44 };
45
46 /***
47 * Set the matrix elements.
48 */
49 matrix_ptr->forAllRows( f );
50 std::cout << *matrix_ptr << std::endl;
51
52 /***
53 * Set the right-hand side vector.
54 */
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
59 std::cout << "Vector b = " << b << std::endl;
60
61 /***
62 * Solve the linear system using diagonal (Jacobi) preconditioner.
63 */
64 auto solver_ptr = TNL::Solvers::getLinearSolver< MatrixType >( "tfqmr" );
65 auto preconditioner_ptr = TNL::Solvers::getPreconditioner< MatrixType >( "diagonal" );
66 preconditioner_ptr->update( matrix_ptr );
67 solver_ptr->setMatrix( matrix_ptr );
68 solver_ptr->setPreconditioner( preconditioner_ptr );
69 solver_ptr->setConvergenceResidue( 1.0e-6 );
70 solver_ptr->solve( b, x );
71 std::cout << "Vector x = " << x << std::endl;
72}
73
74int
75main( int argc, char* argv[] )
76{
77 std::cout << "Solving linear system on host: " << std::endl;
78 iterativeLinearSolverExample< TNL::Devices::Sequential >();
79
80#ifdef __CUDACC__
81 std::cout << "Solving linear system on CUDA device: " << std::endl;
82 iterativeLinearSolverExample< TNL::Devices::Cuda >();
83#endif
84}

The result looks as follows:

Solving linear system on host:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Vector x = [ 1, 1, 1, 1, 1 ]
Solving linear system on CUDA device:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Vector x = [ 1, 1, 1, 1, 1 ]

◆ getPreconditionerOptions()

std::vector< std::string > TNL::Solvers::getPreconditionerOptions ( )
inline

Function returning available linear preconditioners.

Returns
container with names of available linear preconditioners.