39 #include "model_include.hpp" 75 if ( modelFcn != NULL )
173 uu[i] = x[i+1+modelFcnNX];
177 pp[i] = x[i+1+modelFcnNX+modelFcnNU];
181 ww[i] = x[i+1+modelFcnNX+modelFcnNU+modelFcnNP];
186 mxArray* argOut[] = { FF };
188 mexCallMATLAB( 1,argOut, 6,argIn,
"generic_ode" );
191 double* ff = mxGetPr( *argOut );
196 mxDestroyArray( *argOut );
201 void genericJacobian (
int number,
double* x,
double* seed,
double* f,
double* df,
void *userData )
255 uu[i] = x[i+1+modelFcnNX];
259 pp[i] = x[i+1+modelFcnNX+modelFcnNU];
263 ww[i] = x[i+1+modelFcnNX+modelFcnNU+modelFcnNP];
268 mxArray* argOut[] = { FF };
271 mexCallMATLAB( 1,argOut, 6,argIn,
"generic_jacobian" );
274 unsigned int rowLen = mxGetM(*argOut);
275 unsigned int colLen = mxGetN(*argOut);
280 mexErrMsgTxt(
"ERROR (ACADOintegrator): Jacobian matrix rows do not match (should be modelFcnNX+modelFcnNXA). " );
283 if (colLen != modelFcnNX+
modelFcnNXA+modelFcnNU+modelFcnNP+modelFcnNW)
285 mexErrMsgTxt(
"ERROR (ACADOintegrator): Jacobian matrix columns do not match (should be modelFcnNX+modelFcnNXA+modelFcnNU+modelFcnNP+modelFcnNW). " );
288 J = mxGetPr( *argOut );
297 for (j=0; j < modelFcnNX+modelFcnNXA+modelFcnNU+modelFcnNP+
modelFcnNW; ++j){
298 df[i] += J[(j*(modelFcnNX+
modelFcnNXA))+i]*seed[j+1];
308 mxArray* argOut2[] = { FF2 };
310 mexCallMATLAB( 1,argOut2, 6,argIn2,
"generic_ode" );
313 ff = mxGetPr( *argOut2 );
314 memcpy(
f_store, ff, (modelFcnNX) *
sizeof (
double ));
322 mxDestroyArray( *argOut );
323 mxDestroyArray( *argOut2 );
348 xxa[i] = x[i+1+modelFcnNX];
352 uu[i] = x[i+1+modelFcnNX+modelFcnNXA];
356 pp[i] = x[i+1+modelFcnNX+modelFcnNXA+modelFcnNU];
360 ww[i] = x[i+1+modelFcnNX+modelFcnNXA+modelFcnNU+modelFcnNP];
367 mxArray* argOut[] = { FF };
371 mexCallMATLAB( 1,argOut, 7,argIn_f,
"generic_dae" );
372 ff = mxGetPr( *argOut );
378 mxDestroyArray( *argOut );
392 if ( ( PrintLevel_ != NULL ) && ( PrintLevel_[0] <= 0.0 ) )
394 switch ( returnvalue )
397 mexPrintf(
"ERROR (ACADOintegrator): The integrator misses some inputs.\n" );
400 mexPrintf(
"ERROR (ACADOintegrator): The integration interval is too small or negative.\n" );
403 mexPrintf(
"ERROR (ACADOintegrator): The maximum number of integration steps is exceeded.\n" );
406 mexPrintf(
"ERROR (ACADOintegrator): At least one of the inputs has a wrong dimension.\n" );
412 mexPrintf(
"ERROR (ACADOintegrator): Unsuccessful return from the integrator.\n");
426 if( strcmp(integratorName,
"DiscretizedODE") == 0 )
431 if ( modelName != NULL )
435 fcn = allocateFunctionPointer( modelName );
441 mexErrMsgTxt(
"ERROR (ACADOintegrator): A model with the specified name does not exist.\n" );
464 mexErrMsgTxt(
"bug!" );
466 if( strcmp(integratorName,
"RK12") == 0 )
469 if( strcmp(integratorName,
"RK23") == 0 )
472 if( strcmp(integratorName,
"RK45") == 0 )
475 if( strcmp(integratorName,
"RK78") == 0 )
478 if( strcmp(integratorName,
"DiscretizedODE") == 0 )
481 if( strcmp(integratorName,
"BDF") == 0 )
514 int nrhs,
const mxArray *prhs[]
524 const mxArray *IntegratorSettings = NULL;
527 double *xStart = NULL;
530 double *xaStart = NULL;
533 double *tStart = NULL;
554 mxArray *ModelName = NULL;
555 char *modelName = NULL;
558 mxArray *IntegratorName = NULL;
559 char *integratorName = NULL;
566 mxArray *aTol = NULL;
570 mxArray *MaxNumStep = NULL;
571 double *maxNumStep = NULL;
574 mxArray *MinStep = NULL;
575 double *minStep = NULL;
578 mxArray *InitialStep = NULL;
579 double *initialStep = NULL;
582 mxArray *MaxStep = NULL;
583 double *maxStep = NULL;
586 mxArray *StepTuning = NULL;
587 double *stepTuning = NULL;
591 char *linearAlgebraSolver = NULL;
606 mxArray *DxInit = NULL;
607 double *dxInit = NULL;
610 mxArray *SensitivityMode = NULL;
611 char *sensitivityMode = NULL;
614 mxArray *Bseed = NULL;
615 double *bseed = NULL;
634 mxArray *Bseed2 = NULL;
635 double *bseed2 = NULL;
654 mxArray *PrintLevel_ = NULL;
655 double *printLevel_ = NULL;
658 mxArray *PlotXTrajectory = NULL;
659 mxArray *PlotXaTrajectory = NULL;
662 mxArray *UseSubplots = NULL;
665 mxArray *Jacobian = NULL;
673 double *xaEnd = NULL;
677 double *statusPtr = NULL;
680 mxArray *NumberOfSteps = NULL;
681 double *numberOfSteps = NULL;
684 mxArray *NumberOfRejectedSteps = NULL;
685 double *numberOfRejectedSteps = NULL;
688 mxArray *GetStepSize = NULL;
689 double *getStepSize = NULL;
692 mxArray *CorrectorTolerance = NULL;
693 double *correctorTolerance = NULL;
696 mxArray *XTrajectory = NULL;
697 double *xTrajectory = NULL;
700 mxArray *XaTrajectory = NULL;
701 double *xaTrajectory = NULL;
744 mxArray *StorageResolution = NULL;
745 double *storageResolution = NULL;
751 unsigned int nxa = 0;
755 unsigned int nbDir = 0;
756 unsigned int nfDir = 0;
757 unsigned int nbDir2 = 0;
758 unsigned int nfDir2 = 0;
764 if ( ( nrhs < 4 ) || ( nrhs > 5 ) )
765 mexErrMsgTxt(
"ERROR (ACADOintegrator): Invalid number of input arguments!\n Type 'help ACADOintegrators' for further information." );
767 if ( ( nlhs < 1 ) || ( nlhs > 3 ) )
768 mexErrMsgTxt(
"ERROR (ACADOintegrator): Invalid number of output arguments!\n Type 'help ACADOintegrators' for further information." );
774 if( !mxIsEmpty( prhs[0]) )
776 IntegratorSettings = prhs[0];
777 if ( !mxIsStruct( IntegratorSettings ) )
778 mexErrMsgTxt(
"ERROR (ACADOintegrator): No integrator settings defined!" );
781 mexErrMsgTxt(
"ERROR (ACADOintegrator): No integrator settings defined!" );
783 if( !mxIsEmpty( prhs[1]) )
785 xStart = mxGetPr( prhs[1] );
786 nx = mxGetM( prhs[1] );
788 if ( mxGetN( prhs[1] ) != 1 )
789 mexErrMsgTxt(
"ERROR (ACADOintegrator): Start value for differential state needs to be given as column vector." );
792 mexErrMsgTxt(
"ERROR (ACADOintegrator): No initial value specified." );
809 if( !mxIsEmpty( prhs[2]) )
811 xaStart = mxGetPr( prhs[2] );
812 nxa = mxGetM( prhs[2] );
814 if ( mxGetN( prhs[2] ) != 1 )
815 mexErrMsgTxt(
"ERROR (ACADOintegrator): Start value for algebraic state needs to be given as column vector." );
819 if( !mxIsEmpty( prhs[tStartIdx]) )
821 tStart = mxGetPr( prhs[tStartIdx] );
823 if (
isScalar( prhs[tStartIdx] ) == 0 )
824 mexErrMsgTxt(
"ERROR (ACADOintegrator): Start time needs to be a scalar." );
827 mexErrMsgTxt(
"ERROR (ACADOintegrator): No start time defined." );
830 if( !mxIsEmpty( prhs[tEndIdx]) )
832 tEnd = mxGetPr( prhs[tEndIdx] );
834 if (
isScalar( prhs[tEndIdx] ) == 0 )
835 mexErrMsgTxt(
"ERROR (ACADOintegrator): End time needs to be a scalar." );
838 mexErrMsgTxt(
"ERROR (ACADOintegrator): No end time defined." );
844 ModelName = mxGetField( IntegratorSettings,0,
"Model" );
845 IntegratorName = mxGetField( IntegratorSettings,0,
"Integrator" );
846 Tol = mxGetField( IntegratorSettings,0,
"Tolerance" );
847 aTol = mxGetField( IntegratorSettings,0,
"AbsoluteTolerance" );
848 MaxNumStep = mxGetField( IntegratorSettings,0,
"MaxNumberOfSteps" );
849 MinStep = mxGetField( IntegratorSettings,0,
"MinimumStepSize" );
850 InitialStep = mxGetField( IntegratorSettings,0,
"InitialStepSize" );
851 MaxStep = mxGetField( IntegratorSettings,0,
"MaximumStepSize" );
852 CorrectorTolerance = mxGetField( IntegratorSettings,0,
"CorrectorTolerance" );
853 StepTuning = mxGetField( IntegratorSettings,0,
"StepSizeTuning" );
854 LinearAlgebraSolver = mxGetField( IntegratorSettings,0,
"LinearAlgebraSolver" );
855 U = mxGetField( IntegratorSettings,0,
"u" );
856 P = mxGetField( IntegratorSettings,0,
"p" );
857 W = mxGetField( IntegratorSettings,0,
"w" );
858 DxInit = mxGetField( IntegratorSettings,0,
"dxInit" );
859 SensitivityMode = mxGetField( IntegratorSettings,0,
"SensitivityMode" );
860 Bseed = mxGetField( IntegratorSettings,0,
"mu" );
861 Dx = mxGetField( IntegratorSettings,0,
"lambdaX" );
862 Du = mxGetField( IntegratorSettings,0,
"lambdaU" );
863 Dp = mxGetField( IntegratorSettings,0,
"lambdaP" );
864 Dw = mxGetField( IntegratorSettings,0,
"lambdaW" );
865 Bseed2 = mxGetField( IntegratorSettings,0,
"mu2" );
866 Dx2 = mxGetField( IntegratorSettings,0,
"lambda2X" );
867 Du2 = mxGetField( IntegratorSettings,0,
"lambda2U" );
868 Dp2 = mxGetField( IntegratorSettings,0,
"lambda2P" );
869 Dw2 = mxGetField( IntegratorSettings,0,
"lambda2W" );
870 PrintLevel_ = mxGetField( IntegratorSettings,0,
"PrintLevel" );
871 PlotXTrajectory = mxGetField( IntegratorSettings,0,
"PlotXTrajectory" );
872 PlotXaTrajectory = mxGetField( IntegratorSettings,0,
"PlotXaTrajectory" );
873 UseSubplots = mxGetField( IntegratorSettings,0,
"UseSubplots" );
874 Jacobian = mxGetField( IntegratorSettings,0,
"Jacobian" );
875 StorageResolution = mxGetField( IntegratorSettings,0,
"StorageResolution" );
877 if( !mxIsEmpty(IntegratorName) )
879 if ( !mxIsChar(IntegratorName) )
880 mexErrMsgTxt(
"ERROR (ACADOintegrator): No integrator specified." );
882 integratorName = mxArrayToString( IntegratorName );
885 mexErrMsgTxt(
"ERROR (ACADOintegrator): No integrator specified." );
887 if( !mxIsEmpty(MaxNumStep) )
889 maxNumStep = mxGetPr( MaxNumStep );
892 mexErrMsgTxt(
"ERROR (ACADOintegrator): Maximum number of steps needs to be a scalar." );
895 if( !mxIsEmpty(MinStep) )
897 minStep = mxGetPr( MinStep );
900 mexErrMsgTxt(
"ERROR (ACADOintegrator): Minimum step size must be a scalar ." );
903 if( !mxIsEmpty(InitialStep) )
905 initialStep = mxGetPr( InitialStep );
908 mexErrMsgTxt(
"ERROR (ACADOintegrator): Initial step size must be a scalar ." );
911 if( !mxIsEmpty(CorrectorTolerance) )
913 correctorTolerance = mxGetPr( CorrectorTolerance );
915 if (
isScalar( CorrectorTolerance ) == 0 )
916 mexErrMsgTxt(
"ERROR (ACADOintegrator): Corrector Tolerance must be a scalar ." );
919 if( !mxIsEmpty(MaxStep) )
921 maxStep = mxGetPr( MaxStep );
924 mexErrMsgTxt(
"ERROR (ACADOintegrator): Maximum step size must be a scalar ." );
927 if( !mxIsEmpty(StepTuning) )
929 stepTuning = mxGetPr( StepTuning );
932 mexErrMsgTxt(
"ERROR (ACADOintegrator): StepTuning size must be a scalar ." );
935 if( !mxIsEmpty(LinearAlgebraSolver) )
937 if ( !mxIsChar(LinearAlgebraSolver) )
938 mexErrMsgTxt(
"ERROR (ACADOintegrator): No linear algebra solver specified." );
940 linearAlgebraSolver = mxArrayToString( LinearAlgebraSolver );
943 mexErrMsgTxt(
"ERROR (ACADOintegrator): No linear algebra solver specified." );
945 if( !mxIsEmpty(Tol) )
947 tol = mxGetPr( Tol );
950 mexErrMsgTxt(
"ERROR (ACADOintegrator): Tolerance needs to be a scalar." );
953 if( !mxIsEmpty(aTol) )
955 atol = mxGetPr( aTol );
958 mexErrMsgTxt(
"ERROR (ACADOintegrator): Absolute Tolerance needs to be a scalar." );
966 if ( mxGetN( U ) != 1 )
967 mexErrMsgTxt(
"ERROR (ACADOintegrator): Controls need to be given as column vector." );
975 if ( mxGetN( P ) != 1 )
976 mexErrMsgTxt(
"ERROR (ACADOintegrator): Parameters need to be given as column vector." );
984 if ( mxGetN( W ) != 1 )
985 mexErrMsgTxt(
"ERROR (ACADOintegrator): Disturbances need to be given as column vector." );
988 if( !mxIsEmpty(DxInit) )
990 dxInit = mxGetPr( DxInit );
991 if ( mxGetM( DxInit ) != nx )
994 mexErrMsgTxt(
"ERROR (ACADOintegrator): Initial value for differential states derivatives has invalid dimensions." );
997 if ( mxGetN( DxInit ) != 1 )
1000 mexErrMsgTxt(
"ERROR (ACADOintegrator): Initial value for differential states derivatives needs to be given as column vector." );
1004 if( !mxIsEmpty(Bseed) )
1006 bseed = mxGetPr( Bseed );
1008 if ( mxGetN( Bseed ) != nx )
1009 mexErrMsgTxt(
"ERROR (ACADOintegrator): Backward seed has incompatible dimensions." );
1011 nbDir = mxGetM( Bseed );
1014 if( !mxIsEmpty(Dx) )
1018 if ( mxGetM( Dx ) != nx )
1019 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1021 nfDir = mxGetN( Dx );
1024 if( !mxIsEmpty(Du) )
1028 if ( mxGetM( Du ) != nx )
1029 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1031 if ( ( nfDir != 0 ) && ( nfDir != mxGetN( Du ) ) )
1032 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1034 nfDir = mxGetN( Du );
1037 if( !mxIsEmpty(Dp) )
1041 if ( mxGetM( Dp ) != nx )
1042 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1044 if ( ( nfDir != 0 ) && ( nfDir != mxGetN( Dp ) ) )
1045 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1047 nfDir = mxGetN( Dp );
1050 if( !mxIsEmpty(Dw) )
1054 if ( mxGetM( Dw ) != nx )
1055 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1057 if ( ( nfDir != 0 ) && ( nfDir != mxGetN( Dw ) ) )
1058 mexErrMsgTxt(
"ERROR (ACADOintegrator): Forward seed has incompatible dimensions." );
1060 nfDir = mxGetN( Dw );
1063 if( !mxIsEmpty(Bseed2) )
1065 bseed2 = mxGetPr( Bseed2 );
1067 if ( mxGetN( Bseed2 ) != nx )
1068 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second backward seed has incompatible dimensions." );
1070 nbDir2 = mxGetM( Bseed2 );
1073 if( !mxIsEmpty(Dx2) )
1075 dx2 = mxGetPr( Dx2 );
1077 if ( mxGetM( Dx2 ) != nx )
1078 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1080 nfDir2 = mxGetN( Dx2 );
1083 if( !mxIsEmpty(Du2) )
1085 du2 = mxGetPr( Du2 );
1087 if ( mxGetM( Du2 ) != nx )
1088 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1090 if ( ( nfDir2 != 0 ) && ( nfDir2 != mxGetN( Du2 ) ) )
1091 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1093 nfDir2 = mxGetN( Du2 );
1096 if( !mxIsEmpty(Dp2) )
1098 dp2 = mxGetPr( Dp2 );
1100 if ( mxGetM( Dp2 ) != nx )
1101 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1103 if ( ( nfDir2 != 0 ) && ( nfDir2 != mxGetN( Dp2 ) ) )
1104 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1106 nfDir2 = mxGetN( Dp2 );
1109 if( !mxIsEmpty(Dw2) )
1111 dw2 = mxGetPr( Dw2 );
1113 if ( mxGetM( Dw2 ) != nx )
1114 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1116 if ( ( nfDir2 != 0 ) && ( nfDir2 != mxGetN( Dw2 ) ) )
1117 mexErrMsgTxt(
"ERROR (ACADOintegrator): Second forward seed has incompatible dimensions." );
1119 nfDir2 = mxGetN( Dw2 );
1122 if( !mxIsEmpty(PrintLevel_) )
1123 printLevel_ = mxGetPr( PrintLevel_ );
1127 if ( mxIsCell(Jacobian) )
1129 if ( ( mxGetN( Jacobian ) == 1 ) && ( mxGetCell( Jacobian,0 ) != NULL ) )
1131 ModelFcn_jac = mxDuplicateArray( mxGetCell( Jacobian,0 ) );
1134 mexErrMsgTxt(
"ERROR (ACADOintegrator): Jacobian: No valid dynamic model specified." );
1136 mexErrMsgTxt(
"ERROR (ACADOintegrator): Jacobian: Could not set Jacobian" );
1141 if( !mxIsEmpty(StorageResolution) )
1143 storageResolution = mxGetPr( StorageResolution );
1145 if (
isScalar( StorageResolution ) == 0 )
1146 mexErrMsgTxt(
"ERROR (ACADOintegrator): Maximum number of steps needs to be a scalar." );
1157 if( !mxIsEmpty(ModelName) )
1159 if ( mxGetM( ModelName ) != 1 )
1160 mexErrMsgTxt(
"ERROR (ACADOintegrator): No valid dynamic model specified." );
1162 if ( mxIsChar(ModelName) )
1164 modelName = mxArrayToString( ModelName );
1168 mexErrMsgTxt(
"ERROR (ACADOintegrator): No valid dynamic model specified." );
1171 mexErrMsgTxt(
"ERROR (ACADOintegrator): Start value for algebraic states has invalid dimension." );
1175 mexErrMsgTxt(
"ERROR (ACADOintegrator): Start value for differential state has invalid dimension." );
1180 if ( mxIsCell(ModelName) )
1183 if ( ( mxGetN( ModelName ) > 0 ) && ( mxGetCell( ModelName,0 ) != NULL ) )
1185 ModelFcn_f = mxDuplicateArray( mxGetCell( ModelName,0 ) );
1188 mexErrMsgTxt(
"ERROR (ACADOintegrator): No valid dynamic model specified." );
1191 mexErrMsgTxt(
"ERROR (ACADOintegrator): No valid dynamic model specified." );
1198 ModelFcnT = mxCreateDoubleMatrix( 1, 1,mxREAL );
1199 ModelFcnX = mxCreateDoubleMatrix( nx, 1,mxREAL );
1200 ModelFcnXA = mxCreateDoubleMatrix( nxa,1,mxREAL );
1201 ModelFcnDX = mxCreateDoubleMatrix( nx, 1,mxREAL );
1202 ModelFcnU = mxCreateDoubleMatrix( nu, 1,mxREAL );
1203 ModelFcnP = mxCreateDoubleMatrix( np, 1,mxREAL );
1204 ModelFcnW = mxCreateDoubleMatrix( nw, 1,mxREAL );
1261 if( !mxIsEmpty(Jacobian) )
1264 *f << cLinkModel(is);
1269 *f << cLinkModel(is);
1275 *f << cLinkModel(is);
1284 mexErrMsgTxt(
"ERROR (ACADOintegrator): No dynamic model specified." );
1288 mexErrMsgTxt(
"ERROR (ACADOintegrator): No dynamic model specified." );
1290 if( !mxIsEmpty(SensitivityMode) )
1292 if ( !mxIsChar(SensitivityMode) )
1295 mexErrMsgTxt(
"ERROR (ACADOintegrator): No valid sensitivity mode specified." );
1298 sensitivityMode = mxArrayToString( SensitivityMode );
1300 if ( ( modelName == NULL ) && ( strcmp(sensitivityMode,
"AD_BACKWARD") == 0 ) )
1303 mexErrMsgTxt(
"ERROR (ACADOintegrator): Adjoint sensitivity generation via function handle not possible." );
1315 mexErrMsgTxt(
"ERROR (ACADOintegrator): Invalid number of output arguments." );
1334 double status = 0.0;
1336 plhs[0] = mxCreateDoubleMatrix( nx,1,mxREAL );
1337 xEnd = mxGetPr( plhs[0] );
1341 plhs[xaEndIdx] = mxCreateDoubleMatrix( nxa,1,mxREAL );
1342 xaEnd = mxGetPr( plhs[xaEndIdx] );
1345 if ( outputIdx > 0 )
1349 const char* outputFieldNames[] = {
"Status",
1351 "NumberOfRejectedSteps",
1367 plhs[outputIdx] = mxCreateStructMatrix( 1,1,16,outputFieldNames );
1374 if ( ( outputIdx > 0 ) || ( !mxIsEmpty( PlotXTrajectory ) ) || ( !mxIsEmpty( PlotXaTrajectory ) ) )
1380 int *maxNumStepInt = 0;
1381 if ( !mxIsEmpty(MaxNumStep) )
1383 maxNumStepInt =
new int;
1384 maxNumStepInt[0] = (int) *maxNumStep;
1394 if ( integrator == NULL )
1397 mexErrMsgTxt(
"ERROR (ACADOintegrator): The specified integrator has not been found.\n");
1403 if( maxNumStep != 0 )
1409 if( initialStep != 0 )
1421 if( stepTuning != 0 )
1424 if( correctorTolerance != 0 )
1447 if( dxInit != NULL )
1450 if( freezeTrajectory ==
BT_TRUE )
1455 Grid timeInterval( *tStart, *tEnd, (
int) *storageResolution );
1469 integrator->
getX ( xEnd_ );
1470 integrator->
getXA( xaEnd_ );
1472 for( i=0; i<nx; ++i )
1477 for( i=0; i<nxa; ++i )
1478 xaEnd[i] = xaEnd_(i);
1485 if ( ( !mxIsEmpty(SensitivityMode) ) && ( status >= 0.0 ) )
1489 if( strcmp(sensitivityMode,
"AD_FORWARD") == 0 )
1491 if ( ( dx == NULL ) && ( du == NULL ) && ( dp == NULL ) && ( dw == NULL ) )
1493 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1494 mexErrMsgTxt(
"ERROR (ACADOintegrator): The forward seed is not defined." );
1505 D_x.transposeInPlace();
1512 D_u.transposeInPlace();
1519 D_p.transposeInPlace();
1526 D_w.transposeInPlace();
1537 for( run1 = 0; run1 < nfDir; run1++ ){
1562 JJ = mxCreateDoubleMatrix( nx+nxa,nfDir,mxREAL );
1565 mxSetField( plhs[outputIdx],0,
"J",JJ );
1570 if( strcmp(sensitivityMode,
"AD_BACKWARD") == 0 )
1574 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1575 mexErrMsgTxt(
"ERROR (ACADOintegrator): The backward seed is not defined." );
1581 xSeed.transposeInPlace();
1590 for( run1 = 0; run1 < nbDir; run1++ ){
1604 DVector JXX(nx+nxa), JPP(np), JUU(nu), JWW(nw);
1618 if ( outputIdx > 0 )
1620 Jx = mxCreateDoubleMatrix( nbDir,nx+nxa,mxREAL );
1623 mxSetField( plhs[outputIdx],0,
"Jx",Jx );
1627 Ju = mxCreateDoubleMatrix( nbDir,nu,mxREAL );
1630 mxSetField( plhs[outputIdx],0,
"Ju",Ju );
1635 Jp = mxCreateDoubleMatrix( nbDir,np,mxREAL );
1638 mxSetField( plhs[outputIdx],0,
"Jp",Jp );
1643 Jw = mxCreateDoubleMatrix( nbDir,nw,mxREAL );
1646 mxSetField( plhs[outputIdx],0,
"Jw",Jw );
1654 if ( strcmp(sensitivityMode,
"AD_FORWARD2") == 0 )
1656 if ( ( dx == NULL ) && ( du == NULL ) && ( dp == NULL ) && ( dw == NULL ) )
1658 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1659 mexErrMsgTxt(
"ERROR (integrator): The forward seed is not defined." );
1662 if ( ( dx2 == NULL ) && ( du2 == NULL ) && ( dp2 == NULL ) && ( dw2 == NULL ) )
1664 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1665 mexErrMsgTxt(
"ERROR (integrator): The second order forward seed is not defined." );
1670 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1671 mexErrMsgTxt(
"ERROR (integrator): More than one first order seed is not allowed in second order mode - please compute the required directions in a loop." );
1682 D_x.transposeInPlace();
1689 D_u.transposeInPlace();
1696 D_p.transposeInPlace();
1703 D_w.transposeInPlace();
1718 D_x2.transposeInPlace();
1725 D_u2.transposeInPlace();
1732 D_p2.transposeInPlace();
1739 D_w2.transposeInPlace();
1749 for( run1 = 0; run1 < nfDir; run1++ ){
1771 for( run2 = 0; run2 < nfDir2; run2++ ){
1799 JJ = mxCreateDoubleMatrix( nx+nxa,nfDir,mxREAL );
1802 mxSetField( plhs[outputIdx],0,
"J",JJ );
1804 JJ2 = mxCreateDoubleMatrix( nx+nxa,nfDir2,mxREAL );
1805 jj2 = mxGetPr( JJ2 );
1807 mxSetField( plhs[outputIdx],0,
"J2",JJ2 );
1813 if ( strcmp(sensitivityMode,
"AD_FORWARD_BACKWARD") == 0 )
1815 if ( ( dx == NULL ) && ( du == NULL ) && ( dp == NULL ) && ( dw == NULL ) )
1817 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1818 mexErrMsgTxt(
"ERROR (integrator): The forward seed is not defined." );
1821 if( bseed2 == NULL )
1823 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1824 mexErrMsgTxt(
"ERROR (integrator): The second order backward seed is not defined." );
1829 delete integrator;
delete maxNumStepInt;
delete f;
delete cModel;
clearAllGlobals( );
1830 mexErrMsgTxt(
"ERROR (integrator): More than one first order seed is not allowed in second order mode - please compute the required directions in a loop." );
1841 D_x.transposeInPlace();
1848 D_u.transposeInPlace();
1855 D_p.transposeInPlace();
1862 D_w.transposeInPlace();
1872 bSeed2.transposeInPlace();
1874 DMatrix J_x2( nbDir2,nx+nxa );
1882 for( run1 = 0; run1 < nfDir; run1++ ){
1904 for( run2 = 0; run2 < nbDir2; run2++ ){
1918 DVector JXX2(nx+nxa), JPP2(np), JUU2(nu), JWW2(nw);
1922 J_x2.
setRow( run2, JXX2 );
1923 J_p2.
setRow( run2, JPP2 );
1924 J_u2.
setRow( run2, JUU2 );
1925 J_w2.
setRow( run2, JWW2 );
1934 JJ = mxCreateDoubleMatrix( nx+nxa,nfDir,mxREAL );
1937 mxSetField( plhs[outputIdx],0,
"J",JJ );
1939 Jx2 = mxCreateDoubleMatrix( nbDir2,nx+nxa,mxREAL );
1940 jx2 = mxGetPr( Jx2 );
1942 mxSetField( plhs[outputIdx],0,
"J2x",Jx2 );
1946 Ju2 = mxCreateDoubleMatrix( nbDir2,nu,mxREAL );
1947 ju2 = mxGetPr( Ju2 );
1949 mxSetField( plhs[outputIdx],0,
"J2u",Ju2 );
1954 Jp2 = mxCreateDoubleMatrix( nbDir2,np,mxREAL );
1955 jp2 = mxGetPr( Jp2 );
1957 mxSetField( plhs[outputIdx],0,
"J2p",Jp2 );
1962 Jw2 = mxCreateDoubleMatrix( nbDir2,nw,mxREAL );
1963 jw2 = mxGetPr( Jw2 );
1965 mxSetField( plhs[outputIdx],0,
"J2w",Jw2 );
1975 if ( ( outputIdx > 0 ) || ( !mxIsEmpty( PlotXTrajectory ) ) || ( !mxIsEmpty( PlotXaTrajectory ) ) )
1977 Status = mxCreateDoubleMatrix( 1,1,mxREAL );
1978 statusPtr = mxGetPr( Status );
1979 statusPtr[0] = status;
1981 if ( status >= 0.0 )
1986 integrator->
getX(out_x);
1989 xTrajectory = mxGetPr( XTrajectory );
1994 xTrajectory[(1+j)*out_x.
getNumPoints() + i] = out_x(i, j);
2003 integrator->
getXA(out_xa);
2006 xaTrajectory = mxGetPr( XaTrajectory );
2011 xaTrajectory[(1+j)*out_xa.
getNumPoints() + i] = out_xa(i, j);
2017 NumberOfSteps = mxCreateDoubleMatrix( 1,1,mxREAL );
2018 numberOfSteps = mxGetPr( NumberOfSteps );
2021 NumberOfRejectedSteps = mxCreateDoubleMatrix( 1,1,mxREAL );
2022 numberOfRejectedSteps = mxGetPr( NumberOfRejectedSteps );
2025 GetStepSize = mxCreateDoubleMatrix( 1,1,mxREAL );
2026 getStepSize = mxGetPr( GetStepSize );
2027 *getStepSize = (double) integrator->
getStepSize();
2032 mxArray *PlotType = mxCreateDoubleMatrix( 1,1,mxREAL );
2033 double *plotType = mxGetPr( PlotType );
2035 mxArray *IsDiscretized = mxCreateDoubleMatrix( 1,1,mxREAL );
2036 double *isDiscretized = mxGetPr( IsDiscretized );
2038 if( strcmp(integratorName,
"DiscretizedODE") == 0 )
2039 isDiscretized[0] = 1.0;
2041 isDiscretized[0] = 0.0;
2044 if ( !mxIsEmpty( PlotXTrajectory ) )
2047 mxArray* plotArguments[] = { XTrajectory,PlotXTrajectory,UseSubplots,
2048 PlotType,IsDiscretized };
2049 mexCallMATLAB( 0,0,5,plotArguments,
"plot_trajectory" );
2052 if ( ( isODE ==
BT_FALSE ) && ( !mxIsEmpty( PlotXaTrajectory ) ) )
2055 mxArray* plotArguments[] = { XaTrajectory,PlotXaTrajectory,UseSubplots,
2056 PlotType,IsDiscretized };
2057 mexCallMATLAB( 0,0,5,plotArguments,
"plot_trajectory" );
2060 mxDestroyArray( IsDiscretized );
2061 mxDestroyArray( PlotType );
2067 if ( outputIdx > 0 )
2069 mxSetField( plhs[outputIdx],0,
"Status",Status );
2070 mxSetField( plhs[outputIdx],0,
"NumberOfSteps",NumberOfSteps );
2071 mxSetField( plhs[outputIdx],0,
"NumberOfRejectedSteps",NumberOfRejectedSteps );
2072 mxSetField( plhs[outputIdx],0,
"xTrajectory",XTrajectory );
2073 mxSetField( plhs[outputIdx],0,
"GetStepSize",GetStepSize );
2076 mxSetField( plhs[outputIdx],0,
"xaTrajectory",XaTrajectory );
2082 if ( outputIdx > 0 )
2083 mxSetField( plhs[outputIdx],0,
"Status",Status );
2095 if ( modelName != NULL )
2098 if( !mxIsEmpty(IntegratorName) )
2099 mxFree(integratorName);
2101 if( !mxIsEmpty(SensitivityMode) )
2102 mxFree(sensitivityMode);
2104 if ( !mxIsEmpty(MaxNumStep) )
2105 delete maxNumStepInt;
uint getNumPoints() const
int isFunctionHandle(const mxArray *const M)
void genericDAE(double *x, double *f, void *userData)
virtual returnValue freezeAll()=0
virtual double getStepSize() const =0
virtual int getNumberOfSteps() const =0
Implements the backward-differentiation formula for integrating DAEs.
A matrix or vector expression mapping an existing array of data.
returnValue set(OptionsName name, int value)
GenericMatrix & setCol(unsigned _idx, const GenericVector< T > &_arg)
#define USING_NAMESPACE_ACADO
DifferentialEquation * allocateDifferentialEquation(char *modelName, char *integratorName)
virtual int getNumberOfRejectedSteps() const =0
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
EIGEN_STRONG_INLINE Index cols() const
Allows to conveniently handle (one-dimensional) grids consisting of time points.
BEGIN_NAMESPACE_ACADO int isScalar(const mxArray *const M)
returnValue getXA(DVector &xaEnd) const
DifferentialEquation * modelFcn
GenericVector< T > getCol(unsigned _idx) const
returnValue getForwardSensitivities(DVector &Dx, int order) const
unsigned int determineNumberOfAlgebraicStates()
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Implements the Runge-Kutta-23 scheme for integrating ODEs.
Abstract base class for all kinds of algorithms for integrating differential equations (ODEs or DAEs)...
Allows to setup and evaluate discretized differential equations based on SymbolicExpressions.
unsigned int jacobianNumber
GenericVector< T > getRow(unsigned _idx) const
Expression jacobian(const Expression &arg1, const Expression &arg2)
uint getNumValues() const
returnValue setForwardSeed(const int &order, const DVector &xSeed, const DVector &pSeed=emptyVector, const DVector &uSeed=emptyVector, const DVector &wSeed=emptyVector)
virtual returnValue setDxInitialization(double *dx0)=0
GenericMatrix & setRow(unsigned _idx, const GenericVector< T > &_values)
Implements the Runge-Kutta-78 scheme for integrating ODEs.
void genericJacobian(int number, double *x, double *seed, double *f, double *df, void *userData)
Integrator * allocateIntegrator(char *integratorName, DifferentialEquation *f)
Derived & setZero(Index size)
EIGEN_STRONG_INLINE Index rows() const
Implements the Runge-Kutta-12 scheme for integrating ODEs.
void genericODE(double *x, double *f, void *userData)
int getNumAlgebraicEquations() const
int getNumDynamicEquations() const
double getTime(uint pointIdx) const
returnValue clearAllStaticCounters()
Implements the Runge-Kutta-45 scheme for integrating ODEs.
returnValue getX(DVector &xEnd) const
returnValue integrateSensitivities()
returnValue getBackwardSensitivities(DVector &Dx_x0, DVector &Dx_p, DVector &Dx_u, DVector &Dx_w, int order) const
void plotErrorMessage(returnValue returnvalue, double *PrintLevel_)
returnValue setBackwardSeed(const int &order, const DVector &seed)
USING_NAMESPACE_ACADO mxArray * ModelFcn_f
returnValue integrate(double t0, double tend, double *x0, double *xa=0, double *p=0, double *u=0, double *w=0)
Implements a scheme for evaluating discretized ODEs.
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.