export_acado_function.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 
36 
37 using namespace std;
39 
40 
41 //
42 // PUBLIC MEMBER FUNCTIONS:
43 //
44 
46 {
47  numX = 0;
48  numXA = 0;
49  numU = 0;
50  numP = 0;
51  numDX = 0;
52  numOD = 0;
53 
54  f = std::shared_ptr< Function >(new Function( ));
55 
56  external = false;
57 }
58 
59 
61  const std::string& _name
62  ) : ExportFunction( _name )
63 {
64  init(_f, _name);
65 }
66 
68  ) : ExportFunction( _name )
69 {
70  init(Function(), _name);
71  external = true;
72 }
73 
75 {}
76 
77 
79 {
80  return new ExportAcadoFunction(*this);
81 }
82 
84  const std::string& _name,
85  const uint _numX,
86  const uint _numXA,
87  const uint _numU,
88  const uint _numP,
89  const uint _numDX,
90  const uint _numOD
91  )
92 {
93  numX = _numX;
94  numXA = _numXA;
95  numU = _numU;
96  numP = _numP;
97  numDX = _numDX;
98  numOD = _numOD;
99 
100  f = std::shared_ptr< Function >(new Function( _f ));
101 
102  globalVar.setup( "rhs_aux", f->getGlobalExportVariableSize(), 1, REAL, ACADO_WORKSPACE);
103  f->setGlobalExportVariableName( globalVar.getFullName() );
104 
105  external = false;
106 
107  // Just add two dummy arguments in order to keep addFunctionCall function happy.
108  return ExportFunction::init(_name, ExportArgument("input", 1, 1), ExportArgument("output", 1, 1));
109 }
110 
112  const std::string& _realString,
113  const std::string& _intString,
114  int _precision
115  ) const
116 {
117  ASSERT( external == false );
118 
119  stream << _realString << " " << f->getGlobalExportVariableName()
120  << "[ " << f->getGlobalExportVariableSize( ) << " ];" << std::endl;
121 
122  return SUCCESSFUL_RETURN;
123 }
124 
125 
127  const std::string& _realString,
128  const std::string& _intString,
129  int _precision
130  ) const
131 {
132  if (flagPrivate == true)
133  return SUCCESSFUL_RETURN;
134 
135  if (external == true)
136  {
137  stream << endl;
138  stream << "/** An external function for evaluation of symbolic expressions. */" << endl;
139  stream << "void " << name << "(const " << _realString << "* in, " << _realString << "* out);" << endl;
140 
141  return SUCCESSFUL_RETURN;
142  }
143 
144  return f->exportForwardDeclarations(stream, name.c_str(), _realString.c_str());
145 }
146 
147 
149  const std::string& _realString,
150  const std::string& _intString,
151  int _precision
152  ) const
153 {
154  if (external == true)
155  return SUCCESSFUL_RETURN;
156 
157  return f->exportCode(
158  stream, name.c_str(), _realString.c_str(), numX, numXA, numU, numP, numDX, numOD,
159  // TODO: Here we allocate local memory for the function, this should be extended.
160  false, false);
161 }
162 
163 
165 {
166  if (f->getDim() > 0 || external == true)
167  return true;
168 
169  return false;
170 }
171 
172 
174 {
175  ASSERT( external == false );
176 
177  return f->getDim();
178 }
179 
181 {
182  return deepcopy( globalVar );
183 }
184 
186 {
187  ASSERT( external == false );
188 
189  if (getFunctionDim() == 0)
190  return SUCCESSFUL_RETURN;
191 
192  // TODO This is more hurting that helping. The dev has to take care of the size
193  // outside this function, the whole point of this function is to set names,
194  // not to check sizes.
195 // ASSERT(var.getNumRows() >= f->getGlobalExportVariableSize() && var.getNumCols() == 1);
196 
197  globalVar = var;
198  f->setGlobalExportVariableName( globalVar.getFullName() );
199 
200  return SUCCESSFUL_RETURN;
201 }
202 
204 {
205  return external;
206 }
207 
virtual returnValue exportDataDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
ExportVariable getGlobalExportVariable() const
ExportVariable & setup(const std::string &_name, uint _nRows=1, uint _nCols=1, ExportType _type=REAL, ExportStruct _dataStruct=ACADO_LOCAL, bool _callItByValue=false, const std::string &_prefix=std::string())
returnValue init(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
Allows to pass back messages to the calling function.
virtual ExportStatement * clone() const
std::shared_ptr< Function > f
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
#define CLOSE_NAMESPACE_ACADO
Defines a matrix-valued variable that can be passed as argument to exported functions.
Base class for all kind of statements to be exported by the code generation tool. ...
Allows to export code of an arbitrary function.
std::string getFullName() const
#define ASSERT(x)
returnValue setGlobalExportVariable(const ExportVariable &var)
A deepcopy(const A &a)
Make a deep copy of an object (Note: default is a shallow copy!)
#define BEGIN_NAMESPACE_ACADO
virtual bool isDefined() const
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
virtual returnValue exportForwardDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
returnValue init(const Function &_f, const std::string &_name="acadoFcn", const uint _numX=0, const uint _numXA=0, const uint _numU=0, const uint _numP=0, const uint _numDX=0, const uint _numOD=0)
Defines a matrix-valued variable to be used for exporting code.


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