SFadUnitTests.hpp

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

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