OpenFPM_io  0.2.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Functions Variables Typedefs
VTKWriter_grids.hpp
1 /*
2  * VTKWriter_grids.hpp
3  *
4  * Created on: May 5, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef VTKWRITER_GRIDS_HPP_
9 #define VTKWRITER_GRIDS_HPP_
10 
11 #include <boost/mpl/pair.hpp>
12 #include "VTKWriter_grids_util.hpp"
13 
20 template <typename Grid, typename St>
21 class ele_g
22 {
23 public:
24 
25  typedef Grid value_type;
26 
27  ele_g(const Grid & g, const Point<Grid::dims,St> & offset, const Point<Grid::dims,St> & spacing, const Box<Grid::dims,St> & dom)
28  :g(g),offset(offset),spacing(spacing),dom(dom)
29  {}
30 
32  std::string dataset;
34  const Grid & g;
36  Point<Grid::dims,St> offset;
37  // spacing of the grid
38  Point<Grid::dims,St> spacing;
39  // Part of the grid that is real domain
40  Box<Grid::dims,size_t> dom;
41 };
42 
43 
44 
56 template<typename ele_g, typename St>
57 struct prop_out_g
58 {
59  // property output string
60  std::string & v_out;
61 
62  // grid that we are processing
63  const openfpm::vector_std< ele_g > & vg;
64 
70  prop_out_g(std::string & v_out, const openfpm::vector_std< ele_g > & vg)
71  :v_out(v_out),vg(vg)
72  {};
73 
75  template<typename T>
76  void operator()(T& t) const
77  {
78  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
79 
80  meta_prop<boost::mpl::int_<T::value> ,ele_g,St, ptype > m(vg,v_out);
81  }
82 
83  void lastProp()
84  {
85  // Create point data properties
86  v_out += "SCALARS domain float\n";
87 
88  // Default lookup table
89  v_out += "LOOKUP_TABLE default\n";
90 
91  // Produce point data
92  for (size_t k = 0 ; k < vg.size() ; k++)
93  {
95  auto it = vg.get(k).g.getIterator();
96 
97  // if there is the next element
98  while (it.isNext())
99  {
100  if (vg.get(k).dom.isInside(it.get().toPoint()) == true)
101  v_out += "1.0\n";
102  else
103  v_out += "0.0\n";
104 
105  // increment the iterator and counter
106  ++it;
107  }
108  }
109  }
110 };
111 
121 template <typename pair>
122 class VTKWriter<pair,VECTOR_GRIDS>
123 {
125  openfpm::vector< ele_g<typename pair::first,typename pair::second> > vg;
126 
132  size_t get_total()
133  {
134  size_t tot = 0;
135 
137  for (size_t i = 0 ; i < vg.size() ; i++)
138  {
139  tot += vg.get(i).g.size();
140  }
141  return tot;
142  }
143 
153  {
155  std::string v_out;
156 
157  // write the number of vertex
158  v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
159 
160  // return the vertex properties string
161  return v_out;
162  }
163 
173  {
175  std::string v_out;
176 
177  // write the number of vertex
178  v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";
179 
180  // return the vertex properties string
181  return v_out;
182  }
183 
187  std::string get_point_list()
188  {
190  std::stringstream v_out;
191 
193 
194  for (size_t i = 0 ; i < vg.size() ; i++)
195  {
197  auto it = vg.get(i).g.getIterator();
198 
200  Box<pair::first::dims,typename pair::second> dom;
201 
202  // if there is the next element
203  while (it.isNext())
204  {
205  Point<pair::first::dims,typename pair::second> p;
206  p = it.get().toPoint();
207  p = p * vg.get(i).spacing + vg.get(i).offset;
208 
209  if (pair::first::dims == 2)
210  v_out << p.toString() << " 0.0" << "\n";
211  else
212  v_out << p.toString() << "\n";
213 
214  // increment the iterator and counter
215  ++it;
216  }
217  }
218 
219  // return the vertex list
220  return v_out.str();
221  }
222 
226  std::string get_vertex_list()
227  {
229  std::string v_out;
230 
231  size_t k = 0;
232 
233  for (size_t i = 0 ; i < vg.size() ; i++)
234  {
236  auto it = vg.get(i).g.getIterator();
237 
238  while (it.isNext())
239  {
240  v_out += "1 " + std::to_string(k) + "\n";
241 
242  ++k;
243  ++it;
244  }
245  }
246  // return the vertex list
247  return v_out;
248  }
249 
256  std::string get_point_data_header()
257  {
258  std::string v_out;
259 
260  v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
261 
262  return v_out;
263  }
264 
265 public:
266 
273  {}
274 
283  void add(const typename pair::first & g, const Point<pair::first::dims,typename pair::second> & offset, const Point<pair::first::dims,typename pair::second> & spacing, const Box<pair::first::dims,typename pair::second> & dom)
284  {
286 
287  vg.add(t);
288  }
289 
300  template<int prp = -1> bool write(std::string file, std::string f_name = "grids" , file_type ft = file_type::ASCII)
301  {
302  // Header for the vtk
303  std::string vtk_header;
304  // Point list of the VTK
305  std::string point_list;
306  // Vertex list of the VTK
307  std::string vertex_list;
308  // Graph header
309  std::string vtk_binary_or_ascii;
310  // vertex properties header
311  std::string point_prop_header;
312  // edge properties header
313  std::string vertex_prop_header;
314  // Data point header
315  std::string point_data_header;
316  // Data point
317  std::string point_data;
318 
319  // VTK header
320  vtk_header = "# vtk DataFile Version 3.0\n"
321  + f_name + "\n";
322 
323  // Choose if binary or ASCII
324  if (ft == file_type::ASCII)
325  {vtk_header += "ASCII\n";}
326  else
327  {vtk_header += "BINARY\n";}
328 
329  // Data type for graph is DATASET POLYDATA
330  vtk_header += "DATASET POLYDATA\n";
331 
332  // point properties header
333  point_prop_header = get_point_properties_list();
334 
335  // Get point list
336  point_list = get_point_list();
337 
338  // vertex properties header
339  vertex_prop_header = get_vertex_properties_list();
340 
341  // Get vertex list
342  vertex_list = get_vertex_list();
343 
344  // Get the point data header
345  point_data_header = get_point_data_header();
346 
347  // For each property in the vertex type produce a point data
348 
349  prop_out_g< ele_g<typename pair::first,typename pair::second>, typename pair::second > pp(point_data, vg);
350 
351  if (prp == -1)
352  boost::mpl::for_each< boost::mpl::range_c<int,0, pair::first::value_type::max_prop> >(pp);
353  else
354  boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);
355 
356  // Add the last property
357  pp.lastProp();
358 
359 
360  // write the file
361  std::ofstream ofs(file);
362 
363  // Check if the file is open
364  if (ofs.is_open() == false)
365  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
366 
367  ofs << vtk_header << point_prop_header << point_list <<
368  vertex_prop_header << vertex_list << point_data_header << point_data;
369 
370  // Close the file
371 
372  ofs.close();
373 
374  // Completed succefully
375  return true;
376  }
377 };
378 
379 
380 #endif /* VTKWRITER_GRAPH_HPP_ */
bool write(std::string file, std::string f_name="grids", file_type ft=file_type::ASCII)
It write a VTK file from a graph.
openfpm::vector< ele_g< typename pair::first, typename pair::second > > vg
Vector of grids.
void operator()(T &t) const
It produce an output for each property.
std::string get_point_data_header()
Get the point data header.
std::string get_point_list()
Create the VTK point definition.
std::string dataset
Dataset name.
std::string get_point_properties_list()
It get the vertex properties list.
void add(const typename pair::first &g, const Point< pair::first::dims, typename pair::second > &offset, const Point< pair::first::dims, typename pair::second > &spacing, const Box< pair::first::dims, typename pair::second > &dom)
Add grid dataset.
Point< Grid::dims, St > offset
offset where it start
This class is an helper to create properties output from scalar and compile-time array elements...
size_t get_total()
Get the total number of points.
std::string get_vertex_list()
Create the VTK vertex definition.
const Grid & g
Grid.
this class is a functor for "for_each" algorithm
prop_out_g(std::string &v_out, const openfpm::vector_std< ele_g > &vg)
constructor
std::string get_vertex_properties_list()
It get the vertex properties list.
It store one grid.