mbsim  4.0.0
MBSim Kernel
piecewise_defined_function.h
1 /* Copyright (C) 2004-2014 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@gmail.com
18  */
19 
20 #ifndef _PIECEWISE_DEFINED_FUNCTIONS_H_
21 #define _PIECEWISE_DEFINED_FUNCTIONS_H_
22 
23 #include "mbsim/functions/function.h"
24 #include "mbsim/utils/utils.h"
25 
26 namespace MBSim {
27 
28  template<typename Sig> class LimitedFunction;
29 
30  template <typename Ret, typename Arg>
31  struct LimitedFunction<Ret(Arg)> {
32  LimitedFunction(Function<Ret(Arg)> *function_, double limit_) : function(function_), limit(limit_) { }
33  Function<Ret(Arg)> *function;
34  double limit;
35  };
36 
37  template<typename Sig> class PiecewiseDefinedFunction;
38 
39  template<typename Ret, typename Arg>
40  class PiecewiseDefinedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
41  using B = fmatvec::Function<Ret(Arg)>;
42  public:
43  PiecewiseDefinedFunction() { a.push_back(0); }
44  ~PiecewiseDefinedFunction() override {
45  for(unsigned int i=0; i<function.size(); i++)
46  delete function[i];
47  }
48  void addLimitedFunction(const LimitedFunction<Ret(Arg)> &limitedFunction) {
49  function.push_back(limitedFunction.function);
50  limitedFunction.function->setParent(this);
51  a.push_back(limitedFunction.limit);
52  }
53  int getArgSize() const override { return 1; }
54  std::pair<int, int> getRetSize() const override { return function.size()?function[0]->getRetSize():std::make_pair(0,1); }
55  Ret operator()(const Arg &x) override {
56  for(unsigned int i=0; i<function.size(); i++)
57  if(ToDouble<Arg>::cast(x)<=a[i+1])
58  return y0[i] + (*function[i])(x-FromDouble<Arg>::cast(x0[i]));
59  this->throwError("(PiecewiseDefinedFunction::operator()): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
60  }
61  typename B::DRetDArg parDer(const Arg &x) override {
62  for(unsigned int i=0; i<function.size(); i++)
63  if(ToDouble<Arg>::cast(x)<=a[i+1])
64  return function[i]->parDer(x-FromDouble<Arg>::cast(x0[i]));
65  this->throwError("(PiecewiseDefinedFunction::parDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
66  }
67  typename B::DRetDArg parDerDirDer(const Arg &xDir, const Arg &x) override {
68  for(unsigned int i=0; i<function.size(); i++)
69  if(ToDouble<Arg>::cast(x)<=a[i+1])
70  return function[i]->parDerDirDer(xDir,x-FromDouble<Arg>::cast(x0[i]));
71  this->throwError("(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
72  }
73 
74  void initializeUsingXML(xercesc::DOMElement *element) override {
75  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
76  xercesc::DOMElement *ee=e->getFirstElementChild();
77  while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
78  addLimitedFunction(LimitedFunction<Ret(Arg)>(ObjectFactory::createAndInit<Function<Ret(Arg)>>(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"function")->getFirstElementChild()),MBXMLUtils::E(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"limit"))->getText<double>()));
79  ee=ee->getNextElementSibling();
80  }
81  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftAbscissa");
82  if(e) shiftAbscissa=MBXMLUtils::E(e)->getText<bool>();
83  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftOrdinate");
84  if(e) shiftOrdinate=MBXMLUtils::E(e)->getText<bool>();
85  }
86  void init(Element::InitStage stage, const InitConfigSet &config) override {
87  Function<Ret(Arg)>::init(stage, config);
88  for(auto it=function.begin(); it!=function.end(); it++)
89  (*it)->init(stage, config);
90  if(stage==Element::preInit) {
91  if(shiftAbscissa) {
92  for(unsigned int i=1; i<a.size(); i++)
93  a[i] += a[i-1];
94  x0 = a;
95  }
96  else
97  x0.resize(a.size());
98  y0.resize(a.size(),0.0*(*function[0])(Arg(1)));
99  if(shiftOrdinate) {
100  for(unsigned int i=1; i<a.size(); i++)
101  y0[i] = (*this)(FromDouble<Arg>::cast(a[i]));
102  }
103  }
104  }
105  private:
106  std::vector<Function<Ret(Arg)> *> function;
107  std::vector<double> a, x0;
108  std::vector<Ret> y0;
109  bool shiftAbscissa{false}, shiftOrdinate{false};
110  };
111 
112 }
113 
114 #endif
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: piecewise_defined_function.h:86
Definition: piecewise_defined_function.h:28
Definition: utils.h:61
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: utils.h:89
Definition: contact_kinematics.h:32
Definition: element.h:64
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:76
Definition: piecewise_defined_function.h:37
namespace MBSim
Definition: eigenanalyzer.cc:47