matlab_integrator_ode.cpp
Go to the documentation of this file.
1 
34 #include <acado_toolkit.hpp> // Include the ACADO toolkit
35 #include <acado/utils/matlab_acado_utils.hpp> // Include specific Matlab utils
36 
37 USING_NAMESPACE_ACADO // Open the namespace
38 
39 mxArray* ModelFcn_f = NULL; // Globals
40 mxArray* ModelFcn_T = NULL;
41 mxArray* ModelFcn_X = NULL;
42 mxArray* ModelFcn_XA = NULL;
43 mxArray* ModelFcn_U = NULL;
44 mxArray* ModelFcn_P = NULL;
45 mxArray* ModelFcn_W = NULL;
46 unsigned int ModelFcn_NT = 0;
47 unsigned int ModelFcn_NX = 0;
48 unsigned int ModelFcn_NXA = 0;
49 unsigned int ModelFcn_NU = 0;
50 unsigned int ModelFcn_NP = 0;
51 unsigned int ModelFcn_NW = 0;
52 
53 
54 void clearAllGlobals( ){ // Helper method to clear all global variables. Don't modify this method.
55 
56  if ( ModelFcn_f != NULL ){
57  mxDestroyArray( ModelFcn_f );
58  ModelFcn_f = NULL;
59  }
60 
61  if ( ModelFcn_T != NULL ){
62  mxDestroyArray( ModelFcn_T );
63  ModelFcn_T = NULL;
64  }
65 
66  if ( ModelFcn_X != NULL ){
67  mxDestroyArray( ModelFcn_X );
68  ModelFcn_X = NULL;
69  }
70 
71  if ( ModelFcn_XA != NULL ){
72  mxDestroyArray( ModelFcn_XA );
73  ModelFcn_XA = NULL;
74  }
75 
76  if ( ModelFcn_U != NULL ){
77  mxDestroyArray( ModelFcn_U );
78  ModelFcn_U = NULL;
79  }
80 
81  if ( ModelFcn_P != NULL ){
82  mxDestroyArray( ModelFcn_P );
83  ModelFcn_P = NULL;
84  }
85 
86  if ( ModelFcn_W != NULL ){
87  mxDestroyArray( ModelFcn_W );
88  ModelFcn_W = NULL;
89  }
90 
91  ModelFcn_NT = 0;
92  ModelFcn_NX = 0;
93  ModelFcn_NXA = 0;
94  ModelFcn_NU = 0;
95  ModelFcn_NP = 0;
96  ModelFcn_NW = 0;
97 }
98 
99 void genericODE( double* x, double* f, void *userData ){ // Helper function to call ODE's. Don't modify this method.
100  unsigned int i;
101  double* tt = mxGetPr( ModelFcn_T );
102  tt[0] = x[0];
103  double* xx = mxGetPr( ModelFcn_X );
104  for( i=0; i<ModelFcn_NX; ++i )
105  xx[i] = x[i+1];
106  double* uu = mxGetPr( ModelFcn_U );
107  for( i=0; i<ModelFcn_NU; ++i )
108  uu[i] = x[i+1+ModelFcn_NX];
109  double* pp = mxGetPr( ModelFcn_P );
110  for( i=0; i<ModelFcn_NP; ++i )
111  pp[i] = x[i+1+ModelFcn_NX+ModelFcn_NU];
112  double* ww = mxGetPr( ModelFcn_W );
113  for( i=0; i<ModelFcn_NW; ++i )
114  ww[i] = x[i+1+ModelFcn_NX+ModelFcn_NU+ModelFcn_NP];
115  mxArray* FF = NULL;
117  mxArray* argOut[] = { FF };
118 
119  mexCallMATLAB( 1,argOut, 6,argIn,"generic_ode" ); // Call generic_ode
120  double* ff = mxGetPr( *argOut );
121  for( i=0; i<ModelFcn_NX; ++i ){
122  f[i] = ff[i];
123  }
124 
125  mxDestroyArray( *argOut );
126 }
127 
128 
129 
130 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) // Start the MEX function. Do NOT change the header of this function.
131  {
132  clearAllStaticCounters( ); // Clear software counters
133 
134  // Set sizes
135  ModelFcn_NT = 1; ModelFcn_T = mxCreateDoubleMatrix( ModelFcn_NT, 1,mxREAL );
136  ModelFcn_NX = 2; ModelFcn_X = mxCreateDoubleMatrix( ModelFcn_NX, 1,mxREAL );
137  ModelFcn_NXA = 0; ModelFcn_XA = mxCreateDoubleMatrix( ModelFcn_NXA, 1,mxREAL );
138  ModelFcn_NP = 1; ModelFcn_U = mxCreateDoubleMatrix( ModelFcn_NP, 1,mxREAL );
139  ModelFcn_NU = 1; ModelFcn_P = mxCreateDoubleMatrix( ModelFcn_NU, 1,mxREAL );
140  ModelFcn_NW = 0; ModelFcn_W = mxCreateDoubleMatrix( ModelFcn_NW, 1,mxREAL );
141 
142  // ODE name
143  ModelFcn_f = mxCreateString("matlab_integrator_ode_pendulum");
144 
145  // Set expressions
146  DifferentialState phi, dphi;
147  Control u;
148  Parameter p;
149  TIME t;
150 
151  // Set intermediate state to be passed to the ode
152  IntermediateState x(5);
153  x(0) = t ;
154  x(1) = phi ;
155  x(2) = dphi;
156  x(3) = u ;
157  x(4) = p ;
158 
159  // Link ODE
160  CFunction pendulumModel( ModelFcn_NX, genericODE );
161 
162 
163 
164  // Define a Right-Hand-Side:
165  // -------------------------
166 
168  f << pendulumModel(x);
169 
170 
171  // DEFINE AN INTEGRATOR:
172  // ---------------------
173  IntegratorRK45 integrator( f );
174 
175 
176  // DEFINE INITIAL VALUES:
177  // ----------------------
178 
179  double x_start[2] = { 0.0, 0.0 };
180  double u_ [1] = { 1.0 };
181  double p_ [1] = { 1.0 };
182 
183 
184  double t_start = 0.0;
185  double t_end = 1.0;
186 
187 
188  // START THE INTEGRATION:
189  // ----------------------
190  integrator.set(INTEGRATOR_PRINTLEVEL, HIGH );
191  integrator.freezeAll();
192  integrator.integrate( t_start, t_end, x_start, 0, p_, u_ );
193 
194 
195  // DEFINE A SEED MATRIX:
196  // ---------------------
197  Vector seed1(2);
198  Vector seed2(2);
199 
200  seed1(0) = 1.0;
201  seed1(1) = 0.0;
202 
203  seed2(0) = 1.0;
204  seed2(1) = 0.0;
205 
206  // COMPUTE FIRST ORDER DERIVATIVES:
207  // --------------------------------
208  integrator.setForwardSeed(1,seed1);
209  integrator.integrateSensitivities();
210 
211  // COMPUTE SECOND ORDER DERIVATIVES:
212  // ---------------------------------
213  integrator.setForwardSeed(2,seed2);
214  integrator.integrateSensitivities();
215 
216 
217 
218  clearAllGlobals( ); // Clear all globals
219  clearAllStaticCounters( ); // Clear software counters
220 }
221 
void genericODE(double *x, double *f, void *userData)
mxArray * ModelFcn_P
unsigned int ModelFcn_NXA
returnValue set(OptionsName name, int value)
unsigned int ModelFcn_NU
#define USING_NAMESPACE_ACADO
mxArray * ModelFcn_XA
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mxArray * ModelFcn_W
unsigned int ModelFcn_NX
unsigned int ModelFcn_NP
mxArray * ModelFcn_U
void clearAllGlobals()
unsigned int ModelFcn_NW
returnValue setForwardSeed(const int &order, const DVector &xSeed, const DVector &pSeed=emptyVector, const DVector &uSeed=emptyVector, const DVector &wSeed=emptyVector)
Definition: integrator.cpp:308
mxArray * ModelFcn_X
USING_NAMESPACE_ACADO mxArray * ModelFcn_f
virtual returnValue freezeAll()
returnValue clearAllStaticCounters()
Implements the Runge-Kutta-45 scheme for integrating ODEs.
unsigned int ModelFcn_NT
const double t_end
const double t_start
(no description yet)
Definition: c_function.hpp:54
returnValue integrateSensitivities()
Definition: integrator.cpp:357
mxArray * ModelFcn_T
returnValue integrate(double t0, double tend, double *x0, double *xa=0, double *p=0, double *u=0, double *w=0)
Definition: integrator.cpp:207
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.


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