All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
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 const PlotFeatureEnum plotRecursive, openMBV, debug, separateFilePerGroup, energy;
46 
47  class DynamicSystemSolver;
48  class Frame;
49 
56  class Element : virtual public fmatvec::Atom {
57  public:
62  enum InitStage {
67  LASTINITSTAGE
68  };
69 
73  Element(const std::string &name);
74 
78  ~Element() override = default;
79 
80 #ifndef SWIG
81  [[noreturn]]
82 #endif
83  void throwError(const std::string &msg) const {
84  throw MBSimError(this, msg);
85  }
86 
91  virtual void setDynamicSystemSolver(DynamicSystemSolver *sys) { ds = sys; }
92 
98  virtual void plot();
99 
105  virtual void plotAtSpecialEvent() { }
106 
110  const std::string& getName() const { return name; }
111 
115  void setName(const std::string &str) { name = str; }
116 
117  // internal function do not use
118  void setPath(const std::string &str) { path=str; }
119 
124 
129  virtual void init(InitStage stage, const InitConfigSet &config);
130 
134  virtual void createPlotGroup();
135 
140 
143  bool getPlotFeature(const PlotFeatureEnum &pf) {
144  auto it=plotFeature.find(std::ref(pf));
145  if(it==plotFeature.end())
146  return false;
147  return it->second;
148  }
149 
155  virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value);
156 
162  void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value);
163 
169  void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value) { setPlotFeature(pf,value); setPlotFeatureForChildren(pf,value); }
170 
171  virtual void initializeUsingXML(xercesc::DOMElement *element);
172 
177  template<class T> T* getByPath(const std::string &path, bool initialCaller=true) const;
178 
184  std::string getPath(const Element *relTo=nullptr, std::string sep="/") const;
185 
189  virtual Element* getChildByContainerAndName(const std::string &container, const std::string &name) const {
190  throwError("This element has no containers with childs.");
191  }
192 
193  virtual std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() {return std::shared_ptr<OpenMBV::Group>();}
194 
195  virtual Element* getParent() {return parent;}
196  virtual const Element* getParent() const {return parent;}
197  virtual void setParent(Element* parent_) {parent = parent_;}
198 
203  std::vector<Element*> getDependencies() const { return dependency; }
204 
205  void addDependency(Element* ele) { if(ele) dependency.push_back(ele); }
206 
212  int computeLevel();
213 
214  virtual void updatePositions(Frame *frame) { }
215  virtual void updateVelocities(Frame *frame) { }
216  virtual void updateAccelerations(Frame *frame) { }
217  virtual void updateJacobians(Frame *frame, int j=0) { }
218  virtual void updateGyroscopicAccelerations(Frame *frame) { }
219 
220  virtual void resetUpToDate() { }
221 
222  const double& getTime() const;
223  double getStepSize() const;
224 
225 #ifndef SWIG
226  const MBXMLUtils::DOMEvalException& getDOMEvalError() const { return domEvalError; };
227 #endif
228 
229  protected:
230  Element *parent;
231 
235  std::string name;
236 
242  std::string path;
243 
244 #ifndef SWIG
247 #endif
248 
253 
258 
262  std::vector<double> plotVector;
263 
267  std::vector<std::string> plotColumns;
268 
273 
274  void updatePlotFeatures();
275 
279  std::vector<Element*> dependency;
280 
284  PlotFeatureMap plotFeature, plotFeatureForChildren;
285  };
286 
287  template<class T>
288  T* Element::getByPath(const std::string &path, bool initialCaller) const {
289  try {
290  if(path.substr(0, 1) == "/") { // if absolute path ...
291  if(parent) // .. and a parent exists ...
292  return parent->getByPath<T>(path, false); // ... than call getByPath of the parent (walk to the top)
293  else // .. and no parent exits ...
294  return getByPath<T>(path.substr(1), false); // ... we are at top and call getByPath again with the leading "/" removed (call relative to top)
295  }
296  else if (path.substr(0, 3) == "../") { // if relative path to parent ...
297  if(!parent)
298  throwError("Reference to parent '"+path+"' requested but already at the root node.");
299  return parent->getByPath<T>(path.substr(3), false); // ... call getByPath of the parent with the leading "../" removed
300  }
301  else { // if relative path to a child ...
302  // extract the first path and all other paths (rest)
303  size_t idx=path.find('/');
304  std::string first=path.substr(0, idx);
305  std::string rest;
306  if(idx!=std::string::npos)
307  rest=path.substr(idx+1);
308  // get the object of the first child path by calling the virtual function getChildByContainerAndName
309  size_t pos0=first.find('[');
310  if(pos0==std::string::npos)
311  throwError("Syntax error in subreference '"+first+"': no [ found.");
312  std::string container=first.substr(0, pos0);
313  if(first[first.size()-1]!=']')
314  throwError("Syntax error in subreference '"+first+"': not ending with ].");
315  std::string name=first.substr(pos0+1, first.size()-pos0-2);
316  Element *e=getChildByContainerAndName(container, name);
317  // if their are other child paths call getByPath of e for this
318  if(!rest.empty())
319  return e->getByPath<T>(rest, false);
320  // this is the last relative path -> check type and return
321  auto *t=dynamic_cast<T*>(e);
322  if(t)
323  return t;
324  else
325  throwError("Cannot cast this element to type "+container+".");
326  }
327  }
328  catch(MBSimError &ex) {
329  if(initialCaller)
330  throwError("Evaluation of refernece '"+path+"' failed: Message from "+
331  ex.getPath()+": "+ex.getErrorMessage());
332  else
333  throw ex;
334  }
335  }
336 
337 }
338 
339 #endif
std::vector< Element * > getDependencies() const
checks dependency on other elements.
Definition: element.h:203
virtual void createPlotGroup()
creates the plotGroup for H5-output
Definition: element.cc:119
virtual void plot()
plots time dependent data
Definition: element.cc:75
virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value)
Set a plot feature.
Definition: element.cc:215
bool getPlotFeature(const PlotFeatureEnum &pf)
Definition: element.h:143
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:86
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:189
DynamicSystemSolver * getDynamicSystemSolver()
Definition: element.h:123
solver interface for modelling and simulation of dynamic systems
Definition: dynamic_system_solver.h:46
Definition: element.h:63
std::vector< Element * > dependency
vector containing all dependencies.
Definition: element.h:279
virtual void plotAtSpecialEvent()
plots time dependent data at special events
Definition: element.h:105
PlotFeatureMap plotFeature
plot feature
Definition: element.h:284
H5::VectorSerie< double > * plotVectorSerie
time series
Definition: element.h:257
void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value)
Set a plot feature for this object and the children of this object.
Definition: element.h:169
basic class of MBSim mainly for plotting
Definition: element.h:56
std::string path
The path of this object. Is set during the init stage reorganizeHierarchy. Before this the path is ca...
Definition: element.h:242
DynamicSystemSolver * ds
dynamic system
Definition: element.h:252
H5::GroupBase * getPlotGroup()
Definition: element.h:139
void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value)
Set a plot feature for the children of this object.
Definition: element.cc:219
std::vector< std::string > plotColumns
columns of time series
Definition: element.h:267
Definition: plotfeatureenum.h:29
Definition: element.h:65
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:155
InitStage
The stages of the initialization.
Definition: element.h:62
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:288
std::vector< double > plotVector
one entry of time series
Definition: element.h:262
~Element() override=default
destructor
std::string name
name of element
Definition: element.h:235
MBXMLUtils::DOMEvalException domEvalError
Special XML helper variable.
Definition: element.h:246
Definition: element.h:64
const std::string & getName() const
Definition: element.h:110
virtual void setDynamicSystemSolver(DynamicSystemSolver *sys)
sets the used dynamics system solver to the element
Definition: element.h:91
Definition: element.h:66
H5::GroupBase * plotGroup
associated plot group
Definition: element.h:272
void setName(const std::string &str)
Definition: element.h:115
basic error class for mbsim
Definition: mbsim_event.h:39
std::ostream & msg(MsgType type) const
int computeLevel()
computes the length of the pathes in the graph that represents the dependencies between all elements...
Definition: element.cc:245
Element(const std::string &name)
constructor
Definition: element.cc:72

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home