All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
two_dimensional_piecewise_polynom_function.h
1 /* Copyright (C) 2004-2016 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 _TWO_DIMENSIONAL_PIECEWISE_POLYNOM_FUNCTION_H_
21 #define _TWO_DIMENSIONAL_PIECEWISE_POLYNOM_FUNCTION_H_
22 
23 #include "mbsim/functions/piecewise_polynom_function.h"
24 
25 namespace MBSim {
26 
27  template<typename Sig> class TwoDimensionalPiecewisePolynomFunction;
28 
29  template<typename Ret, typename Arg1, typename Arg2>
30  class TwoDimensionalPiecewisePolynomFunction<Ret(Arg1, Arg2)> : public Function<Ret(Arg1, Arg2)> {
32  public:
33  enum InterpolationMethod {
34  cSplinePeriodic,
35  cSplineNatural,
36  piecewiseLinear
37  };
38 
39  TwoDimensionalPiecewisePolynomFunction() : method1(cSplineNatural), method2(cSplineNatural) {
40  f1.setParent(this);
41  f2.setParent(this);
42  }
43 
44  virtual void initializeUsingXML(xercesc::DOMElement *element) {
45  xercesc::DOMElement * e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"x");
46  if(e) {
47  setx(Element::getVec(e));
48  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"y");
49  sety(Element::getVec(e));
50  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"z");
51  setz(Element::getMat(e, y.size(), x.size()));
52  }
53  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xyz");
54  if(e) setxyz(Element::getMat(e));
55  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"interpolationMethodFirstDimension");
56  if(e) {
57  std::string str=MBXMLUtils::X()%MBXMLUtils::E(e)->getFirstTextChild()->getData();
58  str=str.substr(1,str.length()-2);
59  if(str=="cSplinePeriodic") method1=cSplinePeriodic;
60  else if(str=="cSplineNatural") method1=cSplineNatural;
61  else if(str=="piecewiseLinear") method1=piecewiseLinear;
62  }
63  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"interpolationMethodSecondDimension");
64  if(e) {
65  std::string str=MBXMLUtils::X()%MBXMLUtils::E(e)->getFirstTextChild()->getData();
66  str=str.substr(1,str.length()-2);
67  if(str=="cSplinePeriodic") method2=cSplinePeriodic;
68  else if(str=="cSplineNatural") method2=cSplineNatural;
69  else if(str=="piecewiseLinear") method2=piecewiseLinear;
70  }
71  }
72 
73  virtual Ret operator()(const Arg1& xVal, const Arg2& yVal) {
74  f2.sety(f1(xVal));
75  f2.reset();
76  f2.calculateSpline();
77  return f2(yVal);
78  }
79 
80  typename B::DRetDArg1 parDer1(const Arg1 &xVal, const Arg2 &yVal) {
81  f2.sety(f1.parDer(xVal));
82  f2.reset();
83  f2.calculateSpline();
84  return f2(yVal);
85  }
86 
87  typename B::DRetDArg2 parDer2(const Arg1 &xVal, const Arg2 &yVal) {
88  f2.sety(f1(xVal));
89  f2.reset();
90  f2.calculateSpline();
91  return f2.parDer(yVal);
92  }
93 
94  typename B::DRetDArg1 parDer1DirDer1(const Arg1 &xdVal, const Arg1 &xVal, const Arg2 &yVal) {
95  f2.sety(f1.parDerDirDer(xdVal,xVal));
96  f2.reset();
97  f2.calculateSpline();
98  return f2(yVal);
99  }
100 
101  typename B::DRetDArg1 parDer1DirDer2(const Arg2 &ydVal, const Arg1 &xVal, const Arg2 &yVal) {
102  f2.sety(f1.parDer(xVal));
103  f2.reset();
104  f2.calculateSpline();
105  return f2.parDer(yVal)*ydVal;
106  }
107 
108  typename B::DRetDArg2 parDer2DirDer1(const Arg1 &xdVal, const Arg1 &xVal, const Arg2 &yVal) {
109  f2.sety(f1.parDer(xVal)*xdVal);
110  f2.reset();
111  f2.calculateSpline();
112  return f2.parDer(yVal);
113  }
114 
115  typename B::DRetDArg2 parDer2DirDer2(const Arg2 &ydVal, const Arg1 &xVal, const Arg2 &yVal) {
116  f2.sety(f1(xVal));
117  f2.reset();
118  f2.calculateSpline();
119  return f2.parDerDirDer(ydVal,yVal);
120  }
121 
122  void setx(const fmatvec::VecV &x_) { x = x_; }
123 
124  void sety(const fmatvec::VecV &y_) { y = y_; }
125 
126  void setz(const fmatvec::MatV &z_) { z = z_; }
127 
128  void setxyz(const fmatvec::MatV &xyz) {
129  if(xyz.rows() <= 1 or xyz.cols() <= 1)
130  THROW_MBSIMERROR("Dimension missmatch in size of xyz");
131  x = xyz.row(0)(fmatvec::RangeV(1,xyz.cols()-1)).T();
132  y = xyz.col(0)(fmatvec::RangeV(1,xyz.rows()-1));
133  z = xyz(fmatvec::RangeV(1,xyz.rows()-1),fmatvec::RangeV(1,xyz.cols()-1));
134  }
135 
136  void setInterpolationMethodFirstDimension(InterpolationMethod method1_) { method1 = method1_; }
137  void setInterpolationMethodSecondDimension(InterpolationMethod method2_) { method2 = method2_; }
138 
139  void init(Element::InitStage stage) {
141  if(stage==Element::preInit) {
142  if (z.cols() != x.size())
143  THROW_MBSIMERROR("Dimension missmatch in xSize");
144  if (z.rows() != y.size())
145  THROW_MBSIMERROR("Dimension missmatch in ySize");
146  for (int i = 1; i < x.size(); i++)
147  if (x(i - 1) >= x(i))
148  THROW_MBSIMERROR("x values must be strictly monotonic increasing!");
149  for (int i = 1; i < y.size(); i++)
150  if (y(i - 1) >= y(i))
151  THROW_MBSIMERROR("y values must be strictly monotonic increasing!");
152  f1.setx(x);
153  f1.sety(z.T());
154  f1.setInterpolationMethod(static_cast<typename PiecewisePolynomFunction<fmatvec::VecV(Arg1)>::InterpolationMethod>(method1));
155  f2.setx(y);
156  f2.sety(y);
157  f2.setInterpolationMethod(static_cast<typename PiecewisePolynomFunction<Ret(Arg2)>::InterpolationMethod>(method2));
158  }
159  f1.init(stage);
160  f2.init(stage);
161  }
162 
163  protected:
164  fmatvec::VecV x;
165  fmatvec::VecV y;
166  fmatvec::MatV z;
169  InterpolationMethod method1, method2;
170  };
171 }
172 
173 #endif
Definition: two_dimensional_piecewise_polynom_function.h:27
InitStage
The stages of the initialization.
Definition: element.h:97
void init(Element::InitStage stage)
plots time series header
Definition: two_dimensional_piecewise_polynom_function.h:139
Definition: piecewise_polynom_function.h:32
Definition: planar_contour.h:31
Definition: element.h:100

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML