irk_3stage_single_newton_export.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 
35 
36 using namespace std;
37 
39 
40 //
41 // PUBLIC MEMBER FUNCTIONS:
42 //
43 
45  const std::string& _commonHeaderName
46  ) : ExportGaussElim( _userInteraction,_commonHeaderName )
47 {
48  stepsize = 0;
49  implicit = false;
50  tau = 0;
51 }
52 
54 {}
55 
57  ExportStruct dataStruct
58  ) const
59 {
60  ExportGaussElim::getDataDeclarations( declarations, dataStruct );
61 
62  declarations.addDeclaration( A_mem,dataStruct );
63  declarations.addDeclaration( b_mem,dataStruct );
64 
65  if( TRANSPOSE ) {
66  declarations.addDeclaration( b_mem_trans,dataStruct );
67  }
68 
69  return SUCCESSFUL_RETURN;
70 }
71 
72 
74  ) const
75 {
77 
78  declarations.addDeclaration( solve_full );
79  if( REUSE ) {
80  declarations.addDeclaration( solveReuse_full );
81  }
82 
83  return SUCCESSFUL_RETURN;
84 }
85 
86 
88  )
89 {
91 
92  setupFactorization( solve, rk_swap, determinant, string("fabs") );
93  code.addFunction( solve );
94 
95  if( REUSE ) { // Also export the extra function which reuses the factorization of the matrix A
97  code.addFunction( solveReuse );
98 
99  if( TRANSPOSE ) {
102  }
103  }
104 
105  ExportVariable tau_var( stepsize*tau );
106 
107  // SETUP solve_full
108  ExportIndex i( "i" );
109  ExportIndex j( "j" );
110  solve_full.addIndex(i);
111  solve_full.addIndex(j);
112 
113  // form the linear subsystem matrix
114  ExportForLoop loop01( i, 0, dim );
115  ExportForLoop loop02( j, 0, dim );
116  if( implicit ) {
117  loop02.addStatement( A_mem.getElement(i,j) == tau_var*A_full.getElement(i,j) );
118  loop02.addStatement( A_mem.getElement(i,j) += I_full.getElement(i,j) );
119  }
120  else {
121  loop02.addStatement( A_mem.getElement(i,j) == tau_var*A_full.getElement(i,j) );
122  }
123  loop01.addStatement( loop02 );
124  solve_full.addStatement( loop01 );
125  if( !implicit ) {
126  ExportForLoop loop1( i, 0, dim );
127  loop1.addStatement( A_mem.getElement(i,i) -= 1.0 );
128  solve_full.addStatement( loop1 );
129  }
130 
131  // factorize the real and complex linear systems
133 
134  code.addFunction( solve_full );
135 
136  // SETUP solveReuse_full
137  if( REUSE ) {
140 
141  ExportVariable transf1_var( transf1 );
142  ExportVariable transf2_var( transf2 );
143  ExportVariable low_tria_var( low_tria );
144 
145  // transform the right-hand side
146  performTransformation( solveReuse_full, b_full, b_mem, transf1_var, i );
147 
148  // solveReuse the real and complex linear systems
150 
151  if( !implicit ) {
152  ExportForLoop loop2( i, 0, dim );
153  loop2.addStatement( b_mem.getRow(dim+i) -= low_tria_var.getElement(0,0)*b_mem.getRow(i) );
154  solveReuse_full.addStatement( loop2 );
156 
157  ExportForLoop loop3( i, 0, dim );
158  loop3.addStatement( b_mem.getRow(2*dim+i) -= low_tria_var.getElement(1,0)*b_mem.getRow(i) );
159  solveReuse_full.addStatement( loop3 );
160  ExportForLoop loop4( i, 0, dim );
161  loop4.addStatement( b_mem.getRow(2*dim+i) -= low_tria_var.getElement(2,0)*b_mem.getRow(dim+i) );
162  solveReuse_full.addStatement( loop4 );
164  }
165  else {
167 
168  ExportVariable b_tmp( "b_tmp", 1, nRightHandSides, REAL, ACADO_LOCAL );
170 
171  ExportForLoop loop22( j, 0, dim );
172  loop22.addStatement( b_tmp == zeros<double>(1,nRightHandSides) );
173  ExportForLoop loop21( i, 0, dim );
174  loop21.addStatement( b_tmp += I_full.getElement(j,i) * b_mem.getRow(i) );
175  loop22.addStatement( loop21 );
176  loop22.addStatement( b_mem.getRow(dim+j) += low_tria_var.getElement(0,0)*b_tmp );
177  loop22.addStatement( b_mem.getRow(2*dim+j) += low_tria_var.getElement(1,0)*b_tmp );
178  solveReuse_full.addStatement( loop22 );
180 
181  ExportForLoop loop32( j, 0, dim );
182  loop32.addStatement( b_tmp == zeros<double>(1,nRightHandSides) );
183  ExportForLoop loop31( i, 0, dim );
184  loop31.addStatement( b_tmp += I_full.getElement(j,i) * b_mem.getRow(dim+i) );
185  loop32.addStatement( loop31 );
186  loop32.addStatement( b_mem.getRow(2*dim+j) += low_tria_var.getElement(2,0)*b_tmp );
187  solveReuse_full.addStatement( loop32 );
189  }
190 
191  // transform back to the solution
192  performTransformation( solveReuse_full, b_mem, b_full, transf2_var, i );
193 
195 
196  if( TRANSPOSE ) {
197  uint NUM_RHS = nRightHandSides;
198  nRightHandSides = 1;
201 
202  ExportVariable transf1_T_var( transf1_T );
203  ExportVariable transf2_T_var( transf2_T );
204 
205  // transform the right-hand side
207 
208  // solveReuse the real and complex linear systems
210 
211  if( !implicit ) {
212 
213  ExportForLoop loop2( i, 0, dim );
214  loop2.addStatement( b_mem_trans.getRow(dim+i) -= low_tria_var.getElement(2,0)*b_mem_trans.getRow(2*dim+i) );
217 
218  ExportForLoop loop3( i, 0, dim );
219  loop3.addStatement( b_mem_trans.getRow(i) -= low_tria_var.getElement(1,0)*b_mem_trans.getRow(2*dim+i) );
221  ExportForLoop loop4( i, 0, dim );
222  loop4.addStatement( b_mem_trans.getRow(i) -= low_tria_var.getElement(0,0)*b_mem_trans.getRow(dim+i) );
225  }
226  else {
228  }
229 
230  // transform back to the solution
232 
234  nRightHandSides = NUM_RHS;
235  }
236  }
237 
238  return SUCCESSFUL_RETURN;
239 }
240 
241 
243 {
244  uint i, j;
245 
246  ExportForLoop loop0( index, 0, 3*dim );
247  for( j = 0; j < nRightHandSides; j++ ) {
248  loop0.addStatement( to.getElement(index,j) == 0.0 );
249  }
250  code.addStatement( loop0 );
251 
252  ExportForLoop loop1( index, 0, dim );
253  for( j = 0; j < nRightHandSides; j++ ) {
254 
255  for( i = 0; i < 3; i++ ) {
256  if( !transf.isZero(0,i) ) loop1.addStatement( to.getElement(index,j) += transf.getElement(0,i)*from.getElement(index+i*dim,j) );
257  }
258 
259  for( i = 0; i < 3; i++ ) {
260  if( !transf.isZero(1,i) ) loop1.addStatement( to.getElement(dim+index,j) += transf.getElement(1,i)*from.getElement(index+i*dim,j) );
261  }
262 
263  for( i = 0; i < 3; i++ ) {
264  if( !transf.isZero(2,i) ) loop1.addStatement( to.getElement(2*dim+index,j) += transf.getElement(2,i)*from.getElement(index+i*dim,j) );
265  }
266  }
267  code.addStatement( loop1 );
268 
269  return SUCCESSFUL_RETURN;
270 }
271 
272 
274 {
276 
277  if (nRightHandSides <= 0)
279 
280  int useOMP;
281  get(CG_USE_OPENMP, useOMP);
282  ExportStruct structWspace;
283  structWspace = useOMP ? ACADO_LOCAL : ACADO_WORKSPACE;
284 
285  A_mem = ExportVariable( std::string( "rk_mem_" ) + identifier + "A", dim, dim, REAL, structWspace );
286  b_mem = ExportVariable( std::string( "rk_mem_" ) + identifier + "b", 3*dim, nRightHandSides, REAL, structWspace );
287 
289  solve.setReturnValue( determinant, false );
290  solve.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
291 
292  if( REUSE ) {
294  solveReuse.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
295  if( TRANSPOSE ) {
296  b_mem_trans = ExportVariable( std::string( "rk_mem_trans_" ) + identifier + "b", 4*dim, 1, REAL, structWspace );
298  solveReuseTranspose.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
299  }
300  }
301 
302  A_full = ExportVariable( "A", dim, dim, REAL );
303  I_full = ExportVariable( "A_I", dim, dim, REAL );
305  rk_perm_full = ExportVariable( "rk_perm", 1, dim, INT );
306 
307  if( implicit ) {
308  solve_full = ExportFunction( getNameSolveFunction(), A_full, I_full, rk_perm_full ); // Only perform the LU factorization!
309  }
310  else {
311  solve_full = ExportFunction( getNameSolveFunction(), A_full, rk_perm_full ); // Only perform the LU factorization!
312  }
314  solve_full.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
315  if( REUSE ) {
316  if( implicit ) {
319  }
320  else {
322  if( TRANSPOSE ) {
323  b_full_trans = ExportVariable( "b", 4*dim, 1, REAL );
325  }
326  }
327  solveReuse_full.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
328  }
329 
330  return SUCCESSFUL_RETURN;
331 }
332 
333 
334 returnValue ExportIRK3StageSingleNewton::setTransformations( const double _tau, const DVector& _low_tria, const DMatrix& _transf1, const DMatrix& _transf2, const DMatrix& _transf1_T, const DMatrix& _transf2_T ) {
335  tau = _tau;
336  low_tria = _low_tria;
337  transf1 = _transf1;
338  transf2 = _transf2;
339  transf1_T = _transf1_T;
340  transf2_T = _transf2_T;
341 
342  if( _tau <= 0 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
343  if( _transf1.getNumRows() != 3 || _transf1.getNumCols() != 3 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
344  if( _transf2.getNumRows() != 3 || _transf2.getNumCols() != 3 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
345  if( _transf1_T.getNumRows() != 3 || _transf1_T.getNumCols() != 3 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
346  if( _transf2_T.getNumRows() != 3 || _transf2_T.getNumCols() != 3 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
347  if( _low_tria.getDim() != 3 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
348  if( _transf1.isZero() || _transf2.isZero() || _transf1_T.isZero() || _transf2_T.isZero() || _low_tria.isZero() ) return ACADOERROR( RET_INVALID_ARGUMENTS );
349 
350  return SUCCESSFUL_RETURN;
351 }
352 
353 
355  stepsize = _stepsize;
356 
357  return SUCCESSFUL_RETURN;
358 }
359 
360 
362 
363  return string( "solve_" ) + identifier + "sub_system";
364 }
365 
366 
368 
369  return string( "solve_" ) + identifier + "sub_system_reuse";
370 }
371 
372 
374 
375  return string( "solve_" ) + identifier + "sub_transpose_reuse";
376 }
377 
378 
380 
381  implicit = _implicit;
382 
383  return SUCCESSFUL_RETURN;
384 }
385 
386 
387 //
388 // PROTECTED MEMBER FUNCTIONS:
389 //
390 
391 
392 
394 
395 // end of file.
ExportIRK3StageSingleNewton(UserInteraction *_userInteraction=0, const std::string &_commonHeaderName="")
returnValue setStepSize(double _stepsize)
bool isEmpty() const
Definition: matrix.hpp:193
ExportVariable getRow(const ExportIndex &idx) const
const std::string getNameSolveTransposeReuseFunction()
ExportFunction solveReuseTranspose
virtual returnValue setup()
virtual returnValue performTransformation(ExportStatementBlock &code, const ExportVariable &from, const ExportVariable &to, const ExportVariable &transf, const ExportIndex &index)
Allows to pass back messages to the calling function.
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
virtual returnValue setupSolveReuseTranspose(ExportFunction &_solveReuse, ExportVariable &_bPerm)
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
Allows to export code of a for-loop.
virtual returnValue setupSolveReuseComplete(ExportFunction &_solveReuse, ExportVariable &_bPerm)
ExportVariable getElement(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
#define CLOSE_NAMESPACE_ACADO
Defines a scalar-valued index variable to be used for exporting code.
virtual returnValue getCode(ExportStatementBlock &code)
ExportStruct
Allows to export Gaussian elimination for solving linear systems of specific dimensions.
unsigned getDim() const
Definition: vector.hpp:172
const std::string getNameSolveReuseFunction()
returnValue setImplicit(BooleanType _implicit)
returnValue setTransformations(const double _tau, const DVector &_low_tria, const DMatrix &_transf1, const DMatrix &_transf2, const DMatrix &_transf1_T, const DMatrix &_transf2_T)
Encapsulates all user interaction for setting options, logging data and plotting results.
Allows to export code of an arbitrary function.
returnValue addStatement(const ExportStatement &_statement)
bool isZero(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
unsigned getNumRows() const
Definition: matrix.hpp:185
returnValue addLinebreak(uint num=1)
ExportFunction & setReturnValue(const ExportVariable &_functionReturnValue, bool _returnAsPointer=false)
virtual returnValue getDataDeclarations(ExportStatementBlock &declarations, ExportStruct dataStruct=ACADO_ANY) const
unsigned getNumCols() const
Definition: matrix.hpp:189
ExportFunction & addVariable(const ExportVariable &_var)
returnValue addDeclaration(const ExportVariable &_data, ExportStruct _dataStruct=ACADO_ANY)
const std::string getNameSolveFunction()
#define BEGIN_NAMESPACE_ACADO
returnValue addFunction(const ExportFunction &_function)
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
Allows to export code for a block of statements.
virtual returnValue getDataDeclarations(ExportStatementBlock &declarations, ExportStruct dataStruct=ACADO_ANY) const
ExportArgument getAddress(const ExportIndex &_rowIdx, const ExportIndex &_colIdx=emptyConstExportIndex) const
const double ZERO_EPS
ExportFunction & addIndex(const ExportIndex &_index)
#define ACADOERROR(retval)
Defines a matrix-valued variable to be used for exporting code.
returnValue addFunctionCall(const std::string &_fName, 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)
virtual returnValue setupFactorization(ExportFunction &_solve, ExportVariable &_swap, ExportVariable &_determinant, const std::string &absF)


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