00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #include <acado/dynamic_discretization/dynamic_discretization.hpp>
00036
00037
00038
00039 BEGIN_NAMESPACE_ACADO
00040
00041
00042
00043
00044
00045
00046
00047
00048 DynamicDiscretization::DynamicDiscretization()
00049 :AlgorithmicBase( ){
00050
00051 setupOptions( );
00052 setupLogging( );
00053
00054 initializeVariables();
00055 }
00056
00057
00058
00059 DynamicDiscretization::DynamicDiscretization( UserInteraction* _userInteraction )
00060 :AlgorithmicBase( _userInteraction ){
00061
00062
00063 if ( _userInteraction == 0 ){
00064
00065 setupOptions( );
00066 setupLogging( );
00067 }
00068 initializeVariables();
00069 }
00070
00071
00072 DynamicDiscretization::DynamicDiscretization( const DynamicDiscretization& arg )
00073 : AlgorithmicBase( arg ){
00074
00075 DynamicDiscretization::copy(arg);
00076 }
00077
00078
00079 DynamicDiscretization::~DynamicDiscretization( ){ }
00080
00081
00082 DynamicDiscretization& DynamicDiscretization::operator=( const DynamicDiscretization& arg ){
00083
00084 if ( this != &arg ){
00085 AlgorithmicBase::operator=(arg);
00086 DynamicDiscretization::copy(arg);
00087 }
00088 return *this;
00089 }
00090
00091
00092 returnValue DynamicDiscretization::setForwardSeed( const BlockMatrix &xSeed_,
00093 const BlockMatrix &pSeed_,
00094 const BlockMatrix &uSeed_,
00095 const BlockMatrix &wSeed_ ){
00096
00097 xSeed = xSeed_;
00098 pSeed = pSeed_;
00099 uSeed = uSeed_;
00100 wSeed = wSeed_;
00101
00102 return SUCCESSFUL_RETURN;
00103 }
00104
00105
00106 returnValue DynamicDiscretization::setUnitForwardSeed(){
00107
00108 BlockMatrix xSeed_( N, 1 );
00109 BlockMatrix pSeed_( N, 1 );
00110 BlockMatrix uSeed_( N, 1 );
00111 BlockMatrix wSeed_( N, 1 );
00112
00113 int run1;
00114 for( run1 = 0; run1 < N; run1++ ){
00115 xSeed_.setIdentity( run1, 0, nx );
00116 pSeed_.setIdentity( run1, 0, np );
00117 uSeed_.setIdentity( run1, 0, nu );
00118 wSeed_.setIdentity( run1, 0, nw );
00119 }
00120 return setForwardSeed( xSeed_, pSeed_, uSeed_, wSeed_ );
00121 }
00122
00123
00124
00125 returnValue DynamicDiscretization::setBackwardSeed( const BlockMatrix &seed ){
00126
00127 bSeed = seed;
00128 return SUCCESSFUL_RETURN;
00129 }
00130
00131
00132 returnValue DynamicDiscretization::setUnitBackwardSeed(){
00133
00134 BlockMatrix seed( 1, N );
00135 int run1;
00136 for( run1 = 0; run1 < N; run1++ )
00137 seed.setIdentity( 0, run1, nx );
00138
00139 return setBackwardSeed( seed );
00140 }
00141
00142
00143
00144 returnValue DynamicDiscretization::getResiduum( BlockMatrix &residuum_ ) const{
00145
00146 int run1;
00147 uint run2;
00148
00149 residuum_.init( N, 1 );
00150
00151 for( run1 = 0; run1 < N; run1++ ){
00152 DMatrix tmp( residuum.getNumValues(), 1 );
00153 for( run2 = 0; run2 < residuum.getNumValues(); run2++ )
00154 tmp( run2, 0 ) = residuum(run1,run2);
00155 residuum_.setDense(run1,0,tmp);
00156 }
00157
00158 return SUCCESSFUL_RETURN;
00159 }
00160
00161
00162 returnValue DynamicDiscretization::getForwardSensitivities( BlockMatrix &D ) const{
00163
00164 D = dForward;
00165 return SUCCESSFUL_RETURN;
00166 }
00167
00168
00169 returnValue DynamicDiscretization::getBackwardSensitivities( BlockMatrix &D ) const{
00170
00171 D = dBackward;
00172 return SUCCESSFUL_RETURN;
00173 }
00174
00175
00176 returnValue DynamicDiscretization::deleteAllSeeds(){
00177
00178 BlockMatrix empty;
00179 setBackwardSeed( empty );
00180 setForwardSeed( empty, empty, empty, empty );
00181 return SUCCESSFUL_RETURN;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 returnValue DynamicDiscretization::setupOptions( ){
00191
00192
00193 addOption( FREEZE_INTEGRATOR , defaultFreezeIntegrator );
00194 addOption( FEASIBILITY_CHECK , defaultFeasibilityCheck );
00195 addOption( PLOT_RESOLUTION , defaultPlotResoltion );
00196
00197
00198 addOption( MAX_NUM_INTEGRATOR_STEPS , defaultMaxNumSteps );
00199 addOption( INTEGRATOR_TOLERANCE , defaultIntegratorTolerance );
00200 addOption( ABSOLUTE_TOLERANCE , defaultAbsoluteTolerance );
00201 addOption( INITIAL_INTEGRATOR_STEPSIZE , defaultInitialStepsize );
00202 addOption( MIN_INTEGRATOR_STEPSIZE , defaultMinStepsize );
00203 addOption( MAX_INTEGRATOR_STEPSIZE , defaultMaxStepsize );
00204 addOption( STEPSIZE_TUNING , defaultStepsizeTuning );
00205 addOption( CORRECTOR_TOLERANCE , defaultCorrectorTolerance );
00206 addOption( INTEGRATOR_PRINTLEVEL , defaultIntegratorPrintlevel );
00207 addOption( LINEAR_ALGEBRA_SOLVER , defaultLinearAlgebraSolver );
00208 addOption( ALGEBRAIC_RELAXATION , defaultAlgebraicRelaxation );
00209 addOption( RELAXATION_PARAMETER , defaultRelaxationParameter );
00210
00211 return SUCCESSFUL_RETURN;
00212 }
00213
00214
00215 returnValue DynamicDiscretization::setupLogging( )
00216 {
00217 LogRecord tmp( LOG_AT_END );
00218
00219 tmp.addItem( LOG_DIFFERENTIAL_STATES );
00220 tmp.addItem( LOG_ALGEBRAIC_STATES );
00221 tmp.addItem( LOG_PARAMETERS );
00222 tmp.addItem( LOG_CONTROLS );
00223 tmp.addItem( LOG_DISTURBANCES );
00224 tmp.addItem( LOG_INTERMEDIATE_STATES );
00225
00226 tmp.addItem( LOG_DISCRETIZATION_INTERVALS );
00227 tmp.addItem( LOG_STAGE_BREAK_POINTS );
00228
00229 outputLoggingIdx = addLogRecord( tmp );
00230
00231 return SUCCESSFUL_RETURN;
00232 }
00233
00234 void DynamicDiscretization::initializeVariables(){
00235
00236 N = 0 ;
00237 printLevel = LOW ;
00238
00239 nx = 0 ;
00240 na = 0 ;
00241 np = 0 ;
00242 nu = 0 ;
00243 nw = 0 ;
00244 }
00245
00246 void DynamicDiscretization::copy( const DynamicDiscretization& arg ){
00247
00248 N = arg.N ;
00249 nx = arg.nx;
00250 na = arg.na;
00251 np = arg.np;
00252 nu = arg.nu;
00253 nw = arg.nw;
00254
00255 unionGrid = arg.unionGrid ;
00256 printLevel = arg.printLevel;
00257 residuum = arg.residuum ;
00258
00259 xSeed = arg.xSeed ;
00260 pSeed = arg.pSeed ;
00261 uSeed = arg.uSeed ;
00262 wSeed = arg.wSeed ;
00263
00264 bSeed = arg.bSeed ;
00265
00266 dForward = arg.dForward ;
00267 dBackward = arg.dBackward;
00268 }
00269
00270
00271 uint DynamicDiscretization::getNumEvaluationPoints() const{
00272
00273 uint nEvaluationPoints = 0;
00274 uint nShoot = unionGrid.getNumIntervals();
00275
00276 int plotResolution;
00277 get( PLOT_RESOLUTION,plotResolution );
00278
00279 switch ( (PrintLevel) plotResolution ) {
00280
00281 case LOW : nEvaluationPoints = (uint)(10.0 / sqrt((double)nShoot) + 2.0); break;
00282 case MEDIUM: nEvaluationPoints = (uint)(30.0 / sqrt((double)nShoot) + 2.0); break;
00283 case HIGH : nEvaluationPoints = (uint)(100.0 / sqrt((double)nShoot) + 2.0); break;
00284 default : nEvaluationPoints = (uint)(10.0 / sqrt((double)nShoot) + 2.0); break;
00285 }
00286 return nEvaluationPoints;
00287 }
00288
00289
00290
00291 CLOSE_NAMESPACE_ACADO
00292
00293