hydroscal.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00054 #include <acado_optimal_control.hpp>
00055 #include <acado_gnuplot.hpp>
00056 #include "../integrator/hydroscal_model.hpp"     // MODEL FILE
00057 
00058 #include <time.h>
00059 
00060 USING_NAMESPACE_ACADO
00061 
00062 void ffcn_model( double *x, double *f, void *user_data  ){
00063 
00064     int i;
00065     double *xd = new double[NXD];
00066     double *xa = new double[NXA];
00067     double *u  = new double[ NU];
00068     double *p  = new double[ NP];
00069 
00070     for( i = 0; i < NXD; i++ ) xd[i] = x[            1+i ];
00071     for( i = 0; i < NXA; i++ ) xa[i] = x[        NXD+1+i ];
00072     for( i = 0; i <  NU; i++ )  u[i] = x[    NXA+NXD+1+i ];
00073     for( i = 0; i <  NP; i++ )  p[i] = x[ NXA+NXD+NU+1+i ];
00074 
00075     ffcn( &x[0], xd, xa, u, p, f );
00076     gfcn( &x[0], xd, xa, u, p, &(f[NXD]) );
00077 
00078     delete[] xd;
00079     delete[] xa;
00080     delete[]  u;
00081     delete[]  p;
00082 
00083 }
00084 
00085 
00086 
00087 int main( ){
00088 
00089         double clock1 = clock();
00090 
00091         double t_start                  =    0.0;
00092         double t_end                    =  600.0;
00093         int    intervals                =    5  ;
00094         int    i;
00095 
00096         TIME t;
00097         DifferentialState               x("", NXD, 1);
00098         AlgebraicState                  z("", NXA, 1);
00099         Control                                 u("", NU, 1);
00100         Parameter                               p("", NP, 1);
00101         IntermediateState               is(1+NXD+NXA+NU+NP);
00102 
00103                                 is(0)              = t;
00104         for (i=0; i < NXD; ++i) is(1+i)            = x(i);
00105         for (i=0; i < NXA; ++i) is(1+NXD+i)        = z(i);
00106         for (i=0; i < NU;  ++i) is(1+NXD+NXA+i)    = u(i);
00107         for (i=0; i < NP;  ++i) is(1+NXD+NXA+NU+i) = p(i);
00108 
00109         CFunction hydroscalModel( NXD+NXA, ffcn_model );
00110 
00111 
00112         // Define a Right-Hand-Side:
00113         // -------------------------
00114         DifferentialEquation f;
00115         f << hydroscalModel(is);
00116 
00117 
00118         // DEFINE INITIAL VALUES:
00119         // ----------------------
00120         double xd[NXD] = {  2.1936116177990631E-01,   // X_0
00121                                                 3.3363028623863722E-01,   // X_1
00122                                                 3.7313133250625952E-01,   // X_2
00123                                                 3.9896472354654333E-01,   // X_3
00124                                                 4.1533719381260475E-01,   // X_4
00125                                                 4.2548399372287182E-01,   // X_5
00126 
00127                                                 4.3168379354213621E-01,   // X_6
00128                                                 4.3543569751236455E-01,   // X_7
00129                                                 4.3768918647214428E-01,   // X_8
00130                                                 4.3903262905928286E-01,   // X_9
00131                                                 4.3982597315656735E-01,   // X_10
00132                                                 4.4028774979047969E-01,   // X_11
00133                                                 4.4055002518902953E-01,   // X_12
00134                                                 4.4069238917008052E-01,   // X_13
00135                                                 4.4076272408112094E-01,   // X_14
00136                                                 4.4078980543461005E-01,   // X_15
00137                                                 4.4079091412311144E-01,   // X_16
00138                                                 4.4077642312834125E-01,   // X_17
00139                                                 4.4075255679998443E-01,   // X_18
00140                                                 4.4072304911231042E-01,   // X_19
00141                                                 4.4069013958173919E-01,   // X_20
00142                                                 6.7041926189645151E-01,   // X_21
00143                                                 7.3517997375758948E-01,   // X_22
00144                                                 7.8975978943631409E-01,   // X_23
00145                                                 8.3481725159539033E-01,   // X_24
00146                                                 8.7125377077380739E-01,   // X_25
00147                                                 9.0027275078767721E-01,   // X_26
00148                                                 9.2312464536394301E-01,   // X_27
00149                                                 9.4096954980798608E-01,   // X_28
00150                                                 9.5481731262797742E-01,   // X_29
00151                                                 9.6551271145368878E-01,   // X_30
00152                                                 9.7374401773010488E-01,   // X_31
00153                                                 9.8006186072166701E-01,   // X_32
00154                                                 9.8490109485675337E-01,   // X_33
00155                                                 9.8860194771099286E-01,   // X_34
00156                                                 9.9142879342008328E-01,   // X_35
00157                                                 9.9358602331847468E-01,   // X_36
00158                                                 9.9523105632238640E-01,   // X_37
00159                                                 9.9648478785701988E-01,   // X_38
00160                                                 9.9743986301741971E-01,   // X_39
00161                                                 9.9816716097314861E-01,   // X_40
00162                                                 9.9872084014280071E-01,   // X_41
00163                                                 3.8633811956730968E+00,   // n_1
00164                                                 3.9322260498028840E+00,   // n_2
00165                                                 3.9771965626392531E+00,   // n_3
00166                                                 4.0063070333869728E+00,   // n_4
00167                                                 4.0246026844143410E+00,   // n_5
00168                                                 4.0358888958821835E+00,   // n_6
00169                                                 4.0427690398786789E+00,   // n_7
00170                                                 4.0469300433477020E+00,   // n_8
00171                                                 4.0494314648020326E+00,   // n_9
00172                                                 4.0509267560029381E+00,   // n_10
00173                                                 4.0518145583397631E+00,   // n_11
00174                                                 4.0523364846379799E+00,   // n_12
00175                                                 4.0526383977460299E+00,   // n_13
00176                                                 4.0528081437632766E+00,   // n_14
00177                                                 4.0528985491134542E+00,   // n_15
00178                                                 4.0529413510270169E+00,   // n_16
00179                                                 4.0529556049324462E+00,   // n_17
00180                                                 4.0529527471448805E+00,   // n_18
00181                                                 4.0529396392278008E+00,   // n_19
00182                                                 4.0529203970496912E+00,   // n_20
00183                                                 3.6071164950918582E+00,   // n_21
00184                                                 3.7583754503438387E+00,   // n_22
00185                                                 3.8917148481441974E+00,   // n_23
00186                                                 4.0094300698741563E+00,   // n_24
00187                                                 4.1102216725798293E+00,   // n_25
00188                                                 4.1944038520620675E+00,   // n_26
00189                                                 4.2633275166560596E+00,   // n_27
00190                                                 4.3188755452109175E+00,   // n_28
00191                                                 4.3630947909857642E+00,   // n_29
00192                                                 4.3979622247841386E+00,   // n_30
00193                                                 4.4252580012497740E+00,   // n_31
00194                                                 4.4465128947193868E+00,   // n_32
00195                                                 4.4630018314791968E+00,   // n_33
00196                                                 4.4757626150015568E+00,   // n_34
00197                                                 4.4856260094946823E+00,   // n_35
00198                                                 4.4932488551808500E+00,   // n_36
00199                                                 4.4991456959629330E+00,   // n_37
00200                                                 4.5037168116896273E+00,   // n_38
00201                                                 4.5072719605639726E+00,   // n_39
00202                                                 4.5100498969782414E+00    // n_40
00203                                          };
00204 
00205         double ud[ NU] = {  4.1833910982822058E+00,   // L_vol
00206                                                 2.4899344742988991E+00    // Q
00207                                          };
00208 
00209         double pd[ NP] = {  1.5458567140000001E-01,   // n^{ref}_{tray}  = 0.155 l
00210                                                 1.7499999999999999E-01,   // (not in use?)
00211                                                 3.4717208398678062E-01,   // \alpha_{rect}   = 35 %
00212                                                 6.1895708603484367E-01,   // \alpha_{strip}  = 62 %
00213                                                 1.6593025789999999E-01,   // W_{tray}        = 0.166 l^{-.5} s^{.1}
00214                                                 5.0695122527590109E-01,   // Q_{loss}        = 0.51 kW
00215                                                 8.5000000000000000E+00,   // n^v_0           = 8.5 l
00216                                                 1.7000000000000001E-01,   // n^v_{N+1}       = 0.17 l
00217                                                 9.3885430857029321E+04,   // P_{top}         = 939 h Pa
00218                                                 2.5000000000000000E+02,   // \Delta P_{strip}= 2.5 h Pa and \Delta P_{rect} = 1.9 h Pa
00219                                                 1.4026000000000000E+01,   // F_{vol}         = 14.0 l h^{-1}
00220                                                 3.2000000000000001E-01,   // X_F             = 0.32
00221                                                 7.1054000000000002E+01,   // T_F             = 71 oC
00222                                                 4.7163089489100003E+01,   // T_C             = 47.2 oC
00223                                                 4.1833910753991770E+00,   // (not in use?)
00224                                                 2.4899344810136301E+00,   // (not in use?)
00225                                                 1.8760537088149468E+02    // (not in use?)
00226                              };
00227 
00228         DVector x0(NXD, xd);
00229         DVector p0(NP,  pd);
00230 
00231 
00232         // DEFINE AN OPTIMAL CONTROL PROBLEM:
00233         // ----------------------------------
00234         OCP ocp( t_start, t_end, intervals );
00235 
00236         // LSQ Term on temperature deviations and controls
00237         Function h;
00238 
00239 
00240 //     for( i = 0; i < NXD; i++ )
00241 //              h << 0.001*x(i);
00242 
00243 
00244         h << 0.1 * ( z(94)  - 88.0    );   // Temperature tray 14
00245         h << 0.1 * ( z(108) - 70.0    );   // Temperature tray 28
00246         h << 0.01 * ( u(0)   - ud[0]   );   // L_vol
00247         h << 0.01 * ( u(1)   - ud[1]   );   // Q
00248         ocp.minimizeLSQ( h );
00249 
00250         // W.r.t. differential equation
00251         ocp.subjectTo( f );
00252 
00253         // Fix states
00254         ocp.subjectTo( AT_START, x == x0 );
00255 
00256         // Fix parameters
00257         ocp.subjectTo( p == p0 );
00258 
00259         // Path constraint on controls
00260         ocp.subjectTo( ud[0] - 2.0 <=  u(0)  <= ud[0] + 2.0 );
00261         ocp.subjectTo( ud[1] - 2.0 <=  u(1)  <= ud[1] + 2.0 );
00262 
00263 
00264 
00265         // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00266         // ---------------------------------------------------
00267         OptimizationAlgorithm algorithm(ocp);
00268 
00269         algorithm.initializeAlgebraicStates("hydroscal_algebraic_states.txt");
00270 
00271         algorithm.set( INTEGRATOR_TYPE,                  INT_BDF                        );
00272         algorithm.set( MAX_NUM_ITERATIONS,               5                                      );
00273         algorithm.set( KKT_TOLERANCE,                    1e-3                           );
00274         algorithm.set( INTEGRATOR_TOLERANCE,     1e-4                           );
00275         algorithm.set( ABSOLUTE_TOLERANCE  ,     1e-6                           );
00276         algorithm.set( PRINT_SCP_METHOD_PROFILE, YES                            );
00277         algorithm.set( LINEAR_ALGEBRA_SOLVER,    SPARSE_LU                      );
00278         algorithm.set( DISCRETIZATION_TYPE,      MULTIPLE_SHOOTING      );
00279 
00280     //algorithm.set( LEVENBERG_MARQUARDT, 1e-3 );
00281 
00282     algorithm.set( DYNAMIC_SENSITIVITY,  FORWARD_SENSITIVITY_LIFTED );
00283         //algorithm.set( DYNAMIC_SENSITIVITY,  FORWARD_SENSITIVITY );
00284         //algorithm.set( CONSTRAINT_SENSITIVITY,  FORWARD_SENSITIVITY );
00285         //algorithm.set( ALGEBRAIC_RELAXATION,ART_EXPONENTIAL );    //results in an extra step but steps are quicker
00286 
00287 
00288         algorithm.solve();
00289 
00290         double clock2 = clock();
00291         printf("total computation time = %.16e \n", (clock2-clock1)/CLOCKS_PER_SEC  );
00292 
00293 
00294         // PLOT RESULTS:
00295         // ---------------------------------------------------
00296         VariablesGrid out_states;
00297         algorithm.getDifferentialStates( out_states );
00298         out_states.print( "OUT_states.m","STATES",PS_MATLAB );
00299 
00300         VariablesGrid out_controls;
00301         algorithm.getControls( out_controls );
00302         out_controls.print( "OUT_controls.m","CONTROLS",PS_MATLAB );
00303 
00304         VariablesGrid out_algstates;
00305         algorithm.getAlgebraicStates( out_algstates );
00306         out_algstates.print( "OUT_algstates.m","ALGSTATES",PS_MATLAB );
00307 
00308         GnuplotWindow window;
00309         window.addSubplot( out_algstates(94),  "Temperature tray 14" );
00310         window.addSubplot( out_algstates(108), "Temperature tray 28" );
00311         window.addSubplot( out_controls(0),    "L_vol"               );
00312         window.addSubplot( out_controls(1),    "Q"                   );
00313         window.plot( );
00314 
00315     return 0;
00316 }
00317 
00318 
00319 


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