TSFVectorTester.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
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 TSF_VECTORTESTER_HPP
00031 #define TSF_VECTORTESTER_HPP
00032 
00033 #include "TSFVectorDecl.hpp"
00034 #include "TSFLinearCombinationImpl.hpp"
00035 #include "Thyra_TestSpecifier.hpp"
00036 #include "Teuchos_ScalarTraits.hpp"
00037 
00038 using namespace TSFExtended;
00039 using namespace TSFExtendedOps;
00040 using namespace Teuchos;
00041 
00042 using Thyra::TestSpecifier;
00043 
00044 namespace TSFExtended
00045 {
00046 
00047   /** 
00048    * Run comparisons between element-wise calculations and Vector member
00049    * functions.
00050    */
00051   template <class Scalar>
00052   class VectorTester
00053   {
00054   public:
00055     /** \brief Local typedef for promoted scalar magnitude */
00056     typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType ScalarMag;
00057 
00058     /** */
00059     VectorTester(const VectorSpace<Scalar>& space,
00060                  const TestSpecifier<Scalar>& spec,
00061                  const Teuchos::MPIComm& comm = Teuchos::MPIComm::world());
00062 
00063     /** */
00064     bool runAllTests() const ;
00065 
00066     /** */
00067     bool sumTest() const ;
00068 
00069     /** */
00070     bool dotStarTest() const ;
00071 
00072     /** */
00073     bool dotSlashTest() const ;
00074 
00075     /** */
00076     bool scalarMultTest() const ;
00077 
00078     /** */
00079     bool overloadedUpdateTest() const ;
00080 
00081 
00082   private:
00083 
00084     /** */
00085     void randomizeVec(Vector<Scalar>& x) const ;
00086 
00087     TestSpecifier<Scalar> spec_;
00088 
00089     VectorSpace<Scalar> space_;
00090 
00091     Teuchos::MPIComm comm_;
00092 
00093   };
00094 
00095   template <class Scalar> 
00096   inline VectorTester<Scalar>
00097   ::VectorTester(const VectorSpace<Scalar>& space,
00098                  const TestSpecifier<Scalar>& spec,
00099                  const Teuchos::MPIComm& comm)
00100     : spec_(spec), space_(space), comm_(comm)
00101   {;}
00102 
00103   template <class Scalar> 
00104   inline bool VectorTester<Scalar>
00105   ::runAllTests() const
00106   {
00107     bool pass = true;
00108 
00109     pass = sumTest() && pass;
00110     pass = dotStarTest() && pass;
00111     pass = dotSlashTest() && pass;
00112     pass = scalarMultTest() && pass;
00113     pass = overloadedUpdateTest() && pass;
00114 
00115     return pass;
00116   }
00117 
00118   template <class Scalar> 
00119   inline void VectorTester<Scalar>
00120   ::randomizeVec(Vector<Scalar>& x) const
00121   {
00122     typedef Teuchos::ScalarTraits<Scalar> ST;
00123 
00124     /* do the operation elementwise */
00125     SequentialIterator<Scalar> i;
00126     for (i=space_.begin(); i != space_.end(); i++)
00127       {
00128         x[i] = 2.0*(drand48()-0.5);
00129       }    
00130   }
00131 
00132   template <class Scalar> 
00133   inline bool VectorTester<Scalar>
00134   ::sumTest() const 
00135   {
00136     if (spec_.doTest())
00137       {
00138         std::cerr << "running vector addition test..." << std::endl;
00139 
00140         Vector<Scalar> a = space_.createMember();
00141         Vector<Scalar> b = space_.createMember();
00142         Vector<Scalar> x = space_.createMember();
00143         Vector<Scalar> y = space_.createMember();
00144         x.zero();
00145         y.zero();
00146         cout << "x = " << x << std::endl;
00147         cout << "y = " << y << std::endl;
00148         randomizeVec(a);
00149         randomizeVec(b);
00150         cout << "a = " << a << std::endl;
00151         cout << "b = " << b << std::endl;
00152 
00153         /* do the operation elementwise */
00154         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00155           {
00156             y[i] = a[i] + b[i];
00157           }
00158 
00159         /* do the operation with member functions */
00160         x = a + b ;
00161 
00162         cout << "op   (a+b)=" << std::endl << x << std::endl;
00163         cout << "loop (a+b)=" << std::endl << y << std::endl;
00164   
00165         double err = (x-y).normInf();
00166 
00167         std::cerr << "|sum error|=" << err << std::endl;
00168         if (err > spec_.errorTol())
00169           {
00170             std::cerr << "vector sum test FAILED: tol = " 
00171                  << spec_.errorTol() << std::endl;
00172             return false;
00173           }
00174         else if (err > spec_.warningTol())
00175           {
00176             std::cerr << "WARNING: vector sum test could not beat tol = " 
00177                  << spec_.warningTol() << std::endl;
00178           }
00179   
00180       }
00181     else
00182       {
00183         std::cerr << "skipping vector addition test..." << std::endl;
00184       }
00185     std::cerr << "vector addition test PASSED: tol = " 
00186          << spec_.errorTol() << std::endl;
00187     return true;
00188   }
00189 
00190   
00191 
00192   
00193 
00194   template <class Scalar> 
00195   inline bool VectorTester<Scalar>
00196   ::dotStarTest() const 
00197   {
00198     if (spec_.doTest())
00199       {
00200         std::cerr << "running vector dotStar test..." << std::endl;
00201 
00202         Vector<Scalar> a = space_.createMember();
00203         Vector<Scalar> b = space_.createMember();
00204         Vector<Scalar> x = space_.createMember();
00205         Vector<Scalar> y = space_.createMember();
00206         randomizeVec(a);
00207         randomizeVec(b);
00208 
00209 
00210         /* do the operation with member functions */
00211         x = a.dotStar(b);
00212 
00213         /* do the operation elementwise */
00214         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00215           {
00216             y[i] = a[i] * b[i];
00217           }
00218 
00219         double err = (x-y).normInf();
00220 
00221         std::cerr << "|dotStar error|=" << err << std::endl;
00222         if (err > spec_.errorTol())
00223           {
00224             std::cerr << "vector dotStar test FAILED: tol = " 
00225                  << spec_.errorTol() << std::endl;
00226             return false;
00227           }
00228         else if (err > spec_.warningTol())
00229           {
00230             std::cerr << "WARNING: vector dotStar test could not beat tol = " 
00231                  << spec_.warningTol() << std::endl;
00232           }
00233   
00234       }
00235     else
00236       {
00237         std::cerr << "skipping vector dotStar test..." << std::endl;
00238       }
00239     std::cerr << "vector dotStar test PASSED: tol = " 
00240          << spec_.errorTol() << std::endl;
00241     return true;
00242   }
00243 
00244 
00245   template <class Scalar> 
00246   inline bool VectorTester<Scalar>
00247   ::dotSlashTest() const 
00248   {
00249     if (spec_.doTest())
00250       {
00251         std::cerr << "running vector dotSlash test..." << std::endl;
00252 
00253         Vector<Scalar> a = space_.createMember();
00254         Vector<Scalar> b = space_.createMember();
00255         Vector<Scalar> x = space_.createMember();
00256         Vector<Scalar> y = space_.createMember();
00257         randomizeVec(a);
00258         randomizeVec(b);
00259 
00260 
00261         /* do the operation with member functions */
00262         x = a.dotSlash(b);
00263 
00264 
00265         /* do the operation elementwise */
00266         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00267           {
00268             y[i] = a[i] / b[i];
00269           }
00270   
00271         double err = (x-y).normInf();
00272 
00273         std::cerr << "|dotSlash error|=" << err << std::endl;
00274         if (err > spec_.errorTol())
00275           {
00276             std::cerr << "vector dotSlash test FAILED: tol = " 
00277                  << spec_.errorTol() << std::endl;
00278             return false;
00279           }
00280         else if (err > spec_.warningTol())
00281           {
00282             std::cerr << "WARNING: vector dotSlash test could not beat tol = " 
00283                  << spec_.warningTol() << std::endl;
00284           }
00285   
00286       }
00287     else
00288       {
00289         std::cerr << "skipping vector dotSlash test..." << std::endl;
00290       }
00291     std::cerr << "vector dotSlash test PASSED: tol = " 
00292          << spec_.errorTol() << std::endl;
00293     return true;
00294   }
00295 
00296   
00297   template <class Scalar> 
00298   inline bool VectorTester<Scalar>
00299   ::scalarMultTest() const 
00300   {
00301     if (spec_.doTest())
00302       {
00303         std::cerr << "running vector scalarMult test..." << std::endl;
00304 
00305         Vector<Scalar> a = space_.createMember();
00306         Vector<Scalar> x = space_.createMember();
00307         Vector<Scalar> y = space_.createMember();
00308         randomizeVec(a);
00309 
00310 
00311         /* do the operation with member functions */
00312         x = 3.14*a;
00313 
00314         /* do the operation elementwise */
00315         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00316           {
00317             y[i] = 3.14 * a[i];
00318           }
00319 
00320         double err = (x-y).normInf();
00321 
00322         std::cerr << "|scalarMult error|=" << err << std::endl;
00323         if (err > spec_.errorTol())
00324           {
00325             std::cerr << "vector scalarMult test FAILED: tol = " 
00326                  << spec_.errorTol() << std::endl;
00327             return false;
00328           }
00329         else if (err > spec_.warningTol())
00330           {
00331             std::cerr << "WARNING: vector scalarMult test could not beat tol = " 
00332                  << spec_.warningTol() << std::endl;
00333           }
00334   
00335       }
00336     else
00337       {
00338         std::cerr << "skipping vector scalarMult test..." << std::endl;
00339       }
00340     std::cerr << "vector scalarMult test PASSED: tol = " 
00341          << spec_.errorTol() << std::endl;
00342     return true;
00343   }
00344  
00345   template <class Scalar> 
00346   inline bool VectorTester<Scalar>
00347   ::overloadedUpdateTest() const 
00348   {
00349     if (spec_.doTest())
00350       {
00351         std::cerr << "running vector overloadedUpdate test..." << std::endl;
00352 
00353         Vector<Scalar> a = space_.createMember();
00354         Vector<Scalar> b = space_.createMember();
00355         Vector<Scalar> x = space_.createMember();
00356         Vector<Scalar> y = space_.createMember();
00357         randomizeVec(a);
00358         randomizeVec(b);
00359 
00360 
00361         /* do the operation with member functions */
00362         x = 3.14*a + 1.4*b;
00363 
00364         /* do the operation elementwise */
00365         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00366           {
00367             y[i] = 3.14*a[i] + 1.4*b[i];
00368           }
00369 
00370         double err = (x-y).normInf();
00371 
00372         std::cerr << "|overloadedUpdate error|=" << err << std::endl;
00373         if (err > spec_.errorTol())
00374           {
00375             std::cerr << "vector overloadedUpdate test FAILED: tol = " 
00376                  << spec_.errorTol() << std::endl;
00377             return false;
00378           }
00379         else if (err > spec_.warningTol())
00380           {
00381             std::cerr << "WARNING: vector overloadedUpdate test could not beat tol = " 
00382                  << spec_.warningTol() << std::endl;
00383           }
00384   
00385       }
00386     else
00387       {
00388         std::cerr << "skipping vector overloadedUpdate test..." << std::endl;
00389       }
00390     std::cerr << "vector overloadedUpdate test PASSED: tol = " 
00391          << spec_.errorTol() << std::endl;
00392     return true;
00393   }
00394 
00395 
00396 
00397 }
00398 #endif

Site Contact