ug4
file_io_vtu.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__LIB_GRID__FILE_IO_VTU__
34 #define __H__LIB_GRID__FILE_IO_VTU__
35 
36 #include <iostream>
37 #include <vector>
38 #include <utility>
39 #include "common/parser/rapidxml/rapidxml.hpp"
40 #include "lib_grid/grid/grid.h"
41 #include "lib_grid/multi_grid.h"
47 
48 namespace ug
49 {
50 
53 
58 template <class TAPosition>
60  const char* filename, APosition& aPos);
61 
63 
70 bool LoadGridFromVTU(Grid& grid, ISubsetHandler& sh,
71  const char* filename);
72 
73 
75 template <class TAPosition>
76 bool SaveGridToVTU(Grid& grid, ISubsetHandler* psh, const char* filename,
77  TAPosition& aPos);
78 
79 
83 
87 {
88  public:
89  GridWriterVTU();
91 
93  GridWriterVTU(std::ostream* out);
94 
95  virtual ~GridWriterVTU();
96 
98 
100  void set_stream(std::ostream* out);
101 
102  void finish();
103 
112  template <class TPositionAttachment>
113  bool new_piece(Grid& grid, ISubsetHandler* psh,
114  TPositionAttachment& aPos);
115 
117 
120  void add_subset_handler(ISubsetHandler& sh, const std::string& name);
121 
122  void begin_point_data();
123  //...
124  void end_point_data();
125 
126  void begin_cell_data();
127  void end_cell_data();
128 
129  protected:
134 
135  inline std::ostream& out_stream();
136 
139  void write_data_array_header(const char* type, const char* name,
140  int numberOfComponents);
141 
143 
144  template <class TElem, class TAttachment>
145  void write_vector_data(Grid& grid,
146  TAttachment aData,
147  const char* name = "",
148  typename Grid::traits<TElem>::callback consider_elem =
149  ConsiderAll());
150 
151  template <class TElem>
152  void collect_cells(std::vector<GridObject*>& cellsOut,
153  Grid& grid,
154  typename Grid::traits<TElem>::callback consider_elem =
155  ConsiderAll());
156 
157  void write_cells(std::vector<GridObject*>& cells, Grid& grid,
158  AAVrtIndex aaInd);
159 
160  void end_piece();
161 
162 
163  enum Mode{
166  CLOSED
167  };
168 
169  std::ostream* m_pout;
173 
176 
177  std::vector<GridObject*> m_cells;
178  std::vector<std::pair<ISubsetHandler*, std::string> > m_pieceSubsetHandlers;
179 };
180 
181 
182 
185 
191 {
192  public:
193  GridReaderVTU();
194  virtual ~GridReaderVTU();
195 
197  bool parse_file(const char* filename);
198 
200  size_t num_grids() const {return m_entries.size();}
201 
203 
205  template <class TPositionAttachment>
206  bool grid(Grid& gridOut, size_t index, TPositionAttachment& aPos);
207 
209  const char* get_grid_name(size_t index) const;
210 
212  size_t num_subset_handlers(size_t refGridIndex) const;
213 
215  const char* get_subset_handler_name(size_t refGridIndex,
216  size_t subsetHandlerIndex) const;
217 
219  bool subset_handler(ISubsetHandler& shOut,
220  size_t refGridIndex,
221  size_t subsetHandlerIndex);
222 
223  static std::string const getRegionOfInterestIdentifyer()
224  { return m_regionOfInterest; }
225 
226  static void setRegionOfInterestIdentifier( std::string const & regOfInt )
227  { m_regionOfInterest = regOfInt; }
228 
229  protected:
231  {
232  SubsetHandlerEntry(rapidxml::xml_node<>* n) : node(n), sh(NULL) {}
233 
234  rapidxml::xml_node<>* node;
236  };
237 
238  struct GridEntry
239  {
240  GridEntry(rapidxml::xml_node<>* n) : node(n), grid(NULL), mg(NULL) {}
241 
242  rapidxml::xml_node<>* node;
245  std::vector<SubsetHandlerEntry> subsetHandlerEntries;
246  std::vector<Vertex*> vertices;
247  std::vector<GridObject*> cells;
248  };
249 
250  protected:
252 
257  virtual bool new_document_parsed();
258 
260 
263  template <class TAAPos>
264  bool create_vertices(std::vector<Vertex*>& vrtsOut, Grid& grid,
265  rapidxml::xml_node<>* vrtNode, TAAPos aaPos);
266 
267  bool create_cells(std::vector<GridObject*>& cellsOut,
268  Grid& grid,
269  rapidxml::xml_node<>* node,
270  std::vector<Vertex*> vertices,
271  size_t pieceVrtOffset);
272 
273  template <class T>
274  void read_scalar_data(std::vector<T>& dataOut,
275  rapidxml::xml_node<>* dataNode,
276  bool clearData = true);
277 
278  void trafoDblVec2Int( std::vector<double> const & dblVec, std::vector<int> & intVec );
279 
280  template <class T>
281  void check_indices(std::vector<T>& inds, size_t first, size_t num, size_t max);
282 
283  rapidxml::xml_node<>* find_child_node_by_argument_value(
284  rapidxml::xml_node<>* parent,
285  const char* nodeName,
286  const char* argName,
287  const char* argValue);
288  protected:
290  std::string m_filename;
291 
293  rapidxml::xml_document<> m_doc;
294 
296  std::vector<GridEntry> m_entries;
297 
298 
299 
300  static std::string m_regionOfInterest; // ProMesh standard = "regions", in Braunschweig case often "Material Id", but not always
301 
302 };
303 
304 }// end of namespace
305 
307 // include implementation
308 #include "file_io_vtu_impl.h"
309 
310 #endif
location name
Definition: checkpoint_util.lua:128
A generic specialization of IAttachment.
Definition: attachment_pipe.h:263
callback that always returns true
Definition: basic_callbacks.h:50
Definition: grid.h:211
Definition: grid.h:221
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
Grants read access to vtu (vtk) files.
Definition: file_io_vtu.h:191
size_t num_grids() const
returns the number of grids in the given file
Definition: file_io_vtu.h:200
static std::string const getRegionOfInterestIdentifyer()
Definition: file_io_vtu.h:223
bool parse_file(const char *filename)
parses an xml file
Definition: file_io_vtu.cpp:451
std::vector< GridEntry > m_entries
holds grids which already have been created
Definition: file_io_vtu.h:296
virtual ~GridReaderVTU()
Definition: file_io_vtu.cpp:412
GridReaderVTU()
Definition: file_io_vtu.cpp:408
bool subset_handler(ISubsetHandler &shOut, size_t refGridIndex, size_t subsetHandlerIndex)
fills the given subset-handler
Definition: file_io_vtu.cpp:592
std::string m_filename
stores the file-name
Definition: file_io_vtu.h:290
rapidxml::xml_document m_doc
the xml_document which stores the data
Definition: file_io_vtu.h:293
const char * get_subset_handler_name(size_t refGridIndex, size_t subsetHandlerIndex) const
returns the name of the given subset handler
Definition: file_io_vtu.cpp:579
bool create_vertices(std::vector< Vertex * > &vrtsOut, Grid &grid, rapidxml::xml_node<> *vrtNode, TAAPos aaPos)
creates vertices from a vertex-node.
Definition: file_io_vtu_impl.h:267
void read_scalar_data(std::vector< T > &dataOut, rapidxml::xml_node<> *dataNode, bool clearData=true)
Definition: file_io_vtu_impl.h:357
virtual bool new_document_parsed()
initializes internal arrays
Definition: file_io_vtu.cpp:525
rapidxml::xml_node * find_child_node_by_argument_value(rapidxml::xml_node<> *parent, const char *nodeName, const char *argName, const char *argValue)
Definition: file_io_vtu.cpp:875
bool grid(Grid &gridOut, size_t index, TPositionAttachment &aPos)
returns the i-th grid.
Definition: file_io_vtu_impl.h:212
static std::string m_regionOfInterest
Definition: file_io_vtu.h:300
size_t num_subset_handlers(size_t refGridIndex) const
returns the number of subset handlers for the given grid
Definition: file_io_vtu.cpp:567
const char * get_grid_name(size_t index) const
returns the name of the i-th grid
Definition: file_io_vtu.cpp:558
void trafoDblVec2Int(std::vector< double > const &dblVec, std::vector< int > &intVec)
Definition: file_io_vtu.cpp:863
bool create_cells(std::vector< GridObject * > &cellsOut, Grid &grid, rapidxml::xml_node<> *node, std::vector< Vertex * > vertices, size_t pieceVrtOffset)
Definition: file_io_vtu.cpp:716
static void setRegionOfInterestIdentifier(std::string const &regOfInt)
Definition: file_io_vtu.h:226
void check_indices(std::vector< T > &inds, size_t first, size_t num, size_t max)
Definition: file_io_vtu_impl.h:385
Grants write access to vtu files.
Definition: file_io_vtu.h:87
Mode m_pointDataMode
Definition: file_io_vtu.h:171
void write_data_array_footer()
Definition: file_io_vtu.cpp:195
void end_cell_data()
Definition: file_io_vtu.cpp:244
void begin_cell_data()
Definition: file_io_vtu.cpp:228
Mode m_pieceMode
Definition: file_io_vtu.h:170
Grid::VertexAttachmentAccessor< AInt > AAVrtIndex
Definition: file_io_vtu.h:130
Grid::EdgeAttachmentAccessor< AInt > AAEdgeIndex
Definition: file_io_vtu.h:131
std::ostream * m_pout
Definition: file_io_vtu.h:169
Grid::FaceAttachmentAccessor< AInt > AAFaceIndex
Definition: file_io_vtu.h:132
std::vector< std::pair< ISubsetHandler *, std::string > > m_pieceSubsetHandlers
Definition: file_io_vtu.h:178
Grid::VolumeAttachmentAccessor< AInt > AAVolIndex
Definition: file_io_vtu.h:133
void set_stream(std::ostream *out)
Pass a pointer to an ostream to which the data shall be written.
Definition: file_io_vtu.cpp:153
virtual ~GridWriterVTU()
Definition: file_io_vtu.cpp:147
void write_data_array_header(const char *type, const char *name, int numberOfComponents)
Definition: file_io_vtu.cpp:181
Mode
Definition: file_io_vtu.h:163
@ NONE
Definition: file_io_vtu.h:164
@ OPEN
Definition: file_io_vtu.h:165
@ CLOSED
Definition: file_io_vtu.h:166
Mode m_cellDataMode
Definition: file_io_vtu.h:172
bool new_piece(Grid &grid, ISubsetHandler *psh, TPositionAttachment &aPos)
Definition: file_io_vtu_impl.h:97
void write_vector_data(Grid &grid, TAttachment aData, const char *name="", typename Grid::traits< TElem >::callback consider_elem=ConsiderAll())
Definition: file_io_vtu_impl.h:165
Grid * m_curGrid
Definition: file_io_vtu.h:174
void finish()
Definition: file_io_vtu.cpp:380
void begin_point_data()
Definition: file_io_vtu.cpp:201
std::vector< GridObject * > m_cells
Definition: file_io_vtu.h:177
std::ostream & out_stream()
Definition: file_io_vtu_impl.h:89
void end_piece()
Definition: file_io_vtu.cpp:358
void write_cells(std::vector< GridObject * > &cells, Grid &grid, AAVrtIndex aaInd)
Definition: file_io_vtu.cpp:286
GridWriterVTU()
Definition: file_io_vtu.cpp:125
ISubsetHandler * m_curSH
Definition: file_io_vtu.h:175
void end_point_data()
Definition: file_io_vtu.cpp:216
void add_subset_handler(ISubsetHandler &sh, const std::string &name)
You may add subset-handlers which will be written as regions to the vtk-file.
Definition: file_io_vtu.cpp:172
void collect_cells(std::vector< GridObject * > &cellsOut, Grid &grid, typename Grid::traits< TElem >::callback consider_elem=ConsiderAll())
Definition: file_io_vtu_impl.h:193
Definition: subset_handler_interface.h:223
Definition: multi_grid.h:72
std::pair< counting_iterator< size_t >, counting_iterator< size_t > > vertices(ug::BidirectionalMatrix< T > const &M)
Definition: bidirectional_boost.h:60
the ug namespace
bool SaveGridToVTU(Grid &grid, ISubsetHandler *psh, const char *filename, TAPosition &aPos)
Writes a grid to a vtu (vtk unstructured mesh) file.
Definition: file_io_vtu_impl.h:67
bool LoadGridFromVTU(Grid &grid, ISubsetHandler &sh, const char *filename)
Reads a grid to a vtu (vtk unstructured mesh) file.
Definition: file_io_vtu.cpp:103
boost::function< bool(base_object *)> callback
callback type for the elements base type.
Definition: grid.h:150
Definition: file_io_vtu.h:239
GridEntry(rapidxml::xml_node<> *n)
Definition: file_io_vtu.h:240
std::vector< GridObject * > cells
Definition: file_io_vtu.h:247
Grid * grid
Definition: file_io_vtu.h:243
MultiGrid * mg
Definition: file_io_vtu.h:244
std::vector< Vertex * > vertices
Definition: file_io_vtu.h:246
rapidxml::xml_node * node
Definition: file_io_vtu.h:242
std::vector< SubsetHandlerEntry > subsetHandlerEntries
Definition: file_io_vtu.h:245
Definition: file_io_vtu.h:231
SubsetHandlerEntry(rapidxml::xml_node<> *n)
Definition: file_io_vtu.h:232
ISubsetHandler * sh
Definition: file_io_vtu.h:235
rapidxml::xml_node * node
Definition: file_io_vtu.h:234