29 #ifndef ANASAZI_SOLVER_UTILS_HPP
30 #define ANASAZI_SOLVER_UTILS_HPP
51 #include "Teuchos_ScalarTraits.hpp"
54 #include "Teuchos_BLAS.hpp"
55 #include "Teuchos_LAPACK.hpp"
56 #include "Teuchos_SerialDenseMatrix.hpp"
60 template<
class ScalarType,
class MV,
class OP>
64 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
65 typedef typename Teuchos::ScalarTraits<ScalarType> SCT;
82 static void permuteVectors(
const int n,
const std::vector<int> &perm, MV &Q, std::vector<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType >* resids = 0);
85 static void permuteVectors(
const std::vector<int> &perm, Teuchos::SerialDenseMatrix<int,ScalarType> &Q);
114 static void applyHouse(
int k, MV &V,
const Teuchos::SerialDenseMatrix<int,ScalarType> &H,
const std::vector<ScalarType> &tau, Teuchos::RCP<MV> workMV = Teuchos::null);
148 static int directSolver(
int size,
const Teuchos::SerialDenseMatrix<int,ScalarType> &KK,
149 Teuchos::RCP<
const Teuchos::SerialDenseMatrix<int,ScalarType> > MM,
150 Teuchos::SerialDenseMatrix<int,ScalarType> &EV,
151 std::vector<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &theta,
152 int &nev,
int esType = 0);
161 static typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
errorEquality(
const MV &X,
const MV &MX, Teuchos::RCP<const OP> M = Teuchos::null);
183 template<
class ScalarType,
class MV,
class OP>
195 template<
class ScalarType,
class MV,
class OP>
198 const std::vector<int> &perm,
200 std::vector<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType >* resids)
206 std::vector<int> permcopy(perm), swapvec(n-1);
207 std::vector<int> index(1);
208 ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
209 ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
211 TEUCHOS_TEST_FOR_EXCEPTION(n > MVT::GetNumberVecs(Q), std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): argument n larger than width of input multivector.");
217 for (i=0; i<n-1; i++) {
220 for (j=i; j<n; j++) {
221 if (permcopy[j] == i) {
225 TEUCHOS_TEST_FOR_EXCEPTION(j == n-1, std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): permutation index invalid.");
229 std::swap( permcopy[j], permcopy[i] );
235 for (i=n-2; i>=0; i--) {
242 std::swap( (*resids)[i], (*resids)[j] );
247 Teuchos::RCP<MV> tmpQ = MVT::CloneCopy( Q, index );
248 Teuchos::RCP<MV> tmpQj = MVT::CloneViewNonConst( Q, index );
250 Teuchos::RCP<MV> tmpQi = MVT::CloneViewNonConst( Q, index );
251 MVT::MvAddMv( one, *tmpQi, zero, *tmpQi, *tmpQj );
252 MVT::MvAddMv( one, *tmpQ, zero, *tmpQ, *tmpQi );
259 template<
class ScalarType,
class MV,
class OP>
261 const std::vector<int> &perm,
262 Teuchos::SerialDenseMatrix<int,ScalarType> &Q)
266 Teuchos::BLAS<int,ScalarType> blas;
267 const int n = perm.size();
268 const int m = Q.numRows();
270 TEUCHOS_TEST_FOR_EXCEPTION(n != Q.numCols(), std::invalid_argument,
"Anasazi::SolverUtils::permuteVectors(): size of permutation vector not equal to number of columns.");
273 Teuchos::SerialDenseMatrix<int,ScalarType> copyQ( Q );
274 for (
int i=0; i<n; i++) {
275 blas.COPY(m, copyQ[perm[i]], 1, Q[i], 1);
287 template<
class ScalarType,
class MV,
class OP>
290 const int n = MVT::GetNumberVecs(V);
291 const ScalarType ONE = SCT::one();
292 const ScalarType ZERO = SCT::zero();
295 if (MVT::GetNumberVecs(V) == 0 || MVText::GetGlobalLength(V) == 0 || k == 0) {
299 if (workMV == Teuchos::null) {
301 workMV = MVT::Clone(V,1);
303 else if (MVT::GetNumberVecs(*workMV) > 1) {
304 std::vector<int> first(1);
306 workMV = MVT::CloneViewNonConst(*workMV,first);
309 TEUCHOS_TEST_FOR_EXCEPTION(MVT::GetNumberVecs(*workMV) < 1,std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): work multivector was empty.");
313 TEUCHOS_TEST_FOR_EXCEPTION( H.numCols() != k, std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): H must have at least k columns.");
314 TEUCHOS_TEST_FOR_EXCEPTION( (
int)tau.size() != k, std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): tau must have at least k entries.");
315 TEUCHOS_TEST_FOR_EXCEPTION( H.numRows() != MVT::GetNumberVecs(V), std::invalid_argument,
"Anasazi::SolverUtils::applyHouse(): Size of H,V are inconsistent.");
319 for (
int i=0; i<k; i++) {
322 std::vector<int> activeind(n-i);
323 for (
int j=0; j<n-i; j++) activeind[j] = j+i;
324 Teuchos::RCP<MV> actV = MVT::CloneViewNonConst(V,activeind);
330 Teuchos::SerialDenseMatrix<int,ScalarType> v(Teuchos::Copy,H,n-i,1,i,i);
338 MVT::MvTimesMatAddMv(-tau[i],*actV,v,ZERO,*workMV);
342 Teuchos::SerialDenseMatrix<int,ScalarType> vT(v,Teuchos::CONJ_TRANS);
343 MVT::MvTimesMatAddMv(ONE,*workMV,vT,ONE,*actV);
345 actV = Teuchos::null;
356 template<
class ScalarType,
class MV,
class OP>
359 const Teuchos::SerialDenseMatrix<int,ScalarType> &KK,
360 Teuchos::RCP<
const Teuchos::SerialDenseMatrix<int,ScalarType> > MM,
361 Teuchos::SerialDenseMatrix<int,ScalarType> &EV,
362 std::vector<
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &theta,
363 int &nev,
int esType)
407 Teuchos::LAPACK<int,ScalarType> lapack;
408 Teuchos::BLAS<int,ScalarType> blas;
413 if (size < nev || size < 0) {
416 if (KK.numCols() < size || KK.numRows() < size) {
419 if ((esType == 0 || esType == 1)) {
420 if (MM == Teuchos::null) {
423 else if (MM->numCols() < size || MM->numRows() < size) {
427 if (EV.numCols() < size || EV.numRows() < size) {
430 if (theta.size() < (
unsigned int) size) {
438 std::string lapack_name =
"hetrd";
439 std::string lapack_opts =
"u";
440 int NB = lapack.ILAENV(1, lapack_name, lapack_opts, size, -1, -1, -1);
441 int lwork = size*(NB+2);
442 std::vector<ScalarType> work(lwork);
443 std::vector<MagnitudeType> rwork(3*size-2);
446 std::vector<MagnitudeType> tt( size );
447 typedef typename std::vector<MagnitudeType>::iterator MTIter;
449 MagnitudeType tol = SCT::magnitude(SCT::squareroot(SCT::eps()));
451 ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
452 ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
454 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > KKcopy, MMcopy;
455 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > U;
463 for (rank = size; rank > 0; --rank) {
465 U = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(rank,rank) );
469 KKcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, KK, rank, rank ) );
470 MMcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, *MM, rank, rank ) );
475 lapack.HEGV(1,
'V',
'U', rank, KKcopy->values(), KKcopy->stride(),
476 MMcopy->values(), MMcopy->stride(), &tt[0], &work[0], lwork,
482 std::cerr << std::endl;
483 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, argument " << -info <<
"has an illegal value.\n";
484 std::cerr << std::endl;
495 MMcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, *MM, rank, rank ) );
496 for (
int i = 0; i < rank; ++i) {
497 for (
int j = 0; j < i; ++j) {
498 (*MMcopy)(i,j) = SCT::conjugate((*MM)(j,i));
502 TEUCHOS_TEST_FOR_EXCEPTION(
503 U->multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,one,*MMcopy,*KKcopy,zero) != 0,
504 std::logic_error,
"Anasazi::SolverUtils::directSolver() call to Teuchos::SerialDenseMatrix::multiply() returned an error.");
506 TEUCHOS_TEST_FOR_EXCEPTION(
507 MMcopy->multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,one,*KKcopy,*U,zero) != 0,
508 std::logic_error,
"Anasazi::SolverUtils::directSolver() call to Teuchos::SerialDenseMatrix::multiply() returned an error.");
509 MagnitudeType maxNorm = SCT::magnitude(zero);
510 MagnitudeType maxOrth = SCT::magnitude(zero);
511 for (
int i = 0; i < rank; ++i) {
512 for (
int j = i; j < rank; ++j) {
514 maxNorm = SCT::magnitude((*MMcopy)(i,j) - one) > maxNorm
515 ? SCT::magnitude((*MMcopy)(i,j) - one) : maxNorm;
517 maxOrth = SCT::magnitude((*MMcopy)(i,j)) > maxOrth
518 ? SCT::magnitude((*MMcopy)(i,j)) : maxOrth;
529 if ((maxNorm <= tol) && (maxOrth <= tol)) {
538 nev = (rank < nev) ? rank : nev;
539 EV.putScalar( zero );
540 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
541 for (
int i = 0; i < nev; ++i) {
542 blas.COPY( rank, (*KKcopy)[i], 1, EV[i], 1 );
552 KKcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, KK, size, size ) );
553 MMcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, *MM, size, size ) );
558 lapack.HEGV(1,
'V',
'U', size, KKcopy->values(), KKcopy->stride(),
559 MMcopy->values(), MMcopy->stride(), &tt[0], &work[0], lwork,
565 std::cerr << std::endl;
566 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, argument " << -info <<
"has an illegal value.\n";
567 std::cerr << std::endl;
574 std::cerr << std::endl;
575 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In HEGV, DPOTRF or DHEEV returned an error code (" << info <<
").\n";
576 std::cerr << std::endl;
583 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
584 for (
int i = 0; i < nev; ++i) {
585 blas.COPY( size, (*KKcopy)[i], 1, EV[i], 1 );
595 KKcopy = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( Teuchos::Copy, KK, size, size ) );
599 lapack.HEEV(
'V',
'U', size, KKcopy->values(), KKcopy->stride(), &tt[0], &work[0], lwork, &rwork[0], &info);
603 std::cerr << std::endl;
605 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In DHEEV, argument " << -info <<
" has an illegal value\n";
608 std::cerr <<
"Anasazi::SolverUtils::directSolver(): In DHEEV, the algorithm failed to converge (" << info <<
").\n";
610 std::cerr << std::endl;
617 std::copy(tt.begin(),tt.begin()+nev,theta.begin());
618 for (
int i = 0; i < nev; ++i) {
619 blas.COPY( size, (*KKcopy)[i], 1, EV[i], 1 );
634 template<
class ScalarType,
class MV,
class OP>
635 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
642 MagnitudeType maxDiff = SCT::magnitude(SCT::zero());
644 int xc = MVT::GetNumberVecs(X);
645 int mxc = MVT::GetNumberVecs(MX);
647 TEUCHOS_TEST_FOR_EXCEPTION(xc != mxc,std::invalid_argument,
"Anasazi::SolverUtils::errorEquality(): input multivecs have different number of columns.");
652 MagnitudeType maxCoeffX = SCT::magnitude(SCT::zero());
653 std::vector<MagnitudeType> tmp( xc );
654 MVT::MvNorm(MX, tmp);
656 for (
int i = 0; i < xc; ++i) {
657 maxCoeffX = (tmp[i] > maxCoeffX) ? tmp[i] : maxCoeffX;
660 std::vector<int> index( 1 );
661 Teuchos::RCP<MV> MtimesX;
662 if (M != Teuchos::null) {
663 MtimesX = MVT::Clone( X, xc );
664 OPT::Apply( *M, X, *MtimesX );
667 MtimesX = MVT::CloneCopy(X);
669 MVT::MvAddMv( -1.0, MX, 1.0, *MtimesX, *MtimesX );
670 MVT::MvNorm( *MtimesX, tmp );
672 for (
int i = 0; i < xc; ++i) {
673 maxDiff = (tmp[i] > maxDiff) ? tmp[i] : maxDiff;
676 return (maxCoeffX == 0.0) ? maxDiff : maxDiff/maxCoeffX;
682 #endif // ANASAZI_SOLVER_UTILS_HPP
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
Declaration of basic traits for the multivector type.
virtual ~SolverUtils()
Destructor.
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.
Abstract class definition for Anasazi Output Managers.
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...
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...
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...