10 #if !defined(KRATOS_SPECTRA_SYM_G_EIGS_SHIFT_SOLVER_H_INCLUDED)
11 #define KRATOS_SPECTRA_SYM_G_EIGS_SHIFT_SOLVER_H_INCLUDED
15 #include <Spectra/SymGEigsShiftSolver.h>
16 #if defined EIGEN_USE_MKL_ALL
32 class TSparseSpaceType = UblasSpace<double, CompressedMatrix, Vector>,
33 class TDenseSpaceType = UblasSpace<double, Matrix, Vector>,
34 class TPreconditionerType = Preconditioner<TSparseSpaceType, TDenseSpaceType>,
35 class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType>>
37 :
public IterativeSolver<TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType>
58 "solver_type": "spectra_sym_g_eigs_shift",
59 "number_of_eigenvalues": 1,
60 "normalize_eigenvectors": false,
61 "max_iteration": 1000,
66 mParam.ValidateAndAssignDefaults(default_params);
88 const int nroot = mParam[
"number_of_eigenvalues"].GetInt();
90 const int echo_level = mParam[
"echo_level"].GetInt();
91 const double shift = mParam[
"shift"].GetDouble();
98 const auto&
a = a_wrapper.
matrix();
99 const auto&
b = b_wrapper.
matrix();
108 using OpType = OwnSymShiftInvert<scalar_t>;
109 using BOpType = OwnSparseSymMatProd<scalar_t>;
113 const int ncv = 3 * nroot;
114 Spectra::SymGEigsShiftSolver<OpType, BOpType, Spectra::GEigsMode::ShiftInvert> eigs(op, Bop, nroot, ncv, shift);
117 const int nconv = eigs.compute(Spectra::SortRule::LargestAlge, max_iteration);
118 const int niter = eigs.num_iterations();
119 const int nops = eigs.num_operations();
125 KRATOS_ERROR_IF(eigs.info() != Spectra::CompInfo::Successful) <<
"SpectraSymGEigsShiftSolver: failed" << std::endl;
127 if (
static_cast<int>(rEigenvalues.size()) != nroot) {
128 rEigenvalues.resize(nroot);
130 if (
static_cast<int>(rEigenvectors.size1()) != nroot || rEigenvectors.size2() != rK.size1()) {
131 rEigenvectors.resize(nroot, rK.size1());
134 Eigen::Map<vector_t> eigvals (rEigenvalues.data().begin(), rEigenvalues.size());
135 Eigen::Map<matrix_t> eigvecs (rEigenvectors.data().begin(), rEigenvectors.size1(), rEigenvectors.size2());
138 eigvals = eigs.eigenvalues().reverse();
140 eigvecs = eigs.eigenvectors().transpose().colwise().reverse();
146 if(mParam[
"normalize_eigenvectors"].GetBool())
148 for (
int i = 0;
i != nroot; ++
i)
150 const double tmp = eigvecs.row(
i) * b_wrapper.
matrix() * eigvecs.row(
i).transpose();
151 const double factor = 1.0 / std::sqrt(
tmp);
159 Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols,
", ",
", ",
"",
"",
"[ ",
" ]");
161 KRATOS_INFO(
"SpectraSymGEigsShiftSolver:") <<
"Completed in " <<
timer << std::endl
162 <<
" Eigenvalues = " << eigvals.transpose().format(fmt) << std::endl;
171 rOStream <<
"SpectraSymGEigsShiftSolver";
190 template <
typename Scalar_>
191 class OwnSparseSymMatProd
197 using Scalar = Scalar_;
202 using MapConstVec = Eigen::Map<const Vector>;
203 using MapVec = Eigen::Map<Vector>;
206 using ConstGenericSparseMatrix =
const Eigen::Ref<const SparseMatrix>;
208 ConstGenericSparseMatrix m_mat;
218 OwnSparseSymMatProd(ConstGenericSparseMatrix&
mat) :
225 Index rows()
const {
return m_mat.rows(); }
229 Index cols()
const {
return m_mat.cols(); }
238 void perform_op(
const Scalar* x_in, Scalar* y_out)
const
240 MapConstVec
x(x_in, m_mat.cols());
241 MapVec
y(y_out, m_mat.rows());
242 y.noalias() = m_mat *
x;
250 return m_mat * mat_in;
258 return m_mat.coeff(
i,
j);
276 template <
typename Scalar_>
277 class OwnSymShiftInvert
283 using Scalar = Scalar_;
295 using MapConstVec = Eigen::Map<const Vector>;
296 using MapVec = Eigen::Map<Vector>;
298 using ResType = MatrixA;
300 #if defined EIGEN_USE_MKL_ALL
301 using FacType = Eigen::PardisoLU<ResType>;
303 using FacType = Eigen::SparseLU<ResType>;
306 using ConstGenericMatrixA =
const Eigen::Ref<const MatrixA>;
307 using ConstGenericMatrixB =
const Eigen::Ref<const MatrixB>;
309 ConstGenericMatrixA m_matA;
310 ConstGenericMatrixB m_matB;
324 OwnSymShiftInvert(ConstGenericMatrixA&
A, ConstGenericMatrixB&
B) :
325 m_matA(
A), m_matB(
B), m_n(
A.rows())
327 KRATOS_ERROR_IF(m_n !=
A.cols() || m_n !=
B.rows() || m_n !=
B.cols()) <<
"SymShiftInvert: A and B must be square matrices of the same size" << std::endl;
333 Index rows()
const {
return m_n; }
337 Index cols()
const {
return m_n; }
342 void set_shift(
const Scalar&
sigma)
345 m_solver.compute(m_matA);
347 m_solver.compute(m_matA -
sigma * m_matB);
349 const bool success = m_solver.info() == Eigen::Success;
350 KRATOS_ERROR_IF_NOT(success) <<
"SymShiftInvert: factorization failed with the given shift" << std::endl;
360 void perform_op(
const Scalar* x_in, Scalar* y_out)
const
362 MapConstVec
x(x_in, m_n);
363 MapVec
y(y_out, m_n);
364 y.noalias() = m_solver.solve(
x);
375 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
377 std::istream& rIStream,
380 TReordererType>& rThis)
388 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
390 std::ostream& rOStream,
394 rOStream << std::endl;
Definition: builtin_timer.h:26
Base class for all the iterative solvers in Kratos.
Definition: iterative_solver.h:68
virtual void SetMaxIterationsNumber(unsigned int NewMaxIterationsNumber)
Definition: iterative_solver.h:285
virtual IndexType GetMaxIterationsNumber()
Definition: iterative_solver.h:290
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: spectra_sym_g_eigs_shift_solver.h:38
SpectraSymGEigsShiftSolver(Parameters param)
Definition: spectra_sym_g_eigs_shift_solver.h:52
IterativeSolver< TSparseSpaceType, TDenseSpaceType, TPreconditionerType, TReordererType > BaseType
Definition: spectra_sym_g_eigs_shift_solver.h:44
~SpectraSymGEigsShiftSolver() override
Definition: spectra_sym_g_eigs_shift_solver.h:71
void PrintInfo(std::ostream &rOStream) const override
Definition: spectra_sym_g_eigs_shift_solver.h:169
TDenseSpaceType::MatrixType DenseMatrixType
Definition: spectra_sym_g_eigs_shift_solver.h:50
TSparseSpaceType::VectorType VectorType
Definition: spectra_sym_g_eigs_shift_solver.h:48
void PrintData(std::ostream &rOStream) const override
Definition: spectra_sym_g_eigs_shift_solver.h:177
KRATOS_CLASS_POINTER_DEFINITION(SpectraSymGEigsShiftSolver)
TSparseSpaceType::MatrixType SparseMatrixType
Definition: spectra_sym_g_eigs_shift_solver.h:46
void Solve(SparseMatrixType &rK, SparseMatrixType &rM, VectorType &rEigenvalues, DenseMatrixType &rEigenvectors) override
Definition: spectra_sym_g_eigs_shift_solver.h:76
Definition: ublas_wrapper.h:32
const Eigen::Map< const TEigenSparseMatrix > & matrix() const
Definition: ublas_wrapper.h:53
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO(label)
Definition: logger.h:250
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
mapped_matrix< double > SparseMatrix
Definition: ublas_interface.h:84
Expression::Pointer operator*(const Expression::ConstPointer &rpLeft, const double Right)
Internals::Matrix< double, AMatrix::dynamic, 1 > Vector
Definition: amatrix_interface.h:472
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: linear_solvers_define.h:32
Internals::Matrix< double, AMatrix::dynamic, AMatrix::dynamic > Matrix
Definition: amatrix_interface.h:470
Eigen::SparseMatrix< _Scalar, Eigen::RowMajor, int > EigenSparseMatrix
Definition: linear_solvers_define.h:35
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
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
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
float sigma
Definition: rotating_cone.py:79
mat
Definition: script_ELASTIC.py:155
echo_level
Definition: script.py:68
A
Definition: sensitivityMatrix.py:70
x
Definition: sensitivityMatrix.py:49
B
Definition: sensitivityMatrix.py:76
def MatrixB(DN)
This method defines the deformation matrix B.
Definition: sympy_fe_utilities.py:202
integer i
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254