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 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_FAD_GENERALFADTESTINGHELPERS_HPP
11 
12 #include "Sacado_ConfigDefs.h"
13 #ifdef HAVE_SACADO_TEUCHOSCORE
14 
15 #define SACADO_FAD_GENERALFADTESTINGHELPERS_HPP
16 
17 namespace Sacado {
18  namespace Fad {
19  // relErrFadImpl version for non-Fad types
20  template <typename T1, typename T2>
21  typename std::enable_if<
24  >::type
25  relErrFadImpl(const T1& v1, const T2& v2) { return Teuchos::relErr(v1,v2); }
26 
27  // relErrFadImpl version for Fad types
28  template <typename T1, typename T2>
29  typename std::enable_if<
31  typename Teuchos::ScalarTraits<
32  typename std::common_type< typename ScalarType<T1>::type, typename ScalarType<T2>::type >::type
33  >::magnitudeType
34  >::type
35  relErrFadImpl(const T1& v1, const T2& v2)
36  {
37  typedef typename Teuchos::ScalarTraits<typename std::common_type< typename ScalarType<T1>::type, typename ScalarType<T2>::type >::type>::magnitudeType magnitudeType;
38  magnitudeType maxRelErr = relErrFadImpl(v1.val(), v2.val());
39  for (int i=0; i<v1.size(); ++i) {
40  magnitudeType tmpRelErr = relErrFadImpl(v1.dx(i), v2.dx(i));
41  if (tmpRelErr >= maxRelErr)
42  maxRelErr = tmpRelErr;
43  }
44  return maxRelErr;
45  }
46  }
47 }
48 
49 namespace Teuchos {
50  template <typename T1, typename T2>
51  struct TestRelErr<T1,T2,
52  typename std::enable_if<Sacado::IsExpr<T1>::value ||
53  Sacado::IsExpr<T2>::value >::type >
54  {
55  typedef typename Sacado::ScalarType<T1>::type scalarType1;
56  typedef typename Sacado::ScalarType<T2>::type scalarType2;
57  typedef typename std::common_type<scalarType1,scalarType2>::type scalarType;
59  static bool eval(
60  const std::string &v1_name,
61  const T1 &vv1,
62  const std::string &v2_name,
63  const T2 &vv2,
64  const std::string &maxRelErr_error_name,
65  const magnitudeType &maxRelErr_error,
66  const std::string &maxRelErr_warning_name,
67  const magnitudeType &maxRelErr_warning,
68  const Ptr<std::ostream> &out
69  )
70  {
71  using std::endl;
73  typename Sacado::BaseExprType<T1>::type v1(vv1);
74  typename Sacado::BaseExprType<T2>::type v2(vv2);
75  const magnitudeType rel_err = Sacado::Fad::relErrFadImpl( v1, v2);
76  const bool success = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error)
77  && rel_err <= maxRelErr_error );
78  if (!is_null(out)) {
79  *out
80  << endl
81  << "Check: rel_err(" << v1_name << ", " << v2_name << ")\n"
82  << " = rel_err(" << v1 << ", " << v2 << ") "
83  << "= " << rel_err << endl
84  << " <= " << maxRelErr_error_name
85  << " = " << maxRelErr_error << " : " << Teuchos::passfail(success) << endl;
86  if( success && rel_err >= maxRelErr_warning ) {
87  *out
88  << "Warning! rel_err(" << v1_name << ", " << v2_name << ")\n"
89  << " = rel_err(" << v1 << ", " << v2 << ") "
90  << "= " << rel_err << endl
91  << " >= " << maxRelErr_warning_name
92  << " = " << maxRelErr_warning << "!\n";
93  }
94  }
95  return success;
96  }
97  };
98 }
99 
100 #define TEUCHOS_TEST_FLOATING_NOT_EQUALITY( v1, v2, tol, out, success ) \
101  { \
102  const bool l_result = Teuchos::testRelErr( \
103  #v1, v1, #v2, v2, "tol", tol, "tol", tol, Teuchos::outArg(out) ); \
104  if (l_result) (success) = false; \
105  }
106 
107 #endif
108 
109 #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:558
#define T1(r, f)
Definition: Sacado_rad.hpp:583
Teuchos::ScalarTraits< typename std::common_type< Scalar1, Scalar2 >::type >::magnitudeType relErr(const Scalar1 &s1, const Scalar2 &s2)
int value