Sacado Package Browser (Single Doxygen Collection)  Version of the Day
FadUnitTests.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 FADUNITTESTS_HPP
33 #define FADUNITTESTS_HPP
34 
35 // Sacado includes
36 #include "Sacado_No_Kokkos.hpp"
37 #include "Sacado_Random.hpp"
38 
39 // Fad includes
40 #include "Fad/fad.h"
41 
42 // Cppunit includes
43 #include <cppunit/extensions/HelperMacros.h>
44 
45 #define COMPARE_VALUES(a, b) \
46  CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
47 
48 #define COMPARE_FADS(a, b) \
49 CPPUNIT_ASSERT(a.size() == b.size()); \
50 CPPUNIT_ASSERT(a.hasFastAccess() == b.hasFastAccess()); \
51 COMPARE_VALUES(a.val(), b.val()); \
52 for (int i=0; i<a.size(); i++) { \
53  COMPARE_VALUES(a.dx(i), b.dx(i)); \
54  COMPARE_VALUES(a.fastAccessDx(i), b.fastAccessDx(i)); \
55  } \
56  ;
57 
58 #define BINARY_OP_TEST(TESTNAME,OP) \
59  void TESTNAME () { \
60  c_dfad = a_dfad OP b_dfad; \
61  c_fad = a_fad OP b_fad; \
62  COMPARE_FADS(c_dfad, c_fad); \
63  \
64  double val = urand.number(); \
65  c_dfad = a_dfad OP val; \
66  c_fad = a_fad OP val; \
67  COMPARE_FADS(c_dfad, c_fad); \
68  \
69  c_dfad = val OP b_dfad; \
70  c_fad = val OP b_fad; \
71  COMPARE_FADS(c_dfad, c_fad); \
72  }
73 
74 #define RELOP_TEST(TESTNAME,OP) \
75  void TESTNAME () { \
76  bool r1 = a_dfad OP b_dfad; \
77  bool r2 = a_fad OP b_fad; \
78  CPPUNIT_ASSERT(r1 == r2); \
79  \
80  double val = urand.number(); \
81  r1 = a_dfad OP val; \
82  r2 = a_fad OP val; \
83  CPPUNIT_ASSERT(r1 == r2); \
84  \
85  r1 = val OP b_dfad; \
86  r2 = val OP b_fad; \
87  CPPUNIT_ASSERT(r1 == r2); \
88  }
89 
90 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
91  void TESTNAME () { \
92  c_dfad = FUNC (a_dfad,b_dfad); \
93  c_fad = FUNC (a_fad,b_fad); \
94  COMPARE_FADS(c_dfad, c_fad); \
95  \
96  double val = urand.number(); \
97  c_dfad = FUNC (a_dfad,val); \
98  c_fad = FUNC (a_fad,val); \
99  COMPARE_FADS(c_dfad, c_fad); \
100  \
101  c_dfad = FUNC (val,b_dfad); \
102  c_fad = FUNC (val,b_fad); \
103  COMPARE_FADS(c_dfad, c_fad); \
104  }
105 
106 #define UNARY_OP_TEST(TESTNAME,OP) \
107  void TESTNAME () { \
108  c_dfad = OP a_dfad; \
109  c_fad = OP a_fad; \
110  COMPARE_FADS(c_dfad, c_fad); \
111  }
112 
113 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
114  void TESTNAME () { \
115  c_dfad = FUNC (a_dfad); \
116  c_fad = FUNC (a_fad); \
117  COMPARE_FADS(c_dfad, c_fad); \
118  }
119 
120 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
121  void TESTNAME () { \
122  c_dfad OP a_dfad; \
123  c_fad OP a_fad; \
124  COMPARE_FADS(c_dfad, c_fad); \
125  \
126  double val = urand.number(); \
127  c_dfad OP val; \
128  c_fad OP val; \
129  COMPARE_FADS(c_dfad, c_fad); \
130  }
131 
132 // A class for testing each Fad operation
133 template <class FadType, class ScalarType>
134 class FadOpsUnitTest : public CppUnit::TestFixture {
135 
137 
138  CPPUNIT_TEST(testAddition);
139  CPPUNIT_TEST(testSubtraction);
140  CPPUNIT_TEST(testMultiplication);
141  CPPUNIT_TEST(testDivision);
142 
143  CPPUNIT_TEST(testEquals);
144  CPPUNIT_TEST(testNotEquals);
145  CPPUNIT_TEST(testLessThanOrEquals);
146  CPPUNIT_TEST(testGreaterThanOrEquals);
147  CPPUNIT_TEST(testLessThan);
148  CPPUNIT_TEST(testGreaterThan);
149 
150  CPPUNIT_TEST(testPow);
153 
154  CPPUNIT_TEST(testUnaryPlus);
155  CPPUNIT_TEST(testUnaryMinus);
156 
157  CPPUNIT_TEST(testExp);
158  CPPUNIT_TEST(testLog);
159  CPPUNIT_TEST(testLog10);
160  CPPUNIT_TEST(testSqrt);
161  CPPUNIT_TEST(testCos);
162  CPPUNIT_TEST(testSin);
163  CPPUNIT_TEST(testTan);
164  CPPUNIT_TEST(testACos);
165  CPPUNIT_TEST(testASin);
166  CPPUNIT_TEST(testATan);
167  CPPUNIT_TEST(testCosh);
168  CPPUNIT_TEST(testSinh);
169  CPPUNIT_TEST(testTanh);
170  CPPUNIT_TEST(testAbs);
171  CPPUNIT_TEST(testFAbs);
172 
173  CPPUNIT_TEST(testPlusEquals);
174  CPPUNIT_TEST(testMinusEquals);
175  CPPUNIT_TEST(testTimesEquals);
176  CPPUNIT_TEST(testDivideEquals);
177 
179 
184 
186 
187 public:
188 
189  FadOpsUnitTest();
190 
191  FadOpsUnitTest(int numComponents, ScalarType absolute_tolerance,
192  ScalarType relative_tolerance);
193 
194  void setUp();
195 
196  void tearDown();
197 
198  BINARY_OP_TEST(testAddition, +);
199  BINARY_OP_TEST(testSubtraction, -);
200  BINARY_OP_TEST(testMultiplication, *);
201  BINARY_OP_TEST(testDivision, /);
202 
203  RELOP_TEST(testEquals, ==);
204  RELOP_TEST(testNotEquals, !=);
205  RELOP_TEST(testLessThanOrEquals, <=);
206  RELOP_TEST(testGreaterThanOrEquals, >=);
207  RELOP_TEST(testLessThan, <);
208  RELOP_TEST(testGreaterThan, >);
209 
210  BINARY_FUNC_TEST(testPow, pow);
211 
212  UNARY_OP_TEST(testUnaryPlus, +);
213  UNARY_OP_TEST(testUnaryMinus, -);
214 
215  UNARY_FUNC_TEST(testExp, exp);
216  UNARY_FUNC_TEST(testLog, log);
217  UNARY_FUNC_TEST(testLog10, log10);
218  UNARY_FUNC_TEST(testSqrt, sqrt);
219  UNARY_FUNC_TEST(testCos, cos);
220  UNARY_FUNC_TEST(testSin, sin);
221  UNARY_FUNC_TEST(testTan, tan);
222  UNARY_FUNC_TEST(testACos, acos);
223  UNARY_FUNC_TEST(testASin, asin);
224  UNARY_FUNC_TEST(testATan, atan);
225  UNARY_FUNC_TEST(testCosh, cosh);
226  UNARY_FUNC_TEST(testSinh, sinh);
227  UNARY_FUNC_TEST(testTanh, tanh);
228  UNARY_FUNC_TEST(testAbs, abs);
229  UNARY_FUNC_TEST(testFAbs, fabs);
230 
231  UNARY_ASSIGNOP_TEST(testPlusEquals, +=);
232  UNARY_ASSIGNOP_TEST(testMinusEquals, -=);
233  UNARY_ASSIGNOP_TEST(testTimesEquals, *=);
234  UNARY_ASSIGNOP_TEST(testDivideEquals, /=);
235 
236  void testMax();
237  void testMin();
238 
239  template <typename ScalarT>
240  ScalarT composite1(const ScalarT& a, const ScalarT& b) {
241  ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
242  ScalarT t2 = 1.0e3;
243  ScalarT t3 = 5.7e4;
244  ScalarT t4 = 3.2e5;
245  t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(abs(a * log10(abs(b)))));
246  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));
247  t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./a) - 9.;
248  t1 += pow(abs(a*4.),b-8.)/cos(a*b*a);
249 
250  return t1;
251 }
252 
253  void testComposite1() {
257  }
258 
259  void testPlusLR() {
260  FadType aa_dfad = a_dfad;
261  FAD::Fad<ScalarType> aa_fad = a_fad;
262  aa_dfad = 1.0;
263  aa_fad = 1.0;
264  aa_dfad = aa_dfad + b_dfad;
265  aa_fad = aa_fad + b_fad;
266  COMPARE_FADS(aa_dfad, aa_fad);
267  }
268 
269  void testMinusLR() {
270  FadType aa_dfad = a_dfad;
271  FAD::Fad<ScalarType> aa_fad = a_fad;
272  aa_dfad = 1.0;
273  aa_fad = 1.0;
274  aa_dfad = aa_dfad - b_dfad;
275  aa_fad = aa_fad - b_fad;
276  COMPARE_FADS(aa_dfad, aa_fad);
277  }
278 
279  void testTimesLR() {
280  FadType aa_dfad = a_dfad;
281  FAD::Fad<ScalarType> aa_fad = a_fad;
282  aa_dfad = 2.0;
283  aa_fad = 2.0;
284  aa_dfad = aa_dfad * b_dfad;
285  aa_fad = aa_fad * b_fad;
286  COMPARE_FADS(aa_dfad, aa_fad);
287  }
288 
289  void testDivideLR() {
290  FadType aa_dfad = a_dfad;
291  FAD::Fad<ScalarType> aa_fad = a_fad;
292  aa_dfad = 2.0;
293  aa_fad = 2.0;
294  aa_dfad = aa_dfad / b_dfad;
295  aa_fad = aa_fad / b_fad;
296  COMPARE_FADS(aa_dfad, aa_fad);
297  }
298 
299 protected:
300 
301  // DFad variables
303 
304  // Fad variables
305  FAD::Fad<ScalarType> a_fad, b_fad, c_fad;
306 
307  // Random number generator
309 
310  // Number of derivative components
311  int n;
312 
313  // Tolerances to which fad objects should be the same
314  ScalarType tol_a, tol_r;
315 
316 }; // class FadOpsUnitTest
317 
318 template <class FadType, class ScalarType>
321  urand(), n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
322 
323 template <class FadType, class ScalarType>
325 FadOpsUnitTest(int numComponents, ScalarType absolute_tolerance,
326  ScalarType relative_tolerance) :
327  urand(),
328  n(numComponents),
329  tol_a(absolute_tolerance),
330  tol_r(relative_tolerance) {}
331 
332 template <class FadType, class ScalarType>
334  ScalarType val;
335 
336  val = urand.number();
337  a_dfad = FadType(n,val);
338  a_fad = FAD::Fad<ScalarType>(n,val);
339 
340  val = urand.number();
341  b_dfad = FadType(n,val);
342  b_fad = FAD::Fad<ScalarType>(n,val);
343 
344  for (int i=0; i<n; i++) {
345  val = urand.number();
346  a_dfad.fastAccessDx(i) = val;
347  a_fad.fastAccessDx(i) = val;
348 
349  val = urand.number();
350  b_dfad.fastAccessDx(i) = val;
351  b_fad.fastAccessDx(i) = val;
352  }
353 }
354 
355 template <class FadType, class ScalarType>
358 
359 template <class FadType, class ScalarType>
362  ScalarType val;
363 
364  FadType aa_dfad = a_dfad + 1.0;
365  c_dfad = max(aa_dfad, a_dfad);
366  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
367  for (int i=0; i<n; i++) {
368  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
369  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
370  }
371 
372  c_dfad = max(a_dfad, aa_dfad);
373  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
374  for (int i=0; i<n; i++) {
375  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
376  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
377  }
378 
379  c_dfad = max(a_dfad+1.0, a_dfad);
380  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
381  for (int i=0; i<n; i++) {
382  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
383  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
384  }
385 
386  c_dfad = max(a_dfad, a_dfad+1.0);
387  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
388  for (int i=0; i<n; i++) {
389  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
390  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
391  }
392 
393  val = a_dfad.val() + 1;
394  c_dfad = max(a_dfad, val);
395  COMPARE_VALUES(c_dfad.val(), val);
396  for (int i=0; i<n; i++)
397  COMPARE_VALUES(c_dfad.dx(i), 0.0);
398 
399  val = a_dfad.val() - 1;
400  c_dfad = max(a_dfad, val);
401  COMPARE_VALUES(c_dfad.val(), a_dfad.val());
402  for (int i=0; i<n; i++) {
403  COMPARE_VALUES(c_dfad.dx(i), a_dfad.dx(i));
404  COMPARE_VALUES(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
405  }
406 
407  val = b_dfad.val() + 1;
408  c_dfad = max(val, b_dfad);
409  COMPARE_VALUES(c_dfad.val(), val);
410  for (int i=0; i<n; i++)
411  COMPARE_VALUES(c_dfad.dx(i), 0.0);
412 
413  val = b_dfad.val() - 1;
414  c_dfad = max(val, b_dfad);
415  COMPARE_VALUES(c_dfad.val(), b_dfad.val());
416  for (int i=0; i<n; i++) {
417  COMPARE_VALUES(c_dfad.dx(i), b_dfad.dx(i));
418  COMPARE_VALUES(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
419  }
420 }
421 
422 template <class FadType, class ScalarType>
425  ScalarType val;
426 
427  FadType aa_dfad = a_dfad - 1.0;
428  c_dfad = min(aa_dfad, a_dfad);
429  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
430  for (int i=0; i<n; i++) {
431  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
432  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
433  }
434 
435  c_dfad = min(a_dfad, aa_dfad);
436  COMPARE_VALUES(c_dfad.val(), aa_dfad.val());
437  for (int i=0; i<n; i++) {
438  COMPARE_VALUES(c_dfad.dx(i), aa_dfad.dx(i));
439  COMPARE_VALUES(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
440  }
441 
442  val = a_dfad.val() - 1;
443  c_dfad = min(a_dfad, val);
444  COMPARE_VALUES(c_dfad.val(), val);
445  for (int i=0; i<n; i++)
446  COMPARE_VALUES(c_dfad.dx(i), 0.0);
447 
448  val = a_dfad.val() + 1;
449  c_dfad = min(a_dfad, val);
450  COMPARE_VALUES(c_dfad.val(), a_dfad.val());
451  for (int i=0; i<n; i++) {
452  COMPARE_VALUES(c_dfad.dx(i), a_dfad.dx(i));
453  COMPARE_VALUES(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
454  }
455 
456  val = b_dfad.val() - 1;
457  c_dfad = min(val, b_dfad);
458  COMPARE_VALUES(c_dfad.val(), val);
459  for (int i=0; i<n; i++)
460  COMPARE_VALUES(c_dfad.dx(i), 0.0);
461 
462  val = b_dfad.val() + 1;
463  c_dfad = min(val, b_dfad);
464  COMPARE_VALUES(c_dfad.val(), b_dfad.val());
465  for (int i=0; i<n; i++) {
466  COMPARE_VALUES(c_dfad.dx(i), b_dfad.dx(i));
467  COMPARE_VALUES(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
468  }
469 }
470 
471 #undef COMPARE_VALUES
472 #undef COMPARE_FADS
473 
474 #endif // FADUNITTESTS_HPP
RELOP_TEST(testEquals,==)
expr val()
UNARY_FUNC_TEST(testExp, exp)
Sacado::Fad::DFad< double > FadType
SimpleFad< ValueT > sqrt(const SimpleFad< ValueT > &a)
ScalarType tol_r
SimpleFad< ValueT > asin(const SimpleFad< ValueT > &a)
BINARY_OP_TEST(testAddition,+)
ScalarT composite1(const ScalarT &a, const ScalarT &b)
SimpleFad< ValueT > log(const SimpleFad< ValueT > &a)
UNARY_ASSIGNOP_TEST(testPlusEquals,+=)
SimpleFad< ValueT > exp(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > atan(const SimpleFad< ValueT > &a)
#define COMPARE_FADS(a, b)
SimpleFad< ValueT > log10(const SimpleFad< ValueT > &a)
CPPUNIT_TEST(testAddition)
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
SimpleFad< ValueT > sin(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > sinh(const SimpleFad< ValueT > &a)
FAD::Fad< ScalarType > b_fad
FAD::Fad< ScalarType > a_fad
FAD::Fad< ScalarType > c_fad
SimpleFad< ValueT > pow(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
SimpleFad< ValueT > tan(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > cos(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > cosh(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
ScalarType tol_a
KOKKOS_INLINE_FUNCTION Expr< AbsOp< Expr< T > > > abs(const Expr< T > &expr)
KOKKOS_INLINE_FUNCTION Expr< FAbsOp< Expr< T > > > fabs(const Expr< T > &expr)
CPPUNIT_TEST_SUITE(FadOpsUnitTest)
BINARY_FUNC_TEST(testPow, pow)
Sacado::Random< ScalarType > urand
#define COMPARE_VALUES(a, b)
SimpleFad< ValueT > acos(const SimpleFad< ValueT > &a)
SimpleFad< ValueT > tanh(const SimpleFad< ValueT > &a)
int n
UNARY_OP_TEST(testUnaryPlus,+)