source: trunk/ADOL-C/examples/traceless_vector_indo.cpp

Last change on this file was 746, checked in by kulshres, 19 months ago

Merge of 'adtl-cleanup' branch from git and one memory fix

The following commits were merged:

commit c157696aa17f332488275bd423a9e45e847c4d5a
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Dec 5 15:28:33 2017 +0100

changing buffer-size when overwriting a tag might cause problems

so free the tape resources before start_trace, which will then reallocate

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 8759e05af6cb1e64fd6a786ae02829f58d346173
Author: Benjamin Jurgelucks <bjurgel@…>
Date: Tue Apr 4 14:52:01 2017 +0200

Added prototyp for traceless higher order vector mode and a corresponding example.

commit e22feb7e2f68ee80f5444f1090be4b0d870d71d2
Author: Mladen Banovic <mladenbanovic2705@…>
Date: Wed Mar 29 17:59:30 2017 +0200

Change macro VAL to PRIMAL_VALUE so it does not conflict with OpenCASCADE sources.

commit 9fd04c5f9fe1f96ddb94f4925ae11af77b6445a9
Author: Mladen Banovic <mladenbanovic2705@…>
Date: Wed Mar 29 14:11:06 2017 +0200

Add traceless_vector_indo example.

commit 4770a666580cd1964a4261ac9d25795da9a082c0
Author: Mladen Banovic <mladenbanovic2705@…>
Date: Fri Mar 24 17:27:39 2017 +0100

Make sparsedrivers.cpp compilable. Remove func_ad from adtl.h. Change func_ad in adtl_indo.h to be a templated class such that it can work with adoubles from adtl and adtl_indo namespace. Declare ADOLC_get_sparse_jacobian outside of any namespace.

commit 87542e4746eaea7d27ba834ff3efb73602465d5a
Author: Mladen Banovic <mladenbanovic2705@…>
Date: Thu Mar 23 15:13:43 2017 +0100

Lightweight traceless approach. Clean up current adtl namespace. Create adtl_indo namespace (files adtl_indo.h and adouble_tl_indo.cpp) that serves for sparsity patterns.

File size: 3.0 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     tapeless_vector.cpp
4 Revision: $Id$
5 Contents: computation of coordinate transform,
6           vector tapeless forward mode
7           described in the manual
8
9 Copyright (c) Andrea Walther, Andreas Kowarz
10 
11 This file is part of ADOL-C. This software is provided as open source.
12 Any use, reproduction, or distribution of the software constitutes
13 recipient's acceptance of the terms of the accompanying license file.
14 
15---------------------------------------------------------------------------*/
16
17/****************************************************************************/
18/*                                                                 INCLUDES */
19
20#include <iostream>
21using namespace std;
22
23#include <adolc/adtl_indo.h>
24#include <adolc/sparse/sparsedrivers.h>
25
26template<typename T>
27class my_function : public func_ad<T> {
28public:
29    int operator() (int n, T *x, int m, T *y)
30    {
31        y[0] = sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
32        y[1] = atan(sqrt(x[0]*x[0]+x[1]*x[1])/x[2]);
33        y[2] = atan(x[1]/x[0]);
34        return 1;
35    }
36};
37
38
39int main(int argc, char *argv[]) {
40    const int m=3, n=3;
41    adtl::setNumDir(n);
42
43    my_function<adtl::adouble> fun;
44    my_function<adtl_indo::adouble> fun_indo;
45    adtl::adouble x[n], y[m];
46
47    for (int i=0; i<n;++i)          // Initialize x_i
48    {
49        x[i] = i + 1.0;
50        for (int j=0; j<m;++j)
51            if (i==j)
52                x[i].setADValue(j,1);
53    }
54
55    cout.precision(15);
56    cout << endl << "Transform from Cartesian to spherical polar coordinates" << endl << endl;
57
58    cout << "cartesian coordinates: " << endl;
59    cout << "x[0] = " << x[0].getValue() << "  x[1] = " << x[1].getValue()
60    << "  x[2] = " << x[2].getValue() << endl << endl;
61
62    fun(3, x, 3, y);
63
64    cout << "cpherical polar coordinates: " << endl;
65    cout << "y[0] = " << y[0].getValue() << "  y[1] = " << y[1].getValue()
66    << "  y[2] = " << y[2].getValue() << endl <<endl;
67
68    // "use" the derivative
69    cout << "derivatives:" << endl;
70    for (int i=0; i<3;++i) {
71        for (int j=0; j<3;++j)
72            cout << y[i].getADValue(j) << "  ";
73        cout << endl;
74    }
75    cout << endl;
76
77    double* basepoints = new double[n];
78    for(int i = 0; i<n; i++)
79        basepoints[i] = x[i].getValue();
80    int retVal;
81    int nnz;
82    unsigned int *rind = NULL, *cind = NULL;
83    double *values = NULL;
84    retVal = ::ADOLC_get_sparse_jacobian(&fun, &fun_indo, n, m, 0, basepoints, &nnz, &rind, &cind, &values);
85
86    cout << endl;
87    cout << "Checking results with ADOLC_get_sparse_jacobian functionality..." << endl;
88    cout << "number of non-zero elements in jacobian: " << nnz << endl;
89    cout << "Elements are:" << endl;
90
91    for(int i = 0; i < nnz; i++)
92    {
93      cout << "[" << *rind<< "][" << *cind << "]: " << *values << endl;
94      rind++;
95      cind++;
96      values++;
97    }
98   
99    return 0;
100}
101
Note: See TracBrowser for help on using the repository browser.