14#ifndef TRACEMIN_RITZ_OP_HPP
15#define TRACEMIN_RITZ_OP_HPP
22#ifdef HAVE_ANASAZI_BELOS
23 #include "BelosMultiVecTraits.hpp"
24 #include "BelosLinearProblem.hpp"
25 #include "BelosPseudoBlockGmresSolMgr.hpp"
26 #include "BelosOperator.hpp"
27 #ifdef HAVE_ANASAZI_TPETRA
28 #include "BelosTpetraAdapter.hpp"
32#include "Teuchos_RCP.hpp"
33#include "Teuchos_SerialDenseSolver.hpp"
34#include "Teuchos_ScalarTraitsDecl.hpp"
49template <
class Scalar,
class MV,
class OP>
53 virtual ~TraceMinOp() { };
54 virtual void Apply(
const MV& X, MV& Y)
const =0;
55 virtual void removeIndices(
const std::vector<int>& indicesToRemove) =0;
66template <
class Scalar,
class MV,
class OP>
75 TraceMinProjOp(
const Teuchos::RCP<const MV> X,
const Teuchos::RCP<const OP> B, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs);
81 void Apply(
const MV& X, MV& Y)
const;
84 void removeIndices(
const std::vector<int>& indicesToRemove);
87 Teuchos::Array< Teuchos::RCP<const MV> > projVecs_;
88 Teuchos::RCP<Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman_;
89 Teuchos::RCP<const OP> B_;
91#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
92 RCP<Teuchos::Time> ProjTime_;
103template <
class Scalar,
class MV,
class OP>
104class TraceMinRitzOp :
public TraceMinOp<Scalar,MV,OP>
106 template <
class Scalar_,
class MV_,
class OP_>
friend class TraceMinProjRitzOp;
107 template <
class Scalar_,
class MV_,
class OP_>
friend class TraceMinProjRitzOpWithPrec;
108 template <
class Scalar_,
class MV_,
class OP_>
friend class TraceMinProjectedPrecOp;
117 TraceMinRitzOp(
const Teuchos::RCP<const OP>& A,
const Teuchos::RCP<const OP>& B = Teuchos::null,
const Teuchos::RCP<const OP>& Prec = Teuchos::null);
120 ~TraceMinRitzOp() { };
123 void setRitzShifts(std::vector<Scalar> shifts) {ritzShifts_ = shifts;};
125 Scalar getRitzShift(
const int subscript) {
return ritzShifts_[subscript]; };
127 Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > getPrec() {
return Prec_; };
130 void setInnerTol(
const std::vector<Scalar>& tolerances) { tolerances_ = tolerances; };
132 void getInnerTol(std::vector<Scalar>& tolerances)
const { tolerances = tolerances_; };
134 void setMaxIts(
const int maxits) { maxits_ = maxits; };
136 int getMaxIts()
const {
return maxits_; };
139 void Apply(
const MV& X, MV& Y)
const;
142 void ApplyInverse(
const MV& X, MV& Y);
144 void removeIndices(
const std::vector<int>& indicesToRemove);
147 Teuchos::RCP<const OP> A_, B_;
148 Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > Prec_;
151 std::vector<Scalar> ritzShifts_;
152 std::vector<Scalar> tolerances_;
154 Teuchos::RCP< PseudoBlockMinres< Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> > > solver_;
156#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
157 RCP<Teuchos::Time> PetraMultTime_, AopMultTime_;
169template <
class Scalar,
class MV,
class OP>
170class TraceMinProjRitzOp :
public TraceMinOp<Scalar,MV,OP>
177 TraceMinProjRitzOp(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs,
const Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman = Teuchos::null);
178 TraceMinProjRitzOp(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs,
const Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs);
181 void Apply(
const MV& X, MV& Y)
const;
185 void ApplyInverse(
const MV& X, MV& Y);
187 void removeIndices(
const std::vector<int>& indicesToRemove);
190 Teuchos::RCP< TraceMinRitzOp<Scalar,MV,OP> > Op_;
191 Teuchos::RCP< TraceMinProjOp<Scalar,MV,OP> > projector_;
193 Teuchos::RCP< PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> > > solver_;
205template <
class Scalar,
class MV,
class OP>
206class TraceMinProjectedPrecOp :
public TraceMinOp<Scalar,MV,OP>
215 TraceMinProjectedPrecOp(
const Teuchos::RCP<const OP> Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs);
217 ~TraceMinProjectedPrecOp();
219 void Apply(
const MV& X, MV& Y)
const;
221 void removeIndices(
const std::vector<int>& indicesToRemove);
224 Teuchos::RCP<const OP> Op_;
225 Teuchos::Array< Teuchos::RCP<const MV> > projVecs_;
227 Teuchos::RCP< Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman_;
228 Teuchos::RCP<const OP> B_;
239#ifdef HAVE_ANASAZI_BELOS
240template <
class Scalar,
class MV,
class OP>
241class TraceMinProjRitzOpWithPrec :
public TraceMinOp<Scalar,MV,OP>
249 TraceMinProjRitzOpWithPrec(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman = Teuchos::null);
250 TraceMinProjRitzOpWithPrec(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs);
252 void Apply(
const MV& X, MV& Y)
const;
256 void ApplyInverse(
const MV& X, MV& Y);
258 void removeIndices(
const std::vector<int>& indicesToRemove);
261 Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > Op_;
262 Teuchos::RCP<TraceMinProjectedPrecOp<Scalar,MV,OP> > Prec_;
264 Teuchos::RCP< Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> > > solver_;
265 Teuchos::RCP< Belos::LinearProblem<Scalar,MV,TraceMinOp<Scalar,MV,OP> > > problem_;
281template <
class Scalar,
class MV,
class OP>
282class OperatorTraits <Scalar, MV,
Experimental::TraceMinOp<Scalar,MV,OP> >
286 Apply (
const Experimental::TraceMinOp<Scalar,MV,OP>& Op,
288 MV& y) {Op.
Apply(x,y);};
292 HasApplyTranspose (
const Experimental::TraceMinOp<Scalar,MV,OP>& Op) {
return true;};
297#ifdef HAVE_ANASAZI_BELOS
300template <
class Scalar,
class MV,
class OP>
301class OperatorTraits <Scalar, MV,
Anasazi::Experimental::TraceMinOp<Scalar,MV,OP> >
305 Apply (
const Anasazi::Experimental::TraceMinOp<Scalar,MV,OP>& Op,
307 MV& y, Belos::ETrans trans = NOTRANS) {Op.Apply(x,y);};
311 HasApplyTranspose (
const Anasazi::Experimental::TraceMinOp<Scalar,MV,OP>& Op) {
return true;};
320template <
class Scalar,
class MV,
class OP>
321class OperatorTraits <Scalar, MV,
Experimental::TraceMinRitzOp<Scalar,MV,OP> >
325 Apply (
const Experimental::TraceMinRitzOp<Scalar,MV,OP>& Op,
327 MV& y) {Op.
Apply(x,y);};
331 HasApplyTranspose (
const Experimental::TraceMinRitzOp<Scalar,MV,OP>& Op) {
return true;};
339template <
class Scalar,
class MV,
class OP>
340class OperatorTraits <Scalar, MV,
Experimental::TraceMinProjRitzOp<Scalar,MV,OP> >
344 Apply (
const Experimental::TraceMinProjRitzOp<Scalar,MV,OP>& Op,
346 MV& y) {Op.
Apply(x,y);};
350 HasApplyTranspose (
const Experimental::TraceMinProjRitzOp<Scalar,MV,OP>& Op) {
return true;};
358template <
class Scalar,
class MV,
class OP>
359class OperatorTraits <Scalar, MV,
Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP> >
363 Apply (
const Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP>& Op,
365 MV& y) {Op.
Apply(x,y);};
369 HasApplyTranspose (
const Experimental::TraceMinProjectedPrecOp<Scalar,MV,OP>& Op) {
return true;};
382template <
class Scalar,
class MV,
class OP>
384ONE(Teuchos::ScalarTraits<Scalar>::one())
386#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
387 ProjTime_ = Teuchos::TimeMonitor::getNewTimer(
"Anasazi: TraceMinProjOp::Apply()");
392 if(orthman_ != Teuchos::null && B_ != Teuchos::null)
393 orthman_->setOp(Teuchos::null);
397 if(B_ != Teuchos::null)
399 int nvec = MVT::GetNumberVecs(*X);
401 if(orthman_ != Teuchos::null)
404 Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
405 orthman_->normalizeMat(*helperMV);
406 projVecs_.push_back(helperMV);
410 std::vector<Scalar> normvec(nvec);
411 MVT::MvNorm(*X,normvec);
412 for(
int i=0; i<nvec; i++)
413 normvec[i] = ONE/normvec[i];
414 Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
415 MVT::MvScale(*helperMV,normvec);
416 projVecs_.push_back(helperMV);
420 projVecs_.push_back(MVT::CloneCopy(*X));
424template <
class Scalar,
class MV,
class OP>
425TraceMinProjOp<Scalar,MV,OP>::TraceMinProjOp(
const Teuchos::RCP<const MV> X,
const Teuchos::RCP<const OP> B, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs) :
426ONE(Teuchos::ScalarTraits<Scalar>::one())
428#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
429 ProjTime_ = Teuchos::TimeMonitor::getNewTimer(
"Anasazi: TraceMinProjOp::Apply()");
434 if(B_ != Teuchos::null)
435 orthman_->setOp(Teuchos::null);
441 if(B_ != Teuchos::null)
444 Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*X);
445 orthman_->normalizeMat(*helperMV);
446 projVecs_.push_back(helperMV);
450 projVecs_.push_back(MVT::CloneCopy(*X));
455template <
class Scalar,
class MV,
class OP>
456TraceMinProjOp<Scalar,MV,OP>::~TraceMinProjOp()
458 if(orthman_ != Teuchos::null)
464template <
class Scalar,
class MV,
class OP>
465void TraceMinProjOp<Scalar,MV,OP>::Apply(
const MV& X, MV& Y)
const
467#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
468 Teuchos::TimeMonitor lcltimer( *ProjTime_ );
471 if(orthman_ != Teuchos::null)
474 orthman_->projectMat(Y,projVecs_);
478 int nvec = MVT::GetNumberVecs(X);
479 std::vector<Scalar> dotProducts(nvec);
480 MVT::MvDot(*projVecs_[0],X,dotProducts);
481 Teuchos::RCP<MV> helper = MVT::CloneCopy(*projVecs_[0]);
482 MVT::MvScale(*helper,dotProducts);
483 MVT::MvAddMv(ONE,X,-ONE,*helper,Y);
489template <
class Scalar,
class MV,
class OP>
490void TraceMinProjOp<Scalar,MV,OP>::removeIndices(
const std::vector<int>& indicesToRemove)
492 if (orthman_ == Teuchos::null) {
493 const int nprojvecs = projVecs_.size();
494 const int nvecs = MVT::GetNumberVecs(*projVecs_[nprojvecs-1]);
495 const int numRemoving = indicesToRemove.size();
496 std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);
498 for (
int i=0; i<nvecs; i++) {
502 std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());
504 Teuchos::RCP<MV> helperMV = MVT::CloneCopy(*projVecs_[nprojvecs-1],indicesToLeave);
505 projVecs_[nprojvecs-1] = helperMV;
515template <
class Scalar,
class MV,
class OP>
516TraceMinRitzOp<Scalar,MV,OP>::TraceMinRitzOp(
const Teuchos::RCP<const OP>& A,
const Teuchos::RCP<const OP>& B,
const Teuchos::RCP<const OP>& Prec) :
517ONE(Teuchos::ScalarTraits<Scalar>::one()),
518ZERO(Teuchos::ScalarTraits<Scalar>::zero())
525#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
526 PetraMultTime_ = Teuchos::TimeMonitor::getNewTimer(
"Anasazi: TraceMinRitzOp: *Petra::Apply()");
527 AopMultTime_ = Teuchos::TimeMonitor::getNewTimer(
"Anasazi: TraceMinRitzOp::Apply()");
531 Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(
this);
535 if(Prec != Teuchos::null)
536 Prec_ = Teuchos::rcp(
new TraceMinRitzOp<Scalar,MV,OP>(Prec) );
539 solver_ = Teuchos::rcp(
new PseudoBlockMinres< Scalar,MV,TraceMinRitzOp<Scalar,MV,OP> >(linSolOp,Prec_) );
544template <
class Scalar,
class MV,
class OP>
545void TraceMinRitzOp<Scalar,MV,OP>::Apply(
const MV& X, MV& Y)
const
547 int nvecs = MVT::GetNumberVecs(X);
549#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
550 Teuchos::TimeMonitor outertimer( *AopMultTime_ );
555#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
556 Teuchos::TimeMonitor lcltimer( *PetraMultTime_ );
563 if(ritzShifts_.size() > 0)
566 std::vector<int> nonzeroRitzIndices;
567 nonzeroRitzIndices.reserve(nvecs);
568 for(
int i=0; i<nvecs; i++)
570 if(ritzShifts_[i] != ZERO)
571 nonzeroRitzIndices.push_back(i);
575 int numRitzShifts = nonzeroRitzIndices.size();
576 if(numRitzShifts > 0)
579 Teuchos::RCP<const MV> ritzX = MVT::CloneView(X,nonzeroRitzIndices);
580 Teuchos::RCP<MV> ritzY = MVT::CloneViewNonConst(Y,nonzeroRitzIndices);
583 std::vector<Scalar> nonzeroRitzShifts(numRitzShifts);
584 for(
int i=0; i<numRitzShifts; i++)
585 nonzeroRitzShifts[i] = ritzShifts_[nonzeroRitzIndices[i]];
588 if(B_ != Teuchos::null)
590 Teuchos::RCP<MV> BX = MVT::Clone(*ritzX,numRitzShifts);
591 OPT::Apply(*B_,*ritzX,*BX);
592 MVT::MvScale(*BX,nonzeroRitzShifts);
593 MVT::MvAddMv(ONE,*ritzY,-ONE,*BX,*ritzY);
598 Teuchos::RCP<MV> scaledX = MVT::CloneCopy(*ritzX);
599 MVT::MvScale(*scaledX,nonzeroRitzShifts);
600 MVT::MvAddMv(ONE,*ritzY,-ONE,*scaledX,*ritzY);
608template <
class Scalar,
class MV,
class OP>
609void TraceMinRitzOp<Scalar,MV,OP>::ApplyInverse(
const MV& X, MV& Y)
611 int nvecs = MVT::GetNumberVecs(X);
612 std::vector<int> indices(nvecs);
613 for(
int i=0; i<nvecs; i++)
616 Teuchos::RCP<const MV> rcpX = MVT::CloneView(X,indices);
617 Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);
620 solver_->setTol(tolerances_);
621 solver_->setMaxIter(maxits_);
624 solver_->setSol(rcpY);
625 solver_->setRHS(rcpX);
633template <
class Scalar,
class MV,
class OP>
634void TraceMinRitzOp<Scalar,MV,OP>::removeIndices(
const std::vector<int>& indicesToRemove)
636 int nvecs = tolerances_.size();
637 int numRemoving = indicesToRemove.size();
638 std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);
639 std::vector<Scalar> helperS(nvecs-numRemoving);
641 for(
int i=0; i<nvecs; i++)
644 std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());
646 for(
int i=0; i<nvecs-numRemoving; i++)
647 helperS[i] = ritzShifts_[indicesToLeave[i]];
648 ritzShifts_ = helperS;
650 for(
int i=0; i<nvecs-numRemoving; i++)
651 helperS[i] = tolerances_[indicesToLeave[i]];
652 tolerances_ = helperS;
662template <
class Scalar,
class MV,
class OP>
663TraceMinProjRitzOp<Scalar,MV,OP>::TraceMinProjRitzOp(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs,
const Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman)
668 projector_ = Teuchos::rcp(
new TraceMinProjOp<Scalar,MV,OP>(projVecs, Op_->B_, orthman) );
671 Teuchos::RCP<TraceMinProjRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(
this);
675 solver_ = Teuchos::rcp(
new PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> >(linSolOp) );
679template <
class Scalar,
class MV,
class OP>
680TraceMinProjRitzOp<Scalar,MV,OP>::TraceMinProjRitzOp(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs,
const Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array<Teuchos::RCP<const MV> > auxVecs)
685 projector_ = Teuchos::rcp(
new TraceMinProjOp<Scalar,MV,OP>(projVecs, Op_->B_, orthman, auxVecs) );
688 Teuchos::RCP<TraceMinProjRitzOp<Scalar,MV,OP> > linSolOp = Teuchos::rcp(
this);
692 solver_ = Teuchos::rcp(
new PseudoBlockMinres< Scalar,MV,TraceMinProjRitzOp<Scalar,MV,OP> >(linSolOp) );
698template <
class Scalar,
class MV,
class OP>
699void TraceMinProjRitzOp<Scalar,MV,OP>::Apply(
const MV& X, MV& Y)
const
701 int nvecs = MVT::GetNumberVecs(X);
708 Teuchos::RCP<MV> APX = MVT::Clone(X,nvecs);
709 OPT::Apply(*Op_,X,*APX);
712 projector_->Apply(*APX,Y);
717template <
class Scalar,
class MV,
class OP>
718void TraceMinProjRitzOp<Scalar,MV,OP>::ApplyInverse(
const MV& X, MV& Y)
720 int nvecs = MVT::GetNumberVecs(X);
721 std::vector<int> indices(nvecs);
722 for(
int i=0; i<nvecs; i++)
725 Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);
726 Teuchos::RCP<MV> PX = MVT::Clone(X,nvecs);
727 projector_->Apply(X,*PX);
730 solver_->setTol(Op_->tolerances_);
731 solver_->setMaxIter(Op_->maxits_);
734 solver_->setSol(rcpY);
743template <
class Scalar,
class MV,
class OP>
744void TraceMinProjRitzOp<Scalar,MV,OP>::removeIndices(
const std::vector<int>& indicesToRemove)
746 Op_->removeIndices(indicesToRemove);
748 projector_->removeIndices(indicesToRemove);
754#ifdef HAVE_ANASAZI_BELOS
760template <
class Scalar,
class MV,
class OP>
761TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::TraceMinProjRitzOpWithPrec(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) :
762ONE(Teuchos::ScalarTraits<Scalar>::one())
767 Teuchos::RCP<TraceMinProjRitzOpWithPrec<Scalar,MV,OP> > linSolOp = Teuchos::rcp(
this);
771 problem_ = Teuchos::rcp(
new Belos::LinearProblem< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
774 problem_->setOperator(linSolOp);
778 Prec_ = Teuchos::rcp(
new TraceMinProjectedPrecOp<Scalar,MV,OP>(Op_->Prec_->A_, projVecs, orthman) );
783 solver_ = Teuchos::rcp(
new Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
787template <
class Scalar,
class MV,
class OP>
788TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::TraceMinProjRitzOpWithPrec(
const Teuchos::RCP<TraceMinRitzOp<Scalar,MV,OP> >& Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs) :
789ONE(Teuchos::ScalarTraits<Scalar>::one())
794 Teuchos::RCP<const TraceMinProjRitzOpWithPrec<Scalar,MV,OP> > linSolOp = Teuchos::rcp(
this);
798 problem_ = Teuchos::rcp(
new Belos::LinearProblem< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
801 problem_->setOperator(linSolOp);
805 Prec_ = Teuchos::rcp(
new TraceMinProjectedPrecOp<Scalar,MV,OP>(Op_->Prec_->A_, projVecs, orthman, auxVecs) );
810 solver_ = Teuchos::rcp(
new Belos::PseudoBlockGmresSolMgr< Scalar,MV,TraceMinOp<Scalar,MV,OP> >());
814template <
class Scalar,
class MV,
class OP>
815void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::Apply(
const MV& X, MV& Y)
const
817 int nvecs = MVT::GetNumberVecs(X);
818 RCP<MV> Ydot = MVT::Clone(Y,nvecs);
819 OPT::Apply(*Op_,X,*Ydot);
820 Prec_->Apply(*Ydot,Y);
824template <
class Scalar,
class MV,
class OP>
825void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::ApplyInverse(
const MV& X, MV& Y)
827 int nvecs = MVT::GetNumberVecs(X);
828 std::vector<int> indices(nvecs);
829 for(
int i=0; i<nvecs; i++)
832 Teuchos::RCP<MV> rcpY = MVT::CloneViewNonConst(Y,indices);
833 Teuchos::RCP<MV> rcpX = MVT::Clone(X,nvecs);
835 Prec_->Apply(X,*rcpX);
838 problem_->setProblem(rcpY,rcpX);
841 solver_->setProblem(problem_);
847 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::rcp(
new Teuchos::ParameterList());
848 pl->set(
"Convergence Tolerance", Op_->tolerances_[0]);
849 pl->set(
"Block Size", nvecs);
852 pl->set(
"Maximum Iterations", Op_->getMaxIts());
853 pl->set(
"Num Blocks", Op_->getMaxIts());
854 solver_->setParameters(pl);
861template <
class Scalar,
class MV,
class OP>
862void TraceMinProjRitzOpWithPrec<Scalar,MV,OP>::removeIndices(
const std::vector<int>& indicesToRemove)
864 Op_->removeIndices(indicesToRemove);
866 Prec_->removeIndices(indicesToRemove);
876template <
class Scalar,
class MV,
class OP>
877TraceMinProjectedPrecOp<Scalar,MV,OP>::TraceMinProjectedPrecOp(
const Teuchos::RCP<const OP> Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman) :
878ONE (Teuchos::ScalarTraits<Scalar>::one())
883 int nvecs = MVT::GetNumberVecs(*projVecs);
884 Teuchos::RCP<MV> helperMV = MVT::Clone(*projVecs,nvecs);
887 OPT::Apply(*Op_,*projVecs,*helperMV);
889 if(orthman_ != Teuchos::null)
892 B_ = orthman_->getOp();
893 orthman_->setOp(Op_);
895 Teuchos::RCP<MV> locProjVecs = MVT::CloneCopy(*projVecs);
898 const int rank = orthman_->normalizeMat (*locProjVecs, Teuchos::null, helperMV);
901 TEUCHOS_TEST_FOR_EXCEPTION(rank != nvecs, std::runtime_error,
"Belos::TraceMinProjectedPrecOp: constructor: Loss of orthogonality detected.");
903 orthman_->setOp(Teuchos::null);
907 std::vector<Scalar> dotprods(nvecs);
908 MVT::MvDot(*projVecs,*helperMV,dotprods);
910 for(
int i=0; i<nvecs; i++)
911 dotprods[i] = ONE/sqrt(dotprods[i]);
913 MVT::MvScale(*helperMV, dotprods);
916 projVecs_.push_back(helperMV);
920template <
class Scalar,
class MV,
class OP>
921TraceMinProjectedPrecOp<Scalar,MV,OP>::TraceMinProjectedPrecOp(
const Teuchos::RCP<const OP> Op,
const Teuchos::RCP<const MV> projVecs, Teuchos::RCP<
Anasazi::MatOrthoManager<Scalar,MV,OP> > orthman, Teuchos::Array< Teuchos::RCP<const MV> > auxVecs) :
922ONE(Teuchos::ScalarTraits<Scalar>::one())
928 Teuchos::RCP<MV> locProjVecs;
931 if(auxVecs.size() > 0)
934 nvecs = MVT::GetNumberVecs(*projVecs);
935 for(
int i=0; i<auxVecs.size(); i++)
936 nvecs += MVT::GetNumberVecs(*auxVecs[i]);
939 locProjVecs = MVT::Clone(*projVecs, nvecs);
943 std::vector<int> locind(nvecs);
945 locind.resize(MVT::GetNumberVecs(*projVecs));
946 for (
size_t i = 0; i<locind.size(); i++) {
947 locind[i] = startIndex + i;
949 startIndex += locind.size();
950 MVT::SetBlock(*projVecs,locind,*locProjVecs);
952 for (
size_t i=0; i < static_cast<size_t> (auxVecs.size ()); ++i)
954 locind.resize(MVT::GetNumberVecs(*auxVecs[i]));
955 for(
size_t j=0; j<locind.size(); j++) locind[j] = startIndex + j;
956 startIndex += locind.size();
957 MVT::SetBlock(*auxVecs[i],locind,*locProjVecs);
963 nvecs = MVT::GetNumberVecs(*projVecs);
964 locProjVecs = MVT::CloneCopy(*projVecs);
967 Teuchos::RCP<MV> helperMV = MVT::Clone(*projVecs,nvecs);
970 OPT::Apply(*Op_,*locProjVecs,*helperMV);
973 B_ = orthman_->getOp();
974 orthman_->setOp(Op_);
977 const int rank = orthman_->normalizeMat(*locProjVecs,Teuchos::null,helperMV);
979 projVecs_.push_back(helperMV);
984 TEUCHOS_TEST_FOR_EXCEPTION(
985 rank != nvecs, std::runtime_error,
986 "Belos::TraceMinProjectedPrecOp: constructor: Loss of orthogonality detected.");
988 orthman_->setOp(Teuchos::null);
992template <
class Scalar,
class MV,
class OP>
993TraceMinProjectedPrecOp<Scalar,MV,OP>::~TraceMinProjectedPrecOp()
995 if(orthman_ != Teuchos::null)
1001template <
class Scalar,
class MV,
class OP>
1002void TraceMinProjectedPrecOp<Scalar,MV,OP>::Apply(
const MV& X, MV& Y)
const
1004 int nvecsX = MVT::GetNumberVecs(X);
1006 if(orthman_ != Teuchos::null)
1009 int nvecsP = MVT::GetNumberVecs(*projVecs_[0]);
1010 OPT::Apply(*Op_,X,Y);
1012 Teuchos::RCP< Teuchos::SerialDenseMatrix<int,Scalar> > projX = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,Scalar>(nvecsP,nvecsX));
1014 MVT::MvTransMv(ONE, *projVecs_[0], X, *projX);
1016 MVT::MvTimesMatAddMv(-ONE, *projVecs_[0], *projX, ONE, Y);
1020 Teuchos::RCP<MV> MX = MVT::Clone(X,nvecsX);
1021 OPT::Apply(*Op_,X,*MX);
1023 std::vector<Scalar> dotprods(nvecsX);
1024 MVT::MvDot(*projVecs_[0], X, dotprods);
1026 Teuchos::RCP<MV> helper = MVT::CloneCopy(*projVecs_[0]);
1027 MVT::MvScale(*helper, dotprods);
1028 MVT::MvAddMv(ONE, *MX, -ONE, *helper, Y);
1033template <
class Scalar,
class MV,
class OP>
1034void TraceMinProjectedPrecOp<Scalar,MV,OP>::removeIndices(
const std::vector<int>& indicesToRemove)
1036 if(orthman_ == Teuchos::null)
1038 int nvecs = MVT::GetNumberVecs(*projVecs_[0]);
1039 int numRemoving = indicesToRemove.size();
1040 std::vector<int> helper(nvecs), indicesToLeave(nvecs-numRemoving);
1042 for(
int i=0; i<nvecs; i++)
1045 std::set_difference(helper.begin(), helper.end(), indicesToRemove.begin(), indicesToRemove.end(), indicesToLeave.begin());
1047 Teuchos::RCP<const MV> helperMV = MVT::CloneCopy(*projVecs_[0],indicesToLeave);
1048 projVecs_[0] = helperMV;
Anasazi header file which uses auto-configuration information to include necessary C++ headers.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Abstract base class for trace minimization eigensolvers.
static void Apply(const OP &Op, const MV &x, MV &y)
Application method which performs operation y = Op*x. An OperatorError exception is thrown if there i...
Anasazi's templated virtual class for constructing an operator that can interface with the OperatorTr...
virtual void Apply(const MultiVec< ScalarType > &x, MultiVec< ScalarType > &y) const =0
This method takes the Anasazi::MultiVec x and applies the operator to it resulting in the Anasazi::Mu...
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package.