MBSim

Project Page
function.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: martin.o.foerg@googlemail.com
18  */
19 
20 #ifndef FUNCTION_H_
21 #define FUNCTION_H_
22 
23 #include <mbsimtinyxml/tinyxml-src/tinyxml.h>
24 #include <mbsim/element.h>
25 #include <mbsim/utils/eps.h>
26 
27 #include <fmatvec.h>
28 
29 namespace MBSim {
30 
36  template<class Ret, class Arg>
37  class Function1 {
38  public:
42  virtual ~Function1() {}
43 
44  /* INTERFACE FOR DERIVED CLASSES */
50  virtual Ret operator()(const Arg& x, const void * =NULL)=0;
51 
56  virtual void initializeUsingXML(TiXmlElement *element) {}
57  /***************************************************/
58  };
59 
65  template<class Ret, class Arg1, class Arg2>
66  class Function2 {
67  public:
71  virtual ~Function2() {}
72 
73  /* INTERFACE FOR DERIVED CLASSES */
79  virtual Ret operator()(const Arg1& p1, const Arg2& p2, const void * =NULL)=0;
80 
85  virtual void initializeUsingXML(TiXmlElement *element) {}
86  /***************************************************/
87  };
88 
94  template<class Ret, class Arg1, class Arg2, class Arg3>
95  class Function3 {
96  public:
100  virtual ~Function3() {}
101 
102  /* INTERFACE FOR DERIVED CLASSES */
108  virtual Ret operator()(const Arg1& p1, const Arg2& p2, const Arg3& p3, const void * =NULL)=0;
109 
114  virtual void initializeUsingXML(TiXmlElement *element) {}
115  /***************************************************/
116  };
117 
124  template<class Ret>
125  class DifferentiableFunction1 : public Function1<Ret,double> {
126  public:
130  DifferentiableFunction1() : Function1<Ret,double>(), order(0) {}
131 
135  virtual ~DifferentiableFunction1() { delete derivatives[0]; derivatives.erase(derivatives.begin()); }
136 
137  /* INHERITED INTERFACE OF FUNCTION1 */
138  virtual Ret operator()(const double& x, const void * =NULL) {
139  assert(derivatives.size()>0); return (getDerivative(order))(x); }
140  /***************************************************/
141 
146  const Function1<Ret,double>& getDerivative(int degree) const { return *(derivatives[degree]); }
147 
152  Function1<Ret,double>& getDerivative(int degree) { return *(derivatives[degree]); }
153 
157  void addDerivative(Function1<Ret,double> *diff) { derivatives.push_back(diff); }
158 
163  void setDerivative(Function1<Ret,double> *diff,size_t degree) { derivatives.resize(max(derivatives.size(),degree+1)); derivatives[degree]=diff; }
164 
168  void setOrderOfDerivative(int i) { order=i; }
169 
174  virtual void initializeUsingXML(TiXmlElement *element) {
176  TiXmlElement * e;
177  e=element->FirstChildElement(MBSIMNS"orderOfDerivative");
178  if (e)
179  setOrderOfDerivative(atoi(e->GetText()));
180  }
181  /***************************************************/
182 
183  protected:
187  std::vector<Function1<Ret,double>* > derivatives;
188  int order;
189  };
190 
196  template<class Ret, class Arg>
197  class ConstantFunction1 : public Function1<Ret,Arg> {
198  public:
203 
208  ConstantFunction1(Ret c_) : c(c_) {}
209 
210  /* INHERITED INTERFACE OF FUNCTION1 */
211  virtual Ret operator()(const Arg& p, const void * =NULL) { return c; }
212  void initializeUsingXML(TiXmlElement *element) {
214  TiXmlElement *e;
215  e=element->FirstChildElement(MBSIMNS"value");
216  c=Ret(e->GetText());
217  }
218  /***************************************************/
219 
220  /* GETTER / SETTER */
221  void setValue(Ret c_) { c=c_; }
222  /***************************************************/
223 
224  protected:
228  Ret c;
229  };
230 
236  template<class Arg>
237  class ConstantFunction1<double, Arg> : public Function1<double,Arg> {
238  public:
243 
248  ConstantFunction1(double c_) : c(c_) {}
249 
250  /* INHERITED INTERFACE OF FUNCTION1 */
251  virtual double operator()(const Arg& p, const void * =NULL) { return c; }
252  void initializeUsingXML(TiXmlElement *element) {
254  TiXmlElement *e;
255  e=element->FirstChildElement(MBSIMNS"value");
256  c=Element::getDouble(e);
257  }
258  /***************************************************/
259 
260  /* GETTER / SETTER */
261  void setValue(double c_) { c=c_; }
262  /***************************************************/
263 
264  protected:
268  double c;
269  };
270 
276  template<class Ret, class Arg1, class Arg2>
277  class ConstantFunction2 : public Function2<Ret,Arg1,Arg2> {
278  public:
283 
288  ConstantFunction2(Ret c_) : c(c_) {}
289 
290  /* INHERITED INTERFACE OF FUNCTION2 */
291  virtual Ret operator()(const Arg1& p1, const Arg2& p2, const void * =NULL) { return c; }
292  void initializeUsingXML(TiXmlElement *element) {
294  TiXmlElement *e;
295  e=element->FirstChildElement(MBSIMNS"value");
296  c=Ret(e->GetText());
297  }
298  /***************************************************/
299 
300  /* GETTER / SETTER */
301  void setValue(Ret c_) { c=c_; }
302  /***************************************************/
303 
304  protected:
308  Ret c;
309  };
310 
317  class LinearSpringDamperForce : public Function2<double,double,double> {
318  public:
323 
330  LinearSpringDamperForce(double c_, double d_, double l0_) : c(c_), d(d_), l0(l0_) {}
331 
332  /* INHERITED INTERFACE OF FUNCTION2 */
333  virtual double operator()(const double& g, const double& gd, const void * =NULL) { return c*(g-l0) + d*gd; }
334  void initializeUsingXML(TiXmlElement *element);
335  /***************************************************/
336 
337  /* GETTER / SETTER */
338  void setParameters(double c_, double d_, double l0_) { c=c_; d=d_; l0=l0_; }
339  /***************************************************/
340 
341  protected:
345  double c, d, l0;
346  };
347 
354  class NonlinearSpringDamperForce : public Function2<double,double,double> {
355  public:
360 
367 
368  /* INHERITED INTERFACE OF FUNCTION2 */
369  virtual double operator()(const double& g, const double& gd, const void * =NULL) { return (*gForceFun)(g)(0) + (*gdForceFun)(gd)(0); }
370  void initializeUsingXML(TiXmlElement *element);
371  /***************************************************/
372 
373  /* GETTER / SETTER */
374  void setParameters(Function1<fmatvec::Vec, double> * gForceFun_, Function1<fmatvec::Vec, double> * gdForceFun_) { gForceFun=gForceFun_; gdForceFun=gdForceFun_; }
375  /***************************************************/
376 
377  protected:
382 
387  };
388 
394  template<class Arg1, class Arg2>
395  class ConstantFunction2<double, Arg1, Arg2> : public Function2<double,Arg1,Arg2> {
396  public:
405  ConstantFunction2(double c_) : c(c_) {}
406 
407  /* INHERITED INTERFACE OF FUNCTION2 */
408  virtual double operator()(const Arg1& p1, const Arg2& p2, const void * =NULL) { return c; }
409  void initializeUsingXML(TiXmlElement *element) {
411  TiXmlElement *e;
412  e=element->FirstChildElement(MBSIMNS"value");
413  c=Element::getDouble(e);
414  }
415  /***************************************************/
416 
417  /* GETTER / SETTER */
418  void setValue(double c_) { c=c_; }
419  /***************************************************/
420 
421  protected:
422  double c;
423  };
424 
431  class LinearRegularizedUnilateralConstraint: public Function2<double,double,double> {
432  public:
437 
443  LinearRegularizedUnilateralConstraint(double c_, double d_) : c(c_), d(d_) {}
444 
445  /* INHERITED INTERFACE OF FUNCTION2 */
446  virtual double operator()(const double& g, const double& gd, const void * =NULL) {
447  if(g>0)
448  return 0;
449  else if(gd<0)
450  return -c*g - d*gd;
451  else
452  return -c*g;
453  }
454  virtual void initializeUsingXML(TiXmlElement *element);
455  /***************************************************/
456 
457  /* GETTER / SETTER */
458  void setParameter(double c_, double d_) { c=c_; d=d_; }
459  /***************************************************/
460 
461  private:
465  double c, d;
466  };
467 
474  class LinearRegularizedBilateralConstraint: public Function2<double,double,double> {
475  public:
480 
486  LinearRegularizedBilateralConstraint(double c_, double d_) : c(c_), d(d_) {}
487 
488  /* INHERITED INTERFACE OF FUNCTION2 */
489  virtual double operator()(const double& g, const double& gd, const void * =NULL) {
490  return -c*g - d*gd;
491  }
492  virtual void initializeUsingXML(TiXmlElement *element);
493  /***************************************************/
494 
495  /* GETTER / SETTER */
496  void setParameter(double c_, double d_) { c=c_; d=d_; }
497  /***************************************************/
498 
499  private:
503  double c, d;
504  };
505 
513  class LinearRegularizedCoulombFriction : public Function2<fmatvec::Vec,fmatvec::Vec,double> {
514  public:
518  LinearRegularizedCoulombFriction() : mu(0), gdLim(0.01) {}
519 
525  LinearRegularizedCoulombFriction(double mu_, double gdLim_=0.01) : mu(mu_), gdLim(gdLim_) {}
526 
527  /* INHERITED INTERFACE OF FUNCTION2 */
528  virtual fmatvec::Vec operator()(const fmatvec::Vec &gd, const double& laN, const void * =NULL);
529  virtual void initializeUsingXML(TiXmlElement *element);
530  /***************************************************/
531 
532  /* GETTER / SETTER */
533  void setFrictionCoefficient(double mu_) { mu=mu_; }
534  void setMarginalVelocity(double gdLim_) { gdLim=gdLim_; }
535  /***************************************************/
536 
537  protected:
541  double mu, gdLim;
542  };
543 
551  class LinearRegularizedStribeckFriction : public Function2<fmatvec::Vec,fmatvec::Vec,double> {
552  public:
557 
563  LinearRegularizedStribeckFriction(Function1<double,double> *fmu_, double gdLim_=0.01) : fmu(fmu_), gdLim(gdLim_) {}
564 
565  /* INHERITED INTERFACE OF FUNCTION2 */
566  virtual fmatvec::Vec operator()(const fmatvec::Vec &gd, const double& laN, const void * =NULL);
567  virtual void initializeUsingXML(TiXmlElement *element);
568  /***************************************************/
569 
570  /* GETTER / SETTER */
571  void setFrictionFunction(Function1<double,double> *fmu_) { fmu=fmu_; }
572  void setMarginalVelocity(double gdLim_) { gdLim=gdLim_; }
573  /***************************************************/
574 
575  protected:
580 
584  double gdLim;
585  };
586 
590  class InfluenceFunction : public Function2<double, fmatvec::Vec, fmatvec::Vec > {
591  public:
592  InfluenceFunction(const std::string& firstContourName_, const std::string& secondContourName_) :
593  firstContourName(firstContourName_), secondContourName(secondContourName_) {
594  }
595  /* INHERITED INTERFACE OF FUNCTION2 */
596  virtual double operator()(const fmatvec::Vec& firstContourLagrangeParameter, const fmatvec::Vec& secondContourLagrangeParameter, const void * = NULL)=0;
597  virtual void initializeUsingXML(TiXmlElement *element) {
598  throw MBSimError("InfluenceFuntion::initializeUsingXML : Method not implemented");
599  }
600  /***************************************************/
601 
602  /* GETTER / SETTER */
603  std::string getFirstContourName() {
604  return firstContourName;
605  }
606  std::string getSecondContourName() {
607  return secondContourName;
608  }
609  /***************************************************/
610 
611  protected:
615  std::string firstContourName;
616 
620  std::string secondContourName;
621 
622  };
623 
624  /*
625  * \brief Influence function for flexibility of contour with no influence to other contours (or contour points)
626  */
628  public:
629  FlexibilityInfluenceFunction(const std::string& ContourName_, const double & flexibility_) :
630  InfluenceFunction(ContourName_, ContourName_), flexibility(flexibility_) {
631  }
632  virtual ~FlexibilityInfluenceFunction() {}
633  /* INHERITED INTERFACE OF FUNCTION2 */
634  virtual double operator()(const fmatvec::Vec& firstContourLagrangeParameter, const fmatvec::Vec& secondContourLagrangeParameter, const void * = NULL) {
635  if(nrm2(firstContourLagrangeParameter - secondContourLagrangeParameter) < macheps())
636  return flexibility;
637  else
638  return 0;
639  }
640  /***************************************************/
641 
642  protected:
643  double flexibility;
644  };
645 
646  /*
647  * \brief a class for Influence-Functions with constant coupling
648  */
650  public:
651  ConstantInfluenceFunction(const std::string& firstContourName_, const std::string& secondContourName_, const double & couplingValue_) :
652  InfluenceFunction(firstContourName_, secondContourName_), couplingValue(couplingValue_) {
653  }
654  virtual ~ConstantInfluenceFunction() {}
655  /* INHERITED INTERFACE OF FUNCTION2 */
656  virtual double operator()(const fmatvec::Vec& firstContourLagrangeParameter, const fmatvec::Vec& secondContourLagrangeParameter, const void * = NULL) {
657  return couplingValue;
658  }
659  /***************************************************/
660 
661  protected:
662  double couplingValue;
663  };
664 }
665 
666 #endif /* FUNCTION_H_ */
667