MBSim

Project Page
element.h
00001 /* Copyright (C) 2004-2014 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  *          rzander@users.berlios.de
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       // NOTE!!! When adding a new PlotFeature here, the default setting for this feature must
00068       // be specified in dynamic_system_solver.cc:DynamicSystemSolver::constructor() and the
00069       // new feature must also be added in
00070       // element.cc:Element::initializeUsingXML(xercesc::DOMElement *element)
00071       // and in
00072       // mbsimxml/schema/mbsim.xsd.in
00074       enum PlotFeature {
00075         plotRecursive=0, 
00076         separateFilePerGroup, 
00077         state, 
00078         stateDerivative, 
00079         notMinimalState, // TODO
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, /*< build up complex internal models */
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       /* INTERFACE */
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       // internal function do not use
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 
00231       template<class T> T* getByPath(const std::string &path, bool initialCaller=true) const;
00232 
00238       std::string getPath(const Element *relTo=NULL, std::string sep="/") const;
00239 
00243       virtual Element* getChildByContainerAndName(const std::string &container, const std::string &name) const {
00244         THROW_MBSIMERROR("This element has no containers with childs.");
00245       }
00246 
00247       // some convenience function for XML
00248       static double getDouble(xercesc::DOMElement *e);
00249       static int getInt(xercesc::DOMElement *e);
00250       static bool getBool(xercesc::DOMElement *e);
00251       static fmatvec::Vec3 getVec3(xercesc::DOMElement *e);
00252       static fmatvec::Vec getVec(xercesc::DOMElement *e, int rows=0);
00253       static fmatvec::Mat3xV getMat3xV(xercesc::DOMElement *e, int cols=0);
00254       static fmatvec::Mat getMat(xercesc::DOMElement *e, int rows=0, int cols=0);
00255       static fmatvec::SqrMat3 getSqrMat3(xercesc::DOMElement *e);
00256       static fmatvec::SqrMat getSqrMat(xercesc::DOMElement *e, int size=0);
00257       static fmatvec::SymMat3 getSymMat3(xercesc::DOMElement *e);
00258       static fmatvec::SymMat getSymMat(xercesc::DOMElement *e, int size=0);
00259 
00260 #ifdef HAVE_OPENMBVCPPINTERFACE
00261       virtual OpenMBV::Group* getOpenMBVGrp() {return 0;}
00262 #endif
00263 
00264       virtual Element* getParent() {return parent;}
00265       virtual const Element* getParent() const {return parent;}
00266       virtual void setParent(Element* parent_) {parent = parent_;}
00267 
00268     protected:
00269       Element *parent;
00270 
00274       std::string name;
00275 
00281       std::string path;
00282 
00286       DynamicSystemSolver *ds;
00287 
00291       H5::VectorSerie<double> *plotVectorSerie;
00292 
00296       std::vector<double> plotVector;
00297 
00301       std::vector<std::string> plotColumns;
00302 
00306       H5::GroupBase *plotGroup;
00307 
00308       void updatePlotFeatures();
00309 
00310     private:
00314       PlotFeatureStatus plotFeature[LASTPLOTFEATURE], plotFeatureForChildren[LASTPLOTFEATURE];
00315   };
00316 
00317   template<class T>
00318   T* Element::getByPath(const std::string &path, bool initialCaller) const {
00319     try {
00320       if(path.substr(0, 1) == "/") { // if absolute path ...
00321         if(parent) // .. and a parent exists ...
00322           return parent->getByPath<T>(path, false); // ... than call getByPath of the parent (walk to the top)
00323         else // .. and no parent exits ...
00324           return getByPath<T>(path.substr(1), false); // ... we are at top and call getByPath again with the leading "/" removed (call relative to top)
00325       }
00326       else if (path.substr(0, 3) == "../") // if relative path to parent ...
00327         return parent->getByPath<T>(path.substr(3), false); // ... call getByPath of the parent with the leading "../" removed
00328       else { // if relative path to a child ...
00329         // extract the first path and all other paths (rest)
00330         size_t idx=path.find('/');
00331         std::string first=path.substr(0, idx);
00332         std::string rest;
00333         if(idx!=std::string::npos)
00334           rest=path.substr(idx+1);
00335         // get the object of the first child path by calling the virtual function getChildByContainerAndName
00336         size_t pos0=first.find('[');
00337         if(pos0==std::string::npos)
00338           THROW_MBSIMERROR("Syntax error in subreference '"+first+"': no [ found.");
00339         std::string container=first.substr(0, pos0);
00340         if(first[first.size()-1]!=']')
00341           THROW_MBSIMERROR("Syntax error in subreference '"+first+"': not ending with ].");
00342         std::string name=first.substr(pos0+1, first.size()-pos0-2);
00343         Element *e=getChildByContainerAndName(container, name);
00344         // if their are other child paths call getByPath of e for this
00345         if(!rest.empty())
00346           return e->getByPath<T>(rest, false);
00347         // this is the last relative path -> check type and return
00348         T *t=dynamic_cast<T*>(e);
00349         if(t)
00350           return t;
00351         else
00352           THROW_MBSIMERROR("Cannot cast this element to type "+container+".");
00353       }
00354     }
00355     catch(MBSimError &ex) {
00356       if(initialCaller)
00357         THROW_MBSIMERROR("Evaluation of refernece '"+path+"' failed: Message from "+
00358           ex.getPath()+": "+ex.getErrorMessage());
00359       else
00360         throw ex;
00361     }
00362   }
00363 
00364 
00365 }
00366 
00367 #endif