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.
power_iteration_eigenvalue_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 // Collaborator: Vicente Mataix Ferrandiz
12 //
13 //
14 
15 #if !defined(KRATOS_POWER_ITERATION_EIGENVALUE_SOLVER_H_INCLUDED )
16 #define KRATOS_POWER_ITERATION_EIGENVALUE_SOLVER_H_INCLUDED
17 
18 // System includes
19 
20 // External includes
21 
22 // Project includes
23 #include "spaces/ublas_space.h"
24 #include "includes/define.h"
27 
28 namespace Kratos
29 {
30 
33 
37 
41 
45 
49 
59 template<class TSparseSpaceType, class TDenseSpaceType, class TLinearSolverType,
60  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
61  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
63  : public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
64 {
65 public:
68 
71 
73 
75 
77 
79 
81 
82  typedef std::size_t SizeType;
83 
84  typedef std::size_t IndexType;
85 
89 
92 
102  double MaxTolerance,
103  unsigned int MaxIterationNumber,
104  unsigned int RequiredEigenvalueNumber,
105  typename TLinearSolverType::Pointer pLinearSolver
106  ): BaseType(MaxTolerance, MaxIterationNumber),
107  mRequiredEigenvalueNumber(RequiredEigenvalueNumber),
108  mpLinearSolver(pLinearSolver)
109  {
110 
111  }
112 
120  Parameters ThisParameters,
121  typename TLinearSolverType::Pointer pLinearSolver
122  ): mpLinearSolver(pLinearSolver)
123  {
124  Parameters DefaultParameters = Parameters(R"(
125  {
126  "solver_type" : "power_iteration_eigenvalue_solver",
127  "max_iteration" : 10000,
128  "tolerance" : 1e-8,
129  "required_eigen_number" : 1,
130  "shifting_convergence" : 0.25,
131  "verbosity" : 1,
132  "linear_solver_settings" : {}
133  })" );
134 
135  ThisParameters.ValidateAndAssignDefaults(DefaultParameters);
136 
137  mRequiredEigenvalueNumber = ThisParameters["required_eigen_number"].GetInt();
138  mEchoLevel = ThisParameters["verbosity"].GetInt();
139  BaseType::SetTolerance( ThisParameters["tolerance"].GetDouble() );
140  BaseType::SetMaxIterationsNumber( ThisParameters["max_iteration"].GetInt() );
141  }
142 
145  {
146 
147  }
148 
149 
152  {
153 
154  }
155 
156 
160 
163  {
164  BaseType::operator=(Other);
165  return *this;
166  }
167 
171 
179  void Solve(
181  SparseMatrixType& M,
182  DenseVectorType& Eigenvalues,
183  DenseMatrixType& Eigenvectors
184  ) override
185  {
186 
187  const SizeType size = K.size1();
188  const SizeType max_iteration = BaseType::GetMaxIterationsNumber();
189  const double tolerance = BaseType::GetTolerance();
190 
191  VectorType x = boost::numeric::ublas::zero_vector<double>(size);
192  VectorType y = boost::numeric::ublas::zero_vector<double>(size);
193 
195 
196  if(Eigenvalues.size() < 1)
197  Eigenvalues.resize(1, 0.0);
198 
199  // Starting with first step
200  double beta = 0.0;
201  double rho = 0.0;
202  double old_rho = Eigenvalues[0];
203  VectorType y_old = boost::numeric::ublas::zero_vector<double>(size);
204 
205  for(SizeType i = 0 ; i < max_iteration ; i++) {
206  // K*x = y
207  mpLinearSolver->Solve(K, x, y);
208 
209  rho = inner_prod(y, x);
210 
211  // y = M*x
213  beta = inner_prod(x, y);
214 
215  KRATOS_ERROR_IF(beta <= 0.0) << "M is not Positive-definite. beta = " << beta << std::endl;
216 
217  rho /= beta;
218  beta = std::sqrt(beta);
219  TSparseSpaceType::InplaceMult(y, 1.0/beta);
220 
221  KRATOS_ERROR_IF(rho == 0.0) << "Perpendicular eigenvector to M" << std::endl;
222 
223  const double convergence_rho = std::abs((rho - old_rho) / rho);
224  const double convergence_norm = TSparseSpaceType::TwoNorm(y - y_old)/TSparseSpaceType::TwoNorm(y);
225 
226  if (mEchoLevel > 1)
227  KRATOS_INFO("Power Iterator Eigenvalue Solver: ") << "Iteration: " << i << " \t beta: " << beta << "\trho: " << rho << " \tConvergence norm: " << convergence_norm << " \tConvergence rho: " << convergence_rho << std::endl;
228 
229  if(convergence_norm < tolerance || convergence_rho < tolerance)
230  break;
231 
232  old_rho = rho;
233  TSparseSpaceType::Assign(y_old, 1.0, y);
234  }
235 
236  if (mEchoLevel > 0) {
237  KRATOS_INFO("rho: ") << rho << std::endl;
238  KRATOS_INFO("y: ") << y << std::endl;
239  }
240 
241  Eigenvalues[0] = rho;
242 
243  if((Eigenvectors.size1() < 1) || (Eigenvectors.size2() < size))
244  Eigenvectors.resize(1,size);
245 
246  for(SizeType i = 0 ; i < size ; i++)
247  Eigenvectors(0,i) = y[i];
248  }
249 
253 
254 
258 
259 
263 
265  std::string Info() const override
266  {
267  std::stringstream buffer;
268  buffer << "Power iteration eigenvalue solver with " << BaseType::GetPreconditioner()->Info();
269  return buffer.str();
270  }
271 
273  void PrintInfo(std::ostream& rOStream) const override
274  {
275  rOStream << Info();
276  }
277 
279  void PrintData(std::ostream& rOStream) const override
280  {
281  BaseType::PrintData(rOStream);
282  }
283 
284 
288 
289 
291 
292 protected:
295 
296 
300 
301 
305 
306 
310 
311 
315 
316 
320 
321 
325 
326 
328 
329 private:
332 
333 
337 
338  unsigned int mRequiredEigenvalueNumber;
339 
340  unsigned int mEchoLevel;
341 
342  typename TLinearSolverType::Pointer mpLinearSolver;
343 
347 
348 
352 
353 
357 
358 
362 
363 
367 
368 
370 
371 }; // Class PowerIterationEigenvalueSolver
372 
374 
377 
378 
382 
383 
385 template<class TSparseSpaceType, class TDenseSpaceType,
386  class TPreconditionerType,
387  class TReordererType>
388 inline std::istream& operator >> (std::istream& IStream,
389  PowerIterationEigenvalueSolver<TSparseSpaceType, TDenseSpaceType,
390  TPreconditionerType, TReordererType>& rThis)
391 {
392  return IStream;
393 }
394 
396 template<class TSparseSpaceType, class TDenseSpaceType,
397  class TPreconditionerType,
398  class TReordererType>
399 inline std::ostream& operator << (std::ostream& OStream,
400  const PowerIterationEigenvalueSolver<TSparseSpaceType, TDenseSpaceType,
401  TPreconditionerType, TReordererType>& rThis)
402 {
403  rThis.PrintInfo(OStream);
404  OStream << std::endl;
405  rThis.PrintData(OStream);
406 
407  return OStream;
408 }
410 
411 
412 } // namespace Kratos.
413 
414 #endif // KRATOS_POWER_ITERATION_EIGENVALUE_SOLVER_H_INCLUDED defined
Base class for all the iterative solvers in Kratos.
Definition: iterative_solver.h:68
void SetTolerance(double NewTolerance) override
This method allows to set the tolerance in the linear solver.
Definition: iterative_solver.h:305
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: iterative_solver.h:360
virtual void SetMaxIterationsNumber(unsigned int NewMaxIterationsNumber)
Definition: iterative_solver.h:285
IterativeSolver & operator=(const IterativeSolver &Other)
Assignment operator.
Definition: iterative_solver.h:176
virtual IndexType GetMaxIterationsNumber()
Definition: iterative_solver.h:290
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
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
This class uses the inverted power iteration method to obtain the lowest eigenvalue of a system.
Definition: power_iteration_eigenvalue_solver.h:64
std::size_t IndexType
Definition: power_iteration_eigenvalue_solver.h:84
TDenseSpaceType::MatrixType DenseMatrixType
Definition: power_iteration_eigenvalue_solver.h:78
PowerIterationEigenvalueSolver(double MaxTolerance, unsigned int MaxIterationNumber, unsigned int RequiredEigenvalueNumber, typename TLinearSolverType::Pointer pLinearSolver)
Alternative constructor.
Definition: power_iteration_eigenvalue_solver.h:101
PowerIterationEigenvalueSolver(Parameters ThisParameters, typename TLinearSolverType::Pointer pLinearSolver)
Alternative constructor.
Definition: power_iteration_eigenvalue_solver.h:119
void Solve(SparseMatrixType &K, SparseMatrixType &M, DenseVectorType &Eigenvalues, DenseMatrixType &Eigenvectors) override
The power iteration algorithm.
Definition: power_iteration_eigenvalue_solver.h:179
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: power_iteration_eigenvalue_solver.h:273
std::string Info() const override
Turn back information as a string.
Definition: power_iteration_eigenvalue_solver.h:265
TSparseSpaceType::VectorType VectorType
Definition: power_iteration_eigenvalue_solver.h:76
std::size_t SizeType
Definition: power_iteration_eigenvalue_solver.h:82
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: power_iteration_eigenvalue_solver.h:279
~PowerIterationEigenvalueSolver() override
Destructor.
Definition: power_iteration_eigenvalue_solver.h:151
PowerIterationEigenvalueSolver(const PowerIterationEigenvalueSolver &Other)
Copy constructor.
Definition: power_iteration_eigenvalue_solver.h:144
TSparseSpaceType::MatrixType SparseMatrixType
Definition: power_iteration_eigenvalue_solver.h:74
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: power_iteration_eigenvalue_solver.h:72
PowerIterationEigenvalueSolver()
Default constructor.
Definition: power_iteration_eigenvalue_solver.h:91
TDenseSpaceType::VectorType DenseVectorType
Definition: power_iteration_eigenvalue_solver.h:80
KRATOS_CLASS_POINTER_DEFINITION(PowerIterationEigenvalueSolver)
Pointer definition of PowerIterationEigenvalueSolver.
PowerIterationEigenvalueSolver & operator=(const PowerIterationEigenvalueSolver &Other)
Assignment operator.
Definition: power_iteration_eigenvalue_solver.h:162
static void RandomInitialize(const SparseMatrixType &K, VectorType &R, const bool Inverse=false)
This method initializes a vector using a normal distribution. The mean and the variance is taken from...
Definition: random_initializer_utility.h:118
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO(label)
Definition: logger.h:250
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
LinearSolver< TSpaceType< TDataType >, TLocalSpaceType< TOtherDataType > > TLinearSolverType
Definition: add_linear_solvers_to_python.cpp:50
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
void Mult(TSpaceType &dummy, typename TSpaceType::MatrixType &rA, typename TSpaceType::VectorType &rX, typename TSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:98
void Assign(const Expression &rExpression, const IndexType EntityIndex, const IndexType EntityDataBeginIndex, TDataType &rValue, std::index_sequence< TIndex... >)
Definition: variable_expression_data_io.cpp:41
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
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
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
rho
Definition: generate_droplet_dynamics.py:86
x
Definition: sensitivityMatrix.py:49
K
Definition: sensitivityMatrix.py:73
integer i
Definition: TensorModule.f:17