Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfad_taylor_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 // taylor_example
11 //
12 // usage:
13 // taylor_example
14 //
15 // output:
16 // prints the results of computing a single Taylor series expansion of
17 // the solution to:
18 //
19 // dx/dt = 1 + x^2, x(0) = x0 = 1.0;
20 //
21 // The exact solution is x(t) = tan(t + atan(x0)) = tan(t + pi/4)
22 // Also computes the derivative of the Taylor series solution with
23 // respect to x0. The exact series derivative is
24 // dx/dx0(t) = 1/2 * sec^2(t + atan(x0))
25 
26 #include <iostream>
27 
28 #include "Sacado_No_Kokkos.hpp"
29 
30 // Function implementing RHS of ODE
31 template <typename ScalarT>
32 void func(ScalarT& f, const ScalarT& x) {
33  f = 1.0 + x*x;
34 }
35 
36 int main(int argc, char **argv)
37 {
38  double x0 = 1.0; // Initial condition
39  int deg = 40; // Degree of Taylor series solution
40 
41  Sacado::Tay::Taylor<double> x = x0; // Taylor polynomial for independent
42  Sacado::Tay::Taylor<double> f; // Taylor polynomial for dependent
43 
44  // Reserve space for degree deg coefficients
45  x.reserve(deg);
46 
47  // Compute Taylor series solution to dx/dt = f(x)
48  for (int k=0; k<deg; k++) {
49  func(f, x);
50 
51  // Set next coefficient
52  x.resize(k+1, true);
53 
54  // x_{k+1} = f_k / (k+1)
55  x.fastAccessCoeff(k+1) = f.coeff(k) / (k+1);
56  }
57 
58  // Compute derivative w.r.t. x0
61  func(f_fad, x_fad);
62  Sacado::Tay::Taylor<double> dxdx0(deg);
63  dxdx0.fastAccessCoeff(0) = 1.0;
64  for (int k=0; k<deg; k++) {
65  dxdx0.fastAccessCoeff(k+1) = 0.0;
66  for (int j=0; j<=k; j++)
67  dxdx0.fastAccessCoeff(k+1) += f_fad.dx(0).coeff(k-j) * dxdx0.coeff(j);
68  dxdx0.fastAccessCoeff(k+1) /= k+1;
69  }
70 
71  // Print Taylor series solution
72  std::cout << "Taylor series solution = " << std::endl
73  << x << std::endl;
74 
75  // Print Taylor series solution derivative
76  std::cout << "Taylor series solution derivative= " << std::endl
77  << dxdx0 << std::endl;
78 
79  // Compute Taylor series expansion of solution x(t) = tan(t+pi/4)
80  double pi = std::atan(1.0)*4.0;
82  t.fastAccessCoeff(0) = pi/4.0;
83  t.fastAccessCoeff(1) = 1.0;
85 
86  // Compute Taylor series expansion of derivative
87  Sacado::Tay::Taylor<double> dudx0 = 0.5*(1.0+u*u);
88 
89  // Print expansion of solution
90  std::cout << "Exact expansion = " << std::endl
91  << u << std::endl;
92 
93  // Print expansion of solution
94  std::cout << "Exact expansion derivative = " << std::endl
95  << dudx0 << std::endl;
96 
97  // Compute maximum relative error
98  double max_err = 0.0;
99  double err = 0.0;
100  for (int k=0; k<=deg; k++) {
101  err = std::fabs(x.coeff(k) - u.coeff(k)) / (1.0 + fabs(u.coeff(k)));
102  if (err > max_err) max_err = err;
103  }
104  std::cout << "Maximum relative error = " << max_err << std::endl;
105 
106  // Compute maximum derivative relative error
107  double deriv_max_err = 0.0;
108  double deriv_err = 0.0;
109  for (int k=0; k<=deg; k++) {
110  deriv_err = std::fabs(dxdx0.coeff(k) - dudx0.coeff(k)) /
111  (1.0 + fabs(dudx0.coeff(k)));
112  if (deriv_err > deriv_max_err) deriv_max_err = deriv_err;
113  }
114  std::cout << "Maximum derivative relative error = " << deriv_max_err
115  << std::endl;
116 
117  double tol = 1.0e-12;
118  if ((max_err < tol) && (deriv_max_err < tol)) {
119  std::cout << "\nExample passed!" << std::endl;
120  return 0;
121  }
122  else {
123  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
124  return 1;
125  }
126 
127  return 0;
128 }
129 
130 
131 
void resize(int d, bool keep_coeffs)
Resize polynomial to degree d.
void f()
atan(expr.val())
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
int main()
Definition: ad_example.cpp:171
tan(expr.val())
const T * coeff() const
Returns Taylor coefficient array.
void reserve(int d)
Reserve space for a degree d polynomial.
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:29
fabs(expr.val())