AD_test.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 /* Use POSIX clock_gettime() for timing on non-Windows machines. */
6 #include <time.h>
7 #include <sys/stat.h>
8 #include <sys/time.h>
9 
10 typedef double real_t;
11 
12 typedef struct timer
13 {
14  struct timeval tic;
15  struct timeval toc;
16 } timer;
17 
18 /* read current time */
19 void tic(timer* t)
20 {
21  gettimeofday(&t->tic, 0);
22 }
23 
24 /* return time passed since last call to tic on this timer */
26 {
27  struct timeval temp;
28 
29  gettimeofday(&t->toc, 0);
30 
31  if ((t->toc.tv_usec - t->tic.tv_usec) < 0)
32  {
33  temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec - 1;
34  temp.tv_usec = 1000000 + t->toc.tv_usec - t->tic.tv_usec;
35  }
36  else
37  {
38  temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec;
39  temp.tv_usec = t->toc.tv_usec - t->tic.tv_usec;
40  }
41 
42  return (real_t)temp.tv_sec + (real_t)temp.tv_usec / 1e6;
43 }
44 
45 /* SOME CONVENIENT DEFINTIONS: */
46 /* --------------------------------------------------------------- */
47  #define ACADO_NX 8
48  #define ACADO_NU 2
49  #define ACADO_NZ 10
50  #define ACADO_NE 2
51  #define TIMING 10000
52 /* --------------------------------------------------------------- */
53 /* --------------------------------------------------------------- */
54 
55 void initializeX( double* x ) {
56  int i, j;
57  //~ srand ( time(NULL) );
58  srand ( 36 );
59  /* INITIALIZATION: */
60  /* ---------------------------------------- */
61  for( i = 0; i < (ACADO_NX)*(1+ACADO_NX+ACADO_NU)+ACADO_NE+ACADO_NU; i++ ) {
62  x[i] = (2*((real_t)rand() / (real_t)RAND_MAX)-1);
63  }
64  printf( "input: \n[" );
65  for( i = 0; i < ACADO_NX; i++ ) {
66  if( i < (ACADO_NX-1) ) printf( "%.3g, ", x[i] );
67  else printf( "%.3g]", x[i] );
68  }
69  printf( "\n[" );
70  for( i = 0; i < ACADO_NE; i++ ) {
71  if( i < (ACADO_NE-1) ) printf( "%.3g, ", x[ACADO_NX+i] );
72  else printf( "%.3g]", x[ACADO_NX+i] );
73  }
74  printf( "\n[" );
75  for( i = 0; i < ACADO_NX; i++ ) {
76  for( j = 0; j < ACADO_NX+ACADO_NU; j++ ) {
77  if( j < (ACADO_NX+ACADO_NU-1) ) printf( "%.3g, ", x[ACADO_NX+ACADO_NE+i*(ACADO_NX+ACADO_NU)+j] );
78  else printf( "%.3g;", x[ACADO_NX+ACADO_NE+i*(ACADO_NX+ACADO_NU)+j] );
79  }
80  if( i < (ACADO_NX-1) ) printf( "\n" );
81  else printf( "]" );
82  }
83  printf( "\n[" );
84  for( i = 0; i < ACADO_NU; i++ ) {
85  if( i < (ACADO_NU-1) ) printf( "%.3g, ", x[ACADO_NX+ACADO_NE+ACADO_NX*(ACADO_NX+ACADO_NU)+i] );
86  else printf( "%.3g]\n\n", x[ACADO_NX+ACADO_NE+ACADO_NX*(ACADO_NX+ACADO_NU)+i] );
87  }
88 }
89 
90 typedef struct ACADOworkspace_
91 {
93 double acado_aux[ 800 ];
94 
95 
97 
99 /* --------------------------------------------------------------- */
100 /* --------------------------------------------------------------- */
101 
102 #include "ADsymbolic_output.c"
103 
104 int main(){
105  printf( "------------------------------------------------------------\nRunning test..\n\n" );
106 
107  /* INTRODUCE AUXILIARY VAIRABLES: */
108  /* ------------------------------ */
109  int i, j, k;
110  double x[(ACADO_NX)*(1+ACADO_NX+ACADO_NU)+ACADO_NE+ACADO_NU];
111  int numZ = ACADO_NZ*(ACADO_NZ+1)/2.0;
112  double f1[numZ];
113  double f2[numZ];
114  double diff[numZ];
115  timer t_timer;
116  double t_tmp1, t_tmp2;
117  double error;
118 
119  initializeX( x );
120 
121  // AD SYMMETRIC:
122  tic( &t_timer );
123  for( i = 0; i < TIMING; i++ ) {
124  symmetricDerivative(x, f1);
125  }
126  t_tmp1 = toc( &t_timer )/TIMING;
127 
128  printf( "symmetricDerivative: \n" );
129  i = 0;
130  for( j = 0; j < ACADO_NZ; j++ ) {
131  for( k = 0; k <= j; k++ ) {
132  printf( "%.3g, ", f1[i] );
133  i = i+1;
134  }
135  printf( "\n" );
136  }
137 
138  // ALT SYMMETRIC:
139  tic( &t_timer );
140  for( i = 0; i < TIMING; i++ ) {
141  alternativeSymmetric(x, f2);
142  }
143  t_tmp2 = toc( &t_timer )/TIMING;
144 
145  printf( "\nalternativeSymmetric: \n" );
146  i = 0;
147  for( j = 0; j < ACADO_NZ; j++ ) {
148  for( k = 0; k <= j; k++ ) {
149  printf( "%.3g, ", f2[i] );
150  i = i+1;
151  }
152  printf( "\n" );
153  }
154 
155  // ERROR:
156  printf( "\n------------------------------\nDifference: \n" );
157  error = -1e10;
158  i = 0;
159  for( j = 0; j < ACADO_NZ; j++ ) {
160  for( k = 0; k <= j; k++ ) {
161  diff[i] = fabs( f1[i] - f2[i] );
162  if( diff[i] > error ) error = diff[i];
163  printf( "%.3g, ", diff[i] );
164  i = i+1;
165  }
166  printf( "\n" );
167  }
168  printf( "\n" );
169  printf( "Error: %.3g\n------------------------------\n", error );
170 
171  printf( "Timing symmetricDerivative : %.3g μs\n", 1e6*t_tmp1 );
172  printf( "Timing alternativeSymmetric: %.3g μs\n\n", 1e6*t_tmp2 );
173 
174  printf( "done!\n------------------------------------------------------------\n" );
175  return 0;
176 }
Expression symmetricDerivative(const Expression &arg1, const Expression &arg2, const Expression &forward_seed, const Expression &backward_seed, Expression *forward_result, Expression *backward_result)
struct timer timer
ACADOworkspace acadoWorkspace
Definition: AD_test.c:98
void initializeX(double *x)
Definition: AD_test.c:55
#define ACADO_NZ
Definition: AD_test.c:49
#define ACADO_NX
Definition: AD_test.c:47
Definition: AD_test.c:12
#define ACADO_NE
Definition: AD_test.c:50
#define TIMING
Definition: AD_test.c:51
struct timeval tic
Definition: AD_test.c:14
struct ACADOworkspace_ ACADOworkspace
#define ACADO_NU
Definition: AD_test.c:48
int main()
Definition: AD_test.c:104
double real_t
Definition: AD_test.c:10
struct timeval toc
Definition: AD_test.c:15


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:27