$extrastylesheet
mesh_data.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_MESH_DATA_H
00021 #define LIBMESH_MESH_DATA_H
00022 
00023 // Local Includes
00024 #include "libmesh/libmesh.h"
00025 #include "libmesh/enum_xdr_mode.h"
00026 
00027 // C++ includes
00028 #include <cstddef>
00029 #include <map>
00030 #include <vector>
00031 
00032 namespace libMesh
00033 {
00034 
00035 
00036 // Forward Declarations
00037 class Node;
00038 class Elem;
00039 class MeshBase;
00040 class MeshDataUnvHeader;
00041 
00042 
00056 // ------------------------------------------------------------
00057 // MeshData class definition
00058 class MeshData
00059 {
00060 public:
00061 
00062 
00063   //----------------------------------------------------------
00064   // convenient typedefs
00073   typedef std::map<const Node*, std::vector<Number> >::const_iterator const_node_data_iterator;
00074 
00083   typedef std::map<const Elem*, std::vector<Number> >::const_iterator const_elem_data_iterator;
00084 
00085 
00086   //----------------------------------------------------------
00091   MeshData (const MeshBase& m);
00092 
00096   ~MeshData ();
00097 
00107   void activate (const std::string& descriptor="");
00108 
00121   void enable_compatibility_mode (const std::string& descriptor="");
00122 
00128   void clear ();
00129 
00138   void slim (const bool node_id_map = true,
00139              const bool elem_id_map = true);
00140 
00155   void translate (const MeshBase& out_mesh,
00156                   std::vector<Number>& data_values,
00157                   std::vector<std::string>& data_names) const;
00158 
00165   void read (const std::string& name);
00166 
00171   void write (const std::string& name);
00172 
00177   std::string get_info () const;
00178 
00182   void print_info (std::ostream& os=libMesh::out) const;
00183 
00187   friend std::ostream& operator << (std::ostream& os, const MeshData& m);
00188 
00189 
00190   //----------------------------------------------------------
00191   // Node-associated data
00197   Number operator() (const Node* node,
00198                      const unsigned int i=0) const;
00199 
00204   bool has_data (const Node* node) const;
00205 
00212   const std::vector<Number>& get_data (const Node* node) const;
00213 
00218   void set_data (const Node* node, const std::vector<Number>& val);
00219 
00227   unsigned int n_val_per_node () const;
00228 
00233   dof_id_type n_node_data () const;
00234 
00240   const_node_data_iterator node_data_begin () const;
00241 
00246   const_node_data_iterator node_data_end () const;
00247 
00263   void insert_node_data (std::map<const Node*,
00264                          std::vector<Number> >& nd,
00265                          const bool close_elem_data = true);
00266 
00267 
00268   //----------------------------------------------------------
00269   // Element-associated data
00275   Number operator() (const Elem* elem,
00276                      const unsigned int i=0) const;
00277 
00282   bool has_data (const Elem* elem) const;
00283 
00290   const std::vector<Number>& get_data (const Elem* elem) const;
00291 
00296   void set_data (const Elem* elem, const std::vector<Number> &val);
00297 
00305   unsigned int n_val_per_elem () const;
00306 
00311   dof_id_type n_elem_data () const;
00312 
00318   const_elem_data_iterator elem_data_begin () const;
00319 
00324   const_elem_data_iterator elem_data_end () const;
00325 
00342   void insert_elem_data (std::map<const Elem*,
00343                          std::vector<Number> >& ed,
00344                          const bool close_node_data = true);
00345 
00346 
00347   //----------------------------------------------------------
00352   bool active () const;
00357   bool compatibility_mode () const;
00358 
00364   bool elem_initialized () const;
00365 
00370   bool node_initialized () const;
00371 
00372 
00373   //----------------------------------------------------------
00374   // Methods for accessing the node and element maps.
00375   // Heavily used by the \p read() and \p write() methods.
00379   const Node* foreign_id_to_node (const unsigned int fid) const;
00380 
00384   const Elem* foreign_id_to_elem (const unsigned int fid) const;
00385 
00389   unsigned int node_to_foreign_id (const Node* n) const;
00390 
00394   unsigned int elem_to_foreign_id (const Elem* n) const;
00395 
00396   //----------------------------------------------------------
00397   // Methods for the header information in universal formated
00398   // datasets.
00399 
00403   const MeshDataUnvHeader & get_unv_header() const;
00404 
00409   void set_unv_header(MeshDataUnvHeader* unv_header);
00410 
00411 
00418   void assign (const MeshData& omd);
00419 
00420 
00421   //----------------------------------------------------------
00422   // Methods used by mesh importes to communicate node/element
00423   // labels to this \p MeshData
00424 
00432   void add_foreign_node_id (const Node* node,
00433                             const unsigned int foreign_node_id);
00434 
00442   void add_foreign_elem_id (const Elem* elem,
00443                             const unsigned int foreign_elem_id);
00444 
00449   void close_foreign_id_maps ();
00450 
00451 
00452 protected:
00453 
00454 
00455   //----------------------------------------------------------
00456   // read/write Methods
00460   void read_tetgen (const std::string& name);
00461 
00467   void read_unv (const std::string& file_name);
00468 
00475   void read_unv_implementation (std::istream& in_file);
00476 
00482   void write_unv (const std::string& file_name);
00483 
00490   void write_unv_implementation (std::ostream& out_file);
00491 
00492 
00500   void read_xdr (const std::string& name,
00501                  const XdrMODE mode = READ);
00502 
00509   void write_xdr (const std::string& name,
00510                   const XdrMODE mode = WRITE);
00511 
00512 
00516   const MeshBase& _mesh;
00517 
00522   std::string _data_descriptor;
00523 
00524 
00525   //--------------------------------------------------
00526   // node associated data & maps
00531   std::map<const Node*,
00532            std::vector<Number> > _node_data;
00533 
00538   std::map<const Node*,
00539            unsigned int> _node_id;
00540 
00545   std::map<unsigned int,
00546            const Node*> _id_node;
00547 
00548 
00549 
00550   //--------------------------------------------------
00551   // element associated data & maps
00555   std::map<const Elem*,
00556            std::vector<Number> > _elem_data;
00557 
00562   std::map<const Elem*,
00563            unsigned int> _elem_id;
00568   std::map<unsigned int,
00569            const Elem*> _id_elem;
00570 
00571 
00572 
00573   //--------------------------------------------------------
00582   bool _node_id_map_closed;
00583 
00588   bool _node_data_closed;
00589 
00590 
00591   //--------------------------------------------------------
00600   bool _elem_id_map_closed;
00601 
00606   bool _elem_data_closed;
00607 
00608 
00609   //--------------------------------------------------------
00614   bool _active;
00615 
00621   bool _compatibility_mode;
00622 
00626   MeshDataUnvHeader* _unv_header;
00627 
00631   friend class MeshDataUnvHeader;
00632 
00633 };
00634 
00635 
00636 
00637 //-----------------------------------------------------------
00638 // MeshDataUnvHeader class definition
00639 
00683 class MeshDataUnvHeader
00684 {
00685 public:
00686 
00691   MeshDataUnvHeader ();
00692 
00696   ~MeshDataUnvHeader ();
00697 
00712   void which_dataset (const unsigned int ds_label);
00713 
00718   void operator = (const MeshDataUnvHeader& omduh);
00719 
00724   bool operator == (const MeshDataUnvHeader& omduh) const;
00725 
00729   unsigned int dataset_label;
00730 
00734   std::string dataset_name;
00735 
00740   unsigned int dataset_location;
00741 
00745   std::vector<std::string> id_lines_1_to_5;
00746 
00756   unsigned int model_type,
00757     analysis_type,
00758     data_characteristic,
00759     result_type;
00760 
00766   unsigned int data_type;
00767 
00772   unsigned int nvaldc;
00773 
00778   std::vector<int> record_10,
00779     record_11;
00780 
00785   std::vector<Real> record_12,
00786     record_13;
00787 
00788 
00789 protected:
00790 
00799   bool read (std::istream& in_file);
00800 
00804   void write (std::ostream& out_file);
00805 
00806 
00807 private:
00808 
00813   unsigned int _desired_dataset_label;
00814 
00821   static bool need_D_to_e (std::string& number);
00822 
00826   friend class MeshData;
00827 
00828 };
00829 
00830 
00831 
00832 // ------------------------------------------------------------
00833 // MeshData inline methods
00834 
00835 //-------------------------------------------------------------
00836 // element data inline methods
00837 inline
00838 Number MeshData::operator() (const Node* node,
00839                              const unsigned int i) const
00840 {
00841   libmesh_assert (_active || _compatibility_mode);
00842   libmesh_assert (_node_data_closed);
00843 
00844   std::map<const Node*,
00845     std::vector<Number> >::const_iterator pos = _node_data.find(node);
00846 
00847   if (pos == _node_data.end())
00848     return libMesh::zero;
00849 
00850   // we only get here when pos != _node_data.end()
00851   libmesh_assert_less (i, pos->second.size());
00852   return pos->second[i];
00853 }
00854 
00855 
00856 
00857 inline
00858 bool MeshData::has_data (const Node* node) const
00859 {
00860   libmesh_assert (_active || _compatibility_mode);
00861   libmesh_assert (_node_data_closed);
00862 
00863   std::map<const Node*,
00864     std::vector<Number> >::const_iterator pos = _node_data.find(node);
00865 
00866   return (pos != _node_data.end());
00867 }
00868 
00869 
00870 
00871 inline
00872 const std::vector<Number>& MeshData::get_data (const Node* node) const
00873 {
00874   libmesh_assert (_active || _compatibility_mode);
00875   libmesh_assert (_node_data_closed);
00876 
00877   std::map<const Node*,
00878     std::vector<Number> >::const_iterator pos = _node_data.find(node);
00879 
00880 #ifdef DEBUG
00881   if (pos == _node_data.end())
00882     libmesh_error_msg("ERROR: No data for this node.  Use has_data() first!");
00883 #endif
00884 
00885   return pos->second;
00886 }
00887 
00888 
00889 
00890 inline
00891 void MeshData::set_data (const Node* node,
00892                          const std::vector<Number> &val)
00893 {
00894   this->_node_data[node] = val;
00895 }
00896 
00897 
00898 
00899 inline
00900 MeshData::const_node_data_iterator MeshData::node_data_begin () const
00901 {
00902   return _node_data.begin();
00903 }
00904 
00905 
00906 
00907 inline
00908 MeshData::const_node_data_iterator MeshData::node_data_end () const
00909 {
00910   return _node_data.end();
00911 }
00912 
00913 
00914 
00915 //-------------------------------------------------------------
00916 // element data inline methods
00917 inline
00918 Number MeshData::operator() (const Elem* elem,
00919                              const unsigned int i) const
00920 {
00921   libmesh_assert (_active || _compatibility_mode);
00922   libmesh_assert (_elem_data_closed);
00923 
00924   std::map<const Elem*,
00925     std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
00926 
00927   if (pos == _elem_data.end())
00928     return libMesh::zero;
00929 
00930   // we only get here when pos != _elem_data.end()
00931   libmesh_assert_less (i, pos->second.size());
00932   return pos->second[i];
00933 }
00934 
00935 
00936 
00937 inline
00938 bool MeshData::has_data (const Elem* elem) const
00939 {
00940   libmesh_assert (_active || _compatibility_mode);
00941   libmesh_assert (_elem_data_closed);
00942 
00943   std::map<const Elem*,
00944     std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
00945 
00946   return (pos != _elem_data.end());
00947 }
00948 
00949 
00950 
00951 inline
00952 const std::vector<Number>& MeshData::get_data (const Elem* elem) const
00953 {
00954   libmesh_assert (_active || _compatibility_mode);
00955   libmesh_assert (_elem_data_closed);
00956 
00957   std::map<const Elem*,
00958     std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
00959 
00960 #ifdef DEBUG
00961   if (pos == _elem_data.end())
00962     libmesh_error_msg("ERROR: No data for this element.  Use has_data() first!");
00963 #endif
00964 
00965   return pos->second;
00966 }
00967 
00968 
00969 
00970 inline
00971 void MeshData::set_data (const Elem* elem,
00972                          const std::vector<Number> &val)
00973 {
00974   this->_elem_data[elem] = val;
00975 }
00976 
00977 
00978 
00979 inline
00980 MeshData::const_elem_data_iterator MeshData::elem_data_begin () const
00981 {
00982   return _elem_data.begin();
00983 }
00984 
00985 
00986 
00987 inline
00988 MeshData::const_elem_data_iterator MeshData::elem_data_end () const
00989 {
00990   return _elem_data.end();
00991 }
00992 
00993 
00994 
00995 //-------------------------------------------------------------
00996 // other inline methods
00997 inline
00998 bool MeshData::active() const
00999 {
01000   return _active;
01001 }
01002 
01003 
01004 
01005 inline
01006 bool MeshData::compatibility_mode() const
01007 {
01008   return _compatibility_mode;
01009 }
01010 
01011 
01012 
01013 inline
01014 bool MeshData::elem_initialized() const
01015 {
01016   return (_active && _elem_data_closed);
01017 }
01018 
01019 
01020 
01021 inline
01022 bool MeshData::node_initialized() const
01023 {
01024   return (_active && _node_data_closed);
01025 }
01026 
01027 
01028 
01029 inline
01030 void MeshData::add_foreign_node_id (const Node* node,
01031                                     const unsigned int foreign_node_id)
01032 {
01033   if (_active)
01034     {
01035       libmesh_assert (!_node_id_map_closed);
01036       libmesh_assert(node);
01037       libmesh_assert (_node_id.find(node) == _node_id.end());
01038       libmesh_assert (_id_node.find(foreign_node_id) == _id_node.end());
01039 
01040       /*
01041        * _always_ insert in _id_node and _node_id.  If we would
01042        * use the mesh.node(unsigned int) method or the node.id()
01043        * to get Node* and unsigned int, respectively, we would not
01044        * be safe any more when the mesh gets refined or re-numbered
01045        * within libMesh. And we could get in big trouble that would
01046        * be hard to find when importing data _after_ having refined...
01047        */
01048       _node_id.insert(std::make_pair(node, foreign_node_id));
01049       _id_node.insert(std::make_pair(foreign_node_id, node));
01050     }
01051 }
01052 
01053 
01054 
01055 inline
01056 void MeshData::add_foreign_elem_id (const Elem* elem,
01057                                     const unsigned int foreign_elem_id)
01058 {
01059   if (_active)
01060     {
01061       libmesh_assert (!_elem_id_map_closed);
01062       libmesh_assert(elem);
01063       libmesh_assert (_elem_id.find(elem) == _elem_id.end());
01064       libmesh_assert (_id_elem.find(foreign_elem_id) == _id_elem.end());
01065 
01066       _elem_id.insert(std::make_pair(elem, foreign_elem_id));
01067       _id_elem.insert(std::make_pair(foreign_elem_id, elem));
01068     }
01069 }
01070 
01071 
01072 inline
01073 const MeshDataUnvHeader & MeshData::get_unv_header () const
01074 {
01075   libmesh_assert(this->_unv_header);
01076   return *this->_unv_header;
01077 }
01078 
01079 
01080 inline
01081 void MeshData::set_unv_header (MeshDataUnvHeader* unv_header)
01082 {
01083   libmesh_assert(unv_header);
01084   this->_unv_header = unv_header;
01085 }
01086 
01087 
01088 //-----------------------------------------------------------
01089 // MeshDataUnvHeader inline methods
01090 
01091 
01092 } // namespace libMesh
01093 
01094 #endif // LIBMESH_MESH_DATA_H