Reference documentation for deal.II version 9.1.0-pre
adolc_number_types.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/config.h>
18 
19 #ifdef DEAL_II_WITH_ADOLC
20 
21 # include <deal.II/differentiation/ad/ad_number_traits.h>
22 # include <deal.II/differentiation/ad/adolc_number_types.h>
23 
24 # include <utility>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 
29 # ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
30 
31 namespace numbers
32 {
33  namespace internal
34  {
35  namespace
36  {
37  // Apply some comparator and extract the boolean result of the operation,
38  // instead of the "adub" return type tpyically returned by Adol-C for
39  // such a comparison. This is implemented as a general function so that
40  // the list of implemented comparative operations can be easily extended.
41  bool
42  adouble_boolean_comparator(
43  const adouble &value_1,
44  const adouble &value_2,
45  const std::function<adouble(const adouble &, const adouble &)>
46  &comparator)
47  {
48  using ad_type = typename Differentiation::AD::NumberTraits<
49  double,
51  static_assert(
52  std::is_same<adouble, ad_type>::value,
53  "The type of the AD number is not that which was expected.");
54  const ad_type result = comparator(value_1, value_2);
56  result) == 0.0);
57  }
58  } // namespace
59  } // namespace internal
60 
61  bool
62  values_are_equal(const adouble &value_1, const adouble &value_2)
63  {
64  return internal::adouble_boolean_comparator(
65  value_1, value_2, [](const adouble &a, const adouble &b) -> adouble {
66  return ::internal::NumberType<adouble>::value(a == b);
67  });
68  }
69 
70  bool
71  value_is_less_than(const adouble &value_1, const adouble &value_2)
72  {
73  return internal::adouble_boolean_comparator(
74  value_1, value_2, [](const adouble &a, const adouble &b) -> adouble {
75  return ::internal::NumberType<adouble>::value(a < b);
76  });
77  }
78 } // namespace numbers
79 
80 # endif
81 
82 
83 /*---------------------- Explicit Instantiations ----------------------*/
84 
85 # include "adolc_number_types.inst"
86 
87 DEAL_II_NAMESPACE_CLOSE
88 
89 #endif
bool values_are_equal(const Number1 &value_1, const Number2 &value_2)
Definition: numbers.h:786
bool value_is_less_than(const Number1 &value_1, const Number2 &value_2)
Definition: numbers.h:810