mesh_data.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2016 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_MESH_DATA_H
21 #define LIBMESH_MESH_DATA_H
22 
23 // Local Includes
24 #include "libmesh/libmesh.h"
25 #include "libmesh/enum_xdr_mode.h"
26 
27 // C++ includes
28 #include <cstddef>
29 #include <map>
30 #include <vector>
31 
32 namespace libMesh
33 {
34 
35 // Forward Declarations
36 class Node;
37 class Elem;
38 class MeshBase;
39 class MeshDataUnvHeader;
40 
54 class MeshData
55 {
56 public:
57  //----------------------------------------------------------
58  // convenient typedefs
67  typedef std::map<const Node *, std::vector<Number> >::const_iterator const_node_data_iterator;
68 
77  typedef std::map<const Elem *, std::vector<Number> >::const_iterator const_elem_data_iterator;
78 
79 
80  //----------------------------------------------------------
85  MeshData (const MeshBase & m);
86 
90  ~MeshData ();
91 
101  void activate (const std::string & descriptor="");
102 
115  void enable_compatibility_mode (const std::string & descriptor="");
116 
122  void clear ();
123 
132  void slim (const bool node_id_map = true,
133  const bool elem_id_map = true);
134 
149  void translate (const MeshBase & out_mesh,
150  std::vector<Number> & data_values,
151  std::vector<std::string> & data_names) const;
152 
159  void read (const std::string & name);
160 
165  void write (const std::string & name);
166 
171  std::string get_info () const;
172 
176  void print_info (std::ostream & os=libMesh::out) const;
177 
181  friend std::ostream & operator << (std::ostream & os, const MeshData & m);
182 
183 
184  //----------------------------------------------------------
185  // Node-associated data
191  Number operator() (const Node * node,
192  const unsigned int i=0) const;
193 
198  bool has_data (const Node * node) const;
199 
206  const std::vector<Number> & get_data (const Node * node) const;
207 
212  void set_data (const Node * node, const std::vector<Number> & val);
213 
221  unsigned int n_val_per_node () const;
222 
227  dof_id_type n_node_data () const;
228 
234  const_node_data_iterator node_data_begin () const;
235 
240  const_node_data_iterator node_data_end () const;
241 
257  void insert_node_data (std::map<const Node *,
258  std::vector<Number> > & nd,
259  const bool close_elem_data = true);
260 
261 
262  //----------------------------------------------------------
263  // Element-associated data
269  Number operator() (const Elem * elem,
270  const unsigned int i=0) const;
271 
276  bool has_data (const Elem * elem) const;
277 
284  const std::vector<Number> & get_data (const Elem * elem) const;
285 
290  void set_data (const Elem * elem, const std::vector<Number> & val);
291 
299  unsigned int n_val_per_elem () const;
300 
305  dof_id_type n_elem_data () const;
306 
312  const_elem_data_iterator elem_data_begin () const;
313 
318  const_elem_data_iterator elem_data_end () const;
319 
336  void insert_elem_data (std::map<const Elem *, std::vector<Number> > & ed,
337  const bool close_node_data = true);
338 
339 
340  //----------------------------------------------------------
345  bool active () const;
350  bool compatibility_mode () const;
351 
357  bool elem_initialized () const;
358 
363  bool node_initialized () const;
364 
365 
366  //----------------------------------------------------------
367  // Methods for accessing the node and element maps.
368  // Heavily used by the \p read() and \p write() methods.
372  const Node * foreign_id_to_node (const unsigned int fid) const;
373 
377  const Elem * foreign_id_to_elem (const unsigned int fid) const;
378 
382  unsigned int node_to_foreign_id (const Node * n) const;
383 
387  unsigned int elem_to_foreign_id (const Elem * n) const;
388 
389  //----------------------------------------------------------
390  // Methods for the header information in universal formated
391  // datasets.
392 
396  const MeshDataUnvHeader & get_unv_header() const;
397 
402  void set_unv_header(MeshDataUnvHeader * unv_header);
403 
404 
411  void assign (const MeshData & omd);
412 
413 
414  //----------------------------------------------------------
415  // Methods used by mesh importes to communicate node/element
416  // labels to this \p MeshData
417 
425  void add_foreign_node_id (const Node * node,
426  const unsigned int foreign_node_id);
427 
435  void add_foreign_elem_id (const Elem * elem,
436  const unsigned int foreign_elem_id);
437 
442  void close_foreign_id_maps ();
443 
444 
445 protected:
446 
447 
448  //----------------------------------------------------------
449  // read/write Methods
453  void read_tetgen (const std::string & name);
454 
460  void read_unv (const std::string & file_name);
461 
468  void read_unv_implementation (std::istream & in_file);
469 
475  void write_unv (const std::string & file_name);
476 
483  void write_unv_implementation (std::ostream & out_file);
484 
485 
493  void read_xdr (const std::string & name,
494  const XdrMODE mode = READ);
495 
502  void write_xdr (const std::string & name,
503  const XdrMODE mode = WRITE);
504 
505 
509  const MeshBase & _mesh;
510 
515  std::string _data_descriptor;
516 
517 
518  //--------------------------------------------------
519  // node associated data & maps
524  std::map<const Node *,
525  std::vector<Number> > _node_data;
526 
531  std::map<const Node *,
532  unsigned int> _node_id;
533 
538  std::map<unsigned int,
539  const Node *> _id_node;
540 
541 
542 
543  //--------------------------------------------------
544  // element associated data & maps
548  std::map<const Elem *,
549  std::vector<Number> > _elem_data;
550 
555  std::map<const Elem *,
556  unsigned int> _elem_id;
561  std::map<unsigned int,
562  const Elem *> _id_elem;
563 
564 
565 
566  //--------------------------------------------------------
576 
582 
583 
584  //--------------------------------------------------------
594 
600 
601 
602  //--------------------------------------------------------
607  bool _active;
608 
615 
620 
624  friend class MeshDataUnvHeader;
625 
626 };
627 
628 
629 
674 {
675 public:
676 
682 
687 
702  void which_dataset (const unsigned int ds_label);
703 
708  void operator = (const MeshDataUnvHeader & omduh);
709 
714  bool operator == (const MeshDataUnvHeader & omduh) const;
715 
719  unsigned int dataset_label;
720 
724  std::string dataset_name;
725 
730  unsigned int dataset_location;
731 
735  std::vector<std::string> id_lines_1_to_5;
736 
746  unsigned int model_type,
749  result_type;
750 
756  unsigned int data_type;
757 
762  unsigned int nvaldc;
763 
768  std::vector<int> record_10,
769  record_11;
770 
775  std::vector<Real> record_12,
776  record_13;
777 
778 
779 protected:
780 
789  bool read (std::istream & in_file);
790 
794  void write (std::ostream & out_file);
795 
796 
797 private:
798 
804 
811  static bool need_D_to_e (std::string & number);
812 
816  friend class MeshData;
817 
818 };
819 
820 
821 
822 // ------------------------------------------------------------
823 // MeshData inline methods
824 
825 //-------------------------------------------------------------
826 // element data inline methods
827 inline
829  const unsigned int i) const
830 {
833 
834  std::map<const Node *,
835  std::vector<Number> >::const_iterator pos = _node_data.find(node);
836 
837  if (pos == _node_data.end())
838  return libMesh::zero;
839 
840  // we only get here when pos != _node_data.end()
841  libmesh_assert_less (i, pos->second.size());
842  return pos->second[i];
843 }
844 
845 
846 
847 inline
848 bool MeshData::has_data (const Node * node) const
849 {
852 
853  std::map<const Node *,
854  std::vector<Number> >::const_iterator pos = _node_data.find(node);
855 
856  return (pos != _node_data.end());
857 }
858 
859 
860 
861 inline
862 const std::vector<Number> & MeshData::get_data (const Node * node) const
863 {
866 
867  std::map<const Node *,
868  std::vector<Number> >::const_iterator pos = _node_data.find(node);
869 
870 #ifdef DEBUG
871  if (pos == _node_data.end())
872  libmesh_error_msg("ERROR: No data for this node. Use has_data() first!");
873 #endif
874 
875  return pos->second;
876 }
877 
878 
879 
880 inline
881 void MeshData::set_data (const Node * node,
882  const std::vector<Number> & val)
883 {
884  this->_node_data[node] = val;
885 }
886 
887 
888 
889 inline
891 {
892  return _node_data.begin();
893 }
894 
895 
896 
897 inline
899 {
900  return _node_data.end();
901 }
902 
903 
904 
905 //-------------------------------------------------------------
906 // element data inline methods
907 inline
909  const unsigned int i) const
910 {
913 
914  std::map<const Elem *,
915  std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
916 
917  if (pos == _elem_data.end())
918  return libMesh::zero;
919 
920  // we only get here when pos != _elem_data.end()
921  libmesh_assert_less (i, pos->second.size());
922  return pos->second[i];
923 }
924 
925 
926 
927 inline
928 bool MeshData::has_data (const Elem * elem) const
929 {
932 
933  std::map<const Elem *,
934  std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
935 
936  return (pos != _elem_data.end());
937 }
938 
939 
940 
941 inline
942 const std::vector<Number> & MeshData::get_data (const Elem * elem) const
943 {
946 
947  std::map<const Elem *,
948  std::vector<Number> >::const_iterator pos = _elem_data.find(elem);
949 
950 #ifdef DEBUG
951  if (pos == _elem_data.end())
952  libmesh_error_msg("ERROR: No data for this element. Use has_data() first!");
953 #endif
954 
955  return pos->second;
956 }
957 
958 
959 
960 inline
961 void MeshData::set_data (const Elem * elem,
962  const std::vector<Number> & val)
963 {
964  this->_elem_data[elem] = val;
965 }
966 
967 
968 
969 inline
971 {
972  return _elem_data.begin();
973 }
974 
975 
976 
977 inline
979 {
980  return _elem_data.end();
981 }
982 
983 
984 
985 //-------------------------------------------------------------
986 // other inline methods
987 inline
988 bool MeshData::active() const
989 {
990  return _active;
991 }
992 
993 
994 
995 inline
997 {
998  return _compatibility_mode;
999 }
1000 
1001 
1002 
1003 inline
1005 {
1006  return (_active && _elem_data_closed);
1007 }
1008 
1009 
1010 
1011 inline
1013 {
1014  return (_active && _node_data_closed);
1015 }
1016 
1017 
1018 
1019 inline
1021  const unsigned int foreign_node_id)
1022 {
1023  if (_active)
1024  {
1026  libmesh_assert(node);
1027  libmesh_assert (_node_id.find(node) == _node_id.end());
1028  libmesh_assert (_id_node.find(foreign_node_id) == _id_node.end());
1029 
1030  /*
1031  * _always_ insert in _id_node and _node_id. If we would
1032  * use the mesh.node(unsigned int) method or the node.id()
1033  * to get Node * and unsigned int, respectively, we would not
1034  * be safe any more when the mesh gets refined or re-numbered
1035  * within libMesh. And we could get in big trouble that would
1036  * be hard to find when importing data _after_ having refined...
1037  */
1038  _node_id.insert(std::make_pair(node, foreign_node_id));
1039  _id_node.insert(std::make_pair(foreign_node_id, node));
1040  }
1041 }
1042 
1043 
1044 
1045 inline
1047  const unsigned int foreign_elem_id)
1048 {
1049  if (_active)
1050  {
1052  libmesh_assert(elem);
1053  libmesh_assert (_elem_id.find(elem) == _elem_id.end());
1054  libmesh_assert (_id_elem.find(foreign_elem_id) == _id_elem.end());
1055 
1056  _elem_id.insert(std::make_pair(elem, foreign_elem_id));
1057  _id_elem.insert(std::make_pair(foreign_elem_id, elem));
1058  }
1059 }
1060 
1061 
1062 inline
1064 {
1065  libmesh_assert(this->_unv_header);
1066  return *this->_unv_header;
1067 }
1068 
1069 
1070 inline
1072 {
1073  libmesh_assert(unv_header);
1074  this->_unv_header = unv_header;
1075 }
1076 
1077 
1078 //-----------------------------------------------------------
1079 // MeshDataUnvHeader inline methods
1080 
1081 
1082 } // namespace libMesh
1083 
1084 #endif // LIBMESH_MESH_DATA_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
bool _node_id_map_closed
Definition: mesh_data.h:575
unsigned int _desired_dataset_label
Definition: mesh_data.h:803
const std::vector< Number > & get_data(const Node *node) const
Definition: mesh_data.h:862
void read_unv_implementation(std::istream &in_file)
A geometric point in (x,y,z) space associated with a DOF.
Definition: node.h:54
unsigned int data_characteristic
Definition: mesh_data.h:746
std::map< const Node *, std::vector< Number > >::const_iterator const_node_data_iterator
Definition: mesh_data.h:67
bool has_data(const Node *node) const
Definition: mesh_data.h:848
unsigned int n_val_per_elem() const
Definition: mesh_data.C:633
libmesh_assert(remote_elem)
std::vector< Real > record_13
Definition: mesh_data.h:775
void operator=(const MeshDataUnvHeader &omduh)
std::vector< int > record_10
Definition: mesh_data.h:768
const_elem_data_iterator elem_data_end() const
Definition: mesh_data.h:978
void insert_node_data(std::map< const Node *, std::vector< Number > > &nd, const bool close_elem_data=true)
Definition: mesh_data.C:497
Number operator()(const Node *node, const unsigned int i=0) const
Definition: mesh_data.h:828
const_node_data_iterator node_data_begin() const
Definition: mesh_data.h:890
void which_dataset(const unsigned int ds_label)
The base class for all geometric element types.
Definition: elem.h:92
unsigned int result_type
Definition: mesh_data.h:746
bool active() const
Definition: mesh_data.h:988
std::vector< Real > record_12
Definition: mesh_data.h:775
void add_foreign_node_id(const Node *node, const unsigned int foreign_node_id)
Definition: mesh_data.h:1020
static bool need_D_to_e(std::string &number)
const Number zero
Definition: libmesh.h:179
void write_xdr(const std::string &name, const XdrMODE mode=WRITE)
bool _elem_id_map_closed
Definition: mesh_data.h:593
std::vector< std::string > id_lines_1_to_5
Definition: mesh_data.h:735
const_node_data_iterator node_data_end() const
Definition: mesh_data.h:898
void slim(const bool node_id_map=true, const bool elem_id_map=true)
Definition: mesh_data.C:122
Base class for Mesh.
Definition: mesh_base.h:70
void print_info(std::ostream &os=libMesh::out) const
Definition: mesh_data.C:353
std::map< unsigned int, const Node * > _id_node
Definition: mesh_data.h:539
unsigned int analysis_type
Definition: mesh_data.h:746
const MeshDataUnvHeader & get_unv_header() const
Definition: mesh_data.h:1063
bool elem_initialized() const
Definition: mesh_data.h:1004
unsigned int dataset_label
Definition: mesh_data.h:719
void translate(const MeshBase &out_mesh, std::vector< Number > &data_values, std::vector< std::string > &data_names) const
Definition: mesh_data.C:160
unsigned int node_to_foreign_id(const Node *n) const
Definition: mesh_data.C:399
std::string get_info() const
Definition: mesh_data.C:322
void set_unv_header(MeshDataUnvHeader *unv_header)
Definition: mesh_data.h:1071
bool _compatibility_mode
Definition: mesh_data.h:614
MeshData(const MeshBase &m)
Definition: mesh_data.C:35
const Elem * foreign_id_to_elem(const unsigned int fid) const
Definition: mesh_data.C:434
bool _elem_data_closed
Definition: mesh_data.h:599
void read_unv(const std::string &file_name)
std::map< const Elem *, std::vector< Number > >::const_iterator const_elem_data_iterator
Definition: mesh_data.h:77
void read_xdr(const std::string &name, const XdrMODE mode=READ)
std::string _data_descriptor
Definition: mesh_data.h:515
bool operator==(const MeshDataUnvHeader &omduh) const
std::vector< int > record_11
Definition: mesh_data.h:768
void write(const std::string &name)
Definition: mesh_data.C:282
dof_id_type n_elem_data() const
Definition: mesh_data.C:652
bool read(std::istream &in_file)
unsigned int elem_to_foreign_id(const Elem *n) const
Definition: mesh_data.C:463
std::map< const Elem *, unsigned int > _elem_id
Definition: mesh_data.h:556
void read(const std::string &name)
Definition: mesh_data.C:235
void write_unv(const std::string &file_name)
const MeshBase & _mesh
Definition: mesh_data.h:509
MeshDataUnvHeader * _unv_header
Definition: mesh_data.h:619
void set_data(const Node *node, const std::vector< Number > &val)
Definition: mesh_data.h:881
void activate(const std::string &descriptor="")
Definition: mesh_data.C:64
friend std::ostream & operator<<(std::ostream &os, const MeshData &m)
Definition: mesh_data.C:360
void insert_elem_data(std::map< const Elem *, std::vector< Number > > &ed, const bool close_node_data=true)
Definition: mesh_data.C:553
void close_foreign_id_maps()
Definition: mesh_data.C:217
void enable_compatibility_mode(const std::string &descriptor="")
Definition: mesh_data.C:81
dof_id_type n_node_data() const
Definition: mesh_data.C:622
std::map< unsigned int, const Elem * > _id_elem
Definition: mesh_data.h:562
void add_foreign_elem_id(const Elem *elem, const unsigned int foreign_elem_id)
Definition: mesh_data.h:1046
std::map< const Node *, unsigned int > _node_id
Definition: mesh_data.h:532
void assign(const MeshData &omd)
Definition: mesh_data.C:663
unsigned int n_val_per_node() const
Definition: mesh_data.C:603
const Node * foreign_id_to_node(const unsigned int fid) const
Definition: mesh_data.C:369
void write(std::ostream &out_file)
std::map< const Node *, std::vector< Number > > _node_data
Definition: mesh_data.h:525
unsigned int dataset_location
Definition: mesh_data.h:730
void read_tetgen(const std::string &name)
const_elem_data_iterator elem_data_begin() const
Definition: mesh_data.h:970
OStreamProxy out(std::cout)
std::map< const Elem *, std::vector< Number > > _elem_data
Definition: mesh_data.h:549
void write_unv_implementation(std::ostream &out_file)
bool compatibility_mode() const
Definition: mesh_data.h:996
bool node_initialized() const
Definition: mesh_data.h:1012
uint8_t dof_id_type
Definition: id_types.h:64
bool _node_data_closed
Definition: mesh_data.h:581