irk_4stage_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  ExportForLoop loop5( i, 0, dim );
166  loop5.addStatement( b_mem.getRow(3*dim+i) -= low_tria_var.getElement(3,0)*b_mem.getRow(i) );
167  solveReuse_full.addStatement( loop5 );
168  ExportForLoop loop6( i, 0, dim );
169  loop6.addStatement( b_mem.getRow(3*dim+i) -= low_tria_var.getElement(4,0)*b_mem.getRow(dim+i) );
170  solveReuse_full.addStatement( loop6 );
171  ExportForLoop loop7( i, 0, dim );
172  loop7.addStatement( b_mem.getRow(3*dim+i) -= low_tria_var.getElement(5,0)*b_mem.getRow(2*dim+i) );
173  solveReuse_full.addStatement( loop7 );
175  }
176  else {
178 
179  ExportVariable b_tmp( "b_tmp", 1, nRightHandSides, REAL, ACADO_LOCAL );
181 
182  ExportForLoop loop22( j, 0, dim );
183  loop22.addStatement( b_tmp == zeros<double>(1,nRightHandSides) );
184  ExportForLoop loop21( i, 0, dim );
185  loop21.addStatement( b_tmp += I_full.getElement(j,i) * b_mem.getRow(i) );
186  loop22.addStatement( loop21 );
187  loop22.addStatement( b_mem.getRow(dim+j) += low_tria_var.getElement(0,0)*b_tmp );
188  loop22.addStatement( b_mem.getRow(2*dim+j) += low_tria_var.getElement(1,0)*b_tmp );
189  loop22.addStatement( b_mem.getRow(3*dim+j) += low_tria_var.getElement(3,0)*b_tmp );
190  solveReuse_full.addStatement( loop22 );
192 
193  ExportForLoop loop32( j, 0, dim );
194  loop32.addStatement( b_tmp == zeros<double>(1,nRightHandSides) );
195  ExportForLoop loop31( i, 0, dim );
196  loop31.addStatement( b_tmp += I_full.getElement(j,i) * b_mem.getRow(dim+i) );
197  loop32.addStatement( loop31 );
198  loop32.addStatement( b_mem.getRow(2*dim+j) += low_tria_var.getElement(2,0)*b_tmp );
199  loop32.addStatement( b_mem.getRow(3*dim+j) += low_tria_var.getElement(4,0)*b_tmp );
200  solveReuse_full.addStatement( loop32 );
202 
203  ExportForLoop loop42( j, 0, dim );
204  loop42.addStatement( b_tmp == zeros<double>(1,nRightHandSides) );
205  ExportForLoop loop41( i, 0, dim );
206  loop41.addStatement( b_tmp += I_full.getElement(j,i) * b_mem.getRow(2*dim+i) );
207  loop42.addStatement( loop41 );
208  loop42.addStatement( b_mem.getRow(3*dim+j) += low_tria_var.getElement(5,0)*b_tmp );
209  solveReuse_full.addStatement( loop42 );
211  }
212 
213  // transform back to the solution
214  performTransformation( solveReuse_full, b_mem, b_full, transf2_var, i );
215 
217 
218  if( TRANSPOSE ) {
219  uint NUM_RHS = nRightHandSides;
220  nRightHandSides = 1;
223 
224  ExportVariable transf1_T_var( transf1_T );
225  ExportVariable transf2_T_var( transf2_T );
226 
227  // transform the right-hand side
229 
230  // solveReuse the real and complex linear systems
232 
233  if( !implicit ) {
234 
235  ExportForLoop loop2( i, 0, dim );
236  loop2.addStatement( b_mem_trans.getRow(2*dim+i) -= low_tria_var.getElement(5,0)*b_mem_trans.getRow(3*dim+i) );
239 
240  ExportForLoop loop3( i, 0, dim );
241  loop3.addStatement( b_mem_trans.getRow(dim+i) -= low_tria_var.getElement(4,0)*b_mem_trans.getRow(3*dim+i) );
243  ExportForLoop loop4( i, 0, dim );
244  loop4.addStatement( b_mem_trans.getRow(dim+i) -= low_tria_var.getElement(2,0)*b_mem_trans.getRow(2*dim+i) );
247 
248  ExportForLoop loop5( i, 0, dim );
249  loop5.addStatement( b_mem_trans.getRow(i) -= low_tria_var.getElement(3,0)*b_mem_trans.getRow(3*dim+i) );
251  ExportForLoop loop6( i, 0, dim );
252  loop6.addStatement( b_mem_trans.getRow(i) -= low_tria_var.getElement(1,0)*b_mem_trans.getRow(2*dim+i) );
254  ExportForLoop loop7( i, 0, dim );
255  loop7.addStatement( b_mem_trans.getRow(i) -= low_tria_var.getElement(0,0)*b_mem_trans.getRow(dim+i) );
258  }
259  else {
261  }
262 
263  // transform back to the solution
265 
267  nRightHandSides = NUM_RHS;
268  }
269  }
270 
271  return SUCCESSFUL_RETURN;
272 }
273 
274 
276 {
277  uint i, j;
278 
279  ExportForLoop loop0( index, 0, 4*dim );
280  for( j = 0; j < nRightHandSides; j++ ) {
281  loop0.addStatement( to.getElement(index,j) == 0.0 );
282  }
283  code.addStatement( loop0 );
284 
285  ExportForLoop loop1( index, 0, dim );
286  for( j = 0; j < nRightHandSides; j++ ) {
287 
288  for( i = 0; i < 4; i++ ) {
289  if( !transf.isZero(0,i) ) loop1.addStatement( to.getElement(index,j) += transf.getElement(0,i)*from.getElement(index+i*dim,j) );
290  }
291 
292  for( i = 0; i < 4; i++ ) {
293  if( !transf.isZero(1,i) ) loop1.addStatement( to.getElement(dim+index,j) += transf.getElement(1,i)*from.getElement(index+i*dim,j) );
294  }
295 
296  for( i = 0; i < 4; i++ ) {
297  if( !transf.isZero(2,i) ) loop1.addStatement( to.getElement(2*dim+index,j) += transf.getElement(2,i)*from.getElement(index+i*dim,j) );
298  }
299 
300  for( i = 0; i < 4; i++ ) {
301  if( !transf.isZero(3,i) ) loop1.addStatement( to.getElement(3*dim+index,j) += transf.getElement(3,i)*from.getElement(index+i*dim,j) );
302  }
303  }
304  code.addStatement( loop1 );
305 
306  return SUCCESSFUL_RETURN;
307 }
308 
309 
311 {
313 
314  if (nRightHandSides <= 0)
316 
317  int useOMP;
318  get(CG_USE_OPENMP, useOMP);
319  ExportStruct structWspace;
320  structWspace = useOMP ? ACADO_LOCAL : ACADO_WORKSPACE;
321 
322  A_mem = ExportVariable( std::string( "rk_mem_" ) + identifier + "A", dim, dim, REAL, structWspace );
323  b_mem = ExportVariable( std::string( "rk_mem_" ) + identifier + "b", 4*dim, nRightHandSides, REAL, structWspace );
324 
326  solve.setReturnValue( determinant, false );
327  solve.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
328 
329  if( REUSE ) {
331  solveReuse.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
332  if( TRANSPOSE ) {
333  b_mem_trans = ExportVariable( std::string( "rk_mem_trans_" ) + identifier + "b", 4*dim, 1, REAL, structWspace );
335  solveReuseTranspose.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
336  }
337  }
338 
339  A_full = ExportVariable( "A", dim, dim, REAL );
340  I_full = ExportVariable( "A_I", dim, dim, REAL );
342  rk_perm_full = ExportVariable( "rk_perm", 1, dim, INT );
343 
344  if( implicit ) {
345  solve_full = ExportFunction( getNameSolveFunction(), A_full, I_full, rk_perm_full ); // Only perform the LU factorization!
346  }
347  else {
348  solve_full = ExportFunction( getNameSolveFunction(), A_full, rk_perm_full ); // Only perform the LU factorization!
349  }
351  solve_full.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
352  if( REUSE ) {
353  if( implicit ) {
356  }
357  else {
359  if( TRANSPOSE ) {
360  b_full_trans = ExportVariable( "b", 4*dim, 1, REAL );
362  }
363  }
364  solveReuse_full.addLinebreak( ); // FIX: TO MAKE SURE IT GETS EXPORTED
365  }
366 
367  return SUCCESSFUL_RETURN;
368 }
369 
370 
371 returnValue ExportIRK4StageSingleNewton::setTransformations( const double _tau, const DVector& _low_tria, const DMatrix& _transf1, const DMatrix& _transf2, const DMatrix& _transf1_T, const DMatrix& _transf2_T ) {
372  tau = _tau;
373  low_tria = _low_tria;
374  transf1 = _transf1;
375  transf2 = _transf2;
376  transf1_T = _transf1_T;
377  transf2_T = _transf2_T;
378 
379  if( _tau <= 0 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
380  if( _transf1.getNumRows() != 4 || _transf1.getNumCols() != 4 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
381  if( _transf2.getNumRows() != 4 || _transf2.getNumCols() != 4 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
382  if( _transf1_T.getNumRows() != 4 || _transf1_T.getNumCols() != 4 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
383  if( _transf2_T.getNumRows() != 4 || _transf2_T.getNumCols() != 4 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
384  if( _low_tria.getDim() != 6 ) return ACADOERROR( RET_INVALID_ARGUMENTS );
385  if( _transf1.isZero() || _transf2.isZero() || _transf1_T.isZero() || _transf2_T.isZero() || _low_tria.isZero() ) return ACADOERROR( RET_INVALID_ARGUMENTS );
386 
387  return SUCCESSFUL_RETURN;
388 }
389 
390 
392  stepsize = _stepsize;
393 
394  return SUCCESSFUL_RETURN;
395 }
396 
397 
399 
400  return string( "solve_" ) + identifier + "sub_system";
401 }
402 
403 
405 
406  return string( "solve_" ) + identifier + "sub_system_reuse";
407 }
408 
409 
411 
412  return string( "solve_" ) + identifier + "sub_transpose_reuse";
413 }
414 
416 
417  implicit = _implicit;
418 
419  return SUCCESSFUL_RETURN;
420 }
421 
422 
423 //
424 // PROTECTED MEMBER FUNCTIONS:
425 //
426 
427 
428 
430 
431 // end of file.
bool isEmpty() const
Definition: matrix.hpp:193
ExportVariable getRow(const ExportIndex &idx) const
const std::string getNameSolveTransposeReuseFunction()
ExportFunction solveReuseTranspose
virtual returnValue setup()
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)
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
returnValue setImplicit(BooleanType _implicit)
Defines a scalar-valued index variable to be used for exporting code.
returnValue setTransformations(const double _tau, const DVector &_low_tria, const DMatrix &_transf1, const DMatrix &_transf2, const DMatrix &_transf1_T, const DMatrix &_transf2_T)
virtual returnValue getCode(ExportStatementBlock &code)
virtual returnValue getDataDeclarations(ExportStatementBlock &declarations, ExportStruct dataStruct=ACADO_ANY) const
ExportStruct
Allows to export Gaussian elimination for solving linear systems of specific dimensions.
unsigned getDim() const
Definition: vector.hpp:172
const std::string getNameSolveReuseFunction()
Encapsulates all user interaction for setting options, logging data and plotting results.
Allows to export code of an arbitrary function.
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
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 setStepSize(double _stepsize)
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.
ExportArgument getAddress(const ExportIndex &_rowIdx, const ExportIndex &_colIdx=emptyConstExportIndex) const
virtual returnValue performTransformation(ExportStatementBlock &code, const ExportVariable &from, const ExportVariable &to, const ExportVariable &transf, const ExportIndex &index)
ExportIRK4StageSingleNewton(UserInteraction *_userInteraction=0, const std::string &_commonHeaderName="")
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