1 | /* $Id: ode.cpp 2907 2013-10-06 14:20:31Z bradbell $ */ |
---|
2 | /* -------------------------------------------------------------------------- |
---|
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell |
---|
4 | |
---|
5 | CppAD is distributed under multiple licenses. This distribution is under |
---|
6 | the terms of the |
---|
7 | Eclipse Public License Version 1.0. |
---|
8 | |
---|
9 | A copy of this license is included in the COPYING file of this distribution. |
---|
10 | Please visit http://www.coin-or.org/CppAD/ for information on other licenses. |
---|
11 | -------------------------------------------------------------------------- */ |
---|
12 | /* |
---|
13 | $begin fadbad_ode.cpp$$ |
---|
14 | $spell |
---|
15 | fadiff |
---|
16 | cassert |
---|
17 | namespace |
---|
18 | fabs |
---|
19 | const |
---|
20 | std |
---|
21 | typedef |
---|
22 | diff |
---|
23 | Jacobian |
---|
24 | cstring |
---|
25 | cppad |
---|
26 | hpp |
---|
27 | retape |
---|
28 | Fadbad |
---|
29 | bool |
---|
30 | CppAD |
---|
31 | $$ |
---|
32 | |
---|
33 | $section Fadbad Speed: Ode$$ |
---|
34 | |
---|
35 | |
---|
36 | $index link_ode, fadbad$$ |
---|
37 | $index fadbad, link_ode$$ |
---|
38 | $index speed, fadbad$$ |
---|
39 | $index fadbad, speed$$ |
---|
40 | $index ode, speed fadbad$$ |
---|
41 | |
---|
42 | $head Specifications$$ |
---|
43 | See $cref link_ode$$. |
---|
44 | |
---|
45 | $head Implementation$$ |
---|
46 | |
---|
47 | $codep */ |
---|
48 | # include <FADBAD++/fadiff.h> |
---|
49 | # include <algorithm> |
---|
50 | # include <cassert> |
---|
51 | # include <cppad/vector.hpp> |
---|
52 | # include <cppad/speed/uniform_01.hpp> |
---|
53 | # include <cppad/speed/ode_evaluate.hpp> |
---|
54 | |
---|
55 | namespace fadbad { |
---|
56 | // define fabs for use by ode_evaluate |
---|
57 | fadbad::F<double> fabs(const fadbad::F<double>& x) |
---|
58 | { return std::max(-x, x); } |
---|
59 | } |
---|
60 | |
---|
61 | bool link_ode( |
---|
62 | size_t size , |
---|
63 | size_t repeat , |
---|
64 | CppAD::vector<double> &x , |
---|
65 | CppAD::vector<double> &jacobian |
---|
66 | ) |
---|
67 | { |
---|
68 | assert( x.size() == size ); |
---|
69 | assert( jacobian.size() == size * size ); |
---|
70 | |
---|
71 | // speed test global option values |
---|
72 | extern bool global_retape, global_atomic, global_optimize; |
---|
73 | if( ! global_retape || global_atomic || global_optimize ) |
---|
74 | return false; |
---|
75 | |
---|
76 | // ------------------------------------------------------------- |
---|
77 | // setup |
---|
78 | typedef fadbad::F<double> ADScalar; |
---|
79 | typedef CppAD::vector<ADScalar> ADVector; |
---|
80 | |
---|
81 | size_t i, j; |
---|
82 | size_t p = 0; // use ode to calculate function values |
---|
83 | size_t n = size; // number of independent variables |
---|
84 | size_t m = n; // number of dependent variables |
---|
85 | ADVector X(n), Y(m); // independent and dependent variables |
---|
86 | |
---|
87 | // ------------------------------------------------------------- |
---|
88 | while(repeat--) |
---|
89 | { // choose next x value |
---|
90 | CppAD::uniform_01(n, x); |
---|
91 | for(j = 0; j < n; j++) |
---|
92 | { // set value of x[j] |
---|
93 | X[j] = x[j]; |
---|
94 | // set up for X as the independent variable vector |
---|
95 | X[j].diff(j, n); |
---|
96 | } |
---|
97 | |
---|
98 | // evaluate function |
---|
99 | CppAD::ode_evaluate(X, p, Y); |
---|
100 | |
---|
101 | // return values with Y as the dependent variable vector |
---|
102 | for(i = 0; i < m; i++) |
---|
103 | { for(j = 0; j < n; j++) |
---|
104 | jacobian[ i * n + j ] = Y[i].d(j); |
---|
105 | } |
---|
106 | } |
---|
107 | return true; |
---|
108 | } |
---|
109 | /* $$ |
---|
110 | $end |
---|
111 | */ |
---|