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.
tfqmr_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 //
12 //
13 
14 
15 #if !defined(KRATOS_TFQMR_SOLVER_H_INCLUDED )
16 #define KRATOS_TFQMR_SOLVER_H_INCLUDED
17 
18 // System includes
19 #include <cmath>
20 
21 // External includes
22 
23 // Project includes
24 #include "includes/define.h"
27 
28 namespace Kratos
29 {
30 template<class TSparseSpaceType,
31  class TDenseSpaceType,
32  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
33  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
34 class TFQMRSolver : public IterativeSolver<TSparseSpaceType,
35  TDenseSpaceType,
36  TPreconditionerType,
37  TReordererType>
38 {
39 public:
40 
43 
44 
46 
48 
50 
52 
53 
54 
55 
56 
59 
60 
61  TFQMRSolver(double NewTolerance) : BaseType(NewTolerance) {}
62 
63  TFQMRSolver(Parameters settings ) : BaseType(settings)
64  {
65  if(settings.Has("preconditioner_type"))
66  BaseType::SetPreconditioner( PreconditionerFactory<TSparseSpaceType,TDenseSpaceType>().Create(settings["preconditioner_type"].GetString()) );
67  }
68  TFQMRSolver(Parameters settings,typename TPreconditionerType::Pointer pNewPreconditioner) : BaseType(settings) {}
69 
70 
71  TFQMRSolver(double NewTolerance,
72  unsigned int NewMaxIterationsNumber) : BaseType(NewTolerance, NewMaxIterationsNumber) {}
73 
74 
75  TFQMRSolver(double NewMaxTolerance,
76  unsigned int NewMaxIterationsNumber,
77  typename TPreconditionerType::Pointer pNewPreconditioner) :
78  BaseType(NewMaxTolerance, NewMaxIterationsNumber, pNewPreconditioner) {}
79 
81  TFQMRSolver(const TFQMRSolver& Other):BaseType(Other) {}
82 
83 
85  ~TFQMRSolver() override {}
86 
87 
95  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
96  {
97  if(this->IsNotConsistent(rA, rX, rB))
98  return false;
99 
100 
101  /* GetTimeTable()->Start(Info()); */
102 
103 
104  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
105  BaseType::GetPreconditioner()->ApplyInverseRight(rX);
106  BaseType::GetPreconditioner()->ApplyLeft(rB);
107 
108 
109  bool is_solved = IterativeSolve(rA,rX,rB);
110 
111 
112  BaseType::GetPreconditioner()->Finalize(rX);
113 
114 
115  /* GetTimeTable()->Stop(Info()); */
116 
117 
118  return is_solved;
119  }
120 
129  {
130  /* GetTimeTable()->Start(Info()); */
131 
132 
133  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
134 
135 
136  bool is_solved = true;
139  for(unsigned int i = 0 ; i < TDenseSpaceType::Size2(rX) ; i++)
140  {
141  TDenseSpaceType::GetColumn(i,rX, x);
142  TDenseSpaceType::GetColumn(i,rB, b);
143 
144  BaseType::GetPreconditioner()->ApplyInverseRight(x);
145  BaseType::GetPreconditioner()->ApplyLeft(b);
146 
147 
148  is_solved &= IterativeSolve(rA,x,b);
149 
150 
151  BaseType::GetPreconditioner()->Finalize(x);
152  }
153 
154 
155  /* GetTimeTable()->Stop(Info()); */
156 
157 
158  return is_solved;
159  }
160 
161 
162 
164  std::string Info() const override
165  {
166  std::stringstream buffer;
167  buffer << "Tranpose-free QMR linear solver with " << BaseType::GetPreconditioner()->Info();
168  return buffer.str();
169  }
170 
172  void PrintInfo(std::ostream& OStream) const override
173  {
174  OStream << "Tranpose-free QMR linear solver with ";
175  BaseType::GetPreconditioner()->PrintInfo(OStream);
176  }
177 
178 
180  void PrintData(std::ostream& OStream) const override
181  {
182  BaseType::PrintData(OStream);
183  }
184 
185 
186 
187 
188 private:
189 
190 
191  bool IterativeSolve(SparseMatrixType& rA, VectorType& rX, VectorType& rB)
192  {
193  const int size = TSparseSpaceType::Size(rX);
194 
195 
196  int info_count = 0;
198 
199 
201 
202 
203  double errtol = BaseType::mBNorm*BaseType::GetTolerance();
204 
205 
206  const VectorType& r(rB);
207  VectorType w(r);
208 
209 
210  VectorType y1(r);
211  VectorType y2(size);
212  for(typename VectorType::iterator ity2 = y2.begin(); ity2 != y2.end(); ity2++) *ity2 = 0.00;
213 
214 
215  VectorType d(size);
216  for(typename VectorType::iterator itd = d.begin(); itd != d.end(); itd++) *itd = 0.00;
217 
218 
219  VectorType v(size);
220  this->PreconditionedMult(rA, y1, v);
221 
222 
223  VectorType u1(v);
224  VectorType u2(size);
225  for(typename VectorType::iterator itu2 = u2.begin(); itu2 != u2.end(); itu2++) *itu2 = 0.00;
226 
227 
228  double theta = 0.00;
229  double eta = 0.00;
230  double sigma = 0.00;
231  double alpha = 0.00;
232  double c = 0.00;
233 
234 
235  double tau = TSparseSpaceType::TwoNorm(r);
236  double rho = tau*tau;
237  double rhon = 0.00;
238  double beta = 0.00;
239 
240 
241  int m = 0;
242 
243 
244 
246  {
247 
248 
250 
251 
253 
254  if (sigma == 0.00) break;
255 
256  alpha = rho/sigma;
257 
258 
259 
261 
262 
264 
265 
266  //w=w-alpha*u1;
268 
269 
270  //d=y1+(theta*theta*eta/alpha)*d;
271  TSparseSpaceType::ScaleAndAdd(1.00, y1, (theta*theta*eta/alpha), d);
272 
274  c = 1.00/sqrt(1.00+theta*theta);
275  tau = tau*theta*c;
276  eta = c*c*alpha;
277 
278 
279  //x=x+eta*d;
280  TSparseSpaceType::ScaleAndAdd(eta, d, 1.00, rX);
281 
282  BaseType::mResidualNorm = tau*sqrt((double)(m+1));
283 
284  if (BaseType::mResidualNorm <= errtol) break;
285 
286 
287 
289 
290 
292 
293 
294  //y2=y1-alpha*v;
295  TSparseSpaceType::ScaleAndAdd(1.00, y1, -alpha, v, y2);
296 
297 
298  //u2=A*y2;
299  this->PreconditionedMult(rA,y2,u2);
300 
301  //w=w-alpha*u2;
303 
304  //d=y2+(theta*theta*eta/alpha)*d;
305  TSparseSpaceType::ScaleAndAdd(1.00, y2, (theta*theta*eta/alpha), d);
306 
307 
309  c = 1.00/sqrt(1+theta*theta);
310  tau = tau*theta*c;
311  eta = c*c*alpha;
312 
313 
314  //x=x+eta*d;
315  TSparseSpaceType::ScaleAndAdd(eta, d, 1.00, rX);
316 
317 
318  BaseType::mResidualNorm = tau*sqrt((double)(m+1));
319 
320  if (BaseType::mResidualNorm <= errtol) break;
321 
322 
323 
325 
326 
327 
328  if (rho == 0.00) break;
329 
330  rhon = TSparseSpaceType::Dot(r,w);
331  beta = rhon/rho;
332  rho = rhon;
333 
334 
335  //y1= w + beta*y2;
336  TSparseSpaceType::ScaleAndAdd(1.00, w, beta, y2, y1);
337 
338 
339  //u1 = A*y1;
340  this->PreconditionedMult(rA,y1,u1);
341 
342  //v=u1+beta*(u2+beta*v);
343  TSparseSpaceType::ScaleAndAdd(1.00, u2, beta, v);
344  TSparseSpaceType::ScaleAndAdd(1.00, u1, beta, v);
345 
346 
347  //Print iteration info
348  info_count++;
349  if (info_count==100)
350  {
351  std::cout<<"it = "<<BaseType::mIterationsNumber<<" res = "<<(BaseType::mResidualNorm/BaseType::mBNorm)<<std::endl;
352  info_count=0;
353  }
354 
355 
356  }
357 
358 
359  std::cout<<std::endl;
360  return BaseType::IsConverged();
361  }
362 
363 
364 
366  TFQMRSolver& operator=(const TFQMRSolver& Other);
367 
368 
369 
370 
371 }; // ********** Class TFQMRSolver *****************
372 
373 
374 
376 template<class TSparseSpaceType,
377  class TDenseSpaceType,
378  class TPreconditionerType,
379  class TReordererType>
380 inline std::istream& operator >> (std::istream& IStream, TFQMRSolver<TSparseSpaceType,
381  TDenseSpaceType,
382  TPreconditionerType,
383  TReordererType>& rThis)
384 {
385  return IStream;
386 }
387 
388 
390 template<class TSparseSpaceType,
391  class TDenseSpaceType,
392  class TPreconditionerType,
393  class TReordererType>
394 inline std::ostream& operator << (std::ostream& OStream,const TFQMRSolver<TSparseSpaceType,
395  TDenseSpaceType,
396  TPreconditionerType,
397  TReordererType>& rThis)
398 {
399  rThis.PrintInfo(OStream);
400  OStream << std::endl;
401  rThis.PrintData(OStream);
402  return OStream;
403 }
404 
405 } // namespace Kratos.
406 
407 
408 #endif // KRATOS_TFQMR_SOLVER_H_INCLUDED defined
409 
410 
411 
412 
Base class for all the iterative solvers in Kratos.
Definition: iterative_solver.h:68
IndexType mIterationsNumber
Definition: iterative_solver.h:403
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
virtual IndexType GetMaxIterationsNumber()
Definition: iterative_solver.h:290
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 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
Definition: tfqmr_solver.h:38
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: tfqmr_solver.h:45
void PrintData(std::ostream &OStream) const override
Print object's data.
Definition: tfqmr_solver.h:180
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: tfqmr_solver.h:128
TFQMRSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: tfqmr_solver.h:68
KRATOS_CLASS_POINTER_DEFINITION(TFQMRSolver)
Counted pointer of TFQMRSolver.
TFQMRSolver(double NewTolerance, unsigned int NewMaxIterationsNumber)
Definition: tfqmr_solver.h:71
TFQMRSolver(Parameters settings)
Definition: tfqmr_solver.h:63
TSparseSpaceType::VectorType VectorType
Definition: tfqmr_solver.h:49
std::string Info() const override
Return information about this object.
Definition: tfqmr_solver.h:164
TFQMRSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: tfqmr_solver.h:75
TFQMRSolver()
Default constructor.
Definition: tfqmr_solver.h:58
~TFQMRSolver() override
Destructor.
Definition: tfqmr_solver.h:85
TFQMRSolver(const TFQMRSolver &Other)
Copy constructor.
Definition: tfqmr_solver.h:81
void PrintInfo(std::ostream &OStream) const override
Print information about this object.
Definition: tfqmr_solver.h:172
TDenseSpaceType::MatrixType DenseMatrixType
Definition: tfqmr_solver.h:51
TSparseSpaceType::MatrixType SparseMatrixType
Definition: tfqmr_solver.h:47
TFQMRSolver(double NewTolerance)
Definition: tfqmr_solver.h:61
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: tfqmr_solver.h:95
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
v
Definition: generate_convection_diffusion_explicit_element.py:114
w
Definition: generate_convection_diffusion_explicit_element.py:108
alpha
Definition: generate_convection_diffusion_explicit_element.py:113
tau
Definition: generate_convection_diffusion_explicit_element.py:115
rho
Definition: generate_droplet_dynamics.py:86
u2
Definition: generate_frictional_mortar_condition.py:76
u1
Definition: generate_frictional_mortar_condition.py:75
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
int d
Definition: ode_solve.py:397
float sigma
Definition: rotating_cone.py:79
int m
Definition: run_marine_rain_substepping.py:8
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17