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  auto definition=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"definition");
48  if(!definition) { // MISSING SymbolicFunction depr: remove
49  MBXMLUtils::Deprecated::message(this->msg(fmatvec::Atom::Deprecated),
50  "Symbolic functions must be defined inside of a <definition> child element.", element);
51  definition=element;
52  }
53 
54  std::stringstream func(MBXMLUtils::E(definition)->getText<std::string>());
55  char buf[3];
56  func.read(buf, 2); buf[2]=0;
57  if(buf!=std::string("f("))
58  throw MBXMLUtils::DOMEvalException("Function does not start with 'f(': "+
59  MBXMLUtils::E(definition)->getText<std::string>(), definition);
60  typename fmatvec::SymbolicFunction<Ret(Arg)>::ArgS argS;
61  func >> argS;
62  this->setIndependentVariable(argS);
63  func.read(buf, 2); buf[2]=0;
64  if(buf!=std::string(")="))
65  throw MBXMLUtils::DOMEvalException("Function does not end with ')=': "+
66  MBXMLUtils::E(definition)->getText<std::string>(), definition);
67  typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS retS;
68  func >> retS;
69  this->setDependentFunction(retS);
70 
71  // check symbolic function arguments: we need to throw errors during initializeUsingXML to enable the ObjectFactory
72  // to test other possible combinations (more general ones)
73 // checkFunctionIODim();
74  }
75 
76  private:
77 
78 // void checkFunctionIODim() {
79 // // check function <-> template argument dimension
80 // if(this->argSize!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::ArgS>::size1(this->argS)!=this->argSize)
81 // this->throwError("The dimension of the parameter does not match.");
82 // if(this->retSize1!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS>::size1(this->retS)!=this->retSize1)
83 // this->throwError("The output row dimension does not match.");
84 // if(this->retSize2!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg)>::RetS>::size2(this->retS)!=this->retSize2)
85 // this->throwError("The output column dimension does not match.");
86 // }
87  };
88 
89  template<typename Ret, typename Arg1, typename Arg2>
90  class SymbolicFunction<Ret(Arg1, Arg2)> : public Function<Ret(Arg1, Arg2)>,
91  public fmatvec::SymbolicFunction<Ret(Arg1, Arg2)> {
92 
93  public:
94  SymbolicFunction() = default;
95 
96  void init(Element::InitStage stage, const InitConfigSet &config) override {
97  Function<Ret(Arg1, Arg2)>::init(stage, config);
98  if(stage == Element::preInit) {
99  fmatvec::SymbolicFunction<Ret(Arg1, Arg2)>::init();
100 // checkFunctionIODim();
101  }
102  }
103 
104  void initializeUsingXML(xercesc::DOMElement *element) override {
105  Function<Ret(Arg1, Arg2)>::initializeUsingXML(element);
106 
107  auto definition=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"definition");
108  if(!definition) { // MISSING SymbolicFunction depr: remove
109  MBXMLUtils::Deprecated::message(this->msg(fmatvec::Atom::Deprecated),
110  "Symbolic functions must be defined inside of a <definition> child element.", element);
111  definition=element;
112  }
113 
114  std::stringstream func(MBXMLUtils::E(definition)->getText<std::string>());
115  char buf[3];
116  func.read(buf, 2); buf[2]=0;
117  if(buf!=std::string("f("))
118  throw MBXMLUtils::DOMEvalException("Function does not start with 'f(': "+
119  MBXMLUtils::E(definition)->getText<std::string>(), definition);
120  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg1S arg1S;
121  func >> arg1S;
122  this->setIndependentVariable1(arg1S);
123  func.read(buf, 1);
124  if(buf[0]!=',')
125  throw MBXMLUtils::DOMEvalException("Function must have two arguments: "+
126  MBXMLUtils::E(definition)->getText<std::string>(), definition);
127  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg2S arg2S;
128  func >> arg2S;
129  this->setIndependentVariable2(arg2S);
130  func.read(buf, 2); buf[2]=0;
131  if(buf!=std::string(")="))
132  throw MBXMLUtils::DOMEvalException("Function does not end with ')=': "+
133  MBXMLUtils::E(definition)->getText<std::string>(), definition);
134  typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS retS;
135  func >> retS;
136  this->setDependentFunction(retS);
137 
138  // check symbolic function arguments: we need to throw errors during initializeUsingXML to enable the ObjectFactory
139  // to test other possible combinations (more general ones)
140 // checkFunctionIODim();
141  }
142 
143  private:
144 
145 // void checkFunctionIODim() {
146 // // check function <-> template argument dimension
147 // if(this->arg1Size!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg1S>::size1(this->arg1S)!=this->arg1Size)
148 // this->throwError("The dimension of the parameter does not match.");
149 // if(this->arg2Size!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::Arg2S>::size1(this->arg2S)!=this->arg2Size)
150 // this->throwError("The dimension of the parameter does not match.");
151 // if(this->retSize1!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS>::size1(this->retS)!=this->retSize1)
152 // this->throwError("The output row dimension does not match.");
153 // if(this->retSize2!=0 && fmatvec::Helper<typename fmatvec::SymbolicFunction<Ret(Arg1,Arg2)>::RetS>::size2(this->retS)!=this->retSize2)
154 // this->throwError("The output column dimension does not match.");
155 // }
156  };
157 
158 }
159 
160 #endif
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: symbolic_function.h:36
Definition: symbolic_function.h:28
static void message(std::ostream &str, const std::string &msg, const xercesc::DOMElement *e=nullptr)
InitStage
The stages of the initialization.
Definition: element.h:64
Definition: contact_kinematics.h:32
Definition: element.h:66
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: symbolic_function.h:96
namespace MBSim
Definition: bilateral_constraint.cc:29