MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
discctrl.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/elec/discctrl.h,v 1.30 2017/01/12 14:46:22 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /* Discrete Control:
33  * writes the output of selected dofs to a subprocess that generates the
34  * input; then applies the input to selected dofs as an abstract force
35  */
36 
37 #ifndef DISCCTRL_H
38 #define DISCCTRL_H
39 
40 #include <iostream>
41 #include <fstream>
42 
43 #include "elec.h"
44 #include "scalarvalue.h"
45 
46 #include "id.h"
47 #include "gpc.h"
48 #include "px.h"
49 
50 /* DiscreteControlProcess - begin */
51 /* This class provides input and output writing to and from the discrete
52  * control MBDyn element and the external process that actually computes
53  * the control inputs from the control outputs.
54  *
55  * At present, for developing and debugging purposes, the class itself
56  * generates the output.
57  *
58  * The discrete system is thought in the form:
59  *
60  * y(k) = alpha_1*y(k-1)+...+alpha_p*y(k-p)
61  * +beta_0*u(k)+...+beta_p*u(k-p)
62  *
63  * where y(i) is the m x 1 vector of the measures at step i,
64  * alpha_v is the v-th m x m system matrix,
65  * beta_w is the w-th m x r input matrix, and
66  * u(j) is the r x 1 input vector at step j.
67  *
68  * The controller computes the control input at step k based on the inputs
69  * and outputs at previous time steps [and on the desired value
70  * for the outputs] by means of the control matrices a_v, b_w
71  * [and m_d] :
72  *
73  * u(k) = a_1*y(k-1)+...+a_p*y(k-p)+b_1*u(k-1)+...+b_p*u(k-p)
74  *
75  * [ ... + m_s*yd(k+s)+...+m_0*y_d(k)
76  * (Since usually the future values of y_d are unknown, the actual oves are
77  * considered, that is y_d is shifted backwards by s places) [B]
78  *
79  * The matrices are organised as follows:
80  *
81  * A = [a_1,...,a_p]
82  * B = [b_1,...,b_p]
83  *
84  * [ M = [m_s,...,m_0] ]
85  *
86  * The vectors are stacked as follows:
87  *
88  * Y = [y(k-1),...,y(k-p)]
89  * U = [u(k-1),...,u(k-p)]
90  *
91  * as soon as a new measure set and a new control set is available,
92  * it replaces the oldest one, that is not required any more, and the
93  * computation of the input uses the old inputs and outputs vectors
94  * as queues, maintaining a reference to the latest ones. This minimizes the
95  * operations required for the computation of control inputs for large systems
96  */
97 
99 public:
100  enum {
102 
103  DISCPROC_AR = 0x1U,
104  DISCPROC_MA = 0x2U,
105  DISCPROC_X = 0x4U,
106 
110 
112  };
113 
114 public:
115  virtual ~DiscreteControlProcess(void);
116 
117  // Returns the new control input values in array dIn
118  virtual void GetInput(std::vector<doublereal>& dIn) = 0;
119 
120  // Sets the new measures (and the input)
121  virtual void PutOutput(const std::vector<doublereal>& dOut,
122  const std::vector<doublereal>& dIn,
123  const std::vector<doublereal>& dDesiredOut) = 0;
124 };
125 
126 /* DiscreteControlProcess - end */
127 
128 
129 /* DiscreteControlARXProcess_Debug - begin */
130 
132 protected:
133  integer iNumOutputs; // Number of outputs (measures)
134  integer iNumInputs; // Number of inputs (forces)
135  integer iOrderA; // Order of the system (p)
136  integer iOrderB; // Order of the input (p)
137  doublereal* pdA; // Stack of matrices a_c (r x (p*m))
138  doublereal* pdY; // Stack of output vectors at previous times (p*m)
139  doublereal* pdB; // Stack of matrices b_c (r x (p*r) )
140  doublereal* pdU; // Stack of input vectors at previous times (p*r)
141  doublereal* pdU0; // Current input vector (r)
142  integer iRefA; // Current position of most recent output
143  integer iRefB; // Current position of most recent input
144 
145  const std::string infile;
146 
147  // Reads the control matrices
148  int
149  ReadMatrix(std::istream& In, doublereal* pd,
150  unsigned int iRows, unsigned int iCols,
151  unsigned int iNumSubMats,
152  const char* sMatName);
153 
154 public:
156  integer iOrdA, integer iOrdB, const std::string& infile);
157  virtual ~DiscreteControlARXProcess_Debug(void);
158 
159  // Returns the new control input values in array dIn
160  void GetInput(std::vector<doublereal>& dIn);
161 
162  // Sets the new measures (and the input)
163  void
164  PutOutput(const std::vector<doublereal>& dOut,
165  const std::vector<doublereal>& dIn,
166  const std::vector<doublereal>& dDesiredOut);
167 };
168 
169 /* DiscreteControlARXProcess_Debug - end */
170 
171 
172 /* DiscreteIdentProcess_Debug - begin */
173 
175 protected:
176  integer iNumOutputs; /* Number of outputs (measures) */
177  integer iNumInputs; /* Number of inputs (forces) */
178  integer iOrderA; /* Order of the system (p) */
179  integer iOrderB; /* Order of the input (p) */
180 
181  IdentProcess* pId; /* Identifier */
182  PersistentExcitation* pPx; /* Excitation */
183 
184  // provvisorio?!?
185  const std::string outfile;
186  std::ofstream out;
187 
188 public:
190  integer iOrdA, integer iOrdB,
191  ForgettingFactor* pf,
193  unsigned f_proc,
194  const std::string& sf);
195  virtual ~DiscreteIdentProcess_Debug(void);
196 
197  // Returns the new control input values in array dIn
198  void GetInput(std::vector<doublereal>& dIn);
199 
200  // Sets the new measures (and the input)
201  void PutOutput(const std::vector<doublereal>& dOut,
202  const std::vector<doublereal>& dIn,
203  const std::vector<doublereal>& dDesiredOut);
204 };
205 
206 /* DiscreteIdentProcess_Debug - end */
207 
208 
209 /* DAC_Process_Debug - begin */
210 
212 protected:
213  integer iNumOutputs; // Number of outputs (measures)
214  integer iNumInputs; // Number of inputs (forces)
215  integer iOrderA; // Order of the system (pa)
216  integer iOrderB; // Order of the input (pb)
217  integer iOrderMd; // Order of the desired output
219  doublereal* pdTheta; // Predicted matrix
220  doublereal* pdA; // Stack of matrices a_c (r x (pa*m))
221  doublereal* pdY; // Stack of output vectors at previous times (p*m)
222  doublereal* pdB; // Stack of matrices b_c (r x (pb*r))
223  doublereal* pdU; // Stack of input vectors at previous times (p*r)
224  unsigned f_proc;
225  doublereal* pdC; // Stack of matrices c_c (r x (pa*m))
226  doublereal* pdE; // Stack of error vectors at previous times
227  doublereal* pdMd; // Stack of matrices m_c (r x (pa*?))
228  doublereal* pdYd; // Stack of desired output vectors at following times
229  doublereal* pdU0; // Current input vector (r)
230  integer iRefA; // Current position of most recent output
231  integer iRefB; // Current position of most recent input
232  integer iRefMd; // Current position of most recent desired output
233 
234  IdentProcess* pId; // Identifier
235  GPCDesigner* pCD; // control designer
236  PersistentExcitation* pPx; // excitation
237  DriveOwner Trigger; // control trigger
238 
239  std::vector<DriveOwner*> vDesiredOut;
240 
241  // provvisorio?!?
242  const std::string outfile;
243  std::ofstream out;
244 
245 public:
246  DAC_Process_Debug(integer iNumOut, integer iNumIn,
247  integer iOrdA, integer iOrdB,
248  ForgettingFactor* pf,
249  GPCDesigner* pd,
251  DriveCaller* PTrig,
252  std::vector<DriveCaller *>& vDesOut,
253  const std::string& sf,
254  unsigned f_proc);
255  virtual ~DAC_Process_Debug(void);
256 
257  // Returns the new control input values in array dIn
258  void GetInput(std::vector<doublereal>& dIn);
259 
260  // Sets the new measures (and the input)
261  void PutOutput(const std::vector<doublereal>& dOut,
262  const std::vector<doublereal>& dIn,
263  const std::vector<doublereal>& dDesiredOut);
264 };
265 
266 /* DAC_Process_Debug - end */
267 
268 
269 /* DiscreteControlElem - begin */
270 
271 class DiscreteControlElem : virtual public Elem, public Electric {
272 protected:
274  bool bNewStep; // Decides whether to read data from control process
275  integer iNumIter; // The control acts at iNumIter*dt time steps
277 
279  std::vector<ScalarValue *> vOutputs;
280  std::vector<DriveOwner*> vOutScaleFact;
281  std::vector<doublereal> dOut;
282 
285  std::vector<doublereal> dIn;
286 
287 public:
288  DiscreteControlElem(unsigned int uL, const DofOwner* pDO,
289  integer iNumOut,
290  std::vector<ScalarValue *>& vOut,
291  std::vector<DriveCaller *>& vOutSF,
292  integer iNumIn,
293  ScalarDof* ppIn,
295  integer iNIt,
296  flag fOut);
297  virtual ~DiscreteControlElem(void);
298 
299  virtual Electric::Type GetElectricType(void) const;
300 
301  /* Scrive il contributo dell'elemento al file di restart */
302  virtual std::ostream& Restart(std::ostream& out) const;
303 
304  virtual void
305  AfterConvergence(const VectorHandler& X, const VectorHandler& XP);
306 
307  /* ritorna il numero di Dofs per gli elementi che sono anche DofOwners */
308  virtual unsigned int iGetNumDof(void) const;
309 
310  /* esegue operazioni sui dof di proprieta' dell'elemento */
311  virtual DofOrder::Order GetDofType(unsigned int /* i */ ) const;
312 
313  /* Dimensioni del workspace */
314  virtual void WorkSpaceDim(integer* piNumRows, integer* piNumCols) const;
315 
316  /* assemblaggio jacobiano */
317  virtual VariableSubMatrixHandler&
318  AssJac(VariableSubMatrixHandler& WorkMat,
319  doublereal /* dCoef */ ,
320  const VectorHandler& /* XCurr */ ,
321  const VectorHandler& /* XPrimeCurr */ );
322 
323  /* assemblaggio residuo */
324  virtual SubVectorHandler&
325  AssRes(SubVectorHandler& WorkVec,
326  doublereal dCoef,
327  const VectorHandler& XCurr,
328  const VectorHandler& XPrimeCurr);
329 
330  /* Dati privati */
331  virtual unsigned int iGetNumPrivData(void) const;
332  virtual unsigned int iGetPrivDataIdx(const char *s) const;
333  virtual doublereal dGetPrivData(unsigned int i) const;
334 
335  /* *******PER IL SOLUTORE PARALLELO******** */
336  /* Fornisce il tipo e la label dei nodi che sono connessi all'elemento
337  * utile per l'assemblaggio della matrice di connessione fra i dofs */
338  virtual void
339  GetConnectedNodes(std::vector<const Node *>& connectedNodes) const;
340  /* ************************************************ */
341 };
342 
343 /* DiscreteControlElem - end */
344 
345 #endif /* DISCCTRL_H */
346 
unsigned f_proc
Definition: discctrl.h:224
const std::string infile
Definition: discctrl.h:145
std::vector< doublereal > dOut
Definition: discctrl.h:281
long int flag
Definition: mbdyn.h:43
integer iNumOutputs
Definition: discctrl.h:213
const std::string outfile
Definition: discctrl.h:185
IdentProcess * pId
Definition: discctrl.h:234
std::vector< DriveOwner * > vOutScaleFact
Definition: discctrl.h:280
doublereal * pdU0
Definition: discctrl.h:229
std::vector< doublereal > dIn
Definition: discctrl.h:285
doublereal * pdC
Definition: discctrl.h:225
std::vector< DriveOwner * > vDesiredOut
Definition: discctrl.h:239
integer iNumInputs
Definition: discctrl.h:214
IdentProcess * pId
Definition: discctrl.h:181
virtual ~DiscreteControlProcess(void)
Definition: discctrl.cc:46
doublereal * pdE
Definition: discctrl.h:226
GPCDesigner * pCD
Definition: discctrl.h:235
integer iOrderA
Definition: discctrl.h:215
DiscreteControlProcess * pDCP
Definition: discctrl.h:273
integer iOrderB
Definition: discctrl.h:216
doublereal * pdTheta
Definition: discctrl.h:219
PersistentExcitation * pPx
Definition: discctrl.h:236
std::vector< ScalarValue * > vOutputs
Definition: discctrl.h:279
doublereal * pdBase
Definition: discctrl.h:218
doublereal * pdU
Definition: discctrl.h:223
ScalarDof * pInputs
Definition: discctrl.h:284
const std::string outfile
Definition: discctrl.h:242
PersistentExcitation * pPx
Definition: discctrl.h:182
doublereal * pdY
Definition: discctrl.h:221
Definition: elec.h:43
integer iNumOutputs
Definition: discctrl.h:278
Type
Definition: elec.h:46
std::ofstream out
Definition: discctrl.h:243
DriveOwner Trigger
Definition: discctrl.h:237
integer iNumInputs
Definition: discctrl.h:283
virtual void PutOutput(const std::vector< doublereal > &dOut, const std::vector< doublereal > &dIn, const std::vector< doublereal > &dDesiredOut)=0
doublereal * pdB
Definition: discctrl.h:222
integer iOrderMd
Definition: discctrl.h:217
doublereal * pdMd
Definition: discctrl.h:227
Definition: elem.h:75
integer iRefMd
Definition: discctrl.h:232
virtual void GetInput(std::vector< doublereal > &dIn)=0
doublereal * pdA
Definition: discctrl.h:220
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
doublereal * pdYd
Definition: discctrl.h:228