30 #ifndef NESTED_FADUNITTESTS_HPP
31 #define NESTED_FADUNITTESTS_HPP
41 #include <cppunit/extensions/HelperMacros.h>
43 #define COMPARE_VALUES(a, b) \
44 CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
46 #define COMPARE_FADS(a, b) \
47 CPPUNIT_ASSERT(a.size() == b.size()); \
48 CPPUNIT_ASSERT(a.hasFastAccess() == b.hasFastAccess()); \
49 COMPARE_VALUES(a.val(), b.val()); \
50 for (int zz=0; zz<a.size(); zz++) { \
51 COMPARE_VALUES(a.dx(zz), b.dx(zz)); \
52 COMPARE_VALUES(a.fastAccessDx(zz), b.fastAccessDx(zz)); \
56 #define COMPARE_NESTED_FADS(a, b) \
57 CPPUNIT_ASSERT(a.size() == b.size()); \
58 CPPUNIT_ASSERT(a.hasFastAccess() == b.hasFastAccess()); \
59 COMPARE_FADS(a.val(), b.val()); \
60 for (int z=0; z<a.size(); z++) { \
61 COMPARE_FADS(a.dx(z), b.dx(z)); \
62 COMPARE_FADS(a.fastAccessDx(z), b.fastAccessDx(z)); \
66 #define BINARY_OP_TEST(TESTNAME,OP) \
68 c_dfad = a_dfad OP b_dfad; \
69 c_fad = a_fad OP b_fad; \
70 COMPARE_NESTED_FADS(c_dfad, c_fad); \
72 double val = urand.number(); \
73 c_dfad = a_dfad OP val; \
74 c_fad = a_fad OP FadType(val); \
75 COMPARE_NESTED_FADS(c_dfad, c_fad); \
77 c_dfad = val OP b_dfad; \
78 c_fad = FadType(val) OP b_fad; \
79 COMPARE_NESTED_FADS(c_dfad, c_fad); \
82 #define RELOP_TEST(TESTNAME,OP) \
84 bool r1 = a_dfad OP b_dfad; \
85 bool r2 = a_fad OP b_fad; \
86 CPPUNIT_ASSERT(r1 == r2); \
88 double val = urand.number(); \
90 r2 = a_fad OP FadType(val); \
91 CPPUNIT_ASSERT(r1 == r2); \
94 r2 = FadType(val) OP b_fad; \
95 CPPUNIT_ASSERT(r1 == r2); \
98 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
100 c_dfad = FUNC (a_dfad,b_dfad); \
101 c_fad = FUNC (a_fad,b_fad); \
102 COMPARE_NESTED_FADS(c_dfad, c_fad); \
104 double val = urand.number(); \
105 c_dfad = FUNC (a_dfad,val); \
106 c_fad = FUNC (a_fad,FadType(val)); \
107 COMPARE_NESTED_FADS(c_dfad, c_fad); \
109 c_dfad = FUNC (val,b_dfad); \
110 c_fad = FUNC (FadType(val),b_fad); \
111 COMPARE_NESTED_FADS(c_dfad, c_fad); \
114 #define UNARY_OP_TEST(TESTNAME,OP) \
116 c_dfad = OP a_dfad; \
118 COMPARE_NESTED_FADS(c_dfad, c_fad); \
121 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
123 c_dfad = FUNC (a_dfad); \
124 c_fad = FUNC (a_fad); \
125 COMPARE_NESTED_FADS(c_dfad, c_fad); \
128 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
132 COMPARE_NESTED_FADS(c_dfad, c_fad); \
134 double val = urand.number(); \
136 c_fad OP FadType(val); \
137 COMPARE_NESTED_FADS(c_dfad, c_fad); \
141 template <
class FadFadType,
class ScalarType>
199 typedef typename FadFadType::value_type
FadType;
204 ScalarType absolute_tolerance,
205 ScalarType relative_tolerance);
252 template <
typename ScalarT>
254 ScalarT t1 = 3. * a +
sin(b) /
log(
fabs(a - b * 7.));
260 t1 /=
cosh(b - 0.7) + 7.*
sinh(t1 + 0.8)*
tanh(9./a) - 9.;
266 template <
typename ScalarT>
287 FadFadType aa_dfad =
a_dfad;
288 FAD::Fad< FadType > aa_fad =
a_fad;
291 aa_dfad = aa_dfad +
b_dfad;
292 aa_fad = aa_fad +
b_fad;
297 FadFadType aa_dfad =
a_dfad;
298 FAD::Fad< FadType > aa_fad =
a_fad;
301 aa_dfad = aa_dfad -
b_dfad;
302 aa_fad = aa_fad -
b_fad;
307 FadFadType aa_dfad =
a_dfad;
308 FAD::Fad< FadType > aa_fad =
a_fad;
311 aa_dfad = aa_dfad *
b_dfad;
312 aa_fad = aa_fad *
b_fad;
317 FadFadType aa_dfad =
a_dfad;
318 FAD::Fad< FadType > aa_fad =
a_fad;
321 aa_dfad = aa_dfad /
b_dfad;
322 aa_fad = aa_fad /
b_fad;
328 FadFadType
a, b,
c, cc;
334 ScalarType
f =
pow(a.val().val(), b.val().val());
335 ScalarType fp = b.val().val()*
pow(a.val().val(),b.val().val()-1);
336 ScalarType fpp = b.val().val()*(b.val().val()-1)*
pow(a.val().val(),b.val().val()-2);
338 for (
int i=0; i<
n2; ++i)
339 cc.val().fastAccessDx(i) = fp*a.val().dx(i);
340 for (
int i=0; i<
n1; ++i) {
341 cc.fastAccessDx(i) =
FadType(n2,fp*a.dx(i).val());
342 for (
int j=0; j<
n2; ++j)
343 cc.fastAccessDx(i).fastAccessDx(j) = fpp*a.dx(i).val()*a.val().dx(j) + fp*a.dx(i).dx(j);
350 c =
pow(a, b.val().val());
357 for (
int i=0; i<
n1; ++i)
358 cc.fastAccessDx(i) =
FadType(n2,0.0);
364 for (
int i=0; i<
n1; ++i)
365 cc.fastAccessDx(i) =
FadType(n2,0.0);
367 c =
pow(a, b.val().val());
375 for (
int i=0; i<
n1; ++i)
376 cc.fastAccessDx(i) = 0.0;
381 for (
int i=0; i<
n1; ++i)
382 cc.fastAccessDx(i) = 0.0;
384 c =
pow(a, b.val().val());
391 for (
int i=0; i<
n1; ++i)
392 cc.fastAccessDx(i) = 0.0;
398 c =
pow(a, b.val().val());
424 template <
class FadFadType,
class ScalarType>
427 urand(0.0, 1.0, 123456), n1(5), n2(3), tol_a(1.0e-15), tol_r(1.0e-14) {}
429 template <
class FadFadType,
class ScalarType>
432 ScalarType absolute_tolerance,
433 ScalarType relative_tolerance) :
434 urand(0.0, 1.0, 123456),
437 tol_a(absolute_tolerance),
438 tol_r(relative_tolerance) {}
440 template <
class FadFadType,
class ScalarType>
444 val = urand.number();
445 a_dfad = FadFadType(n1,
FadType(n2,val));
446 a_fad = FAD::Fad<FadType>(n1,
FadType(n2,val));
448 val = urand.number();
449 b_dfad = FadFadType(n1,
FadType(n2,val));
450 b_fad = FAD::Fad<FadType>(n1,
FadType(n2,val));
452 for (
int j=0; j<n2; j++) {
454 val2 = urand.number();
455 a_dfad.val().fastAccessDx(j) = val2;
456 a_fad.val().fastAccessDx(j) = val2;
458 val2 = urand.number();
459 b_dfad.val().fastAccessDx(j) = val2;
460 b_fad.val().fastAccessDx(j) = val2;
463 for (
int i=0; i<n1; i++) {
464 val = urand.number();
465 a_dfad.fastAccessDx(i) =
FadType(n2,val);
466 a_fad.fastAccessDx(i) =
FadType(n2,val);
468 val = urand.number();
469 b_dfad.fastAccessDx(i) =
FadType(n2,val);
470 b_fad.fastAccessDx(i) =
FadType(n2,val);
472 for (
int j=0; j<n2; j++) {
474 val2 = urand.number();
475 a_dfad.fastAccessDx(i).fastAccessDx(j) = val2;
476 a_fad.fastAccessDx(i).fastAccessDx(j) = val2;
478 val2 = urand.number();
479 b_dfad.fastAccessDx(i).fastAccessDx(j) = val2;
480 b_fad.fastAccessDx(i).fastAccessDx(j) = val2;
485 template <
class FadFadType,
class ScalarType>
489 template <
class FadFadType,
class ScalarType>
494 FadFadType aa_dfad = a_dfad + 1.0;
495 c_dfad =
max(aa_dfad, a_dfad);
497 for (
int i=0; i<n1; i++) {
499 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
502 c_dfad =
max(a_dfad, aa_dfad);
504 for (
int i=0; i<n1; i++) {
506 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
509 c_dfad =
max(a_dfad+1.0, a_dfad);
511 for (
int i=0; i<n1; i++) {
513 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
516 c_dfad =
max(a_dfad, a_dfad+1.0);
518 for (
int i=0; i<n1; i++) {
520 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
523 val = a_dfad.val() + 1;
524 c_dfad =
max(a_dfad, val);
526 for (
int i=0; i<n1; i++) {
530 val = a_dfad.val() - 1;
531 c_dfad =
max(a_dfad, val);
533 for (
int i=0; i<n1; i++) {
535 COMPARE_FADS(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
538 val = b_dfad.val() + 1;
539 c_dfad =
max(val, b_dfad);
541 for (
int i=0; i<n1; i++) {
545 val = b_dfad.val() - 1;
546 c_dfad =
max(val, b_dfad);
548 for (
int i=0; i<n1; i++) {
550 COMPARE_FADS(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
554 template <
class FadFadType,
class ScalarType>
559 FadFadType aa_dfad = a_dfad - 1.0;
560 c_dfad =
min(aa_dfad, a_dfad);
562 for (
int i=0; i<n1; i++) {
564 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
567 c_dfad =
min(a_dfad, aa_dfad);
569 for (
int i=0; i<n1; i++) {
571 COMPARE_FADS(c_dfad.fastAccessDx(i), aa_dfad.fastAccessDx(i));
574 val = a_dfad.val() - 1;
575 c_dfad =
min(a_dfad, val);
577 for (
int i=0; i<n1; i++) {
581 val = a_dfad.val() + 1;
582 c_dfad =
min(a_dfad, val);
584 for (
int i=0; i<n1; i++) {
586 COMPARE_FADS(c_dfad.fastAccessDx(i), a_dfad.fastAccessDx(i));
589 val = b_dfad.val() - 1;
590 c_dfad =
min(val, b_dfad);
592 for (
int i=0; i<n1; i++) {
596 val = b_dfad.val() + 1;
597 c_dfad =
min(val, b_dfad);
599 for (
int i=0; i<n1; i++) {
601 COMPARE_FADS(c_dfad.fastAccessDx(i), b_dfad.fastAccessDx(i));
605 #undef COMPARE_VALUES
607 #undef COMPARE_NESTED_FADS
609 #endif // NESETD_FADUNITTESTS_HPP
ScalarT composite1(const ScalarT &a, const ScalarT &b)
#define COMPARE_FADS(a, b)
UNARY_OP_TEST(testUnaryPlus,+)
FAD::Fad< FadType > a_fad
Sacado::Fad::DFad< double > FadType
BINARY_FUNC_TEST(testPow, pow)
FAD::Fad< FadType > b_fad
KOKKOS_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
BINARY_OP_TEST(testAddition,+)
FAD::Fad< FadType > c_fad
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define COMPARE_NESTED_FADS(a, b)
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_TEST(testAddition)
UNARY_FUNC_TEST(testExp, exp)
Sacado::Random< ScalarType > urand
UNARY_ASSIGNOP_TEST(testPlusEquals,+=)
FadFadType::value_type FadType
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
ScalarT composite1_fad(const ScalarT &a, const ScalarT &b)
RELOP_TEST(testEquals,==)
CPPUNIT_TEST_SUITE(FadFadOpsUnitTest)