export_acado_function.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 
00034 #include <acado/code_generation/export_acado_function.hpp>
00035 #include <acado/function/function_.hpp>
00036 
00037 using namespace std;
00038 BEGIN_NAMESPACE_ACADO
00039 
00040 
00041 //
00042 // PUBLIC MEMBER FUNCTIONS:
00043 //
00044 
00045 ExportAcadoFunction::ExportAcadoFunction( ) : ExportFunction( )
00046 {
00047         numX = 0;
00048         numXA = 0;
00049         numU = 0;
00050         numP = 0;
00051         numDX = 0;
00052         numOD = 0;
00053 
00054         f = std::tr1::shared_ptr< Function >(new Function( ));
00055 
00056         external = false;
00057 }
00058 
00059 
00060 ExportAcadoFunction::ExportAcadoFunction(       const Function& _f,
00061                                                                                         const std::string& _name
00062                                                                                         ) : ExportFunction( _name )
00063 {
00064         init(_f, _name);
00065 }
00066 
00067 ExportAcadoFunction::ExportAcadoFunction(       const std::string& _name
00068                                                                                         ) : ExportFunction( _name )
00069 {
00070         init(Function(), _name);
00071         external = true;
00072 }
00073 
00074 ExportAcadoFunction::~ExportAcadoFunction( )
00075 {}
00076 
00077 
00078 ExportStatement* ExportAcadoFunction::clone( ) const
00079 {
00080         return new ExportAcadoFunction(*this);
00081 }
00082 
00083 returnValue ExportAcadoFunction::init(  const Function& _f,
00084                                                                                 const std::string& _name,
00085                                                                                 const uint _numX,
00086                                                                                 const uint _numXA,
00087                                                                                 const uint _numU,
00088                                                                                 const uint _numP,
00089                                                                                 const uint _numDX,
00090                                                                                 const uint _numOD
00091                                                                                 )
00092 {
00093         numX = _numX;
00094         numXA = _numXA;
00095         numU = _numU;
00096         numP = _numP;
00097         numDX = _numDX;
00098         numOD = _numOD;
00099 
00100         f = std::tr1::shared_ptr< Function >(new Function( _f ));
00101 
00102         globalVar.setup("acado_aux", f->getGlobalExportVariableSize(), 1, REAL, ACADO_WORKSPACE);
00103         f->setGlobalExportVariableName( globalVar.getFullName() );
00104 
00105         external = false;
00106 
00107         // Just add two dummy arguments in order to keep addFunctionCall function happy.
00108         return ExportFunction::init(_name, ExportArgument("input", 1, 1), ExportArgument("output", 1, 1));
00109 }
00110 
00111 returnValue ExportAcadoFunction::exportDataDeclaration( std::ostream& stream,
00112                                                                                                                 const std::string& _realString,
00113                                                                                                                 const std::string& _intString,
00114                                                                                                                 int _precision
00115                                                                                                                 ) const
00116 {
00117         ASSERT( external == false );
00118 
00119         stream  << _realString << " " << f->getGlobalExportVariableName()
00120                         << "[ " << f->getGlobalExportVariableSize( ) << " ];" << std::endl;
00121 
00122         return SUCCESSFUL_RETURN;
00123 }
00124 
00125 
00126 returnValue ExportAcadoFunction::exportForwardDeclaration(      std::ostream& stream,
00127                                                                                                                         const std::string& _realString,
00128                                                                                                                         const std::string& _intString,
00129                                                                                                                         int _precision
00130                                                                                                                         ) const
00131 {
00132         if (flagPrivate == true)
00133                 return SUCCESSFUL_RETURN;
00134 
00135         if (external == true)
00136         {
00137                 stream << endl;
00138                 stream << "/** An external function for evaluation of symbolic expressions. */" << endl;
00139                 stream << "void " << name << "(const " << _realString << "* in, " << _realString << "* out);" << endl;
00140 
00141                 return SUCCESSFUL_RETURN;
00142         }
00143 
00144         return f->exportForwardDeclarations(stream, name.c_str(), _realString.c_str());
00145 }
00146 
00147 
00148 returnValue ExportAcadoFunction::exportCode(    std::ostream& stream,
00149                                                                                                 const std::string& _realString,
00150                                                                                                 const std::string& _intString,
00151                                                                                                 int _precision
00152                                                                                                 ) const
00153 {
00154         if (external == true)
00155                 return SUCCESSFUL_RETURN;
00156 
00157         return f->exportCode(
00158                         stream, name.c_str(), _realString.c_str(), numX, numXA, numU, numP, numDX, numOD,
00159                         // TODO: Here we allocate local memory for the function, this should be extended.
00160                         false, false);
00161 }
00162 
00163 
00164 bool ExportAcadoFunction::isDefined( ) const
00165 {
00166         if (f->getDim() > 0 || external == true)
00167                 return true;
00168 
00169         return false;
00170 }
00171 
00172 
00173 unsigned ExportAcadoFunction::getFunctionDim( void )
00174 {
00175         ASSERT( external == false );
00176 
00177         return f->getDim();
00178 }
00179 
00180 ExportVariable ExportAcadoFunction::getGlobalExportVariable( ) const
00181 {
00182         return deepcopy( globalVar );
00183 }
00184 
00185 returnValue ExportAcadoFunction::setGlobalExportVariable(const ExportVariable& var)
00186 {
00187         ASSERT( external == false );
00188 
00189         if (getFunctionDim() == 0)
00190                 return SUCCESSFUL_RETURN;
00191 
00192         // TODO This is more hurting that helping. The dev has to take care of the size
00193         //      outside this function, the whole point of this function is to set names,
00194         //      not to check sizes.
00195 //      ASSERT(var.getNumRows() >= f->getGlobalExportVariableSize() && var.getNumCols() == 1);
00196 
00197         globalVar = var;
00198         f->setGlobalExportVariableName( globalVar.getFullName() );
00199 
00200         return SUCCESSFUL_RETURN;
00201 }
00202 
00203 bool ExportAcadoFunction::isExternal() const
00204 {
00205         return external;
00206 }
00207 
00208 CLOSE_NAMESPACE_ACADO


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