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.
deflated_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: Riccardo Rossi
11 //
12 
13 #if !defined(KRATOS_DEFLATED_CG_SOLVER_H_INCLUDED )
14 #define KRATOS_DEFLATED_CG_SOLVER_H_INCLUDED
15 
16 
17 
18 // System includes
19 #include <string>
20 #include <iostream>
21 #include <vector>
22 #include <set>
23 
24 // External includes
25 
26 
27 // Project includes
28 #include "includes/define.h"
32 
33 namespace Kratos
34 {
35 
38 
42 
46 
50 
54 
56 
59 template<class TSparseSpaceType, class TDenseSpaceType,
60  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
61  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
62 class DeflatedCGSolver : public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
63 {
64 public:
67 
70 
72 
73  // typedef LinearSolver<TSparseSpaceType, TDenseSpaceType, TReordererType> LinearSolverType;
74 
76 
78 
80 
82 
86 
88 
90  {
91  }
92 
93  DeflatedCGSolver(double NewMaxTolerance, bool assume_constant_structure, int max_reduced_size) :
94  BaseType(NewMaxTolerance)
95  , mmax_reduced_size(max_reduced_size)
96  , massume_constant_structure(assume_constant_structure)
97  {
98  }
99 
100  DeflatedCGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, bool assume_constant_structure, int max_reduced_size) :
101  BaseType(NewMaxTolerance, NewMaxIterationsNumber)
102  , mmax_reduced_size(max_reduced_size)
103  , massume_constant_structure(assume_constant_structure)
104  {
105  }
106 
107  DeflatedCGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber,
108  typename TPreconditionerType::Pointer pNewPreconditioner, bool assume_constant_structure, int max_reduced_size) :
109  BaseType(NewMaxTolerance, NewMaxIterationsNumber, pNewPreconditioner)
110  , mmax_reduced_size(max_reduced_size)
111  , massume_constant_structure(assume_constant_structure)
112  {
113  }
114 
116  typename TPreconditionerType::Pointer pNewPreconditioner = Kratos::make_shared<TPreconditionerType>()
117  ): BaseType ()
118 
119  {
120  KRATOS_TRY
121 
122 
123  Parameters default_parameters( R"(
124  {
125  "solver_type": "DeflatedCGSolver",
126  "tolerance" : 1.0e-6,
127  "max_iteration" : 200,
128  "assume_constant_structure" : false,
129  "max_reduced_size" : 1024,
130  "scaling":false
131  } )" );
132 
133  //now validate agains defaults -- this also ensures no type mismatch
134  settings.ValidateAndAssignDefaults(default_parameters);
135 
136  this->SetTolerance( settings["tolerance"].GetDouble() );
137  this->SetMaxIterationsNumber( settings["max_iteration"].GetInt() );
138  massume_constant_structure = settings["assume_constant_structure"].GetBool();
139  mmax_reduced_size = settings["max_reduced_size"].GetInt();
140 
141  KRATOS_CATCH("")
142  }
143 
145 
147  {
148  }
149 
150 
152 
153  ~DeflatedCGSolver() override
154  {
155  }
156 
157 
161 
163 
165  {
166  BaseType::operator=(Other);
167  return *this;
168  }
169 
173 
183  {
184  if (this->IsNotConsistent(rA, rX, rB))
185  return false;
186 
187  // BaseType::GetPreconditioner()->Initialize(rA,rX,rB);
188  // BaseType::GetPreconditioner()->ApplyInverseRight(rX);
189  // BaseType::GetPreconditioner()->ApplyLeft(rB);
190 
191  bool is_solved = IterativeSolve(rA, rX, rB);
192 
193  // BaseType::GetPreconditioner()->Finalize(rX);
194 
195  return is_solved;
196  }
197 
207  {
208 
209  std::cout << "************ DeflatedCGSolver::Solve(SparseMatrixType&, DenseMatrixType&, DenseMatrixType&) not defined! ************" << std::endl;
210 
211  return false;
212  }
213 
217 
218 
222 
223 
227 
229 
230  std::string Info() const override
231  {
232  std::stringstream buffer;
233  buffer << "Deflated Conjugate gradient linear solver with " << BaseType::GetPreconditioner()->Info();
234  return buffer.str();
235  }
236 
238 
239  void PrintInfo(std::ostream& rOStream) const override
240  {
241  rOStream << Info();
242  }
243 
245 
246  void PrintData(std::ostream& rOStream) const override
247  {
248  BaseType::PrintData(rOStream);
249  }
250 
251 
255 
256 
258 
259 protected:
262 
263 
267 
268 
272 
273 
277 
278 
282 
283 
287 
288 
292 
293 
295 
296 private:
299 
300 
304  int mmax_reduced_size;
305  bool massume_constant_structure;
306  std::vector<int> mw;
307  SparseMatrixType mAdeflated;
308 
309  //typename LinearSolverType::Pointer mpLinearSolver;
310 
314 
315 
319 
320  bool IterativeSolve(SparseMatrixType& rA, SparseVectorType& rX, SparseVectorType& rB)
321  {
322  const int full_size = TSparseSpaceType::Size(rX);
323 
324  //construct "coloring" structure and fill reduced matrix structure
325  //note that this has to be done only once if the matrix structure is preserved
326  if (massume_constant_structure == false || mw.size() == 0)
327  {
328  DeflationUtils::ConstructW(mmax_reduced_size, rA, mw, mAdeflated);
329  }
330 // KRATOS_WATCH("__LINE__")
331  //fill reduced matrix mmAdeflated
332  DeflationUtils::FillDeflatedMatrix(rA, mw, mAdeflated);
333 
334  std::size_t reduced_size = mAdeflated.size1();
335 
336  // To save some time, we do the factorization once, and do the solve several times.
337  // When this is available through the LinearSolver interface, replace this.
338  LUSkylineFactorization<TSparseSpaceType, TDenseSpaceType> Factorization;
339  //mpLinearSolver = LinearSolverType::Pointer(new SkylineLUFactorizationSolver<TSparseSpaceType, TDenseSpaceType>);
340 // KRATOS_WATCH(__LINE__)
341  Factorization.copyFromCSRMatrix(mAdeflated);
342  Factorization.factorize();
343 // KRATOS_WATCH(__LINE__)
344 // std::cout << "********** Factorization done!" << std::endl;
345  SparseVectorType r(full_size), t(full_size), d(full_size), p(full_size), q(full_size);
346  SparseVectorType th(reduced_size), dh(reduced_size);
347 
348  // r = rA * rX
349  TSparseSpaceType::Mult(rA, rX, r);
350 
351  // r = rB - r
352  TSparseSpaceType::ScaleAndAdd(1.00, rB, -1.00, r);
353 // KRATOS_WATCH(__LINE__)
354  // th = W^T * r -> form reduced problem
356  // TSparseSpaceType::TransposeMult(W, r, th);
357 
358  // Solve mAdeflated * th = dh
359  Factorization.backForwardSolve(reduced_size, th, dh);
360 
361  // t = W * dh -> transfer reduced problem to large scale one
362  DeflationUtils::ApplyW(mw, dh, t);
363  // TSparseSpaceType::Mult(W, dh, t);
364 
365  // rX = rX + t
366  TSparseSpaceType::ScaleAndAdd(1.00, t, 1.00, rX);
367 
368  //r = rA * rX
369  TSparseSpaceType::Mult(rA, rX, r);
370 
371  // r = B - r
372  TSparseSpaceType::ScaleAndAdd(1.00, rB, -1.00, r);
373 
374  // t = A * r
375  //TSparseSpaceType::Mult(rA, r, t);
376  this->PreconditionedMult(rA, r, t);
377 // KRATOS_WATCH(__LINE__)
378  // th = W^T * t
380 
381  // Solve mAdeflated * th = dh
382  Factorization.backForwardSolve(reduced_size, th, dh);
383 // KRATOS_WATCH(__LINE__)
384  // p = W * dh
385  DeflationUtils::ApplyW(mw, dh, p);
386 
387  // p = r - p
388  TSparseSpaceType::ScaleAndAdd(1.00, r, -1.00, p);
389 
390  // Iteration counter
392 
394 
395  double roh0 = TSparseSpaceType::Dot(r, r);
396  double roh1 = roh0;
397  double beta = 0;
398 
399  if (fabs(roh0) < 1.0e-30) return false;
400 // KRATOS_WATCH(__LINE__)
401  do
402  {
403  TSparseSpaceType::Mult(rA, p, q);
404 
405  double pq = TSparseSpaceType::Dot(p, q);
406 
407  //std::cout << "********** pq = " << pq << std::endl;
408 
409  //if(pq == 0.00)
410  if (fabs(pq) <= 1.0e-30)
411  break;
412 
413  double alpha = roh0 / pq;
414 
417 
418  roh1 = TSparseSpaceType::Dot(r, r);
419 
420  beta = (roh1 / roh0);
421 
422  // t = A * r
423  //TSparseSpaceType::Mult(rA, r, t);
424  TSparseSpaceType::Mult(rA, r, t);
425 
426  // th = W^T * t
428  // TSparseSpaceType::TransposeMult(W, t, th);
429 
430  // Solve mAdeflated * th = dh
431  Factorization.backForwardSolve(reduced_size, th, dh);
432 
433  // t = W * dh
434  DeflationUtils::ApplyW(mw, dh, t);
435  // TSparseSpaceType::Mult(W, dh, t);
436 
437  // t = r - t
438  TSparseSpaceType::ScaleAndAdd(1.00, r, -1.00, t);
439 
440  // p = beta * p + t
441  TSparseSpaceType::ScaleAndAdd(1.00, t, beta, p);
442 
443  roh0 = roh1;
444 
445  BaseType::mResidualNorm = sqrt(roh1);
446 
448 
449  }
450  while (BaseType::IterationNeeded() && (fabs(roh0) > 1.0e-30)/*(roh0 != 0.00)*/);
451 
452  return BaseType::IsConverged();
453  }
454 
455 
456 
457 
461 
462 
466 
467 
471 
472 
474 
475 }; // Class DeflatedCGSolver
476 
478 
481 
482 
486 
487 
489 
490 template<class TSparseSpaceType, class TDenseSpaceType,
491  class TPreconditionerType,
492  class TReordererType>
493 inline std::istream & operator >>(std::istream& IStream,
494  DeflatedCGSolver<TSparseSpaceType, TDenseSpaceType,
495  TPreconditionerType, TReordererType>& rThis)
496 {
497  return IStream;
498 }
499 
501 
502 template<class TSparseSpaceType, class TDenseSpaceType,
503  class TPreconditionerType,
504  class TReordererType>
505 inline std::ostream & operator <<(std::ostream& OStream,
506  const DeflatedCGSolver<TSparseSpaceType, TDenseSpaceType,
507  TPreconditionerType, TReordererType>& rThis)
508 {
509  rThis.PrintInfo(OStream);
510  OStream << std::endl;
511  rThis.PrintData(OStream);
512 
513  return OStream;
514 }
516 
517 
518 } // namespace Kratos.
519 
520 #endif // KRATOS_DEFLATED_CG_SOLVER_H_INCLUDED defined
521 
522 
Short class definition.
Definition: deflated_cg_solver.h:63
~DeflatedCGSolver() override
Destructor.
Definition: deflated_cg_solver.h:153
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: deflated_cg_solver.h:239
DeflatedCGSolver(const DeflatedCGSolver &Other)
Copy constructor.
Definition: deflated_cg_solver.h:146
TSparseSpaceType::VectorType SparseVectorType
Definition: deflated_cg_solver.h:77
DeflatedCGSolver(Parameters settings, typename TPreconditionerType::Pointer pNewPreconditioner=Kratos::make_shared< TPreconditionerType >())
Definition: deflated_cg_solver.h:115
DeflatedCGSolver & operator=(const DeflatedCGSolver &Other)
Assignment operator.
Definition: deflated_cg_solver.h:164
DeflatedCGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, bool assume_constant_structure, int max_reduced_size)
Definition: deflated_cg_solver.h:100
TDenseSpaceType::MatrixType DenseMatrixType
Definition: deflated_cg_solver.h:79
std::string Info() const override
Turn back information as a string.
Definition: deflated_cg_solver.h:230
DeflatedCGSolver(double NewMaxTolerance, unsigned int NewMaxIterationsNumber, typename TPreconditionerType::Pointer pNewPreconditioner, bool assume_constant_structure, int max_reduced_size)
Definition: deflated_cg_solver.h:107
DeflatedCGSolver(double NewMaxTolerance, bool assume_constant_structure, int max_reduced_size)
Definition: deflated_cg_solver.h:93
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: deflated_cg_solver.h:71
KRATOS_CLASS_POINTER_DEFINITION(DeflatedCGSolver)
Pointer definition of DeflatedCGSolver.
TSparseSpaceType::MatrixType SparseMatrixType
Definition: deflated_cg_solver.h:75
DeflatedCGSolver()
Default constructor.
Definition: deflated_cg_solver.h:89
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: deflated_cg_solver.h:246
TDenseSpaceType::VectorType DenseVectorType
Definition: deflated_cg_solver.h:81
bool Solve(SparseMatrixType &rA, SparseVectorType &rX, SparseVectorType &rB) override
Definition: deflated_cg_solver.h:182
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: deflated_cg_solver.h:206
static void FillDeflatedMatrix(const SparseMatrixType &rA, std::vector< int > &w, SparseMatrixType &Ah)
Definition: deflation_utils.h:362
static void ApplyW(const std::vector< int > &w, const SparseVectorType &x, SparseVectorType &y)
Definition: deflation_utils.h:324
static void ApplyWtranspose(const std::vector< int > &w, const SparseVectorType &x, SparseVectorType &y)
Definition: deflation_utils.h:338
static void ConstructW(const std::size_t max_reduced_size, SparseMatrixType &rA, std::vector< int > &w, SparseMatrixType &deflatedA)
Definition: deflation_utils.h:181
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 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
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
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
bool GetBool() const
This method returns the boolean contained in the current Parameter.
Definition: kratos_parameters.cpp:675
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
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
void Mult(TSpaceType &dummy, typename TSpaceType::MatrixType &rA, typename TSpaceType::VectorType &rX, typename TSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:98
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
th
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:32
int t
Definition: ode_solve.py:392
int d
Definition: ode_solve.py:397
p
Definition: sensitivityMatrix.py:52
subroutine pq(T, p, q)
Definition: TensorModule.f:339