All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
fourier_function.h
1 /* Copyright (C) 2004-2014 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 _FOURIER_FUNCTION_H_
21 #define _FOURIER_FUNCTION_H_
22 
23 #include "mbsim/functions/function.h"
24 #include "mbsim/utils/utils.h"
25 
26 namespace MBSim {
27 
28  template<typename Sig> class FourierFunction;
29 
30  template<typename Ret, typename Arg>
31  class FourierFunction<Ret(Arg)> : public Function<Ret(Arg)> {
33 
34  public:
35  FourierFunction() = default;
36  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_) { }
37  int getArgSize() const override { return 1; }
38  std::pair<int, int> getRetSize() const override { return std::make_pair(1,1); }
39  Ret operator()(const Arg& t_) override {
40  double t = ToDouble<Arg>::cast(t_);
41  double y = a0/2;
42  double Om = 2.*M_PI*f;
43  for(int i=0; i<a.size(); i++) {
44  int k = i+1;
45  double phi = k*Om*t;
46  y += a(i)*cos(phi)+b(i)*sin(phi);
47  }
48  return FromDouble<Ret>::cast(y);
49  }
50  typename B::DRetDArg parDer(const Arg &t_) override {
51  double t = ToDouble<Arg>::cast(t_);
52  double yd = 0;
53  double Om = 2.*M_PI*f;
54  for(int i=0; i<a.size(); i++) {
55  int k = i+1;
56  double phi = k*Om*t;
57  yd += k*Om*(b(i)*cos(phi)-a(i)*sin(phi));
58  }
60  }
61  void initializeUsingXML(xercesc::DOMElement * element) override {
62  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"frequency");
63  f=MBXMLUtils::E(e)->getText<double>();
64  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
65  if(e) a0=MBXMLUtils::E(e)->getText<double>();
66  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a");
67  if(e) {
68  a = MBXMLUtils::E(e)->getText<fmatvec::Vec>();
69  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"b");
70  b = MBXMLUtils::E(e)->getText<fmatvec::Vec>();
71  }
72  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"ab");
73  if(e) {
74  fmatvec::MatV xy = MBXMLUtils::E(e)->getText<fmatvec::Mat>();
75  assert(xy.cols() == 2);
76  a = xy.col(0);
77  b = xy.col(1);
78  }
79  e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"amplitudePhaseAngleForm");
80  if(e) amplitudePhaseAngleForm = MBXMLUtils::E(e)->getText<bool>();
81  }
82  void init(Element::InitStage stage, const InitConfigSet &config) override {
83  Function<Ret(Arg)>::init(stage, config);
84  if(stage == Element::preInit) {
85  if(amplitudePhaseAngleForm) {
86  for(int i=0; i<a.size(); i++) {
87  double buf = a.e(i);
88  a(i) = buf*sin(b(i));
89  b(i) = buf*cos(b(i));
90  }
91  }
92  }
93  }
94  protected:
95  double f;
96  double a0;
97  fmatvec::VecV a, b;
98  bool amplitudePhaseAngleForm;
99  int size;
100  private:
101  };
102 
103 }
104 
105 #endif
Definition: utils.h:61
Definition: fourier_function.h:28
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: utils.h:105
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: fourier_function.h:82
Definition: planar_contour.h:30
Definition: element.h:64

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home