File: vfunc_gear.cpp

package info (click to toggle)
adolc 2.7.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,496 kB
  • sloc: cpp: 40,481; ansic: 19,390; sh: 4,277; makefile: 551; python: 486
file content (409 lines) | stat: -rw-r--r-- 11,540 bytes parent folder | download | duplicates (3)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*----------------------------------------------------------------------------
 ADOL-C -- Automatic Differentiation by Overloading in C++
 File:     vfunc_gear.cpp
 Revision: $Id$
 Contents: Example of function module containing the machine tool example
            of gearing
 
   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_GEAR_C_

/****************************************************************************/
/*                                                                 INCLUDES */
#include <adolc/adolc.h>

#include <cmath>
#include <time.h>
#include <cstdlib>


/****************************************************************************/
/*                                                         GLOBAL VARIABLES */

/*--------------------------------------------------------------------------*/
/*                                                        Control file name */
const char* controlFileName = "gearexam.ctrl";

/*--------------------------------------------------------------------------*/
/*                                                               Dimensions */
int indepDim;
int depDim;

/*--------------------------------------------------------------------------*/
/*                                       Other problem dependent parameters */
//static unsigned short int dx[3]; /* variable needed by erand48(.) */


/****************************************************************************/
/*                                                  INIT PROBLEM PARAMETERS */
void initProblemParameters( void ) {
    fprintf(stdout,"GEAREXAM (ADOL-C Example)\n\n");

    /* number of indeps & deps */
    indepDim = 3;
    depDim   = 3;

    /* Init erand48(); */
    struct tm s;
    time_t t;
    time(&t);
    s=*localtime(&t);
    srand(s.tm_sec*s.tm_min);
    /*  dx[0]=rand();
      dx[1]=rand();
      dx[2]=rand();*/
}


/****************************************************************************/
/*                                                        INITIALIZE INDEPs */
void initIndependents( double* indeps ) {
    for (int i=0; i<indepDim; i++)
        indeps[i] = (double)rand();
}


/****************************************************************************/
/*                                                 ORIGINAL SCALAR FUNCTION */

/*--------------------------------------------------------------------------*/
/*                                                       The model function */
#define Pi 3.141592654

/*--------------------------------------------------------------------------*/
// important machine tool parameters

int konvex=     1;          // konvexe oder konkave Flanke

int zz=         13;         // Zaehnezahl
double delta=   16.46;      // Kegelwinkel
double st=      0.00001;    // Verschiebung Teilkegelspitze

double xmw=     79.33279;   // MK-x
double ymw=     -74.05;     // MK-y
double zmw=     159.49741;  // MK-z
double m=       0.0;        // Erzeugungs-Achsversatz
double zwr=     0.0;        // Reitstockeinstellung
double theta_w= -58.2253;   // Waelztrommelwinkel

double xmk=     17.49874;   // Messerversatz
int zm=         5;          // MK-Gangzahl
double ymk=     80.0;       // MK-Versatz

double rho=     101.64155;  // Spitzenradius, Flugkreisr.
double r=       2.1;        // Kopfradius
double rs=      2594.425;   // Sphaerikradius
double ys=      876.147;    // Sphaerik-Mitte-Y
double zs=      -2442.015;  // Sphaerik-Mitte-Z


/*--------------------------------------------------------------------------*/
// elementary rotations

void D1 ( adouble * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    adouble tmpVec2=locSin*vec[1] + locCos*vec[2];
    vec[1]=locCos*vec[1] - locSin*vec[2];
    vec[2]=tmpVec2;
}

void D2 ( adouble * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    adouble tmpVec2=-locSin*vec[0] + locCos*vec[2];
    vec[0]=locCos*vec[0] + locSin*vec[2];
    vec[2]=tmpVec2;
}

void D3 ( adouble * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    adouble tmpVec1=locSin*vec[0] + locCos*vec[1];
    vec[0]=locCos*vec[0] - locSin*vec[1];
    vec[1]=tmpVec1;
}

void D1 ( adouble * vec, adouble & alpha ) {
    adouble locCos=cos(alpha);
    adouble locSin=sin(alpha);
    adouble tmpVec2=locSin*vec[1] + locCos*vec[2];
    vec[1]=locCos*vec[1] - locSin*vec[2];
    vec[2]=tmpVec2;
}

void D2 ( adouble * vec, adouble & alpha ) {
    adouble locCos=cos(alpha);
    adouble locSin=sin(alpha);
    adouble tmpVec2=-locSin*vec[0] + locCos*vec[2];
    vec[0]=locCos*vec[0] + locSin*vec[2];
    vec[2]=tmpVec2;
}

void D2 ( adouble *  depVec, adouble * indepVec,  adouble & alpha ) {
    if ( indepVec == depVec ) {
        D2(depVec,alpha);
        return;
    }
    adouble locCos=cos(alpha);
    adouble locSin=sin(alpha);
    depVec[0]=locCos*indepVec[0] + locSin*indepVec[2];
    depVec[1]=indepVec[1];
    depVec[2]=-locSin*indepVec[0] + locCos*indepVec[2];
}

void D3 ( adouble * vec, adouble & alpha ) {
    adouble locCos=cos(alpha);
    adouble locSin=sin(alpha);
    adouble tmpVec1=locSin*vec[0] + locCos*vec[1];
    vec[0]=locCos*vec[0] - locSin*vec[1];
    vec[1]=tmpVec1;
}

void D1 ( double * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    double tmpVec2=locSin*vec[1] + locCos*vec[2];
    vec[1]=locCos*vec[1] - locSin*vec[2];
    vec[2]=tmpVec2;
}

