MBSim

Project Page
element.h
00001 /* Copyright (C) 2004-2009 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 : 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       const std::string getShortName() const {
00167         size_t i = name.length() - 1;
00168 
00169         while ((name[i-1] != '/') and (i > 1)) {
00170           i--;
00171         }
00172         if(i == 1)
00173           i--;
00174 
00175         return name.substr(i, name.length() - i);
00176       }
00177 
00181       void setName(const std::string &str) { name = str; }
00182 
00186       DynamicSystemSolver* getDynamicSystemSolver() { return ds; }
00187 
00192       virtual void init(InitStage stage);
00193 
00197       virtual void createPlotGroup();
00198 
00202       H5::Group *getPlotGroup() { return plotGroup; }
00203 
00210       virtual void setPlotFeature(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; }
00211 
00217       void setPlotFeatureForChildren(PlotFeature pf, PlotFeatureStatus value) { plotFeatureForChildren[pf]=value; }
00218 
00224       void setPlotFeatureRecursive(PlotFeature pf, PlotFeatureStatus value) { plotFeature[pf]=value; plotFeatureForChildren[pf]=value; }
00225 
00229       PlotFeatureStatus getPlotFeature(PlotFeature pf) { return plotFeature[pf]; }
00230 
00234       PlotFeatureStatus getPlotFeatureForChildren(PlotFeature pf) { return plotFeatureForChildren[pf]; }
00235 
00236       virtual void initializeUsingXML(xercesc::DOMElement *element);
00237       virtual xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *element);
00238 
00242       template<class T>
00243         T* getByPath(std::string path) {
00244           Element * e = getByPathSearch(path);
00245           if (dynamic_cast<T*>(e))
00246             return (T*)(e);
00247           else
00248             throw MBSimError("ERROR in "+getName()+" (Element::getByPath): Element \""+path+"\" not found or not of wanted type!");
00249         }
00250 
00254       virtual Element* getByPathSearch(std::string path) {return 0; }
00255 
00256       // some convenience function for XML
00257       static double getDouble(xercesc::DOMElement *e);
00258       static int getInt(xercesc::DOMElement *e);
00259       static bool getBool(xercesc::DOMElement *e);
00260       static fmatvec::Vec3 getVec3(xercesc::DOMElement *e);
00261       static fmatvec::Vec getVec(xercesc::DOMElement *e, int rows=0);
00262       static fmatvec::Mat3xV getMat3xV(xercesc::DOMElement *e, int cols=0);
00263       static fmatvec::Mat getMat(xercesc::DOMElement *e, int rows=0, int cols=0);
00264       static fmatvec::SqrMat3 getSqrMat3(xercesc::DOMElement *e);
00265       static fmatvec::SqrMat getSqrMat(xercesc::DOMElement *e, int size=0);
00266       static fmatvec::SymMat3 getSymMat3(xercesc::DOMElement *e);
00267       static fmatvec::SymMat getSymMat(xercesc::DOMElement *e, int size=0);
00268 
00269 #ifdef HAVE_OPENMBVCPPINTERFACE
00270       virtual OpenMBV::Group* getOpenMBVGrp() {return 0;}
00271 #endif
00272 
00273       virtual Element* getParent() {return parent;}
00274       virtual const Element* getParent() const {return parent;}
00275       virtual void setParent(Element* parent_) {parent = parent_;}
00280       std::string getPath(char pathDelim='.');
00281 
00282       std::string getXMLPath(MBSim::Element *ref=0, bool rel=false);
00283 
00284     protected:
00285       Element *parent;
00286 
00290       std::string name;
00291 
00295       DynamicSystemSolver *ds;
00296 
00300       H5::VectorSerie<double> *plotVectorSerie;
00301 
00305       std::vector<double> plotVector;
00306 
00310       std::vector<std::string> plotColumns;
00311 
00315       H5::Group *plotGroup;
00316 
00317       void updatePlotFeatures();
00318 
00319     private:
00323       PlotFeatureStatus plotFeature[LASTPLOTFEATURE], plotFeatureForChildren[LASTPLOTFEATURE];
00324   };
00325 
00326 }
00327 
00328 #endif