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 "mbsim/functions/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 Function<Ret(Arg)> {
00035 
00036     public:
00037       TabularFunction() : xIndexOld(0) { }
00038       TabularFunction(const fmatvec::VecV &x_, const fmatvec::MatV &y_) : x(x_), y(y_), xIndexOld(0) { }
00039       Ret operator()(const Arg& xVal_) {
00040         double xVal = ToDouble<Arg>::cast(xVal_);
00041         int i = xIndexOld;
00042         if (xVal <= x(0)) {
00043           xIndexOld = 0;
00044           return FromVecV<Ret>::cast(trans(y.row(0)));
00045         }
00046         else if (xVal >= x(x.size() - 1)) {
00047           xIndexOld = x.size() - 1;
00048           return FromVecV<Ret>::cast(trans(y.row(x.size() - 1)));
00049         }
00050         else if (xVal <= x(i)) {
00051           while (xVal < x(i))
00052             i--;
00053         }
00054         else {
00055           do
00056             i++;
00057           while (xVal > x(i));
00058           i--;
00059         }
00060         xIndexOld = i;
00061         return FromVecV<Ret>::cast(trans(y.row(i) + (xVal - x(i)) * (y.row(i + 1) - y.row(i)) / (x(i + 1) - x(i))));
00062       }
00063       void initializeUsingXML(xercesc::DOMElement * element) {
00064         xercesc::DOMElement *e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"x");
00065         if (e) {
00066           x = Element::getVec(e);
00067           e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"y");
00068           y = Element::getMat(e, x.size(), 0);
00069         }
00070         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xy");
00071         if (e) {
00072           fmatvec::MatV xy = Element::getMat(e);
00073           if(xy.cols() <= 1)
00074             THROW_MBSIMERROR("Dimension missmatch in size of xy");
00075           x = xy.col(0);
00076           y = xy(fmatvec::Range<fmatvec::Var, fmatvec::Var>(0, xy.rows() - 1), fmatvec::Range<fmatvec::Var, fmatvec::Var>(1, xy.cols() - 1));
00077         }
00078       }
00079       void init(Element::InitStage stage) {
00080         Function<Ret(Arg)>::init(stage);
00081         if(stage==Element::preInit) {
00082           for(int i=1; i<x.size(); i++)
00083             if(x(i) <= x(i-1))
00084               THROW_MBSIMERROR("Values of x must be strictly monotonic increasing!");
00085           if(y.rows() != x.size())
00086             THROW_MBSIMERROR("Dimension missmatch in size of x");
00087         }
00088       }
00089     protected:
00090       fmatvec::VecV x;
00091       fmatvec::MatV y;
00092     private:
00093       int xIndexOld;
00094   };
00095 
00096   template<typename Sig> class TwoDimensionalTabularFunction; 
00097 
00098   template<typename Ret, typename Arg1, typename Arg2>
00099   class TwoDimensionalTabularFunction<Ret(Arg1, Arg2)> : public Function<Ret(Arg1, Arg2)> {
00100     public:
00101       TwoDimensionalTabularFunction() : x0Index(0), x1Index(0), y0Index(0), y1Index(0), xy(4, fmatvec::INIT, 1), XYval(4, fmatvec::INIT, 0), XYfac(4, 4, fmatvec::INIT, 0) { }
00102       /* INHERITED INTERFACE OF FUNCTION2 */
00103       virtual void initializeUsingXML(xercesc::DOMElement *element) {
00104         xercesc::DOMElement * e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xValues");
00105         fmatvec::Vec x_ = Element::getVec(e);
00106         setXValues(x_);
00107         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"yValues");
00108         fmatvec::Vec y_ = Element::getVec(e);
00109         setYValues(y_);
00110         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"xyValues");
00111         fmatvec::Mat xy_ = Element::getMat(e, y_.size(), x_.size());
00112         setXYMat(xy_);
00113       }
00114       virtual Ret operator()(const Arg1& x_, const Arg2& y_) {
00115         double x = ToDouble<Arg1>::cast(x_);
00116         double y = ToDouble<Arg2>::cast(y_);
00117         calcIndex(x, xVec, xVec.size(), x0Index, x1Index);
00118         calcIndex(y, yVec, yVec.size(), y0Index, y1Index);
00119 
00120         xy(1) = x;
00121         xy(2) = y;
00122         xy(3) = x * y;
00123         const double x0 = xVec(x0Index);
00124         const double x1 = xVec(x1Index);
00125         const double y0 = yVec(y0Index);
00126         const double y1 = yVec(y1Index);
00127         const double nenner = (x0 - x1) * (y0 - y1);
00128         XYval(0) = XY(y0Index, x0Index);
00129         XYval(1) = XY(y0Index, x1Index);
00130         XYval(2) = XY(y1Index, x0Index);
00131         XYval(3) = XY(y1Index, x1Index);
00132         XYfac(0, 0) = x1 * y1;
00133         XYfac(0, 1) = -x0 * y1;
00134         XYfac(0, 2) = -x1 * y0;
00135         XYfac(0, 3) = x0 * y0;
00136         XYfac(1, 0) = -y1;
00137         XYfac(1, 1) = y1;
00138         XYfac(1, 2) = y0;
00139         XYfac(1, 3) = -y0;
00140         XYfac(2, 0) = -x1;
00141         XYfac(2, 1) = x0;
00142         XYfac(2, 2) = x1;
00143         XYfac(2, 3) = -x0;
00144         XYfac(3, 0) = 1.;
00145         XYfac(3, 1) = -1.;
00146         XYfac(3, 2) = -1.;
00147         XYfac(3, 3) = 1.;
00148 
00149         return FromDouble<Ret>::cast(trans(1. / nenner * XYfac * XYval) * xy);
00150       }
00151       /***************************************************/
00152       /* GETTER / SETTER */
00153       void setXValues(const fmatvec::Vec &xVec_) { xVec = xVec_; }
00154       void setYValues(const fmatvec::Vec &yVec_) { yVec = yVec_; }
00155       void setXYMat(const fmatvec::Mat &XY_) { XY = XY_; }
00156 
00157       double getxMin() { return xVec(0); }
00158       double getxMax() { return xVec(xVec.size() - 1); }
00159       double getyMin() { return yVec(0); }
00160       double getyMax() { return yVec(yVec.size() - 1); }
00161       /***************************************************/
00162 
00163       void init(Element::InitStage stage) {
00164         Function<Ret(Arg1, Arg2)>::init(stage);
00165         if(stage==Element::preInit) {
00166           if (XY.cols() != xVec.size())
00167             THROW_MBSIMERROR("Dimension missmatch in xSize");
00168           if (XY.rows() != yVec.size())
00169             THROW_MBSIMERROR("Dimension missmatch in ySize");
00170           for (int i = 1; i < xVec.size(); i++)
00171             if (xVec(i - 1) >= xVec(i))
00172               THROW_MBSIMERROR("x values must be strictly monotonic increasing!");
00173           for (int i = 1; i < yVec.size(); i++)
00174             if (yVec(i - 1) >= yVec(i))
00175               THROW_MBSIMERROR("y values must be strictly monotonic increasing!");
00176         }
00177       }
00178     protected:
00179       fmatvec::Vec xVec;
00180       fmatvec::Vec yVec;
00181       fmatvec::Mat XY;
00182 
00183       int x0Index, x1Index;
00184       int y0Index, y1Index;
00185 
00186       fmatvec::Vec xy;
00187       fmatvec::Vec XYval;
00188       fmatvec::Mat XYfac;
00189 
00190       void calcIndex(double x, const fmatvec::Vec &X, int xSize, int &xIndexMinus, int &xIndexPlus) {
00191         if (x <= X(0)) {
00192           xIndexPlus = 1;
00193           xIndexMinus = 0;
00194           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "TwoDimensionalTabularFunction: Value (" << x << ") is smaller than the smallest table value(" << X(0) << ")!" << std::endl;
00195         }
00196         else if (x >= X(xSize - 1)) {
00197           xIndexPlus = xSize - 1;
00198           xIndexMinus = xSize - 2;
00199           fmatvec::Atom::msg(fmatvec::Atom::Warn) << "TwoDimensionalTabularFunction: Value (" << x << ") is greater than the greatest table value(" << X(xSize - 1) << ")!" << std::endl;
00200         }
00201         else {
00202           if (x < X(xIndexPlus))
00203             while (x < X(xIndexPlus - 1) && xIndexPlus > 1)
00204               xIndexPlus--;
00205           else if (x > X(xIndexPlus))
00206             while (x > X(xIndexPlus) && xIndexPlus < xSize - 1)
00207               xIndexPlus++;
00208           xIndexMinus = xIndexPlus - 1;
00209         }
00210       }
00211 
00212   };
00213 }
00214 
00215 #endif