MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
module-multi_step_drive.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-multi_step_drive/module-multi_step_drive.cc,v 1.4 2017/01/12 14:55:17 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 <limits>
47 #include <vector>
48 #include <drive.h>
49 
50 #include <myassert.h>
51 #include <except.h>
52 #include <dataman.h>
54 
56 {
57 public:
58  struct StepRecord
59  {
61  :x(x), y(y) { }
64  };
65 
66  explicit MultiStepDrive(const DriveHandler* pDH, const std::vector<StepRecord>& drives);
67  virtual ~MultiStepDrive();
68  bool bIsDifferentiable(void) const;
69  virtual std::ostream& Restart(std::ostream& out) const;
70  doublereal dGet(const doublereal& dVar) const;
71  virtual doublereal dGetP(const doublereal& dVar) const;
72  DriveCaller* pCopy(void) const;
73 
74 private:
75  typedef std::vector<StepRecord>::const_iterator iterator;
76  const std::vector<StepRecord> rgSteps;
77 };
78 
80  DriveCaller *
81  Read(const DataManager* pDM, MBDynParser& HP, bool bDeferred);
82 };
83 
84 MultiStepDrive::MultiStepDrive(const DriveHandler* pDH, const std::vector<StepRecord>& rgSteps)
85 : DriveCaller(pDH),
86  rgSteps(rgSteps)
87 {
88  NO_OP;
89 }
90 
92 {
93  NO_OP;
94 }
95 
97 MultiStepDrive::dGet(const doublereal& dVar) const
98 {
99  ASSERT(rgSteps.size() >= 1u);
100 
101  if (dVar < rgSteps.front().x) {
102  return rgSteps.front().y;
103  }
104 
105  for (iterator i = rgSteps.begin(); i != rgSteps.end() - 1; ++i) {
106  if (dVar >= i->x && dVar < (i + 1)->x) {
107  return i->y;
108  }
109  }
110 
111  return rgSteps.back().y;
112 }
113 
115 {
116  return 0.;
117 }
118 
120 {
121  return false;
122 }
123 
124 /* Restart */
125 std::ostream&
126 MultiStepDrive::Restart(std::ostream& out) const
127 {
128  out << "multi step, " << rgSteps.size() << ", ";
129 
130  for (iterator i = rgSteps.begin(); i != rgSteps.end(); ++i) {
131  out << i->x << ", " << i->y;
132 
133  if (rgSteps.end() - i > 1) {
134  out << ", ";
135  }
136  }
137 
138  return out;
139 }
140 
141 
144 {
145  DriveCaller* pDC = 0;
146 
150 
151  return pDC;
152 }
153 
154 DriveCaller *
155 MultiStepDriveDCR::Read(const DataManager* pDM, MBDynParser& HP, bool bDeferred)
156 {
157  NeedDM(pDM, HP, bDeferred, "multi step");
158 
159  const DriveHandler* pDrvHdl = 0;
160 
161  if (pDM != 0) {
162  pDrvHdl = pDM->pGetDrvHdl();
163  }
164 
165  /* driver legato ad un grado di liberta' nodale */
166  if (pDM == 0) {
167  silent_cerr("sorry, since the driver is not owned by a DataManager" << std::endl
168  << "no DOF dependent drivers are allowed;" << std::endl
169  << "aborting..." << std::endl);
171  }
172 
173  DriveCaller *pDC = 0;
174 
175  const integer iNumSteps = HP.GetInt();
176 
177  if (iNumSteps < 1) {
178  silent_cerr("At least one step expected at line " << HP.GetLineData() << std::endl);
180  }
181 
182  std::vector<MultiStepDrive::StepRecord> rgSteps;
183 
184  rgSteps.reserve(iNumSteps);
185 
186  doublereal xPrev = -std::numeric_limits<doublereal>::max();
187 
188  for (int i = 0; i < iNumSteps; ++i) {
189  const doublereal x = HP.GetReal();
190 
191  if (x <= xPrev) {
192  silent_cerr("X-values must be in ascending order at line "
193  << HP.GetLineData() << std::endl);
195  }
196 
197  const doublereal y = HP.GetReal();
198 
199  rgSteps.push_back(MultiStepDrive::StepRecord(x, y));
200 
201  xPrev = x;
202  }
203 
206  MultiStepDrive(pDrvHdl, rgSteps));
207 
208  return pDC;
209 }
210 
211 bool
213 {
215 
216  if (!SetDriveCallerData("multi" "step", rf)) {
217  delete rf;
218  return false;
219  }
220 
221  return true;
222 }
223 
224 #ifndef STATIC_MODULES
225 
226 extern "C" int
227 module_init(const char *module_name, void *pdm, void *php)
228 {
229  if (!switch_drive_set()) {
230  silent_cerr("multi_step_drive: "
231  "module_init(" << module_name << ") "
232  "failed" << std::endl);
233  return -1;
234  }
235 
236  return 0;
237 }
238 
239 #endif // ! STATIC_MODULES
MultiStepDrive(const DriveHandler *pDH, const std::vector< StepRecord > &drives)
bool SetDriveCallerData(const char *name, DriveCallerRead *rf)
Definition: drive_.cc:1324
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
std::vector< StepRecord >::const_iterator iterator
virtual const DriveHandler * pGetDrvHdl(void) const
Definition: drive.cc:493
DriveCaller * pCopy(void) const
const DriveHandler * pGetDrvHdl(void) const
Definition: dataman.h:340
#define NO_OP
Definition: myassert.h:74
StepRecord(doublereal x=0., doublereal y=0.)
bool switch_drive_set()
const std::vector< StepRecord > rgSteps
bool bIsDifferentiable(void) const
int module_init(const char *module_name, void *pdm, void *php)
This function registers our user defined element for the math parser.
#define ASSERT(expression)
Definition: colamd.c:977
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
virtual doublereal dGet(void) const
Definition: drive.h:489
DriveCaller * Read(const DataManager *pDM, MBDynParser &HP, bool bDeferred)
virtual doublereal dGetP(void) const
Definition: drive.h:501
void NeedDM(const DataManager *pDM, MBDynParser &HP, bool bDeferred, const char *const name)
Definition: drive_.cc:1354
bool multi_step_drive_set()
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
virtual std::ostream & Restart(std::ostream &out) const
virtual doublereal GetReal(const doublereal &dDefval=0.0)
Definition: parser.cc:1056