32 #ifndef FADUNITTESTS_HPP
33 #define FADUNITTESTS_HPP
43 #include <cppunit/extensions/HelperMacros.h>
45 #define COMPARE_VALUES(a, b) \
46 CPPUNIT_ASSERT( std::abs(a-b) < this->tol_a + this->tol_r*std::abs(a) );
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)); \
58 #define BINARY_OP_TEST(TESTNAME,OP) \
60 c_dfad = a_dfad OP b_dfad; \
61 c_fad = a_fad OP b_fad; \
62 COMPARE_FADS(c_dfad, c_fad); \
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); \
69 c_dfad = val OP b_dfad; \
70 c_fad = val OP b_fad; \
71 COMPARE_FADS(c_dfad, c_fad); \
74 #define RELOP_TEST(TESTNAME,OP) \
76 bool r1 = a_dfad OP b_dfad; \
77 bool r2 = a_fad OP b_fad; \
78 CPPUNIT_ASSERT(r1 == r2); \
80 double val = urand.number(); \
83 CPPUNIT_ASSERT(r1 == r2); \
87 CPPUNIT_ASSERT(r1 == r2); \
90 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
92 c_dfad = FUNC (a_dfad,b_dfad); \
93 c_fad = FUNC (a_fad,b_fad); \
94 COMPARE_FADS(c_dfad, c_fad); \
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); \
101 c_dfad = FUNC (val,b_dfad); \
102 c_fad = FUNC (val,b_fad); \
103 COMPARE_FADS(c_dfad, c_fad); \
106 #define UNARY_OP_TEST(TESTNAME,OP) \
108 c_dfad = OP a_dfad; \
110 COMPARE_FADS(c_dfad, c_fad); \
113 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
115 c_dfad = FUNC (a_dfad); \
116 c_fad = FUNC (a_fad); \
117 COMPARE_FADS(c_dfad, c_fad); \
120 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
124 COMPARE_FADS(c_dfad, c_fad); \
126 double val = urand.number(); \
129 COMPARE_FADS(c_dfad, c_fad); \
133 template <
class FadType,
class ScalarType>
194 ScalarType relative_tolerance);
241 template <
typename ScalarT>
243 ScalarT t1 = 3. * a +
sin(b) /
log(
fabs(a - b * 7.));
249 t1 /=
cosh(b - 0.7) + 7.*
sinh(t1 + 0.8)*
tanh(9./a) - 9.;
263 FAD::Fad<ScalarType> aa_fad =
a_fad;
266 aa_dfad = aa_dfad +
b_dfad;
267 aa_fad = aa_fad +
b_fad;
273 FAD::Fad<ScalarType> aa_fad =
a_fad;
276 aa_dfad = aa_dfad -
b_dfad;
277 aa_fad = aa_fad -
b_fad;
283 FAD::Fad<ScalarType> aa_fad =
a_fad;
286 aa_dfad = aa_dfad *
b_dfad;
287 aa_fad = aa_fad *
b_fad;
293 FAD::Fad<ScalarType> aa_fad =
a_fad;
296 aa_dfad = aa_dfad /
b_dfad;
297 aa_fad = aa_fad /
b_fad;
307 for (
int i=0; i<
n; ++i)
312 for (
int i=0; i<
n; ++i)
313 cc.fastAccessDx(i) = b.val()*
pow(a.val(),b.val()-1)*a.dx(i);
324 for (
int i=0; i<
n; ++i)
325 cc.fastAccessDx(i) = 0.0;
337 for (
int i=0; i<
n; ++i)
338 cc.fastAccessDx(i) = 0.0;
349 for (
int i=0; i<
n; ++i)
350 cc.fastAccessDx(i) = 0.0;
377 template <
class FadType,
class ScalarType>
380 urand(),
n(5), tol_a(1.0e-15), tol_r(1.0e-12) {}
382 template <
class FadType,
class ScalarType>
385 ScalarType relative_tolerance) :
388 tol_a(absolute_tolerance),
389 tol_r(relative_tolerance) {}
391 template <
class FadType,
class ScalarType>
395 val = urand.number();
397 a_fad = FAD::Fad<ScalarType>(
n,
val);
399 val = urand.number();
401 b_fad = FAD::Fad<ScalarType>(n,
val);
403 for (
int i=0; i<n; i++) {
404 val = urand.number();
405 a_dfad.fastAccessDx(i) =
val;
406 a_fad.fastAccessDx(i) =
val;
408 val = urand.number();
409 b_dfad.fastAccessDx(i) =
val;
410 b_fad.fastAccessDx(i) =
val;
414 template <
class FadType,
class ScalarType>
418 template <
class FadType,
class ScalarType>
423 FadType aa_dfad = a_dfad + 1.0;
424 c_dfad =
max(aa_dfad, a_dfad);
426 for (
int i=0; i<
n; i++) {
431 c_dfad =
max(a_dfad, aa_dfad);
433 for (
int i=0; i<n; i++) {
438 c_dfad =
max(a_dfad+1.0, a_dfad);
440 for (
int i=0; i<n; i++) {
445 c_dfad =
max(a_dfad, a_dfad+1.0);
447 for (
int i=0; i<n; i++) {
452 val = a_dfad.val() + 1;
453 c_dfad =
max(a_dfad, val);
455 for (
int i=0; i<n; i++)
458 val = a_dfad.val() - 1;
459 c_dfad =
max(a_dfad, val);
461 for (
int i=0; i<n; i++) {
466 val = b_dfad.val() + 1;
467 c_dfad =
max(val, b_dfad);
469 for (
int i=0; i<n; i++)
472 val = b_dfad.val() - 1;
473 c_dfad =
max(val, b_dfad);
475 for (
int i=0; i<n; i++) {
481 template <
class FadType,
class ScalarType>
486 FadType aa_dfad = a_dfad - 1.0;
487 c_dfad =
min(aa_dfad, a_dfad);
489 for (
int i=0; i<
n; i++) {
494 c_dfad =
min(a_dfad, aa_dfad);
496 for (
int i=0; i<n; i++) {
501 val = a_dfad.val() - 1;
502 c_dfad =
min(a_dfad, val);
504 for (
int i=0; i<n; i++)
507 val = a_dfad.val() + 1;
508 c_dfad =
min(a_dfad, val);
510 for (
int i=0; i<n; i++) {
515 val = b_dfad.val() - 1;
516 c_dfad =
min(val, b_dfad);
518 for (
int i=0; i<n; i++)
521 val = b_dfad.val() + 1;
522 c_dfad =
min(val, b_dfad);
524 for (
int i=0; i<n; i++) {
530 #undef COMPARE_VALUES
533 #endif // FADUNITTESTS_HPP
RELOP_TEST(testEquals,==)
UNARY_FUNC_TEST(testExp, exp)
Sacado::Fad::DFad< double > FadType
ScalarT number()
Get random number.
BINARY_OP_TEST(testAddition,+)
ScalarT composite1(const ScalarT &a, const ScalarT &b)
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)
UNARY_ASSIGNOP_TEST(testPlusEquals,+=)
#define COMPARE_FADS(a, b)
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
CPPUNIT_TEST(testAddition)
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
FAD::Fad< ScalarType > b_fad
FAD::Fad< ScalarType > a_fad
FAD::Fad< ScalarType > c_fad
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
CPPUNIT_TEST_SUITE(FadOpsUnitTest)
BINARY_FUNC_TEST(testPow, pow)
Sacado::Random< ScalarType > urand
#define COMPARE_VALUES(a, b)
UNARY_OP_TEST(testUnaryPlus,+)