Loading...
Searching...
No Matches
jacobi.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "solver.hpp"
4#include "linalg/operator.hpp"
5
7
8/// @brief Jacobi iterative solver for linear systems.
9///
10/// Satisfies the SolverLike concept (see solver.hpp).
11/// Uses a diagonal preconditioner and supports relaxation.
12/// The update rule is:
13/// \f[ x^{(k+1)} = x^{(k)} + \omega D^{-1} (b - Ax^{(k)}) \f]
14/// where \f$ D \f$ is the diagonal of \f$ A \f$ and \f$ \omega \f$ is the relaxation parameter.
15/// @tparam OperatorT Operator type (must satisfy OperatorLike).
16template < OperatorLike OperatorT >
17class Jacobi
18{
19 public:
20 /// @brief Operator type to be solved.
21 using OperatorType = OperatorT;
22 /// @brief Solution vector type.
24 /// @brief Right-hand side vector type.
26
27 /// @brief Scalar type for computations.
28 using ScalarType = SolutionVectorType::ScalarType;
29
30 /// @brief Construct a Jacobi solver.
31 /// @param inverse_diagonal Inverse of the diagonal of the operator.
32 /// @param iterations Number of Jacobi iterations to perform.
33 /// @param tmp Temporary vector for workspace.
34 /// @param omega Relaxation parameter (default 1.0).
36 const SolutionVectorType& inverse_diagonal,
37 const int iterations,
38 const SolutionVectorType& tmp,
39 const ScalarType omega = 1.0 )
40 : inverse_diagonal_( inverse_diagonal )
41 , iterations_( iterations )
42 , tmp_( tmp )
43 , omega_( omega )
44 {}
45
46 /// @brief Solve the linear system using Jacobi iteration.
47 /// Applies the update rule for the specified number of iterations.
48 /// @param A Operator (matrix).
49 /// @param x Solution vector (output).
50 /// @param b Right-hand side vector (input).
52 {
53 for ( int iteration = 0; iteration < iterations_; ++iteration )
54 {
55 apply( A, x, tmp_ );
56 lincomb( tmp_, { 1.0, -1.0 }, { b, tmp_ } );
57 scale_in_place( tmp_, inverse_diagonal_ );
58 lincomb( x, { 1.0, omega_ }, { x, tmp_ } );
59 }
60 }
61
63 return inverse_diagonal_;
64 }
65
66 private:
67 SolutionVectorType inverse_diagonal_; ///< Inverse diagonal vector.
68 int iterations_; ///< Number of iterations.
69 SolutionVectorType tmp_; ///< Temporary workspace vector.
70 ScalarType omega_; ///< Relaxation parameter.
71};
72
73/// @brief Static assertion: Jacobi satisfies SolverLike concept.
74static_assert( SolverLike< Jacobi< linalg::detail::DummyConcreteOperator > > );
75
76} // namespace terra::linalg::solvers
Jacobi iterative solver for linear systems.
Definition jacobi.hpp:18
SolutionVectorType & get_inverse_diagonal()
Definition jacobi.hpp:62
Jacobi(const SolutionVectorType &inverse_diagonal, const int iterations, const SolutionVectorType &tmp, const ScalarType omega=1.0)
Construct a Jacobi solver.
Definition jacobi.hpp:35
SolutionVectorType::ScalarType ScalarType
Scalar type for computations.
Definition jacobi.hpp:28
DstOf< OperatorType > RHSVectorType
Right-hand side vector type.
Definition jacobi.hpp:25
SrcOf< OperatorType > SolutionVectorType
Solution vector type.
Definition jacobi.hpp:23
void solve_impl(OperatorType &A, SolutionVectorType &x, const RHSVectorType &b)
Solve the linear system using Jacobi iteration. Applies the update rule for the specified number of i...
Definition jacobi.hpp:51
OperatorT OperatorType
Operator type to be solved.
Definition jacobi.hpp:21
Definition block_preconditioner_2x2.hpp:7
void lincomb(Vector &y, const std::vector< ScalarOf< Vector > > &c, const std::vector< Vector > &x, const ScalarOf< Vector > &c0)
Compute a linear combination of vectors. Implements: .
Definition vector.hpp:72
Operator::SrcVectorType SrcOf
Alias for the source vector type of an operator.
Definition operator.hpp:145
void apply(LinearForm &L, typename LinearForm::DstVectorType &dst)
Apply a linear form and write to a destination vector.
Definition linear_form.hpp:37
Operator::DstVectorType DstOf
Alias for the destination vector type of an operator.
Definition operator.hpp:149
void scale_in_place(Vector &y, const Vector &x)
Scale a vector in place with another vector. For each entry , computes .
Definition vector.hpp:137