Thyra_TesterBase.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 
00030 #ifndef THYRA_TESTERBASE_HPP
00031 #define THYRA_TESTERBASE_HPP
00032 
00033 #include "Thyra_LinearOperatorImpl.hpp"
00034 #include "Thyra_TestSpecifier.hpp"
00035 #include "Teuchos_ScalarTraits.hpp"
00036 #include "Teuchos_Comm.hpp"
00037 #include "Teuchos_CommHelpers.hpp"
00038 
00039 
00040 namespace Thyra {
00041 
00043 template <class Scalar>
00044 class TesterBase 
00045 {
00046 public:
00048   typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag;
00049 
00051   TesterBase(const RCP<const Comm<int> >& comm,
00052     const VectorSpace<Scalar>& space, int nCols,
00053     Teuchos::RCP<Teuchos::FancyOStream>& out)
00054     : comm_(comm), space_(space), nCols_(nCols), out_(out) 
00055     {
00056       using std::endl;
00057       *out << "==========================================================================="
00058            << endl;
00059       *out << "       testing on type " 
00060            << Teuchos::ScalarTraits<Scalar>::name() << endl;
00061       *out << "==========================================================================="
00062            << endl;
00063     }
00064 
00066   virtual ~TesterBase(){;}
00067 
00069   virtual bool runAllTests() const = 0 ;
00070 
00071 
00073   bool checkTest(const TestSpecifier<Scalar>& spec,
00074     const ScalarMag& err, 
00075     const string& testName) const ;
00076 
00078   void randomizeVec(Vector<Scalar>& x) const ;
00079 
00081   LinearOperator<Scalar> randomDenseOp() const ;
00082 
00084   const VectorSpace<Scalar>& space() const {return space_;}
00085 
00087   ostream& out() const {return *out_;}
00088 
00090   const Comm<int>& comm() const {return *comm_;}
00091     
00092 private:
00093   RCP<const Comm<int> > comm_;
00094   VectorSpace<Scalar> space_;
00095   int nCols_;
00096   mutable Teuchos::RCP<Teuchos::FancyOStream> out_;
00097 };
00098 
00099 
00100 template <class Scalar> 
00101 inline void TesterBase<Scalar>
00102 ::randomizeVec(Vector<Scalar>& x) const
00103 {
00104   typedef ScalarTraits<Scalar> ST;
00105   randomize(Scalar(-ST::one()),Scalar(+ST::one()),x.ptr().get());
00106     
00107 }
00108 
00109 
00110 template <class Scalar> 
00111 inline bool TesterBase<Scalar>
00112 ::checkTest(const TestSpecifier<Scalar>& spec,
00113   const ScalarMag& err, 
00114   const string& testName) const 
00115 {
00116     
00117   using std::endl;
00118 
00119   bool rtn = true;
00120   if (err > spec.errorTol())
00121   {
00122     *out_ << testName << " test FAILED: err=" << err << ", tol = " 
00123           << spec.errorTol() << endl;
00124     rtn = false;
00125   }
00126   else if (err > spec.warningTol())
00127   {
00128     *out_ << "WARNING: " << testName << " test err="
00129           << err << " could not beat tol = " 
00130           << spec.warningTol() << endl;
00131   }
00132   else
00133   {
00134     *out_ << "test " << testName << " PASSED with tol=" << spec.errorTol() << endl;
00135   }
00136   return rtn;
00137 }
00138 
00139 
00140 template <class Scalar>
00141 inline LinearOperator<Scalar> TesterBase<Scalar>
00142 ::randomDenseOp() const 
00143 {
00144   typedef ScalarTraits<Scalar> ST;
00145   RCP<MultiVectorBase<Scalar> > mv = space_.createMembers(nCols_);
00146   randomize(-ST::one(), ST::one(), &*mv);
00147   RCP<LinearOpBase<Scalar> > rtn = mv;
00148   return rtn;
00149 }
00150  
00151  
00152 } // namespace Thyra
00153 
00154 
00155 #endif // THYRA_TESTERBASE_HPP

Generated on Wed May 12 21:26:54 2010 for Thyra Operator/Vector Support by  doxygen 1.4.7