AD_test.c
Go to the documentation of this file.
00001 #include <math.h>
00002 #include <string.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 /* Use POSIX clock_gettime() for timing on non-Windows machines. */
00006 #include <time.h>
00007 #include <sys/stat.h>
00008 #include <sys/time.h>
00009 
00010 typedef double real_t;
00011 
00012 typedef struct timer
00013 {
00014         struct timeval tic;
00015         struct timeval toc;
00016 } timer;
00017 
00018 /* read current time */
00019 void tic(timer* t)
00020 {
00021         gettimeofday(&t->tic, 0);
00022 }
00023 
00024 /* return time passed since last call to tic on this timer */
00025 real_t toc(timer* t)
00026 {
00027         struct timeval temp;
00028         
00029         gettimeofday(&t->toc, 0);
00030     
00031         if ((t->toc.tv_usec - t->tic.tv_usec) < 0)
00032         {
00033                 temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec - 1;
00034                 temp.tv_usec = 1000000 + t->toc.tv_usec - t->tic.tv_usec;
00035         }
00036         else
00037         {
00038                 temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec;
00039                 temp.tv_usec = t->toc.tv_usec - t->tic.tv_usec;
00040         }
00041         
00042         return (real_t)temp.tv_sec + (real_t)temp.tv_usec / 1e6;
00043 }
00044 
00045 /* SOME CONVENIENT DEFINTIONS: */
00046 /* --------------------------------------------------------------- */
00047    #define ACADO_NX           8      
00048    #define ACADO_NU           2    
00049    #define ACADO_NZ                       10
00050    #define ACADO_NE           2     
00051    #define TIMING             10000     
00052 /* --------------------------------------------------------------- */
00053 /* --------------------------------------------------------------- */
00054 
00055 void initializeX( double* x ) {
00056         int i, j;
00057         //~ srand ( time(NULL) );
00058         srand ( 36 );
00059         /* INITIALIZATION: */
00060         /* ---------------------------------------- */
00061         for( i = 0; i < (ACADO_NX)*(1+ACADO_NX+ACADO_NU)+ACADO_NE+ACADO_NU; i++ ) {
00062                 x[i] = (2*((real_t)rand() / (real_t)RAND_MAX)-1);
00063         }
00064         printf( "input: \n[" );
00065         for( i = 0; i < ACADO_NX; i++ ) {
00066                 if( i < (ACADO_NX-1) )  printf( "%.3g, ", x[i] );
00067                 else                                    printf( "%.3g]", x[i] );
00068         }
00069         printf( "\n[" );
00070         for( i = 0; i < ACADO_NE; i++ ) {
00071                 if( i < (ACADO_NE-1) )  printf( "%.3g, ", x[ACADO_NX+i] );
00072                 else                                    printf( "%.3g]", x[ACADO_NX+i] );
00073         }
00074         printf( "\n[" );
00075         for( i = 0; i < ACADO_NX; i++ ) {
00076                 for( j = 0; j < ACADO_NX+ACADO_NU; j++ ) {
00077                         if( j < (ACADO_NX+ACADO_NU-1) )         printf( "%.3g, ", x[ACADO_NX+ACADO_NE+i*(ACADO_NX+ACADO_NU)+j] );
00078                         else                                                            printf( "%.3g;", x[ACADO_NX+ACADO_NE+i*(ACADO_NX+ACADO_NU)+j] );
00079                 }
00080                 if( i < (ACADO_NX-1) )  printf( "\n" );
00081                 else                                    printf( "]" );
00082         }
00083         printf( "\n[" );
00084         for( i = 0; i < ACADO_NU; i++ ) {
00085                 if( i < (ACADO_NU-1) )  printf( "%.3g, ", x[ACADO_NX+ACADO_NE+ACADO_NX*(ACADO_NX+ACADO_NU)+i] );
00086                 else                                    printf( "%.3g]\n\n", x[ACADO_NX+ACADO_NE+ACADO_NX*(ACADO_NX+ACADO_NU)+i] );
00087         }
00088 }
00089 
00090 typedef struct ACADOworkspace_
00091 {
00093 double acado_aux[ 800 ];
00094 
00095 
00096 } ACADOworkspace;
00097 
00098 ACADOworkspace acadoWorkspace;
00099 /* --------------------------------------------------------------- */
00100 /* --------------------------------------------------------------- */
00101 
00102 #include "ADsymbolic_output.c"
00103 
00104 int main(){
00105         printf( "------------------------------------------------------------\nRunning test..\n\n" );
00106 
00107         /* INTRODUCE AUXILIARY VAIRABLES: */
00108         /* ------------------------------ */
00109         int i, j, k;
00110         double x[(ACADO_NX)*(1+ACADO_NX+ACADO_NU)+ACADO_NE+ACADO_NU];
00111         int numZ = ACADO_NZ*(ACADO_NZ+1)/2.0;
00112         double f1[numZ];
00113         double f2[numZ];
00114         double diff[numZ];
00115         timer t_timer;
00116         double t_tmp1, t_tmp2;
00117         double error;
00118         
00119         initializeX( x );
00120         
00121         // AD SYMMETRIC:
00122         tic( &t_timer );
00123         for( i = 0; i < TIMING; i++ ) {
00124                 symmetricDerivative(x, f1);
00125         }
00126         t_tmp1 = toc( &t_timer )/TIMING;
00127         
00128         printf( "symmetricDerivative: \n" );
00129         i = 0;
00130         for( j = 0; j < ACADO_NZ; j++ ) {
00131                 for( k = 0; k <= j; k++ ) {
00132                         printf( "%.3g, ", f1[i] );
00133                         i = i+1;
00134                 }
00135                 printf( "\n" );
00136         }
00137         
00138         // ALT SYMMETRIC:
00139         tic( &t_timer );
00140         for( i = 0; i < TIMING; i++ ) {
00141                 alternativeSymmetric(x, f2);
00142         }
00143         t_tmp2 = toc( &t_timer )/TIMING;
00144         
00145         printf( "\nalternativeSymmetric: \n" );
00146         i = 0;
00147         for( j = 0; j < ACADO_NZ; j++ ) {
00148                 for( k = 0; k <= j; k++ ) {
00149                         printf( "%.3g, ", f2[i] );
00150                         i = i+1;
00151                 }
00152                 printf( "\n" );
00153         }
00154         
00155         // ERROR:
00156         printf( "\n------------------------------\nDifference: \n" );
00157         error = -1e10;
00158         i = 0;
00159         for( j = 0; j < ACADO_NZ; j++ ) {
00160                 for( k = 0; k <= j; k++ ) {
00161                         diff[i] = fabs( f1[i] - f2[i] );
00162                         if( diff[i] > error ) error = diff[i];
00163                         printf( "%.3g, ", diff[i] );
00164                         i = i+1;
00165                 }
00166                 printf( "\n" );
00167         }
00168         printf( "\n" );
00169         printf( "Error: %.3g\n------------------------------\n", error );
00170         
00171         printf( "Timing symmetricDerivative :   %.3g μs\n", 1e6*t_tmp1 );
00172         printf( "Timing alternativeSymmetric:   %.3g μs\n\n", 1e6*t_tmp2 );
00173 
00174         printf( "done!\n------------------------------------------------------------\n" );
00175         return 0;
00176 }


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:57:48