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.
eigen_dense_eigenvalue_solver.h
Go to the documentation of this file.
1 /* KRATOS _ _ ____ _
2 // | | (_)_ __ ___ __ _ _ __/ ___| ___ | |_ _____ _ __ ___
3 // | | | | '_ \ / _ \/ _` | '__\___ \ / _ \| \ \ / / _ \ '__/ __|
4 // | |___| | | | | __/ (_| | | ___) | (_) | |\ V / __/ | \__ |
5 // |_____|_|_| |_|\___|\__,_|_| |____/ \___/|_| \_/ \___|_| |___/ Application
6 //
7 // Author: Manuel Messmer
8 */
9 
10 #if !defined(KRATOS_EIGEN_DENSE_EIGENVALUE_SOLVER_H_INCLUDED)
11 #define KRATOS_EIGEN_DENSE_EIGENVALUE_SOLVER_H_INCLUDED
12 
13 // External includes
14 #include <Eigen/Core>
15 #include <Eigen/Eigenvalues>
16 // Project includes
17 #include "includes/define.h"
18 #include "linear_solvers_define.h"
19 #include "spaces/ublas_space.h"
22 
23 namespace Kratos {
24 
25 template <typename TScalar = double,
26  class TSparseSpaceType = TUblasDenseSpace<TScalar>,
27  class TDenseSpaceType = TUblasDenseSpace<TScalar>>
29  public LinearSolver<TSparseSpaceType, TDenseSpaceType>
30 {
31  Parameters mParam;
32 
33  int mEchoLevel;
34 
35 public:
37 
39 
41 
42  DenseEigenvalueSolver( Parameters param) : mParam(param){
43 
44  Parameters default_params(R"(
45  {
46  "solver_type" : "dense_eigensolver",
47  "ascending_order" : true,
48  "echo_level" : 0
49  })");
50 
51  mParam.ValidateAndAssignDefaults(default_params);
52 
53  mEchoLevel = mParam["echo_level"].GetInt();
54  }
55 
56  ~DenseEigenvalueSolver() override {}
57 
67  DenseMatrixType& rDummy,
68  DenseVectorType& rEigenvalues,
69  DenseMatrixType& rEigenvectors) override
70  {
71 
72  BuiltinTimer eigensolver_timer;
73 
74  KRATOS_INFO_IF("DenseEigenvalueSolver", mEchoLevel > 0) << "Start" << std::endl;
75 
76  using vector_t = Kratos::EigenDynamicVector<TScalar>;
77  using matrix_t = Kratos::EigenDynamicMatrix<TScalar>;
78 
79  Eigen::Map<matrix_t> A(rA.data().begin(), rA.size1(), rA.size2());
80 
81  Eigen::SelfAdjointEigenSolver<matrix_t> solver;
82 
83  solver.compute(A, Eigen::ComputeEigenvectors);
84 
85  rEigenvalues.resize(rA.size1());
86  rEigenvectors.resize(rA.size1(), rA.size1());
87 
88  Eigen::Map<vector_t> eigvals (rEigenvalues.data().begin(), rEigenvalues.size());
89  Eigen::Map<matrix_t> eigvecs (rEigenvectors.data().begin(), rEigenvectors.size1(), rEigenvectors.size2());
90 
91  if( mParam["ascending_order"].GetBool() ){
92  eigvals = solver.eigenvalues();
93  eigvecs = solver.eigenvectors();
94  }
95  else{
96  eigvals = solver.eigenvalues().reverse();
97  eigvecs = solver.eigenvectors().rowwise().reverse();
98  }
99 
100  const bool success = solver.info() == Eigen::Success;
101 
102  if( success ){
103  KRATOS_INFO_IF("DenseEigenvalueSolver", mEchoLevel > 0) << "Completed in "
104  << eigensolver_timer << std::endl;
105 
106  Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols, ", ", ", ", "", "", "[ ", " ]");
107 
108  KRATOS_INFO_IF("", mEchoLevel > 1)
109  << " Eigenvalues = " << eigvals.transpose().format(fmt) << std::endl;
110  }
111  else{
112  KRATOS_WARNING("DenseEigenvalueSolver") << "Decomposition failed!" << std::endl;
113  }
114  }
115 
116  void PrintInfo(std::ostream &rOStream) const override
117  {
118  rOStream << "DenseEigenvalueSolver";
119  }
120 
121 };
122 
123 } // namespace Kratos
124 
125 #endif // defined(KRATOS_EIGEN_DENSE_EIGENVALUE_SOLVER_H_INCLUDED)
Definition: builtin_timer.h:26
Definition: eigen_dense_eigenvalue_solver.h:30
TDenseSpaceType::VectorType DenseVectorType
Definition: eigen_dense_eigenvalue_solver.h:40
KRATOS_CLASS_POINTER_DEFINITION(DenseEigenvalueSolver)
~DenseEigenvalueSolver() override
Definition: eigen_dense_eigenvalue_solver.h:56
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: eigen_dense_eigenvalue_solver.h:116
DenseEigenvalueSolver(Parameters param)
Definition: eigen_dense_eigenvalue_solver.h:42
TDenseSpaceType::MatrixType DenseMatrixType
Definition: eigen_dense_eigenvalue_solver.h:38
void Solve(DenseMatrixType &rA, DenseMatrixType &rDummy, DenseVectorType &rEigenvalues, DenseMatrixType &rEigenvectors) override
Dense eigenvalue solver.
Definition: eigen_dense_eigenvalue_solver.h:66
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
#define KRATOS_WARNING(label)
Definition: logger.h:265
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: linear_solvers_define.h:32
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: linear_solvers_define.h:33
solver
Definition: script.py:98
A
Definition: sensitivityMatrix.py:70