MBSim

Project Page
tabular_functions.h
00001 /* Copyright (C) 2004-2009 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: markus.ms.schneider@gmail.com
00018  */
00019 
00020 #ifndef _TABULAR_FUNCTIONS_H_
00021 #define _TABULAR_FUNCTIONS_H_
00022 
00023 #include <iostream>
00024 #include "fmatvec/function.h"
00025 #include "mbsim/objectfactory.h"
00026 #include "mbsim/element.h"
00027 #include "mbsim/utils/utils.h"
00028 
00029 namespace MBSim {
00030 
00031   template<typename Sig> class TabularFunction; 
00032 
00033   template<typename Ret, typename Arg>
00034   class TabularFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00035 
00036     public:
00037       TabularFunction() :
00038           xIndexOld(0) {
00039       }
00040       TabularFunction(const fmatvec::VecV &x_, const fmatvec::MatV &y_) :
00041           x(x_), y(y_), xIndexOld(0) {
00042         init();
00043       }
00044       Ret operator()(const Arg& xVal_) {
00045         double xVal = ToDouble<Arg>::cast(xVal_);
00046         int i = xIndexOld;
00047         if (xVal <= x(0)) {
00048           xIndexOld = 0;
00049           return FromVecV<Ret>::cast(trans(y.row(0)));
00050         }
00051         else if (xVal >= x(xSize - 1)) {
00052           xIndexOld = xSize - 1;
00053           return FromVecV<Ret>::cast(trans(y.row(xSize - 1)));
00054         }
00055         else if (xVal <= x(i)) {
00056           while (xVal < x(i))
00057             i--;
00058         }
00059         else {
00060           do
00061             i++;
00062           while (xVal > x(i));
00063           i--;
00064         }
00065         xIndexOld = i;
00066         return FromVecV<Ret>::cast(trans(y.row(i) + (xVal - x(i)) * (y.row(i + 1) - y.row(i)) / (x(i + 1) - x(i))));
00067       }
00068       void initializeUsingXML(xercesc::DOMElement * element) {
00069         xercesc::DOMElement *e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"x");
00070         if (e) {
00071           x = Element::getVec(e);
00072           e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"y");
00073           y = Element::getMat(e, x.size(), 0);
00074         }
00075         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xy");
00076         if (e) {
00077           fmatvec::MatV xy = Element::getMat(e);
00078           assert(xy.cols() > 1);
00079           x = xy.col(0);
00080           y = xy(fmatvec::Range<fmatvec::Var, fmatvec::Var>(0, xy.rows() - 1), fmatvec::Range<fmatvec::Var, fmatvec::Var>(1, xy.cols() - 1));
00081         }
00082         init();
00083       }
00084     protected:
00085       fmatvec::VecV x;
00086       fmatvec::MatV y;
00087     private:
00088       int xIndexOld, xSize;
00089       void init() {
00090         for (int i = 1; i < x.size(); i++)
00091           assert(x(i) > x(i - 1));
00092         assert(x.size() == y.rows());
00093         xSize = x.size();
00094       }
00095   };
00096 
00097   template<typename Sig> class PeriodicTabularFunction; 
00098 
00099   template<typename Ret, typename Arg>
00100   class PeriodicTabularFunction<Ret(Arg)> : public TabularFunction<Ret(Arg)> {
00101     public:
00102       PeriodicTabularFunction() {
00103       }
00104       PeriodicTabularFunction(const fmatvec::VecV &x_, const fmatvec::MatV &y_) :
00105           TabularFunction<Ret>(x_, y_) {
00106         init();
00107       }
00108       Ret operator()(const Arg& x) {
00109         double xValTmp = ToDouble<Arg>::cast(x);
00110         while (xValTmp < xMin)
00111           xValTmp += xDelta;
00112         while (xValTmp > xMax)
00113           xValTmp -= xDelta;
00114         return TabularFunction<Ret(Arg)>::operator()(xValTmp);
00115       }
00116       void initializeUsingXML(xercesc::DOMElement *element) {
00117         TabularFunction<Ret(Arg)>::initializeUsingXML(element);
00118         init();
00119       }
00120     private:
00121       double xMin, xMax, xDelta;
00122       void init() {
00123         xMin = TabularFunction<Ret(Arg)>::x(0);
00124         xMax = this->x(TabularFunction<Ret(Arg)>::x.size() - 1);
00125         xDelta = xMax - xMin;
00126       }
00127   };
00128 
00129   template<typename Sig> class TwoDimensionalTabularFunction; 
00130 
00131   template<typename Ret, typename Arg1, typename Arg2>
00132   class TwoDimensionalTabularFunction<Ret(Arg1,Arg2)> : public fmatvec::Function<Ret(Arg1, Arg2)> {
00133     public:
00134       TwoDimensionalTabularFunction() :
00135           xSize(0), ySize(0), x0Index(0), x1Index(0), y0Index(0), y1Index(0), func_value(1, fmatvec::INIT, 0), xy(4, fmatvec::INIT, 1), XYval(4, fmatvec::INIT, 0), XYfac(4, 4, fmatvec::INIT, 0) {
00136       }
00137       /* INHERITED INTERFACE OF FUNCTION2 */
00138       virtual void initializeUsingXML(xercesc::DOMElement *element) {
00139         xercesc::DOMElement * e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xValues");
00140         fmatvec::Vec x_ = Element::getVec(e);
00141         setXValues(x_);
00142         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"yValues");
00143         fmatvec::Vec y_ = Element::getVec(e);
00144         setYValues(y_);
00145         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xyValues");
00146         fmatvec::Mat xy_ = Element::getMat(e, y_.size(), x_.size());
00147         setXYMat(xy_);
00148       }
00149       virtual Ret operator()(const Arg1& x_, const Arg2& y_) {
00150         double x = ToDouble<Arg1>::cast(x_);
00151         double y = ToDouble<Arg2>::cast(y_);
00152         calcIndex(&x, xVec, &xSize, &x0Index, &x1Index);
00153         calcIndex(&y, yVec, &ySize, &y0Index, &y1Index);
00154 
00155         xy(1) = x;
00156         xy(2) = y;
00157         xy(3) = x * y;
00158         const double x0 = xVec(x0Index);
00159         const double x1 = xVec(x1Index);
00160         const double y0 = yVec(y0Index);
00161         const double y1 = yVec(y1Index);
00162         const double nenner = (x0 - x1) * (y0 - y1);
00163         XYval(0) = XY(y0Index, x0Index);
00164         XYval(1) = XY(y0Index, x1Index);
00165         XYval(2) = XY(y1Index, x0Index);
00166         XYval(3) = XY(y1Index, x1Index);
00167         XYfac(0, 0) = x1 * y1;
00168         XYfac(0, 1) = -x0 * y1;
00169         XYfac(0, 2) = -x1 * y0;
00170         XYfac(0, 3) = x0 * y0;
00171         XYfac(1, 0) = -y1;
00172         XYfac(1, 1) = y1;
00173         XYfac(1, 2) = y0;
00174         XYfac(1, 3) = -y0;
00175         XYfac(2, 0) = -x1;
00176         XYfac(2, 1) = x0;
00177         XYfac(2, 2) = x1;
00178         XYfac(2, 3) = -x0;
00179         XYfac(3, 0) = 1.;
00180         XYfac(3, 1) = -1.;
00181         XYfac(3, 2) = -1.;
00182         XYfac(3, 3) = 1.;
00183 
00184         return FromDouble<Ret>::cast(trans(1. / nenner * XYfac * XYval) * xy);
00185       }
00186       /***************************************************/
00187       /* GETTER / SETTER */
00188       void setXValues(const fmatvec::Vec &xVec_) {
00189         xVec << xVec_;
00190         xSize = xVec.size();
00191 
00192         for (int i = 1; i < xVec.size(); i++)
00193           if (xVec(i - 1) >= xVec(i))
00194             throw MBSimError("xVec must be strictly monotonic increasing!");
00195         xSize = xVec.size();
00196       }
00197       void setYValues(const fmatvec::Vec &yVec_) {
00198         yVec << yVec_;
00199         ySize = yVec.size();
00200 
00201         for (int i = 1; i < yVec.size(); i++)
00202           if (yVec(i - 1) >= yVec(i))
00203             throw MBSimError("yVec must be strictly monotonic increasing!");
00204       }
00205       void setXYMat(const fmatvec::Mat &XY_) {
00206         XY << XY_;
00207 
00208         if (xSize == 0)
00209           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "It is strongly recommended to set x file first! Continuing anyway." << std::endl;
00210         else if (ySize == 0)
00211           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "It is strongly recommended to set y file first! Continuing anyway." << std::endl;
00212         else {
00213           if (XY.cols() != xSize)
00214             throw MBSimError("Dimension missmatch in xSize");
00215           else if (XY.rows() != ySize)
00216             throw MBSimError("Dimension missmatch in ySize");
00217         }
00218       }
00219 
00220       double getxMin() {
00221         return xVec(0);
00222       }
00223       double getxMax() {
00224         return xVec(xVec.size() - 1);
00225       }
00226       double getyMin() {
00227         return yVec(0);
00228       }
00229       double getyMax() {
00230         return yVec(yVec.size() - 1);
00231       }
00232 
00233       /***************************************************/
00234 
00235     protected:
00236       fmatvec::Vec xVec;
00237       fmatvec::Vec yVec;
00238       fmatvec::Mat XY;
00239 
00240       int xSize;
00241       int ySize;
00242       int x0Index, x1Index;
00243       int y0Index, y1Index;
00244 
00245       fmatvec::Vec func_value;
00246       fmatvec::Vec xy;
00247       fmatvec::Vec XYval;
00248       fmatvec::Mat XYfac;
00249 
00250       void calcIndex(const double * x, fmatvec::Vec X, int * xSize, int * xIndexMinus, int * xIndexPlus) {
00251         if (*x <= X(0)) {
00252           *xIndexPlus = 1;
00253           *xIndexMinus = 0;
00254           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "TwoDimensionalTabularFunction: Value (" << *x << ") is smaller than the smallest table value(" << X(0) << ")!" << std::endl;
00255         }
00256         else if (*x >= X(*xSize - 1)) {
00257           *xIndexPlus = *xSize - 1;
00258           *xIndexMinus = *xSize - 2;
00259           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "TwoDimensionalTabularFunction: Value (" << *x << ") is greater than the greatest table value(" << X(*xSize - 1) << ")!" << std::endl;
00260         }
00261         else {
00262           if (*x < X(*xIndexPlus))
00263             while (*x < X(*xIndexPlus - 1) && *xIndexPlus > 1)
00264               (*xIndexPlus)--;
00265           else if (*x > X(*xIndexPlus))
00266             while (*x > X(*xIndexPlus) && *xIndexPlus < *xSize - 1)
00267               (*xIndexPlus)++;
00268           *xIndexMinus = *xIndexPlus - 1;
00269         }
00270       }
00271 
00272   };
00273 }
00274 
00275 #endif