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_highest_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: Vicente Mataix Ferrandiz
11 //
12 //
13 
14 #if !defined(KRATOS_POWER_ITERATION_HIGHEST_EIGENVALUE_SOLVER_H_INCLUDED )
15 #define KRATOS_POWER_ITERATION_HIGHEST_EIGENVALUE_SOLVER_H_INCLUDED
16 
17 // System includes
18 
19 // External includes
20 #include "boost/range/algorithm/max_element.hpp"
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 
58 template<class TSparseSpaceType, class TDenseSpaceType, class TLinearSolverType,
59  class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
60  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
62  : public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
63 {
64 public:
67 
70 
72 
74 
76 
78 
80 
81  typedef std::size_t SizeType;
82 
83  typedef std::size_t IndexType;
84 
88 
91 
101  double MaxTolerance,
102  unsigned int MaxIterationNumber,
103  unsigned int RequiredEigenvalueNumber,
104  typename TLinearSolverType::Pointer pLinearSolver
105  ): BaseType(MaxTolerance, MaxIterationNumber),
106  mRequiredEigenvalueNumber(RequiredEigenvalueNumber),
107  mpLinearSolver(pLinearSolver)
108  {
109 
110  }
111 
119  Parameters ThisParameters,
120  typename TLinearSolverType::Pointer pLinearSolver
121  ): mpLinearSolver(pLinearSolver)
122  {
123  Parameters DefaultParameters = Parameters(R"(
124  {
125  "solver_type" : "power_iteration_highest_eigenvalue_solver",
126  "max_iteration" : 10000,
127  "tolerance" : 1e-8,
128  "required_eigen_number" : 1,
129  "shifting_convergence" : 0.25,
130  "verbosity" : 1,
131  "linear_solver_settings" : {}
132  })" );
133 
134  ThisParameters.ValidateAndAssignDefaults(DefaultParameters);
135 
136  mRequiredEigenvalueNumber = ThisParameters["required_eigen_number"].GetInt();
137  mEchoLevel = ThisParameters["verbosity"].GetInt();
138  BaseType::SetTolerance( ThisParameters["tolerance"].GetDouble() );
139  BaseType::SetMaxIterationsNumber( ThisParameters["max_iteration"].GetInt() );
140  }
141 
144  {
145 
146  }
147 
148 
151  {
152 
153  }
154 
155 
159 
162  {
163  BaseType::operator=(Other);
164  return *this;
165  }
166 
170 
178  void Solve(
180  SparseMatrixType& M,
181  DenseVectorType& Eigenvalues,
182  DenseMatrixType& Eigenvectors
183  ) override
184  {
185  const SizeType size = K.size1();
186  const SizeType max_iteration = BaseType::GetMaxIterationsNumber();
187  const double tolerance = BaseType::GetTolerance();
188 
189  VectorType x = boost::numeric::ublas::zero_vector<double>(size);
190  VectorType y = boost::numeric::ublas::zero_vector<double>(size);
191 
193 
194  if(Eigenvalues.size() < 1) {
195  Eigenvalues.resize(1);
196  Eigenvalues[0] = 0.0;
197  }
198 
199  // Starting with first step
200  double rho = 0.0;
201  double old_rho = Eigenvalues[0];
202  VectorType y_old = boost::numeric::ublas::zero_vector<double>(size);
203 
204  for(SizeType i = 0 ; i < max_iteration ; i++) {
205  // x = K*y
207 
208  // y = M*x
210 
211  rho = static_cast<double>(*boost::max_element(y));
212 
213  KRATOS_ERROR_IF(rho == 0.0) << "Perpendicular eigenvector to M" << std::endl;
214 
215  TSparseSpaceType::InplaceMult(y, 1.0/rho);
216 
217  const double convergence_rho = std::abs((rho - old_rho) / rho);
218  const double norm_y = TSparseSpaceType::TwoNorm(y);
219  double convergence_norm = TSparseSpaceType::TwoNorm(y - y_old);
220  if (norm_y > 0.0)
221  convergence_norm /= norm_y;
222 
223  if (mEchoLevel > 1)
224  KRATOS_INFO("Power Iterator Highest Eigenvalue Solver: ") << "Iteration: " << i << "\trho: " << rho << " \tConvergence norm: " << convergence_norm << " \tConvergence rho: " <<
225  convergence_rho << std::endl;
226 
227  if(convergence_norm < tolerance || convergence_rho < tolerance)
228  break;
229 
230  old_rho = rho;
231  TSparseSpaceType::Assign(y_old, 1.0, y);
232  }
233 
234  if (mEchoLevel > 0) {
235  KRATOS_INFO("rho: ") << rho << std::endl;
236  KRATOS_INFO("y: ") << y << std::endl;
237  }
238 
239  Eigenvalues[0] = rho;
240 
241  if((Eigenvectors.size1() != 1) || (Eigenvectors.size2() < size))
242  Eigenvectors.resize(1, size, false);
243 
244  for(SizeType i = 0 ; i < size ; i++)
245  Eigenvectors(0,i) = y[i];
246  }
247 
251 
252 
256 
257 
261 
263  std::string Info() const override
264  {
265  std::stringstream buffer;
266  buffer << "Power iteration eigenvalue solver with " << BaseType::GetPreconditioner()->Info();
267  return buffer.str();
268  }
269 
271  void PrintInfo(std::ostream& rOStream) const override
272  {
273  rOStream << Info();
274  }
275 
277  void PrintData(std::ostream& rOStream) const override
278  {
279  BaseType::PrintData(rOStream);
280  }
281 
282 
286 
287 
289 
290 protected:
293 
294 
298 
299 
303 
304 
308 
309 
313 
314 
318 
319 
323 
324 
326 
327 private:
330 
331 
335 
336  unsigned int mRequiredEigenvalueNumber;
337 
338  unsigned int mEchoLevel;
339 
340  typename TLinearSolverType::Pointer mpLinearSolver;
341 
345 
346 
350 
351 
355 
356 
360 
361 
365 
366 
368 
369 }; // Class PowerIterationHighestEigenvalueSolver
370 
372 
375 
376 
380 
381 
383 template<class TSparseSpaceType, class TDenseSpaceType,
384  class TPreconditionerType,
385  class TReordererType>
386 inline std::istream& operator >> (std::istream& IStream,
387  PowerIterationHighestEigenvalueSolver<TSparseSpaceType, TDenseSpaceType,
388  TPreconditionerType, TReordererType>& rThis)
389 {
390  return IStream;
391 }
392 
394 template<class TSparseSpaceType, class TDenseSpaceType,
395  class TPreconditionerType,
396  class TReordererType>
397 inline std::ostream& operator << (std::ostream& OStream,
398  const PowerIterationHighestEigenvalueSolver<TSparseSpaceType, TDenseSpaceType,
399  TPreconditionerType, TReordererType>& rThis)
400 {
401  rThis.PrintInfo(OStream);
402  OStream << std::endl;
403  rThis.PrintData(OStream);
404 
405  return OStream;
406 }
408 
409 
410 } // namespace Kratos.
411 
412 #endif // KRATOS_POWER_ITERATION_HIGHEST_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_highest_eigenvalue_solver.h:63
PowerIterationHighestEigenvalueSolver & operator=(const PowerIterationHighestEigenvalueSolver &Other)
Assignment operator.
Definition: power_iteration_highest_eigenvalue_solver.h:161
std::size_t IndexType
Definition: power_iteration_highest_eigenvalue_solver.h:83
PowerIterationHighestEigenvalueSolver(Parameters ThisParameters, typename TLinearSolverType::Pointer pLinearSolver)
Alternative constructor.
Definition: power_iteration_highest_eigenvalue_solver.h:118
std::size_t SizeType
Definition: power_iteration_highest_eigenvalue_solver.h:81
PowerIterationHighestEigenvalueSolver()
Default constructor.
Definition: power_iteration_highest_eigenvalue_solver.h:90
TDenseSpaceType::MatrixType DenseMatrixType
Definition: power_iteration_highest_eigenvalue_solver.h:77
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: power_iteration_highest_eigenvalue_solver.h:71
~PowerIterationHighestEigenvalueSolver() override
Destructor.
Definition: power_iteration_highest_eigenvalue_solver.h:150
PowerIterationHighestEigenvalueSolver(const PowerIterationHighestEigenvalueSolver &Other)
Copy constructor.
Definition: power_iteration_highest_eigenvalue_solver.h:143
std::string Info() const override
Turn back information as a string.
Definition: power_iteration_highest_eigenvalue_solver.h:263
PowerIterationHighestEigenvalueSolver(double MaxTolerance, unsigned int MaxIterationNumber, unsigned int RequiredEigenvalueNumber, typename TLinearSolverType::Pointer pLinearSolver)
Alternative constructor.
Definition: power_iteration_highest_eigenvalue_solver.h:100
KRATOS_CLASS_POINTER_DEFINITION(PowerIterationHighestEigenvalueSolver)
Pointer definition of PowerIterationHighestEigenvalueSolver.
TSparseSpaceType::VectorType VectorType
Definition: power_iteration_highest_eigenvalue_solver.h:75
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: power_iteration_highest_eigenvalue_solver.h:271
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: power_iteration_highest_eigenvalue_solver.h:277
TSparseSpaceType::MatrixType SparseMatrixType
Definition: power_iteration_highest_eigenvalue_solver.h:73
void Solve(SparseMatrixType &K, SparseMatrixType &M, DenseVectorType &Eigenvalues, DenseMatrixType &Eigenvectors) override
The power iteration algorithm.
Definition: power_iteration_highest_eigenvalue_solver.h:178
TDenseSpaceType::VectorType DenseVectorType
Definition: power_iteration_highest_eigenvalue_solver.h:79
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
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