Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SafeSqrtTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Sacado.hpp"
16 
17 const int N = 10;
18 
19 // Check whether the safe_sqrt() function works as expected
20 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SafeSqrt, SafeSqrt, AD )
21 {
22  typedef AD ad_type;
23 
24  success = true;
25 
26  // Check non-zero value
27  ad_type x(N, 1.5);
28  for (int i=0; i<N; ++i)
29  x.fastAccessDx(i) = 2.0;
30  ad_type y = safe_sqrt(x);
31  ad_type z = sqrt(x);
32  TEST_EQUALITY( y.val(), z.val() );
33  for (int i=0; i<N; ++i)
34  TEST_EQUALITY( y.dx(i), z.dx(i) );
35 
36  // Check zero value
37  x.val() = 0.0;
38  y = safe_sqrt(x);
39  TEST_EQUALITY_CONST( y.val(), 0.0 );
40  for (int i=0; i<N; ++i)
41  TEST_EQUALITY_CONST( y.dx(i), 0.0 );
42 
43  // Check double
44  double a = 1.5;
45  double b = Sacado::safe_sqrt(a);
46  double c = std::sqrt(a);
47  TEST_EQUALITY( b, c );
48 }
49 
54 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SLFadType )
55 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SFadType )
56 
57 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
58 typedef Sacado::ELRFad::SLFad<double,N> ELRFad_SLFadType;
59 typedef Sacado::ELRFad::SFad<double,N> ELRFad_SFadType;
60 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_DFadType )
61 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SLFadType )
62 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SFadType )
63 
64 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
65 typedef Sacado::CacheFad::SLFad<double,N> CacheFad_SLFadType;
66 typedef Sacado::CacheFad::SFad<double,N> CacheFad_SFadType;
67 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_DFadType )
68 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SLFadType )
69 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SFadType )
70 
71 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
72 typedef Sacado::ELRCacheFad::SLFad<double,N> ELRCacheFad_SLFadType;
73 typedef Sacado::ELRCacheFad::SFad<double,N> ELRCacheFad_SFadType;
74 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_DFadType )
75 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SLFadType )
76 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SFadType )
77 
78 #if defined(SACADO_ENABLE_NEW_DESIGN) && !defined(SACADO_NEW_FAD_DESIGN_IS_DEFAULT)
79 typedef Sacado::Fad::Exp::DFad<double> ExpFad_DFadType;
80 typedef Sacado::Fad::Exp::SLFad<double,N> ExpFad_SLFadType;
81 typedef Sacado::Fad::Exp::SFad<double,N> ExpFad_SFadType;
82 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_DFadType )
83 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SLFadType )
84 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SFadType )
85 #endif
86 
87 int main( int argc, char* argv[] ) {
88  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
90 }
Sacado::CacheFad::SLFad< double, 10 > Fad_SLFadType
#define TEST_EQUALITY_CONST(v1, v2)
Sacado::CacheFad::DFad< double > Fad_DFadType
GeneralFad< StaticStorage< T, Num > > SLFad
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(ConditionalReturnType, Fad, FAD)
static int runUnitTestsFromMain(int argc, char *argv[])
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
GeneralFad< DynamicStorage< T > > DFad
Forward-mode AD class templated on the storage for the derivative array.
int main()
Definition: ad_example.cpp:171
sqrt(expr.val())
const int N
Uncopyable z
SACADO_INLINE_FUNCTION T safe_sqrt(const T &x)
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(TEST_GROUP, TEST_NAME, TYPE)
#define TEST_EQUALITY(v1, v2)
GeneralFad< StaticFixedStorage< T, Num > > SFad
Sacado::CacheFad::SFad< double, 5 > Fad_SFadType
const double y