00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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