source: trunk/speed/fadbad/ode.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1/* $Id: ode.cpp 2506 2012-10-24 19:36:49Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Eclipse Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please 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$$
43See $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
55namespace 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
61bool 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        typedef CppAD::vector<double>   DblVector;
81
82        size_t i, j;
83        size_t p = 0;          // use ode to calculate function values
84        size_t n = size;       // number of independent variables
85        size_t m = n;          // number of dependent variables
86        ADVector X(n), Y(m);   // independent and dependent variables
87
88        // -------------------------------------------------------------
89        while(repeat--)
90        {       // choose next x value
91                CppAD::uniform_01(n, x);
92                for(j = 0; j < n; j++)
93                {       // set value of x[j]
94                        X[j] = x[j];
95                        // set up for X as the independent variable vector
96                        X[j].diff(j, n);
97                }
98
99                // evaluate function
100                CppAD::ode_evaluate(X, p, Y);
101
102                // return values with Y as the dependent variable vector
103                for(i = 0; i < m; i++)
104                {       for(j = 0; j < n; j++)
105                                jacobian[ i * n + j ] = Y[i].d(j);
106                }
107        }
108        return true;
109}
110/* $$
111$end
112*/
Note: See TracBrowser for help on using the repository browser.