00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _ELEMENT_H_
00022 #define _ELEMENT_H_
00023
00024 #include "fmatvec/fmatvec.h"
00025 #include "fmatvec/atom.h"
00026 #include "mbsim/objectfactory.h"
00027 #include <string>
00028 #include <hdf5serie/vectorserie.h>
00029 #include <mbxmlutilshelper/dom.h>
00030
00031 #include "mbsim_event.h"
00032
00033 #ifdef HAVE_OPENMBVCPPINTERFACE
00034 namespace OpenMBV {
00035 class Group;
00036 }
00037 #endif
00038
00039 namespace H5 {
00040 class Group;
00041 }
00042
00046 namespace MBSim {
00047
00048 const MBXMLUtils::NamespaceURI MBSIM("http://mbsim.berlios.de/MBSim");
00049
00050 class DynamicSystemSolver;
00051
00058 class Element : virtual public fmatvec::Atom {
00059 public:
00061 enum PlotFeatureStatus {
00062 enabled,
00063 disabled,
00064 unset
00065 };
00066
00067
00068
00069
00070
00071
00072
00074 enum PlotFeature {
00075 plotRecursive=0,
00076 separateFilePerGroup,
00077 state,
00078 stateDerivative,
00079 notMinimalState,
00080 rightHandSide,
00081 globalPosition,
00082 globalVelocity,
00083 globalAcceleration,
00084 energy,
00085 openMBV,
00086 generalizedLinkForce,
00087 linkKinematics,
00088 stopVector,
00089 debug,
00090 LASTPLOTFEATURE
00091 };
00092
00097 enum InitStage {
00098 modelBuildup=0,
00099 resolveXMLPath,
00100 preInit,
00101 resize,
00102 relativeFrameContourLocation,
00103 worldFrameContourLocation,
00104 plotting,
00105 reorganizeHierarchy,
00106 unknownStage,
00107 calculateLocalInitialValues,
00108 LASTINITSTAGE
00109 };
00110
00114 Element(const std::string &name);
00115
00119 virtual ~Element();
00120
00121
00125 virtual void initDataInterfaceBase(DynamicSystemSolver *parentds) {};
00126
00130 virtual std::string getType() const { return "Element"; }
00131
00136 virtual void setDynamicSystemSolver(DynamicSystemSolver *sys) { ds = sys; }
00137
00143 virtual void plot(double t, double dt = 1);
00144
00150 virtual void plotAtSpecialEvent(double t, double dt = 1) {}
00151
00155 virtual void closePlot();
00156
00157
00161 const std::string& getName() const { return name; }
00162
00166 void setName(const std::string &str) { name = str; }
00167
00168
00169 void setPath(const std::string &str) { path=str; }
00170
00174 DynamicSystemSolver* getDynamicSystemSolver() { return ds; }
00175
00180 virtual void init(InitStage stage);
00181
00185 virtual void createPlotGroup();
00186
00190 H5::GroupBase *getPlotGroup() { return plotGroup; }
00191
00198 virtual void setPlotFeature(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; }
00199
00205 void setPlotFeatureForChildren(PlotFeature pf, PlotFeatureStatus value) { plotFeatureForChildren[pf]=value; }
00206
00212 void setPlotFeatureRecursive(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; plotFeatureForChildren[pf]=value; }
00213
00217 PlotFeatureStatus getPlotFeature(PlotFeature pf) { return plotFeature[pf]; }
00218
00222 PlotFeatureStatus getPlotFeatureForChildren(PlotFeature pf) { return plotFeatureForChildren[pf]; }
00223
00224 virtual void initializeUsingXML(xercesc::DOMElement *element);
00225 virtual xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *element);
00226
00227 const std::vector<MBXMLUtils::EmbedDOMLocator>& getLocationStack() const { return locationStack; }
00228
00233 template<class T> T* getByPath(const std::string &path, bool initialCaller=true) const;
00234
00240 std::string getPath(const Element *relTo=NULL, std::string sep="/") const;
00241
00245 virtual Element* getChildByContainerAndName(const std::string &container, const std::string &name) const {
00246 THROW_MBSIMERROR("This element has no containers with childs.");
00247 }
00248
00249
00250 static double getDouble(xercesc::DOMElement *e);
00251 static int getInt(xercesc::DOMElement *e);
00252 static bool getBool(xercesc::DOMElement *e);
00253 static fmatvec::Vec3 getVec3(xercesc::DOMElement *e);
00254 static fmatvec::Vec getVec(xercesc::DOMElement *e, int rows=0);
00255 static fmatvec::Mat3xV getMat3xV(xercesc::DOMElement *e, int cols=0);
00256 static fmatvec::Mat getMat(xercesc::DOMElement *e, int rows=0, int cols=0);
00257 static fmatvec::SqrMat3 getSqrMat3(xercesc::DOMElement *e);
00258 static fmatvec::SqrMat getSqrMat(xercesc::DOMElement *e, int size=0);
00259 static fmatvec::SymMat3 getSymMat3(xercesc::DOMElement *e);
00260 static fmatvec::SymMat getSymMat(xercesc::DOMElement *e, int size=0);
00261
00262 #ifdef HAVE_OPENMBVCPPINTERFACE
00263 virtual boost::shared_ptr<OpenMBV::Group> getOpenMBVGrp() {return boost::shared_ptr<OpenMBV::Group>();}
00264 #endif
00265
00266 virtual Element* getParent() {return parent;}
00267 virtual const Element* getParent() const {return parent;}
00268 virtual void setParent(Element* parent_) {parent = parent_;}
00269
00274 std::vector<Element*> getElementsDependingOn() const { return dependency; }
00275
00276 void addDependency(Element* ele) { dependency.push_back(ele); }
00282 int computeLevel();
00283
00284 protected:
00285 Element *parent;
00286
00290 std::string name;
00291
00297 std::string path;
00298
00299 std::vector<MBXMLUtils::EmbedDOMLocator> locationStack;
00300
00304 DynamicSystemSolver *ds;
00305
00309 H5::VectorSerie<double> *plotVectorSerie;
00310
00314 std::vector<double> plotVector;
00315
00319 std::vector<std::string> plotColumns;
00320
00324 H5::GroupBase *plotGroup;
00325
00326 void updatePlotFeatures();
00327
00331 std::vector<Element*> dependency;
00332
00333 private:
00337 PlotFeatureStatus plotFeature[LASTPLOTFEATURE], plotFeatureForChildren[LASTPLOTFEATURE];
00338 };
00339
00340 template<class T>
00341 T* Element::getByPath(const std::string &path, bool initialCaller) const {
00342 try {
00343 if(path.substr(0, 1) == "/") {
00344 if(parent)
00345 return parent->getByPath<T>(path, false);
00346 else
00347 return getByPath<T>(path.substr(1), false);
00348 }
00349 else if (path.substr(0, 3) == "../")
00350 return parent->getByPath<T>(path.substr(3), false);
00351 else {
00352
00353 size_t idx=path.find('/');
00354 std::string first=path.substr(0, idx);
00355 std::string rest;
00356 if(idx!=std::string::npos)
00357 rest=path.substr(idx+1);
00358
00359 size_t pos0=first.find('[');
00360 if(pos0==std::string::npos)
00361 THROW_MBSIMERROR("Syntax error in subreference '"+first+"': no [ found.");
00362 std::string container=first.substr(0, pos0);
00363 if(first[first.size()-1]!=']')
00364 THROW_MBSIMERROR("Syntax error in subreference '"+first+"': not ending with ].");
00365 std::string name=first.substr(pos0+1, first.size()-pos0-2);
00366 Element *e=getChildByContainerAndName(container, name);
00367
00368 if(!rest.empty())
00369 return e->getByPath<T>(rest, false);
00370
00371 T *t=dynamic_cast<T*>(e);
00372 if(t)
00373 return t;
00374 else
00375 THROW_MBSIMERROR("Cannot cast this element to type "+container+".");
00376 }
00377 }
00378 catch(MBSimError &ex) {
00379 if(initialCaller)
00380 THROW_MBSIMERROR("Evaluation of refernece '"+path+"' failed: Message from "+
00381 ex.getPath()+": "+ex.getErrorMessage());
00382 else
00383 throw ex;
00384 }
00385 }
00386
00387
00388 }
00389
00390 #endif