All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
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>
40  class PiecewiseDefinedFunction<Ret(double)> : public Function<Ret(double)> {
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(double)> &limitedFunction) {
49  function.push_back(limitedFunction.function);
50  limitedFunction.function->setParent(this);
51  a.push_back(limitedFunction.limit);
52  }
53  Ret zeros(const Ret &x) { return Ret(x.size()); }
54  int getArgSize() const override { return 1; }
55  std::pair<int, int> getRetSize() const override { return function.size()?function[0]->getRetSize():std::make_pair(0,1); }
56  Ret operator()(const double &x) override {
57  for(unsigned int i=0; i<function.size(); i++)
58  if(x<=a[i+1])
59  return y0[i] + (*function[i])(x-x0[i]);
60  this->throwError("(PiecewiseDefinedFunction::operator()): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
61  }
62  typename B::DRetDArg parDer(const double &x) override {
63  for(unsigned int i=0; i<function.size(); i++)
64  if(x<=a[i+1])
65  return function[i]->parDer(x-x0[i]);
66  this->throwError("(PiecewiseDefinedFunction::parDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
67  }
68  typename B::DRetDArg parDerDirDer(const double &xDir, const double &x) override {
69  for(unsigned int i=0; i<function.size(); i++)
70  if(x<=a[i+1])
71  return function[i]->parDerDirDer(xDir,x-x0[i]);
72  this->throwError("(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
73  }
74  typename B::DDRetDDArg parDerParDer(const double &x) override {
75  for(unsigned int i=0; i<function.size(); i++)
76  if(x<=a[i+1])
77  return function[i]->parDerParDer(x-x0[i]);
78  this->throwError("(PiecewiseDefinedFunction::parDerParDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
79  }
80 
81  void initializeUsingXML(xercesc::DOMElement *element) override {
82  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
83  xercesc::DOMElement *ee=e->getFirstElementChild();
84  while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
85  addLimitedFunction(LimitedFunction<Ret(double)>(ObjectFactory::createAndInit<Function<Ret(double)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"function")->getFirstElementChild()),MBXMLUtils::E(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"limit"))->getText<double>()));
86  ee=ee->getNextElementSibling();
87  }
88  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftAbscissa");
89  if(e) shiftAbscissa=MBXMLUtils::E(e)->getText<bool>();
90  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftOrdinate");
91  if(e) shiftOrdinate=MBXMLUtils::E(e)->getText<bool>();
92  }
93  void init(Element::InitStage stage, const InitConfigSet &config) override {
94  Function<Ret(double)>::init(stage, config);
95  for(auto it=function.begin(); it!=function.end(); it++)
96  (*it)->init(stage, config);
97  if(stage==Element::preInit) {
98  if(shiftAbscissa) {
99  for(unsigned int i=1; i<a.size(); i++)
100  a[i] += a[i-1];
101  x0 = a;
102  }
103  else
104  x0.resize(a.size());
105  y0.resize(a.size(),zeros((*function[0])(0)));
106  if(shiftOrdinate) {
107  for(unsigned int i=1; i<a.size(); i++)
108  y0[i] = (*this)(a[i]);
109  }
110  }
111  }
112  private:
113  std::vector<Function<Ret(double)> *> function;
114  std::vector<double> a, x0;
115  std::vector<Ret> y0;
116  bool shiftAbscissa{false}, shiftOrdinate{false};
117  };
118 
119  template<>
120  inline double PiecewiseDefinedFunction<double(double)>::zeros(const double &x) { return 0; }
121 
122  template<typename Ret, typename Arg>
123  class PiecewiseDefinedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
125  public:
126  PiecewiseDefinedFunction() { a.push_back(0); }
127  ~PiecewiseDefinedFunction() override {
128  for(unsigned int i=0; i<function.size(); i++)
129  delete function[i];
130  }
131  void addLimitedFunction(const LimitedFunction<Ret(Arg)> &limitedFunction) {
132  function.push_back(limitedFunction.function);
133  limitedFunction.function->setParent(this);
134  a.push_back(limitedFunction.limit);
135  }
136  int getArgSize() const override { return 1; }
137  std::pair<int, int> getRetSize() const override { return function.size()?function[0]->getRetSize():std::make_pair(0,1); }
138  Ret zeros(const Ret &x) { return Ret(x.size()); }
139  Ret operator()(const Arg &x) override {
140  for(unsigned int i=0; i<function.size(); i++)
141  if(ToDouble<Arg>::cast(x)<=a[i+1])
142  return y0[i] + (*function[i])(x-FromDouble<Arg>::cast(x0[i]));
143  this->throwError("(PiecewiseDefinedFunction::operator()): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
144  }
145  typename B::DRetDArg parDer(const Arg &x) override {
146  for(unsigned int i=0; i<function.size(); i++)
147  if(ToDouble<Arg>::cast(x)<=a[i+1])
148  return function[i]->parDer(x-FromDouble<Arg>::cast(x0[i]));
149  this->throwError("(PiecewiseDefinedFunction::parDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
150  }
151  typename B::DRetDArg parDerDirDer(const Arg &xDir, const Arg &x) override {
152  for(unsigned int i=0; i<function.size(); i++)
153  if(ToDouble<Arg>::cast(x)<=a[i+1])
154  return function[i]->parDerDirDer(xDir,x-FromDouble<Arg>::cast(x0[i]));
155  this->throwError("(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+fmatvec::toString(x)+", upper bound= "+fmatvec::toString(a[function.size()]));
156  }
157 
158  void initializeUsingXML(xercesc::DOMElement *element) override {
159  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
160  xercesc::DOMElement *ee=e->getFirstElementChild();
161  while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
162  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>()));
163  ee=ee->getNextElementSibling();
164  }
165  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftAbscissa");
166  if(e) shiftAbscissa=MBXMLUtils::E(e)->getText<bool>();
167  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"shiftOrdinate");
168  if(e) shiftOrdinate=MBXMLUtils::E(e)->getText<bool>();
169  }
170  void init(Element::InitStage stage, const InitConfigSet &config) override {
171  Function<Ret(Arg)>::init(stage, config);
172  for(auto it=function.begin(); it!=function.end(); it++)
173  (*it)->init(stage, config);
174  if(stage==Element::preInit) {
175  if(shiftAbscissa) {
176  for(unsigned int i=1; i<a.size(); i++)
177  a[i] += a[i-1];
178  x0 = a;
179  }
180  else
181  x0.resize(a.size());
182  y0.resize(a.size(),zeros((*function[0])(Arg(1))));
183  if(shiftOrdinate) {
184  for(unsigned int i=1; i<a.size(); i++)
185  y0[i] = (*this)(FromDouble<Arg>::cast(a[i]));
186  }
187  }
188  }
189  private:
190  std::vector<Function<Ret(Arg)> *> function;
191  std::vector<double> a, x0;
192  std::vector<Ret> y0;
193  bool shiftAbscissa{false}, shiftOrdinate{false};
194  };
195 
196  template <>
197  inline double PiecewiseDefinedFunction<double(fmatvec::VecV)>::zeros(const double &x) { return 0; }
198 
199 }
200 
201 #endif
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: piecewise_defined_function.h:170
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:86
Definition: piecewise_defined_function.h:28
Definition: utils.h:61
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: utils.h:105
Definition: planar_contour.h:30
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: piecewise_defined_function.h:93
Definition: element.h:64
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:97
std::string toString(const T &value, int precision=0)
Definition: piecewise_defined_function.h:37

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home