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.
bicgstab_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_BICGSTAB_SOLVER_H_INCLUDED )
15 #define KRATOS_BICGSTAB_SOLVER_H_INCLUDED
16 
17 // System includes
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
25 
26 namespace Kratos
27 {
28 
31 
35 
39 
43 
47 
49 
51 template<class TSparseSpaceType, class TDenseSpaceType,
52  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
53  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
54 class BICGSTABSolver : public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
55 {
56 public:
59 
62 
64 
66 
68 
70 
74 
77 
78  BICGSTABSolver(double NewTolerance) : BaseType(NewTolerance) {}
79 
80  BICGSTABSolver(double NewTolerance, unsigned int NewMaxIterationsNumber) : BaseType(NewTolerance, NewMaxIterationsNumber) {}
81 
82  BICGSTABSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner) :
83  BaseType(NewMaxTolerance, NewMaxIterationsNumber, pNewPreconditioner) {}
84 
85  BICGSTABSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner):
86  BaseType(settings, pNewPreconditioner) {}
87 
89  BaseType(settings)
90  {
91  if(settings.Has("preconditioner_type"))
92  BaseType::SetPreconditioner( PreconditionerFactory<TSparseSpaceType,TDenseSpaceType>().Create(settings["preconditioner_type"].GetString()) );
93  }
94 
96  BICGSTABSolver(const BICGSTABSolver& Other) : BaseType(Other) {}
97 
99  ~BICGSTABSolver() override {}
100 
101 
105 
108  {
109  BaseType::operator=(Other);
110  return *this;
111  }
112 
116 
125  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
126  {
127  if(this->IsNotConsistent(rA, rX, rB))
128  return false;
129 
130  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
131 
132  BaseType::GetPreconditioner()->ApplyInverseRight(rX);
133 
134  BaseType::GetPreconditioner()->ApplyLeft(rB);
135 
136  bool is_solved = IterativeSolve(rA,rX,rB);
137 
138  BaseType::GetPreconditioner()->Finalize(rX);
139 
140  return is_solved;
141  }
142 
152  {
153  //GetTimeTable()->Start(Info());
154 
155  BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
156 
157  bool is_solved = true;
160  for(unsigned int i = 0 ; i < TDenseSpaceType::Size2(rX) ; i++)
161  {
162  TDenseSpaceType::GetColumn(i,rX, x);
163  TDenseSpaceType::GetColumn(i,rB, b);
164 
165  BaseType::GetPreconditioner()->ApplyInverseRight(x);
166  BaseType::GetPreconditioner()->ApplyLeft(b);
167 
168  is_solved &= IterativeSolve(rA,x,b);
169 
170  BaseType::GetPreconditioner()->Finalize(x);
171  }
172 
173  //GetTimeTable()->Stop(Info());
174 
175  return is_solved;
176  }
177 
181 
182 
186 
187 
191 
193  std::string Info() const override
194  {
195  std::stringstream buffer;
196  buffer << "Biconjugate gradient stabilized linear solver with " << BaseType::GetPreconditioner()->Info();
197  return buffer.str();
198  }
199 
201  void PrintInfo(std::ostream& OStream) const override
202  {
203  OStream << "Biconjugate gradient stabilized linear solver with ";
204  BaseType::GetPreconditioner()->PrintInfo(OStream);
205  }
206 
208  void PrintData(std::ostream& OStream) const override
209  {
210  BaseType::PrintData(OStream);
211  }
212 
213 
217 
218 
220 
221 protected:
224 
225 
229 
230 
234 
235 
239 
240 
244 
245 
249 
250 
254 
255 
257 
258 private:
261 
262 
266 
267 
271 
272 
276 
277  bool IterativeSolve(SparseMatrixType& rA, VectorType& rX, VectorType& rB)
278  {
279  const int size = TSparseSpaceType::Size(rX);
280 // KRATOS_WATCH("ln316");
282 
283  VectorType r(size);
284 // KRATOS_WATCH(r.size());
285 // KRATOS_WATCH("ln319");
286 // KRATOS_WATCH(rA.size1());
287 // KRATOS_WATCH(rA.size2());
288 // KRATOS_WATCH(r.size());
289 // KRATOS_WATCH(rX.size());
290 // KRATOS_WATCH(rB.size());
291 
292  this->PreconditionedMult(rA,rX,r);
293 // KRATOS_WATCH("ln321");
294  TSparseSpaceType::ScaleAndAdd(1.00, rB, -1.00, r);
295 // KRATOS_WATCH("ln322");
297 // KRATOS_WATCH("ln324");
298  VectorType p(r);
299  VectorType s(size);
300  VectorType q(size);
301 
302  VectorType rs(r);
303  VectorType qs(size);
304 
305  double roh0 = TSparseSpaceType::Dot(r, rs);
306  double roh1 = roh0;
307  double alpha = 0.00;
308  double beta = 0.00;
309  double omega = 0.00;
310 // KRATOS_WATCH("ln337");
311 // if(roh0 < 1e-30) //we start from the real solution
312 // return BaseType::IsConverged();
313 
314  do
315  {
316  this->PreconditionedMult(rA,p,q);
317 // KRATOS_WATCH("ln344");
319  if (fabs(alpha) <= 1.0e-40)
320  break;
321  alpha = roh0 / alpha;
322 
323  TSparseSpaceType::ScaleAndAdd(1.00, r, -alpha, q, s);
324 // KRATOS_WATCH("ln348");
325  this->PreconditionedMult(rA,s,qs);
326 
327  omega = TSparseSpaceType::Dot(qs,qs);
328 
329  //if(omega == 0.00)
330  if(fabs(omega) <= 1.0e-40)
331  break;
332 // KRATOS_WATCH("ln356");
333  omega = TSparseSpaceType::Dot(qs,s) / omega;
334 
336  TSparseSpaceType::ScaleAndAdd(omega, s, 1.00, rX);
337  TSparseSpaceType::ScaleAndAdd(-omega, qs, 1.00, s, r);
338 
339  roh1 = TSparseSpaceType::Dot(r,rs);
340 
341  //if((roh0 == 0.00) || (omega == 0.00))
342  if((fabs(roh0) <= 1.0e-40) || (fabs(omega) <= 1.0e-40))
343  break;
344 
345  beta = (roh1 * alpha) / (roh0 * omega);
346 // KRATOS_WATCH("ln370");
347  TSparseSpaceType::ScaleAndAdd(1.00, p, -omega, q);
348  TSparseSpaceType::ScaleAndAdd(1.00, r, beta, q, p);
349 
350  roh0 = roh1;
351 
354 
355  }
356  while(BaseType::IterationNeeded());
357 
358  return BaseType::IsConverged();
359  }
360 
364 
365 
369 
370 
374 
375 
377 
378 }; // Class BICGSTABSolver
379 
381 
384 
385 
389 
390 
392 template<class TSparseSpaceType, class TDenseSpaceType,
393  class TPreconditionerType,
394  class TReordererType>
395 inline std::istream& operator >> (std::istream& IStream,
396  BICGSTABSolver<TSparseSpaceType, TDenseSpaceType,
397  TPreconditionerType, TReordererType>& rThis)
398 {
399  return IStream;
400 }
401 
403 template<class TSparseSpaceType, class TDenseSpaceType,
404  class TPreconditionerType,
405  class TReordererType>
406 inline std::ostream& operator << (std::ostream& OStream,
407  const BICGSTABSolver<TSparseSpaceType, TDenseSpaceType,
408  TPreconditionerType, TReordererType>& rThis)
409 {
410  rThis.PrintInfo(OStream);
411  OStream << std::endl;
412  rThis.PrintData(OStream);
413 
414  return OStream;
415 }
417 
418 
419 } // namespace Kratos.
420 
421 #endif // KRATOS_BICGSTAB_SOLVER_H_INCLUDED defined
422 
423 
Short class definition.
Definition: bicgstab_solver.h:55
void PrintInfo(std::ostream &OStream) const override
Print information about this object.
Definition: bicgstab_solver.h:201
TSparseSpaceType::MatrixType SparseMatrixType
Definition: bicgstab_solver.h:65
BICGSTABSolver(double NewTolerance)
Definition: bicgstab_solver.h:78
BICGSTABSolver(const BICGSTABSolver &Other)
Copy constructor.
Definition: bicgstab_solver.h:96
void PrintData(std::ostream &OStream) const override
Print object's data.
Definition: bicgstab_solver.h:208
BICGSTABSolver & operator=(const BICGSTABSolver &Other)
Assignment operator.
Definition: bicgstab_solver.h:107
BICGSTABSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: bicgstab_solver.h:85
KRATOS_CLASS_POINTER_DEFINITION(BICGSTABSolver)
Counted pointer of BICGSTABSolver.
TSparseSpaceType::VectorType VectorType
Definition: bicgstab_solver.h:67
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: bicgstab_solver.h:63
~BICGSTABSolver() override
Destructor.
Definition: bicgstab_solver.h:99
BICGSTABSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner)
Definition: bicgstab_solver.h:82
BICGSTABSolver(double NewTolerance, unsigned int NewMaxIterationsNumber)
Definition: bicgstab_solver.h:80
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: bicgstab_solver.h:125
BICGSTABSolver()
Default constructor.
Definition: bicgstab_solver.h:76
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: bicgstab_solver.h:151
std::string Info() const override
Return information about this object.
Definition: bicgstab_solver.h:193
BICGSTABSolver(Parameters settings)
Definition: bicgstab_solver.h:88
TDenseSpaceType::MatrixType DenseMatrixType
Definition: bicgstab_solver.h:69
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
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
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