FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
richardson.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/solver/iterative.hpp>
11
12namespace FEAT
13{
14 namespace Solver
15 {
29 template<typename Matrix_, typename Filter_>
30 class Richardson :
31 public PreconditionedIterativeSolver<typename Matrix_::VectorTypeR>
32 {
33 public:
34 typedef Matrix_ MatrixType;
35 typedef Filter_ FilterType;
36 typedef typename MatrixType::VectorTypeR VectorType;
37 typedef typename MatrixType::DataType DataType;
39
41
42 protected:
44 const MatrixType& _system_matrix;
46 const FilterType& _system_filter;
48 DataType _omega;
50 VectorType _vec_def;
52 VectorType _vec_cor;
53
54 public:
70 explicit Richardson(const MatrixType& matrix, const FilterType& filter,
71 DataType omega = DataType(1), std::shared_ptr<PrecondType> precond = nullptr) :
72 BaseClass("Richardson", precond),
73 _system_matrix(matrix),
74 _system_filter(filter),
75 _omega(omega)
76 {
77 // set communicator by system matrix
78 this->_set_comm_by_matrix(matrix);
79 }
80
100 explicit Richardson(const String& section_name, const PropertyMap* section,
101 const MatrixType& matrix, const FilterType& filter,
102 std::shared_ptr<PrecondType> precond = nullptr) :
103 BaseClass("Richardson", section_name, section, precond),
104 _system_matrix(matrix),
105 _system_filter(filter),
106 _omega(1)
107 {
108 // set communicator by system matrix
109 this->_set_comm_by_matrix(matrix);
110 // Check if we have set omega
111 auto omega_p = section->query("omega");
112 if(omega_p.second)
113 {
114 set_omega(DataType(std::stod(omega_p.first)));
115 }
116 }
117
121 virtual ~Richardson()
122 {
123 }
124
125 virtual String name() const override
126 {
127 return "Richardson";
128 }
129
137 void set_omega(DataType omega)
138 {
139 XASSERT(omega > DataType(0));
140 _omega = omega;
141 }
142
143 virtual void init_symbolic() override
144 {
146 // create two temporary vectors
147 _vec_def = this->_system_matrix.create_vector_r();
148 _vec_cor = this->_system_matrix.create_vector_r();
149 }
150
151 virtual void done_symbolic() override
152 {
153 this->_vec_cor.clear();
154 this->_vec_def.clear();
156 }
157
158 virtual Status apply(VectorType& vec_cor, const VectorType& vec_def) override
159 {
160 // save defect
161 this->_vec_def.copy(vec_def);
162
163 // clear solution vector
164 vec_cor.format();
165
166 // apply
167 this->_status = _apply_intern(vec_cor, vec_def);
168 this->plot_summary();
169 return this->_status;
170 }
171
172 virtual Status correct(VectorType& vec_sol, const VectorType& vec_rhs) override
173 {
174 // compute defect
175 this->_system_matrix.apply(this->_vec_def, vec_sol, vec_rhs, -DataType(1));
176 this->_system_filter.filter_def(this->_vec_def);
177
178 // apply
179 this->_status = _apply_intern(vec_sol, vec_rhs);
180 this->plot_summary();
181 return this->_status;
182 }
183
184 protected:
185 virtual Status _apply_intern(VectorType& vec_sol, const VectorType& vec_rhs)
186 {
187 IterationStats pre_iter(*this);
188 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
189
190 VectorType& vec_def(this->_vec_def);
191 VectorType& vec_cor(this->_vec_cor);
192 const MatrixType& matrix(this->_system_matrix);
193 const FilterType& filter(this->_system_filter);
194
195 // compute initial defect
196 Status status = this->_set_initial_defect(vec_def, vec_sol);
197
198 pre_iter.destroy();
199
200 // start iterating
201 while(status == Status::progress)
202 {
203 IterationStats stat(*this);
204
205 // apply preconditioner
206 if(!this->_apply_precond(vec_cor, vec_def, filter))
207 {
208 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
209 return Status::aborted;
210 }
211 //filter.filter_cor(vec_cor);
212
213 // update solution vector
214 vec_sol.axpy(vec_cor, this->_omega);
215
216 // compute new defect vector
217 matrix.apply(vec_def, vec_sol, vec_rhs, -DataType(1));
218 filter.filter_def(vec_def);
219
220 // compute new defect norm
221 status = this->_set_new_defect(vec_def, vec_sol);
222 }
223
224 // return our status
225 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), status, this->get_num_iter()));
226 return status;
227 }
228 }; // class Richardson<...>
229
248 template<typename Matrix_, typename Filter_>
249 inline std::shared_ptr<Richardson<Matrix_, Filter_>> new_richardson(
250 const Matrix_& matrix, const Filter_& filter,
251 typename Matrix_::DataType omega = typename Matrix_::DataType(1),
252 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond = nullptr)
253 {
254 return std::make_shared<Richardson<Matrix_, Filter_>>(matrix, filter, omega, precond);
255 }
256
278 template<typename Matrix_, typename Filter_>
279 inline std::shared_ptr<Richardson<Matrix_, Filter_>> new_richardson(
280 const String& section_name, const PropertyMap* section,
281 const Matrix_& matrix, const Filter_& filter,
282 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond = nullptr)
283 {
284 return std::make_shared<Richardson<Matrix_, Filter_>>(section_name, section, matrix, filter, precond);
285 }
286 } // namespace Solver
287} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
FEAT Kernel base header.
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
Index get_num_iter() const
Returns number of performed iterations.
Definition: iterative.hpp:462
Status _status
current status of the solver
Definition: iterative.hpp:213
void _set_comm_by_matrix(const Matrix_ &matrix)
Sets the communicator for the solver from a matrix.
Definition: iterative.hpp:680
virtual Status _set_new_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the new (next) defect vector.
Definition: iterative.hpp:924
virtual void plot_summary() const
Plot a summary of the last solver run.
Definition: iterative.hpp:627
virtual Status _set_initial_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the initial defect vector.
Definition: iterative.hpp:802
Abstract base-class for preconditioned iterative solvers.
Definition: iterative.hpp:1027
virtual void init_symbolic() override
Symbolic initialization method.
Definition: iterative.hpp:1086
bool _apply_precond(VectorType &vec_cor, const VectorType &vec_def, const Filter_ &filter)
Applies the preconditioner onto a defect vector.
Definition: iterative.hpp:1140
virtual void done_symbolic() override
Symbolic finalization method.
Definition: iterative.hpp:1110
(Preconditioned) Richardson solver implementation
Definition: richardson.hpp:32
VectorType _vec_cor
correction vector
Definition: richardson.hpp:52
Richardson(const MatrixType &matrix, const FilterType &filter, DataType omega=DataType(1), std::shared_ptr< PrecondType > precond=nullptr)
Constructor.
Definition: richardson.hpp:70
virtual ~Richardson()
Empty virtual destructor.
Definition: richardson.hpp:121
void set_omega(DataType omega)
Sets the damping parameter.
Definition: richardson.hpp:137
const FilterType & _system_filter
the filter for the solver
Definition: richardson.hpp:46
virtual String name() const override
Returns a descriptive string.
Definition: richardson.hpp:125
Richardson(const String &section_name, const PropertyMap *section, const MatrixType &matrix, const FilterType &filter, std::shared_ptr< PrecondType > precond=nullptr)
Constructor using a PropertyMap.
Definition: richardson.hpp:100
DataType _omega
damping parameter
Definition: richardson.hpp:48
const MatrixType & _system_matrix
the matrix for the solver
Definition: richardson.hpp:44
virtual void done_symbolic() override
Symbolic finalization method.
Definition: richardson.hpp:151
VectorType _vec_def
defect vector
Definition: richardson.hpp:50
virtual void init_symbolic() override
Symbolic initialization method.
Definition: richardson.hpp:143
Polymorphic solver interface.
Definition: base.hpp:183
String class implementation.
Definition: string.hpp:46
std::shared_ptr< Richardson< Matrix_, Filter_ > > new_richardson(const Matrix_ &matrix, const Filter_ &filter, typename Matrix_::DataType omega=typename Matrix_::DataType(1), std::shared_ptr< SolverBase< typename Matrix_::VectorTypeL > > precond=nullptr)
Creates a new Richardson solver object.
Definition: richardson.hpp:249
Status
Solver status return codes enumeration.
Definition: base.hpp:47
@ progress
continue iteration (internal use only)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
FEAT namespace.
Definition: adjactor.hpp:12