1 //#***************************************************************************************
2 //# filename:     elementDataAccess.cpp
3 //#
4 //# author:				Johannes Gerstmayr, Yuri Vetyukov
5 //#
6 //# generated:
7 //# description:
8 //#
9 //# comments:
10 //#
11 //# Copyright (c) 2003-2013 Johannes Gerstmayr, Linz Center of Mechatronics GmbH, Austrian
12 //# Center of Competence in Mechatronics GmbH, Institute of Technical Mechanics at the
13 //# Johannes Kepler Universitaet Linz, Austria. All rights reserved.
14 //#
15 //# This file is part of HotInt.
16 //# HotInt is free software: you can redistribute it and/or modify it under the terms of
17 //# the HOTINT license. See folder 'licenses' for more details.
18 //#
19 //# bug reports are welcome!!!
20 //# WWW:		www.hotint.org
21 //# email:	bug_reports@hotint.org or support@hotint.org
22 //#***************************************************************************************
23 
24 //#include "stdafx.h"
25 #include "mbs_interface.h"
26 #include "elementData.h"
27 #include "mathfunc.h"
28 #include "elementDataAccess.h"
29 
WarnElementData(MBS * mbs,const ElementDataContainer & edc,const char * name)30 void WarnElementData(MBS* mbs, const ElementDataContainer& edc, const char* name)
31 {
32 	if (mbs == 0) return;
33 
34 	int en = 0;
35 	int pos = edc.Find("Element_number");
36 	if (pos)
37 	{
38 		const ElementData& ed = edc.Get(pos);
39 		if (ed.IsInt())
40 		{
41 			en = ed.GetInt();
42 		}
43 	}
44 	mbs->UO() << "Error in element";
45 	if (en) mbs->UO() << " " << en;
46 	mbs->UO() << ": Data entry '" << name << "' not found!\n";
47 }
48 
49 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52 
53 //the following functions are used for simplified EDC access, especially for access by Set/GetElementData functions of Elements
54 //flag&1 --> needed value
55 //$JG2012-01-19: because element data EDCs are now hierarchical, these functions have been changed from Find(...) to TreeFind(...) needed to change,
56 //                because from now on this function searches in the whole tree, not only in the actual list
57 //                WARNING: note that the integer return value now is changed from the local position in the EDC to 0/1 only!!!
GetElemDataBool(MBS * mbs,const ElementDataContainer & edc,const char * name,int & v,int flag)58 int GetElemDataBool(MBS* mbs, const ElementDataContainer& edc, const char* name, int& v, int flag)
59 {
60 	//old: int pos = edc.Find(name); if (pos) ...
61 
62 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; because element data EDCs are now hierarchical, these functions have been changed from Find(...) to TreeFind(...) needed to change, because from now on this function searches in the whole tree, not only in the actual list
63 	int found = (ed != 0);
64 	if (ed)
65 	{
66 		if (ed->IsBool())
67 		{
68 			v = ed->GetBool();
69 		}
70 	}
71 	else if (flag&1)
72 		WarnElementData(mbs, edc, name);
73 
74 	return found; //old: return pos
75 }
76 
GetElemDataInt(MBS * mbs,const ElementDataContainer & edc,const char * name,int & v,int flag)77 int GetElemDataInt(MBS* mbs, const ElementDataContainer& edc, const char* name, int& v, int flag)
78 {
79 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
80 	int found = (ed != 0);
81 	if (ed)
82 	{
83 		if (ed->IsInt())
84 		{
85 			v = ed->GetInt();
86 		}
87 	}
88 	else if (flag&1)
89 		WarnElementData(mbs, edc, name);
90 
91 	return found;
92 }
93 
GetElemDataDouble(MBS * mbs,const ElementDataContainer & edc,const char * name,double & v,int flag)94 int GetElemDataDouble(MBS* mbs, const ElementDataContainer& edc, const char* name, double& v, int flag)
95 {
96 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
97 	int found = (ed != 0);
98 	if (ed)
99 	{
100 		if (ed->IsDouble())
101 		{
102 			v = ed->GetDouble();
103 		}
104 	}
105 	else if (flag&1)
106 		WarnElementData(mbs, edc, name);
107 
108 	return found;
109 }
110 
GetElemDataText(MBS * mbs,const ElementDataContainer & edc,const char * name,mystr & str,int flag)111 int GetElemDataText(MBS* mbs, const ElementDataContainer& edc, const char* name, mystr& str, int flag)
112 {
113 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
114 	int found = (ed != 0);
115 	if (ed)
116 	{
117 		if (ed->IsText())
118 		{
119 			str = ed->GetText();
120 		}
121 	}
122 	else if (flag&1)
123 		WarnElementData(mbs, edc, name);
124 
125 	return found;
126 }
127 
GetElemDataVector2D(MBS * mbs,const ElementDataContainer & edc,const char * name,Vector2D & v,int flag)128 int GetElemDataVector2D(MBS* mbs, const ElementDataContainer& edc, const char* name, Vector2D& v, int flag)
129 {
130 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
131 	int found = (ed != 0);
132 	if (ed)
133 	{
134 		if (ed->IsVector() && ed->GetVectorLen() == 2)
135 		{
136 			ed->GetVector(v.X(), v.Y());
137 		}
138 	}
139 	else if (flag&1)
140 		WarnElementData(mbs, edc, name);
141 
142 	return found;
143 }
144 
GetElemDataVector3D(MBS * mbs,const ElementDataContainer & edc,const char * name,Vector3D & v,int flag)145 int GetElemDataVector3D(MBS* mbs, const ElementDataContainer& edc, const char* name, Vector3D& v, int flag)
146 {
147 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
148 	int found = (ed != 0);
149 	if (ed)
150 	{
151 		if (ed->IsVector() && ed->GetVectorLen() == 3)
152 		{
153 			ed->GetVector(v.X(), v.Y(), v.Z());
154 		}
155 	}
156 	else if (flag&1)
157 		WarnElementData(mbs, edc, name);
158 
159 	return found;
160 }
161 
GetElemDataVector2D(MBS * mbs,const ElementDataContainer & edc,const char * name,double & v1,double & v2,int flag)162 int GetElemDataVector2D(MBS* mbs, const ElementDataContainer& edc, const char* name, double& v1, double& v2, int flag)
163 {
164 	Vector2D v(v1, v2);
165 	int found = GetElemDataVector2D(mbs, edc, name, v, flag);
166 
167 	v1 = v.X();
168 	v2 = v.Y();
169 
170 	return found;
171 }
172 
GetElemDataVector3D(MBS * mbs,const ElementDataContainer & edc,const char * name,double & v1,double & v2,double & v3,int flag)173 int GetElemDataVector3D(MBS* mbs, const ElementDataContainer& edc, const char* name, double& v1, double& v2, double& v3, int flag)
174 {
175 	Vector3D v(v1, v2, v3);
176 	int found = GetElemDataVector3D(mbs, edc, name, v, flag);
177 
178 	v1 = v.X();
179 	v2 = v.Y();
180 	v3 = v.Z();
181 
182 	return found;
183 }
184 
GetElemDataVector(MBS * mbs,const ElementDataContainer & edc,const char * name,Vector & v,int flag)185 int GetElemDataVector(MBS* mbs, const ElementDataContainer& edc, const char* name, Vector& v, int flag)
186 {
187 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
188 	int found = (ed != 0);
189 	if (ed)
190 	{
191 		if (ed->IsVector())
192 		{
193 			v.SetLen(ed->GetVectorLen());
194 			for (int i=1; i <= ed->GetVectorLen(); i++)
195 			{
196 				v(i) = ed->GetVectorVal(i);
197 			}
198 		}
199 	}
200 	else if (flag&1)
201 		WarnElementData(mbs, edc, name);
202 
203 	return found;
204 }
205 
GetElemDataIVector(MBS * mbs,const ElementDataContainer & edc,const char * name,IVector & v,int flag)206 int GetElemDataIVector(MBS* mbs, const ElementDataContainer& edc, const char* name, IVector& v, int flag)
207 {
208 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
209 	int found = (ed != 0);
210 	if (ed)
211 	{
212 		if (ed->IsVector())
213 		{
214 			v.SetLen(ed->GetVectorLen());
215 			for (int i=1; i <= ed->GetVectorLen(); i++)
216 			{
217 				v(i) = (int)ed->GetVectorVal(i);
218 			}
219 		}
220 	}
221 	else if (flag&1)
222 		WarnElementData(mbs, edc, name);
223 
224 	return found;
225 }
226 
GetElemDataMatrix(MBS * mbs,const ElementDataContainer & edc,const char * name,Matrix & v,int flag)227 int GetElemDataMatrix(MBS* mbs, const ElementDataContainer& edc, const char* name, Matrix& v, int flag)
228 {
229 	const ElementData* ed = edc.TreeFind(name); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
230 	int found = (ed != 0);
231 	if (ed)
232 	{
233 		if (ed->IsMatrix())
234 		{
235 			v.SetSize(ed->GetMatrixRows(), ed->GetMatrixCols());
236 
237 			for (int i=1; i <= ed->GetMatrixRows(); i++)
238 			{
239 				for (int j=1; j <= ed->GetMatrixCols(); j++)
240 				{
241 					v(i,j) = ed->GetMatrixVal(i,j);
242 				}
243 			}
244 		}
245 	}
246 	else if (flag&1)
247 		WarnElementData(mbs, edc, name);
248 
249 	return found;
250 }
251 
GetElemDataMathFunc(MBS * mbs,const ElementDataContainer & edc,const mystr & funcname,MathFunction & mathfunc,int flag)252 int GetElemDataMathFunc(MBS* mbs, const ElementDataContainer& edc, const mystr& funcname, MathFunction& mathfunc, int flag) //depreciated!!!!, do not use anymore
253 
254 {
255 	int rv;
256 
257 	//mathfunc
258 	int mode;
259 	Matrix data;
260 
261 	const ElementData* ed = edc.TreeFind(mystr(funcname+mystr("_type")).c_str()); //$JG2012-01-19; needed to change, because from now on this function searches in the whole tree, not only in the actual list
262 	int found = (ed != 0);
263 	//int pos = edc.Find(mystr(funcname+mystr("_type")).c_str());
264 	if (found)
265 	{
266 		mystr fntype = funcname+mystr("_type");
267 		mystr fndata = funcname+mystr("_data");
268 
269 		GetElemDataInt(mbs, edc, fntype.c_str(), mode, 1);
270 		GetElemDataMatrix(mbs, edc, fndata.c_str(), data, 1);
271 
272 		rv = mathfunc.SetData(mode, data); //set data only accepts valid data!
273 	}
274 	else
275 	{
276 		mystr funcstr;
277 		Vector coeffs;
278 
279 		GetElemDataText(mbs, edc, "Math_function_name", funcstr, 1);
280 		GetElemDataVector(mbs, edc, "Mathfunc_coefficients", coeffs, 1);
281 
282 		rv = mathfunc.SetData(funcstr, coeffs);
283 	}
284 
285 
286 
287 
288 
289 	return rv;
290 }
291 
292 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
293 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
294 //ElementData access
295 
SetElemDataVector2D(ElementData & ed,const Vector2D & v,const char * name)296 void SetElemDataVector2D(ElementData& ed, const Vector2D& v, const char* name)
297 {
298 	ed.SetVector2D(v.X(), v.Y(), name);
299 }
300 
SetElemDataVector3D(ElementData & ed,const Vector3D & v,const char * name)301 void SetElemDataVector3D(ElementData& ed, const Vector3D& v, const char* name)
302 {
303 	ed.SetVector3D(v.X(), v.Y(), v.Z(), name);
304 }
305 
SetElemDataIVector(ElementData & ed,const IVector & v,const char * name)306 void SetElemDataIVector(ElementData& ed, const IVector& v, const char* name)
307 {
308 	Vector vv(v.Length());
309 	for (int i=1; i <= v.Length(); i++) vv(i) = v(i);
310 
311 	ed.SetVector(vv.GetVecPtr(), vv.Length(), name);
312 	ed.SetValuesInt();
313 }
314 
SetElemDataVector(ElementData & ed,const Vector & v,const char * name)315 void SetElemDataVector(ElementData& ed, const Vector& v, const char* name)
316 {
317 	ed.SetVector(v.GetVecPtr(), v.Length(), name);
318 }
319 
SetElemDataMatrix(ElementData & ed,const Matrix & v,const char * name)320 void SetElemDataMatrix(ElementData& ed, const Matrix& v, const char* name)
321 {
322 	ed.SetMatrix(v.GetMatPtr(), v.Getrows(), v.Getcols(), name);
323 }
324 
325 
326 //ElementDataContainer access
327 
SetElemDataVector2D(ElementDataContainer & edc,const Vector2D & v,const char * name,const char * tooltiptext)328 void SetElemDataVector2D(ElementDataContainer& edc, const Vector2D& v, const char* name, const char* tooltiptext)
329 {
330 	ElementData ed;
331 	ed.SetVector2D(v.X(), v.Y(), name);
332 	ed.SetToolTipText(tooltiptext);
333 	edc.Add(ed);
334 }
335 
SetElemDataVector3D(ElementDataContainer & edc,const Vector3D & v,const char * name,const char * tooltiptext)336 void SetElemDataVector3D(ElementDataContainer& edc, const Vector3D& v, const char* name, const char* tooltiptext)
337 {
338 	ElementData ed;
339 	ed.SetVector3D(v.X(), v.Y(), v.Z(), name);
340 	ed.SetToolTipText(tooltiptext);
341 	edc.Add(ed);
342 }
343 
SetElemDataIVector(ElementDataContainer & edc,const IVector & v,const char * name,const char * tooltiptext)344 void SetElemDataIVector(ElementDataContainer& edc, const IVector& v, const char* name, const char* tooltiptext)
345 {
346 	ElementData ed;
347 	Vector vv(v.Length());
348 	for (int i=1; i <= v.Length(); i++) vv(i) = v(i);
349 
350 	ed.SetVector(vv.GetVecPtr(), vv.Length(), name);
351 	ed.SetValuesInt();
352 	ed.SetToolTipText(tooltiptext);
353 	edc.Add(ed);
354 }
355 
SetElemDataVector(ElementDataContainer & edc,const Vector & v,const char * name,const char * tooltiptext)356 void SetElemDataVector(ElementDataContainer& edc, const Vector& v, const char* name, const char* tooltiptext)
357 {
358 	ElementData ed;
359 
360 	ed.SetVector(v.GetVecPtr(), v.Length(), name);
361 	ed.SetToolTipText(tooltiptext);
362 	edc.Add(ed);
363 }
364 
SetElemDataMatrix(ElementDataContainer & edc,const Matrix & v,const char * name,const char * tooltiptext)365 void SetElemDataMatrix(ElementDataContainer& edc, const Matrix& v, const char* name, const char* tooltiptext)
366 {
367 	ElementData ed;
368 
369 	ed.SetMatrix(v.GetMatPtr(), v.Getrows(), v.Getcols(), name);
370 	ed.SetToolTipText(tooltiptext);
371 	edc.Add(ed);
372 }
373 
SetElemDataMathFunc(ElementDataContainer & edc,MathFunction & mathfunc,const mystr & funcname)374 void SetElemDataMathFunc(ElementDataContainer& edc, MathFunction& mathfunc, const mystr& funcname) //depreciated!!!!, do not use anymore
375 {
376 	ElementData ed;
377 
378 	//mathfunc
379 	int mode;
380 	Matrix data;
381 	mathfunc.GetData(mode, data);
382 
383 	if (mathfunc.GetFuncMode() <= mathfunc.GetMaxFuncMode())
384 	{
385 		mystr fntype = funcname+mystr("_type");
386 		mystr fndata = funcname+mystr("_data");
387 		ed.SetInt(mode, fntype.c_str(), 0, mathfunc.GetMaxFuncMode()); ed.SetToolTipText("0=no func., 1=polynomial, 2/3/4=piecewise const./linear/quad., 5=harmonic"); edc.Add(ed);
388 		SetElemDataMatrix(edc, data, fndata.c_str());
389 		edc.Last().SetVariableLength();
390 		edc.Last().SetToolTipText("columns: 1=coeff | 2/3=time,value | 4=time,pos,vel | 5=freq.,phase,amplitude");
391 	}
392 	else
393 	{
394 		mystr funcstr;
395 		Vector coeffs;
396 
397 		switch (mathfunc.GetFuncMode())
398 		{
399 		case TMFsin: //***
400 			{
401 				mathfunc.GetData(funcstr, coeffs);
402 				ed.SetText("Math_function_name", funcstr); ed.SetLocked(1); edc.Add(ed);
403 				SetElemDataVector(edc, coeffs, "Mathfunc_coefficients"); edc.Last().SetVariableLength(); edc.Last().SetToolTipText("coefficients = [A omega phi], y = A*Sin(omega*x + phi)");
404 
405 				break;
406 			}
407 		case TMFcos: //***
408 			{
409 				mathfunc.GetData(funcstr, coeffs);
410 				ed.SetText("Math_function_name", funcstr); ed.SetLocked(1); edc.Add(ed);
411 				SetElemDataVector(edc, coeffs, "Mathfunc_coefficients"); edc.Last().SetVariableLength(); edc.Last().SetToolTipText("coefficients = [A omega phi], y = A*Cos(omega*x + phi)");
412 
413 				break;
414 			}
415 		case TMFstaticDynamicFricion: //***
416 			{
417 				assert(0 && "ERROR in mbs_communication.cpp: SetStaticDynamicFricionFunction not anymore supported by MathFunction, use TMFExpression instead!");
418 
419 				mathfunc.GetData(funcstr, coeffs);
420 				ed.SetText("Math_function_name", funcstr); ed.SetLocked(1); edc.Add(ed);
421 				SetElemDataVector(edc, coeffs, "Mathfunc_coefficients"); edc.Last().SetVariableLength(); edc.Last().SetToolTipText("coefficients = [staticFrictionCoeff dynamicFrictionCoeff zeroZone]");
422 
423 				break;
424 			}
425 		default: ;
426 		}
427 	}
428 	//+++++++++++++++++++++++++++
429 }
430 
EDCTreeGetVector3D(ElementDataContainer & edc,const char * name,Vector3D default_val)431 Vector3D EDCTreeGetVector3D(ElementDataContainer& edc, const char* name, Vector3D default_val)
432 {
433 	double vx = 0;double vy = 0;double vz = 0;
434 	if (edc.TreeGetVector3D(name, vx, vy, vz))
435 	{
436 		return Vector3D(vx,vy,vz);
437 	}
438 	return default_val;
439 }
440 
EDCTreeGetVector(ElementDataContainer & edc,const char * name,Vector default_val)441 Vector EDCTreeGetVector(ElementDataContainer& edc, const char* name, Vector default_val)
442 {
443 	double* v = 0;
444 	int len;
445 	if (edc.TreeGetVector(name, &v, len))
446 	{
447 		Vector ta(len);
448 		for(int i=1;i<=len; i++)
449 		{
450 			ta(i) = v[i-1];
451 		}
452 		return ta;
453 	}
454 	return default_val;
455 }
456 
GetRotUnitStr(int type)457 mystr GetRotUnitStr(int type) //0=rad, 1=degree
458 {
459 	if (type == 0)
460 	{
461 		return mystr("(rad)");
462 	}
463 	return mystr("(�)");
464 }