Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfad_example.cpp
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 // dfad_example
11 //
12 // usage:
13 // dfad_example
14 //
15 // output:
16 // prints the results of differentiating a simple function with forward
17 // mode AD using the Sacado::Fad::DFad class (uses dynamic memory
18 // allocation for number of derivative components).
19 
20 #include <iostream>
21 #include <iomanip>
22 
23 #include "Sacado.hpp"
24 
25 // The function to differentiate
26 template <typename ScalarT>
27 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
28  ScalarT r = c*std::log(b+1.)/std::sin(a);
29 
30  return r;
31 }
32 
33 // The analytic derivative of func(a,b,c) with respect to a and b
34 void func_deriv(double a, double b, double c, double& drda, double& drdb)
35 {
36  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
37  drdb = c / ((b+1.)*std::sin(a));
38 }
39 
40 int main(int argc, char **argv)
41 {
42  double pi = std::atan(1.0)*4.0;
43 
44  // Values of function arguments
45  double a = pi/4;
46  double b = 2.0;
47  double c = 3.0;
48 
49  // Number of independent variables
50  int num_deriv = 2;
51 
52  // Fad objects
53  Sacado::Fad::DFad<double> afad(num_deriv, 0, a); // First (0) indep. var
54  Sacado::Fad::DFad<double> bfad(num_deriv, 1, b); // Second (1) indep. var
55  Sacado::Fad::DFad<double> cfad(c); // Passive variable
56  Sacado::Fad::DFad<double> rfad; // Result
57 
58  // Compute function
59  double r = func(a, b, c);
60 
61  // Compute derivative analytically
62  double drda, drdb;
63  func_deriv(a, b, c, drda, drdb);
64 
65  // Compute function and derivative with AD
66  rfad = func(afad, bfad, cfad);
67 
68  // Extract value and derivatives
69  double r_ad = rfad.val(); // r
70  double drda_ad = rfad.dx(0); // dr/da
71  double drdb_ad = rfad.dx(1); // dr/db
72 
73  // Print the results
74  int p = 4;
75  int w = p+7;
76  std::cout.setf(std::ios::scientific);
77  std::cout.precision(p);
78  std::cout << " r = " << r << " (original) == " << std::setw(w) << r_ad
79  << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
80  << "dr/da = " << std::setw(w) << drda << " (analytic) == "
81  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
82  << drda - drda_ad << std::endl
83  << "dr/db = " << std::setw(w) << drdb << " (analytic) == "
84  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
85  << drdb - drdb_ad << std::endl;
86 
87  double tol = 1.0e-14;
88  if (std::fabs(r - r_ad) < tol &&
89  std::fabs(drda - drda_ad) < tol &&
90  std::fabs(drdb - drdb_ad) < tol) {
91  std::cout << "\nExample passed!" << std::endl;
92  return 0;
93  }
94  else {
95  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
96  return 1;
97  }
98 }
const char * p
atan(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
int main()
Definition: ad_example.cpp:171
void func_deriv(double a, double b, double c, double &drda, double &drdb)
sin(expr.val())
log(expr.val())
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:29
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
fabs(expr.val())
cos(expr.val())