void D2 ( double * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    double tmpVec2=-locSin*vec[0] + locCos*vec[2];
    vec[0]=locCos*vec[0] + locSin*vec[2];
    vec[2]=tmpVec2;
}

void D2 ( double * depVec, double * indepVec,  double & alpha ) {
    if ( indepVec == depVec ) {
        D2(depVec,alpha);
        return;
    }
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    depVec[0]=locCos*indepVec[0] + locSin*indepVec[2];
    depVec[1]=indepVec[1];
    depVec[2]=-locSin*indepVec[0] + locCos*indepVec[2];
}

void D3 ( double * vec, double & alpha ) {
    double locCos=cos(alpha);
    double locSin=sin(alpha);
    double tmpVec1=locSin*vec[0] + locCos*vec[1];
    vec[0]=locCos*vec[0] - locSin*vec[1];
    vec[1]=tmpVec1;
}


/*--------------------------------------------------------------------------*/
// parametrized cutting edge

void def_messer(adouble *z, adouble *messer) {

    double u0, uOri, phi0;
    adouble h;

    phi0= asin((r+ys)/(r+rs));
    if (konvex==1) {
        u0=rs*phi0;
        uOri=1.0;
    } else {
        u0=rs*(phi0-Pi);
        uOri=-1.0;
    };

    h= (z[0]-u0)/(uOri*rs);
    messer[0]=zs+rs*cos(h);
    messer[1]=0.0;
    messer[2]=-ys-rs*sin(h);
}

void def_messer(double *z, double *messer) {

    double u0, uOri, phi0;
    double h;

    phi0= asin((r+ys)/(r+rs));
    if (konvex==1) {
        u0=rs*phi0;
        uOri=1.0;
    } else {
        u0=rs*(phi0-Pi);
        uOri=-1.0;
    };

    h= (z[0]-u0)/(uOri*rs);
    messer[0]=zs+rs*cos(h);
    messer[1]=0.0;
    messer[2]=-ys-rs*sin(h);
}

/*--------------------------------------------------------------------------*/
// the main function

void gearFunction(double* pz, double* pf) {
    double ah;
    double* messer;
    messer= new double[3];

    def_messer(pz, messer);

    // Position der Schneide am Messerkopf
    messer[0]+=rho;
    messer[1]-=xmk;

    // Messerkopfrotation mit Parameter v
    D3(messer,pz[1]);

    // Lage des Messerkopfs auf der Wiege
    messer[2]-=ymk;

    // Eindrehen in Orientierung der Wiege
    ah=messer[0];
    messer[0]=messer[1];
    messer[1]=ah;
    messer[2]=-messer[2];

    // Verschiebung
    messer[0]-=xmw;
    messer[1]+=zmw;
    messer[2]+=ymw;

    // Wiegenwinkel thetaW, entspricht dem wert t=0
    // + Wiegenbewegung mit Parameter t
    ah = theta_w+pz[2];
    D3(messer,ah);

    // Achsversatz
    messer[0]+=m;

    // Eindrehen in Orientierung des Werkrades
    messer[0]=-messer[0];
    ah=messer[1];
    messer[1]=-messer[2];
    messer[2]=-ah;

    // Teilkegeloeffnungswinkel delta, y-Achsen entgegengesetzt
    D1(messer,delta);

    // neue Verschiebung der Werkradachse
    messer[2]+=zwr+st;

    // gekoppelte Werkraddrehung in Abhaengigkeit von t und v
    ah = (1/sin(delta))*pz[2] + (double(zm)/zz)*pz[1];
    D2(pf,messer,ah);

    delete[] messer;
}

/*--------------------------------------------------------------------------*/
/*                                                   The interface function */
void originalVectorFunction( double* indeps, double* deps ) {
    gearFunction(indeps,deps);
}


/****************************************************************************/
/*                                                   TAPING SCALAR FUNCTION */

/*--------------------------------------------------------------------------*/
/*                                                       The model function */
void activeGearFunction(adouble* z, adouble* f) {
    adouble ah;

    adouble* messer = new adouble[3];
    def_messer(z, messer);

    // Position der Schneide am Messerkopf
    messer[0]+=rho;
    messer[1]-=xmk;

    // Messerkopfrotation mit Parameter v
    D3(messer,z[1]);

    // Lage des Messerkopfs auf der Wiege
    messer[2]-=ymk;

    // Eindrehen in Orientierung der Wiege
    ah=messer[0];
    messer[0]=messer[1];
    messer[1]=ah;
    messer[2]=-messer[2];

    // Verschiebung
    messer[0]-=xmw;
    messer[1]+=zmw;
    messer[2]+=ymw;

    // Wiegenwinkel thetaW, entspricht dem wert t=0
    // + Wiegenbewegung mit Parameter t
    ah = theta_w+z[2];
    D3(messer,ah);

    // Achsversatz
    messer[0]+=m;

    // Eindrehen in Orientierung des Werkrades
    messer[0]=-messer[0];
    ah=messer[1];
    messer[1]=-messer[2];
    messer[2]=-ah;

    // Teilkegeloeffnungswinkel delta, y-Achsen entgegengesetzt
    D1(messer,delta);

    // neue Verschiebung der Werkradachse
    messer[2]+=zwr+st;

    // gekoppelte Werkraddrehung in Abhaengigkeit von t und v
    ah = (1/sin(delta))*z[2] + (double(zm)/zz)*z[1];
    D2(f,messer,ah);

    delete[] messer;
}


/*--------------------------------------------------------------------------*/
/*                                                   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++;
    activeGearFunction(activeIndeps,activeDeps);
    aIP = activeDeps;
    iP  = deps;
    for (i=0; i<depDim; i++)
        *aIP++ >>= *iP++;
    trace_off();
}

#undef _VFUNC_GEAR_C_