Debian Codegraph @

include/deal.II/differentiation/ad/sacado_math.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2017 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_differentiation_ad_sacado_math_h
16 #define dealii_differentiation_ad_sacado_math_h
17
18 #include <deal.II/base/config.h>
19
20 #ifdef DEAL_II_TRILINOS_WITH_SACADO
21
22 # include <deal.II/base/numbers.h>
23
24 # include <deal.II/differentiation/ad/ad_number_traits.h>
25 # include <deal.II/differentiation/ad/sacado_number_types.h>
26
27
28 # ifndef DOXYGEN
29
30 DEAL_II_NAMESPACE_OPEN
31 /**
32 * Implementation of the error function for real-valued Sacado numbers.
33 */
34 template <
35 typename ADNumberType,
36 typename = std::enable_if_t<
37 dealii::Differentiation::AD::is_sacado_number<ADNumberType>::value &&
38 dealii::Differentiation::AD::is_real_valued_ad_number<ADNumberType>::value>>
39 inline ADNumberType
40 erf(ADNumberType x)
41 {
42 // Reference:
43 // Handbook of Mathematical Functions: with Formulas, Graphs, and
44 // Mathematical Tables Abramowitz, M. and Stegun, I. Dover Books on
45 // Mathematics. 1972.
46 //
47 // Current implementation source:
48 // https://www.johndcook.com/blog/cpp_erf/
49 // https://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
50 //
51 // Note: This implementation has a reported maximum round-off error
52 // of 1.5e-7.
53
54 // Constants
55 const double a1 = 0.254829592;
56 const double a2 = -0.284496736;
57 const double a3 = 1.421413741;
58 const double a4 = -1.453152027;
59 const double a5 = 1.061405429;
60 const double p = 0.3275911;
61
62 // Save the sign of x
63 const bool neg_val =
64 (x < dealii::internal::NumberType<ADNumberType>::value(0.0) ? true : false);
65 x = std::fabs(x);
66
67 // Abramowitz1972a equation 7.1.26
68 const ADNumberType t = 1.0 / (1.0 + p * x);
69 const ADNumberType y =
70 1.0 -
71 (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * std::exp(-x * x);
72
73 if (!neg_val)
74 return y;
75 else
76 return -y;
77 }
78
79
80 /**
81 * Implementation of the complementary error function for Sacado numbers.
82 */
83 template <typename ADNumberType,
84 typename = std::enable_if_t<
85 dealii::Differentiation::AD::is_sacado_number<ADNumberType>::value>>
86 inline ADNumberType
87 erfc(const ADNumberType &x)
88 {
89 return 1.0 - std::erf(x);
90 }
91
92 DEAL_II_NAMESPACE_CLOSE
93
94 # endif // DOXYGEN
95
96 #else
97
98 // Make sure the scripts that create the C++20 module input files have
99 // something to latch on if the preprocessor #ifdef above would
100 // otherwise lead to an empty content of the file.
101 DEAL_II_NAMESPACE_OPEN
102 DEAL_II_NAMESPACE_CLOSE
103
104 #endif // DEAL_II_TRILINOS_WITH_SACADO
105
106 #endif