transfer_device.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 
00034 #include <acado/transfer_device/transfer_device.hpp>
00035 
00036 
00037 
00038 BEGIN_NAMESPACE_ACADO
00039 
00040 
00041 
00042 //
00043 // PUBLIC MEMBER FUNCTIONS:
00044 //
00045 
00046 TransferDevice::TransferDevice( ) : SimulationBlock( )
00047 {
00048         additiveNoise = 0;
00049 
00050         setStatus( BS_NOT_INITIALIZED );
00051 }
00052 
00053 
00054 TransferDevice::TransferDevice( uint _dim,
00055                                                                 BlockName _name,
00056                                                                 double _samplingTime
00057                                                                 ) : SimulationBlock( _name,_samplingTime )
00058 {
00059         lastSignal.init( _dim,1 );
00060 
00061         additiveNoise = new Noise*[_dim];
00062         for( uint i=0; i<_dim; ++i )
00063                 additiveNoise[i] = 0;
00064 
00065         noiseSamplingTimes.init( _dim );
00066         noiseSamplingTimes.setAll( 0.0 );
00067 
00068         deadTimes.init( _dim );
00069         deadTimes.setAll( 0.0 );
00070 
00071         setStatus( BS_NOT_INITIALIZED );
00072 }
00073 
00074 
00075 TransferDevice::TransferDevice( const TransferDevice& rhs ) : SimulationBlock( rhs )
00076 {
00077         lastSignal = rhs.lastSignal;
00078 
00079         if ( rhs.additiveNoise != 0 )
00080         {
00081                 additiveNoise = new Noise*[rhs.getDim( )];
00082 
00083                 for( uint i=0; i<getDim( ); ++i )
00084                         if ( rhs.additiveNoise[i] != 0 )
00085                                 additiveNoise[i] = (rhs.additiveNoise[i])->clone( );
00086                         else
00087                                 additiveNoise[i] = 0;
00088         }
00089         else
00090                 additiveNoise = 0;
00091 
00092         noiseSamplingTimes = rhs.noiseSamplingTimes;
00093         
00094         deadTimes = rhs.deadTimes;
00095 }
00096 
00097 
00098 TransferDevice::~TransferDevice( )
00099 {
00100         if ( additiveNoise != 0 )
00101         {
00102                 for( uint i=0; i<getDim( ); ++i )
00103                         if ( additiveNoise[i] != 0 )
00104                                 delete additiveNoise[i];
00105 
00106                 delete[] additiveNoise;
00107         }
00108 }
00109 
00110 
00111 TransferDevice& TransferDevice::operator=( const TransferDevice& rhs )
00112 {
00113         if ( this != &rhs )
00114         {
00115                 SimulationBlock::operator=( rhs );
00116 
00117                 if ( additiveNoise != 0 )
00118                 {
00119                         for( uint i=0; i<getDim( ); ++i )
00120                                 if ( additiveNoise[i] != 0 )
00121                                         delete additiveNoise[i];
00122         
00123                         delete[] additiveNoise;
00124                 }
00125 
00126 
00127                 lastSignal = rhs.lastSignal;
00128 
00129                 if ( rhs.additiveNoise != 0 )
00130                 {
00131                         additiveNoise = new Noise*[rhs.getDim( )];
00132         
00133                         for( uint i=0; i<getDim( ); ++i )
00134                                 if ( rhs.additiveNoise[i] != 0 )
00135                                         additiveNoise[i] = (rhs.additiveNoise[i])->clone( );
00136                                 else
00137                                         additiveNoise[i] = 0;
00138                 }
00139                 else
00140                         additiveNoise = 0;
00141 
00142                 noiseSamplingTimes = rhs.noiseSamplingTimes;
00143 
00144                 deadTimes = rhs.deadTimes;
00145         }
00146 
00147         return *this;
00148 }
00149 
00150 
00151 
00152 
00153 //
00154 // PROTECTED MEMBER FUNCTIONS:
00155 //
00156 
00157 
00158 returnValue TransferDevice::init(       double _startTime,
00159                                                                         const DVector& _startValue
00160                                                                         )
00161 {
00162         // initialise lastSignal
00163         lastSignal.init( getDim( ),1 );
00164 
00165         lastSignal.setTime( 0,_startTime );
00166 
00167         if ( _startValue.getDim( ) == getDim( ) )
00168         {
00169                 lastSignal.setVector( 0,_startValue );
00170         }
00171         else
00172         {
00173                 DVector tmp( getDim( ) );
00174                 tmp.setAll( 0.0 );
00175                 lastSignal.setVector( 0,tmp );
00176         }
00177 
00178         // initialise additive noise
00179         if ( additiveNoise != 0 )
00180         {
00181                 for( uint i=0; i<getDim( ); ++i )
00182                 {
00183                         if ( additiveNoise[i] != 0 )
00184                                 additiveNoise[i]->init( );
00185                 }
00186         }
00187 
00188         setStatus( BS_READY );
00189 
00190         return SUCCESSFUL_RETURN;
00191 }
00192 
00193 
00194 returnValue TransferDevice::generateNoise(      double startTime,
00195                                                                                         double endTime,
00196                                                                                         VariablesGrid& currentNoise
00197                                                                                         ) const
00198 {
00199         double horizonLength = endTime - startTime;
00200         if ( horizonLength <= 0.0 )
00201                 return ACADOERROR( RET_INVALID_ARGUMENTS );
00202 
00203         // generate noise grid
00204         Grid noiseGrid;
00205 
00206         if ( acadoIsPositive( noiseSamplingTimes(0) ) == BT_TRUE )
00207         {
00208                 for( uint i=0; i<=floor( horizonLength/noiseSamplingTimes(0) ); ++i )
00209                         noiseGrid.addTime( ((double)i) * noiseSamplingTimes(0) );
00210 
00211                 if ( acadoIsInteger( horizonLength/noiseSamplingTimes(0) ) == BT_FALSE )
00212                         noiseGrid.addTime( endTime );
00213 
00214                 noiseGrid.shiftTimes( startTime );
00215         }
00216         else
00217         {
00218                 noiseGrid.init( startTime,endTime );
00219         }
00220 
00221         // generate current noise
00222         DVector noiseVector( 1 );
00223 
00224         currentNoise.init( getDim( ),noiseGrid );
00225         currentNoise.setZero( );
00226 
00227         if ( additiveNoise != 0 )
00228         {
00229                 // generate noise
00230                 for( uint i=0; i<getDim( ); ++i )
00231                 {
00232                         if ( additiveNoise[i] != 0 )
00233                         {
00234                                 for( uint j=0; j<currentNoise.getNumPoints( )-1; ++j )
00235                                 {
00236                                         additiveNoise[i]->step( noiseVector );
00237                                         currentNoise( j,i ) = noiseVector( 0 );
00238                                 }
00239                                 currentNoise( currentNoise.getNumPoints( )-1,i ) = noiseVector( 0 );
00240                         }
00241                 }
00242         }
00243 
00244         return SUCCESSFUL_RETURN;
00245 }
00246 
00247 
00248 
00249 CLOSE_NAMESPACE_ACADO
00250 
00251 /*
00252  *      end of file
00253  */


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:39:56