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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
34 
35 #include "Sacado.hpp"
36 
37 const int N = 10;
38 
39 // Check whether the safe_sqrt() function works as expected
40 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SafeSqrt, SafeSqrt, AD )
41 {
42  typedef AD ad_type;
43 
44  success = true;
45 
46  // Check non-zero value
47  ad_type x(N, 1.5);
48  for (int i=0; i<N; ++i)
49  x.fastAccessDx(i) = 2.0;
50  ad_type y = safe_sqrt(x);
51  ad_type z = sqrt(x);
52  TEST_EQUALITY( y.val(), z.val() );
53  for (int i=0; i<N; ++i)
54  TEST_EQUALITY( y.dx(i), z.dx(i) );
55 
56  // Check zero value
57  x.val() = 0.0;
58  y = safe_sqrt(x);
59  TEST_EQUALITY_CONST( y.val(), 0.0 );
60  for (int i=0; i<N; ++i)
61  TEST_EQUALITY_CONST( y.dx(i), 0.0 );
62 
63  // Check double
64  double a = 1.5;
65  double b = Sacado::safe_sqrt(a);
66  double c = std::sqrt(a);
67  TEST_EQUALITY( b, c );
68 }
69 
74 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SLFadType )
75 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SFadType )
76 
77 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
78 typedef Sacado::ELRFad::SLFad<double,N> ELRFad_SLFadType;
79 typedef Sacado::ELRFad::SFad<double,N> ELRFad_SFadType;
80 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_DFadType )
81 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SLFadType )
82 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SFadType )
83 
84 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
85 typedef Sacado::CacheFad::SLFad<double,N> CacheFad_SLFadType;
86 typedef Sacado::CacheFad::SFad<double,N> CacheFad_SFadType;
87 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_DFadType )
88 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SLFadType )
89 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SFadType )
90 
91 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
92 typedef Sacado::ELRCacheFad::SLFad<double,N> ELRCacheFad_SLFadType;
93 typedef Sacado::ELRCacheFad::SFad<double,N> ELRCacheFad_SFadType;
94 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_DFadType )
95 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SLFadType )
96 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SFadType )
97 
98 #if defined(SACADO_ENABLE_NEW_DESIGN) && !defined(SACADO_NEW_FAD_DESIGN_IS_DEFAULT)
99 typedef Sacado::Fad::Exp::DFad<double> ExpFad_DFadType;
100 typedef Sacado::Fad::Exp::SLFad<double,N> ExpFad_SLFadType;
101 typedef Sacado::Fad::Exp::SFad<double,N> ExpFad_SFadType;
102 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_DFadType )
103 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SLFadType )
104 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SFadType )
105 #endif
106 
107 int main( int argc, char* argv[] ) {
108  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
110 }
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)
KOKKOS_INLINE_FUNCTION T safe_sqrt(const T &x)
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:191
sqrt(expr.val())
const int N
#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