34 #ifndef ANASAZI_ICSG_ORTHOMANAGER_HPP
35 #define ANASAZI_ICSG_ORTHOMANAGER_HPP
48 #include "Teuchos_TimeMonitor.hpp"
49 #include "Teuchos_LAPACK.hpp"
50 #include "Teuchos_BLAS.hpp"
51 #ifdef ANASAZI_ICGS_DEBUG
52 # include <Teuchos_FancyOStream.hpp>
57 template<
class ScalarType,
class MV,
class OP>
61 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
62 typedef Teuchos::ScalarTraits<ScalarType> SCT;
71 ICGSOrthoManager( Teuchos::RCP<const OP> Op = Teuchos::null,
int numIters = 2,
73 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType eps = 0.0,
74 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol = 0.20 );
168 Teuchos::Array<Teuchos::RCP<const MV> > X,
169 Teuchos::Array<Teuchos::RCP<const MV> > Y,
171 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
172 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
173 Teuchos::RCP<MV> MS = Teuchos::null,
174 Teuchos::Array<Teuchos::RCP<const MV> > MX = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null)),
175 Teuchos::Array<Teuchos::RCP<const MV> > MY = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
272 Teuchos::Array<Teuchos::RCP<const MV> > X,
273 Teuchos::Array<Teuchos::RCP<const MV> > Y,
275 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
276 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
277 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null,
278 Teuchos::RCP<MV> MS = Teuchos::null,
279 Teuchos::Array<Teuchos::RCP<const MV> > MX = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null)),
280 Teuchos::Array<Teuchos::RCP<const MV> > MY = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
304 Teuchos::Array<Teuchos::RCP<const MV> > Q,
305 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
306 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
307 Teuchos::RCP<MV> MX = Teuchos::null,
308 Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
322 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null,
323 Teuchos::RCP<MV> MX = Teuchos::null
337 Teuchos::Array<Teuchos::RCP<const MV> > Q,
338 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
339 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
340 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null,
341 Teuchos::RCP<MV> MX = Teuchos::null,
342 Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
354 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
361 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
362 orthogErrorMat(
const MV &X1,
const MV &X2, Teuchos::RCP<const MV> MX1, Teuchos::RCP<const MV> MX2)
const;
371 numIters_ = numIters;
372 TEUCHOS_TEST_FOR_EXCEPTION(numIters_ < 1,std::invalid_argument,
373 "Anasazi::ICGSOrthoManager::setNumIters(): input must be >= 1.");
389 int findBasis(MV &X, Teuchos::RCP<MV> MX,
390 Teuchos::SerialDenseMatrix<int,ScalarType> &B,
391 bool completeBasis,
int howMany = -1)
const;
398 template<
class ScalarType,
class MV,
class OP>
401 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType eps,
402 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tol) :
406 TEUCHOS_TEST_FOR_EXCEPTION(eps_ < SCT::magnitude(SCT::zero()),std::invalid_argument,
407 "Anasazi::ICGSOrthoManager::ICGSOrthoManager(): argument \"eps\" must be non-negative.");
409 Teuchos::LAPACK<int,MagnitudeType> lapack;
410 eps_ = lapack.LAMCH(
'E');
411 eps_ = Teuchos::ScalarTraits<MagnitudeType>::pow(eps_,.50);
413 TEUCHOS_TEST_FOR_EXCEPTION(
414 tol_ < SCT::magnitude(SCT::zero()) || tol_ > SCT::magnitude(SCT::one()),
415 std::invalid_argument,
416 "Anasazi::ICGSOrthoManager::ICGSOrthoManager(): argument \"tol\" must be in [0,1].");
423 template<
class ScalarType,
class MV,
class OP>
424 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
426 const ScalarType ONE = SCT::one();
427 int rank = MVT::GetNumberVecs(X);
428 Teuchos::SerialDenseMatrix<int,ScalarType> xTx(rank,rank);
430 for (
int i=0; i<rank; i++) {
433 return xTx.normFrobenius();
440 template<
class ScalarType,
class MV,
class OP>
441 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
443 int r1 = MVT::GetNumberVecs(X1);
444 int r2 = MVT::GetNumberVecs(X2);
445 Teuchos::SerialDenseMatrix<int,ScalarType> xTx(r1,r2);
447 return xTx.normFrobenius();
453 template<
class ScalarType,
class MV,
class OP>
456 Teuchos::Array<Teuchos::RCP<const MV> > Q,
457 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
459 Teuchos::Array<Teuchos::RCP<const MV> > MQ
462 projectGen(X,Q,Q,
true,C,MX,MQ,MQ);
469 template<
class ScalarType,
class MV,
class OP>
472 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
473 Teuchos::RCP<MV> MX)
const
478 int xc = MVT::GetNumberVecs(X);
479 ptrdiff_t xr = MVText::GetGlobalLength(X);
484 if (MX == Teuchos::null) {
486 MX = MVT::Clone(X,xc);
487 OPT::Apply(*(this->_Op),X,*MX);
488 this->_OpCounter += MVT::GetNumberVecs(X);
494 if ( B == Teuchos::null ) {
495 B = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(xc,xc) );
498 int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
499 ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
502 TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
503 "Anasazi::ICGSOrthoManager::normalizeMat(): X must be non-empty" );
504 TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
505 "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not consistent with size of B" );
506 TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
507 "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not consistent with size of MX" );
508 TEUCHOS_TEST_FOR_EXCEPTION( static_cast<ptrdiff_t>(xc) > xr, std::invalid_argument,
509 "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not feasible for normalization" );
511 return findBasis(X, MX, *B,
true );
518 template<
class ScalarType,
class MV,
class OP>
521 Teuchos::Array<Teuchos::RCP<const MV> > Q,
522 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
523 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
525 Teuchos::Array<Teuchos::RCP<const MV> > MQ
528 return projectAndNormalizeGen(X,Q,Q,
true,C,B,MX,MQ,MQ);
534 template<
class ScalarType,
class MV,
class OP>
537 Teuchos::Array<Teuchos::RCP<const MV> > X,
538 Teuchos::Array<Teuchos::RCP<const MV> > Y,
540 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
542 Teuchos::Array<Teuchos::RCP<const MV> > MX,
543 Teuchos::Array<Teuchos::RCP<const MV> > MY
561 #ifdef ANASAZI_ICGS_DEBUG
563 Teuchos::RCP<Teuchos::FancyOStream>
564 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
565 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
566 *out <<
"Entering Anasazi::ICGSOrthoManager::projectGen(...)\n";
569 const ScalarType ONE = SCT::one();
570 const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
571 Teuchos::LAPACK<int,ScalarType> lapack;
572 Teuchos::BLAS<int,ScalarType> blas;
574 int sc = MVT::GetNumberVecs( S );
575 ptrdiff_t sr = MVText::GetGlobalLength( S );
576 int numxy = X.length();
577 TEUCHOS_TEST_FOR_EXCEPTION(X.length() != Y.length(),std::invalid_argument,
578 "Anasazi::ICGSOrthoManager::projectGen(): X and Y must contain the same number of multivectors.");
579 std::vector<int> xyc(numxy);
581 if (numxy == 0 || sc == 0 || sr == 0) {
582 #ifdef ANASAZI_ICGS_DEBUG
583 *out <<
"Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
597 TEUCHOS_TEST_FOR_EXCEPTION( sc<0 || sr<0, std::invalid_argument,
598 "Anasazi::ICGSOrthoManager::projectGen(): MVT returned negative dimensions for S." );
601 if (this->_hasOp ==
true) {
602 if (MS != Teuchos::null) {
603 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MS) != sr, std::invalid_argument,
604 "Anasazi::ICGSOrthoManager::projectGen(): MS length not consistent with S." );
605 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*MS) != sc, std::invalid_argument,
606 "Anasazi::ICGSOrthoManager::projectGen(): MS width not consistent with S." );
611 ptrdiff_t sumxyc = 0;
614 for (
int i=0; i<numxy; i++) {
615 if (X[i] != Teuchos::null && Y[i] != Teuchos::null) {
616 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*X[i]) != sr, std::invalid_argument,
617 "Anasazi::ICGSOrthoManager::projectGen(): X[" << i <<
"] length not consistent with S." );
618 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*Y[i]) != sr, std::invalid_argument,
619 "Anasazi::ICGSOrthoManager::projectGen(): Y[" << i <<
"] length not consistent with S." );
620 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*X[i]) != MVT::GetNumberVecs(*Y[i]), std::invalid_argument,
621 "Anasazi::ICGSOrthoManager::projectGen(): X[" << i <<
"] and Y[" << i <<
"] widths not consistent." );
623 xyc[i] = MVT::GetNumberVecs( *X[i] );
624 TEUCHOS_TEST_FOR_EXCEPTION( sr < static_cast<ptrdiff_t>(xyc[i]), std::invalid_argument,
625 "Anasazi::ICGSOrthoManager::projectGen(): X[" << i <<
"],Y[" << i <<
"] have less rows than columns, and therefore cannot be full rank." );
629 if ( C[i] == Teuchos::null ) {
630 C[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(xyc[i],sc) );
633 TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != xyc[i] || C[i]->numCols() != sc , std::invalid_argument,
634 "Anasazi::ICGSOrthoManager::projectGen(): Size of Q not consistent with size of C." );
638 if (MX[i] != Teuchos::null) {
639 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MX[i]) != sr || MVT::GetNumberVecs(*MX[i]) != xyc[i], std::invalid_argument,
640 "Anasazi::ICGSOrthoManager::projectGen(): Size of MX[" << i <<
"] not consistent with size of X[" << i <<
"]." );
645 if (MY[i] != Teuchos::null) {
646 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MY[i]) != sr || MVT::GetNumberVecs(*MY[i]) != xyc[i], std::invalid_argument,
647 "Anasazi::ICGSOrthoManager::projectGen(): Size of MY[" << i <<
"] not consistent with size of Y[" << i <<
"]." );
655 TEUCHOS_TEST_FOR_EXCEPTION(X[i] != Teuchos::null || Y[i] != Teuchos::null, std::invalid_argument,
656 "Anasazi::ICGSOrthoManager::projectGen(): "
657 << (X[i] == Teuchos::null ?
"Y[" :
"X[") << i <<
"] was provided but "
658 << (X[i] == Teuchos::null ?
"X[" :
"Y[") << i <<
"] was not.");
663 TEUCHOS_TEST_FOR_EXCEPTION(sumxyc > sr, std::invalid_argument,
664 "Anasazi::ICGSOrthoManager::projectGen(): dimension of all X[i],Y[i] is "
665 << sumxyc <<
", but length of vectors is only " << sr <<
". This is infeasible.");
700 if (MXmissing == 0) {
703 if (MYmissing == 0) {
710 switch (whichAlloc) {
726 if (MS == Teuchos::null) {
727 #ifdef ANASAZI_ICGS_DEBUG
728 *out <<
"Allocating MS...\n";
730 MS = MVT::Clone(S,MVT::GetNumberVecs(S));
731 OPT::Apply(*(this->_Op),S,*MS);
732 this->_OpCounter += MVT::GetNumberVecs(S);
736 if (whichAlloc == 0) {
738 for (
int i=0; i<numxy; i++) {
739 if (MX[i] == Teuchos::null) {
740 #ifdef ANASAZI_ICGS_DEBUG
741 *out <<
"Allocating MX[" << i <<
"]...\n";
743 Teuchos::RCP<MV> tmpMX = MVT::Clone(*X[i],xyc[i]);
744 OPT::Apply(*(this->_Op),*X[i],*tmpMX);
746 this->_OpCounter += xyc[i];
753 MS = Teuchos::rcpFromRef(S);
756 TEUCHOS_TEST_FOR_EXCEPTION(updateMS == -1,std::logic_error,
757 "Anasazi::ICGSOrthoManager::projectGen(): Error in updateMS logic.");
766 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > YMX(numxy);
767 if (isBiortho ==
false) {
768 for (
int i=0; i<numxy; i++) {
769 #ifdef ANASAZI_ICGS_DEBUG
770 *out <<
"Computing YMX[" << i <<
"] and its Cholesky factorization...\n";
772 YMX[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(xyc[i],xyc[i]) );
774 #ifdef ANASAZI_ICGS_DEBUG
781 MagnitudeType err = ZERO;
782 for (
int jj=0; jj<YMX[i]->numCols(); jj++) {
783 err =+ SCT::magnitude(SCT::imag((*YMX[i])(jj,jj)));
784 for (
int ii=jj; ii<YMX[i]->numRows(); ii++) {
785 err += SCT::magnitude( (*YMX[i])(ii,jj) - SCT::conjugate((*YMX[i])(jj,ii)) );
788 *out <<
"Symmetry error in YMX[" << i <<
"] == " << err <<
"\n";
793 lapack.POTRF(
'U',YMX[i]->numRows(),YMX[i]->values(),YMX[i]->stride(),&info);
794 TEUCHOS_TEST_FOR_EXCEPTION(info != 0,std::logic_error,
795 "Anasazi::ICGSOrthoManager::projectGen(): Error computing Cholesky factorization of Y[i]^T * M * X[i] using POTRF: returned info " << info);
800 #ifdef ANASAZI_ICGS_DEBUG
801 std::vector<MagnitudeType> oldNorms(sc);
803 *out <<
"oldNorms = { ";
804 std::copy(oldNorms.begin(), oldNorms.end(), std::ostream_iterator<MagnitudeType>(*out,
" "));
810 Teuchos::Array<Teuchos::SerialDenseMatrix<int,ScalarType> > Ccur(numxy);
811 for (
int i=0; i<numxy; i++) {
812 C[i]->putScalar(ZERO);
813 Ccur[i].reshape(C[i]->numRows(),C[i]->numCols());
816 for (
int iter=0; iter < numIters_; iter++) {
817 #ifdef ANASAZI_ICGS_DEBUG
818 *out <<
"beginning iteration " << iter+1 <<
"\n";
822 for (
int i=0; i<numxy; i++) {
825 if (isBiortho ==
false) {
828 lapack.POTRS(
'U',YMX[i]->numCols(),Ccur[i].numCols(),
829 YMX[i]->values(),YMX[i]->stride(),
830 Ccur[i].values(),Ccur[i].stride(), &info);
831 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
832 "Anasazi::ICGSOrthoManager::projectGen(): Error code " << info <<
" from lapack::POTRS." );
836 #ifdef ANASAZI_ICGS_DEBUG
837 *out <<
"Applying projector P_{X[" << i <<
"],Y[" << i <<
"]}...\n";
839 MVT::MvTimesMatAddMv( -ONE, *X[i], Ccur[i], ONE, S );
846 #ifdef ANASAZI_ICGS_DEBUG
847 *out <<
"Updating MS...\n";
849 OPT::Apply( *(this->_Op), S, *MS);
850 this->_OpCounter += sc;
852 else if (updateMS == 2) {
853 #ifdef ANASAZI_ICGS_DEBUG
854 *out <<
"Updating MS...\n";
856 MVT::MvTimesMatAddMv( -ONE, *MX[i], Ccur[i], ONE, *MS );
861 #ifdef ANASAZI_ICGS_DEBUG
862 std::vector<MagnitudeType> newNorms(sc);
864 *out <<
"newNorms = { ";
865 std::copy(newNorms.begin(), newNorms.end(), std::ostream_iterator<MagnitudeType>(*out,
" "));
870 #ifdef ANASAZI_ICGS_DEBUG
871 *out <<
"Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
879 template<
class ScalarType,
class MV,
class OP>
882 Teuchos::Array<Teuchos::RCP<const MV> > X,
883 Teuchos::Array<Teuchos::RCP<const MV> > Y,
885 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
886 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
888 Teuchos::Array<Teuchos::RCP<const MV> > MX,
889 Teuchos::Array<Teuchos::RCP<const MV> > MY
907 #ifdef ANASAZI_ICGS_DEBUG
909 Teuchos::RCP<Teuchos::FancyOStream>
910 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
911 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
912 *out <<
"Entering Anasazi::ICGSOrthoManager::projectAndNormalizeGen(...)\n";
916 int sc = MVT::GetNumberVecs( S );
917 ptrdiff_t sr = MVText::GetGlobalLength( S );
918 int numxy = X.length();
919 TEUCHOS_TEST_FOR_EXCEPTION(X.length() != Y.length(),std::invalid_argument,
920 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X and Y must contain the same number of multivectors.");
921 std::vector<int> xyc(numxy);
923 if (sc == 0 || sr == 0) {
924 #ifdef ANASAZI_ICGS_DEBUG
925 *out <<
"Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
939 TEUCHOS_TEST_FOR_EXCEPTION( sc<0 || sr<0, std::invalid_argument,
940 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MVT returned negative dimensions for S." );
943 if (this->_hasOp ==
true) {
944 if (MS != Teuchos::null) {
945 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MS) != sr, std::invalid_argument,
946 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MS length not consistent with S." );
947 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*MS) != sc, std::invalid_argument,
948 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MS width not consistent with S." );
953 ptrdiff_t sumxyc = 0;
956 for (
int i=0; i<numxy; i++) {
957 if (X[i] != Teuchos::null && Y[i] != Teuchos::null) {
958 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*X[i]) != sr, std::invalid_argument,
959 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i <<
"] length not consistent with S." );
960 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*Y[i]) != sr, std::invalid_argument,
961 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Y[" << i <<
"] length not consistent with S." );
962 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*X[i]) != MVT::GetNumberVecs(*Y[i]), std::invalid_argument,
963 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i <<
"] and Y[" << i <<
"] widths not consistent." );
965 xyc[i] = MVT::GetNumberVecs( *X[i] );
966 TEUCHOS_TEST_FOR_EXCEPTION( sr < static_cast<ptrdiff_t>(xyc[i]), std::invalid_argument,
967 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i <<
"],Y[" << i <<
"] have less rows than columns, and therefore cannot be full rank." );
971 if ( C[i] == Teuchos::null ) {
972 C[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(xyc[i],sc) );
975 TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != xyc[i] || C[i]->numCols() != sc , std::invalid_argument,
976 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of Q not consistent with size of C." );
980 if (MX[i] != Teuchos::null) {
981 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MX[i]) != sr || MVT::GetNumberVecs(*MX[i]) != xyc[i], std::invalid_argument,
982 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of MX[" << i <<
"] not consistent with size of X[" << i <<
"]." );
987 if (MY[i] != Teuchos::null) {
988 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MY[i]) != sr || MVT::GetNumberVecs(*MY[i]) != xyc[i], std::invalid_argument,
989 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of MY[" << i <<
"] not consistent with size of Y[" << i <<
"]." );
997 TEUCHOS_TEST_FOR_EXCEPTION(X[i] != Teuchos::null || Y[i] != Teuchos::null, std::invalid_argument,
998 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): "
999 << (X[i] == Teuchos::null ?
"Y[" :
"X[") << i <<
"] was provided but "
1000 << (X[i] == Teuchos::null ?
"X[" :
"Y[") << i <<
"] was not.");
1005 TEUCHOS_TEST_FOR_EXCEPTION(sumxyc + sc > sr, std::invalid_argument,
1006 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): dimension of all X[i],Y[i] is "
1007 << sumxyc <<
" and requested " << sc <<
"-dimensional basis, but length of vectors is only "
1008 << sr <<
". This is infeasible.");
1043 if (MXmissing == 0) {
1046 if (MYmissing == 0) {
1053 switch (whichAlloc) {
1069 if (MS == Teuchos::null) {
1070 #ifdef ANASAZI_ICGS_DEBUG
1071 *out <<
"Allocating MS...\n";
1073 MS = MVT::Clone(S,MVT::GetNumberVecs(S));
1074 OPT::Apply(*(this->_Op),S,*MS);
1075 this->_OpCounter += MVT::GetNumberVecs(S);
1079 if (whichAlloc == 0) {
1081 for (
int i=0; i<numxy; i++) {
1082 if (MX[i] == Teuchos::null) {
1083 #ifdef ANASAZI_ICGS_DEBUG
1084 *out <<
"Allocating MX[" << i <<
"]...\n";
1086 Teuchos::RCP<MV> tmpMX = MVT::Clone(*X[i],xyc[i]);
1087 OPT::Apply(*(this->_Op),*X[i],*tmpMX);
1089 this->_OpCounter += xyc[i];
1096 MS = Teuchos::rcpFromRef(S);
1099 TEUCHOS_TEST_FOR_EXCEPTION(updateMS == -1,std::logic_error,
1100 "Anasazi::ICGSOrthoManager::projectGen(): Error in updateMS logic.");
1105 if ( B == Teuchos::null ) {
1106 B = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(sc,sc) );
1110 TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != sc || B->numCols() != sc, std::invalid_argument,
1111 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of S must be consistent with size of B" );
1116 projectGen(S,X,Y,isBiortho,C,MS,MX,MY);
1118 Teuchos::SerialDenseMatrix<int,ScalarType> oldCoeff(sc,1);
1124 int curssize = sc - rank;
1129 #ifdef ANASAZI_ICGS_DEBUG
1130 *out <<
"Attempting to find orthonormal basis for X...\n";
1132 rank = findBasis(S,MS,*B,
false,curssize);
1134 if (oldrank != -1 && rank != oldrank) {
1140 for (
int i=0; i<sc; i++) {
1141 (*B)(i,oldrank) = oldCoeff(i,0);
1146 if (rank != oldrank) {
1154 for (
int i=0; i<sc; i++) {
1155 oldCoeff(i,0) = (*B)(i,rank);
1162 #ifdef ANASAZI_ICGS_DEBUG
1163 *out <<
"Finished computing basis.\n";
1168 TEUCHOS_TEST_FOR_EXCEPTION( rank < oldrank,
OrthoError,
1169 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): basis lost rank; this shouldn't happen");
1171 if (rank != oldrank) {
1179 if (numTries <= 0) {
1187 #ifdef ANASAZI_ICGS_DEBUG
1188 *out <<
"Inserting random vector in X[" << rank <<
"]. Attempt " << 10-numTries <<
".\n";
1190 Teuchos::RCP<MV> curS, curMS;
1191 std::vector<int> ind(1);
1193 curS = MVT::CloneViewNonConst(S,ind);
1194 MVT::MvRandom(*curS);
1196 #ifdef ANASAZI_ICGS_DEBUG
1197 *out <<
"Applying operator to random vector.\n";
1199 curMS = MVT::CloneViewNonConst(*MS,ind);
1200 OPT::Apply( *(this->_Op), *curS, *curMS );
1201 this->_OpCounter += MVT::GetNumberVecs(*curS);
1209 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > dummyC(0);
1210 projectGen(*curS,X,Y,isBiortho,dummyC,curMS,MX,MY);
1216 TEUCHOS_TEST_FOR_EXCEPTION( rank > sc || rank < 0, std::logic_error,
1217 "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Debug error in rank variable." );
1219 #ifdef ANASAZI_ICGS_DEBUG
1220 *out <<
"Returning " << rank <<
" from Anasazi::ICGSOrthoManager::projectAndNormalizeGen(...)\n";
1231 template<
class ScalarType,
class MV,
class OP>
1233 MV &X, Teuchos::RCP<MV> MX,
1234 Teuchos::SerialDenseMatrix<int,ScalarType> &B,
1235 bool completeBasis,
int howMany )
const {
1252 #ifdef ANASAZI_ICGS_DEBUG
1254 Teuchos::RCP<Teuchos::FancyOStream>
1255 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
1256 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
1257 *out <<
"Entering Anasazi::ICGSOrthoManager::findBasis(...)\n";
1260 const ScalarType ONE = SCT::one();
1261 const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
1263 int xc = MVT::GetNumberVecs( X );
1265 if (howMany == -1) {
1272 TEUCHOS_TEST_FOR_EXCEPTION(this->_hasOp ==
true && MX == Teuchos::null, std::logic_error,
1273 "Anasazi::ICGSOrthoManager::findBasis(): calling routine did not specify MS.");
1274 TEUCHOS_TEST_FOR_EXCEPTION( howMany < 0 || howMany > xc, std::logic_error,
1275 "Anasazi::ICGSOrthoManager::findBasis(): Invalid howMany parameter" );
1280 int xstart = xc - howMany;
1282 for (
int j = xstart; j < xc; j++) {
1291 for (
int i=j+1; i<xc; ++i) {
1296 std::vector<int> index(1);
1298 Teuchos::RCP<MV> Xj = MVT::CloneViewNonConst( X, index );
1299 Teuchos::RCP<MV> MXj;
1300 if ((this->_hasOp)) {
1302 MXj = MVT::CloneViewNonConst( *MX, index );
1310 std::vector<int> prev_idx( numX );
1311 Teuchos::RCP<const MV> prevX, prevMX;
1314 for (
int i=0; i<numX; ++i) prev_idx[i] = i;
1315 prevX = MVT::CloneView( X, prev_idx );
1317 prevMX = MVT::CloneView( *MX, prev_idx );
1321 bool rankDef =
true;
1326 for (
int numTrials = 0; numTrials < 10; numTrials++) {
1327 #ifdef ANASAZI_ICGS_DEBUG
1328 *out <<
"Trial " << numTrials <<
" for vector " << j <<
"\n";
1332 Teuchos::SerialDenseMatrix<int,ScalarType> product(numX, 1);
1333 std::vector<MagnitudeType> origNorm(1), newNorm(1), newNorm2(1);
1338 Teuchos::RCP<MV> oldMXj = MVT::CloneCopy( *MXj );
1340 #ifdef ANASAZI_ICGS_DEBUG
1341 *out <<
"origNorm = " << origNorm[0] <<
"\n";
1352 #ifdef ANASAZI_ICGS_DEBUG
1353 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
1355 MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
1362 #ifdef ANASAZI_ICGS_DEBUG
1363 *out <<
"Updating MX[" << j <<
"]...\n";
1365 MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
1370 MagnitudeType product_norm = product.normOne();
1372 #ifdef ANASAZI_ICGS_DEBUG
1373 *out <<
"newNorm = " << newNorm[0] <<
"\n";
1374 *out <<
"prodoct_norm = " << product_norm <<
"\n";
1378 if ( product_norm/newNorm[0] >= tol_ || newNorm[0] < eps_*origNorm[0]) {
1379 #ifdef ANASAZI_ICGS_DEBUG
1380 if (product_norm/newNorm[0] >= tol_) {
1381 *out <<
"product_norm/newNorm == " << product_norm/newNorm[0] <<
"... another step of Gram-Schmidt.\n";
1384 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... another step of Gram-Schmidt.\n";
1389 Teuchos::SerialDenseMatrix<int,ScalarType> P2(numX,1);
1392 #ifdef ANASAZI_ICGS_DEBUG
1393 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
1395 MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
1396 if ((this->_hasOp)) {
1397 #ifdef ANASAZI_ICGS_DEBUG
1398 *out <<
"Updating MX[" << j <<
"]...\n";
1400 MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
1404 product_norm = P2.normOne();
1405 #ifdef ANASAZI_ICGS_DEBUG
1406 *out <<
"newNorm2 = " << newNorm2[0] <<
"\n";
1407 *out <<
"product_norm = " << product_norm <<
"\n";
1409 if ( product_norm/newNorm2[0] >= tol_ || newNorm2[0] < eps_*origNorm[0] ) {
1411 #ifdef ANASAZI_ICGS_DEBUG
1412 if (product_norm/newNorm2[0] >= tol_) {
1413 *out <<
"product_norm/newNorm2 == " << product_norm/newNorm2[0] <<
"... setting vector to zero.\n";
1415 else if (newNorm[0] < newNorm2[0]) {
1416 *out <<
"newNorm2 > newNorm... setting vector to zero.\n";
1419 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... setting vector to zero.\n";
1422 MVT::MvInit(*Xj,ZERO);
1423 if ((this->_hasOp)) {
1424 #ifdef ANASAZI_ICGS_DEBUG
1425 *out <<
"Setting MX[" << j <<
"] to zero as well.\n";
1427 MVT::MvInit(*MXj,ZERO);
1434 if (numTrials == 0) {
1435 for (
int i=0; i<numX; i++) {
1436 B(i,j) = product(i,0);
1442 if ( newNorm[0] != ZERO && newNorm[0] > SCT::sfmin() ) {
1443 #ifdef ANASAZI_ICGS_DEBUG
1444 *out <<
"Normalizing X[" << j <<
"], norm(X[" << j <<
"]) = " << newNorm[0] <<
"\n";
1448 MVT::MvScale( *Xj, ONE/newNorm[0]);
1450 #ifdef ANASAZI_ICGS_DEBUG
1451 *out <<
"Normalizing M*X[" << j <<
"]...\n";
1454 MVT::MvScale( *MXj, ONE/newNorm[0]);
1458 if (numTrials == 0) {
1459 B(j,j) = newNorm[0];
1467 #ifdef ANASAZI_ICGS_DEBUG
1468 *out <<
"Not normalizing M*X[" << j <<
"]...\n";
1475 if (completeBasis) {
1477 #ifdef ANASAZI_ICGS_DEBUG
1478 *out <<
"Inserting random vector in X[" << j <<
"]...\n";
1480 MVT::MvRandom( *Xj );
1482 #ifdef ANASAZI_ICGS_DEBUG
1483 *out <<
"Updating M*X[" << j <<
"]...\n";
1485 OPT::Apply( *(this->_Op), *Xj, *MXj );
1486 this->_OpCounter += MVT::GetNumberVecs(*Xj);
1497 if (rankDef ==
true) {
1498 TEUCHOS_TEST_FOR_EXCEPTION( rankDef && completeBasis, OrthoError,
1499 "Anasazi::ICGSOrthoManager::findBasis(): Unable to complete basis" );
1500 #ifdef ANASAZI_ICGS_DEBUG
1501 *out <<
"Returning early, rank " << j <<
" from Anasazi::ICGSOrthoManager::findBasis(...)\n";
1508 #ifdef ANASAZI_ICGS_DEBUG
1509 *out <<
"Returning " << xc <<
" from Anasazi::ICGSOrthoManager::findBasis(...)\n";
1516 #endif // ANASAZI_ICSG_ORTHOMANAGER_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.
~ICGSOrthoManager()
Destructor.
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogErrorMat(const MV &X1, const MV &X2, Teuchos::RCP< const MV > MX1, Teuchos::RCP< const MV > MX2) const
This method computes the error in orthogonality of two multivectors, measured as the Frobenius norm o...
Virtual base class which defines basic traits for the operator type.
void projectGen(MV &S, Teuchos::Array< Teuchos::RCP< const MV > > X, Teuchos::Array< Teuchos::RCP< const MV > > Y, bool isBiOrtho, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< MV > MS=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MX=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null)), Teuchos::Array< Teuchos::RCP< const MV > > MY=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Applies a series of generic projectors.
An implementation of the Anasazi::GenOrthoManager that performs orthogonalization using iterated clas...
ICGSOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null, int numIters=2, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType eps=0.0, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType tol=0.20)
Constructor specifying the operator defining the inner product as well as the number of orthogonaliza...
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
void innerProdMat(const MV &X, const MV &Y, Teuchos::SerialDenseMatrix< int, ScalarType > &Z, Teuchos::RCP< const MV > MX=Teuchos::null, Teuchos::RCP< const MV > MY=Teuchos::null) const
Provides a matrix-based inner product.
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormErrorMat(const MV &X, Teuchos::RCP< const MV > MX=Teuchos::null) const
This method computes the error in orthonormality of a multivector, measured as the Frobenius norm of ...
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...
Exception thrown to signal error in an orthogonalization manager method.
int projectAndNormalizeMat(MV &X, Teuchos::Array< Teuchos::RCP< const MV > > Q, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MX=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MQ=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Given a set of bases Q[i] and a multivector X, this method computes an orthonormal basis for ...
int projectAndNormalizeGen(MV &S, Teuchos::Array< Teuchos::RCP< const MV > > X, Teuchos::Array< Teuchos::RCP< const MV > > Y, bool isBiOrtho, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MS=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MX=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null)), Teuchos::Array< Teuchos::RCP< const MV > > MY=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Applies a series of generic projectors and returns an orthonormal basis for the residual data...
int normalizeMat(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MX=Teuchos::null) const
This method takes a multivector X and attempts to compute an orthonormal basis for ...
int getNumIters() const
Return parameter for re-orthogonalization threshold.
void normMat(const MV &X, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, Teuchos::RCP< const MV > MX=Teuchos::null) const
Provides the norm induced by the matrix-based inner product.
void projectMat(MV &X, Teuchos::Array< Teuchos::RCP< const MV > > Q, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< MV > MX=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MQ=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Given a list of mutually orthogonal and internally orthonormal bases Q, this method projects a multiv...
void setNumIters(int numIters)
Set parameter for re-orthogonalization threshold.