HermiteUnitTests.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 HERMITEUNITTESTS_HPP
00033 #define HERMITEUNITTESTS_HPP
00034 
00035 // Sacado includes
00036 #include "Sacado.hpp"
00037 #include "Sacado_Random.hpp"
00038 #include "Sacado_PCE_OrthogPoly.hpp"
00039 #include "Stokhos.hpp"
00040 
00041 typedef Stokhos::HermiteBasis<int,double> basis_type;
00042 typedef Stokhos::DerivOrthogPolyExpansion<int,double> exp_type;
00043 typedef Sacado::PCE::OrthogPoly<double> pce_type;
00044 
00045 // Cppunit includes
00046 #include <cppunit/extensions/HelperMacros.h>
00047 
00048 #define BINARY_OP_TEST(TESTNAME,OP) \
00049   void TESTNAME () {        \
00050     cc = ac OP bc;        \
00051     c = a OP b;         \
00052     comparePCEs(cc, c);       \
00053             \
00054     cc = ac OP b;       \
00055     c = a OP b;         \
00056     comparePCEs(cc, c);       \
00057             \
00058     cc = a OP bc;       \
00059     c = a OP b;         \
00060     comparePCEs(cc, c);       \
00061   }
00062 
00063 #define RELOP_TEST(TESTNAME,OP)     \
00064   void TESTNAME () {        \
00065     bool r1 = ac OP bc;       \
00066     bool r2 = a OP b;       \
00067     CPPUNIT_ASSERT(r1 == r2);     \
00068             \
00069     r1 = ac OP b;             \
00070     r2 = a OP b;              \
00071     CPPUNIT_ASSERT(r1 == r2);     \
00072             \
00073     r1 = a OP bc;             \
00074     r2 = a OP b;              \
00075     CPPUNIT_ASSERT(r1 == r2);     \
00076   }
00077 
00078 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
00079   void TESTNAME () {      \
00080     cc = FUNC (ac, bc);     \
00081     c = FUNC (a, b);      \
00082     comparePCEs(cc, c);     \
00083               \
00084     cc = FUNC (ac, b);      \
00085     c = FUNC (a, b);      \
00086     comparePCEs(cc, c);     \
00087               \
00088     cc = FUNC (a, bc);      \
00089     c = FUNC (a, b);      \
00090     comparePCEs(cc, c);     \
00091   }
00092 
00093 #define UNARY_OP_TEST(TESTNAME,OP)      \
00094   void TESTNAME () {          \
00095     cc = OP ac;           \
00096     c = OP a;           \
00097     comparePCEs(cc, c);         \
00098   }
00099 
00100 #define UNARY_FUNC_TEST(TESTNAME,FUNC)      \
00101   void TESTNAME () {          \
00102     cc = FUNC (ac);         \
00103     c = FUNC (a);         \
00104     comparePCEs(cc, c);         \
00105   }
00106 
00107 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP)    \
00108   void TESTNAME () {          \
00109     cc OP ac;           \
00110     c OP a;           \
00111     comparePCEs(cc, c);         \
00112               \
00113     cc OP a;            \
00114     c OP a;           \
00115     comparePCEs(cc, c);         \
00116   }
00117 
00118 // A class for testing each DFad operation
00119 class HermiteUnitTest : public CppUnit::TestFixture {
00120 
00121   CPPUNIT_TEST_SUITE( HermiteUnitTest );
00122   
00123   CPPUNIT_TEST(testAddition);
00124   CPPUNIT_TEST(testSubtraction);
00125   CPPUNIT_TEST(testMultiplication);
00126   CPPUNIT_TEST(testDivision);
00127 
00128   CPPUNIT_TEST(testEquals);
00129   CPPUNIT_TEST(testNotEquals);
00130   CPPUNIT_TEST(testLessThanOrEquals);
00131   CPPUNIT_TEST(testGreaterThanOrEquals);
00132   CPPUNIT_TEST(testLessThan);
00133   CPPUNIT_TEST(testGreaterThan);
00134 
00135   CPPUNIT_TEST(testPow);
00136 
00137   CPPUNIT_TEST(testUnaryPlus);
00138   CPPUNIT_TEST(testUnaryMinus);
00139   
00140   CPPUNIT_TEST(testExp);
00141   CPPUNIT_TEST(testLog);
00142   CPPUNIT_TEST(testLog10);
00143   CPPUNIT_TEST(testSqrt);
00144   CPPUNIT_TEST(testCos);
00145   CPPUNIT_TEST(testSin);
00146   CPPUNIT_TEST(testTan);
00147   CPPUNIT_TEST(testACos);
00148   CPPUNIT_TEST(testASin);
00149   CPPUNIT_TEST(testATan);
00150   CPPUNIT_TEST(testCosh);
00151   CPPUNIT_TEST(testSinh);
00152   CPPUNIT_TEST(testTanh);
00153   CPPUNIT_TEST(testAbs);
00154   CPPUNIT_TEST(testFAbs);
00155 
00156   CPPUNIT_TEST(testPlusEquals);
00157   CPPUNIT_TEST(testMinusEquals);
00158   CPPUNIT_TEST(testTimesEquals);
00159   CPPUNIT_TEST(testDivideEquals);
00160 
00161   CPPUNIT_TEST(testComposite1);
00162 
00163   CPPUNIT_TEST_SUITE_END();
00164 
00165 public:
00166 
00167   HermiteUnitTest();
00168 
00169   HermiteUnitTest(double absolute_tolerance, 
00170       double relative_tolerance);
00171 
00172   void setUp();
00173 
00174   void tearDown();
00175 
00176   // Assert to PCE objects are the same
00177   void comparePCEs(const pce_type& xc, double x);
00178 
00179   // Assert to doubles are the same to relative precision
00180   void compareDoubles(double a, double b);
00181 
00182   BINARY_OP_TEST(testAddition, +);
00183   BINARY_OP_TEST(testSubtraction, -);
00184   BINARY_OP_TEST(testMultiplication, *);
00185   BINARY_OP_TEST(testDivision, /);
00186 
00187   RELOP_TEST(testEquals, ==);
00188   RELOP_TEST(testNotEquals, !=);
00189   RELOP_TEST(testLessThanOrEquals, <=);
00190   RELOP_TEST(testGreaterThanOrEquals, >=);
00191   RELOP_TEST(testLessThan, <);
00192   RELOP_TEST(testGreaterThan, >);
00193 
00194   BINARY_FUNC_TEST(testPow, pow);
00195 
00196   UNARY_OP_TEST(testUnaryPlus, +);
00197   UNARY_OP_TEST(testUnaryMinus, -);
00198 
00199   UNARY_FUNC_TEST(testExp, exp);
00200   UNARY_FUNC_TEST(testLog, log);
00201   UNARY_FUNC_TEST(testLog10, log10);
00202   UNARY_FUNC_TEST(testSqrt, sqrt);
00203   UNARY_FUNC_TEST(testCos, cos);
00204   UNARY_FUNC_TEST(testSin, sin);
00205   UNARY_FUNC_TEST(testTan, tan);
00206   UNARY_FUNC_TEST(testACos, acos);
00207   UNARY_FUNC_TEST(testASin, asin);
00208   UNARY_FUNC_TEST(testATan, atan);
00209   UNARY_FUNC_TEST(testCosh, cosh);
00210   UNARY_FUNC_TEST(testSinh, sinh);
00211   UNARY_FUNC_TEST(testTanh, tanh);
00212   UNARY_FUNC_TEST(testAbs, fabs);
00213   UNARY_FUNC_TEST(testFAbs, fabs);
00214 
00215   UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
00216   UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
00217   UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
00218   UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
00219 
00220   template <typename ScalarT>
00221   ScalarT composite1(const ScalarT& x, const ScalarT& y) {
00222     ScalarT t1 = 3. * x + sin(y) / log(fabs(x - y * 7.));
00223     ScalarT t2 = 1.0e3;
00224     ScalarT t3 = 5.7e4;
00225     ScalarT t4 = 3.2e5;
00226     t1 *= cos(x + exp(t1)) / 6. - tan(t1*sqrt(fabs(x * log10(fabs(y)))));
00227     t1 -= acos((6.+asin(pow(fabs(x),y)/t2))/t3) * asin(pow(fabs(y),2.)*1.0/t4) * atan((y*pow(2.,log(fabs(x))))/(t3*t4));
00228     t1 /= cosh(y - 0.7) + 7.*sinh(t1 + 0.8)*tanh((9./x)/1000.) - 9.;
00229     t1 += pow(fabs(x*4.),y-0.5)/cos(x*y*x);
00230     
00231   return t1;
00232 }
00233 
00234   void testComposite1() {
00235     cc = composite1(ac, bc);
00236     c = composite1(a, b);
00237     comparePCEs(cc, c);
00238   }
00239 
00240 protected:
00241 
00242   // PCE variables
00243   pce_type ac, bc, cc;
00244 
00245   // Scalars
00246   double a, b, c;
00247 
00248   // Random number generator
00249   Sacado::Random<double> urand;
00250 
00251   // Tolerances to which expansions should be the same
00252   double tol_a, tol_r;
00253 
00254   Teuchos::RCP<exp_type> expansion;
00255 
00256 }; // class HermiteUnitTest
00257 
00258 #endif // HERMITEUNITTESTS_HPP

Generated on Wed May 12 21:39:33 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7