matlab_integrator_ode.cpp
Go to the documentation of this file.
00001 
00034 #include <acado_toolkit.hpp>                    // Include the ACADO toolkit
00035 #include <acado/utils/matlab_acado_utils.hpp>   // Include specific Matlab utils
00036 
00037 USING_NAMESPACE_ACADO                           // Open the namespace
00038 
00039 mxArray* ModelFcn_f = NULL;                     // Globals
00040 mxArray* ModelFcn_T  = NULL;
00041 mxArray* ModelFcn_X  = NULL;
00042 mxArray* ModelFcn_XA = NULL;
00043 mxArray* ModelFcn_U  = NULL;
00044 mxArray* ModelFcn_P  = NULL;
00045 mxArray* ModelFcn_W  = NULL;
00046 unsigned int ModelFcn_NT  = 0;
00047 unsigned int ModelFcn_NX  = 0;
00048 unsigned int ModelFcn_NXA = 0;
00049 unsigned int ModelFcn_NU  = 0;
00050 unsigned int ModelFcn_NP  = 0;
00051 unsigned int ModelFcn_NW  = 0;
00052 
00053 
00054 void clearAllGlobals( ){                   // Helper method to clear all global variables. Don't modify this method.
00055 
00056     if ( ModelFcn_f != NULL ){
00057         mxDestroyArray( ModelFcn_f );
00058         ModelFcn_f = NULL;
00059     }
00060 
00061     if ( ModelFcn_T != NULL ){
00062         mxDestroyArray( ModelFcn_T );
00063         ModelFcn_T = NULL;
00064     }
00065 
00066     if ( ModelFcn_X != NULL ){
00067         mxDestroyArray( ModelFcn_X );
00068         ModelFcn_X = NULL;
00069     }
00070 
00071     if ( ModelFcn_XA != NULL ){
00072         mxDestroyArray( ModelFcn_XA );
00073         ModelFcn_XA = NULL;
00074     }
00075 
00076     if ( ModelFcn_U != NULL ){
00077         mxDestroyArray( ModelFcn_U );
00078         ModelFcn_U = NULL;
00079     }
00080 
00081     if ( ModelFcn_P != NULL ){
00082         mxDestroyArray( ModelFcn_P );
00083         ModelFcn_P = NULL;
00084     }
00085 
00086     if ( ModelFcn_W != NULL ){
00087         mxDestroyArray( ModelFcn_W );
00088         ModelFcn_W = NULL;
00089     }
00090 
00091     ModelFcn_NT  = 0;
00092     ModelFcn_NX  = 0;
00093     ModelFcn_NXA = 0;
00094     ModelFcn_NU  = 0;
00095     ModelFcn_NP  = 0;
00096     ModelFcn_NW  = 0;
00097 }
00098 
00099 void genericODE( double* x, double* f, void *userData ){   // Helper function to call ODE's. Don't modify this method.
00100     unsigned int i;
00101     double* tt = mxGetPr( ModelFcn_T );
00102     tt[0] = x[0];
00103     double* xx = mxGetPr( ModelFcn_X );
00104     for( i=0; i<ModelFcn_NX; ++i )
00105         xx[i] = x[i+1];
00106     double* uu = mxGetPr( ModelFcn_U );
00107     for( i=0; i<ModelFcn_NU; ++i )
00108         uu[i] = x[i+1+ModelFcn_NX];
00109     double* pp = mxGetPr( ModelFcn_P );
00110     for( i=0; i<ModelFcn_NP; ++i )
00111         pp[i] = x[i+1+ModelFcn_NX+ModelFcn_NU];
00112     double* ww = mxGetPr( ModelFcn_W );
00113     for( i=0; i<ModelFcn_NW; ++i )
00114         ww[i] = x[i+1+ModelFcn_NX+ModelFcn_NU+ModelFcn_NP];
00115     mxArray* FF = NULL;
00116     mxArray* argIn[]  = { ModelFcn_f,ModelFcn_T,ModelFcn_X,ModelFcn_U,ModelFcn_P,ModelFcn_W };
00117     mxArray* argOut[] = { FF };
00118 
00119     mexCallMATLAB( 1,argOut, 6,argIn,"generic_ode" );       // Call generic_ode
00120     double* ff = mxGetPr( *argOut );
00121     for( i=0; i<ModelFcn_NX; ++i ){
00122         f[i] = ff[i];
00123     }
00124 
00125     mxDestroyArray( *argOut );
00126 }
00127 
00128 
00129 
00130 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )  // Start the MEX function. Do NOT change the header of this function.
00131  { 
00132     clearAllStaticCounters( );                  // Clear software counters
00133     
00134     // Set sizes
00135     ModelFcn_NT  = 1;   ModelFcn_T  = mxCreateDoubleMatrix( ModelFcn_NT,  1,mxREAL );
00136     ModelFcn_NX  = 2;   ModelFcn_X  = mxCreateDoubleMatrix( ModelFcn_NX,  1,mxREAL );
00137     ModelFcn_NXA = 0;   ModelFcn_XA = mxCreateDoubleMatrix( ModelFcn_NXA, 1,mxREAL );
00138     ModelFcn_NP  = 1;   ModelFcn_U  = mxCreateDoubleMatrix( ModelFcn_NP,  1,mxREAL );
00139     ModelFcn_NU  = 1;   ModelFcn_P  = mxCreateDoubleMatrix( ModelFcn_NU,  1,mxREAL );
00140     ModelFcn_NW  = 0;   ModelFcn_W  = mxCreateDoubleMatrix( ModelFcn_NW,  1,mxREAL );
00141     
00142     // ODE name
00143     ModelFcn_f = mxCreateString("matlab_integrator_ode_pendulum");
00144 
00145     // Set expressions
00146     DifferentialState phi, dphi;
00147     Control u;
00148     Parameter p;
00149     TIME t;
00150     
00151     // Set intermediate state to be passed to the ode
00152     IntermediateState x(5);
00153     x(0) = t   ;
00154     x(1) = phi ;
00155     x(2) = dphi;
00156     x(3) = u   ;
00157     x(4) = p   ;
00158     
00159     // Link ODE
00160     CFunction pendulumModel( ModelFcn_NX, genericODE );
00161 
00162     
00163     
00164     // Define a Right-Hand-Side:
00165     // -------------------------
00166 
00167     DifferentialEquation f;
00168     f << pendulumModel(x);
00169 
00170 
00171     // DEFINE AN INTEGRATOR:
00172     // ---------------------
00173     IntegratorRK45 integrator( f );
00174 
00175 
00176     // DEFINE INITIAL VALUES:
00177     // ----------------------
00178 
00179     double x_start[2] = { 0.0, 0.0 };
00180     double u_     [1] = { 1.0      };
00181     double p_     [1] = { 1.0      };
00182 
00183 
00184     double t_start    =  0.0;
00185     double t_end      =  1.0;
00186 
00187 
00188     // START THE INTEGRATION:
00189     // ----------------------
00190     integrator.set(INTEGRATOR_PRINTLEVEL, HIGH );
00191     integrator.freezeAll();
00192     integrator.integrate( t_start, t_end, x_start, 0, p_, u_ );
00193 
00194 
00195     // DEFINE A SEED MATRIX:
00196     // ---------------------
00197     Vector seed1(2);
00198     Vector seed2(2);
00199 
00200     seed1(0) = 1.0;
00201     seed1(1) = 0.0;
00202 
00203     seed2(0) = 1.0;
00204     seed2(1) = 0.0;
00205 
00206     // COMPUTE FIRST ORDER DERIVATIVES:
00207     // --------------------------------
00208     integrator.setForwardSeed(1,seed1);
00209     integrator.integrateSensitivities();
00210 
00211     // COMPUTE SECOND ORDER DERIVATIVES:
00212     // ---------------------------------
00213     integrator.setForwardSeed(2,seed2);
00214     integrator.integrateSensitivities();
00215     
00216     
00217     
00218     clearAllGlobals( );                         // Clear all globals
00219     clearAllStaticCounters( );                  // Clear software counters
00220 } 
00221 


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