mbsim  4.0.0
MBSim Kernel
symbolic_function.h
1 /* Copyright (C) 2004-2009 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@googlemail.com
18  */
19 
20 #ifndef _MBSIM_SYMBOLIC_FUNCTION_H_
21 #define _MBSIM_SYMBOLIC_FUNCTION_H_
22 
23 #include <mbsim/functions/function.h>
24 #include <fmatvec/symbolic_function.h>
25 
26 namespace MBSim {
27 
28  template<typename Sig> class SymbolicFunction;
29 
30  template<typename Ret, typename Arg>
31  class SymbolicFunction<Ret(Arg)> : public Function<Ret(Arg)>, public fmatvec::SymbolicFunction<Ret(Arg)> {
32 
33  public:
34  SymbolicFunction() = default;
35 
36  void init(Element::InitStage stage, const InitConfigSet &config) override {
37  Function<Ret(Arg)>::init(stage, config);
38  if(stage == Element::preInit) {
39  fmatvec::SymbolicFunction<Ret(Arg)>::init();
40 // checkFunctionIODim();
41  }
42  }
43 
44  void initializeUsingXML(xercesc::DOMElement *element) override {
45  Function<Ret(Arg)>::initializeUsingXML(element);
46 
47  std::stringstream func(MBXMLUtils::E(element)->getText<std::string>());
48  char buf[3];
49  func.read(buf, 2); buf[2]=0;
50  if(buf!=std::string("f("))
51  throw MBXMLUtils::DOMEvalException("Function does not start with 'f(': "+
52  MBXMLUtils::E(element)->getText<std::string>(), element);
53  typename fmatvec::SymbolicFunction<Ret(Arg)>::ArgS argS;
54  func >> argS;
55  this->setIndependentVariable(argS);
56  func.read(buf, 2); buf[2]=0;
57  if(buf!=std::string(")="))
58  throw MBXMLUtils::DOMEvalException("Function does not end with ')=': "+
59  MBXMLUtils::E(element)->getText<std::string>(), element);
60  typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS retS;
61  func >> retS;
62  this->setDependentFunction(retS);
63 
64  // check symbolic function arguments: we need to throw errors during initializeUsingXML to enable the ObjectFactory
65  // to test other possible combinations (more general ones)
66 // checkFunctionIODim();
67  }
68 
69  private:
70 
71 // void checkFunctionIODim() {
72 // // check function <-> template argument dimension
73 // if(this->argSize!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::ArgS>::size1(this->argS)!=this->argSize)
74 // this->throwError("The dimension of the parameter does not match.");
75 // if(this->retSize1!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS>::size1(this->retS)!=this->retSize1)
76 // this->throwError("The output row dimension does not match.");
77 // if(this->retSize2!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS>::size2(this->retS)!=this->retSize2)
78 // this->throwError("The output column dimension does not match.");
79 // }
80  };
81 
82  template<typename Ret, typename Arg1, typename Arg2>
83  class SymbolicFunction<Ret(Arg1, Arg2)> : public Function<Ret(Arg1, Arg2)>,
84  public fmatvec::SymbolicFunction<Ret(Arg1, Arg2)> {
85 
86  public:
87  SymbolicFunction() = default;
88 
89  void init(Element::InitStage stage, const InitConfigSet &config) override {
90  Function<Ret(Arg1, Arg2)>::init(stage, config);
91  if(stage == Element::preInit) {
92  fmatvec::SymbolicFunction<Ret(Arg1, Arg2)>::init();
93 // checkFunctionIODim();
94  }
95  }
96 
97  void initializeUsingXML(xercesc::DOMElement *element) override {
98  Function<Ret(Arg1, Arg2)>::initializeUsingXML(element);
99 
100  std::stringstream func(MBXMLUtils::E(element)->getText<std::string>());
101  char buf[3];
102  func.read(buf, 2); buf[2]=0;
103  if(buf!=std::string("f("))
104  throw MBXMLUtils::DOMEvalException("Function does not start with 'f(': "+
105  MBXMLUtils::E(element)->getText<std::string>(), element);
106  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg1S arg1S;
107  func >> arg1S;
108  this->setIndependentVariable1(arg1S);
109  func.read(buf, 1);
110  if(buf[0]!=',')
111  throw MBXMLUtils::DOMEvalException("Function must have two arguments: "+
112  MBXMLUtils::E(element)->getText<std::string>(), element);
113  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg2S arg2S;
114  func >> arg2S;
115  this->setIndependentVariable2(arg2S);
116  func.read(buf, 2); buf[2]=0;
117  if(buf!=std::string(")="))
118  throw MBXMLUtils::DOMEvalException("Function does not end with ')=': "+
119  MBXMLUtils::E(element)->getText<std::string>(), element);
120  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS retS;
121  func >> retS;
122  this->setDependentFunction(retS);
123 
124  // check symbolic function arguments: we need to throw errors during initializeUsingXML to enable the ObjectFactory
125  // to test other possible combinations (more general ones)
126 // checkFunctionIODim();
127  }
128 
129  private:
130 
131 // void checkFunctionIODim() {
132 // // check function <-> template argument dimension
133 // if(this->arg1Size!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg1S>::size1(this->arg1S)!=this->arg1Size)
134 // this->throwError("The dimension of the parameter does not match.");
135 // if(this->arg2Size!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg2S>::size1(this->arg2S)!=this->arg2Size)
136 // this->throwError("The dimension of the parameter does not match.");
137 // if(this->retSize1!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS>::size1(this->retS)!=this->retSize1)
138 // this->throwError("The output row dimension does not match.");
139 // if(this->retSize2!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS>::size2(this->retS)!=this->retSize2)
140 // this->throwError("The output column dimension does not match.");
141 // }
142  };
143 
144 }
145 
146 #endif
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: symbolic_function.h:36
Definition: symbolic_function.h:28
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: contact_kinematics.h:32
Definition: element.h:64
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: symbolic_function.h:89
namespace MBSim
Definition: bilateral_constraint.cc:29