MBSim

Project Page
function_library.h
1 /* Copyright (C) 2004-2009 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: markus.ms.schneider@gmail.com
18  */
19 
20 #ifndef _FUNCTION_LIBRARY_H_
21 #define _FUNCTION_LIBRARY_H_
22 
23 #include "mbsim/utils/function.h"
24 
25 namespace MBSim {
26 
27  class Function1_SS_from_VS : public Function1<double, double> {
28  public:
29  Function1_SS_from_VS() : fun(NULL) {}
30  Function1_SS_from_VS(Function1<fmatvec::Vec, double> * fun_) : fun(fun_) {assert((*fun)(0).size()==1); }
31  void setFunction(Function1<fmatvec::Vec, double> * fun_) {fun=fun_; assert((*fun)(0).size()==1); }
32  double operator()(const double& x, const void * =NULL) {return (*fun)(x)(0); }
33  void initializeUsingXML(TiXmlElement *element);
34  private:
36  };
37 
38  class Function1_VS_from_SS : public Function1<fmatvec::Vec, double> {
39  public:
40  Function1_VS_from_SS() : fun(NULL), vec(0) {}
41  Function1_VS_from_SS(Function1<double, double> * fun_, fmatvec::Vec v) : fun(fun_), vec(v) {vec/=nrm2(v); }
42  void setFunction(Function1<double, double> * fun_) {fun=fun_; }
43  void setVector(fmatvec::Vec v) {vec=v; vec/=nrm2(v); }
44  fmatvec::Vec operator()(const double& x, const void * =NULL) {return (*fun)(x)*vec; }
45  void initializeUsingXML(TiXmlElement *element);
46  private:
48  fmatvec::Vec vec;
49  };
50 
51 
58  class QuadraticFunction1_VS : public DifferentiableFunction1<fmatvec::Vec> {
59  public:
61  QuadraticFunction1_VS(fmatvec::Vec a0_, fmatvec::Vec a1_, fmatvec::Vec a2_);
62  void initializeUsingXML(TiXmlElement *element);
63 
64  class ZerothDerivative : public Function1<fmatvec::Vec,double> {
65  public:
67  fmatvec::Vec operator()(const double& x, const void * =NULL);
68  private:
69  QuadraticFunction1_VS *parent;
70  };
71 
72  class FirstDerivative : public Function1<fmatvec::Vec,double> {
73  public:
75  fmatvec::Vec operator()(const double& x, const void * =NULL);
76  private:
77  QuadraticFunction1_VS *parent;
78  };
79 
80  class SecondDerivative : public Function1<fmatvec::Vec,double> {
81  public:
83  fmatvec::Vec operator()(const double& x, const void * =NULL);
84  private:
85  QuadraticFunction1_VS *parent;
86  };
87  protected:
88  int ySize;
89  fmatvec::Vec a0, a1, a2;
90  private:
91  };
92 
93 
100  class SinusFunction1_VS : public DifferentiableFunction1<fmatvec::Vec> {
101  public:
103  SinusFunction1_VS(fmatvec::Vec amplitude_, fmatvec::Vec frequency_, fmatvec::Vec phase_, fmatvec::Vec offset_);
104  void initializeUsingXML(TiXmlElement *element);
105 
106  class ZerothDerivative : public Function1<fmatvec::Vec,double> {
107  public:
109  fmatvec::Vec operator()(const double& x, const void * =NULL);
110  private:
111  SinusFunction1_VS *parent;
112  };
113 
114  class FirstDerivative : public Function1<fmatvec::Vec,double> {
115  public:
117  fmatvec::Vec operator()(const double& x, const void * =NULL);
118  private:
119  SinusFunction1_VS *parent;
120  };
121 
122  class SecondDerivative : public Function1<fmatvec::Vec,double> {
123  public:
125  fmatvec::Vec operator()(const double& x, const void * =NULL);
126  private:
127  SinusFunction1_VS *parent;
128  };
129  protected:
130  int ySize;
131  fmatvec::Vec amplitude, frequency, phase, offset;
132  private:
133  void check();
134  };
135 
136 
138  public:
140  PositiveSinusFunction1_VS(fmatvec::Vec amplitude, fmatvec::Vec frequency, fmatvec::Vec phase, fmatvec::Vec offset) : SinusFunction1_VS(amplitude, frequency, phase, offset) {}
141  fmatvec::Vec operator()(const double& tVal, const void * =NULL);
142  void initializeUsingXML(TiXmlElement *element) {
144  }
145  };
146 
147 
148  class StepFunction1_VS : public Function1<fmatvec::Vec, double> {
149  public:
150  StepFunction1_VS() {}
151  StepFunction1_VS(fmatvec::Vec stepTime_, fmatvec::Vec stepSize_) : stepTime(stepTime_), stepSize(stepSize_) {
152  check();
153  }
154  fmatvec::Vec operator()(const double& tVal, const void * =NULL);
155  void initializeUsingXML(TiXmlElement *element);
156  private:
157  fmatvec::Vec stepTime, stepSize;
158  int ySize;
159  void check();
160  };
161 
162 
163  class TabularFunction1_VS : public Function1<fmatvec::Vec, double> {
164  public:
165  TabularFunction1_VS() : xIndexOld(0) {}
166  TabularFunction1_VS(fmatvec::Vec x_, fmatvec::Mat y_) : x(x_), y(y_), xIndexOld(0) {
167  check();
168  }
169  fmatvec::Vec operator()(const double& xVal, const void * =NULL);
170  void initializeUsingXML(TiXmlElement *element);
171  protected:
172  fmatvec::Vec x;
173  fmatvec::Mat y;
174  private:
175  int xIndexOld, xSize;
176  void check();
177  };
178 
179 
181  public:
183  PeriodicTabularFunction1_VS(fmatvec::Vec x_, fmatvec::Mat y_) : TabularFunction1_VS(x_, y_) {
184  check();
185  }
186  fmatvec::Vec operator()(const double& xVal, const void * =NULL);
187  void initializeUsingXML(TiXmlElement *element) {
189  check();
190  }
191  private:
192  double xMin, xMax, xDelta;
193  void check() {
194  xMin=x(0);
195  xMax=x(x.size()-1);
196  xDelta=xMax-xMin;
197  }
198  };
199 
200 
201  class SummationFunction1_VS : public Function1<fmatvec::Vec, double> {
202  public:
203  SummationFunction1_VS() : ySize(0) {};
204  void addFunction(Function1<fmatvec::Vec, double> * function, double factor=1.) {
205  if (!ySize)
206  ySize=((*function)(0)).size();
207  else
208  assert (((*function)(0)).size()==ySize);
209  functions.push_back(function);
210  factors.push_back(factor);
211  }
212  fmatvec::Vec operator()(const double& tVal, const void * =NULL) {
213  fmatvec::Vec y=factors[0]*(*(functions[0]))(tVal);
214  for (unsigned int i=1; i<functions.size(); i++)
215  y+=factors[i]*(*(functions[i]))(tVal);
216  return y;
217  }
218  void initializeUsingXML(TiXmlElement *element);
219  private:
220  std::vector<Function1<fmatvec::Vec, double> *> functions;
221  std::vector<double> factors;
222  int ySize;
223  };
224 
225 
226  class TabularFunction2_SSS: public MBSim::Function2<double,double,double> {
227  public:
229  /* INHERITED INTERFACE OF FUNCTION2 */
230  virtual void initializeUsingXML(TiXmlElement *element);
231  virtual double operator()(const double& x, const double& y, const void * = NULL);
232  /***************************************************/
233  /* GETTER / SETTER */
234  void setXValues(fmatvec::Vec x_);
235  void setYValues(fmatvec::Vec y_);
236  void setXYMat(fmatvec::Mat XY_);
237  /***************************************************/
238 
239  private:
240  fmatvec::Vec xVec;
241  fmatvec::Vec yVec;
242  fmatvec::Mat XY;
243 
244  int xSize;
245  int ySize;
246  int x0Index,x1Index;
247  int y0Index,y1Index;
248 
249  fmatvec::Vec func_value;
250  fmatvec::Vec xy;
251  fmatvec::Vec XYval;
252  fmatvec::Mat XYfac;
253 
254  void calcIndex(const double * x, fmatvec::Vec X, int * xSize, int * xIndexMinus, int * xIndexPlus);
255  };
256 
257 
259  public:
260  Polynom1_SS() {}
261  void initializeUsingXML(TiXmlElement *element);
262 
263  class Polynom1_SSEvaluation : public MBSim::Function1<double,double> {
264  public:
265  Polynom1_SSEvaluation(fmatvec::Vec a_) : MBSim::Function1<double,double>(), a(a_) {}
266  double operator()(const double& x, const void * =NULL);
267  private:
268  fmatvec::Vec a;
269  };
270 
271  void setCoefficients(fmatvec::Vec a);
272  };
273 
274 }
275 
276 #endif /* _FUNCTION_LIBRARY_H_ */
277