Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfad_view_handle_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_view_handle_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) and ViewFad as a
19 // handle into externally stored derivative data
20 
21 #include <iostream>
22 #include <iomanip>
23 
24 #include "Sacado.hpp"
25 
26 // The function to differentiate
27 template <typename ScalarRes, typename Scalar1, typename Scalar2>
28 ScalarRes func(const Scalar1& a, const Scalar1& b, const Scalar2& c) {
29  ScalarRes 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  Kokkos::initialize();
44  int ret = 0;
45  {
46 
47  double pi = std::atan(1.0)*4.0;
48 
49  // Values of function arguments
50  double a = pi/4;
51  double b = 2.0;
52  double c = 3.0;
53 
54  // View to store derivative data
55  const int num_deriv = 2;
56  Kokkos::View<double**,Kokkos::LayoutLeft,Kokkos::HostSpace> v( "v", 2, num_deriv );
57 
58  // Initialize derivative data
59  Kokkos::deep_copy( v, 0.0 );
60  v(0,0) = 1.0; // First (0) indep. var
61  v(1,1) = 1.0; // Second (1) indep. var
62 
63  // The Fad type
65 
66  // View handle type -- first 0 is static length (e.g., SFad), second 0
67  // is static stride, which you can make 1 if you know the View will be
68  // LayoutRight (e.g., not GPU). When values are 0, they are treated
69  // dynamically
70  typedef Sacado::Fad::ViewFad<double,0,0,FadType> ViewFadType;
71 
72  // Fad objects
73  ViewFadType afad( &v(0,0), &a, num_deriv, v.stride_1() );
74  ViewFadType bfad( &v(1,0), &b, num_deriv, v.stride_1() );
75  FadType cfad(c);
76  FadType rfad;
77 
78  // Compute function
79  double r = func<double>(a, b, c);
80 
81  // Compute derivative analytically
82  double drda, drdb;
83  func_deriv(a, b, c, drda, drdb);
84 
85  // Compute function and derivative with AD
86  rfad = func<FadType>(afad, bfad, cfad);
87 
88  // Extract value and derivatives
89  double r_ad = rfad.val(); // r
90  double drda_ad = rfad.dx(0); // dr/da
91  double drdb_ad = rfad.dx(1); // dr/db
92 
93  // Print the results
94  int p = 4;
95  int w = p+7;
96  std::cout.setf(std::ios::scientific);
97  std::cout.precision(p);
98  std::cout << " r = " << r << " (original) == " << std::setw(w) << r_ad
99  << " (AD) Error = " << std::setw(w) << 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 
107  double tol = 1.0e-14;
108  if (std::fabs(r - r_ad) < tol &&
109  std::fabs(drda - drda_ad) < tol &&
110  std::fabs(drdb - drdb_ad) < tol) {
111  std::cout << "\nExample passed!" << std::endl;
112  ret = 0;
113  }
114  else {
115  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
116  ret = 1;
117  }
118 
119  }
120  Kokkos::finalize();
121  return ret;
122 }
const char * p
Sacado::Fad::DFad< double > FadType
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())