1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*----------------------------------------------------------------------------
ADOL-C -- Automatic Differentiation by Overloading in C++
File: vfunc_fic.cpp
Revision: $Id: vfunc_fic.cpp 171 2010-10-04 13:57:19Z kulshres $
Contents: example for function module containing the flow in a channel
Each << function module >> contains:
(1) const char* const controlFileName
(2) int indepDim;
(3) int depDim;
(4) void initProblemParameters( void )
(5) void initIndependents( double* indEPS_ )
(6) void originalVectorFunction( double* indEPS_, double* dEPS_ )
(7) void tapingVectorFunction( int tag, double* indEPS_, double* dEPS_ )
Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
This file is part of ADOL-C. This software is provided as open source.
Any use, reproduction, or distribution of the software constitutes
recipient's acceptance of the terms of the accompanying license file.
---------------------------------------------------------------------------*/
#define _VFUNC_FIC_C_
/****************************************************************************/
/* INCLUDES */
#include <adolc/adolc.h>
#include <math.h>
/****************************************************************************/
/* GLOBAL VARIABLES */
/*--------------------------------------------------------------------------*/
/* Control file name */
const char* controlFileName = "ficexam.ctrl";
/*--------------------------------------------------------------------------*/
/* Dimensions */
int indepDim;
int depDim;
/*--------------------------------------------------------------------------*/
/* Other problem dependent parameters */
int nIntern;
double r__;
/****************************************************************************/
/* INIT PROBLEM PARAMETERS */
void initProblemParameters( void ) {
fprintf(stdout,"FICEXAM Flow in a channel (ADOL-C Example)\n\n");
/* number of indeps & deps */
if (indepDim > 0)
nIntern = indepDim;
else {
fprintf(stdout," number of independents/8 = ? ");
fscanf(stdin,"%d",&nIntern);
fprintf(stdout,"\n");
}
indepDim = nIntern*8;
depDim = indepDim;
}
/****************************************************************************/
/* INITIALIZE INDEPs */
void initIndependents( double* indeps ) {
int i, j, var;
double xt, h;
h = 1.0/nIntern;
xt = 0.0;
for (i=1; i<=nIntern; i++) {
var = 8*(i-1);
indeps[var] = xt*xt*(3.0-2.0*xt);
indeps[var+1] = 6.0 * xt * (1.0 - xt);
indeps[var+2] = 6.0 * (1.0 -2.0*xt);
indeps[var+3] = -12.0;
for (j=1; j<=4; j++)
indeps[var+3+j] = 0.0;
xt = xt + h;
}
r__ = 0;
}
/****************************************************************************/
/* ORIGINAL SCALAR FUNCTION */
/*--------------------------------------------------------------------------*/
/* The fic function */
int pdficfj ( int n, double* x, double* fvec, double r__, int nIntern ) { /* Initialized data */
static double rho[4] = { .0694318413734436035, .330009490251541138,
.66999053955078125, .930568158626556396 };
/* System generated locals */
int i__1;
/* Local variables */
static double h__;
static int i__, j, k, m;
static double w[5], nf, hm, dw[40] /* was [5][8] */, rhnfhk[1280]
/* was [4][8][8][5] */, rhoijh;
static int eqn, var;
--fvec;
--x;
/* Function Body */
/* Initialization. */
h__ = 1. / (double) (nIntern);
hm = 1.;
for (m = 0; m <= 4; ++m) {
for (i__ = 1; i__ <= 4; ++i__) {
rhoijh = hm;
for (j = 0; j <= 7; ++j) {
nf = 1.;
for (k = 0; k <= 7; ++k) {
rhnfhk[i__ + (j + (k + (m << 3) << 3) << 2) - 1] = rhoijh
/ nf;
nf *= (double) (k + 1);
}
rhoijh *= rho[i__ - 1];
}
}
hm *= h__;
}
/* Evaluate the function */
/* Initialize arrays. */
i__1 = n;
for (j = 1; j <= i__1; ++j) {
fvec[j] = 0.;
}
for (k = 1; k <= 8; ++k) {
for (j = 1; j <= 5; ++j) {
dw[j + k * 5 - 6] = 0.;
}
}
/* Set up the boundary equations at t = 0. u(0) = 0, u'(0) = 0. */
fvec[1] = x[1];
fvec[2] = x[2];
i__1 = nIntern;
for (i__ = 1; i__ <= i__1; ++i__) {
var = i__ - 1 << 3;
/* Set up the collocation equations. */
eqn = var + 2;
for (k = 1; k <= 4; ++k) {
for (m = 1; m <= 5; ++m) {
w[m - 1] = 0.;
for (j = m; j <= 4; ++j) {
w[m - 1] += rhnfhk[k + (j - m + (j - m + (j - m << 3) <<
3) << 2) - 1] * x[var + j];
dw[m + j * 5 - 6] = rhnfhk[k + (j - m + (j - m + (j - m
<<
3) << 3) << 2) - 1];
}
for (j = 1; j <= 4; ++j) {
w[m - 1] += rhnfhk[k + (j + 4 - m + (j + 4 - m + (4 - m
+
1 << 3) << 3) << 2) - 1] * x[var + 4 + j];
dw[m + (j + 4) * 5 - 6] = rhnfhk[k + (j + 4 - m + (j + 4
- m + (4 - m + 1 << 3) << 3) << 2) - 1];
}
}
fvec[eqn + k] = w[4] - r__ * (w[1] * w[2] - w[0] * w[3]);
}
/* Set up the continuity equations. */
eqn = var + 6;
for (m = 1; m <= 4; ++m) {
w[m - 1] = 0.;
for (j = m; j <= 4; ++j) {
w[m - 1] += rhnfhk[(j - m + (j - m << 3)) * 32] * x[var + j]
;
dw[m + j * 5 - 6] = rhnfhk[(j - m + (j - m << 3)) * 32];
}
for (j = 1; j <= 4; ++j) {
w[m - 1] += rhnfhk[(j + 4 - m + (4 - m + 1 << 3)) * 32] * x[
var + 4 + j];
dw[m + (j + 4) * 5 - 6] = rhnfhk[(j + 4 - m + (4 - m + 1 <<
3)
) * 32];
}
}
if (i__ == nIntern) {
goto L230;
}
for (m = 1; m <= 4; ++m) {
fvec[eqn + m] = x[var + 8 + m] - w[m - 1];
}
}
/* Set up the boundary equations at t = 1. u(1) = 1, u'(1) = 0. */
L230:
fvec[n - 1] = w[0] - 1.;
fvec[n] = w[1];
return 0;
}
/*--------------------------------------------------------------------------*/
/* The interface function */
void originalVectorFunction( double* indeps, double* deps ) {
pdficfj(indepDim,indeps,deps,r__,nIntern);
}
/****************************************************************************/
/* TAPING SCALAR FUNCTION */
/*--------------------------------------------------------------------------*/
/* The active fic function */
int dficfj( int n, adouble* x, adouble* fvec, double r__, int nIntern ) {
/* Initialized data */
static adouble rho[4] = { .0694318413734436035, .330009490251541138,
.66999053955078125, .930568158626556396 };
/* System generated locals */
int i__1;
/* Local variables */
static adouble h__;
static int i__, j, k, m;
static adouble nf, hm, dw[40] /* was [5][8] */, rhnfhk[1280]
/* was [4][8][8][5] */, rhoijh;
static int eqn, var;
adouble w[5];
--fvec;
--x;
/* Function Body */
/* Initialization. */
h__ = 1. / (double) (nIntern);
hm = 1.;
for (m = 0; m <= 4; ++m) {
for (i__ = 1; i__ <= 4; ++i__) {
rhoijh = hm;
for (j = 0; j <= 7; ++j) {
nf = 1.;
for (k = 0; k <= 7; ++k) {
rhnfhk[i__ + (j + (k + (m << 3) << 3) << 2) - 1] = rhoijh
/ nf;
nf *= (double) (k + 1);
}
rhoijh *= rho[i__ - 1];
}
}
hm *= h__;
}
/* Evaluate the function */
/* Initialize arrays. */
i__1 = n;
for (j = 1; j <= i__1; ++j) {
fvec[j] = 0.;
}
for (k = 1; k <= 8; ++k) {
for (j = 1; j <= 5; ++j) {
dw[j + k * 5 - 6] = 0.;
}
}
/* Set up the boundary equations at t = 0. u(0) = 0, u'(0) = 0. */
fvec[1] = x[1];
fvec[2] = x[2];
i__1 = nIntern;
for (i__ = 1; i__ <= i__1; ++i__) {
var = i__ - 1 << 3;
/* Set up the collocation equations. */
eqn = var + 2;
for (k = 1; k <= 4; ++k) {
for (m = 1; m <= 5; ++m) {
w[m - 1] = 0.;
for (j = m; j <= 4; ++j) {
w[m - 1] += rhnfhk[k + (j - m + (j - m + (j - m << 3) <<
3) << 2) - 1] * x[var + j];
dw[m + j * 5 - 6] = rhnfhk[k + (j - m + (j - m + (j - m
<<
3) << 3) << 2) - 1];
}
for (j = 1; j <= 4; ++j) {
w[m - 1] += rhnfhk[k + (j + 4 - m + (j + 4 - m + (4 - m
+
1 << 3) << 3) << 2) - 1] * x[var + 4 + j];
dw[m + (j + 4) * 5 - 6] = rhnfhk[k + (j + 4 - m + (j + 4
- m + (4 - m + 1 << 3) << 3) << 2) - 1];
}
}
fvec[eqn + k] = w[4] - r__ * (w[1] * w[2] - w[0] * w[3]);
}
/* Set up the continuity equations. */
eqn = var + 6;
for (m = 1; m <= 4; ++m) {
w[m - 1] = 0.;
for (j = m; j <= 4; ++j) {
w[m - 1] += rhnfhk[(j - m + (j - m << 3)) * 32] * x[var + j]
;
dw[m + j * 5 - 6] = rhnfhk[(j - m + (j - m << 3)) * 32];
}
for (j = 1; j <= 4; ++j) {
w[m - 1] += rhnfhk[(j + 4 - m + (4 - m + 1 << 3)) * 32] * x[
var + 4 + j];
dw[m + (j + 4) * 5 - 6] = rhnfhk[(j + 4 - m + (4 - m + 1 <<
3)
) * 32];
}
}
if (i__ != nIntern) {
for (m = 1; m <= 4; ++m) {
fvec[eqn + m] = x[var + 8 + m] - w[m - 1];
}
}
}
/* Set up the boundary equations at t = 1. u(1) = 1, u'(1) = 0. */
fvec[n - 1] = w[0] - 1.;
fvec[n] = w[1];
return 0;
}
/*--------------------------------------------------------------------------*/
/* The interface function */
void tapingVectorFunction( int tag, double* indeps, double* deps ) {
int i;
trace_on(tag);
adouble* activeIndeps = new adouble[indepDim];
adouble* activeDeps = new adouble[depDim];
adouble* aIP = activeIndeps;
double* iP = indeps;
for (i=0; i<indepDim; i++)
*aIP++ <<= *iP++;
dficfj(indepDim,activeIndeps,activeDeps,r__,nIntern);
aIP = activeDeps;
iP = deps;
for (i=0; i<depDim; i++)
*aIP++ >>= *iP++;
trace_off();
}
#undef _VFUNC_FIC_C_
|