Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
30#include "Teuchos_UnitTestHarness.hpp"
31#include "Teuchos_UnitTestRepository.hpp"
32#include "Teuchos_GlobalMPISession.hpp"
33#include "Teuchos_TestingHelpers.hpp"
34
35#include "Sacado.hpp"
36
37const int N = 10;
38
39// Check whether the safe_sqrt() function works as expected
40TEUCHOS_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
73TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_DFadType )
74TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SLFadType )
75TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, Fad_SFadType )
76
77typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
78typedef Sacado::ELRFad::SLFad<double,N> ELRFad_SLFadType;
79typedef Sacado::ELRFad::SFad<double,N> ELRFad_SFadType;
80TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_DFadType )
81TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SLFadType )
82TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRFad_SFadType )
83
84typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
85typedef Sacado::CacheFad::SLFad<double,N> CacheFad_SLFadType;
86typedef Sacado::CacheFad::SFad<double,N> CacheFad_SFadType;
87TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_DFadType )
88TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SLFadType )
89TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, CacheFad_SFadType )
90
91typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
92typedef Sacado::ELRCacheFad::SLFad<double,N> ELRCacheFad_SLFadType;
93typedef Sacado::ELRCacheFad::SFad<double,N> ELRCacheFad_SFadType;
94TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_DFadType )
95TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SLFadType )
96TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ELRCacheFad_SFadType )
97
98#if defined(SACADO_ENABLE_NEW_DESIGN) && !defined(SACADO_NEW_FAD_DESIGN_IS_DEFAULT)
99typedef Sacado::Fad::Exp::DFad<double> ExpFad_DFadType;
100typedef Sacado::Fad::Exp::SLFad<double,N> ExpFad_SLFadType;
101typedef Sacado::Fad::Exp::SFad<double,N> ExpFad_SFadType;
102TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_DFadType )
103TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SLFadType )
104TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SafeSqrt, SafeSqrt, ExpFad_SFadType )
105#endif
106
107int main( int argc, char* argv[] ) {
108 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
109 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
110}
sqrt(expr.val())
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
Sacado::Fad::SLFad< double, N > Fad_SLFadType
Sacado::Fad::SFad< double, N > Fad_SFadType
Sacado::Fad::DFad< double > Fad_DFadType
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(SafeSqrt, SafeSqrt, AD)
const int N
int main()
Fad specializations for Teuchos::BLAS wrappers.
Uncopyable z
const double y
SACADO_INLINE_FUNCTION T safe_sqrt(const T &x)