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#include <boost/core/demangle.hpp>
32
33namespace OpenMBV {
34 class Group;
35}
36
37namespace H5 {
38 class Group;
39}
40
44namespace MBSim {
45
46 extern const PlotFeatureEnum plotRecursive, openMBV, debug;
47
48 class DynamicSystemSolver;
49 class Frame;
50
57 class Element : virtual public fmatvec::Atom {
58 public:
63 enum InitStage {
68 LASTINITSTAGE
69 };
70
74 Element(const std::string &name);
75
79 ~Element() override = default;
80
81#ifndef SWIG
82 [[noreturn]]
83#endif
84 void throwError(const std::string &msg) const {
85 throw MBSimError(this, msg);
86 }
87
92 virtual void setDynamicSystemSolver(DynamicSystemSolver *sys) { ds = sys; }
93
99 virtual void plot();
100
106 virtual void plotAtSpecialEvent() { }
107
111 const std::string& getName() const { return name; }
112
116 void setName(const std::string &str) { name = str; }
117
118 // internal function do not use
119 void setPath(const std::string &str) { path=str; }
120
125
130 virtual void init(InitStage stage, const InitConfigSet &config=InitConfigSet());
131
135 virtual void createPlotGroup();
136
141 virtual H5::GroupBase *getFramesPlotGroup() { return nullptr; }
142 virtual H5::GroupBase *getContoursPlotGroup() { return nullptr; }
143 virtual H5::GroupBase *getGroupsPlotGroup() { return nullptr; }
144 virtual H5::GroupBase *getObjectsPlotGroup() { return nullptr; }
145 virtual H5::GroupBase *getLinksPlotGroup() { return nullptr; }
146 virtual H5::GroupBase *getConstraintsPlotGroup() { return nullptr; }
147 virtual H5::GroupBase *getObserversPlotGroup() { return nullptr; }
148
152 auto it=plotFeature.find(std::ref(pf));
153 if(it==plotFeature.end())
154 return false;
155 return it->second;
156 }
157
163 virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value);
164
170 void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value);
171
177 void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value) { setPlotFeature(pf,value); setPlotFeatureForChildren(pf,value); }
178
179 enum class PlotAttributeStorage { attribute, dataset };
180
184 template<class T>
185 void setPlotAttribute(const std::string &name, const T &value, PlotAttributeStorage storage=PlotAttributeStorage::attribute) {
186 plotAttribute.emplace(name, decltype(plotAttribute)::mapped_type{storage, value});
187 }
188 void setPlotAttribute(const std::string &name, PlotAttributeStorage storage=PlotAttributeStorage::attribute) {
189 plotAttribute.emplace(name, decltype(plotAttribute)::mapped_type{storage, std::monostate()});
190 }
191
192 virtual void initializeUsingXML(xercesc::DOMElement *element);
193
198 template<class T> T* getByPath(const std::string &path, bool initialCaller=true) const;
199
205 std::string getPath(const Element *relTo=nullptr, std::string sep="/") const;
206
210 virtual Element* getChildByContainerAndName(const std::string &container, const std::string &name) const {
211 throwError("This element has no containers with childs.");
212 }
213
214 virtual std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
215 virtual std::shared_ptr<OpenMBV::Group> getFramesOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
216 virtual std::shared_ptr<OpenMBV::Group> getContoursOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
217 virtual std::shared_ptr<OpenMBV::Group> getGroupsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
218 virtual std::shared_ptr<OpenMBV::Group> getObjectsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
219 virtual std::shared_ptr<OpenMBV::Group> getLinksOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
220 virtual std::shared_ptr<OpenMBV::Group> getConstraintsOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
221 virtual std::shared_ptr<OpenMBV::Group> getObserversOpenMBVGrp() { return std::shared_ptr<OpenMBV::Group>(); }
222
223 virtual Element* getParent() {return parent;}
224 virtual const Element* getParent() const {return parent;}
225 virtual void setParent(Element* parent_) {parent = parent_;}
226
231 std::vector<Element*> getDependencies() const { return dependency; }
232
233 void addDependency(Element* ele) { if(ele) dependency.push_back(ele); }
234
240 int computeLevel();
241
242 virtual void updatePositions(Frame *frame) { }
243 virtual void updateVelocities(Frame *frame) { }
244 virtual void updateAccelerations(Frame *frame) { }
245 virtual void updateJacobians(Frame *frame, int j=0) { }
246 virtual void updateGyroscopicAccelerations(Frame *frame) { }
247
248 virtual void resetUpToDate() { }
249
250 const double& getTime() const;
251 double getStepSize() const;
252
253#ifndef SWIG
254 const MBXMLUtils::DOMEvalException& getDOMEvalError() const { return domEvalError; };
255#endif
256
257 protected:
258 Element *parent { nullptr };
259
263 std::string name;
264
270 std::string path;
271
272#ifndef SWIG
275#endif
276
281
286
290 std::vector<double> plotVector;
291
295 std::vector<std::string> plotColumns;
296
301
302 void updatePlotFeatures();
303
307 std::vector<Element*> dependency;
308
312 PlotFeatureMap plotFeature, plotFeatureForChildren;
313
314 std::map<std::string, std::pair<PlotAttributeStorage, std::variant<
315 std::monostate,
316 int,
317 double,
318 std::string,
319 std::vector<int>,
320 std::vector<double>,
321 std::vector<std::vector<double>>
322 >>> plotAttribute;
323
324 public: // addToPlot / plot is public to allow helper classes/functions to handle plots in a element
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 private:
337 Element* getByPathElement(const std::string &path, bool initialCaller=true) const;
338 };
339
340 template<class T>
341 T* Element::getByPath(const std::string &path, bool initialCaller) const {
342 Element *e = getByPathElement(path, initialCaller);
343 auto *t=dynamic_cast<T*>(e);
344 if(t)
345 return t;
346 else
347 throwError(std::string("Cannot cast this element to type ")+boost::core::demangle(typeid(T).name())+".");
348 }
349
350}
351
352#endif
solver interface for modelling and simulation of dynamic systems
Definition: dynamic_system_solver.h:61
basic class of MBSim mainly for plotting
Definition: element.h:57
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:212
H5::GroupBase * plotGroup
associated plot group
Definition: element.h:300
virtual void plot()
plots time dependent data
Definition: element.cc:75
std::vector< Element * > dependency
vector containing all dependencies.
Definition: element.h:307
std::vector< Element * > getDependencies() const
checks dependency on other elements.
Definition: element.h:231
virtual void setPlotFeature(const PlotFeatureEnum &pf, bool value)
Set a plot feature.
Definition: element.cc:276
Element(const std::string &name)
constructor
Definition: element.cc: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:210
virtual void init(InitStage stage, const InitConfigSet &config=InitConfigSet())
plots time series header
Definition: element.cc:86
void setPlotFeatureRecursive(const PlotFeatureEnum &pf, bool value)
Set a plot feature for this object and the children of this object.
Definition: element.h:177
bool getPlotFeature(const PlotFeatureEnum &pf)
Definition: element.h:151
virtual void setDynamicSystemSolver(DynamicSystemSolver *sys)
sets the used dynamics system solver to the element
Definition: element.h:92
~Element() override=default
destructor
virtual void plotAtSpecialEvent()
plots time dependent data at special events
Definition: element.h:106
InitStage
The stages of the initialization.
Definition: element.h:63
@ plotting
Definition: element.h:66
@ unknownStage
Definition: element.h:67
@ resolveStringRef
Definition: element.h:64
@ preInit
Definition: element.h:65
H5::GroupBase * getPlotGroup()
Definition: element.h:140
virtual void createPlotGroup()
creates the plotGroup for H5-output
Definition: element.cc:174
void setPlotAttribute(const std::string &name, const T &value, PlotAttributeStorage storage=PlotAttributeStorage::attribute)
Set a plot attribute: static data attached as key/value pairs to the plot datasets/groups.
Definition: element.h:185
std::string path
The path of this object. Is set during the init stage reorganizeHierarchy. Before this the path is ca...
Definition: element.h:270
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:341
std::vector< double > plotVector
one entry of time series
Definition: element.h:290
H5::VectorSerie< double > * plotVectorSerie
time series
Definition: element.h:285
std::vector< std::string > plotColumns
columns of time series
Definition: element.h:295
MBXMLUtils::DOMEvalException domEvalError
Special XML helper variable.
Definition: element.h:274
std::string name
name of element
Definition: element.h:263
const std::string & getName() const
Definition: element.h:111
PlotFeatureMap plotFeature
plot feature
Definition: element.h:312
DynamicSystemSolver * ds
dynamic system
Definition: element.h:280
DynamicSystemSolver * getDynamicSystemSolver()
Definition: element.h:124
int computeLevel()
computes the length of the pathes in the graph that represents the dependencies between all elements.
Definition: element.cc:334
void setPlotFeatureForChildren(const PlotFeatureEnum &pf, bool value)
Set a plot feature for the children of this object.
Definition: element.cc:280
void setName(const std::string &str)
Definition: element.h:116
basic error class for mbsim
Definition: mbsim_event.h:39
Definition: plotfeatureenum.h:29
namespace MBSim
Definition: bilateral_constraint.cc:30