MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
bufferstreamdrive.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/bufferstreamdrive.cc,v 1.8 2017/01/12 14:46:09 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 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <sstream>
35 
36 #include "dataman.h"
37 #include "filedrv.h"
38 #include "streamdrive.h"
39 #include "bufferstreamdrive.h"
40 
42  const DriveHandler* pDH,
43  integer nd, const std::vector<doublereal>& v0,
45  unsigned int ie,
46  StreamDriveEcho *pSDE)
47 : StreamDrive(uL, pDH, "buffer", nd, v0, true, pMod),
48 InputEvery(ie), InputCounter(ie - 1),
49 pSDE(pSDE)
50 {
51  // NOTE: InputCounter is set to InputEvery - 1 so that input
52  // is expected at initialization (initial time) and then every
53  // InputEvery steps; for example, for InputEvery == 4, input
54  // is expected at:
55  // initial time
56  // initial time + 4 * timestep
57  // initial time + 8 * timestep
58  ASSERT(InputEvery > 0);
59 
61 
62  if (pSDE != 0) {
63  pSDE->Init("BufferStreamDrive_base", uLabel, nd);
64  }
65 }
66 
68 {
69  if (pSDE != 0) {
70  delete pSDE;
71  }
72 }
73 
74 const integer
76 {
77  return iNumDrives;
78 }
79 
80 void
82 {
83  /* read only every InputEvery steps */
84  InputCounter++;
85  if (InputCounter != InputEvery) {
86  return;
87  }
88  InputCounter = 0;
89 
90  if (pSDE != 0) {
92  }
93 
94  // copy values from buffer
95  pMod->Modify(&pdVal[1], GetBufRaw());
96 
97  if (pSDE != 0) {
98  pSDE->Echo(&pdVal[1], iNumDrives);
99  }
100 }
101 
102 
104  const DriveHandler* pDH,
105  integer nd, const std::vector<doublereal>& v0,
106  StreamDrive::Modifier *pMod,
107  unsigned int ie,
108  StreamDriveEcho *pSDE)
109 : BufferStreamDrive_base(uL, pDH, nd, v0, pMod, ie, pSDE),
110 buffer(nd)
111 {
112  NO_OP;
113 }
114 
116 {
117  NO_OP;
118 }
119 
120 const doublereal *
122 {
123  // paranoid sanity check: callers of GetBuf() could have altered the size of the buffer...
124  ASSERT(buffer.size() == iNumDrives);
125 
126  return &buffer[0];
127 }
128 
129 std::vector<doublereal>&
131 {
132  // paranoid sanity check: callers of GetBuf() could have altered the size of the buffer...
133  ASSERT(buffer.size() == iNumDrives);
134 
135  return buffer;
136 }
137 
138 /* Scrive il contributo del DriveCaller al file di restart */
139 std::ostream&
140 BufferStreamDrive::Restart(std::ostream& out) const
141 {
142  // input every, echo, ...
143  out << " file: " << uLabel << ", buffer stream, type, stl, " << iNumDrives << ";" << std::endl;
144  return out;
145 }
146 
147 
149  const DriveHandler* pDH,
150  integer nd, const std::vector<doublereal>& v0,
151  StreamDrive::Modifier *pMod,
152  unsigned int ie,
153  StreamDriveEcho *pSDE,
154  bool bOwnsMemory)
155 : BufferStreamDrive_base(uL, pDH, nd, v0, pMod, ie, pSDE),
156 m_bOwnsMemory(bOwnsMemory),
157 m_pBuffer(0)
158 {
159  if (m_bOwnsMemory) {
160  m_pBuffer = new doublereal[nd];
161  }
162 }
163 
165 {
166  if (m_bOwnsMemory) {
167  delete[] m_pBuffer;
168  }
169 }
170 
171 bool
173 {
174  return m_bOwnsMemory;
175 }
176 
177 void
179 {
180  if (n != iNumDrives) {
181  // error
182  std::ostringstream os;
183  os << "setting buffer pointer in BufferStreamDriveRaw of wrong size (original=" << iNumDrives << ", new=" << n << ")";
184  throw ErrGeneric(MBDYN_EXCEPT_ARGS, os.str());
185  }
186 
187  if (m_bOwnsMemory) {
188  // error
189  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "setting buffer pointer in BufferStreamDriveRaw that owns its memory");
190  }
191 
192  if (m_pBuffer != 0) {
193  // error; maybe we could simply replace it, couldn't we?
194  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "setting buffer pointer in BufferStreamDriveRaw that has already been set");
195  }
196 
197  m_pBuffer = p;
198 }
199 
200 const doublereal *
202 {
203  ASSERT(m_pBuffer != 0);
204 
205  return m_pBuffer;
206 }
207 
208 /* Scrive il contributo del DriveCaller al file di restart */
209 std::ostream&
210 BufferStreamDriveRaw::Restart(std::ostream& out) const
211 {
212  // input every, echo, ...
213  out << " file: " << uLabel << ", buffer stream, type, raw, owns memory, " << (m_bOwnsMemory ? "yes" : "no" ) << iNumDrives << ";" << std::endl;
214  return out;
215 }
216 
217 
218 /* legge i drivers tipo stream */
219 
220 static Drive *
221 ReadBufferStreamDrive(const DataManager *pDM, MBDynParser& HP, unsigned uLabel)
222 {
223  enum {
224  STL,
225  RAW
226  } eType = STL;
227  bool bOwnsMemory(true);
228 
229  if (HP.IsKeyWord("type")) {
230  if (HP.IsKeyWord("raw")) {
231  eType = RAW;
232  if (HP.IsKeyWord("owns" "memory")) {
233  bOwnsMemory = HP.GetYesNoOrBool();
234  }
235 
236  } else if (!HP.IsKeyWord("stl")) {
237  silent_cerr("BufferStreamDrive"
238  "(" << uLabel << "\"): "
239  "invalid type at line " << HP.GetLineData()
240  << std::endl);
242  }
243  }
244 
245  unsigned int InputEvery = 1;
246  if (HP.IsKeyWord("input" "every")) {
247  int i = HP.GetInt();
248  if (i <= 0) {
249  silent_cerr("BufferStreamDrive"
250  "(" << uLabel << "\"): "
251  "invalid \"input every\" value " << i
252  << " at line " << HP.GetLineData()
253  << std::endl);
255  }
256  InputEvery = (unsigned int)i;
257  }
258 
259  StreamDriveEcho *pSDE = ReadStreamDriveEcho(pDM, HP);
260 
261  int idrives = HP.GetInt();
262  if (idrives <= 0) {
263  silent_cerr("BufferStreamDrive"
264  "(" << uLabel << "\"): "
265  "illegal number of channels " << idrives
266  << " at line " << HP.GetLineData()
267  << std::endl);
269  }
270 
271  std::vector<doublereal> v0;
272  if (HP.IsKeyWord("initial" "values")) {
273  v0.resize(idrives);
274  for (int i = 0; i < idrives; i++) {
275  v0[i] = HP.GetReal();
276  }
277  }
278 
279  StreamDrive::Modifier *pMod(0);
280  if (HP.IsKeyWord("modifier")) {
281  pMod = ReadStreamDriveModifier(HP, idrives);
282  }
283 
284  Drive* pDr = 0;
285  switch (eType) {
286  case STL:
288  BufferStreamDrive(uLabel,
289  pDM->pGetDrvHdl(),
290  idrives, v0, pMod,
291  InputEvery, pSDE));
292  break;
293 
294  case RAW:
296  BufferStreamDriveRaw(uLabel,
297  pDM->pGetDrvHdl(),
298  idrives, v0, pMod,
299  InputEvery, pSDE, bOwnsMemory));
300  break;
301 
302  default:
303  ASSERT(0);
304  break;
305  }
306 
307  return pDr;
308 }
309 
310 
311 Drive *
312 BufferStreamDR::Read(unsigned uLabel, const DataManager *pDM, MBDynParser& HP)
313 {
314  return ReadBufferStreamDrive(pDM, HP, uLabel);
315 }
StreamDriveEcho * pSDE
virtual ~BufferStreamDrive_base(void)
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual void ServePending(const doublereal &t)
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
BufferStreamDrive(unsigned int uL, const DriveHandler *pDH, integer nd, const std::vector< doublereal > &v0, StreamDrive::Modifier *pMod, unsigned int ie, StreamDriveEcho *pSDE)
Definition: drive.h:89
StreamDriveEcho * ReadStreamDriveEcho(const DataManager *pDM, MBDynParser &HP)
Definition: streamdrive.cc:280
StreamDrive::Modifier * ReadStreamDriveModifier(MBDynParser &HP, integer nDrives)
Definition: streamdrive.cc:169
BufferStreamDriveRaw(unsigned int uL, const DriveHandler *pDH, integer nd, const std::vector< doublereal > &v0, StreamDrive::Modifier *pMod, unsigned int ie, StreamDriveEcho *pSDE, bool bOwnsMemory)
const DriveHandler * pGetDrvHdl(void) const
Definition: dataman.h:340
#define NO_OP
Definition: myassert.h:74
std::vector< doublereal > buffer
integer iNumDrives
Definition: filedrv.h:47
bool bOwnsMemory(void) const
virtual bool GetYesNoOrBool(bool bDefval=false)
Definition: parser.cc:1038
void Echo(const doublereal *pbuf, unsigned nChannels)
Definition: streamdrive.cc:264
doublereal * pdVal
Definition: filedrv.h:48
void SetBufRaw(integer n, const doublereal *p)
const doublereal * GetBufRaw(void)
static Drive * ReadBufferStreamDrive(const DataManager *pDM, MBDynParser &HP, unsigned uLabel)
DataManager * pDM
Definition: mbpar.h:252
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual void Modify(doublereal *out, const void *in) const =0
virtual ~BufferStreamDrive(void)
Definition: mbdyn.h:77
unsigned int uLabel
Definition: withlab.h:44
#define ASSERT(expression)
Definition: colamd.c:977
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
virtual std::ostream & Restart(std::ostream &out) const
virtual Drive * Read(unsigned uLabel, const DataManager *pDM, MBDynParser &HP)
void EchoPrepare(const doublereal *pbuf, unsigned nChannels)
Definition: streamdrive.cc:256
const doublereal * m_pBuffer
std::vector< doublereal > & GetBuf(void)
virtual ~BufferStreamDriveRaw(void)
bool Init(const std::string &msg, unsigned uLabel, unsigned nChannels)
Definition: streamdrive.cc:223
static const std::vector< doublereal > v0
Definition: fixedstep.cc:45
virtual const doublereal * GetBufRaw(void)=0
const Modifier * pMod
Definition: streamdrive.h:69
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
const doublereal * GetBufRaw(void)
BufferStreamDrive_base(unsigned int uL, const DriveHandler *pDH, integer nd, const std::vector< doublereal > &v0, StreamDrive::Modifier *pMod, unsigned int ie, StreamDriveEcho *pSDE)
const integer GetBufSize(void) const
virtual doublereal GetReal(const doublereal &dDefval=0.0)
Definition: parser.cc:1056