10 #ifndef ANASAZI_SOLVER_UTILS_HPP
11 #define ANASAZI_SOLVER_UTILS_HPP
41 template<
class ScalarType,
class MV,
class OP>
133 int &nev,
int esType = 0);
163 template<
class ScalarType,
class MV,
class OP>
175 template<
class ScalarType,
class MV,
class OP>
178 const std::vector<int> &perm,
186 std::vector<int> permcopy(perm), swapvec(n-1);
187 std::vector<int> index(1);
191 TEUCHOS_TEST_FOR_EXCEPTION(n > MVT::GetNumberVecs(Q), std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): argument n larger than width of input multivector.");
197 for (i=0; i<n-1; i++) {
200 for (j=i; j<n; j++) {
201 if (permcopy[j] == i) {
205 TEUCHOS_TEST_FOR_EXCEPTION(j == n-1, std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): permutation index invalid.");
209 std::swap( permcopy[j], permcopy[i] );
215 for (i=n-2; i>=0; i--) {
222 std::swap( (*resids)[i], (*resids)[j] );
231 MVT::MvAddMv( one, *tmpQi, zero, *tmpQi, *tmpQj );
232 MVT::MvAddMv( one, *tmpQ, zero, *tmpQ, *tmpQi );
239 template<
class ScalarType,
class MV,
class OP>
241 const std::vector<int> &perm,
247 const int n = perm.size();
250 TEUCHOS_TEST_FOR_EXCEPTION(n != Q.
numCols(), std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): size of permutation vector not equal to number of columns.");
254 for (
int i=0; i<n; i++) {
255 blas.
COPY(m, copyQ[perm[i]], 1, Q[i], 1);
267 template<
class ScalarType,
class MV,
class OP>
270 const int n = MVT::GetNumberVecs(V);
271 const ScalarType ONE = SCT::one();
272 const ScalarType ZERO = SCT::zero();
275 if (MVT::GetNumberVecs(V) == 0 || MVT::GetGlobalLength(V) == 0 || k == 0) {
279 if (workMV == Teuchos::null) {
281 workMV = MVT::Clone(V,1);
283 else if (MVT::GetNumberVecs(*workMV) > 1) {
284 std::vector<int> first(1);
286 workMV = MVT::CloneViewNonConst(*workMV,first);
289 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*workMV) < 1,std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): work multivector was empty.");
294 TEUCHOS_TEST_FOR_EXCEPTION( (
int)tau.size() != k, std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): tau must have at least k entries.");
299 for (
int i=0; i<k; i++) {
302 std::vector<int> activeind(n-i);
303 for (
int j=0; j<n-i; j++) activeind[j] = j+i;
318 MVT::MvTimesMatAddMv(-tau[i],*actV,v,ZERO,*workMV);
323 MVT::MvTimesMatAddMv(ONE,*workMV,vT,ONE,*actV);
325 actV = Teuchos::null;
336 template<
class ScalarType,
class MV,
class OP>
343 int &nev,
int esType)
393 if (size < nev || size < 0) {
399 if ((esType == 0 || esType == 1)) {
400 if (MM == Teuchos::null) {
403 else if (MM->numCols() < size || MM->numRows() < size) {
410 if (theta.size() < (
unsigned int) size) {
418 std::string lapack_name =
"hetrd";
419 std::string lapack_opts =
"u";
420 int NB = lapack.
ILAENV(1, lapack_name, lapack_opts, size, -1, -1, -1);
421 int lwork = size*(NB+2);
422 std::vector<ScalarType> work(lwork);
423 std::vector<MagnitudeType> rwork(3*size-2);
426 std::vector<MagnitudeType> tt( size );
429 MagnitudeType tol = SCT::magnitude(SCT::squareroot(SCT::eps()));
443 for (rank = size; rank > 0; --rank) {
455 lapack.
HEGV(1,
'V',
'U', rank, KKcopy->values(), KKcopy->stride(),
456 MMcopy->values(), MMcopy->stride(), &tt[0], &work[0], lwork,
462 std::cerr << std::endl;
463 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, argument " << -info <<
"has an illegal value.\n";
464 std::cerr << std::endl;
476 for (
int i = 0; i < rank; ++i) {
477 for (
int j = 0; j < i; ++j) {
478 (*MMcopy)(i,j) = SCT::conjugate((*MM)(j,i));
484 std::logic_error,
"Anasazi::SolverUtils::directSolver() call to Teuchos::SerialDenseMatrix::multiply() returned an error.");
488 std::logic_error,
"Anasazi::SolverUtils::directSolver() call to Teuchos::SerialDenseMatrix::multiply() returned an error.");
489 MagnitudeType maxNorm = SCT::magnitude(zero);
490 MagnitudeType maxOrth = SCT::magnitude(zero);
491 for (
int i = 0; i < rank; ++i) {
492 for (
int j = i; j < rank; ++j) {
494 maxNorm = SCT::magnitude((*MMcopy)(i,j) - one) > maxNorm
495 ? SCT::magnitude((*MMcopy)(i,j) - one) : maxNorm;
497 maxOrth = SCT::magnitude((*MMcopy)(i,j)) > maxOrth
498 ? SCT::magnitude((*MMcopy)(i,j)) : maxOrth;
509 if ((maxNorm <= tol) && (maxOrth <= tol)) {
518 nev = (rank < nev) ? rank : nev;
520 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
521 for (
int i = 0; i < nev; ++i) {
522 blas.
COPY( rank, (*KKcopy)[i], 1, EV[i], 1 );
538 lapack.
HEGV(1,
'V',
'U', size, KKcopy->values(), KKcopy->stride(),
539 MMcopy->values(), MMcopy->stride(), &tt[0], &work[0], lwork,
545 std::cerr << std::endl;
546 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, argument " << -info <<
"has an illegal value.\n";
547 std::cerr << std::endl;
554 std::cerr << std::endl;
555 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, DPOTRF or DHEEV returned an error code (" << info <<
").\n";
556 std::cerr << std::endl;
563 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
564 for (
int i = 0; i < nev; ++i) {
565 blas.
COPY( size, (*KKcopy)[i], 1, EV[i], 1 );
579 lapack.
HEEV(
'V',
'U', size, KKcopy->values(), KKcopy->stride(), &tt[0], &work[0], lwork, &rwork[0], &info);
583 std::cerr << std::endl;
585 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In DHEEV, argument " << -info <<
" has an illegal value\n";
588 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In DHEEV, the algorithm failed to converge (" << info <<
").\n";
590 std::cerr << std::endl;
597 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
598 for (
int i = 0; i < nev; ++i) {
599 blas.
COPY( size, (*KKcopy)[i], 1, EV[i], 1 );
614 template<
class ScalarType,
class MV,
class OP>
622 MagnitudeType maxDiff = SCT::magnitude(SCT::zero());
624 int xc = MVT::GetNumberVecs(X);
625 int mxc = MVT::GetNumberVecs(MX);
627 TEUCHOS_TEST_FOR_EXCEPTION(xc != mxc,std::invalid_argument,
"Anasazi::SolverUtils::errorEquality(): input multivecs have different number of columns.");
632 MagnitudeType maxCoeffX = SCT::magnitude(SCT::zero());
633 std::vector<MagnitudeType> tmp( xc );
634 MVT::MvNorm(MX, tmp);
636 for (
int i = 0; i < xc; ++i) {
637 maxCoeffX = (tmp[i] > maxCoeffX) ? tmp[i] : maxCoeffX;
640 std::vector<int> index( 1 );
642 if (M != Teuchos::null) {
643 MtimesX = MVT::Clone( X, xc );
644 OPT::Apply( *M, X, *MtimesX );
647 MtimesX = MVT::CloneCopy(X);
649 MVT::MvAddMv( -1.0, MX, 1.0, *MtimesX, *MtimesX );
650 MVT::MvNorm( *MtimesX, tmp );
652 for (
int i = 0; i < xc; ++i) {
653 maxDiff = (tmp[i] > maxDiff) ? tmp[i] : maxDiff;
656 return (maxCoeffX == 0.0) ? maxDiff : maxDiff/maxCoeffX;
662 #endif // ANASAZI_SOLVER_UTILS_HPP
OrdinalType ILAENV(const OrdinalType &ispec, const std::string &NAME, const std::string &OPTS, const OrdinalType &N1=-1, const OrdinalType &N2=-1, const OrdinalType &N3=-1, const OrdinalType &N4=-1) const
Declaration of basic traits for the multivector type.
virtual ~SolverUtils()
Destructor.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
static Teuchos::ScalarTraits< ScalarType >::magnitudeType errorEquality(const MV &X, const MV &MX, Teuchos::RCP< const OP > M=Teuchos::null)
Return the maximum coefficient of the matrix scaled by the maximum coefficient of MX...
Anasazi's templated, static class providing utilities for the solvers.
void COPY(const OrdinalType &n, const ScalarType *x, const OrdinalType &incx, ScalarType *y, const OrdinalType &incy) const
Abstract class definition for Anasazi Output Managers.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void applyHouse(int k, MV &V, const Teuchos::SerialDenseMatrix< int, ScalarType > &H, const std::vector< ScalarType > &tau, Teuchos::RCP< MV > workMV=Teuchos::null)
Apply a sequence of Householder reflectors (from GEQRF) to a multivector, using minimal workspace...
static int directSolver(int size, const Teuchos::SerialDenseMatrix< int, ScalarType > &KK, Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > MM, Teuchos::SerialDenseMatrix< int, ScalarType > &EV, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &theta, int &nev, int esType=0)
Routine for computing the first NEV generalized eigenpairs of the Hermitian pencil (KK...
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
OrdinalType numCols() const
SolverUtils()
Constructor.
static void permuteVectors(const int n, const std::vector< int > &perm, MV &Q, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *resids=0)
Permute the vectors in a multivector according to the permutation vector perm, and optionally the res...
void HEEV(const char &JOBZ, const char &UPLO, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, MagnitudeType *W, ScalarType *WORK, const OrdinalType &lwork, MagnitudeType *RWORK, OrdinalType *info) const
void HEGV(const OrdinalType &itype, const char &JOBZ, const char &UPLO, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, ScalarType *B, const OrdinalType &ldb, MagnitudeType *W, ScalarType *WORK, const OrdinalType &lwork, MagnitudeType *RWORK, OrdinalType *info) const
OrdinalType numRows() const