Sacado Package Browser (Single Doxygen Collection) Version of the Day
FadLAPACKUnitTests.hpp
Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #ifndef FADLAPACKUNITTESTS_HPP
00033 #define FADLAPACKUNITTESTS_HPP
00034 
00035 // Sacado includes
00036 #include "Sacado.hpp"
00037 #include "Sacado_Fad_LAPACK.hpp"
00038 #include "Sacado_Random.hpp"
00039 
00040 // Cppunit includes
00041 #include <cppunit/extensions/HelperMacros.h>
00042 
00043 #define COMPARE_VALUES(a, b) \
00044   CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
00045 
00046 #define COMPARE_FADS(a, b)                              \
00047 CPPUNIT_ASSERT(a.size() == b.size());     \
00048 CPPUNIT_ASSERT(a.hasFastAccess() == b.hasFastAccess()); \
00049 COMPARE_VALUES(a.val(), b.val());     \
00050 for (int k=0; k<a.size(); k++) {      \
00051   COMPARE_VALUES(a.dx(k), b.dx(k));     \
00052   COMPARE_VALUES(a.fastAccessDx(k), b.fastAccessDx(k)); \
00053  }              \
00054  ;
00055 
00056 #define COMPARE_FAD_VECTORS(X1, X2, n)    \
00057   CPPUNIT_ASSERT(X1.size() == std::size_t(n));  \
00058   CPPUNIT_ASSERT(X2.size() == std::size_t(n));  \
00059   for (unsigned int i=0; i<n; i++) {    \
00060     COMPARE_FADS(X1[i], X2[i]);     \
00061   }           \
00062   ;
00063 
00064 // A class for testing differentiated LAPACK operations for general Fad types
00065 template <class FadType, class ScalarType>
00066 class FadLAPACKUnitTests : public CppUnit::TestFixture {
00067 
00068   typedef Sacado::Fad::Vector<unsigned int,FadType> VectorType;
00069 
00070   CPPUNIT_TEST_SUITE( FadLAPACKUnitTests );
00071 
00072   CPPUNIT_TEST(testGESV);
00073 
00074   CPPUNIT_TEST_SUITE_END();
00075 
00076 public:
00077 
00078   FadLAPACKUnitTests();
00079 
00080   FadLAPACKUnitTests(int m, int n, int l, int ndot, 
00081                      double absolute_tolerance, double relative_tolerance);
00082 
00083   void setUp();
00084   void tearDown();
00085 
00086   void testGESV();
00087 
00088 protected:
00089 
00090   // Random number generator
00091   Sacado::Random<ScalarType> urand;
00092 
00093   // Real random number generator for derivative components
00094   Sacado::Random<double> real_urand;
00095 
00096   // Number of matrix rows
00097   unsigned int m;
00098 
00099   // Number of matrix columns
00100   unsigned int n;
00101 
00102   // Number of matrix columns for level 3 blas
00103   unsigned int l;
00104 
00105   // Number of derivative components
00106   unsigned int ndot;
00107 
00108   // Tolerances to which fad objects should be the same
00109   double tol_a, tol_r;
00110 
00111 }; // class FadLAPACKUnitTests
00112 
00113 template <class FadType, class ScalarType>
00114 FadLAPACKUnitTests<FadType,ScalarType>::
00115 FadLAPACKUnitTests() :
00116   urand(), real_urand(), m(5), n(6), l(4), ndot(7), tol_a(1.0e-11), tol_r(1.0e-11) {}
00117 
00118 template <class FadType, class ScalarType>
00119 FadLAPACKUnitTests<FadType,ScalarType>::
00120 FadLAPACKUnitTests(int m_, int n_, int l_, int ndot_, double absolute_tolerance, 
00121                    double relative_tolerance) :
00122   urand(),
00123   real_urand(),
00124   m(m_),
00125   n(n_),
00126   l(l_),
00127   ndot(ndot_), 
00128   tol_a(absolute_tolerance), 
00129   tol_r(relative_tolerance) {}
00130 
00131 template <class FadType, class ScalarType>
00132 void FadLAPACKUnitTests<FadType,ScalarType>::
00133 setUp() {}
00134 
00135 template <class FadType, class ScalarType>
00136 void FadLAPACKUnitTests<FadType,ScalarType>::
00137 tearDown() {}
00138 
00139 // Tests all arguments
00140 template <class FadType, class ScalarType>
00141 void
00142 FadLAPACKUnitTests<FadType,ScalarType>::
00143 testGESV() {
00144 
00145   const int n = 2;
00146   const int nrhs = 1;
00147   double A[] = { 1.1, 0.1, .01, 0.9 };
00148   const int lda = 2;
00149   int IPIV[] = {0, 0};
00150   double B[] = { 0.1, 0.2 };
00151   const int ldb = 2;
00152   int info(0);
00153 
00154   const double refX[] = {0.088978766430738, 0.212335692618807};
00155 
00156   Teuchos::LAPACK<int,double> teuchos_lapack;
00157   teuchos_lapack.GESV(n, nrhs, &A[0], lda, &IPIV[0], &B[0], ldb, &info);
00158 
00159   COMPARE_VALUES(B[0],refX[0]);
00160   COMPARE_VALUES(B[1],refX[1]);
00161 
00162   //Teuchos::LAPACK<int,FadType> sacado_lapack(false);
00163   //sacado_blas.SCAL(m, alpha, &x2[0], 1);
00164   //COMPARE_VALUES(1,0);
00165 }
00166 
00167 #undef COMPARE_VALUES
00168 #undef COMPARE_FADS
00169 #undef COMPARE_FAD_VECTORS
00170 
00171 #endif // FADLAPACKUNITTESTS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines