MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
privpgin.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/privpgin.cc,v 1.30 2017/01/12 14:46:10 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 #include "privpgin.h"
36 
38 : MathParser::PlugIn(mp), pSE(0), iIndex(0), pDM(pDM)
39 {
40  ASSERT(pDM != 0);
41 }
42 
44 {
45  NO_OP;
46 }
47 
48 int
49 PrivPlugIn::Read(int argc, char *argv[])
50 {
51  unsigned int uLabel;
52 
53  if (argc < 1 || argv[0] == 0) {
54  silent_cerr("PrivPlugIn::Read(): "
55  "illegal number of parameters " << argc
56  << std::endl);
58  }
59  uLabel = ReadLabel(argv[0]);
60 
61  if (argc < 2 || argv[1] == 0) {
62  silent_cerr("PrivPlugIn::Read(" << argv[0] << "): "
63  << "illegal number of parameters " << argc
64  << std::endl);
66  }
67  ReadSE(uLabel, argv[1]);
68 
69  unsigned int iMaxIndex = pSE->iGetNumPrivData();
70  switch (iMaxIndex) {
71  case 0:
72  silent_cerr(*this << "allows no private data" << std::endl);
74 
75  case 1:
76  iIndex = 1;
77  if (argc < 3) {
78  break;
79  }
80  /* continue to next case */
81 
82  default:
83  if (argc > 3) {
84  silent_cerr("PrivPlugIn::Read(" << argv[0] << "): "
85  << "illegal number of parameters " << argc
86  << std::endl);
88  }
89  ReadIndex(iMaxIndex, argv[2]);
90  break;
91  }
92 
93  return 0;
94 }
95 
97 PrivPlugIn::GetType(void) const
98 {
99  return TypedValue::VAR_REAL;
100 }
101 
102 TypedValue
103 PrivPlugIn::GetVal(void) const
104 {
105  return TypedValue(pSE->dGetPrivData(iIndex));
106 }
107 
108 unsigned int
109 PrivPlugIn::ReadLabel(const char* s)
110 {
111  unsigned int rc;
112 
113  /*
114  * deve essere terminato da ';' per essere letto da math parser :(
115  */
116  std::istringstream in(std::string(s) + ";");
117  InputStream In(in);
118  rc = (unsigned int)mp.Get(In);
119 
120  return rc;
121 }
122 
123 void
124 PrivPlugIn::ReadIndex(unsigned int iMaxIndex, const char *s)
125 {
126  bool bRefresh(false);
127  if (strncasecmp(s, "string=", STRLENOF("string=")) == 0) {
128  sIndexName = &s[STRLENOF("string=")];
129  iIndex = pSE->iGetPrivDataIdx(sIndexName.c_str());
130  bRefresh = true;
131 
132  } else if (strncasecmp(s, "name=", STRLENOF("name=")) == 0) {
133  sIndexName = &s[STRLENOF("name=")];
134  iIndex = pSE->iGetPrivDataIdx(sIndexName.c_str());
135  bRefresh = true;
136 
137  silent_cerr("PrivPlugIn: "
138  "\"name=" << sIndexName << "\" is deprecated; "
139  "use \"string=" << sIndexName << "\" instead" << std::endl);
140 
141  } else if (strncasecmp(s, "index=", STRLENOF("index=")) == 0) {
142  s = &s[STRLENOF("index=")];
143  iIndex = ReadLabel(s);
144 
145  } else {
146  silent_cerr("PrivPlugIn: "
147  "index name without qualifier is deprecated; "
148  "use \"string=" << sIndexName << "\" instead" << std::endl);
149 
150  iIndex = pSE->iGetPrivDataIdx(s);
151  if (iIndex > 0) {
152  bRefresh = true;
153  } else {
154  iIndex = ReadLabel(s);
155  }
156  }
157 
158  if (bRefresh) {
159  // refresh, as iGetPrivDataIdx could have changed it
160  iMaxIndex = pSE->iGetNumPrivData();
161  }
162 
163  if (iIndex == 0 || iIndex > iMaxIndex) {
164  silent_cerr("illegal index " << iIndex << " for "
165  << *this << std::endl);
167  }
168 }
169 
171 : PrivPlugIn(mp, pDM)
172 {
173  NO_OP;
174 }
175 
177 {
178  NO_OP;
179 }
180 
181 const char *
183 {
184  return "node";
185 }
186 
187 void
188 NodePrivPlugIn::ReadSE(unsigned int uLabel, const char *ss)
189 {
190  unsigned int i;
191  char *s = 0;
192 
193  /* eat spaces */
194  SAFESTRDUP(s, ss);
195  for (i = 0; s[i]; i++) {
196  if (isspace(s[i])) {
197  memmove(&s[i], &s[i + 1], strlen(&s[i]));
198  }
199  }
200 
201  i = str2nodetype(s);
202 
203  SAFEDELETEARR(s);
204 
205  if (i == Node::LASTNODETYPE) {
206  silent_cerr("unknown node type '" << ss << "'" << std::endl);
208  }
209 
210  pSE = dynamic_cast<SimulationEntity *>(pDM->pFindNode(Node::Type(i), uLabel));
211  if (pSE == 0) {
212  silent_cerr("NodePrivPlugIn: "
213  << psReadNodesNodes[i] << "(" << uLabel << ") "
214  "not defined" << std::endl);
216  }
217 }
218 
219 std::ostream&
220 NodePrivPlugIn::Err(std::ostream& out) const
221 {
222  Node *pNode = dynamic_cast<Node *>(pSE);
223  if (pNode == 0) {
225  }
226 
227  return out << psNodeNames[pNode->GetNodeType()]
228  << "(" << pNode->GetLabel() << ")";
229 }
230 
232 : PrivPlugIn(mp, pDM)
233 {
234  NO_OP;
235 }
236 
238 {
239  NO_OP;
240 }
241 
242 const char *
244 {
245  return "element";
246 }
247 
248 void
249 ElemPrivPlugIn::ReadSE(unsigned int uLabel, const char *ss)
250 {
251  unsigned int i;
252  char *s = 0;
253 
254  /* eat spaces */
255  SAFESTRDUP(s, ss);
256  for (i = 0; s[i]; i++) {
257  if (isspace(s[i])) {
258  memmove(&s[i], &s[i + 1], strlen(&s[i]));
259  }
260  }
261 
262  i = str2elemtype(s);
263 
264  SAFEDELETEARR(s);
265 
266  if (i == Elem::LASTELEMTYPE) {
267  silent_cerr("unknown element type \"" << ss << "\"" << std::endl);
269  }
270 
271  pSE = dynamic_cast<SimulationEntity *>(pDM->pFindElem(Elem::Type(i), uLabel));
272  if (pSE == 0) {
273  silent_cerr("ElemPrivPlugIn: "
274  << psReadElemsElems[i] << "(" << uLabel << ") "
275  "not defined" << std::endl);
277  }
278 }
279 
280 std::ostream&
281 ElemPrivPlugIn::Err(std::ostream& out) const
282 {
283  Elem *pElem = dynamic_cast<Elem *>(pSE);
284  if (pElem == 0) {
286  }
287 
288  return out << psElemNames[pElem->GetElemType()]
289  << "(" << pElem->GetLabel() << ")";
290 }
291 
292 std::ostream&
293 operator << (std::ostream& out, const PrivPlugIn& p)
294 {
295  return p.Err(out);
296 }
297 
300 {
301  MathParser::PlugIn *p = 0;
303  NodePrivPlugIn(mp, (DataManager *)arg));
304  return p;
305 }
306 
309 {
310  MathParser::PlugIn *p = 0;
312  ElemPrivPlugIn(mp, (DataManager *)arg));
313  return p;
314 }
315 
std::ostream & operator<<(std::ostream &out, const PrivPlugIn &p)
Definition: privpgin.cc:293
virtual void ReadSE(unsigned int uLabel, const char *s)
Definition: privpgin.cc:188
virtual void ReadSE(unsigned int uLabel, const char *s)=0
MathParser::PlugIn * node_priv_plugin(MathParser &mp, void *arg)
Definition: privpgin.cc:299
PrivPlugIn(MathParser &mp, DataManager *pDM)
Definition: privpgin.cc:37
Real Get(Real d=0.)
Definition: mathp.cc:4496
virtual std::ostream & Err(std::ostream &out) const
Definition: privpgin.cc:220
Elem::Type str2elemtype(const char *const s)
Definition: elem.cc:159
NodePrivPlugIn(MathParser &mp, DataManager *pDM)
Definition: privpgin.cc:170
const char * psReadNodesNodes[]
Definition: enums.cc:398
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Definition: node.h:67
virtual ~NodePrivPlugIn(void)
Definition: privpgin.cc:176
virtual unsigned int iGetPrivDataIdx(const char *s) const
Definition: simentity.cc:142
void ReadIndex(unsigned int iMaxIndex, const char *s)
Definition: privpgin.cc:124
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
int Read(int argc, char *argv[])
Definition: privpgin.cc:49
unsigned int iIndex
Definition: privpgin.h:43
Node::Type str2nodetype(const char *const s)
Definition: node.cc:94
#define NO_OP
Definition: myassert.h:74
virtual Elem::Type GetElemType(void) const =0
unsigned int ReadLabel(const char *s)
Definition: privpgin.cc:109
const char * sName(void) const
Definition: privpgin.cc:243
virtual ~ElemPrivPlugIn(void)
Definition: privpgin.cc:237
Elem * pFindElem(Elem::Type Typ, unsigned int uElem, unsigned int iDeriv) const
Definition: elman.cc:650
std::string sIndexName
Definition: privpgin.h:44
ElemPrivPlugIn(MathParser &mp, DataManager *pDM)
Definition: privpgin.cc:231
virtual void ReadSE(unsigned int uLabel, const char *s)
Definition: privpgin.cc:249
TypedValue::Type GetType(void) const
Definition: privpgin.cc:97
Type
Definition: node.h:71
#define ASSERT(expression)
Definition: colamd.c:977
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
virtual doublereal dGetPrivData(unsigned int i) const
Definition: simentity.cc:149
virtual unsigned int iGetNumPrivData(void) const
Definition: simentity.cc:136
const char * psNodeNames[]
Definition: enums.cc:372
TypedValue GetVal(void) const
Definition: privpgin.cc:103
#define STRLENOF(s)
Definition: mbdyn.h:166
MathParser::PlugIn * elem_priv_plugin(MathParser &mp, void *arg)
Definition: privpgin.cc:308
Definition: elem.h:75
virtual Node::Type GetNodeType(void) const =0
Type
Definition: elem.h:91
const char * psElemNames[]
Definition: enums.cc:39
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
virtual std::ostream & Err(std::ostream &out) const
Definition: privpgin.cc:281
const char * psReadElemsElems[]
Definition: enums.cc:79
virtual ~PrivPlugIn(void)
Definition: privpgin.cc:43
const char * sName(void) const
Definition: privpgin.cc:182
virtual std::ostream & Err(std::ostream &out) const =0
DataManager * pDM
Definition: privpgin.h:45
unsigned int GetLabel(void) const
Definition: withlab.cc:62
Node * pFindNode(Node::Type Typ, unsigned int uNode) const
Definition: nodeman.cc:179
SimulationEntity * pSE
Definition: privpgin.h:42
MathParser & mp
Definition: mathp.h:224