MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
module-switch_drive.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-switch_drive/module-switch_drive.cc,v 1.5 2017/01/12 14:57:40 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 /*
33  AUTHOR: Reinhard Resch <r.resch@secop.com>
34  Copyright (C) 2015(-2017) all rights reserved.
35 
36  The copyright of this code is transferred
37  to Pierangelo Masarati and Paolo Mantegazza
38  for use in the software MBDyn as described
39  in the GNU Public License version 2.1
40 */
41 
42 #ifdef HAVE_CONFIG_H
43 #include "mbconfig.h"
44 #endif
45 
46 #include <vector>
47 #include <drive.h>
48 
49 #include <myassert.h>
50 #include <except.h>
51 #include <mynewmem.h>
52 #include <dataman.h>
53 
54 #include "module-switch_drive.h"
55 
57 {
58 public:
59  enum Type
60  {
64  };
65  explicit SwitchDriveCaller(const DriveHandler* pDH,
66  const DriveOwner& oSwitch,
67  Type eType,
68  const std::vector<DriveOwner>& drives);
69  virtual ~SwitchDriveCaller();
70  bool bIsDifferentiable(void) const;
71  virtual std::ostream& Restart(std::ostream& out) const;
72  doublereal dGet(const doublereal& dVar) const;
73  virtual doublereal dGetP(const doublereal& dVar) const;
74  DriveCaller* pCopy(void) const;
75 
76 private:
77  const DriveCaller* pGetDrive() const;
78 
81  typedef std::vector<DriveOwner>::const_iterator iterator;
82  const std::vector<DriveOwner> rgDrives;
83 };
84 
86  DriveCaller *
87  Read(const DataManager* pDM, MBDynParser& HP, bool bDeferred);
88 };
89 
91  const DriveOwner& oSwitch,
92  Type eType,
93  const std::vector<DriveOwner>& drives)
94 : DriveCaller(pDH),
95  oSwitch(oSwitch),
96  eType(eType),
97  rgDrives(drives)
98 {
99  NO_OP;
100 }
101 
103 {
104  NO_OP;
105 }
106 
108 {
109  const doublereal dSwitch = oSwitch.dGet();
110 
111  integer iDrive;
112 
113  switch (eType) {
114  case SDC_FLOOR:
115  iDrive = floor(dSwitch);
116  break;
117 
118  case SDC_CEIL:
119  iDrive = ceil(dSwitch);
120  break;
121 
122  case SDC_NEAREST:
123  iDrive = round(dSwitch);
124  break;
125 
126  default:
127  ASSERT(false);
129  }
130 
131  const integer iMaxIndex = rgDrives.size() - 1;
132 
133  if (iDrive < 0 || iDrive > iMaxIndex) {
134  silent_cerr("drive caller(" << GetLabel()
135  << "): argument " << iDrive
136  << " from drive caller " << oSwitch.pGetDriveCaller()->GetLabel()
137  << " is not in range [0:" << iMaxIndex << "]"
138  << std::endl);
139 
140  throw ErrIndexOutOfRange(iDrive, 0, iMaxIndex, MBDYN_EXCEPT_ARGS);
141  }
142 
143  return rgDrives[iDrive].pGetDriveCaller();
144 }
145 
148 {
149  return pGetDrive()->dGet(dVar);
150 }
151 
153 {
154  return pGetDrive()->dGetP(dVar);
155 }
156 
158 {
159  for (iterator i = rgDrives.begin(); i != rgDrives.end(); ++i) {
160  if (!i->bIsDifferentiable()) {
161  return false;
162  }
163  }
164 
165  return true;
166 }
167 
168 /* Restart */
169 std::ostream&
170 SwitchDriveCaller::Restart(std::ostream& out) const
171 {
172  out << "switch, " << (eType == SDC_FLOOR ? "floor, " : "ceil, ")
173  << rgDrives.size() << ", ";
174 
175  for (iterator i = rgDrives.begin(); i != rgDrives.end(); ++i) {
176  i->pGetDriveCaller()->Restart(out);
177 
178  if (rgDrives.end() - i > 1) {
179  out << ", ";
180  }
181  }
182 
183  return out;
184 }
185 
186 
189 {
190  DriveCaller* pDC = 0;
191 
195 
196  return pDC;
197 }
198 
199 DriveCaller *
200 SwitchDriveDCR::Read(const DataManager* pDM, MBDynParser& HP, bool bDeferred)
201 {
202  NeedDM(pDM, HP, bDeferred, "switch");
203 
204  const DriveHandler* pDrvHdl = 0;
205 
206  if (pDM != 0) {
207  pDrvHdl = pDM->pGetDrvHdl();
208  }
209 
210  /* driver legato ad un grado di liberta' nodale */
211  if (pDM == 0) {
212  silent_cerr("sorry, since the driver is not owned by a DataManager" << std::endl
213  << "no DOF dependent drivers are allowed;" << std::endl
214  << "aborting..." << std::endl);
216  }
217 
218  DriveCaller *pDC = 0;
219 
220  const DriveOwner oSwitch(HP.GetDriveCaller());
221 
223 
224  if (HP.IsKeyWord("floor")) {
226  } else if (HP.IsKeyWord("ceil")) {
228  } else if (HP.IsKeyWord("nearest")) {
230  } else {
231  silent_cerr("Keyword \"floor\" or \"ceil\" expected at line " << HP.GetLineData() << std::endl);
233  }
234 
235  const integer iNumDrives = HP.GetInt();
236 
237  if (iNumDrives < 1) {
238  silent_cerr("At least one drive caller expected at line " << HP.GetLineData() << std::endl);
240  }
241 
242  std::vector<DriveOwner> rgDrives;
243 
244  rgDrives.reserve(iNumDrives);
245 
246  for (int i = 0; i < iNumDrives; ++i) {
247  rgDrives.push_back(HP.GetDriveCaller());
248  }
249 
252  SwitchDriveCaller(pDrvHdl, oSwitch, eType, rgDrives));
253 
254  return pDC;
255 }
256 
257 bool
259 {
261 
262  if (!SetDriveCallerData("switch", rf)) {
263  delete rf;
264  return false;
265  }
266 
267  return true;
268 }
269 
270 #ifndef STATIC_MODULES
271 
272 extern "C" int
273 module_init(const char *module_name, void *pdm, void *php)
274 {
275  if (!switch_drive_set()) {
276  silent_cerr("switch_drive: "
277  "module_init(" << module_name << ") "
278  "failed" << std::endl);
279  return -1;
280  }
281 
282  return 0;
283 }
284 
285 #endif // ! STATIC_MODULES
DriveCaller * pCopy(void) const
bool SetDriveCallerData(const char *name, DriveCallerRead *rf)
Definition: drive_.cc:1324
const DriveCaller * pGetDrive() const
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
int module_init(const char *module_name, void *pdm, void *php)
This function registers our user defined element for the math parser.
virtual std::ostream & Restart(std::ostream &out) const
virtual const DriveHandler * pGetDrvHdl(void) const
Definition: drive.cc:493
const DriveHandler * pGetDrvHdl(void) const
Definition: dataman.h:340
#define NO_OP
Definition: myassert.h:74
virtual doublereal dGetP(const doublereal &dVar) const
Definition: drive.cc:499
bool bIsDifferentiable(void) const
bool switch_drive_set()
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
SwitchDriveCaller(const DriveHandler *pDH, const DriveOwner &oSwitch, Type eType, const std::vector< DriveOwner > &drives)
#define ASSERT(expression)
Definition: colamd.c:977
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
DriveCaller * pGetDriveCaller(void) const
Definition: drive.cc:658
virtual doublereal dGet(void) const
Definition: drive.h:489
virtual doublereal dGet(const doublereal &dVar) const =0
virtual doublereal dGetP(void) const
Definition: drive.h:501
const std::vector< DriveOwner > rgDrives
doublereal dGet(const doublereal &dVar) const
Definition: drive.cc:664
DriveCaller * GetDriveCaller(bool bDeferred=false)
Definition: mbpar.cc:2033
void NeedDM(const DataManager *pDM, MBDynParser &HP, bool bDeferred, const char *const name)
Definition: drive_.cc:1354
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
unsigned int GetLabel(void) const
Definition: withlab.cc:62
std::vector< DriveOwner >::const_iterator iterator
DriveCaller * Read(const DataManager *pDM, MBDynParser &HP, bool bDeferred)