mbsim 4.0.0 MBSim Kernel

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)> {
32 using B = fmatvec::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 void setFrequency(double f_) { f = f_; }
38 void seta0(double a0_) { a0 = a0_; }
39 void seta(const fmatvec::VecV &a_) { a <<= a_; }
40 void setb(const fmatvec::VecV &b_) { b <<= b_; }
41 void setab(const fmatvec::MatV &ab) {
42 assert(ab.cols() == 2);
43 a <<= ab.col(0);
44 b <<= ab.col(1);
45 }
46 void setAmplitudePhaseAngleForm(bool amplitudePhaseAngleForm_) { amplitudePhaseAngleForm = amplitudePhaseAngleForm_; }
47 int getArgSize() const override { return 1; }
48 std::pair<int, int> getRetSize() const override { return std::make_pair(1,1); }
49 Ret operator()(const Arg& t_) override {
50 double t = ToDouble<Arg>::cast(t_);
51 double y = a0/2;
52 double Om = 2.*M_PI*f;
53 for(int i=0; i<a.size(); i++) {
54 int k = i+1;
55 double phi = k*Om*t;
56 y += a(i)*cos(phi)+b(i)*sin(phi);
57 }
58 return FromDouble<Ret>::cast(y);
59 }
60 typename B::DRetDArg parDer(const Arg &t_) override {
61 double t = ToDouble<Arg>::cast(t_);
62 double yd = 0;
63 double Om = 2.*M_PI*f;
64 for(int i=0; i<a.size(); i++) {
65 int k = i+1;
66 double phi = k*Om*t;
67 yd += k*Om*(b(i)*cos(phi)-a(i)*sin(phi));
68 }
69 return FromDouble<typename B::DRetDArg>::cast(yd);
70 }
71 void initializeUsingXML(xercesc::DOMElement * element) override {
72 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"frequency");
73 f=MBXMLUtils::E(e)->getText<double>();
74 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
75 if(e) a0=MBXMLUtils::E(e)->getText<double>();
76 e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a");
77 if(e) {
78 seta(MBXMLUtils::E(e)->getText<fmatvec::Vec>());
79 e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"b");
80 setb(MBXMLUtils::E(e)->getText<fmatvec::Vec>());
81 }
82 e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"ab");
83 if(e) setab(MBXMLUtils::E(e)->getText<fmatvec::Mat>());
84 e = MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"amplitudePhaseForm");
85 if(e) amplitudePhaseAngleForm = MBXMLUtils::E(e)->getText<bool>();
86 }
87 void init(Element::InitStage stage, const InitConfigSet &config) override {
88 Function<Ret(Arg)>::init(stage, config);
89 if(stage == Element::preInit) {
90 if(amplitudePhaseAngleForm) {
91 for(int i=0; i<a.size(); i++) {
92 double buf = a.e(i);
93 a(i) = buf*cos(b(i));
94 b(i) = buf*sin(b(i));
95 }
96 }
97 }
98 }
99 protected:
100 double f;
101 double a0;
102 fmatvec::VecV a, b;
103 bool amplitudePhaseAngleForm;
104 int size;
105 private:
106 };
107
108 }
109
110 #endif
MBSim::ToDouble
Definition: utils.h:61
MBSim::FourierFunction
Definition: fourier_function.h:28
MBSim::Element::InitStage
InitStage
The stages of the initialization.
Definition: element.h:62
MBSim::FromDouble
Definition: utils.h:89
MBSim::FourierFunction< Ret(Arg)>::init
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: fourier_function.h:87
MBSim::Function
Definition: contact_kinematics.h:32
MBSim::Element::preInit
Definition: element.h:64
MBSim
namespace MBSim
Definition: bilateral_constraint.cc:30