20 #ifndef __mast_slepc_constrained_hermitian_eigen_solver_h__ 21 #define __mast_slepc_constrained_hermitian_eigen_solver_h__ 36 namespace SLEPcWrapper {
46 const std::vector<PetscInt> &dofs,
84 "Eigenvalue index must be less than n_converged");
89 EPSGetEigenvalue(
_eps, i, &re, &im);
109 "Eigenvalue index must be less than n_converged");
115 MatCreateVecs(
_A_sub, &vr, PETSC_NULL);
116 MatCreateVecs(
_A_sub, &vi, PETSC_NULL);
123 EPSGetEigenpair(
_eps, i, &eig, &im, vr, vi);
126 PetscScalar *vals =
nullptr;
127 VecGetArray(vr, &vals);
128 VecSetValues(x,
_dofs.size(),
_dofs.data(), vals, INSERT_VALUES);
129 VecRestoreArray(vr, &vals);
144 bool computeEigenvectors) {
152 MatGetSize(A_mat, &m, &n);
154 Assert2(m == n, m, n,
"Matrix must be square");
162 MatGetSize(*B_mat, &m2, &n2);
165 "Eigensolver type must be Generalized Hermitian");
166 Assert2(m==m2 && n==n2, m2, n2,
"A and B must be same size");
190 if (spectrum == EPS_LARGEST_MAGNITUDE)
191 EPSSetWhichEigenpairs(
_eps, EPS_LARGEST_MAGNITUDE);
192 else if (spectrum == EPS_SMALLEST_MAGNITUDE)
193 EPSSetWhichEigenpairs(
_eps, EPS_SMALLEST_MAGNITUDE);
194 else if (spectrum == EPS_LARGEST_IMAGINARY)
195 EPSSetWhichEigenpairs(
_eps, EPS_LARGEST_IMAGINARY);
196 else if (spectrum == EPS_SMALLEST_IMAGINARY)
197 EPSSetWhichEigenpairs(
_eps, EPS_SMALLEST_IMAGINARY);
198 else if (spectrum == EPS_LARGEST_REAL)
199 EPSSetWhichEigenpairs(
_eps, EPS_LARGEST_REAL);
200 else if (spectrum == EPS_SMALLEST_REAL)
201 EPSSetWhichEigenpairs(
_eps, EPS_SMALLEST_REAL);
203 Assert0(
false,
"Invalid spectrum type");
205 EPSSetDimensions(
_eps, nev, PETSC_DEFAULT, PETSC_DEFAULT);
206 EPSSetFromOptions(
_eps);
227 MatCreateVecs(B, &v1, PETSC_NULL);
228 MatCreateVecs(B, &v2, PETSC_NULL);
229 MatCreateVecs(B, &v3, PETSC_NULL);
240 VecDot(v1, v2, &denom);
243 MatMult(A_sens, v1, v2);
247 MatMult(*B_sens, v1, v3);
250 VecAXPY(v2, -eig, v3);
254 VecAXPY(v2, -eig, v1);
256 VecDot(v1, v2, &num);
268 EPSComputeError(
_eps, i, EPS_ERROR_RELATIVE, &error);
270 << std::setw(10) << i
271 << std::setw(30) << this->
eig(i)
272 << std::setw(30) << error << std::endl;
286 #
if (PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR > 7)
293 #
if (PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR > 7)
315 #endif // __mast_slepc_constrained_hermitian_eigen_solver_h__ ConstrainedHermitianEigenSolver(MPI_Comm comm, const std::vector< PetscInt > &dofs, EPSProblemType type)
dofs is the vector of unconstrained degrees of freedom on the local rank.
void solve(Mat A_mat, Mat *B_mat, uint_t nev, EPSWhich spectrum, bool computeEigenvectors)
method for eigenvalue problems
real_t eig(uint_t i)
this method returns the eigen value
void getEigenPair(uint_t i, real_t &eig, Vec x)
this method returns the eigen pair.
void printResidualForEigenPairs()
real_t sensitivity_solve(Mat B, Mat A_sens, Mat *B_sens, uint_t i)
compute the sensitivity of i th eigenvalue Dimension of the matrices B, A_sens and B_sens is equal t...
unsigned int n_converged()
virtual ~ConstrainedHermitianEigenSolver()
#define Assert0(cond, msg)
#define Assert2(cond, v1, v2, msg)
void _init_sub_matrices(Mat A_mat, Mat *B_mat)
const std::vector< PetscInt > & _dofs