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.
iterative_solver.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: Pooyan Dadvand
11 // Riccardo Rossi
12 //
13 
14 #if !defined(KRATOS_ITERATIVE_SOLVER_H_INCLUDED )
15 #define KRATOS_ITERATIVE_SOLVER_H_INCLUDED
16 
17 // System includes
18 #include <string>
19 #include <iostream>
20 
21 // External includes
22 
23 // Project includes
24 #include "includes/define.h"
28 
29 namespace Kratos
30 {
31 
34 
38 
42 
46 
50 
52 
64 template<class TSparseSpaceType, class TDenseSpaceType,
65  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
66  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
67 class IterativeSolver : public LinearSolver<TSparseSpaceType, TDenseSpaceType, TReordererType>
68 {
69 public:
72 
75 
77 
79 
81 
83 
84  typedef TPreconditionerType PreconditionerType;
85 
88 
92 
95  : mResidualNorm(0)
97  , mBNorm(0)
98  , mpPreconditioner(new TPreconditionerType())
99  , mTolerance(0)
100  , mMaxIterationsNumber(0)
101  {
102  }
103 
104  IterativeSolver(double NewTolerance)
105  : mResidualNorm(0)
106  , mIterationsNumber(0)
107  , mBNorm(0)
108  , mpPreconditioner(new TPreconditionerType())
109  , mTolerance(NewTolerance)
110  , mMaxIterationsNumber(0)
111  {
112  }
113 
114  IterativeSolver(double NewTolerance, unsigned int NewMaxIterationsNumber)
115  : mResidualNorm(0)
116  , mIterationsNumber(0)
117  , mBNorm(0)
118  , mpPreconditioner(new TPreconditionerType())
119  , mTolerance(NewTolerance)
120  , mMaxIterationsNumber(NewMaxIterationsNumber) {}
121 
122  IterativeSolver(double NewTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner) :
124  mpPreconditioner(pNewPreconditioner),
125  mTolerance(NewTolerance),
126  mMaxIterationsNumber(NewMaxIterationsNumber) {}
127 
129  typename TPreconditionerType::Pointer pNewPreconditioner = Kratos::make_shared<TPreconditionerType>()
130  ):
131  mResidualNorm(0),
133  mBNorm(0),
134  mpPreconditioner(pNewPreconditioner)
135  {
136  KRATOS_TRY
137 
138  Parameters default_parameters( R"(
139  {
140  "solver_type": "IterativeSolver",
141  "tolerance" : 1.0e-6,
142  "max_iteration" : 200,
143  "preconditioner_type": "none",
144  "scaling":false
145  } )" );
146 
147  //now validate agains defaults -- this also ensures no type mismatch
148  settings.ValidateAndAssignDefaults(default_parameters);
149 
150  this->SetTolerance( settings["tolerance"].GetDouble() );
151  this->SetMaxIterationsNumber( settings["max_iteration"].GetInt() );
152 
153 
154  KRATOS_CATCH("")
155  }
156 
158  IterativeSolver(const IterativeSolver& Other) : BaseType(Other),
160  mpPreconditioner(Other.mpPreconditioner),
161  mTolerance(Other.mTolerance),
162  mMaxIterationsNumber(Other.mMaxIterationsNumber)
163  {
164 
165  }
166 
168  ~IterativeSolver() override {}
169 
170 
174 
177  {
178  BaseType::operator=(Other);
182  mBNorm = Other.mBNorm;
183  return *this;
184  }
185 
195  {
196  GetPreconditioner()->InitializeSolutionStep(rA,rX,rB);
197  }
198 
206  {
207  GetPreconditioner()->FinalizeSolutionStep(rA,rX,rB);
208  }
209 
214  void Clear() override
215  {
216  GetPreconditioner()->Clear();
217  }
218 
226  {
228  return true;
229  else
230  return false;
231  }
232 
240  SparseMatrixType& rA,
241  VectorType& rX,
242  VectorType& rB,
243  typename ModelPart::DofsArrayType& rdof_set,
244  ModelPart& r_model_part
245  ) override
246  {
248  GetPreconditioner()->ProvideAdditionalData(rA,rX,rB,rdof_set,r_model_part);
249  }
250 
254 
255 
259 
260  virtual typename TPreconditionerType::Pointer GetPreconditioner(void)
261  {
262  return mpPreconditioner;
263  }
264 
265  virtual const typename TPreconditionerType::Pointer GetPreconditioner(void) const
266  {
267  return mpPreconditioner;
268  }
269 
270  virtual void SetPreconditioner(typename TPreconditionerType::Pointer pNewPreconditioner)
271  {
272  mpPreconditioner = pNewPreconditioner;
273  }
274 
275 // virtual typename TStopCriteriaType::Pointer GetStopCriteria(void)
276 // {
277 // return mpStopCriteria;
278 // }
279 
280 // virtual void SetStopCriteria(typename TStopCriteriaType::Pointer pNewStopCriteria)
281 // {
282 // mpStopCriteria = pNewStopCriteria;
283 // }
284 
285  virtual void SetMaxIterationsNumber(unsigned int NewMaxIterationsNumber)
286  {
287  mMaxIterationsNumber = NewMaxIterationsNumber;
288  }
289 
291  {
292  return mMaxIterationsNumber;
293  }
294 
295  virtual void SetIterationsNumber(unsigned int NewIterationNumber)
296  {
297  mIterationsNumber = NewIterationNumber;
298  }
299 
301  {
302  return mIterationsNumber;
303  }
304 
305  void SetTolerance(double NewTolerance) override
306  {
307  mTolerance = NewTolerance;
308  }
309 
310  double GetTolerance() override
311  {
312  return mTolerance;
313  }
314 
315  virtual void SetResidualNorm(double NewResidualNorm)
316  {
317  if (mIterationsNumber == 1)
318  mFirstResidualNorm = NewResidualNorm;
319  mResidualNorm = NewResidualNorm;
320  }
321 
322  virtual double GetResidualNorm()
323  {
324  return mResidualNorm;
325  }
326 
330 
331  virtual bool IterationNeeded()
332  {
333  return (mIterationsNumber < mMaxIterationsNumber) && (mResidualNorm > mTolerance * mBNorm);
334  }
335 
336  virtual bool IsConverged()
337  {
338  return (mResidualNorm <= mTolerance * mBNorm);
339  }
340 
344 
346  std::string Info() const override
347  {
348  std::stringstream buffer;
349  buffer << "Iterative solver with " << GetPreconditioner()->Info();
350  return buffer.str();
351  }
352 
354  void PrintInfo(std::ostream& rOStream) const override
355  {
356  rOStream << Info();
357  }
358 
360  void PrintData(std::ostream& rOStream) const override
361  {
362  if (mBNorm == 0.00)
363  if (mResidualNorm != 0.00)
364  rOStream << " Residual ratio : infinite" << std::endl;
365  else
366  rOStream << " Residual ratio : 0" << std::endl;
367  else
368  {
369  rOStream << " Initial Residual ratio : " << mBNorm << std::endl;
370  rOStream << " Final Residual ratio : " << mResidualNorm << std::endl;
371  rOStream << " Residual ratio : " << mResidualNorm / mBNorm << std::endl;
372  rOStream << " Slope : " << (mResidualNorm - mBNorm) / mIterationsNumber << std::endl;
373  }
374 
375  rOStream << " Tolerance : " << mTolerance << std::endl;
376  rOStream << " Number of iterations : " << mIterationsNumber << std::endl;
377  rOStream << " Maximum number of iterations : " << mMaxIterationsNumber;
378  if (mMaxIterationsNumber == mIterationsNumber)
379  rOStream << std::endl << "!!!!!!!!!!!! ITERATIVE SOLVER NON CONVERGED !!!!!!!!!!!!" << mMaxIterationsNumber;
380  }
381 
382 
386 
387 
389 
390 protected:
393 
394 
398 
400 
402 
404 
405  double mBNorm;
406 
410 
411 
415 
417  {
418  GetPreconditioner()->Mult(rA, rX, rY);
419  }
420 
422  {
423  GetPreconditioner()->TransposeMult(rA, rX, rY);
424  }
425 
429 
430 
434 
435 
439 
440 
442 
443 private:
446 
447 
451 
453  typename TPreconditionerType::Pointer mpPreconditioner;
454 
456  // typename TStopCriteriaType::Pointer mpStopCriteria;
457 
458  double mTolerance;
459 
460  IndexType mMaxIterationsNumber;
461 
465 
466 
470 
471 
475 
476 
480 
481 
485 
486 
488 
489 }; // Class IterativeSolver
490 
492 
495 
496 
500 
501 
503 template<class TSparseSpaceType, class TDenseSpaceType, class TPreconditionerType,
504  class TReordererType>
505 inline std::istream& operator >> (std::istream& IStream,
506  IterativeSolver<TSparseSpaceType, TDenseSpaceType,
507  TPreconditionerType, TReordererType>& rThis)
508 {
509  return IStream;
510 }
511 
513 template<class TSparseSpaceType, class TDenseSpaceType, class TPreconditionerType,
514  class TReordererType>
515 inline std::ostream& operator << (std::ostream& OStream,
516  const IterativeSolver<TSparseSpaceType, TDenseSpaceType,
517  TPreconditionerType, TReordererType>& rThis)
518 {
519  rThis.PrintInfo(OStream);
520  OStream << std::endl;
521  rThis.PrintData(OStream);
522 
523  return OStream;
524 }
525 
527 
528 
529 } // namespace Kratos.
530 
531 #endif // KRATOS_ITERATIVE_SOLVER_H_INCLUDED defined
532 
533 
Base class for all the iterative solvers in Kratos.
Definition: iterative_solver.h:68
IterativeSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner=Kratos::make_shared< TPreconditionerType >())
Definition: iterative_solver.h:128
virtual const TPreconditionerType::Pointer GetPreconditioner(void) const
Definition: iterative_solver.h:265
void SetTolerance(double NewTolerance) override
This method allows to set the tolerance in the linear solver.
Definition: iterative_solver.h:305
IndexType mIterationsNumber
Definition: iterative_solver.h:403
bool AdditionalPhysicalDataIsNeeded() override
Definition: iterative_solver.h:225
virtual bool IterationNeeded()
Definition: iterative_solver.h:331
IterativeSolver(const IterativeSolver &Other)
Copy constructor.
Definition: iterative_solver.h:158
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: iterative_solver.h:360
IndexType GetIterationsNumber() override
Definition: iterative_solver.h:300
double mResidualNorm
Definition: iterative_solver.h:399
double mFirstResidualNorm
Definition: iterative_solver.h:401
void Clear() override
Definition: iterative_solver.h:214
std::string Info() const override
Turn back information as a string.
Definition: iterative_solver.h:346
void PreconditionedMult(SparseMatrixType &rA, VectorType &rX, VectorType &rY)
Definition: iterative_solver.h:416
TSparseSpaceType::MatrixType SparseMatrixType
Definition: iterative_solver.h:78
virtual void SetIterationsNumber(unsigned int NewIterationNumber)
Definition: iterative_solver.h:295
double mBNorm
Definition: iterative_solver.h:405
IterativeSolver()
Default constructor.
Definition: iterative_solver.h:94
virtual void SetMaxIterationsNumber(unsigned int NewMaxIterationsNumber)
Definition: iterative_solver.h:285
KRATOS_CLASS_POINTER_DEFINITION(IterativeSolver)
Pointer definition of IterativeSolver.
IterativeSolver & operator=(const IterativeSolver &Other)
Assignment operator.
Definition: iterative_solver.h:176
virtual IndexType GetMaxIterationsNumber()
Definition: iterative_solver.h:290
void ProvideAdditionalData(SparseMatrixType &rA, VectorType &rX, VectorType &rB, typename ModelPart::DofsArrayType &rdof_set, ModelPart &r_model_part) override
Definition: iterative_solver.h:239
void InitializeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: iterative_solver.h:194
void FinalizeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: iterative_solver.h:205
TSparseSpaceType::IndexType IndexType
The index type definition to be consistent.
Definition: iterative_solver.h:87
void PreconditionedTransposeMult(SparseMatrixType &rA, VectorType &rX, VectorType &rY)
Definition: iterative_solver.h:421
virtual void SetPreconditioner(typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: iterative_solver.h:270
TPreconditionerType PreconditionerType
Definition: iterative_solver.h:84
LinearSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
Definition: iterative_solver.h:76
double GetTolerance() override
This method allows to get the tolerance in the linear solver.
Definition: iterative_solver.h:310
virtual TPreconditionerType::Pointer GetPreconditioner(void)
Definition: iterative_solver.h:260
IterativeSolver(double NewTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: iterative_solver.h:122
virtual bool IsConverged()
Definition: iterative_solver.h:336
virtual double GetResidualNorm()
Definition: iterative_solver.h:322
IterativeSolver(double NewTolerance, unsigned int NewMaxIterationsNumber)
Definition: iterative_solver.h:114
TDenseSpaceType::MatrixType DenseMatrixType
Definition: iterative_solver.h:82
TSparseSpaceType::VectorType VectorType
Definition: iterative_solver.h:80
virtual void SetResidualNorm(double NewResidualNorm)
Definition: iterative_solver.h:315
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: iterative_solver.h:354
~IterativeSolver() override
Destructor.
Definition: iterative_solver.h:168
IterativeSolver(double NewTolerance)
Definition: iterative_solver.h:104
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
LinearSolver & operator=(const LinearSolver &Other)
Assignment operator.
Definition: linear_solver.h:112
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
std::size_t IndexType
Definition: binary_expression.cpp:25
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
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