Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_Utilities.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
55 #ifndef __Teko_Utilities_hpp__
56 #define __Teko_Utilities_hpp__
57 
58 #include "Epetra_CrsMatrix.h"
59 
60 // Teuchos includes
61 #include "Teuchos_VerboseObject.hpp"
62 
63 // Thyra includes
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"
79 
80 // #define Teko_DEBUG_OFF
81 #define Teko_DEBUG_INT 5
82 
83 namespace Teko {
84 
85 using Thyra::multiply;
86 using Thyra::scale;
87 using Thyra::add;
88 using Thyra::identity;
89 using Thyra::zero; // make it to take one argument (square matrix)
90 using Thyra::block2x2;
91 using Thyra::block2x1;
92 using Thyra::block1x2;
93 
112 Teuchos::RCP<Epetra_CrsMatrix> buildGraphLaplacian(int dim,double * coords,const Epetra_CrsMatrix & stencil);
113 
136 Teuchos::RCP<Epetra_CrsMatrix> buildGraphLaplacian(double * x,double * y,double * z,int stride,const Epetra_CrsMatrix & stencil);
137 
144 const Teuchos::RCP<Teuchos::FancyOStream> getOutputStream();
145 // inline const Teuchos::RCP<Teuchos::FancyOStream> getOutputStream();
146 // { return Teuchos::VerboseObjectBase::getDefaultOStream(); }
147 
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()
163 
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(); }
168  ~__DebugScope__()
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);
172 #else
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)
181 #endif
182 
183 // typedefs for increased simplicity
184 typedef Teuchos::RCP<const Thyra::VectorSpaceBase<double> > VectorSpace;
185 
186 // ----------------------------------------------------------------------------
187 
189 
190 
191 typedef Teuchos::RCP<Thyra::ProductMultiVectorBase<double> > BlockedMultiVector;
192 typedef Teuchos::RCP<Thyra::MultiVectorBase<double> > MultiVector;
193 
195 inline MultiVector toMultiVector(BlockedMultiVector & bmv) { return bmv; }
196 
198 inline const MultiVector toMultiVector(const BlockedMultiVector & bmv) { return bmv; }
199 
201 inline const BlockedMultiVector toBlockedMultiVector(const MultiVector & bmv)
202 { return Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(bmv); }
203 
205 inline int blockCount(const BlockedMultiVector & bmv)
206 { return bmv->productSpace()->numBlocks(); }
207 
209 inline MultiVector getBlock(int i,const BlockedMultiVector & bmv)
210 { return Teuchos::rcp_const_cast<Thyra::MultiVectorBase<double> >(bmv->getMultiVectorBlock(i)); }
211 
213 inline MultiVector deepcopy(const MultiVector & v)
214 { return v->clone_mv(); }
215 
217 inline MultiVector copyAndInit(const MultiVector & v,double scalar)
218 { MultiVector mv = v->clone_mv(); Thyra::assign(mv.ptr(),scalar); return mv; }
219 
221 inline BlockedMultiVector deepcopy(const BlockedMultiVector & v)
222 { return toBlockedMultiVector(v->clone_mv()); }
223 
237 inline MultiVector datacopy(const MultiVector & src,MultiVector & dst)
238 {
239  if(dst==Teuchos::null)
240  return deepcopy(src);
241 
242  bool rangeCompat = src->range()->isCompatible(*dst->range());
243  bool domainCompat = src->domain()->isCompatible(*dst->domain());
244 
245  if(not (rangeCompat && domainCompat))
246  return deepcopy(src);
247 
248  // perform data copy
249  Thyra::assign<double>(dst.ptr(),*src);
250  return dst;
251 }
252 
266 inline BlockedMultiVector datacopy(const BlockedMultiVector & src,BlockedMultiVector & dst)
267 {
268  if(dst==Teuchos::null)
269  return deepcopy(src);
270 
271  bool rangeCompat = src->range()->isCompatible(*dst->range());
272  bool domainCompat = src->domain()->isCompatible(*dst->domain());
273 
274  if(not (rangeCompat && domainCompat))
275  return deepcopy(src);
276 
277  // perform data copy
278  Thyra::assign<double>(dst.ptr(),*src);
279  return dst;
280 }
281 
283 BlockedMultiVector buildBlockedMultiVector(const std::vector<MultiVector> & mvs);
284 
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);
299 
301 
302 // ----------------------------------------------------------------------------
303 
305 
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;
310 
312 inline LinearOp zero(const VectorSpace & vs)
313 { return Thyra::zero<double>(vs,vs); }
314 
316 void putScalar(const ModifiableLinearOp & op,double scalar);
317 
319 inline VectorSpace rangeSpace(const LinearOp & lo)
320 { return lo->range(); }
321 
323 inline VectorSpace domainSpace(const LinearOp & lo)
324 { return lo->domain(); }
325 
327 inline BlockedLinearOp toBlockedLinearOp(LinearOp & clo)
328 {
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);
331 }
332 
334 inline const BlockedLinearOp toBlockedLinearOp(const LinearOp & clo)
335 {
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);
338 }
339 
341 inline LinearOp toLinearOp(BlockedLinearOp & blo) { return blo; }
342 
344 inline const LinearOp toLinearOp(const BlockedLinearOp & blo) { return blo; }
345 
347 inline LinearOp toLinearOp(ModifiableLinearOp & blo) { return blo; }
348 
350 inline const LinearOp toLinearOp(const ModifiableLinearOp & blo) { return blo; }
351 
353 inline int blockRowCount(const BlockedLinearOp & blo)
354 { return blo->productRange()->numBlocks(); }
355 
357 inline int blockColCount(const BlockedLinearOp & blo)
358 { return blo->productDomain()->numBlocks(); }
359 
361 inline LinearOp getBlock(int i,int j,const BlockedLinearOp & blo)
362 { return blo->getBlock(i,j); }
363 
365 inline void setBlock(int i,int j,BlockedLinearOp & blo, const LinearOp & lo)
366 { return blo->setBlock(i,j,lo); }
367 
369 inline BlockedLinearOp createBlockedOp()
370 { return rcp(new Thyra::DefaultBlockedLinearOp<double>()); }
371 
383 inline void beginBlockFill(BlockedLinearOp & blo,int rowCnt,int colCnt)
384 { blo->beginBlockFill(rowCnt,colCnt); }
385 
395 inline void beginBlockFill(BlockedLinearOp & blo)
396 { blo->beginBlockFill(); }
397 
399 inline void endBlockFill(BlockedLinearOp & blo)
400 { blo->endBlockFill(); }
401 
403 BlockedLinearOp getUpperTriBlocks(const BlockedLinearOp & blo,bool callEndBlockFill=true);
404 
406 BlockedLinearOp getLowerTriBlocks(const BlockedLinearOp & blo,bool callEndBlockFill=true);
407 
427 BlockedLinearOp zeroBlockedOp(const BlockedLinearOp & blo);
428 
430 bool isZeroOp(const LinearOp op);
431 
440 ModifiableLinearOp getAbsRowSumMatrix(const LinearOp & op);
441 
450 ModifiableLinearOp getAbsRowSumInvMatrix(const LinearOp & op);
451 
459 ModifiableLinearOp getLumpedMatrix(const LinearOp & op);
460 
469 ModifiableLinearOp getInvLumpedMatrix(const LinearOp & op);
470 
472 
474 
475 
494 void applyOp(const LinearOp & A,const MultiVector & x,MultiVector & y,double alpha=1.0,double beta=0.0);
495 
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); }
517 
527 void update(double alpha,const MultiVector & x,double beta,MultiVector & y);
528 
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); }
533 
535 inline void scale(const double alpha,MultiVector & x) { Thyra::scale<double>(alpha,x.ptr()); }
536 
538 inline void scale(const double alpha,BlockedMultiVector & x)
539 { MultiVector x_mv = toMultiVector(x); scale(alpha,x_mv); }
540 
542 inline LinearOp scale(const double alpha,ModifiableLinearOp & a)
543 { return Thyra::nonconstScale(alpha,a); }
544 
546 inline LinearOp adjoint(ModifiableLinearOp & a)
547 { return Thyra::nonconstAdjoint(a); }
548 
550 
552 
553 
565 const ModifiableLinearOp getDiagonalOp(const LinearOp & op);
566 
572 const MultiVector getDiagonal(const LinearOp & op);
573 
585 const ModifiableLinearOp getInvDiagonalOp(const LinearOp & op);
586 
599 const LinearOp explicitMultiply(const LinearOp & opl,const LinearOp & opm,const LinearOp & opr);
600 
615 const ModifiableLinearOp explicitMultiply(const LinearOp & opl,const LinearOp & opm,const LinearOp & opr,
616  const ModifiableLinearOp & destOp);
617 
628 const LinearOp explicitMultiply(const LinearOp & opl,const LinearOp & opr);
629 
643 const ModifiableLinearOp explicitMultiply(const LinearOp & opl,const LinearOp & opr,
644  const ModifiableLinearOp & destOp);
645 
656 const LinearOp explicitAdd(const LinearOp & opl,const LinearOp & opr);
657 
670 const ModifiableLinearOp explicitAdd(const LinearOp & opl,const LinearOp & opr,
671  const ModifiableLinearOp & destOp);
672 
675 const ModifiableLinearOp explicitSum(const LinearOp & opl,
676  const ModifiableLinearOp & destOp);
677 
681 const LinearOp explicitTranspose(const LinearOp & op);
682 
686 const LinearOp buildDiagonal(const MultiVector & v,const std::string & lbl="ANYM");
687 
691 const LinearOp buildInvDiagonal(const MultiVector & v,const std::string & lbl="ANYM");
692 
694 
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);
720 
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);
746 
748 typedef enum { Diagonal
749  , Lumped
750  , AbsRowSum
751  , BlkDiag
752  , NotDiag
753  } DiagonalType;
754 
763 ModifiableLinearOp getDiagonalOp(const Teko::LinearOp & A,const DiagonalType & dt);
764 
773 ModifiableLinearOp getInvDiagonalOp(const Teko::LinearOp & A,const DiagonalType & dt);
774 
780 const MultiVector getDiagonal(const LinearOp & op,const DiagonalType & dt);
781 
788 std::string getDiagonalName(const DiagonalType & dt);
789 
798 DiagonalType getDiagonalType(std::string name);
799 
800 LinearOp probe(Teuchos::RCP<const Epetra_CrsGraph> &G, const LinearOp & Op);
801 
804 double norm_1(const MultiVector & v,std::size_t col);
805 
808 double norm_2(const MultiVector & v,std::size_t col);
809 
813 void clipLower(MultiVector & v,double lowerBound);
814 
818 void clipUpper(MultiVector & v,double upperBound);
819 
823 void replaceValue(MultiVector & v,double currentValue,double newValue);
824 
827 void columnAverages(const MultiVector & v,std::vector<double> & averages);
828 
831 double average(const MultiVector & v);
832 
833 } // end namespace Teko
834 
835 #endif