1 //#**************************************************************
2 //#
3 //# filename:             mathfunc.h
4 //#
5 //# author:               Gerstmayr Johannes
6 //#
7 //# generated:						October 2006
8 //# description:          general mathematical function
9 //#                       mostly for time or space-curves of arbitrary shape
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 #ifndef MATHFUNC__H
25 #define MATHFUNC__H
26 
27 typedef enum {TMFempty=0, TMFpiecewiseconst=1, TMFpiecewiselinear=2,
28 TMFpiecewisequad=3, TMFuserdefined=4, TMFexpression=5,
29 //the following modes are depreciated! do not use, use TMFexpression instead!!!
30 TMFpolynomial=6, TMFharmonic=7, TMFsin=8, TMFcos=9, TMFstaticDynamicFricion=10} TMathFuncType; //***
31 
32 #include "../Parser/parser.h"
33 //class CParsedFunction;
34 
35 //general function that evaluates for certain parameter x or t
36 class MathFunction
37 {
38 public:
MathFunction()39 	MathFunction() {Init();}
40 
MathFunction(const MathFunction & e)41 	MathFunction(const MathFunction& e)
42 	{
43 		Init();
44 		CopyFrom(e);
45 	}
46 	MathFunction& operator=(const MathFunction& e)
47 	{
48 		if (this == &e) {return *this;}
49 		CopyFrom(e);
50 		return *this;
51 	}
52 	//To be overwritten in derived class:
GetCopy()53 	virtual MathFunction* GetCopy()
54 	{
55 		MathFunction* ec = new MathFunction();
56 		ec->CopyFrom(*this);
57 		return ec;
58 	}
~MathFunction()59 	virtual ~MathFunction() { };
60 
61 	//To be overwritten in derived class:
62 	virtual void CopyFrom(const MathFunction& e);
63 
Init()64 	virtual void Init() {dim = 1; funcmode = TMFempty; vectime.SetLen(0); valX.SetLen(0); valY.SetLen(0); valZ.SetLen(0); coeff.SetLen(0); ufunc=NULL;}
65 
66 	virtual const char* GetTypeName() const;
67 
68 	virtual void GetElementData(ElementDataContainer& edc); 		//fill in all element data
69 	virtual int SetElementData(MBS* mbs, ElementDataContainer& edc); //set element data according to ElementDataContainer
70 
71 	virtual int SetData(int mode, const Matrix& data);
72 
73 	virtual void GetData(int& mode, Matrix& data) const;
74 
75 	virtual int SetData(const mystr& funcname, const Vector& coefficients);
76 
77 	virtual void GetData(mystr& funcname, Vector& coefficients) const;
78 
79 	virtual TMathFuncType StringToMathFuncType(const mystr& funcname);
80 
81 	virtual void SetPiecewise(const Vector& times, const Vector& coeffs, int interp = 0);
82 
83 	virtual void SetPiecewiseQuadratic(const Vector& times, const Vector& coeffs_p, const Vector& coeffs_v);
84 
85 	virtual int SetPiecewiseFromFile(const char* filename, int ncolumns, int column1, int column2, int interp = 0,  int nrOfHeaderLines = 0, double offset1 = 0., double offset2 = 0., int offset1_start_index=1, int offset2_start_index=1);	// read x- and y- data from columns col1 and col2 of file, error if return value = 1, specified number of header lines can be cutted off, additional time or signal offset is considered during reading from file
86 
87 	virtual int SetPiecewiseFromFile2(const char* filename, int column1, int column2, int interp = 0, mystr comment = mystr("%"));	// read x- and y- data from columns col1 and col2 of file, error if return value = 1, number of columns is not needed, comment char for cutting off header available
88 
89 
90 	//f = c1*t^0 + c2*t^1 + c3*t^2 + ....
91 	virtual void SetPolynomial(const Vector& coeffs);
92 	virtual void SetVecTimes(const Vector& times); //for polynomial turn on/off times
93 
94 	virtual void SetConstant(double x);
95 
96 	virtual void SetHarmonic(const Vector& frequency, const Vector& phase, const Vector& amplitude);
97 
GetFuncMode()98 	virtual int GetFuncMode() const {return funcmode;}
GetMaxFuncMode()99 	virtual int GetMaxFuncMode() const {return 5;} //maximum number of usable functions!
100 
Dim()101 	virtual int Dim() const {return dim;}
102 	//return 0 or 1 depending if condition f >= 1 is fulfilled:
CheckCondition(double t)103 	virtual int CheckCondition(double t) const {return (Evaluate(t) >= 1); }
104 	//evaluate given function
105 	virtual double Evaluate(double t, int diff=0) const;
106 
107 	//virtual void SetSinFunction(double amplitude, double freq_rad, double phase);
108 	//virtual void SetCosFunction(double amplitude, double freq_rad, double phase); //***
109 	//virtual void SetStaticDynamicFricionFunction(double staticFriction, double viscousFriction, double zeroZone = 0); //***
110   virtual void SetUserDefined(double (*func)(double,const Vector&),const Vector& coeffs = Vector(0)); // sets a userdefined function, additional parameters cam be passed in Vector coeffs
111   virtual void SetExpression(mystr expression, mystr variable, MBS * mbs); // sets a userdefined expression - uses parseobect for evaluation
112 
GetCoeffVector()113 	virtual const Vector& GetCoeffVector() const {return coeff;}
GetCoeffVector()114 	virtual Vector& GetCoeffVector() {return coeff;}
115 
GetTimeVector()116 	virtual const Vector& GetTimeVector() const {return vectime;}
GetTimeVector()117 	virtual Vector& GetTimeVector() {return vectime;}
118 
GetXVector()119 	virtual const Vector& GetXVector() const {return valX;}
GetXVector()120 	virtual Vector& GetXVector() {return valX;}
121 
122 	//$ YV 2012-12-10
123 	/*
124 	virtual const CParsedFunction& PF() const {return pf;}
125 	virtual CParsedFunction& PF(){return pf;}
126 	*/
PF()127 	virtual CParsedFunction* PF() {return &pf;} //$JG2013-04-29
128 
129 	virtual int FindIndexPiecewise(double t) const;  //$ MS 2011-5-23
130 	virtual double InterpolatePiecewise(double t, int index) const; //$ MS 2011-5-23:
131 
132 private:
133 	int funcmode;					//see Evaluate function
134 	int dim;							//dimension of return value
135 	Vector vectime;				//vector of time points
136 	Vector valX;					//value X for time point vectime
137 	Vector valY;					//value Y for time point vectime
138 	Vector valZ;					//value Z for time point vectime
139 
140 	Vector coeff;					//coefficients for mathematical operations - (AD) additional parameters for userdefined function
141 	double (*ufunc)(double,const Vector&);			// pointer to a User-defined function double = f(double,Vector), eg. f( time, other_coefficients)
142 
143 	CParsedFunction pf; //$JG2013-4-29: for acceleration of parsed function!!!
144 
145 	//$JG2013-4-29: shoud be erased soon:
146 	//++++++++++++++++
147 	//$ YV 2012-12-12: we store definition of a parsed function instead of CParsedFunction itself
148 	mystr parsedFunctionExpression;
149 	mystr parsedFunctionVariables;
150 	//++++++++++++++++
151 	MBSParserInterface * mbsPI; //$JG2013-4-29: used to communicate with MBSParser (for translation)
152 };
153 
154 // =====================================================================================================
155 // PieceWiseMathFunction
156 // multiple serial MathFunctions define one (discontinous) function
157 //
158 //$ DR 2012-06: new class PieceWiseMathFunction added
159 class PieceWiseMathFunction
160 {
161 public:
PieceWiseMathFunction()162 		PieceWiseMathFunction() {Init();}
163 
PieceWiseMathFunction(const PieceWiseMathFunction & e)164 	PieceWiseMathFunction(const PieceWiseMathFunction& e)
165 	{
166 		Init();
167 		CopyFrom(e);
168 	}
169 	PieceWiseMathFunction& operator=(const PieceWiseMathFunction& e)
170 	{
171 		if (this == &e) {return *this;}
172 		CopyFrom(e);
173 		return *this;
174 	}
175 	//To be overwritten in derived class:
GetCopy()176 	virtual PieceWiseMathFunction* GetCopy()
177 	{
178 		PieceWiseMathFunction* ec = new PieceWiseMathFunction();
179 		ec->CopyFrom(*this);
180 		return ec;
181 	}
~PieceWiseMathFunction()182 	virtual ~PieceWiseMathFunction() { };
183 
184 	//To be overwritten in derived class:
185 	virtual void CopyFrom(const PieceWiseMathFunction& e);
186 
Init()187 	virtual void Init() {};
188 
189 	virtual void AddMathFunction(MathFunction* mathFpointer, double start_value);
190 	virtual double Evaluate(double x, int left_limit=0) const;
191 
192 	virtual int GetIndexOfMathFunctionPointer(double x) const;
193 
194 private:
195 	TArray<double> start_values;				//MathFunction i is valid for points with x >= start_value(i)
196 	TArray<MathFunction*> MathFuncPointArray;
197 };
198 
199 
200 #endif