integrator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
45 
46 using namespace std;
47 
49 
50 
51 
52 //
53 // PUBLIC MEMBER FUNCTIONS:
54 //
55 
57  :AlgorithmicBase( ){
58 
59  // RHS:
60  // --------
61  rhs = 0;
62  m = 0;
63  ma = 0;
64  mdx = 0;
65  md = 0;
66  mn = 0;
67  mu = 0;
68  mui = 0;
69  mp = 0;
70  mpi = 0;
71  mw = 0;
72 
73  transition = 0;
74 
75 
76  // SETTINGS:
77  // ---------
78  h = (double*)calloc(1,sizeof(double));
79  h[0] = 0.001 ;
80  hmin = 0.000001 ;
81  hmax = 1.0e10 ;
82 
83  tune = 0.5 ;
84  TOL = 0.000001 ;
85 
86 
87  // INTERNAL INDEX LISTS:
88  // ---------------------
89  diff_index = 0;
90  ddiff_index = 0;
91  alg_index = 0;
92  control_index = 0;
93  parameter_index = 0;
97  time_index = 0;
98 
99 
100  // OTHERS:
101  // -------
102  count = 0 ;
103  count2 = 0 ;
104  count3 = 0 ;
105  maxNumberOfSteps = 1000;
106 
107 
108  // PRINT-LEVEL:
109  // ------------
110  PrintLevel = LOW;
111 
112 
113  // SEED DIMENSIONS:
114  // ----------------
115  nFDirs = 0;
116  nBDirs = 0;
117  nFDirs2 = 0;
118  nBDirs2 = 0;
119 
120 
121  // THE STATE OF AGGREGATION:
122  // -------------------------
123  soa = SOA_UNFROZEN;
124 
125  setupOptions( );
126  setupLogging( );
127 }
128 
129 
130 
132  :AlgorithmicBase( arg ){
133 
134  if( arg.transition == 0 ) transition = 0;
135  else transition = new Transition( *arg.transition );
136 }
137 
138 
140 
141 
142  // RHS:
143  // ---------
144  if( rhs != NULL )
145  delete rhs;
146 
147  if( transition != 0 )
148  delete transition;
149 
150 
151  // SETTINGS:
152  // ---------
153  free(h);
154 
155 
156  // INTERNAL INDEX LISTS:
157  // ---------------------
158 
159  if( diff_index != 0 )
160  delete[] diff_index;
161 
162  if( ddiff_index != 0 )
163  delete[] ddiff_index;
164 
165  if( alg_index != 0 )
166  delete[] alg_index;
167 
168  if( control_index != 0 )
169  delete[] control_index;
170 
171  if( parameter_index != 0 )
172  delete[] parameter_index;
173 
174  if( int_control_index != 0 )
175  delete[] int_control_index;
176 
177  if( int_parameter_index != 0 )
178  delete[] int_parameter_index;
179 
180  if( disturbance_index != 0 )
181  delete[] disturbance_index;
182 }
183 
184 
186  const Transition &trs
187  )
188 {
189  returnValue returnvalue;
190 
191  returnvalue = init( rhs_ );
192  transition = new Transition(trs);
193 
194  return returnvalue;
195 }
196 
197 
199  )
200 {
201  if( transition != 0 ) delete transition;
202  transition = new Transition(trs);
203  return SUCCESSFUL_RETURN;
204 }
205 
206 
208  double tend_,
209  double *x0 ,
210  double *xa ,
211  double *p ,
212  double *u ,
213  double *w ){
214 
215  Grid t_( t0_, tend_, 2 );
216  return integrate( t_, x0, xa, p, u, w );
217 }
218 
219 
221  double *x0,
222  double *xa,
223  double *p ,
224  double *u ,
225  double *w ){
226 
227  if( rhs == 0 ) return ACADOERROR( RET_TRIVIAL_RHS );
229 
230  DVector tmpX ( components.getDim(), x0 );
231  DVector tmpXA( rhs->getNXA() , xa );
232  DVector tmpP ( rhs->getNP () , p );
233  DVector tmpU ( rhs->getNU () , u );
234  DVector tmpW ( rhs->getNW () , w );
235 
236  return integrate( t_, tmpX, tmpXA, tmpP, tmpU, tmpW );
237 }
238 
239 
240 
242  double tend_,
243  const DVector &x0 ,
244  const DVector &xa ,
245  const DVector &p ,
246  const DVector &u ,
247  const DVector &w ){
248 
249  Grid t_( t0_, tend_, 2 );
250  return integrate( t_, x0, xa, p, u, w );
251 }
252 
253 
254 
256  const DVector &x0 ,
257  const DVector &xa ,
258  const DVector &p ,
259  const DVector &u ,
260  const DVector &w ){
261 
262  int run1;
263  returnValue returnvalue;
264  if( rhs == 0 ) return ACADOERROR( RET_TRIVIAL_RHS );
265 
266  DVector tmpX;
267 
269 
270  const int N = components.getDim();
271 
272  if( x0.getDim() != 0 ){
273  tmpX.init( components.getDim() );
274  for( run1 = 0; run1 < (int) components.getDim(); run1++ )
275  tmpX(run1) = x0((int) components(run1));
276  }
277 
278 
279 // tmpX.print( "integrator x0" );
280 // u.print( "integrator u0" );
281 // p.print( "integrator p0" );
282  returnvalue = evaluate( tmpX, xa, p, u, w, t_ );
283 
284  if( returnvalue != SUCCESSFUL_RETURN )
285  return returnvalue;
286 
287  xE.init(rhs->getDim());
288  xE.setZero();
289 
290  DVector tmp(rhs->getDim());
291  getProtectedX(&tmp);
292 
293  for( run1 = 0; run1 < N; run1++ )
294  xE((int) components(run1)) = tmp(run1);
295 
296  for( run1 = N; run1 < N + ma; run1++ )
297  xE(run1) = tmp(run1);
298 
299  if( transition != 0 )
300  returnvalue = evaluateTransition( t_.getLastTime(), xE, xa, p, u, w );
301 
302  return returnvalue;
303 }
304 
305 
306 // ======================================================================================
307 
309  const DVector &xSeed,
310  const DVector &pSeed,
311  const DVector &uSeed,
312  const DVector &wSeed ){
313 
314 
315  int run1;
316  if( rhs == 0 ) return ACADOERROR( RET_TRIVIAL_RHS );
317 
318  DVector tmpX;
320 
321  dP = pSeed;
322  dU = uSeed;
323  dW = wSeed;
324 
325  if( xSeed.getDim() != 0 ){
326 
327  tmpX.init( components.getDim() );
328  for( run1 = 0; run1 < (int) components.getDim(); run1++ )
329  tmpX(run1) = xSeed((int) components(run1));
330  }
331 
332  return setProtectedForwardSeed( tmpX, pSeed, uSeed, wSeed, order );
333 }
334 
335 
336 // ======================================================================================
337 
339  const DVector &seed ){
340 
341  dXb = seed;
342 
343  uint run1;
344  DVector tmp( seed.getDim() );
346 
347  if( seed.getDim() != 0 ){
348  tmp.init( components.getDim() );
349  for( run1 = 0; run1 < components.getDim(); run1++ )
350  tmp(run1) = seed((int) components(run1));
351  }
352  return setProtectedBackwardSeed( tmp, order );
353 }
354 
355 
356 
358 
359  uint run1;
360  returnValue returnvalue;
361 
362 
363  if( ( nBDirs > 0 || nBDirs2 > 0 ) && transition != 0 ){
364 
365  int order;
366  if( nBDirs2 > 0 ) order = 2;
367  else order = 1;
368 
369  returnvalue = diffTransitionBackward( dXb, dPb, dUb, dWb, order );
370 
371  setBackwardSeed( order, dXb );
372 
373  if( returnvalue != SUCCESSFUL_RETURN ) return ACADOERROR(returnvalue);
374  }
375 
376  returnvalue = evaluateSensitivities();
377 
378  if( returnvalue != SUCCESSFUL_RETURN ) return ACADOERROR(returnvalue);
379 
380 
381  if( nBDirs > 0 || nBDirs2 > 0 ) return SUCCESSFUL_RETURN;
382 
383  int order = 1;
384  if( nFDirs2 > 0 ) order = 2;
385 
386  DMatrix tmp( rhs->getDim(), 1 );
387  returnvalue = getProtectedForwardSensitivities(&tmp,order);
388 
390 
391  dX.init(rhs->getDim()-ma);
392  dX.setZero();
393 
394  for( run1 = 0; run1 < components.getDim(); run1++ )
395  dX((int) components(run1)) = tmp(run1,0);
396 
397  if( returnvalue != SUCCESSFUL_RETURN ) return ACADOERROR(returnvalue);
398 
399  if( transition != 0 )
400  returnvalue = diffTransitionForward( dX, dP, dU, dW, order );
401 
402  return returnvalue;
403 }
404 
405 
407  int order ) const{
408 
409  Dx = dX;
410  return SUCCESSFUL_RETURN;
411 }
412 
413 
415  int order ) const{
416 
417  if( order == 1 ) Dx = dxStore;
418  if( order == 2 ) Dx = ddxStore;
419 
420  return SUCCESSFUL_RETURN;
421 }
422 
423 
425  DVector &DP ,
426  DVector &DU ,
427  DVector &DW ,
428  int order ) const{
429 
430  int run2;
431  returnValue returnvalue;
432 
433  DVector tmpX ( rhs->getDim() );
434 
435  DX.setZero();
436  DP.setZero();
437  DU.setZero();
438  DW.setZero();
439 
440  returnvalue = getProtectedBackwardSensitivities( tmpX, DP, DU, DW, order );
442 
443  for( run2 = 0; run2 < (int) components.getDim(); run2++ )
444  DX((int) components(run2)) = tmpX(run2);
445 
446  for( run2 = 0; run2 < (int) dPb.getDim(); run2++ )
447  DP(run2) += dPb(run2);
448 
449  for( run2 = 0; run2 < (int) dUb.getDim(); run2++ )
450  DU(run2) += dUb(run2);
451 
452  for( run2 = 0; run2 < (int) dWb.getDim(); run2++ )
453  DW(run2) += dWb(run2);
454 
455  return returnvalue;
456 }
457 
458 
460 
461  return BT_FALSE;
462 }
463 
464 
466 
467  if ( rhs != 0 ) return BT_TRUE ;
468  else return BT_FALSE;
469 }
470 
472 {
473  if ( rhs == 0 ) return BT_FALSE ;
474  else return rhs->isAffine();
475 }
476 
477 
479 {
480  if ( rhs == 0 )
481  return -INFTY;
482  else
483  return rhs->getStepLength( );
484 }
485 
486 
487 
489 
490  nBDirs = 0;
491  nFDirs = 0;
492  nBDirs2 = 0;
493  nFDirs2 = 0;
494 
495  return SUCCESSFUL_RETURN;
496 }
497 
498 
499 
500 
501 //
502 // PROTECTED MEMBER FUNCTIONS:
503 //
504 
505 returnValue Integrator::evaluateTransition( const double time, DVector &xd, const DVector &xa,
506  const DVector &p, const DVector &u, const DVector &w ){
507 
508  ASSERT( transition != 0 );
509  EvaluationPoint z( *transition, xd.getDim(), xa.getDim(), p.getDim(), u.getDim(), w.getDim() );
510  z.setT ( time );
511  z.setX ( xd );
512  z.setXA( xa );
513  z.setP ( p );
514  z.setU ( u );
515  z.setW ( w );
516  xd = transition->evaluate( z );
517  return SUCCESSFUL_RETURN;
518 }
519 
520 
522  const DVector &DP,
523  const DVector &DU,
524  const DVector &DW,
525  const int &order ){
526 
527  ASSERT( transition != 0 );
528  EvaluationPoint z( *transition, DX.getDim(), 0, DP.getDim(), DU.getDim(), DW.getDim() );
529  z.setX ( DX );
530  z.setP ( DP );
531  z.setU ( DU );
532  z.setW ( DW );
533 
534  if( order == 1 ) DX = transition->AD_forward( z );
535 
536  if( order != 1 ) return ACADOERROR( RET_NOT_IMPLEMENTED_YET );
537 
538  return SUCCESSFUL_RETURN;
539 }
540 
541 
543  DVector &DP,
544  DVector &DU,
545  DVector &DW,
546  int &order ){
547 
548  ASSERT( transition != 0 );
549  if( order != 1 ) return ACADOERROR( RET_NOT_IMPLEMENTED_YET );
550 
551  EvaluationPoint z( *transition, DX.getDim(), 0, DP.getDim(), DU.getDim(), DW.getDim() );
552 
553  transition->AD_backward( DX, z );
554 
555  DX = z.getX();
556  DP = z.getP();
557  DU = z.getU();
558  DW = z.getW();
559 
560  return SUCCESSFUL_RETURN;
561 }
562 
563 
564 
565 
567 {
581 
582  return SUCCESSFUL_RETURN;
583 }
584 
585 
586 
588 
590  get( INTEGRATOR_TOLERANCE , TOL );
592  get( MIN_INTEGRATOR_STEPSIZE , hmin );
593  get( MAX_INTEGRATOR_STEPSIZE , hmax );
594  get( STEPSIZE_TUNING , tune );
596  get( LINEAR_ALGEBRA_SOLVER , las );
597 }
598 
600 
602 
603  tmp.addItem( LOG_TIME_INTEGRATOR, "INTEGRATION TIME [sec]: ");
604  tmp.addItem( LOG_NUMBER_OF_INTEGRATOR_STEPS, "NUMBER OF STEPS : ");
605  tmp.addItem( LOG_NUMBER_OF_INTEGRATOR_REJECTED_STEPS, "NUMBER OF REJECTED STEPS : ");
606  tmp.addItem( LOG_NUMBER_OF_INTEGRATOR_FUNCTION_EVALUATIONS, "NUMBER OF RHS EVALUATIONS : ");
607  tmp.addItem( LOG_NUMBER_OF_BDF_INTEGRATOR_JACOBIAN_EVALUATIONS,"NUMBER OF JACOBIAN EVALUATIONS : ");
608  tmp.addItem( LOG_TIME_INTEGRATOR_FUNCTION_EVALUATIONS, "TIME FOR RHS EVALUATIONS [sec]: ");
609  tmp.addItem( LOG_TIME_BDF_INTEGRATOR_JACOBIAN_EVALUATION, "TIME FOR JACOBIAN EVALUATIONS [sec]: ");
610  tmp.addItem( LOG_TIME_BDF_INTEGRATOR_JACOBIAN_DECOMPOSITION, "TIME FOR JACOBIAN DECOMPOSITIONS [sec]: ");
611 
613 
614  return SUCCESSFUL_RETURN;
615 }
616 
617 
619 
620  return getDim();
621 }
622 
623 
625 
627 }
628 
629 
631 
632 // end of file.
virtual double getStepLength() const
#define N
double TOL
Definition: integrator.hpp:645
virtual int getDimX() const
Definition: integrator.cpp:618
returnValue addItem(LogName _name, const char *const _label=DEFAULT_LABEL)
Definition: log_record.cpp:70
virtual returnValue getProtectedX(DVector *xEnd) const =0
double tune
Definition: integrator.hpp:644
const double defaultRelaxationParameter
const double defaultMaxStepsize
virtual returnValue evaluateSensitivities()=0
int * int_parameter_index
Definition: integrator.hpp:659
int maxNumberOfSteps
Definition: integrator.hpp:666
VariablesGrid dxStore
Definition: integrator.hpp:712
const double INFTY
DVector dXb
Definition: integrator.hpp:706
DVector getDifferentialStateComponents() const
short int m
Definition: integrator.hpp:620
void initializeOptions()
Definition: integrator.cpp:587
virtual returnValue deleteAllSeeds()
Definition: integrator.cpp:488
int * diff_index
Definition: integrator.hpp:653
void init(unsigned _dim=0)
Definition: vector.hpp:155
DVector dU
Definition: integrator.hpp:703
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
DVector evaluate(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:520
double hmin
Definition: integrator.hpp:642
DVector dPb
Definition: integrator.hpp:707
short int mdx
Definition: integrator.hpp:622
Base class for all algorithmic modules within the ACADO Toolkit providing some basic functionality...
Allows to setup function evaluation points.
const double defaultStepsizeTuning
int getNU() const
Definition: function.cpp:222
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
virtual int getDim() const =0
const double defaultMinStepsize
Allows to conveniently handle (one-dimensional) grids consisting of time points.
Definition: grid.hpp:58
const int defaultMaxNumSteps
virtual double getDifferentialEquationSampleTime() const
Definition: integrator.cpp:478
short int md
Definition: integrator.hpp:629
#define CLOSE_NAMESPACE_ACADO
int * int_control_index
Definition: integrator.hpp:658
virtual returnValue getProtectedBackwardSensitivities(DVector &Dx_x0, DVector &Dx_p, DVector &Dx_u, DVector &Dx_w, int order) const =0
const double defaultIntegratorTolerance
int getNXA() const
Definition: function.cpp:212
returnValue setX(const DVector &x)
VariablesGrid ddxStore
Definition: integrator.hpp:713
Abstract base class for all kinds of algorithms for integrating differential equations (ODEs or DAEs)...
Definition: integrator.hpp:61
virtual BooleanType canHandleImplicitSwitches() const
Definition: integrator.cpp:459
virtual returnValue setupOptions()
Definition: integrator.cpp:566
const int defaultIntegratorPrintlevel
int * alg_index
Definition: integrator.hpp:655
int getNW() const
Definition: function.cpp:245
StateOfAggregation soa
Definition: integrator.hpp:688
int getNP() const
Definition: function.cpp:233
short int mu
Definition: integrator.hpp:624
returnValue setForwardSeed(const int &order, const DVector &xSeed, const DVector &pSeed=emptyVector, const DVector &uSeed=emptyVector, const DVector &wSeed=emptyVector)
Definition: integrator.cpp:308
unsigned getDim() const
Definition: vector.hpp:172
int getDim() const
short int mw
Definition: integrator.hpp:628
double * h
Definition: integrator.hpp:640
returnValue setT(const double &t)
returnValue printLogRecord(std::ostream &_stream, int idx, LogPrintMode _mode=PRINT_ITEM_BY_ITEM) const
const double defaultCorrectorTolerance
Transition * transition
Definition: integrator.hpp:634
short int mui
Definition: integrator.hpp:625
returnValue setTransition(const Transition &trs)
Definition: integrator.cpp:198
BooleanType isAffine()
Definition: function.cpp:368
int * control_index
Definition: integrator.hpp:656
Derived & setZero(Index size)
int * disturbance_index
Definition: integrator.hpp:660
virtual returnValue setupLogging()
Definition: integrator.cpp:599
short int mp
Definition: integrator.hpp:626
PrintLevel
DVector dX
Definition: integrator.hpp:701
const double defaultInitialStepsize
returnValue AD_backward(const DVector &seed, EvaluationPoint &df, const int &number=0)
Definition: function.cpp:546
const int defaultprintIntegratorProfile
virtual returnValue getProtectedForwardSensitivities(DMatrix *Dx, int order) const =0
DVector xE
Definition: integrator.hpp:699
returnValue getBackwardSensitivities(DVector &Dx_x0, DVector &Dx_p, DVector &Dx_u, DVector &Dx_w, int order) const
Definition: integrator.cpp:424
#define ASSERT(x)
#define BT_TRUE
Definition: acado_types.hpp:47
const int defaultAlgebraicRelaxation
DVector dUb
Definition: integrator.hpp:708
short int mn
Definition: integrator.hpp:623
virtual returnValue setProtectedForwardSeed(const DVector &xSeed, const DVector &pSeed, const DVector &uSeed, const DVector &wSeed, const int &order)=0
DVector AD_forward(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:533
int * parameter_index
Definition: integrator.hpp:657
Allows to setup and evaluate transition functions based on SymbolicExpressions.
Definition: transition.hpp:53
virtual BooleanType isDifferentialEquationAffine() const
Definition: integrator.cpp:471
virtual returnValue diffTransitionForward(DVector &DX, const DVector &DP, const DVector &DU, const DVector &DW, const int &order)
Definition: integrator.cpp:521
int * ddiff_index
Definition: integrator.hpp:654
DVector dW
Definition: integrator.hpp:704
virtual ~Integrator()
Definition: integrator.cpp:139
virtual returnValue init(const DifferentialEquation &rhs)=0
double getLastTime() const
DVector dWb
Definition: integrator.hpp:709
Allows to setup and store user-specified log records of algorithmic information.
Definition: log_record.hpp:72
virtual returnValue diffTransitionBackward(DVector &DX, DVector &DP, DVector &DU, DVector &DW, int &order)
Definition: integrator.cpp:542
#define BEGIN_NAMESPACE_ACADO
int addLogRecord(LogRecord &_record)
returnValue integrateSensitivities()
Definition: integrator.cpp:357
#define BT_FALSE
Definition: acado_types.hpp:49
const int defaultLinearAlgebraSolver
returnValue getForwardSensitivities(DVector &Dx, int order) const
Definition: integrator.cpp:406
short int ma
Definition: integrator.hpp:621
DVector dP
Definition: integrator.hpp:702
short int mpi
Definition: integrator.hpp:627
virtual returnValue evaluate(const DVector &x0, const DVector &xa, const DVector &p, const DVector &u, const DVector &w, const Grid &t_)=0
virtual returnValue evaluateTransition(const double time, DVector &xd, const DVector &xa, const DVector &p, const DVector &u, const DVector &w)
Definition: integrator.cpp:505
double hmax
Definition: integrator.hpp:643
returnValue setBackwardSeed(const int &order, const DVector &seed)
Definition: integrator.cpp:338
virtual returnValue printRunTimeProfile() const
Definition: integrator.cpp:624
returnValue addOption(OptionsName name, int value)
double hini
Definition: integrator.hpp:641
virtual returnValue setProtectedBackwardSeed(const DVector &seed, const int &order)=0
const double defaultAbsoluteTolerance
returnValue integrate(double t0, double tend, double *x0, double *xa=0, double *p=0, double *u=0, double *w=0)
Definition: integrator.cpp:207
#define ACADOERROR(retval)
virtual BooleanType isDifferentialEquationDefined() const
Definition: integrator.cpp:465
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.
DifferentialEquation * rhs
Definition: integrator.hpp:619


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