MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
bufferstream_out_elem.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/bufferstream_out_elem.cc,v 1.9 2017/01/12 14:46:08 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  * Michele Attolico <attolico@aero.polimi.it>
34  */
35 
36 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
37 
38 #include <sstream>
39 
40 #include "dataman.h"
41 #include "bufferstream_out_elem.h"
42 
43 /* BufferStreamElem_base - begin */
44 
46  unsigned int oe,
47  StreamContent *pSC, StreamOutEcho *pSOE)
48 : Elem(uL, flag(0)),
49 StreamOutElem(uL, "buffer", oe),
50 pSC(pSC), pSOE(pSOE)
51 {
52  if (pSOE != 0) {
53  pSOE->Init("BufferStreamElem_base", uLabel, pSC->GetNumChannels());
54  }
55 }
56 
58 {
59  if (pSC != 0) {
60  SAFEDELETE(pSC);
61  }
62 
63  if (pSOE != 0) {
64  delete pSOE;
65  }
66 }
67 
68 const integer
70 {
71  return pSC->GetNumChannels();
72 }
73 
74 void
78 {
79  // do not send "derivatives"
80  OutputCounter = -1;
81 }
82 
83 void
85  const VectorHandler& XP)
86 {
87  /* output only every OutputEvery steps */
88  OutputCounter++;
89  if (OutputCounter != OutputEvery) {
90  return;
91  }
92  OutputCounter = 0;
93 
94  // prepare the output buffer
95  pSC->Prepare();
96 
97  // check whether echo is needed
98  const doublereal *pd = (const doublereal *)pSC->GetBuf();
99  if (pSOE != 0) {
100  pSOE->Echo(pd, pSC->GetNumChannels());
101  }
102 
103  doublereal *pBuffer = const_cast<doublereal *>(GetBufRaw());
104  for (unsigned i = 0; i < pSC->GetNumChannels(); i++) {
105  pBuffer[i] = pd[i];
106  }
107 }
108 
109 void
111  const VectorHandler& XP, const VectorHandler& XPP)
112 {
113  AfterConvergence(X, XP);
114 }
115 
116 /* BufferStreamElem_base - end */
117 
118 /* BufferStreamElem - begin */
119 
121  unsigned int oe,
122  StreamContent *pSC, StreamOutEcho *pSOE)
123 : Elem(uL, flag(0)),
124 BufferStreamElem_base(uL, oe, pSC, pSOE),
125 buffer(pSC->GetNumChannels())
126 {
127  NO_OP;
128 }
129 
131 {
132  NO_OP;
133 }
134 
135 const doublereal *
137 {
138  // paranoid sanity check: callers of GetBuf() could have altered the size of the buffer...
139  ASSERT(buffer.size() == pSC->GetNumChannels());
140 
141  return &buffer[0];
142 }
143 
144 const std::vector<doublereal>&
146 {
147  // paranoid sanity check: callers of GetBuf() could have altered the size of the buffer...
148  ASSERT(buffer.size() == pSC->GetNumChannels());
149 
150  return buffer;
151 }
152 
153 std::ostream&
154 BufferStreamElem::Restart(std::ostream& out) const
155 {
156  return out << "# BufferStreamElem(" << GetLabel() << "): "
157  "not implemented yet" << std::endl;
158 }
159 
160 /* BufferStreamElem - end */
161 
162 /* BufferStreamElemRaw - begin */
163 
165  unsigned int oe,
166  StreamContent *pSC, StreamOutEcho *pSOE,
167  bool bOwnsMemory)
168 : Elem(uL, flag(0)),
169 BufferStreamElem_base(uL, oe, pSC, pSOE),
170 m_bOwnsMemory(bOwnsMemory),
171 m_pBuffer(0)
172 {
173  if (m_bOwnsMemory) {
174  m_pBuffer = new doublereal[pSC->GetNumChannels()];
175  }
176 }
177 
179 {
180  if (m_bOwnsMemory) {
181  delete[] m_pBuffer;
182  }
183 }
184 
185 bool
187 {
188  return m_bOwnsMemory;
189 }
190 
191 void
193 {
194  if (n != integer(pSC->GetNumChannels())) {
195  // error
196  std::ostringstream os;
197  os << "setting buffer pointer in BufferStreamElemRaw of wrong size (original=" << pSC->GetNumChannels() << ", new=" << n << ")";
198  throw ErrGeneric(MBDYN_EXCEPT_ARGS, os.str());
199  }
200 
201  if (m_bOwnsMemory) {
202  // error
203  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "setting buffer pointer in BufferStreamElemRaw that owns its memory");
204  }
205 
206  if (m_pBuffer != 0) {
207  // error; maybe we could simply replace it, couldn't we?
208  throw ErrGeneric(MBDYN_EXCEPT_ARGS, "setting buffer pointer in BufferStreamElemRaw that has already been set");
209  }
210 
211  m_pBuffer = p;
212 }
213 
214 const doublereal *
216 {
217  return m_pBuffer;
218 }
219 
220 std::ostream&
221 BufferStreamElemRaw::Restart(std::ostream& out) const
222 {
223  return out << "# BufferStreamElem(" << GetLabel() << "): "
224  "not implemented yet" << std::endl;
225 }
226 
227 /* BufferStreamElemRaw - end */
228 
229 Elem *
231 {
232  enum {
233  STL,
234  RAW
235  } eType = STL;
236  bool bOwnsMemory(true);
237 
238  if (HP.IsKeyWord("type")) {
239  if (HP.IsKeyWord("raw")) {
240  eType = RAW;
241  if (HP.IsKeyWord("owns" "memory")) {
242  bOwnsMemory = HP.GetYesNoOrBool();
243  }
244 
245  } else if (!HP.IsKeyWord("stl")) {
246  silent_cerr("BufferStreamDrive"
247  "(" << uLabel << "\"): "
248  "invalid type at line " << HP.GetLineData()
249  << std::endl);
251  }
252  }
253 
254  unsigned int OutputEvery = 1;
255  if (HP.IsKeyWord("output" "every")) {
256  int i = HP.GetInt();
257  if (i <= 0) {
258  silent_cerr("BufferStreamElem(" << uLabel << "): "
259  "invalid output every value " << i << " "
260  "at line " << HP.GetLineData() << std::endl);
262  }
263  OutputEvery = (unsigned int)i;
264  }
265 
266  StreamOutEcho *pSOE = ReadStreamOutEcho(HP);
267  StreamContent *pSC = ReadStreamContent(pDM, HP, type);
268 
269  /* Se non c'e' il punto e virgola finale */
270  if (HP.IsArg()) {
271  silent_cerr("BufferStreamElem(" << uLabel << "): "
272  "semicolon expected "
273  "at line " << HP.GetLineData() << std::endl);
275  }
276 
277  Elem *pEl = 0;
278  switch (eType) {
279  case STL:
281  BufferStreamElem(uLabel, OutputEvery, pSC, pSOE));
282  break;
283 
284  case RAW:
286  BufferStreamElemRaw(uLabel, OutputEvery, pSC, pSOE, bOwnsMemory));
287  break;
288 
289  default:
290  ASSERT(0);
291  break;
292  }
293 
294  return pEl;
295 }
296 
unsigned int OutputCounter
Definition: streamoutelem.h:60
long int flag
Definition: mbdyn.h:43
virtual const doublereal * GetBufRaw(void) const
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *ph=0)
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
void Echo(const doublereal *pbuf, unsigned nChannels)
BufferStreamElemRaw(unsigned int uL, unsigned int oe, StreamContent *pSC, StreamOutEcho *pSOE, bool bOwnsMemory)
std::vector< doublereal > buffer
const integer GetBufSize(void) const
void * GetBuf(void) const
#define NO_OP
Definition: myassert.h:74
std::vector< Hint * > Hints
Definition: simentity.h:89
virtual void AfterConvergence(const VectorHandler &X, const VectorHandler &XP)
virtual bool GetYesNoOrBool(bool bDefval=false)
Definition: parser.cc:1038
virtual const doublereal * GetBufRaw(void) const
virtual unsigned GetNumChannels(void) const =0
bool Init(const std::string &msg, unsigned uLabel, unsigned nChannels)
unsigned int OutputEvery
Definition: streamoutelem.h:59
StreamContent * ReadStreamContent(DataManager *pDM, MBDynParser &HP, StreamContent::Type type)
virtual std::ostream & Restart(std::ostream &out) const
StreamOutEcho * ReadStreamOutEcho(MBDynParser &HP)
bool bOwnsMemory(void) const
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual void Prepare(void)=0
virtual ~BufferStreamElemRaw(void)
unsigned int uLabel
Definition: withlab.h:44
#define ASSERT(expression)
Definition: colamd.c:977
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
virtual ~BufferStreamElem(void)
virtual std::ostream & Restart(std::ostream &out) const
virtual const doublereal * GetBufRaw(void) const =0
virtual bool IsArg(void)
Definition: parser.cc:807
Definition: elem.h:75
BufferStreamElem_base(unsigned int uL, unsigned int oe, StreamContent *pSC, StreamOutEcho *pSOE)
Elem * ReadBufferStreamElem(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, StreamContent::Type type)
virtual void SetBufRaw(integer n, const doublereal *p)
BufferStreamElem(unsigned int uL, unsigned int oe, StreamContent *pSC, StreamOutEcho *pSOE)
double doublereal
Definition: colamd.c:52
const std::vector< doublereal > & GetBuf(void) const
const doublereal * m_pBuffer
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
#define SAFEDELETE(pnt)
Definition: mynewmem.h:710
virtual ~BufferStreamElem_base(void)