MBSim

Project Page
fourier_function.h
00001 /* Copyright (C) 2004-2014 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: martin.o.foerg@gmail.com
00018  */
00019 
00020 #ifndef _FOURIER_FUNCTION_H_
00021 #define _FOURIER_FUNCTION_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 FourierFunction; 
00032 
00033   template<typename Ret, typename Arg>
00034   class FourierFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00035 
00036     public:
00037       FourierFunction() { }
00038       FourierFunction(double f_, const fmatvec::VecV &a_, const fmatvec::VecV &b_, double a0_=0, bool amplitudePhaseAngleForm_=false) : f(f_), a0(a0_), a(a_), b(b_), amplitudePhaseAngleForm(amplitudePhaseAngleForm_) { }
00039       Ret operator()(const Arg& t_) {
00040         double t = ToDouble<Arg>::cast(t_);
00041         double y = a0/2;
00042         double Om = 2.*M_PI*f;
00043         for(int i=0; i<a.size(); i++) {
00044           int k = i+1;
00045           double phi = k*Om*t;
00046           y += a(i)*cos(phi)+b(i)*sin(phi);
00047         }
00048         return FromDouble<Ret>::cast(y);
00049       }
00050       typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &t_) {  
00051         double t = ToDouble<Arg>::cast(t_);
00052         double yd = 0;
00053         double Om = 2.*M_PI*f;
00054         for(int i=0; i<a.size(); i++) {
00055           int k = i+1;
00056           double phi = k*Om*t;
00057           yd += k*Om*(b(i)*cos(phi)-a(i)*sin(phi));
00058         }
00059         return FromDouble<Ret>::cast(yd);
00060       }
00061       typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &t_) {  
00062         double t = ToDouble<Arg>::cast(t_);
00063         double ydd = 0;
00064         double Om = 2.*M_PI*f;
00065         for(int i=0; i<a.size(); i++) {
00066           int k = i+1;
00067           double phi = i*Om*t;
00068           ydd -= pow(k*Om,2)*(a(i)*cos(phi)+b(i)*sin(phi));
00069         }
00070         return FromDouble<Ret>::cast(ydd);
00071       }
00072       void initializeUsingXML(xercesc::DOMElement * element) {
00073         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"frequency");
00074         f=Element::getDouble(e);
00075         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
00076         if(e) a0=Element::getDouble(e);
00077         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a");
00078         if(e) {
00079           a = Element::getVec(e);
00080           e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"b");
00081           b = Element::getVec(e);
00082         }
00083         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"ab");
00084         if(e) {
00085           fmatvec::MatV xy = Element::getMat(e);
00086           assert(xy.cols() == 2);
00087           a = xy.col(0);
00088           b = xy.col(1);
00089         }
00090         e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"amplitudePhaseAngleForm");
00091         if(e) amplitudePhaseAngleForm = Element::getBool(e);
00092       }
00093       void init(Element::InitStage stage) {
00094         Function<Ret(Arg)>::init(stage);
00095         if(stage == Element::preInit) {
00096           if(amplitudePhaseAngleForm) {
00097             for(int i=0; i<a.size(); i++) {
00098               double buf = a.e(i);
00099               a(i) = buf*sin(b(i));
00100               b(i) = buf*cos(b(i));
00101             }
00102           }
00103         }
00104       }
00105     protected:
00106       double f;
00107       double a0;
00108       fmatvec::VecV a, b;
00109       bool amplitudePhaseAngleForm;
00110       int size;
00111     private:
00112   };
00113 
00114 }
00115 
00116 #endif