MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
streamdrive.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/streamdrive.cc,v 1.28 2017/08/28 10:34:39 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 "dataman.h"
35 #include "filedrv.h"
36 #include "streamdrive.h"
37 
38 #include "bufmod.h"
39 
40 /* StreamDrive - begin */
41 
42 StreamDrive::StreamDrive(unsigned int uL,
43  const DriveHandler* pDH,
44  const std::string& sFileName,
45  integer nd, const std::vector<doublereal>& v0,
46  bool c, StreamDrive::Modifier *pmod)
47 : FileDrive(uL, pDH, sFileName, nd, v0),
48 create(c),
49 pMod(pmod)
50 {
51  ASSERT(nd > 0);
52 
53  if (pMod == 0) {
54  pMod = new StreamDrive::Copy(nd);
55  }
56 
57  // initialize mailbox and so on
58  size = pMod->GetSize();
59  buf.resize(size);
60 }
61 
63 {
64  if (pMod) {
65  delete pMod;
66  }
67 }
68 
69 void
71 {
72  // FIXME: what if is not 0?
73  if (pMod != 0) {
74  delete pMod;
75  }
76 
77  pMod = p;
78 }
79 
81 {
82  return pMod;
83 }
84 
86 {
87  NO_OP;
88 }
89 
91 {
92  NO_OP;
93 }
94 
96 : m_iND(iND)
97 {
98  NO_OP;
99 }
100 
101 size_t
103 {
104  return (sizeof(doublereal)*m_iND);
105 }
106 
107 void
108 StreamDrive::Copy::Modify(doublereal *out, const void *in) const
109 {
110  const doublereal *pd_in = (const doublereal *)in;
111  for (int i = 0; i < m_iND; i++) {
112  out[i] = pd_in[i];
113  }
114 }
115 
116 /* StreamDrive - end */
117 
118 
119 /* StreamDriveCopyCast - begin */
120 
121 /* moved to streamdrive.h FIXME: DELETE
122 class StreamDriveCopyCast : public StreamDrive::Modifier
123 {
124 protected:
125  size_t m_size;
126  std::vector<BufCast *> m_data;
127 
128 public:
129  StreamDriveCopyCast(size_t size, const std::vector<BufCast *>& data);
130  ~StreamDriveCopyCast(void);
131 
132  size_t GetSize(void) const;
133  void Modify(doublereal *out, const void *in) const;
134 };*/
135 
136 StreamDriveCopyCast::StreamDriveCopyCast(size_t size, const std::vector<BufCast *>& data)
137 : m_size(size), m_data(data)
138 {
139 #ifdef DEBUG
140  size_t minsize = m_data[m_data.size() - 1]->offset() + m_data[m_data.size() - 1]->size();
141  ASSERT(size >= minsize);
142 #endif
143 }
144 
146 {
147  for (std::vector<BufCast *>::iterator i = m_data.begin(); i != m_data.end(); ++i) {
148  delete *i;
149  }
150 }
151 
152 size_t
154 {
155  return m_size;
156 }
157 
158 void
159 StreamDriveCopyCast::Modify(doublereal *out, const void *in) const
160 {
161  for (size_t i = 0; i != m_data.size(); ++i) {
162  out[i] = m_data[i]->cast(in);
163  }
164 }
165 
166 /* StreamDriveCopyCast - end */
167 
170 {
171  StreamDrive::Modifier *pSDM(0);
172 
173  if (HP.IsKeyWord("copy" "cast")) {
174  std::vector<BufCast *> data(nDrives);
175  ReadBufCast(HP, data);
176  size_t minsize = data[data.size() - 1]->offset() + data[data.size() - 1]->size();
177  size_t size = minsize;
178  if (HP.IsKeyWord("size")) {
179  integer i = HP.GetInt();
180  if (i <= 0) {
181  silent_cerr("ReadStreamDriveModifier: invalid size " << i
182  << " at line " << HP.GetLineData() << std::endl);
184  }
185 
186  size = size_t(i);
187  if (size < minsize) {
188  silent_cerr("ReadStreamDriveModifier: size " << size
189  << " is less than min size " << minsize
190  << " at line " << HP.GetLineData() << std::endl);
192  }
193  }
194 
195  pSDM = new StreamDriveCopyCast(size, data);
196 
197  } else if (!HP.IsKeyWord("copy")) {
198  // TODO: "copy" with byte swap for network-independent stuff...
199  silent_cerr("ReadStreamDriveModifier: unknown modifier type at line " << HP.GetLineData() << std::endl);
201  }
202 
203  return pSDM;
204 }
205 
206 /* StreamDriveEcho - begin */
207 
208 StreamDriveEcho::StreamDriveEcho(const DriveHandler *pDrvHdl, std::string& sOutFileName, int iPrecision, doublereal dShift)
209 : pDrvHdl(pDrvHdl),
210 sOutFileName(sOutFileName),
211 iPrecision(iPrecision),
212 dShift(dShift)
213 {
214  NO_OP;
215 }
216 
218 {
219  NO_OP;
220 }
221 
222 bool
223 StreamDriveEcho::Init(const std::string& msg, unsigned uLabel, unsigned nChannels)
224 {
225  outFile.open(sOutFileName.c_str());
226  if (!outFile) {
227  silent_cerr(msg << "(" << uLabel << "): "
228  "unable to open echo file '" << sOutFileName << "'" << std::endl);
230  }
231  echoBuf.resize(nChannels);
232 
233  if (iPrecision > 0) {
234  outFile.precision(iPrecision);
235  }
236  outFile.setf(std::ios::scientific);
237 
238  outFile
239  << "# generated by SocketStreamDrive(" << uLabel << ")"
240  << std::endl;
241  if (nChannels == 1) {
242  outFile
243  << "# Time, Channel #1"
244  << std::endl;
245 
246  } else {
247  outFile
248  << "# Time, Channels #1-" << nChannels
249  << std::endl;
250  }
251 
252  return true;
253 }
254 
255 void
256 StreamDriveEcho::EchoPrepare(const doublereal *pbuf, unsigned size)
257 {
258  for (unsigned i = 1; i < size; i++) {
259  echoBuf[i] = pbuf[i];
260  }
261 }
262 
263 void
264 StreamDriveEcho::Echo(const doublereal *pbuf, unsigned size)
265 {
266  for (unsigned i = 0; i < size ; i++) {
267  if (pbuf[i] != echoBuf[i]) {
268  // changed; need to write
269  outFile << (pDrvHdl->dGetTime() + dShift);
270  for (unsigned j = 0; j < size ; j++) {
271  outFile << " " << pbuf[j];
272  }
273  outFile << std::endl;
274  break;
275  }
276  }
277 }
278 
281 {
282  StreamDriveEcho *pSDE(0);
283 
284  std::string sOutFileName;
285  int iPrecision = -1;
286  doublereal dShift = 0.;
287 
288  if (HP.IsKeyWord("echo")) {
289  const char *s = HP.GetFileName();
290  if (s == NULL) {
291  silent_cerr("ReadStreamDriveEcho: "
292  "unable to parse echo file name "
293  "at line " << HP.GetLineData()
294  << std::endl);
296  }
297 
298  sOutFileName = s;
299 
300  if (HP.IsKeyWord("precision")) {
301  iPrecision = HP.GetInt();
302  if (iPrecision <= 0) {
303  silent_cerr("ReadStreamDriveEcho: "
304  "invalid echo precision " << iPrecision
305  << " at line " << HP.GetLineData()
306  << std::endl);
308  }
309  }
310 
311  if (HP.IsKeyWord("shift")) {
312  dShift = HP.GetReal();
313  }
314 
315  pSDE = new StreamDriveEcho(pDM->pGetDrvHdl(), sOutFileName, iPrecision, dShift);
316  }
317 
318  return pSDE;
319 }
320 
321 /* StreamDriveEcho - end */
322 
size_t GetSize(void) const
Definition: streamdrive.cc:102
const DriveHandler * pDrvHdl
Definition: streamdrive.h:92
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
Copy(integer iND)
Definition: streamdrive.cc:95
~StreamDriveEcho(void)
Definition: streamdrive.cc:217
std::vector< BufCast * > m_data
Definition: streamdrive.h:117
std::vector< char > buf
Definition: streamdrive.h:65
StreamDriveEcho * ReadStreamDriveEcho(const DataManager *pDM, MBDynParser &HP)
Definition: streamdrive.cc:280
StreamDrive::Modifier * ReadStreamDriveModifier(MBDynParser &HP, integer nDrives)
Definition: streamdrive.cc:169
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
const DriveHandler * pGetDrvHdl(void) const
Definition: dataman.h:340
void Modify(doublereal *out, const void *in) const
Definition: streamdrive.cc:108
#define NO_OP
Definition: myassert.h:74
void Echo(const doublereal *pbuf, unsigned nChannels)
Definition: streamdrive.cc:264
int nChannels
Definition: s2s.h:56
std::vector< doublereal > echoBuf
Definition: streamdrive.h:94
virtual size_t GetSize(void) const =0
virtual ~StreamDrive(void)
Definition: streamdrive.cc:62
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual ~Modifier(void)
Definition: streamdrive.cc:90
const StreamDrive::Modifier * pGetModifier(void) const
Definition: streamdrive.cc:80
void Modify(doublereal *out, const void *in) const
Definition: streamdrive.cc:159
void Copy(scalar_func_type &d1, const scalar_func_type &d2, LocalDofMap *)
Definition: gradient.h:2827
#define ASSERT(expression)
Definition: colamd.c:977
void EchoPrepare(const doublereal *pbuf, unsigned nChannels)
Definition: streamdrive.cc:256
static std::stack< cleanup * > c
Definition: cleanup.cc:59
std::ofstream outFile
Definition: streamdrive.h:95
bool Init(const std::string &msg, unsigned uLabel, unsigned nChannels)
Definition: streamdrive.cc:223
StreamDriveEcho(const DriveHandler *pDrvHdl, std::string &sOutFileName, int iPrecision, doublereal dShift)
Definition: streamdrive.cc:208
size_t GetSize(void) const
Definition: streamdrive.cc:153
struct mbrtai_msg_t msg
std::string sOutFileName
Definition: streamdrive.h:93
doublereal dGetTime(void) const
Definition: drive.h:386
StreamDriveCopyCast(size_t size, const std::vector< BufCast * > &data)
Definition: streamdrive.cc:136
doublereal dShift
Definition: streamdrive.h:97
void SetModifier(const Modifier *p)
Definition: streamdrive.cc:70
static const std::vector< doublereal > v0
Definition: fixedstep.cc:45
const Modifier * pMod
Definition: streamdrive.h:69
double doublereal
Definition: colamd.c:52
void ReadBufCast(HighParser &HP, std::vector< BufCast * > &data)
Definition: bufmod.cc:287
long int integer
Definition: colamd.c:51
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
StreamDrive(unsigned int uL, const DriveHandler *pDH, const std::string &sFileName, integer nd, const std::vector< doublereal > &v0, bool c, StreamDrive::Modifier *pmod)
Definition: streamdrive.cc:42
int create
Definition: s2s.h:62
virtual doublereal GetReal(const doublereal &dDefval=0.0)
Definition: parser.cc:1056