All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
tabular_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: markus.ms.schneider@gmail.com
18  */
19 
20 #ifndef _TABULAR_FUNCTION_H_
21 #define _TABULAR_FUNCTION_H_
22 
23 #include "mbsim/functions/function.h"
24 #include "mbsim/utils/utils.h"
25 
26 namespace MBSim {
27 
28  template<typename Sig> class TabularFunction;
29 
30  template<typename Ret, typename Arg>
31  class TabularFunction<Ret(Arg)> : public Function<Ret(Arg)> {
32 
33  public:
34  TabularFunction() : xIndexOld(0) { }
35  TabularFunction(const fmatvec::VecV &x_, const fmatvec::MatV &y_) : x(x_), y(y_), xIndexOld(0) { }
36  Ret operator()(const Arg& xVal_) {
37  double xVal = ToDouble<Arg>::cast(xVal_);
38  int i = xIndexOld;
39  if (xVal <= x(0)) {
40  xIndexOld = 0;
41  return FromVecV<Ret>::cast(trans(y.row(0)));
42  }
43  else if (xVal >= x(x.size() - 1)) {
44  xIndexOld = x.size() - 1;
45  return FromVecV<Ret>::cast(trans(y.row(x.size() - 1)));
46  }
47  else if (xVal <= x(i)) {
48  while (xVal < x(i))
49  i--;
50  }
51  else {
52  do
53  i++;
54  while (xVal > x(i));
55  i--;
56  }
57  xIndexOld = i;
58  return FromVecV<Ret>::cast(trans(y.row(i) + (xVal - x(i)) * (y.row(i + 1) - y.row(i)) / (x(i + 1) - x(i))));
59  }
60  void initializeUsingXML(xercesc::DOMElement * element) {
61  xercesc::DOMElement *e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"x");
62  if (e) {
63  setx(Element::getVec(e));
64  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"y");
65  sety(Element::getMat(e, x.size(), 0));
66  }
67  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xy");
68  if (e) setxy(Element::getMat(e));
69  }
70  void setx(const fmatvec::VecV &x_) { x = x_; }
71  void sety(const fmatvec::MatV &y_) { y = y_; }
72  void setxy(const fmatvec::MatV &xy) {
73  if(xy.cols() <= 1)
74  THROW_MBSIMERROR("Dimension missmatch in size of xy");
75  x = xy.col(0);
76  y = xy(fmatvec::RangeV(0, xy.rows() - 1), fmatvec::RangeV(1, xy.cols() - 1));
77  }
78  void init(Element::InitStage stage) {
80  if(stage==Element::preInit) {
81  for(int i=1; i<x.size(); i++)
82  if(x(i) <= x(i-1))
83  THROW_MBSIMERROR("Values of x must be strictly monotonic increasing!");
84  if(y.rows() != x.size())
85  THROW_MBSIMERROR("Dimension missmatch in size of x");
86  }
87  }
88  protected:
89  fmatvec::VecV x;
90  fmatvec::MatV y;
91  private:
92  int xIndexOld;
93  };
94 }
95 
96 #endif
Definition: utils.h:213
Definition: utils.h:153
InitStage
The stages of the initialization.
Definition: element.h:97
Definition: planar_contour.h:31
Definition: element.h:100
void init(Element::InitStage stage)
plots time series header
Definition: tabular_function.h:78
Definition: tabular_function.h:28

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML