55 #ifndef __Teko_Utilities_hpp__
56 #define __Teko_Utilities_hpp__
58 #include "Epetra_CrsMatrix.h"
61 #include "Teuchos_VerboseObject.hpp"
64 #include "Thyra_LinearOpBase.hpp"
65 #include "Thyra_PhysicallyBlockedLinearOpBase.hpp"
66 #include "Thyra_ProductVectorSpaceBase.hpp"
67 #include "Thyra_VectorSpaceBase.hpp"
68 #include "Thyra_ProductMultiVectorBase.hpp"
69 #include "Thyra_MultiVectorStdOps.hpp"
70 #include "Thyra_MultiVectorBase.hpp"
71 #include "Thyra_VectorBase.hpp"
72 #include "Thyra_VectorStdOps.hpp"
73 #include "Thyra_DefaultBlockedLinearOp.hpp"
74 #include "Thyra_DefaultMultipliedLinearOp.hpp"
75 #include "Thyra_DefaultScaledAdjointLinearOp.hpp"
76 #include "Thyra_DefaultAddedLinearOp.hpp"
77 #include "Thyra_DefaultIdentityLinearOp.hpp"
78 #include "Thyra_DefaultZeroLinearOp.hpp"
81 #define Teko_DEBUG_INT 5
85 using Thyra::multiply;
88 using Thyra::identity;
90 using Thyra::block2x2;
91 using Thyra::block2x1;
92 using Thyra::block1x2;
112 Teuchos::RCP<Epetra_CrsMatrix> buildGraphLaplacian(
int dim,
double * coords,
const Epetra_CrsMatrix & stencil);
136 Teuchos::RCP<Epetra_CrsMatrix> buildGraphLaplacian(
double * x,
double * y,
double * z,
int stride,
const Epetra_CrsMatrix & stencil);
144 const Teuchos::RCP<Teuchos::FancyOStream> getOutputStream();
148 #ifndef Teko_DEBUG_OFF
149 #define Teko_DEBUG_EXPR(str) str
150 #define Teko_DEBUG_MSG(str,level) if(level<=Teko_DEBUG_INT) { \
151 Teuchos::RCP<Teuchos::FancyOStream> out = Teko::getOutputStream(); \
152 *out << "Teko: " << str << std::endl; }
153 #define Teko_DEBUG_MSG_BEGIN(level) if(level<=Teko_DEBUG_INT) { \
154 Teko::getOutputStream()->pushTab(3); \
155 *Teko::getOutputStream() << "Teko: Begin debug MSG\n"; \
156 std::ostream & DEBUG_STREAM = *Teko::getOutputStream(); \
157 Teko::getOutputStream()->pushTab(3);
158 #define Teko_DEBUG_MSG_END() Teko::getOutputStream()->popTab(); \
159 *Teko::getOutputStream() << "Teko: End debug MSG\n"; \
160 Teko::getOutputStream()->popTab(); }
161 #define Teko_DEBUG_PUSHTAB() Teko::getOutputStream()->pushTab(3)
162 #define Teko_DEBUG_POPTAB() Teko::getOutputStream()->popTab()
164 struct __DebugScope__ {
165 __DebugScope__(
const std::string & str,
int level)
166 : str_(str), level_(level)
167 { Teko_DEBUG_MSG(
"BEGIN "+str_,level_); Teko_DEBUG_PUSHTAB(); }
169 { Teko_DEBUG_POPTAB(); Teko_DEBUG_MSG(
"END "+str_,level_); }
170 std::string str_;
int level_; };
171 #define Teko_DEBUG_SCOPE(str,level) __DebugScope__ __dbgScope__(str,level);
173 #define Teko_DEBUG_EXPR(str)
174 #define Teko_DEBUG_MSG(str,level)
175 #define Teko_DEBUG_MSG_BEGIN(level) if(false) { \
176 std::ostream & DEBUG_STREAM = *Teko::getOutputStream();
177 #define Teko_DEBUG_MSG_END() }
178 #define Teko_DEBUG_PUSHTAB()
179 #define Teko_DEBUG_POPTAB()
180 #define Teko_DEBUG_SCOPE(str,level)
184 typedef Teuchos::RCP<const Thyra::VectorSpaceBase<double> > VectorSpace;
191 typedef Teuchos::RCP<Thyra::ProductMultiVectorBase<double> > BlockedMultiVector;
192 typedef Teuchos::RCP<Thyra::MultiVectorBase<double> > MultiVector;
195 inline MultiVector toMultiVector(BlockedMultiVector & bmv) {
return bmv; }
198 inline const MultiVector toMultiVector(
const BlockedMultiVector & bmv) {
return bmv; }
201 inline const BlockedMultiVector toBlockedMultiVector(
const MultiVector & bmv)
202 {
return Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(bmv); }
205 inline int blockCount(
const BlockedMultiVector & bmv)
206 {
return bmv->productSpace()->numBlocks(); }
209 inline MultiVector getBlock(
int i,
const BlockedMultiVector & bmv)
210 {
return Teuchos::rcp_const_cast<Thyra::MultiVectorBase<double> >(bmv->getMultiVectorBlock(i)); }
213 inline MultiVector deepcopy(
const MultiVector & v)
214 {
return v->clone_mv(); }
217 inline MultiVector copyAndInit(
const MultiVector & v,
double scalar)
218 { MultiVector mv = v->clone_mv(); Thyra::assign(mv.ptr(),scalar);
return mv; }
221 inline BlockedMultiVector deepcopy(
const BlockedMultiVector & v)
222 {
return toBlockedMultiVector(v->clone_mv()); }
237 inline MultiVector datacopy(
const MultiVector & src,MultiVector & dst)
239 if(dst==Teuchos::null)
240 return deepcopy(src);
242 bool rangeCompat = src->range()->isCompatible(*dst->range());
243 bool domainCompat = src->domain()->isCompatible(*dst->domain());
245 if(not (rangeCompat && domainCompat))
246 return deepcopy(src);
249 Thyra::assign<double>(dst.ptr(),*src);
266 inline BlockedMultiVector datacopy(
const BlockedMultiVector & src,BlockedMultiVector & dst)
268 if(dst==Teuchos::null)
269 return deepcopy(src);
271 bool rangeCompat = src->range()->isCompatible(*dst->range());
272 bool domainCompat = src->domain()->isCompatible(*dst->domain());
274 if(not (rangeCompat && domainCompat))
275 return deepcopy(src);
278 Thyra::assign<double>(dst.ptr(),*src);
283 BlockedMultiVector buildBlockedMultiVector(
const std::vector<MultiVector> & mvs);
295 Teuchos::RCP<Thyra::VectorBase<double> > indicatorVector(
296 const std::vector<int> & indices,
297 const VectorSpace & vs,
298 double onValue=1.0,
double offValue=0.0);
306 typedef Teuchos::RCP<Thyra::PhysicallyBlockedLinearOpBase<double> > BlockedLinearOp;
307 typedef Teuchos::RCP<const Thyra::LinearOpBase<double> > LinearOp;
308 typedef Teuchos::RCP<Thyra::LinearOpBase<double> > InverseLinearOp;
309 typedef Teuchos::RCP<Thyra::LinearOpBase<double> > ModifiableLinearOp;
312 inline LinearOp zero(
const VectorSpace & vs)
313 {
return Thyra::zero<double>(vs,vs); }
316 void putScalar(
const ModifiableLinearOp & op,
double scalar);
319 inline VectorSpace rangeSpace(
const LinearOp & lo)
320 {
return lo->range(); }
323 inline VectorSpace domainSpace(
const LinearOp & lo)
324 {
return lo->domain(); }
327 inline BlockedLinearOp toBlockedLinearOp(LinearOp & clo)
329 Teuchos::RCP<Thyra::LinearOpBase<double> > lo = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(clo);
330 return Teuchos::rcp_dynamic_cast<Thyra::PhysicallyBlockedLinearOpBase<double> > (lo);
334 inline const BlockedLinearOp toBlockedLinearOp(
const LinearOp & clo)
336 Teuchos::RCP<Thyra::LinearOpBase<double> > lo = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(clo);
337 return Teuchos::rcp_dynamic_cast<Thyra::PhysicallyBlockedLinearOpBase<double> > (lo);
341 inline LinearOp toLinearOp(BlockedLinearOp & blo) {
return blo; }
344 inline const LinearOp toLinearOp(
const BlockedLinearOp & blo) {
return blo; }
347 inline LinearOp toLinearOp(ModifiableLinearOp & blo) {
return blo; }
350 inline const LinearOp toLinearOp(
const ModifiableLinearOp & blo) {
return blo; }
353 inline int blockRowCount(
const BlockedLinearOp & blo)
354 {
return blo->productRange()->numBlocks(); }
357 inline int blockColCount(
const BlockedLinearOp & blo)
358 {
return blo->productDomain()->numBlocks(); }
361 inline LinearOp getBlock(
int i,
int j,
const BlockedLinearOp & blo)
362 {
return blo->getBlock(i,j); }
365 inline void setBlock(
int i,
int j,BlockedLinearOp & blo,
const LinearOp & lo)
366 {
return blo->setBlock(i,j,lo); }
369 inline BlockedLinearOp createBlockedOp()
370 {
return rcp(
new Thyra::DefaultBlockedLinearOp<double>()); }
383 inline void beginBlockFill(BlockedLinearOp & blo,
int rowCnt,
int colCnt)
384 { blo->beginBlockFill(rowCnt,colCnt); }
395 inline void beginBlockFill(BlockedLinearOp & blo)
396 { blo->beginBlockFill(); }
399 inline void endBlockFill(BlockedLinearOp & blo)
400 { blo->endBlockFill(); }
403 BlockedLinearOp getUpperTriBlocks(
const BlockedLinearOp & blo,
bool callEndBlockFill=
true);
406 BlockedLinearOp getLowerTriBlocks(
const BlockedLinearOp & blo,
bool callEndBlockFill=
true);
427 BlockedLinearOp zeroBlockedOp(
const BlockedLinearOp & blo);
430 bool isZeroOp(
const LinearOp op);
440 ModifiableLinearOp getAbsRowSumMatrix(
const LinearOp & op);
450 ModifiableLinearOp getAbsRowSumInvMatrix(
const LinearOp & op);
459 ModifiableLinearOp getLumpedMatrix(
const LinearOp & op);
469 ModifiableLinearOp getInvLumpedMatrix(
const LinearOp & op);
494 void applyOp(
const LinearOp & A,
const MultiVector & x,MultiVector & y,
double alpha=1.0,
double beta=0.0);
514 inline void applyOp(
const LinearOp & A,
const BlockedMultiVector & x,BlockedMultiVector & y,
double alpha=1.0,
double beta=0.0)
515 {
const MultiVector x_mv = toMultiVector(x); MultiVector y_mv = toMultiVector(y);
516 applyOp(A,x_mv,y_mv,alpha,beta); }
527 void update(
double alpha,
const MultiVector & x,
double beta,MultiVector & y);
530 inline void update(
double alpha,
const BlockedMultiVector & x,
double beta,BlockedMultiVector & y)
531 { MultiVector x_mv = toMultiVector(x); MultiVector y_mv = toMultiVector(y);
532 update(alpha,x_mv,beta,y_mv); }
535 inline void scale(
const double alpha,MultiVector & x) { Thyra::scale<double>(alpha,x.ptr()); }
538 inline void scale(
const double alpha,BlockedMultiVector & x)
539 { MultiVector x_mv = toMultiVector(x); scale(alpha,x_mv); }
542 inline LinearOp scale(
const double alpha,ModifiableLinearOp & a)
543 {
return Thyra::nonconstScale(alpha,a); }
546 inline LinearOp adjoint(ModifiableLinearOp & a)
547 {
return Thyra::nonconstAdjoint(a); }
565 const ModifiableLinearOp getDiagonalOp(
const LinearOp & op);
572 const MultiVector getDiagonal(
const LinearOp & op);
585 const ModifiableLinearOp getInvDiagonalOp(
const LinearOp & op);
599 const LinearOp explicitMultiply(
const LinearOp & opl,
const LinearOp & opm,
const LinearOp & opr);
615 const ModifiableLinearOp explicitMultiply(
const LinearOp & opl,
const LinearOp & opm,
const LinearOp & opr,
616 const ModifiableLinearOp & destOp);
628 const LinearOp explicitMultiply(
const LinearOp & opl,
const LinearOp & opr);
643 const ModifiableLinearOp explicitMultiply(
const LinearOp & opl,
const LinearOp & opr,
644 const ModifiableLinearOp & destOp);
656 const LinearOp explicitAdd(
const LinearOp & opl,
const LinearOp & opr);
670 const ModifiableLinearOp explicitAdd(
const LinearOp & opl,
const LinearOp & opr,
671 const ModifiableLinearOp & destOp);
675 const ModifiableLinearOp explicitSum(
const LinearOp & opl,
676 const ModifiableLinearOp & destOp);
681 const LinearOp explicitTranspose(
const LinearOp & op);
686 const LinearOp buildDiagonal(
const MultiVector & v,
const std::string & lbl=
"ANYM");
691 const LinearOp buildInvDiagonal(
const MultiVector & v,
const std::string & lbl=
"ANYM");
718 double computeSpectralRad(
const Teuchos::RCP<
const Thyra::LinearOpBase<double> > & A,
double tol,
719 bool isHermitian=
false,
int numBlocks=5,
int restart=0,
int verbosity=0);
744 double computeSmallestMagEig(
const Teuchos::RCP<
const Thyra::LinearOpBase<double> > & A,
double tol,
745 bool isHermitian=
false,
int numBlocks=5,
int restart=0,
int verbosity=0);
748 typedef enum { Diagonal
763 ModifiableLinearOp getDiagonalOp(
const Teko::LinearOp & A,
const DiagonalType & dt);
773 ModifiableLinearOp getInvDiagonalOp(
const Teko::LinearOp & A,
const DiagonalType & dt);
780 const MultiVector getDiagonal(
const LinearOp & op,
const DiagonalType & dt);
788 std::string getDiagonalName(
const DiagonalType & dt);
798 DiagonalType getDiagonalType(std::string name);
800 LinearOp probe(Teuchos::RCP<const Epetra_CrsGraph> &G,
const LinearOp & Op);
804 double norm_1(
const MultiVector & v,std::size_t col);
808 double norm_2(
const MultiVector & v,std::size_t col);
813 void clipLower(MultiVector & v,
double lowerBound);
818 void clipUpper(MultiVector & v,
double upperBound);
823 void replaceValue(MultiVector & v,
double currentValue,
double newValue);
827 void columnAverages(
const MultiVector & v,std::vector<double> & averages);
831 double average(
const MultiVector & v);