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