KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
diagonal_preconditioner.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 //
13 
14 #if !defined(KRATOS_DIAGONAL_PRECONDITIONER_H_INCLUDED )
15 #define KRATOS_DIAGONAL_PRECONDITIONER_H_INCLUDED
16 
17 // System includes
18 
19 
20 // External includes
21 #include <boost/numeric/ublas/vector.hpp>
22 
23 
24 // Project includes
25 #include "includes/define.h"
26 
27 
28 namespace Kratos
29 {
30 
33 
37 
41 
45 
49 
52 
54 
56 template<class TSparseSpaceType, class TDenseSpaceType>
57 class DiagonalPreconditioner : public Preconditioner<TSparseSpaceType, TDenseSpaceType>
58 {
59 public:
62 
65 
67 
68  typedef typename TSparseSpaceType::DataType DataType;
69 
71 
73 
75 
79 
82 
85  : BaseType(Other), mDiagonal(Other.mDiagonal), mTemp(Other.mTemp) {}
86 
89 
90 
94 
97  {
98  BaseType::operator=(Other);
99  mDiagonal = Other.mDiagonal;
100  mTemp = Other.mTemp;
101  return *this;
102  }
103 
104 
108 
109 
116  void Initialize(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
117  {
118  mDiagonal.resize(TSparseSpaceType::Size(rX));
119  mTemp.resize(TSparseSpaceType::Size(rX));
120 
121  const DataType zero = DataType();
122 
123  IndexPartition<std::size_t>(rA.size1()).for_each([&](std::size_t Index){
124  double diag_Aii = rA(Index,Index);
125  if(diag_Aii != zero)
126  mDiagonal[Index] = 1.00 / std::sqrt(std::abs(diag_Aii));
127  else
128  KRATOS_THROW_ERROR(std::logic_error,"zero found in the diagonal. Diagonal preconditioner can not be used","");
129  });
130 
131  }
132 
134  {
135  BaseType::Initialize(rA, rX, rB);
136  }
137 
138  void Mult(SparseMatrixType& rA, VectorType& rX, VectorType& rY) override
139  {
141  mTemp[Index] = rX[Index] * mDiagonal[Index];
142  });
143 
144  TSparseSpaceType::Mult(rA,mTemp, rY);
145  ApplyLeft(rY);
146  }
147 
149  {
151  mTemp[Index] = rX[Index] * mDiagonal[Index];
152  });
153 
154  TSparseSpaceType::TransposeMult(rA,mTemp, rY);
155  ApplyRight(rY);
156  }
157 
159  {
161  rX[Index] *= mDiagonal[Index];
162  });
163 
164  return rX;
165  }
166 
168  {
170  rX[Index] *= mDiagonal[Index];
171  });
172 
173  return rX;
174  }
175 
183  {
185  rX[Index] *= mDiagonal[Index];
186  });
187 
188  return rX;
189  }
190 
192  {
194  rX[Index] *= mDiagonal[Index];
195  });
196 
197  return rX;
198  }
199 
201  {
203  rX[Index] /= mDiagonal[Index];
204  });
205 
206  return rX;
207  }
208 
210  {
212  rX[Index] *= mDiagonal[Index];
213  });
214 
215  return rX;
216  }
220 
221 
225 
226 
230 
232  std::string Info() const override
233  {
234  return "Diagonal preconditioner";
235  }
236 
238  void PrintInfo(std::ostream& OStream) const override
239  {
240  OStream << "Diagonal preconditioner";
241  }
242 
243 
247 
248 
250 
251 protected:
254 
255 
259 
260 
264 
265 
269 
270 
274 
275 
279 
280 
284 
285 
287 
288 private:
291 
292 
296 
297  VectorType mDiagonal;
298 
299  VectorType mTemp;
300 
301 
305 
306 
310 
311 
315 
316 
320 
321 
325 
326 
328 
329 }; // Class DiagonalPreconditioner
330 
332 
334 
337 
338 
342 
343 
345 template<class TSparseSpaceType, class TDenseSpaceType>
346 inline std::istream& operator >> (std::istream& IStream,
348 {
349  return IStream;
350 }
351 
353 template<class TSparseSpaceType, class TDenseSpaceType>
354 inline std::ostream& operator << (std::ostream& OStream,
356 {
357  rThis.PrintInfo(OStream);
358  OStream << std::endl;
359  rThis.PrintData(OStream);
360 
361  return OStream;
362 }
364 
365 
366 } // namespace Kratos.
367 
368 #endif // KRATOS_DIAGONAL_PRECONDITIONER_H_INCLUDED defined
DiagonalPreconditioner class.
Definition: diagonal_preconditioner.h:58
Preconditioner< TSparseSpaceType, TDenseSpaceType > BaseType
Definition: diagonal_preconditioner.h:66
~DiagonalPreconditioner() override
Destructor.
Definition: diagonal_preconditioner.h:88
void Mult(SparseMatrixType &rA, VectorType &rX, VectorType &rY) override
Definition: diagonal_preconditioner.h:138
TSparseSpaceType::DataType DataType
Definition: diagonal_preconditioner.h:68
void PrintInfo(std::ostream &OStream) const override
Print information about this object.
Definition: diagonal_preconditioner.h:238
void TransposeMult(SparseMatrixType &rA, VectorType &rX, VectorType &rY) override
Definition: diagonal_preconditioner.h:148
void Initialize(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: diagonal_preconditioner.h:133
VectorType & ApplyLeft(VectorType &rX) override
Definition: diagonal_preconditioner.h:158
KRATOS_CLASS_POINTER_DEFINITION(DiagonalPreconditioner)
Counted pointer of DiagonalPreconditioner.
DiagonalPreconditioner(const DiagonalPreconditioner &Other)
Copy constructor.
Definition: diagonal_preconditioner.h:84
DiagonalPreconditioner & operator=(const DiagonalPreconditioner &Other)
Assignment operator.
Definition: diagonal_preconditioner.h:96
VectorType & ApplyInverseRight(VectorType &rX) override
Definition: diagonal_preconditioner.h:200
VectorType & ApplyRight(VectorType &rX) override
Definition: diagonal_preconditioner.h:167
std::string Info() const override
Return information about this object.
Definition: diagonal_preconditioner.h:232
TDenseSpaceType::MatrixType DenseMatrixType
Definition: diagonal_preconditioner.h:74
TSparseSpaceType::MatrixType SparseMatrixType
Definition: diagonal_preconditioner.h:70
VectorType & ApplyTransposeRight(VectorType &rX) override
Definition: diagonal_preconditioner.h:191
DiagonalPreconditioner()
Default constructor.
Definition: diagonal_preconditioner.h:81
VectorType & ApplyTransposeLeft(VectorType &rX) override
Definition: diagonal_preconditioner.h:182
VectorType & Finalize(VectorType &rX) override
Definition: diagonal_preconditioner.h:209
TSparseSpaceType::VectorType VectorType
Definition: diagonal_preconditioner.h:72
void Initialize(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: diagonal_preconditioner.h:116
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
Preconditioner class.
Definition: preconditioner.h:75
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: preconditioner.h:278
virtual void Initialize(SparseMatrixType &rA, VectorType &rX, VectorType &rB)
Definition: preconditioner.h:123
Preconditioner & operator=(const Preconditioner &Other)
Assignment operator.
Definition: preconditioner.h:108
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
void TransposeMult(SparseSpaceType &dummy, SparseSpaceType::MatrixType &rA, SparseSpaceType::VectorType &rX, SparseSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:104
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
void Mult(TSpaceType &dummy, typename TSpaceType::MatrixType &rA, typename TSpaceType::VectorType &rX, typename TSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:98
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
def Index()
Definition: hdf5_io_tools.py:38
zero
Definition: test_pureconvectionsolver_benchmarking.py:94