11 #if !defined(KRATOS_EIGENSYSTEM_SOLVER_H_INCLUDED)
12 #define KRATOS_EIGENSYSTEM_SOLVER_H_INCLUDED
16 #include <Eigen/Eigenvalues>
21 #if defined EIGEN_USE_MKL_ALL
34 class TSparseSpaceType = UblasSpace<double, CompressedMatrix, Vector>,
35 class TDenseSpaceType = UblasSpace<double, Matrix, Vector>,
36 class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
37 class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType>>
39 :
public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
60 "solver_type": "eigen_eigensystem",
61 "number_of_eigenvalues": 1,
62 "normalize_eigenvectors": false,
63 "use_mkl_if_available": true,
64 "max_iteration": 1000,
69 mParam.ValidateAndAssignDefaults(default_params);
98 const int nroot = mParam[
"number_of_eigenvalues"].GetInt();
101 const int echo_level = mParam[
"echo_level"].GetInt();
109 const auto&
a = a_wrapper.
matrix();
110 const auto&
b = b_wrapper.
matrix();
121 int nc =
std::min(2 * nroot, nroot + 8);
128 vector_t prev_eigv = vector_t::Zero(nc);
131 matrix_t r = matrix_t::Zero(nn, nc);
132 for (
int i = 0;
i != nn; ++
i) {
133 r(
i, 0) =
b.coeff(
i,
i);
140 for (
int i = 0;
i != nn; ++
i) {
141 w(
i) = r(
i, 0) /
a.coeff(
i,
i);
152 for (
int j = 1;
j != nc; ++
j) {
155 for (
int i = 0;
i !=
l; ++
i) {
162 for (
int i =
l - 1;
i != nn; ++
i) {
179 #if defined USE_EIGEN_MKL
180 if (mParam[
"use_mkl_if_available"].GetBool()) {
181 p_solver = Kratos::make_unique<DirectSolverWrapper<EigenPardisoLUSolver<double>>>();
183 p_solver = Kratos::make_unique<DirectSolverWrapper<EigenSparseLUSolver<double>>>();
186 p_solver = Kratos::make_unique<DirectSolverWrapper<EigenSparseLUSolver<double>>>();
189 p_solver->Compute(
a);
193 Eigen::GeneralizedSelfAdjointEigenSolver<matrix_t> eig;
200 for (
int j = 0;
j != nc; ++
j) {
202 p_solver->Solve(
tmp, tt);
204 for (
int i =
j;
i != nc; ++
i) {
205 ar(
i,
j) = r.col(
i).dot(tt);
211 for (
int j = 0;
j != nc; ++
j) {
214 for (
int i =
j;
i != nc; ++
i) {
215 br(
i,
j) = r.col(
i).dot(tt);
223 if(eig.info() != Eigen::Success) {
224 KRATOS_WARNING(
"EigensystemSolver:") <<
"Eigen solution was not successful!" << std::endl;
228 r *= eig.eigenvectors();
230 bool is_converged =
true;
231 for (
int i = 0;
i != nc;
i++) {
232 double eigv = eig.eigenvalues()(
i);
233 double dif = eigv - prev_eigv(
i);
234 double rtolv = std::abs(dif / eigv);
236 if (rtolv > tolerance) {
237 is_converged =
false;
238 KRATOS_WARNING_IF(
"EigensystemSolver:",
echo_level > 1) <<
"Convergence not reached for eigenvalue #"<<
i+1<<
": " << rtolv <<
"." << std::endl;
244 KRATOS_INFO_IF(
"EigensystemSolver:",
echo_level > 0) <<
"Convergence reached after " <<
iteration <<
" iterations within a relative tolerance: " << tolerance << std::endl;
247 KRATOS_INFO_IF(
"EigensystemSolver:",
echo_level > 0) <<
"Convergence not reached in " << max_iteration <<
" iterations." << std::endl;
251 prev_eigv = eig.eigenvalues();
255 if (
static_cast<int>(rEigenvalues.size()) != nroot) {
256 rEigenvalues.resize(nroot);
258 if (
static_cast<int>(rEigenvectors.size1()) != nroot ||
static_cast<int>(rEigenvectors.size2()) != nn) {
259 rEigenvectors.resize(nroot, nn);
262 Eigen::Map<vector_t> eigvals (rEigenvalues.data().begin(), rEigenvalues.size());
263 Eigen::Map<matrix_t> eigvecs (rEigenvectors.data().begin(), rEigenvectors.size1(), rEigenvectors.size2());
265 eigvals = eig.eigenvalues().head(nroot);
267 for (
int i = 0;
i != nroot; ++
i) {
269 p_solver->Solve(
tmp, eigvecs.row(
i));
270 eigvecs.row(
i).normalize();
276 if(mParam[
"normalize_eigenvectors"].GetBool())
278 for (
int i = 0;
i != nroot; ++
i)
280 const double tmp = eigvecs.row(
i) *
b * eigvecs.row(
i).transpose();
281 const double factor = 1.0 / std::sqrt(
tmp);
289 Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols,
", ",
", ",
"",
"",
"[ ",
" ]");
292 <<
" Eigenvalues = " << eigvals.transpose().format(fmt) << std::endl;
301 rOStream <<
"EigensystemSolver";
313 struct DirectSolverWrapperBase
315 typedef Eigen::Map<const Kratos::EigenSparseMatrix<double>> MatrixMapType;
317 typedef Eigen::Ref<const EigenVectorType> ConstVectorRefType;
318 typedef Eigen::Ref<EigenVectorType> VectorRefType;
320 virtual ~DirectSolverWrapperBase() =
default;
321 virtual void Compute(MatrixMapType
a) = 0;
322 virtual void Solve(ConstVectorRefType
b, VectorRefType
x) = 0;
325 template<
class TSolver>
326 struct DirectSolverWrapper : DirectSolverWrapperBase
328 typedef DirectSolverWrapperBase
BaseType;
329 typedef typename BaseType::MatrixMapType MatrixMapType;
330 typedef typename BaseType::VectorRefType VectorRefType;
331 typedef typename BaseType::ConstVectorRefType ConstVectorRefType;
333 void Compute(MatrixMapType
a)
override {mSolver.Compute(
a);}
334 void Solve(ConstVectorRefType
b, VectorRefType
x)
override {mSolver.Solve(
b,
x);}
345 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
347 std::istream& rIStream,
350 TReordererType>& rThis)
358 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
360 std::ostream& rOStream,
364 rOStream << std::endl;
Definition: builtin_timer.h:26
Definition: eigensystem_solver.h:40
void Solve(SparseMatrixType &rK, SparseMatrixType &rM, VectorType &rEigenvalues, DenseMatrixType &rEigenvectors) override
Definition: eigensystem_solver.h:86
TDenseSpaceType::MatrixType DenseMatrixType
Definition: eigensystem_solver.h:52
~EigensystemSolver() override
Definition: eigensystem_solver.h:75
TSparseSpaceType::VectorType VectorType
Definition: eigensystem_solver.h:50
EigensystemSolver(Parameters param)
Definition: eigensystem_solver.h:54
TSparseSpaceType::MatrixType SparseMatrixType
Definition: eigensystem_solver.h:48
void PrintInfo(std::ostream &rOStream) const override
Definition: eigensystem_solver.h:299
void PrintData(std::ostream &rOStream) const override
Definition: eigensystem_solver.h:307
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: eigensystem_solver.h:46
KRATOS_CLASS_POINTER_DEFINITION(EigensystemSolver)
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
virtual void SetMaxIterationsNumber(unsigned int NewMaxIterationsNumber)
Definition: iterative_solver.h:285
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
TSparseSpaceType::MatrixType SparseMatrixType
Definition: linear_solver.h:73
TDenseSpaceType::MatrixType DenseMatrixType
Definition: linear_solver.h:81
TSparseSpaceType::VectorType VectorType
Definition: linear_solver.h:77
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
Definition: ublas_wrapper.h:32
const Eigen::Map< const TEigenSparseMatrix > & matrix() const
Definition: ublas_wrapper.h:53
#define KRATOS_INFO(label)
Definition: logger.h:250
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
#define KRATOS_WARNING(label)
Definition: logger.h:265
iteration
Definition: DEM_benchmarks.py:172
static double min(double a, double b)
Definition: GeometryFunctions.h:71
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
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
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
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: linear_solvers_define.h:33
w
Definition: generate_convection_diffusion_explicit_element.py:108
a
Definition: generate_stokes_twofluid_element.py:77
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
int j
Definition: quadrature.py:648
echo_level
Definition: script.py:68
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
integer l
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254