mbsim  4.0.0
MBSim Kernel
element.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@googlemail.com
18  */
19 
20 #ifndef _ELEMENT_H_
21 #define _ELEMENT_H_
22 
23 #include "fmatvec/fmatvec.h"
24 #include "fmatvec/atom.h"
25 #include "mbsim/objectfactory.h"
26 #include "mbsim/utils/plotfeatureenum.h"
27 #include "mbsim/utils/initconfigenum.h"
28 #include "mbsim/namespace.h"
29 #include "mbsim/mbsim_event.h"
30 #include <hdf5serie/vectorserie.h>
31 
32 namespace OpenMBV {
33  class Group;
34 }
35 
36 namespace H5 {
37  class Group;
38 }
39 
43 namespace MBSim {
44 
45  extern int baseIndexForPlot;
46 
47  extern const PlotFeatureEnum plotRecursive, openMBV, debug;
48 
49  class DynamicSystemSolver;
50  class Frame;
51 
58  class Element : virtual public fmatvec::Atom {
59  public:
64  enum InitStage {
69  LASTINITSTAGE
70  };
71 
75  Element(const std::string &name);
76 
80  ~Element() override = default;
81 
82 #ifndef SWIG
83  [[noreturn]]
84 #endif
85  void throwError(const std::string &msg) const {
86  throw MBSimError(this, msg);
87  }
88 
93  virtual void setDynamicSystemSolver(DynamicSystemSolver *sys) { ds = sys; }
94 
100  virtual void plot();
101 
107  virtual void plotAtSpecialEvent() { }
108 
112  const std::string& getName() const { return name; }
113 
117  void setName(const std::string &str) { name = str; }
118 
119  // internal function do not use
120  void setPath(const std::string &str) { path=str; }
121 
126 
131  virtual void init(InitStage stage, const InitConfigSet &config);
132 
136  virtual void createPlotGroup();
137 
142  virtual H5::GroupBase *getFramesPlotGroup() { return nullptr; }
143  virtual H5::GroupBase *getContoursPlotGroup() { return nullptr; }
144  virtual H5::GroupBase *getGroupsPlotGroup() { return nullptr; }
145  virtual H5::GroupBase *getObjectsPlotGroup() { return nullptr; }
146  virtual H5::GroupBase *getLinksPlotGroup() { return nullptr; }
147  virtual H5::GroupBase *getConstraintsPlotGroup() { return nullptr; }
148  virtual H5::GroupBase *getObserversPlotGroup() { return nullptr; }
149 
152  bool getPlotFeature(const PlotFeatureEnum &pf) {
153  auto it=plotFeature.find(std::ref(pf));
154  if(it==plotFeature.end())
155  return false;
156  return it->second;
157  }
158 
164  virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value);
165 
171  void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value);
172 
178  void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value) { setPlotFeature(pf,value); setPlotFeatureForChildren(pf,value); }
179 
183  template<class T>
184  void setPlotAttribute(const std::string &name, const T &value) {
185  plotAttribute[name] = value;
186  }
187  void setPlotAttribute(const std::string &name) {
188  plotAttribute[name] = std::monostate();
189  }
190 
191  virtual void initializeUsingXML(xercesc::DOMElement *element);
192 
197  template<class T> T* getByPath(const std::string &path, bool initialCaller=true) const;
198 
204  std::string getPath(const Element *relTo=nullptr, std::string sep="/") const;
205 
209  virtual Element* getChildByContainerAndName(const std::string &container, const std::string &name) const {
210  throwError("This element has no containers with childs.");
211  }
212 
213  virtual std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
214  virtual std::shared_ptr<OpenMBV::Group> getFramesOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
215  virtual std::shared_ptr<OpenMBV::Group> getContoursOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
216  virtual std::shared_ptr<OpenMBV::Group> getGroupsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
217  virtual std::shared_ptr<OpenMBV::Group> getObjectsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
218  virtual std::shared_ptr<OpenMBV::Group> getLinksOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
219  virtual std::shared_ptr<OpenMBV::Group> getConstraintsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
220  virtual std::shared_ptr<OpenMBV::Group> getObserversOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
221 
222  virtual Element* getParent() {return parent;}
223  virtual const Element* getParent() const {return parent;}
224  virtual void setParent(Element* parent_) {parent = parent_;}
225 
230  std::vector<Element*> getDependencies() const { return dependency; }
231 
232  void addDependency(Element* ele) { if(ele) dependency.push_back(ele); }
233 
239  int computeLevel();
240 
241  virtual void updatePositions(Frame *frame) { }
242  virtual void updateVelocities(Frame *frame) { }
243  virtual void updateAccelerations(Frame *frame) { }
244  virtual void updateJacobians(Frame *frame, int j=0) { }
245  virtual void updateGyroscopicAccelerations(Frame *frame) { }
246 
247  virtual void resetUpToDate() { }
248 
249  const double& getTime() const;
250  double getStepSize() const;
251 
252 #ifndef SWIG
253  const MBXMLUtils::DOMEvalException& getDOMEvalError() const { return domEvalError; };
254 #endif
255 
256  protected:
257  Element *parent;
258 
262  std::string name;
263 
269  std::string path;
270 
271 #ifndef SWIG
274 #endif
275 
280 
285 
289  std::vector<double> plotVector;
290 
294  std::vector<std::string> plotColumns;
295 
300 
301  void updatePlotFeatures();
302 
306  std::vector<Element*> dependency;
307 
311  PlotFeatureMap plotFeature, plotFeatureForChildren;
312 
313  std::map<std::string, std::variant<
314  std::monostate,
315  int,
316  double,
317  std::string,
318  std::vector<int>,
319  std::vector<double>,
320  std::vector<std::vector<double>>
321  >> plotAttribute;
322 
323  int convertIndex(int i) { return baseIndexForPlot+i; }
324 
325  void addToPlot(const std::string &name);
326  void addToPlot(const std::string &name, int size);
327  void addToPlot(const std::string &name, const std::vector<std::string> &iname);
328 
329  template<class AT> void plot(const AT &x) {
330  plotVector.push_back(x);
331  }
332  template<class Type, class AT> void plot(const fmatvec::Vector<Type,AT> &x) {
333  for(int i=0; i<x.size(); i++)
334  plotVector.push_back(x(i));
335  }
336  };
337 
338  template<class T>
339  T* Element::getByPath(const std::string &path, bool initialCaller) const {
340  try {
341  if(path.substr(0, 1) == "/") { // if absolute path ...
342  if(parent) // .. and a parent exists ...
343  return parent->getByPath<T>(path, false); // ... than call getByPath of the parent (walk to the top)
344  else // .. and no parent exits ...
345  return getByPath<T>(path.substr(1), false); // ... we are at top and call getByPath again with the leading "/" removed (call relative to top)
346  }
347  else if (path.substr(0, 3) == "../") { // if relative path to parent ...
348  if(!parent)
349  throwError("Reference to parent '"+path+"' requested but already at the root node.");
350  return parent->getByPath<T>(path.substr(3), false); // ... call getByPath of the parent with the leading "../" removed
351  }
352  else { // if relative path to a child ...
353  // extract the first path and all other paths (rest)
354  size_t idx=path.find('/');
355  std::string first=path.substr(0, idx);
356  std::string rest;
357  if(idx!=std::string::npos)
358  rest=path.substr(idx+1);
359  // get the object of the first child path by calling the virtual function getChildByContainerAndName
360  size_t pos0=first.find('[');
361  if(pos0==std::string::npos)
362  throwError("Syntax error in subreference '"+first+"': no [ found.");
363  std::string container=first.substr(0, pos0);
364  if(first[first.size()-1]!=']')
365  throwError("Syntax error in subreference '"+first+"': not ending with ].");
366  std::string name=first.substr(pos0+1, first.size()-pos0-2);
367  Element *e=getChildByContainerAndName(container, name);
368  // if their are other child paths call getByPath of e for this
369  if(!rest.empty())
370  return e->getByPath<T>(rest, false);
371  // this is the last relative path -> check type and return
372  auto *t=dynamic_cast<T*>(e);
373  if(t)
374  return t;
375  else
376  throwError("Cannot cast this element to type "+container+".");
377  }
378  }
379  catch(MBSimError &ex) {
380  if(initialCaller)
381  throwError("Evaluation of refernece '"+path+"' failed: Message from "+
382  ex.getPath()+": "+ex.getErrorMessage());
383  else
384  throw ex;
385  }
386  }
387 
388 }
389 
390 #endif
virtual void createPlotGroup()
creates the plotGroup for H5-output
Definition: element.cc:138
std::vector< Element * > getDependencies() const
checks dependency on other elements.
Definition: element.h:230
virtual void plot()
plots time dependent data
Definition: element.cc:71
virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value)
Set a plot feature.
Definition: element.cc:233
bool getPlotFeature(const PlotFeatureEnum &pf)
Definition: element.h:152
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:82
DynamicSystemSolver * getDynamicSystemSolver()
Definition: element.h:125
solver interface for modelling and simulation of dynamic systems
Definition: dynamic_system_solver.h:58
Definition: element.h:65
void setPlotAttribute(const std::string &name, const T &value)
Set a plot attribute: static data attached as key/value pairs to the plot datasets/groups.
Definition: element.h:184
std::vector< Element * > dependency
vector containing all dependencies.
Definition: element.h:306
virtual void plotAtSpecialEvent()
plots time dependent data at special events
Definition: element.h:107
PlotFeatureMap plotFeature
plot feature
Definition: element.h:311
H5::VectorSerie< double > * plotVectorSerie
time series
Definition: element.h:284
void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value)
Set a plot feature for this object and the children of this object.
Definition: element.h:178
basic class of MBSim mainly for plotting
Definition: element.h:58
std::string path
The path of this object. Is set during the init stage reorganizeHierarchy. Before this the path is ca...
Definition: element.h:269
DynamicSystemSolver * ds
dynamic system
Definition: element.h:279
H5::GroupBase * getPlotGroup()
Definition: element.h:141
void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value)
Set a plot feature for the children of this object.
Definition: element.cc:237
std::vector< std::string > plotColumns
columns of time series
Definition: element.h:294
Definition: plotfeatureenum.h:29
std::string getPath(const Element *relTo=nullptr, std::string sep="/") const
Return the path of this object. If relativeTo is not NULL return a relative path to relativeTo....
Definition: element.cc:173
Definition: element.h:67
InitStage
The stages of the initialization.
Definition: element.h:64
virtual Element * getChildByContainerAndName(const std::string &container, const std::string &name) const
Get the Element named name in the container named container.
Definition: element.h:209
std::vector< double > plotVector
one entry of time series
Definition: element.h:289
const std::string & getName() const
Definition: element.h:112
~Element() override=default
destructor
std::string name
name of element
Definition: element.h:262
MBXMLUtils::DOMEvalException domEvalError
Special XML helper variable.
Definition: element.h:273
Definition: element.h:66
virtual void setDynamicSystemSolver(DynamicSystemSolver *sys)
sets the used dynamics system solver to the element
Definition: element.h:93
Definition: element.h:68
H5::GroupBase * plotGroup
associated plot group
Definition: element.h:299
void setName(const std::string &str)
Definition: element.h:117
basic error class for mbsim
Definition: mbsim_event.h:39
int computeLevel()
computes the length of the pathes in the graph that represents the dependencies between all elements.
Definition: element.cc:286
T * getByPath(const std::string &path, bool initialCaller=true) const
Get the object of type T represented by the path path. Do not set any argurment other than path!
Definition: element.h:339
Element(const std::string &name)
constructor
Definition: element.cc:68
namespace MBSim
Definition: bilateral_constraint.cc:29