1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/mbpar.h,v 1.51 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 /* Parser per l'ingresso dati - parte generale */
33 
34 /* Si compone di tre diverse strutture di scansione,
35  * piu' le strutture di memorizzazione di stringhe e variabili.
36  *
37  * La prima struttura e' data dal LowParser, che riconosce gli elementi
38  * della sintassi. E' data da:
39  * <statement_list>::=
40  *   <statement> ; <statement_list>
41  *   epsilon
42  * <statement>::=
43  *   <description>
44  *   <description> : <arg_list>
45  * <arg_list>::=
46  *   <arg>
47  *   <arg> , <arg_list>
48  * <arg>::=
49  *   <word>
50  *   <number>
51  * ecc. ecc.
52  *
53  * La seconda struttura e' data dall'HighParser, che riconosce la sintassi
54  * vera e propria. In alternativa al LowParser, qualora sia atteso un valore
55  * numerico esprimibile mediante un'espressione regolare
56  * (espressione matematica), e' possibile invocare la terza struttura,
57  * il MathParser. Questo analizza espressioni anche complesse e multiple,
58  * se correttamente racchiuse tra parentesi.
59  *
60  * L'HighParser deve necessariamente riconoscere una parola chiave nel campo
61  * <description>, mentre puo' trovare parole qualsiasi nel campo <arg>
62  * qualora sia attesa una stringa.
63  *
64  * Le parole chiave vengono fornite all'HighParser attraverso la KeyTable,
65  * ovvero una lista di parole impaccate (senza spazi). L'uso consigliato e':
66  *
67  *   const char sKeyWords[] = { "keyword0",
68  *                              "keyword1",
69  *                              "...",
70  *                              "keywordN"};
71  *
72  *   enum KeyWords { KEYWORD0 = 0,
73  *                   KEYWORD1,
74  *                   ...,
75  *                   KEYWORDN,
76  *                   LASTKEYWORD};
77  *
78  *   KeyTable K((int)LASTKEYWORD, sKeyWords);
79  *
80  * Il MathParser usa una tabella di simboli, ovvero nomi (dotati di tipo)
81  * a cui e' associato un valore. La tabella e' esterna e quindi puo' essere
82  * conservata ed utilizzata in seguito conservando in memoria i nomi
83  * definiti in precedenza.
84  *
85  * A questo punto si puo' generare la tabella dei simboli:
86  *
87  *   int iSymbolTableInitialSize = 10;
88  *   Table T(iSymbolTableInitialSize);
89  *
90  * Quindi si crea il MathParser:
91  *
92  *   MathParser Math(T);
93  *
94  * Infine si genera l'HighParser:
95  *
96  *   HighParser HP(Math, K, StreamIn);
97  *
98  * dove StreamIn e' l'istream da cui avviene la lettura.
99  */
100 
101 
102 #ifndef MBPAR_H
103 #define MBPAR_H
104 
105 #include <stack>
106 
107 #include "parsinc.h"
108 
109 /* Per reference frame */
110 #include "reffrm.h"
111 /* Riferimento assoluto */
112 extern const ReferenceFrame AbsRefFrame;
113 
114 /* Per nodi idraulici */
115 #include "hfluid.h"
116 
117 /* Aerodynamic data */
118 #include "aerodata.h"
119 
120 #include "constltp.h"
121 #include "ScalarFunctions.h"
122 
123 /* Deals with license and disclaimer output */
124 extern void mbdyn_license(void);
125 extern void mbdyn_warranty(void);
126 
127 /* MBDynParser - begin */
128 
129 class MBDynParser : public IncludeParser {
130 public:
131 	class ErrGeneric : public MBDynErrBase {
132 	public:
ErrGeneric(MBDYN_EXCEPT_ARGS_DECL)133 		ErrGeneric(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
134 	};
135 	class ErrReferenceAlreadyDefined : public MBDynErrBase {
136 	public:
ErrReferenceAlreadyDefined(MBDYN_EXCEPT_ARGS_DECL)137 		ErrReferenceAlreadyDefined(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
138 	};
139 	class ErrReferenceUndefined : public MBDynErrBase {
140 	public:
ErrReferenceUndefined(MBDYN_EXCEPT_ARGS_DECL)141 		ErrReferenceUndefined(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
142 	};
143 
144 public:
145 	enum Frame {
146 		UNKNOWNFRAME = 0,
147 
148 		GLOBAL,
149 		NODE,
150 		LOCAL,
151 		REFERENCE,
152 
153 		OTHER_POSITION,
154 		OTHER_ORIENTATION,
155 		OTHER_NODE,
156 
157 		LASTFRAME
158 	};
159 
160 	/* Struttura e dati per la linked list di reference frames */
161 public:
162 	typedef std::map<unsigned, const ReferenceFrame *> RFType;
163 	const RFType& GetReferenceFrameContainer(void) const;
164 
165 protected:
166 	RFType RF;
167 
168 	Frame GetRef(ReferenceFrame& rf);
169 
170 	bool Reference_int(void);
171 friend struct RefFrameDR;
172 
173 	/* Struttura e dati per la linked list di hydraulic fluids */
174 public:
175 	typedef std::map<unsigned, const HydraulicFluid *> HFType;
176 	const HFType& GetHydraulicFluidContainer(void) const;
177 
178 protected:
179 	HFType HF;
180 
181 	bool HydraulicFluid_int(void);
182 friend struct HFluidDR;
183 
184 	/* Struttura e dati per la linked list di c81 data */
185 public:
186 	typedef std::map<unsigned, C81Data *> ADType;
187 	const ADType& GetC81DataContainer(void) const;
188 
189 protected:
190 	ADType AD;
191 
192 	bool C81Data_int(void);
193 friend struct C81DataDR;
194 
195 	typedef std::map<unsigned, const ConstitutiveLaw1D *> CL1DType;
196 	typedef std::map<unsigned, const ConstitutiveLaw3D *> CL3DType;
197 	typedef std::map<unsigned, const ConstitutiveLaw6D *> CL6DType;
198 	CL1DType CL1D;
199 	CL3DType CL3D;
200 	CL6DType CL6D;
201 
202 	bool ConstitutiveLaw_int(void);
203 friend struct ConstLawDR;
204 
205 	/* Drives */
206 public:
207 	typedef std::map<unsigned, const DriveCaller *> DCType;
208 	const DCType& GetDriveCallerContainer(void) const;
209 
210 protected:
211 	DCType DC;
212 
213 	bool DriveCaller_int(void);
214 friend struct DriveCallerDR;
215 
216 public:
217 	/* Template drives */
218 	typedef std::map<unsigned, const TplDriveCaller<doublereal> *> DC1DType;
219 	typedef std::map<unsigned, const TplDriveCaller<Vec3> *> DC3DType;
220 	typedef std::map<unsigned, const TplDriveCaller<Vec6> *> DC6DType;
221 	typedef std::map<unsigned, const TplDriveCaller<Mat3x3> *> DC3x3DType;
222 	typedef std::map<unsigned, const TplDriveCaller<Mat6x6> *> DC6x6DType;
223 
224 	const DC1DType& GetDriveCaller1DContainer(void) const;
225 	const DC3DType& GetDriveCaller3DContainer(void) const;
226 	const DC6DType& GetDriveCaller6DContainer(void) const;
227 	const DC3x3DType& GetDriveCaller3x3DContainer(void) const;
228 	const DC6x6DType& GetDriveCaller6x6DContainer(void) const;
229 
230 protected:
231 	DC1DType DC1D;
232 	DC3DType DC3D;
233 	DC6DType DC6D;
234 	DC3x3DType DC3x3D;
235 	DC6x6DType DC6x6D;
236 
237 	bool TplDriveCaller_int(void);
238 friend struct TplDriveCallerDR;
239 
240 	/* Scalar functions */
241 	typedef std::map<std::string, const BasicScalarFunction *> SFType;
242 	SFType SF;
243 
244 	bool ScalarFunction_int(void);
245 friend struct SFuncDR;
246 
247 	/* Dynamic modules */
248 	bool moduleInitialized;
249 	bool ModuleLoad_int(void);
250 friend struct ModuleLoadDR;
251 
252 	DataManager *pDM;
253 
254 public:
255 	class Manip {
256 	public:
Manip(void)257 		Manip(void) {};
~Manip(void)258 		virtual ~Manip(void) {};
259 	};
260 
261 private:
262 	std::stack<const Manip *> manip;
263 
264 public:
265 	void PopManip(void);
266 	void PushManip(const Manip *);
267 	const Manip *GetManip(void) const;
268 	bool bEmptyManip(void) const;
269 
270 public:
271 	template <class T>
272 	class TplManip : public Manip {
273 	public:
274 		virtual ~TplManip(void);
275 		virtual T Get(const T& t) const = 0;
276 	};
277 
278 private:
279 	/* not allowed */
280 	MBDynParser(const MBDynParser&);
281 
282 public:
283 	MBDynParser(MathParser& MP, InputStream& streamIn,
284 			const char *initial_file);
285 	~MBDynParser(void);
286 
287 	void SetDataManager(DataManager *pdm);
288 	DataManager *GetDataManager(void) const;
289 
290 	// Vec*, Mat*x* moved here from parser.h
291 	/* vettore Vec3 */
292 	virtual Vec3 GetVec3(void);
293 	/* vettore Vec3 */
294 	virtual Vec3 GetVec3(const Vec3& vDef);
295 	/* matrice R mediante i due vettori */
296 	virtual Mat3x3 GetMatR2vec(void);
297 	/* matrice 3x3 simmetrica */
298 	virtual Mat3x3 GetMat3x3Sym(void);
299 	/* matrice 3x3 arbitraria */
300 	virtual Mat3x3 GetMat3x3(void);
301 	virtual Mat3x3 GetMat3x3(const Mat3x3& mDef);
302 
303 	virtual Vec6 GetVec6(void);
304 	virtual Vec6 GetVec6(const Vec6& vDef);
305 	virtual Mat6x6 GetMat6x6(void);
306 	virtual Mat6x6 GetMat6x6(const Mat6x6& mDef);
307 
308 public:
309 	virtual void GetMat6xN(Mat3xN& m1, Mat3xN& m2, integer iNumCols);
310 
311 	/*
312 	 * Lettura di posizioni, vettori e matrici di rotazione
313 	 * relative ed assolute rispetto ad un riferimento
314 	 */
315 	Vec3 GetPosRel(const ReferenceFrame& rf);
316 	Vec3 GetPosRel(const ReferenceFrame& rf, const ReferenceFrame& other_rf, const Vec3& other_X);
317 	Vec3 GetPosAbs(const ReferenceFrame& rf);
318 	Vec3 GetVelRel(const ReferenceFrame& rf, const Vec3& x);
319 	Vec3 GetVelAbs(const ReferenceFrame& rf, const Vec3& x);
320 	Vec3 GetOmeRel(const ReferenceFrame& rf);
321 	Vec3 GetOmeAbs(const ReferenceFrame& rf);
322 	Vec3 GetVecRel(const ReferenceFrame& rf);
323 	Vec3 GetVecAbs(const ReferenceFrame& rf);
324 	Vec3 GetUnitVecRel(const ReferenceFrame& rf);
325 	Vec3 GetUnitVecAbs(const ReferenceFrame& rf);
326 	Mat3x3 GetMatRel(const ReferenceFrame& rf);
327 	Mat3x3 GetMatAbs(const ReferenceFrame& rf);
328 	Mat3x3 GetRotRel(const ReferenceFrame& rf);
329 	Mat3x3 GetRotRel(const ReferenceFrame& rf, const ReferenceFrame& other_rf, const Mat3x3& other_R);
330 	Mat3x3 GetRotAbs(const ReferenceFrame& rf);
331 
332 	enum VecMatOpType {
333 		VM_NONE		= 0,
334 
335 		VM_POSABS,
336 		VM_VELABS,
337 		VM_OMEABS,
338 		VM_VECABS,
339 		VM_UNITVECABS,
340 
341 		VM_MATABS,
342 		VM_ROTABS,
343 
344 		VM_MOD_REL	= 0x1000U,
345 		VM_MOD_OTHER	= 0x2000U,
346 
347 		VM_POSREL	= VM_POSABS | VM_MOD_REL,
348 		VM_VELREL	= VM_VELABS | VM_MOD_REL,
349 		VM_OMEREL	= VM_OMEABS | VM_MOD_REL,
350 		VM_VECREL	= VM_VECABS | VM_MOD_REL,
351 		VM_UNITVECREL	= VM_UNITVECABS | VM_MOD_REL,
352 
353 		VM_MATREL	= VM_MATABS | VM_MOD_REL,
354 		VM_ROTREL	= VM_ROTABS | VM_MOD_REL,
355 
356 		VM_LAST
357 	};
358 
359 	template <class T>
360 	class RefTplManip : public TplManip<T> {
361 	protected:
362 		MBDynParser& HP;
363 		const ReferenceFrame& rf;
364 		VecMatOpType type;
365 
366 	public:
367 		RefTplManip(MBDynParser& HP, const ReferenceFrame& rf, VecMatOpType type);
368 		virtual ~RefTplManip(void);
369 	};
370 
371 	class RefVec3Manip : public RefTplManip<Vec3> {
372 	public:
373 		RefVec3Manip(MBDynParser& HP, const ReferenceFrame& rf, VecMatOpType type);
374 		virtual ~RefVec3Manip(void);
375 		virtual Vec3 Get(const Vec3& v) const;
376 	};
377 
378 	class VecRelManip : public RefVec3Manip {
379 	public:
380 		VecRelManip(MBDynParser& HP, const ReferenceFrame& rf);
381 		virtual ~VecRelManip(void);
382 	};
383 
384 	class VecAbsManip : public RefVec3Manip {
385 	public:
386 		VecAbsManip(MBDynParser& HP, const ReferenceFrame& rf);
387 		virtual ~VecAbsManip(void);
388 	};
389 
390 	virtual doublereal Get(const doublereal& d);
391 	virtual Vec3 Get(const Vec3& v);
392 	virtual Mat3x3 Get(const Mat3x3& m);
393 	virtual Vec6 Get(const Vec6& v);
394 	virtual Mat6x6 Get(const Mat6x6& m);
395 
396 	void OutputFrames(std::ostream& out) const;
397 
398 	HydraulicFluid* GetHydraulicFluid(void);
399 
400 	const c81_data* GetC81Data(unsigned profile) const;
401 
402 	ConstitutiveLaw1D* GetConstLaw1D(ConstLawType::Type& clt);
403 	ConstitutiveLaw3D* GetConstLaw3D(ConstLawType::Type& clt);
404 	ConstitutiveLaw6D* GetConstLaw6D(ConstLawType::Type& clt);
405 	const DriveCaller* GetDrive(unsigned uLabel) const;
406 
407 	DriveCaller* GetDriveCaller(bool bDeferred = false);
408 	template <class T> const TplDriveCaller<T> *GetTplDrive(unsigned uLabel) const;
409 	template <class T> TplDriveCaller<T> *GetTplDriveCaller(void);
410 
411 	const BasicScalarFunction* GetScalarFunction(void);
412 	const BasicScalarFunction* GetScalarFunction(const std::string &s);
413 	bool SetScalarFunction(const std::string &s, const BasicScalarFunction *sf);
414 };
415 
416 /*
417  * Le funzioni:
418  *   ExpectDescription()
419  *   ExpectArg()
420  * informano il parser di cio' che e' atteso; di default il costruttore
421  * setta ExpectDescription().
422  *
423  * Le funzioni:
424  *   GetDescription()
425  *   IsKeyWord()
426  *   GetWord()
427  * restituiscono un intero che corrisponde alla posizione occupata nella
428  * KeyTable dalle parole corrispondenti, oppure -1 se la parola non e'
429  * trovata. Si noti che IsKeyWord(), in caso di esito negativo, ripristina
430  * l'istream. Tutte preparano poi il parser per la lettura successiva.
431  *
432  * La funzione
433  *   IsKeyWord(const char*)
434  * restituisce 0 se non trova la parola e ripristina l'istream, altrimenti
435  * restituisce 1 e prepara il parser alla lettura successiva.
436  *
437  * Le funzioni
438  *   GetInt(),
439  *   GetReal(),
440  *   GetString(),
441  *   GetStringWithDelims(enum Delims)
442  *   GetFileName(enum Delims)
443  * restituiscono i valori attesi e preparano il parser alla lettura successiva.
444  */
445 
446 /* MBDynParser - end */
447 
448 #endif /* MBPAR_H */
449 
450