ELRDFadUnitTests.hpp

Go to the documentation of this file.
00001 // $Id: ELRDFadUnitTests.hpp,v 1.1.2.1 2007/08/14 00:19:08 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/test/TestSuite/ELRDFadUnitTests.hpp,v $ 
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 ELRDFADUNITTESTS_HPP
00033 #define ELRDFADUNITTESTS_HPP
00034 
00035 // Sacado includes
00036 #include "Sacado.hpp"
00037 #include "Sacado_Random.hpp"
00038 
00039 typedef Sacado::ELRFad::DFad<double> DFadType;
00040 
00041 // Fad includes
00042 #include "Fad/fad.h"
00043 
00044 // Cppunit includes
00045 #include <cppunit/extensions/HelperMacros.h>
00046 
00047 #define BINARY_OP_TEST(TESTNAME,OP) \
00048   void TESTNAME () {        \
00049     c_dfad = a_dfad OP b_dfad;      \
00050     c_fad = a_fad OP b_fad;     \
00051     compareFads(c_dfad, c_fad);     \
00052             \
00053     double val = urand.number();    \
00054     c_dfad = a_dfad OP val;     \
00055     c_fad = a_fad OP val;     \
00056     compareFads(c_dfad, c_fad);     \
00057             \
00058     c_dfad = val OP b_dfad;     \
00059     c_fad = val OP b_fad;     \
00060     compareFads(c_dfad, c_fad);     \
00061   }
00062 
00063 #define RELOP_TEST(TESTNAME,OP)     \
00064   void TESTNAME () {        \
00065     bool r1 = a_dfad OP b_dfad;     \
00066     bool r2 = a_fad OP b_fad;     \
00067     CPPUNIT_ASSERT(r1 == r2);     \
00068             \
00069     double val = urand.number();    \
00070     r1 = a_dfad OP val;             \
00071     r2 = a_fad OP val;              \
00072     CPPUNIT_ASSERT(r1 == r2);     \
00073             \
00074     r1 = val OP b_dfad;             \
00075     r2 = val OP b_fad;              \
00076     CPPUNIT_ASSERT(r1 == r2);     \
00077   }
00078 
00079 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
00080   void TESTNAME () {      \
00081     c_dfad = FUNC (a_dfad,b_dfad);  \
00082     c_fad = FUNC (a_fad,b_fad);   \
00083     compareFads(c_dfad, c_fad);   \
00084               \
00085     double val = urand.number();  \
00086     c_dfad = FUNC (a_dfad,val);   \
00087     c_fad = FUNC (a_fad,val);   \
00088     compareFads(c_dfad, c_fad);   \
00089               \
00090     c_dfad = FUNC (val,b_dfad);   \
00091     c_fad = FUNC (val,b_fad);   \
00092     compareFads(c_dfad, c_fad);   \
00093   }
00094 
00095 #define UNARY_OP_TEST(TESTNAME,OP)      \
00096   void TESTNAME () {          \
00097     c_dfad = OP a_dfad;         \
00098     c_fad = OP a_fad;         \
00099     compareFads(c_dfad, c_fad);       \
00100   }
00101 
00102 #define UNARY_FUNC_TEST(TESTNAME,FUNC)      \
00103   void TESTNAME () {          \
00104     c_dfad = FUNC (a_dfad);       \
00105     c_fad = FUNC (a_fad);       \
00106     compareFads(c_dfad, c_fad);       \
00107   }
00108 
00109 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP)    \
00110   void TESTNAME () {          \
00111     c_dfad OP a_dfad;         \
00112     c_fad OP a_fad;         \
00113     compareFads(c_dfad, c_fad);       \
00114               \
00115     double val = urand.number();      \
00116     c_dfad OP val;          \
00117     c_fad OP val;         \
00118     compareFads(c_dfad, c_fad);       \
00119   }
00120 
00121 // A class for testing each DFad operation
00122 class ELRDFadOpsUnitTest : public CppUnit::TestFixture {
00123 
00124   CPPUNIT_TEST_SUITE( ELRDFadOpsUnitTest );
00125   
00126   CPPUNIT_TEST(testAddition);
00127   CPPUNIT_TEST(testSubtraction);
00128   CPPUNIT_TEST(testMultiplication);
00129   CPPUNIT_TEST(testDivision);
00130 
00131   CPPUNIT_TEST(testEquals);
00132   CPPUNIT_TEST(testNotEquals);
00133   CPPUNIT_TEST(testLessThanOrEquals);
00134   CPPUNIT_TEST(testGreaterThanOrEquals);
00135   CPPUNIT_TEST(testLessThan);
00136   CPPUNIT_TEST(testGreaterThan);
00137 
00138   CPPUNIT_TEST(testPow);
00139   CPPUNIT_TEST(testMax);
00140   CPPUNIT_TEST(testMin);
00141 
00142   CPPUNIT_TEST(testUnaryPlus);
00143   CPPUNIT_TEST(testUnaryMinus);
00144   
00145   CPPUNIT_TEST(testExp);
00146   CPPUNIT_TEST(testLog);
00147   CPPUNIT_TEST(testLog10);
00148   CPPUNIT_TEST(testSqrt);
00149   CPPUNIT_TEST(testCos);
00150   CPPUNIT_TEST(testSin);
00151   CPPUNIT_TEST(testTan);
00152   CPPUNIT_TEST(testACos);
00153   CPPUNIT_TEST(testASin);
00154   CPPUNIT_TEST(testATan);
00155   CPPUNIT_TEST(testCosh);
00156   CPPUNIT_TEST(testSinh);
00157   CPPUNIT_TEST(testTanh);
00158   CPPUNIT_TEST(testAbs);
00159   CPPUNIT_TEST(testFAbs);
00160 
00161   CPPUNIT_TEST(testPlusEquals);
00162   CPPUNIT_TEST(testMinusEquals);
00163   CPPUNIT_TEST(testTimesEquals);
00164   CPPUNIT_TEST(testDivideEquals);
00165 
00166   CPPUNIT_TEST(testComposite1);
00167 
00168   CPPUNIT_TEST(testPlusLR);
00169   CPPUNIT_TEST(testMinusLR);
00170   CPPUNIT_TEST(testTimesLR);
00171   CPPUNIT_TEST(testDivideLR);
00172 
00173   CPPUNIT_TEST_SUITE_END();
00174 
00175 public:
00176 
00177   ELRDFadOpsUnitTest();
00178 
00179   ELRDFadOpsUnitTest(int numComponents, double absolute_tolerance, 
00180          double relative_tolerance);
00181 
00182   void setUp();
00183 
00184   void tearDown();
00185 
00186   // Assert to Fad objects are the same
00187   void compareFads(const DFadType& x_dfad,
00188        const FAD::Fad<double>& x_fad);
00189 
00190   // Assert to doubles are the same to relative precision
00191   void compareDoubles(double a, double b);
00192 
00193   BINARY_OP_TEST(testAddition, +);
00194   BINARY_OP_TEST(testSubtraction, -);
00195   BINARY_OP_TEST(testMultiplication, *);
00196   BINARY_OP_TEST(testDivision, /);
00197 
00198   RELOP_TEST(testEquals, ==);
00199   RELOP_TEST(testNotEquals, !=);
00200   RELOP_TEST(testLessThanOrEquals, <=);
00201   RELOP_TEST(testGreaterThanOrEquals, >=);
00202   RELOP_TEST(testLessThan, <);
00203   RELOP_TEST(testGreaterThan, >);
00204 
00205   BINARY_FUNC_TEST(testPow, pow);
00206 
00207   UNARY_OP_TEST(testUnaryPlus, +);
00208   UNARY_OP_TEST(testUnaryMinus, -);
00209 
00210   UNARY_FUNC_TEST(testExp, exp);
00211   UNARY_FUNC_TEST(testLog, log);
00212   UNARY_FUNC_TEST(testLog10, log10);
00213   UNARY_FUNC_TEST(testSqrt, sqrt);
00214   UNARY_FUNC_TEST(testCos, cos);
00215   UNARY_FUNC_TEST(testSin, sin);
00216   UNARY_FUNC_TEST(testTan, tan);
00217   UNARY_FUNC_TEST(testACos, acos);
00218   UNARY_FUNC_TEST(testASin, asin);
00219   UNARY_FUNC_TEST(testATan, atan);
00220   UNARY_FUNC_TEST(testCosh, cosh);
00221   UNARY_FUNC_TEST(testSinh, sinh);
00222   UNARY_FUNC_TEST(testTanh, tanh);
00223   UNARY_FUNC_TEST(testAbs, abs);
00224   UNARY_FUNC_TEST(testFAbs, fabs);
00225 
00226   UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
00227   UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
00228   UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
00229   UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
00230 
00231   void testMax();
00232   void testMin();
00233 
00234   template <typename ScalarT>
00235   ScalarT composite1(const ScalarT& a, const ScalarT& b) {
00236     ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
00237     ScalarT t2 = 1.0e3;
00238     ScalarT t3 = 5.7e4;
00239     ScalarT t4 = 3.2e5;
00240     t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(abs(a * log10(abs(b)))));
00241     t1 -= acos((6.+asin(pow(fabs(a),b)/t2))/t3) * asin(pow(fabs(b),2.)*1.0/t4) * atan((b*pow(2.,log(abs(a))))/(t3*t4));
00242     t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./a) - 9.;
00243     t1 += pow(abs(a*4.),b-8.)/cos(a*b*a);
00244     
00245   return t1;
00246 }
00247 
00248   void testComposite1() {
00249     c_dfad = composite1(a_dfad, b_dfad);
00250     c_fad = composite1(a_fad, b_fad);
00251     compareFads(c_dfad, c_fad);
00252   }
00253 
00254   void testPlusLR() {
00255     DFadType aa_dfad = a_dfad;
00256     FAD::Fad<double> aa_fad = a_fad;
00257     aa_dfad = 1.0;
00258     aa_fad = 1.0;
00259     aa_dfad = aa_dfad + b_dfad;
00260     aa_fad = aa_fad + b_fad;
00261     compareFads(aa_dfad, aa_fad);
00262   }
00263 
00264   void testMinusLR() {
00265     DFadType aa_dfad = a_dfad;
00266     FAD::Fad<double> aa_fad = a_fad;
00267     aa_dfad = 1.0;
00268     aa_fad = 1.0;
00269     aa_dfad = aa_dfad - b_dfad;
00270     aa_fad = aa_fad - b_fad;
00271     compareFads(aa_dfad, aa_fad);
00272   }
00273 
00274   void testTimesLR() {
00275     DFadType aa_dfad = a_dfad;
00276     FAD::Fad<double> aa_fad = a_fad;
00277     aa_dfad = 2.0;
00278     aa_fad = 2.0;
00279     aa_dfad = aa_dfad * b_dfad;
00280     aa_fad = aa_fad * b_fad;
00281     compareFads(aa_dfad, aa_fad);
00282   }
00283 
00284   void testDivideLR() {
00285     DFadType aa_dfad = a_dfad;
00286     FAD::Fad<double> aa_fad = a_fad;
00287     aa_dfad = 2.0;
00288     aa_fad = 2.0;
00289     aa_dfad = aa_dfad / b_dfad;
00290     aa_fad = aa_fad / b_fad;
00291     compareFads(aa_dfad, aa_fad);
00292   }
00293 
00294 protected:
00295 
00296   // DFad variables
00297   DFadType a_dfad, b_dfad, c_dfad;
00298 
00299   // Fad variables
00300   FAD::Fad<double> a_fad, b_fad, c_fad;
00301 
00302   // Random number generator
00303   Sacado::Random urand;
00304 
00305   // Number of derivative components
00306   int n;
00307 
00308   // Tolerances to which fad objects should be the same
00309   double tol_a, tol_r;
00310 
00311 }; // class ELRDFadOpsUnitTest
00312 
00313 #endif // ELRDFADUNITTESTS_HPP

Generated on Tue Oct 20 12:55:02 2009 for Sacado Package Browser (Single Doxygen Collection) by doxygen 1.4.7