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.
cg_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_CG_SOLVER_H_INCLUDED )
15 #define KRATOS_CG_SOLVER_H_INCLUDED
16 
17 
18 // System includes
19 #include <string>
20 #include <iostream>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
28 
29 namespace Kratos
30 {
31 
34 
38 
42 
46 
50 
52 
54 template<class TSparseSpaceType, class TDenseSpaceType,
55  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
56  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
57 class CGSolver : public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
58 {
59 public:
62 
65 
67 
69 
71 
73 
77 
79  CGSolver() {}
80 
81  CGSolver(double NewMaxTolerance) : BaseType(NewMaxTolerance) {}
82 
83  CGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber) : BaseType(NewMaxTolerance, NewMaxIterationsNumber) {}
84 
85  CGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner) :
86  BaseType(NewMaxTolerance, NewMaxIterationsNumber, pNewPreconditioner) {}
87 
88  CGSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner):
89  BaseType(settings, pNewPreconditioner) {}
90 
91  CGSolver(Parameters settings):
92  BaseType(settings)
93  {
94  if(settings.Has("preconditioner_type"))
95  BaseType::SetPreconditioner( PreconditionerFactory<TSparseSpaceType,TDenseSpaceType>().Create(settings["preconditioner_type"].GetString()) );
96  }
97 
99  CGSolver(const CGSolver& Other) : BaseType(Other) {}
100 
101 
103  ~CGSolver() override {}
104 
105 
109 
111  CGSolver& operator=(const CGSolver& Other)
112  {
113  BaseType::operator=(Other);
114  return *this;
115  }
116 
120 
129  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
130  {
131  if(this->IsNotConsistent(rA, rX, rB))
132  return false;
133 
134 // GetTimeTable()->Start(Info());
135 
136  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
137  BaseType::GetPreconditioner()->ApplyInverseRight(rX);
138  BaseType::GetPreconditioner()->ApplyLeft(rB);
139 
140  bool is_solved = IterativeSolve(rA,rX,rB);
141 
142  KRATOS_WARNING_IF("CG Linear Solver", !is_solved)<<"Non converged linear solution. ["<< BaseType::GetResidualNorm()/BaseType::mBNorm << " > "<< BaseType::GetTolerance() << "]" << std::endl;
143 
144  BaseType::GetPreconditioner()->Finalize(rX);
145 
146 // GetTimeTable()->Stop(Info());
147 
148  return is_solved;
149  }
150 
151 
161  {
162 // GetTimeTable()->Start(Info());
163 
164  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
165 
166  bool is_solved = true;
169  for(unsigned int i = 0 ; i < TDenseSpaceType::Size2(rX) ; i++)
170  {
171  TDenseSpaceType::GetColumn(i,rX, x);
172  TDenseSpaceType::GetColumn(i,rB, b);
173 
174  BaseType::GetPreconditioner()->ApplyInverseRight(x);
175  BaseType::GetPreconditioner()->ApplyLeft(b);
176 
177  is_solved &= IterativeSolve(rA,x,b);
178 
179  BaseType::GetPreconditioner()->Finalize(x);
180  }
181 
182 // GetTimeTable()->Stop(Info());
183 
184  return is_solved;
185  }
186 
190 
191 
195 
196 
200 
202  std::string Info() const override
203  {
204  std::stringstream buffer;
205  buffer << "Conjugate gradient linear solver with " << BaseType::GetPreconditioner()->Info();
206  return buffer.str();
207  }
208 
210  void PrintInfo(std::ostream& rOStream) const override
211  {
212  rOStream << Info();
213  }
214 
216  void PrintData(std::ostream& rOStream) const override
217  {
218  BaseType::PrintData(rOStream);
219  }
220 
221 
225 
226 
228 
229 protected:
232 
233 
237 
238 
242 
243 
247 
248 
252 
253 
257 
258 
262 
263 
265 
266 private:
269 
270 
274 
275 
279 
280 
284 
285  bool IterativeSolve(SparseMatrixType& rA, VectorType& rX, VectorType& rB)
286  {
287  const int size = TSparseSpaceType::Size(rX);
288 
290 
291  VectorType r(size);
292 
293  this->PreconditionedMult(rA,rX,r);
294  TSparseSpaceType::ScaleAndAdd(1.00, rB, -1.00, r);
295 
297 
298  VectorType p(r);
299  VectorType q(size);
300 
301  double roh0 = TSparseSpaceType::Dot(r, r);
302  double roh1 = roh0;
303  double beta = 0;
304 
305  if(fabs(roh0) < 1.0e-30) //modification by Riccardo
306 // if(roh0 == 0.00)
307  return false;
308 
309  do
310  {
311  this->PreconditionedMult(rA,p,q);
312 
313  double pq = TSparseSpaceType::Dot(p,q);
314 
315  //if(pq == 0.00)
316  if(fabs(pq) <= 1.0e-30)
317  break;
318 
319  double alpha = roh0 / pq;
320 
323 
324  roh1 = TSparseSpaceType::Dot(r,r);
325 
326  beta = (roh1 / roh0);
327  TSparseSpaceType::ScaleAndAdd(1.00, r, beta, p);
328 
329  roh0 = roh1;
330 
331  BaseType::mResidualNorm = sqrt(roh1);
333  }
334  while(BaseType::IterationNeeded() && (fabs(roh0) > 1.0e-30)/*(roh0 != 0.00)*/);
335 
336  return BaseType::IsConverged();
337  }
338 
342 
343 
347 
348 
352 
353 
355 
356 }; // Class CGSolver
357 
359 
362 
363 
367 
368 
370 template<class TSparseSpaceType, class TDenseSpaceType,
371  class TPreconditionerType,
372  class TReordererType>
373 inline std::istream& operator >> (std::istream& IStream,
374  CGSolver<TSparseSpaceType, TDenseSpaceType,
375  TPreconditionerType, TReordererType>& rThis)
376 {
377  return IStream;
378 }
379 
381 template<class TSparseSpaceType, class TDenseSpaceType,
382  class TPreconditionerType,
383  class TReordererType>
384 inline std::ostream& operator << (std::ostream& OStream,
385  const CGSolver<TSparseSpaceType, TDenseSpaceType,
386  TPreconditionerType, TReordererType>& rThis)
387 {
388  rThis.PrintInfo(OStream);
389  OStream << std::endl;
390  rThis.PrintData(OStream);
391 
392  return OStream;
393 }
395 
396 
397 } // namespace Kratos.
398 
399 #endif // KRATOS_CG_SOLVER_H_INCLUDED defined
400 
401 
Short class definition.
Definition: cg_solver.h:58
KRATOS_CLASS_POINTER_DEFINITION(CGSolver)
Pointer definition of CGSolver.
CGSolver(double NewMaxTolerance)
Definition: cg_solver.h:81
CGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber)
Definition: cg_solver.h:83
CGSolver()
Default constructor.
Definition: cg_solver.h:79
TSparseSpaceType::VectorType VectorType
Definition: cg_solver.h:70
~CGSolver() override
Destructor.
Definition: cg_solver.h:103
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: cg_solver.h:216
CGSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: cg_solver.h:88
TSparseSpaceType::MatrixType SparseMatrixType
Definition: cg_solver.h:68
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: cg_solver.h:160
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: cg_solver.h:129
TDenseSpaceType::MatrixType DenseMatrixType
Definition: cg_solver.h:72
CGSolver & operator=(const CGSolver &Other)
Assignment operator.
Definition: cg_solver.h:111
CGSolver(Parameters settings)
Definition: cg_solver.h:91
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: cg_solver.h:210
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: cg_solver.h:66
CGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: cg_solver.h:85
std::string Info() const override
Turn back information as a string.
Definition: cg_solver.h:202
CGSolver(const CGSolver &Other)
Copy constructor.
Definition: cg_solver.h:99
Base class for all the iterative solvers in Kratos.
Definition: iterative_solver.h:68
IndexType mIterationsNumber
Definition: iterative_solver.h:403
virtual bool IterationNeeded()
Definition: iterative_solver.h:331
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: iterative_solver.h:360
double mResidualNorm
Definition: iterative_solver.h:399
double mBNorm
Definition: iterative_solver.h:405
IterativeSolver & operator=(const IterativeSolver &Other)
Assignment operator.
Definition: iterative_solver.h:176
virtual void SetPreconditioner(typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: iterative_solver.h:270
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
virtual bool IsConverged()
Definition: iterative_solver.h:336
virtual double GetResidualNorm()
Definition: iterative_solver.h:322
virtual bool IsNotConsistent(SparseMatrixType &rA, VectorType &rX, VectorType &rB)
This method checks if the dimensions of the system of equations are not consistent.
Definition: linear_solver.h:346
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
bool Has(const std::string &rEntry) const
This method checks if the Parameter contains a certain entry.
Definition: kratos_parameters.cpp:520
Here we add the functions needed for the registration of preconditioners.
Definition: preconditioner_factory.h:62
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
Modeler::Pointer Create(const std::string &ModelerName, Model &rModel, const Parameters ModelParameters)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:30
TSpaceType::IndexType Size1(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:117
TSpaceType::IndexType Size2(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:123
void ScaleAndAdd(TSpaceType &dummy, const double A, const typename TSpaceType::VectorType &rX, const double B, typename TSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:91
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
double Dot(SparseSpaceType &dummy, SparseSpaceType::VectorType &rX, SparseSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:85
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
alpha
Definition: generate_convection_diffusion_explicit_element.py:113
q
Definition: generate_convection_diffusion_explicit_element.py:109
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
p
Definition: sensitivityMatrix.py:52
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
subroutine pq(T, p, q)
Definition: TensorModule.f:339