Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_GeneralFadTestingHelpers.hpp
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 //
29 // @HEADER
30 
31 #ifndef SACADO_FAD_GENERALFADTESTINGHELPERS_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 #ifdef HAVE_SACADO_TEUCHOSCORE
35 
36 #define SACADO_FAD_GENERALFADTESTINGHELPERS_HPP
37 
38 namespace Sacado {
39  namespace Fad {
40  // relErrFadImpl version for non-Fad types
41  template <typename T1, typename T2>
42  typename std::enable_if<
45  >::type
46  relErrFadImpl(const T1& v1, const T2& v2) { return Teuchos::relErr(v1,v2); }
47 
48  // relErrFadImpl version for Fad types
49  template <typename T1, typename T2>
50  typename std::enable_if<
52  typename Teuchos::ScalarTraits<
53  typename std::common_type< typename ScalarType<T1>::type, typename ScalarType<T2>::type >::type
54  >::magnitudeType
55  >::type
56  relErrFadImpl(const T1& v1, const T2& v2)
57  {
58  typedef typename Teuchos::ScalarTraits<typename std::common_type< typename ScalarType<T1>::type, typename ScalarType<T2>::type >::type>::magnitudeType magnitudeType;
59  magnitudeType maxRelErr = relErrFadImpl(v1.val(), v2.val());
60  for (int i=0; i<v1.size(); ++i) {
61  magnitudeType tmpRelErr = relErrFadImpl(v1.dx(i), v2.dx(i));
62  if (tmpRelErr >= maxRelErr)
63  maxRelErr = tmpRelErr;
64  }
65  return maxRelErr;
66  }
67  }
68 }
69 
70 namespace Teuchos {
71  template <typename T1, typename T2>
72  struct TestRelErr<T1,T2,
73  typename std::enable_if<Sacado::IsExpr<T1>::value ||
74  Sacado::IsExpr<T2>::value >::type >
75  {
76  typedef typename Sacado::ScalarType<T1>::type scalarType1;
77  typedef typename Sacado::ScalarType<T2>::type scalarType2;
78  typedef typename std::common_type<scalarType1,scalarType2>::type scalarType;
80  static bool eval(
81  const std::string &v1_name,
82  const T1 &vv1,
83  const std::string &v2_name,
84  const T2 &vv2,
85  const std::string &maxRelErr_error_name,
86  const magnitudeType &maxRelErr_error,
87  const std::string &maxRelErr_warning_name,
88  const magnitudeType &maxRelErr_warning,
89  const Ptr<std::ostream> &out
90  )
91  {
92  using std::endl;
94  typename Sacado::BaseExprType<T1>::type v1(vv1);
95  typename Sacado::BaseExprType<T2>::type v2(vv2);
96  const magnitudeType rel_err = Sacado::Fad::relErrFadImpl( v1, v2);
97  const bool success = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error)
98  && rel_err <= maxRelErr_error );
99  if (!is_null(out)) {
100  *out
101  << endl
102  << "Check: rel_err(" << v1_name << ", " << v2_name << ")\n"
103  << " = rel_err(" << v1 << ", " << v2 << ") "
104  << "= " << rel_err << endl
105  << " <= " << maxRelErr_error_name
106  << " = " << maxRelErr_error << " : " << Teuchos::passfail(success) << endl;
107  if( success && rel_err >= maxRelErr_warning ) {
108  *out
109  << "Warning! rel_err(" << v1_name << ", " << v2_name << ")\n"
110  << " = rel_err(" << v1 << ", " << v2 << ") "
111  << "= " << rel_err << endl
112  << " >= " << maxRelErr_warning_name
113  << " = " << maxRelErr_warning << "!\n";
114  }
115  }
116  return success;
117  }
118  };
119 }
120 
121 #define TEUCHOS_TEST_FLOATING_NOT_EQUALITY( v1, v2, tol, out, success ) \
122  { \
123  const bool l_result = Teuchos::testRelErr( \
124  #v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \
125  if (l_result) (success) = false; \
126  }
127 
128 #endif
129 
130 #endif
static bool eval(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
bool is_null(const std::shared_ptr< T > &p)
const std::string passfail(const bool result)
std::common_type< magType1, magType2 >::type magnitudeType
#define T2(r, f)
Definition: Sacado_rad.hpp:578
#define T1(r, f)
Definition: Sacado_rad.hpp:603
Teuchos::ScalarTraits< typename std::common_type< Scalar1, Scalar2 >::type >::magnitudeType relErr(const Scalar1 &s1, const Scalar2 &s2)
int value