source: trunk/ADOL-C/examples/additional_examples/helm/helm-auto-exam.cpp @ 624

Last change on this file since 624 was 171, checked in by kulshres, 9 years ago

Squashed merge branch 'master' of 'gitclone' into svn

  • 'master' of 'gitclone': (84 commits) adjust example makefiles and include paths get rid of the symlink in the src subdirectory

details of the commits:
commit c9e4bc332d2363f737fc2e8a8fcfc2e43ddb9d15
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Oct 4 15:43:47 2010 +0200

adjust example makefiles and include paths

include paths in example sources were wrong for some time now
simplify makefile rules too, there is really no need for checking SPARSE
adjust include paths in makefiles.

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

commit e6e1963e41e097fd5b4a79cd1611c12f6868dc94
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Oct 4 15:41:25 2010 +0200

get rid of the symlink in the src subdirectory

windows doesn't like symlinks and make infinite depth directories
we now create a symlink for build in the directory parallel to src
adjust all makefiles.am accordingly for build

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

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     helm-auto-exam.cpp
4 Revision: $Id: helm-auto-exam.cpp 171 2010-10-04 13:57:19Z kulshres $
5 Contents: example for  Helmholtz energy example
6           Computes gradient using AD driver reverse(..)
7
8 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
9               Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
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#include <adolc/adolc.h>
20
21#include <math.h>
22
23
24/****************************************************************************/
25/*                                                    CONSTANTS & VARIABLES */
26const double TE = 0.01; /* originally 0.0 */
27const double R  = sqrt(2.0);
28
29
30/****************************************************************************/
31/*                                                         HELMHOLTZ ENERGY */
32adouble energy( int n, adouble x[], double bv[] ) {
33    adouble he, xax, bx, tem;
34    int i,j;
35    xax = 0;
36    bx  = 0;
37    he  = 0;
38    for (i=0; i<n; i++) {
39        he += x[i]*log(x[i]);
40        bx +=  bv[i]*x[i];
41        tem = (2.0/(1.0+i+i))*x[i];
42        for (j=0; j<i; j++)
43            tem += (1.0/(1.0+i+j))*x[j];
44        xax += x[i]*tem;
45    }
46    xax *= 0.5;
47    he   = 1.3625E-3*(he-TE*log(1.0-bx));
48    he   = he - log((1+bx*(1+R))/(1+bx*(1-R)))*xax/bx;
49    return he;
50}
51
52
53
54/****************************************************************************/
55/*                                                                     MAIN */
56/* This program computes first order directional derivatives
57   for the helmholtz energy function */
58int main() {
59    int nf, n, j, l;
60    fprintf(stdout,"HELM-AUTO-EXAM (ADOL-C Example)\n\n");
61    fprintf(stdout," # of independents/10 =? \n ");
62    scanf("%d",&nf);
63
64    /*--------------------------------------------------------------------------*/
65    double result = 0.0;                                    /* Initilizations */
66    n = 10 * nf;
67    double* bv   = new double[n];
68    double* grad = new double[n];
69
70    adouble* x   = new adouble[n];
71    adouble he;
72
73    double r = 1.0/n;
74    for (j=0; j<n; j++)
75        bv[j]= 0.02*(1.0+fabs(sin(double(j))));
76
77    /*--------------------------------------------------------------------------*/
78    int imd_rev = 1;                                     /* Tracing with keep */
79    trace_on(1,imd_rev);
80    for (j=0; j<n; j++)
81        x[j] <<= r*sqrt(1.0+j);
82    he = energy(n,x,bv);
83    he >>= result;
84    trace_off();
85    fprintf(stdout, "%14.6E -- energy\n",result);
86
87    /*--------------------------------------------------------------------------*/
88    reverse(1,1,n,0,1.0,grad);             /* reverse computation of gradient */
89
90    /*--------------------------------------------------------------------------*/
91    for (l=0; l<n; l++)                                            /* results */
92        fprintf(stdout,"%3d: %14.6E,  \n",l,grad[l]);
93    fprintf(stdout,"%14.6E -- energy\n",result);
94
95    delete [] x;
96    delete [] bv;
97    delete [] grad;
98
99    return 1;
100}
101
102
103/****************************************************************************/
104/*                                                               THAT'S ALL */
105
Note: See TracBrowser for help on using the repository browser.