Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trad_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 // trad_dfad_example
11 //
12 // usage:
13 // trad_dfad_example
14 //
15 // output:
16 // prints the results of computing the second derivative a simple function // with forward nested forward and reverse mode AD using the
17 // Sacado::Fad::DFad and Sacado::Rad::ADvar classes.
18 
19 #include <iostream>
20 #include <iomanip>
21 
22 #include "Sacado_No_Kokkos.hpp"
23 
24 // The function to differentiate
25 template <typename ScalarT>
26 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
27  ScalarT r = c*std::log(b+1.)/std::sin(a);
28  return r;
29 }
30 
31 // The analytic derivative of func(a,b,c) with respect to a and b
32 void func_deriv(double a, double b, double c, double& drda, double& drdb)
33 {
34  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
35  drdb = c / ((b+1.)*std::sin(a));
36 }
37 
38 // The analytic second derivative of func(a,b,c) with respect to a and b
39 void func_deriv2(double a, double b, double c, double& d2rda2, double& d2rdb2,
40  double& d2rdadb)
41 {
42  d2rda2 = c*std::log(b+1.)/std::sin(a) + 2.*(c*std::log(b+1.)/std::pow(std::sin(a),3.))*std::pow(std::cos(a),2.);
43  d2rdb2 = -c / (std::pow(b+1.,2.)*std::sin(a));
44  d2rdadb = -c / ((b+1.)*std::pow(std::sin(a),2.))*std::cos(a);
45 }
46 
47 int main(int argc, char **argv)
48 {
49  double pi = std::atan(1.0)*4.0;
50 
51  // Values of function arguments
52  double a = pi/4;
53  double b = 2.0;
54  double c = 3.0;
55 
56  // Number of independent variables
57  int num_deriv = 2;
58 
59  // Fad objects
61  Sacado::Fad::DFad<double>(num_deriv, 0, a);
63  Sacado::Fad::DFad<double>(num_deriv, 1, b);
66 
67  // Compute function
68  double r = func(a, b, c);
69 
70  // Compute derivative analytically
71  double drda, drdb;
72  func_deriv(a, b, c, drda, drdb);
73 
74  // Compute second derivative analytically
75  double d2rda2, d2rdb2, d2rdadb;
76  func_deriv2(a, b, c, d2rda2, d2rdb2, d2rdadb);
77 
78  // Compute function and derivative with AD
79  rrad = func(arad, brad, crad);
80 
82 
83  // Extract value and derivatives
84  double r_ad = rrad.val().val(); // r
85  double drda_ad = arad.adj().val(); // dr/da
86  double drdb_ad = brad.adj().val(); // dr/db
87  double d2rda2_ad = arad.adj().dx(0); // d^2r/da^2
88  double d2rdadb_ad = arad.adj().dx(1); // d^2r/dadb
89  double d2rdbda_ad = brad.adj().dx(0); // d^2r/dbda
90  double d2rdb2_ad = brad.adj().dx(1); // d^2/db^2
91 
92  // Print the results
93  int p = 4;
94  int w = p+7;
95  std::cout.setf(std::ios::scientific);
96  std::cout.precision(p);
97  std::cout << " r = " << std::setw(w) << r << " (original) == "
98  << std::setw(w) << r_ad << " (AD) Error = " << std::setw(w)
99  << r - r_ad << std::endl
100  << " dr/da = " << std::setw(w) << drda << " (analytic) == "
101  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
102  << drda - drda_ad << std::endl
103  << " dr/db = " << std::setw(w) << drdb << " (analytic) == "
104  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
105  << drdb - drdb_ad << std::endl
106  << "d^2r/da^2 = " << std::setw(w) << d2rda2 << " (analytic) == "
107  << std::setw(w) << d2rda2_ad << " (AD) Error = " << std::setw(w)
108  << d2rda2 - d2rda2_ad << std::endl
109  << "d^2r/db^2 = " << std::setw(w) << d2rdb2 << " (analytic) == "
110  << std::setw(w) << d2rdb2_ad << " (AD) Error = " << std::setw(w)
111  << d2rdb2 - d2rdb2_ad << std::endl
112  << "d^2r/dadb = " << std::setw(w) << d2rdadb << " (analytic) == "
113  << std::setw(w) << d2rdadb_ad << " (AD) Error = " << std::setw(w)
114  << d2rdadb - d2rdadb_ad << std::endl
115  << "d^2r/dbda = " << std::setw(w) << d2rdadb << " (analytic) == "
116  << std::setw(w) << d2rdbda_ad << " (AD) Error = " << std::setw(w)
117  << d2rdadb - d2rdbda_ad << std::endl;
118 
119  // Free Rad's memory to avoid memory leaks. The zero_out() call is
120  // necessary to destroy dynamically allocated DFad arrays (which are
121  // stored outside of Rad's memory management).
124 
125  double tol = 1.0e-14;
126  if (std::fabs(r - r_ad) < tol &&
127  std::fabs(drda - drda_ad) < tol &&
128  std::fabs(drdb - drdb_ad) < tol &&
129  std::fabs(d2rda2 - d2rda2_ad) < tol &&
130  std::fabs(d2rdb2 - d2rdb2_ad) < tol &&
131  std::fabs(d2rdadb - d2rdadb_ad) < tol) {
132  std::cout << "\nExample passed!" << std::endl;
133  return 0;
134  }
135  else {
136  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
137  return 1;
138  }
139 }
const char * p
void func_deriv2(double a, double b, double c, double &d2rda2, double &d2rdb2, double &d2rdadb)
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())