Sacado Package Browser (Single Doxygen Collection)  Version of the Day
LogicalSparseUnitTests.hpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 #ifndef LOGICALSPARSEUNITTESTS_HPP
33 #define LOGICALSPARSEUNITTESTS_HPP
34 
35 // Sacado includes
36 #include "Sacado_No_Kokkos.hpp"
37 #include "Sacado_Random.hpp"
38 
41 
42 // Cppunit includes
43 #include <cppunit/extensions/HelperMacros.h>
44 
45 #define BINARY_OP_TEST(TESTNAME,OP) \
46  void TESTNAME () { \
47  c_dfad = a_dfad OP b_dfad; \
48  c_ls = a_ls OP b_ls; \
49  compareFads(c_dfad, c_ls); \
50  \
51  double val = urand.number(); \
52  c_dfad = a_dfad OP val; \
53  c_ls = a_ls OP val; \
54  compareFads(c_dfad, c_ls); \
55  \
56  c_dfad = val OP b_dfad; \
57  c_ls = val OP b_ls; \
58  compareFads(c_dfad, c_ls); \
59  }
60 
61 #define RELOP_TEST(TESTNAME,OP) \
62  void TESTNAME () { \
63  bool r1 = a_dfad OP b_dfad; \
64  bool r2 = a_ls OP b_ls; \
65  CPPUNIT_ASSERT(r1 == r2); \
66  \
67  double val = urand.number(); \
68  r1 = a_dfad OP val; \
69  r2 = a_ls OP val; \
70  CPPUNIT_ASSERT(r1 == r2); \
71  \
72  r1 = val OP b_dfad; \
73  r2 = val OP b_ls; \
74  CPPUNIT_ASSERT(r1 == r2); \
75  }
76 
77 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
78  void TESTNAME () { \
79  c_dfad = FUNC (a_dfad,b_dfad); \
80  c_ls = FUNC (a_ls,b_ls); \
81  compareFads(c_dfad, c_ls); \
82  \
83  double val = urand.number(); \
84  c_dfad = FUNC (a_dfad,val); \
85  c_ls = FUNC (a_ls,val); \
86  compareFads(c_dfad, c_ls); \
87  \
88  c_dfad = FUNC (val,b_dfad); \
89  c_ls = FUNC (val,b_ls); \
90  compareFads(c_dfad, c_ls); \
91  }
92 
93 #define UNARY_OP_TEST(TESTNAME,OP) \
94  void TESTNAME () { \
95  c_dfad = OP a_dfad; \
96  c_ls = OP a_ls; \
97  compareFads(c_dfad, c_ls); \
98  }
99 
100 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
101  void TESTNAME () { \
102  c_dfad = FUNC (a_dfad); \
103  c_ls = FUNC (a_ls); \
104  compareFads(c_dfad, c_ls); \
105  }
106 
107 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
108  void TESTNAME () { \
109  c_dfad OP a_dfad; \
110  c_ls OP a_ls; \
111  compareFads(c_dfad, c_ls); \
112  \
113  double val = urand.number(); \
114  c_dfad OP val; \
115  c_ls OP val; \
116  compareFads(c_dfad, c_ls); \
117  }
118 
119 // A class for testing each DFad operation
120 class LogicalSparseOpsUnitTest : public CppUnit::TestFixture {
121 
123 
124  CPPUNIT_TEST(testAddition);
125  CPPUNIT_TEST(testSubtraction);
126  CPPUNIT_TEST(testMultiplication);
127  CPPUNIT_TEST(testDivision);
128 
129  CPPUNIT_TEST(testEquals);
130  CPPUNIT_TEST(testNotEquals);
131  CPPUNIT_TEST(testLessThanOrEquals);
132  CPPUNIT_TEST(testGreaterThanOrEquals);
133  CPPUNIT_TEST(testLessThan);
134  CPPUNIT_TEST(testGreaterThan);
135 
136  CPPUNIT_TEST(testPow);
139 
140  CPPUNIT_TEST(testUnaryPlus);
141  CPPUNIT_TEST(testUnaryMinus);
142 
143  CPPUNIT_TEST(testExp);
144  CPPUNIT_TEST(testLog);
145  CPPUNIT_TEST(testLog10);
146  CPPUNIT_TEST(testSqrt);
147  CPPUNIT_TEST(testCos);
148  CPPUNIT_TEST(testSin);
149  CPPUNIT_TEST(testTan);
150  CPPUNIT_TEST(testACos);
151  CPPUNIT_TEST(testASin);
152  CPPUNIT_TEST(testATan);
153  CPPUNIT_TEST(testCosh);
154  CPPUNIT_TEST(testSinh);
155  CPPUNIT_TEST(testTanh);
156  CPPUNIT_TEST(testAbs);
157  CPPUNIT_TEST(testFAbs);
158 
159  CPPUNIT_TEST(testPlusEquals);
160  CPPUNIT_TEST(testMinusEquals);
161  CPPUNIT_TEST(testTimesEquals);
162  CPPUNIT_TEST(testDivideEquals);
163 
165 
170 
172 
173 public:
174 
176 
177  LogicalSparseOpsUnitTest(int numComponents, double absolute_tolerance,
178  double relative_tolerance);
179 
180  void setUp();
181 
182  void tearDown();
183 
184  // Assert to Fad objects are the same
185  void compareFads(const DFadType& x_dfad, const LSType& x_ls);
186 
187  // Assert two doubles are the same to relative precision
188  void compareDoubles(double a, double b);
189 
190  // Assert two bools are the same
191  void compareBools(bool a, bool b);
192 
193  // Assert a double and bool are same (logically)
194  void compareDx(double a, bool b);
195 
196  BINARY_OP_TEST(testAddition, +);
197  BINARY_OP_TEST(testSubtraction, -);
198  BINARY_OP_TEST(testMultiplication, *);
199  BINARY_OP_TEST(testDivision, /);
200 
201  RELOP_TEST(testEquals, ==);
202  RELOP_TEST(testNotEquals, !=);
203  RELOP_TEST(testLessThanOrEquals, <=);
204  RELOP_TEST(testGreaterThanOrEquals, >=);
205  RELOP_TEST(testLessThan, <);
206  RELOP_TEST(testGreaterThan, >);
207 
208  BINARY_FUNC_TEST(testPow, pow);
209 
210  UNARY_OP_TEST(testUnaryPlus, +);
211  UNARY_OP_TEST(testUnaryMinus, -);
212 
213  UNARY_FUNC_TEST(testExp, exp);
214  UNARY_FUNC_TEST(testLog, log);
215  UNARY_FUNC_TEST(testLog10, log10);
216  UNARY_FUNC_TEST(testSqrt, sqrt);
217  UNARY_FUNC_TEST(testCos, cos);
218  UNARY_FUNC_TEST(testSin, sin);
219  UNARY_FUNC_TEST(testTan, tan);
220  UNARY_FUNC_TEST(testACos, acos);
221  UNARY_FUNC_TEST(testASin, asin);
222  UNARY_FUNC_TEST(testATan, atan);
223  UNARY_FUNC_TEST(testCosh, cosh);
224  UNARY_FUNC_TEST(testSinh, sinh);
225  UNARY_FUNC_TEST(testTanh, tanh);
226  UNARY_FUNC_TEST(testAbs, abs);
227  UNARY_FUNC_TEST(testFAbs, fabs);
228 
229  UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
230  UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
231  UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
232  UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
233 
234  void testMax();
235  void testMin();
236 
237  template <typename ScalarT>
238  ScalarT composite1(const ScalarT& a, const ScalarT& b) {
239  ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
240  ScalarT t2 = 1.0e3;
241  ScalarT t3 = 5.7e4;
242  ScalarT t4 = 3.2e5;
243  t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(abs(a * log10(abs(b)))));
244  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));
245  t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./a) - 9.;
246  t1 += pow(abs(a*4.),b-8.)/cos(a*b*a);
247 
248  return t1;
249 }
250 
251  void testComposite1() {
253  c_ls = composite1(a_ls, b_ls);
255  }
256 
257  void testPlusLR() {
258  DFadType aa_dfad = a_dfad;
259  LSType aa_ls = a_ls;
260  aa_dfad = 1.0;
261  aa_ls = 1.0;
262  aa_dfad = aa_dfad + b_dfad;
263  aa_ls = aa_ls + b_ls;
264  compareFads(aa_dfad, aa_ls);
265  }
266 
267  void testMinusLR() {
268  DFadType aa_dfad = a_dfad;
269  LSType aa_ls = a_ls;
270  aa_dfad = 1.0;
271  aa_ls = 1.0;
272  aa_dfad = aa_dfad - b_dfad;
273  aa_ls = aa_ls - b_ls;
274  compareFads(aa_dfad, aa_ls);
275  }
276 
277  void testTimesLR() {
278  DFadType aa_dfad = a_dfad;
279  LSType aa_ls = a_ls;
280  aa_dfad = 2.0;
281  aa_ls = 2.0;
282  aa_dfad = aa_dfad * b_dfad;
283  aa_ls = aa_ls * b_ls;
284  compareFads(aa_dfad, aa_ls);
285  }
286 
287  void testDivideLR() {
288  DFadType aa_dfad = a_dfad;
289  LSType aa_ls = a_ls;
290  aa_dfad = 2.0;
291  aa_ls = 2.0;
292  aa_dfad = aa_dfad / b_dfad;
293  aa_ls = aa_ls / b_ls;
294  compareFads(aa_dfad, aa_ls);
295  }
296 
297 protected:
298 
299  // DFad variables
301 
302  // Logical sparse variables
304 
305  // Random number generator
307 
308  // Number of derivative components
309  int n;
310 
311  // Tolerances to which fad objects should be the same
312  double tol_a, tol_r;
313 
314 }; // class LogicalSparseOpsUnitTest
315 
316 #endif // LOGICALSPARSEUNITTESTS_HPP
void compareDoubles(double a, double b)
UNARY_OP_TEST(testUnaryPlus,+)
void compareDx(double a, bool b)
asin(expr.val())
cosh(expr.val())
abs(expr.val())
RELOP_TEST(testEquals,==)
Sacado::Fad::DFad< double > DFadType
pow(expr1.val(), expr2.val())
atan(expr.val())
CPPUNIT_TEST(testAddition)
CPPUNIT_TEST_SUITE(LogicalSparseOpsUnitTest)
ScalarT composite1(const ScalarT &a, const ScalarT &b)
tanh(expr.val())
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
sin(expr.val())
log(expr.val())
acos(expr.val())
UNARY_ASSIGNOP_TEST(testPlusEquals,+=)
Sacado::LFad::LogicalSparse< double, bool > LSType
UNARY_FUNC_TEST(testExp, exp)
exp(expr.val())
Sacado::Random< double > urand
fabs(expr.val())
BINARY_OP_TEST(testAddition,+)
BINARY_FUNC_TEST(testPow, pow)
void compareFads(const DFadType &x_dfad, const LSType &x_ls)
log10(expr.val())
cos(expr.val())