Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trad_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_example
11 //
12 // usage:
13 // trad_example
14 //
15 // output:
16 // prints the results of differentiating a simple function with reverse
17 // mode AD using the Sacado::Rad::ADvar class.
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 
29  return r;
30 }
31 
32 // The analytic derivative of func(a,b,c) with respect to a and b
33 void func_deriv(double a, double b, double c, double& drda, double& drdb)
34 {
35  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
36  drdb = c / ((b+1.)*std::sin(a));
37 }
38 
39 int main(int argc, char **argv)
40 {
41  double pi = std::atan(1.0)*4.0;
42 
43  // Values of function arguments
44  double a = pi/4;
45  double b = 2.0;
46  double c = 3.0;
47 
48  // Rad objects
51  Sacado::Rad::ADvar<double> crad = c; // Passive variable
52  Sacado::Rad::ADvar<double> rrad; // Result
53 
54  // Compute function
55  double r = func(a, b, c);
56 
57  // Compute derivative analytically
58  double drda, drdb;
59  func_deriv(a, b, c, drda, drdb);
60 
61  // Compute function and derivative with AD
62  rrad = func(arad, brad, crad);
63 
65 
66  // Extract value and derivatives
67  double r_ad = rrad.val(); // r
68  double drda_ad = arad.adj(); // dr/da
69  double drdb_ad = brad.adj(); // dr/db
70 
71  // Free Rad's memory to avoid memory leaks
73 
74  // Print the results
75  int p = 4;
76  int w = p+7;
77  std::cout.setf(std::ios::scientific);
78  std::cout.precision(p);
79  std::cout << " r = " << r << " (original) == " << std::setw(w) << r_ad
80  << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
81  << "dr/da = " << std::setw(w) << drda << " (analytic) == "
82  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
83  << drda - drda_ad << std::endl
84  << "dr/db = " << std::setw(w) << drdb << " (analytic) == "
85  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
86  << drdb - drdb_ad << std::endl;
87 
88  double tol = 1.0e-14;
89 
90  if (std::fabs(r - r_ad) < tol &&
91  std::fabs(drda - drda_ad) < tol &&
92  std::fabs(drdb - drdb_ad) < tol) {
93  std::cout << "\nExample passed!" << std::endl;
94  return 0;
95  }
96  else {
97  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
98  return 1;
99  }
100 }
const char * p
static void Gradcomp()
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())