Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sfad_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 // sfad_example
11 //
12 // usage:
13 // sfad_example
14 //
15 // output:
16 // prints the results of differentiating a simple function with forward
17 // mode AD using the Sacado::Fad::SFad class (uses static memory allocation
18 // for the number of derivative components, meaning this must be known
19 // at compile time.
20 
21 #include <iostream>
22 #include <iomanip>
23 
24 #include "Sacado.hpp"
25 
26 // The function to differentiate
27 template <typename ScalarT>
28 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
29  ScalarT r = c*std::log(b+1.)/std::sin(a);
30 
31  return r;
32 }
33 
34 // The analytic derivative of func(a,b,c) with respect to a and b
35 void func_deriv(double a, double b, double c, double& drda, double& drdb)
36 {
37  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
38  drdb = c / ((b+1.)*std::sin(a));
39 }
40 
41 int main(int argc, char **argv)
42 {
43  double pi = std::atan(1.0)*4.0;
44 
45  // Values of function arguments
46  double a = pi/4;
47  double b = 2.0;
48  double c = 3.0;
49 
50  // Number of independent variables
51  int num_deriv = 2; // Must be == 2 (see below)
52 
53  // Fad objects
54  Sacado::Fad::SFad<double,2> afad(num_deriv, 0, a); // First (0) indep. var
55  Sacado::Fad::SFad<double,2> bfad(num_deriv, 1, b); // Second (1) indep. var
56  Sacado::Fad::SFad<double,2> cfad(c); // Passive variable
57  Sacado::Fad::SFad<double,2> rfad; // Result
58 
59  // Compute function
60  double r = func(a, b, c);
61 
62  // Compute derivative analytically
63  double drda, drdb;
64  func_deriv(a, b, c, drda, drdb);
65 
66  // Compute function and derivative with AD
67  rfad = func(afad, bfad, cfad);
68 
69  // Extract value and derivatives
70  double r_ad = rfad.val(); // r
71  double drda_ad = rfad.dx(0); // dr/da
72  double drdb_ad = rfad.dx(1); // dr/db
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  if (std::fabs(r - r_ad) < tol &&
90  std::fabs(drda - drda_ad) < tol &&
91  std::fabs(drdb - drdb_ad) < tol) {
92  std::cout << "\nExample passed!" << std::endl;
93  return 0;
94  }
95  else {
96  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
97  return 1;
98  }
99 }
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())