OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
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 #include "is_vtk_writable.hpp"
14 
21 template <typename Grid, typename St>
22 class ele_g
23 {
24 public:
25 
26  typedef Grid value_type;
27 
28  ele_g(const Grid & g, const Point<Grid::dims,St> & offset, const Point<Grid::dims,St> & spacing, const Box<Grid::dims,St> & dom)
29  :g(g),offset(offset),spacing(spacing),dom(dom)
30  {}
31 
33  std::string dataset;
35  const Grid & g;
38  // spacing of the grid
39  Point<Grid::dims,St> spacing;
40  // Part of the grid that is real domain
42 };
43 
44 
45 
57 template<typename ele_g, typename St>
58 struct prop_out_g
59 {
61  std::string & v_out;
62 
65 
67  file_type ft;
68 
71 
82  {};
83 
89  template<typename T>
90  void operator()(T& t) const
91  {
92  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
93  typedef typename std::remove_all_extents<ptype>::type base_ptype;
94 
96  }
97 
103  void lastProp()
104  {
105  // Create point data properties
106  v_out += "SCALARS domain float\n";
107 
108  // Default lookup table
109  v_out += "LOOKUP_TABLE default\n";
110 
111  // Produce point data
112  for (size_t k = 0 ; k < vg.size() ; k++)
113  {
115  auto it = vg.get(k).g.getIterator();
116 
117  // if there is the next element
118  while (it.isNext())
119  {
120  if (ft == file_type::ASCII)
121  {
122  if (vg.get(k).dom.isInside(it.get().toPoint()) == true)
123  {
124  float flag = 1.0;
125  flag += vg.get(k).g.getFlag(it.get()) * 2;
126  v_out += std::to_string(flag) + "\n";
127  }
128  else
129  {
130  float flag = 0.0;
131  flag += vg.get(k).g.getFlag(it.get()) * 2;
132  v_out += std::to_string(flag) + "\n";
133  }
134  }
135  else
136  {
137  if (vg.get(k).dom.isInside(it.get().toPoint()) == true)
138  {
139  float flag = 1.0;
140  flag = swap_endian_lt(flag);
141  v_out.append((const char *)&flag,sizeof(flag));
142  }
143  else
144  {
145  float flag = 0.0;
146  flag = swap_endian_lt(flag);
147  v_out.append((const char *)&flag,sizeof(flag));
148  }
149  }
150 
151  // increment the iterator and counter
152  ++it;
153  }
154  }
155  }
156 };
157 
167 template <typename pair>
168 class VTKWriter<pair,VECTOR_GRIDS>
169 {
171 
178  size_t get_total()
179  {
180  size_t tot = 0;
181 
183  for (size_t i = 0 ; i < vg.size() ; i++)
184  {
185  tot += vg.get(i).g.size();
186  }
187  return tot;
188  }
189 
199  {
201  std::string v_out;
202 
203  // write the number of vertex
204  v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
205 
206  // return the vertex properties string
207  return v_out;
208  }
209 
218  {
220  std::string v_out;
221 
222  // write the number of vertex
223  if (std::is_same<typename pair::second,float>::value == true)
224  {v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";}
225  else
226  {v_out += "POINTS " + std::to_string(get_total()) + " double" + "\n";}
227 
228  // return the vertex properties string
229  return v_out;
230  }
231 
239  std::string get_point_list(file_type ft)
240  {
242  std::stringstream v_out;
243 
244  if (std::is_same<typename pair::second,float>::value == true)
245  {v_out << std::setprecision(7);}
246  else
247  {v_out << std::setprecision(16);}
248 
250 
251  for (size_t i = 0 ; i < vg.size() ; i++)
252  {
254  auto it = vg.get(i).g.getIterator();
255 
258 
259  // if there is the next element
260  while (it.isNext())
261  {
263  p = it.get().toPoint();
264  p = pmul(p,vg.get(i).spacing) + vg.get(i).offset;
265 
266  output_point<pair::first::dims,typename pair::second>(p,v_out,ft);
267 
268  // increment the iterator and counter
269  ++it;
270  }
271  }
272 
273  // return the vertex list
274  return v_out.str();
275  }
276 
282  std::string get_vertex_list(file_type ft)
283  {
285  std::string v_out;
286 
287  size_t k = 0;
288 
289  for (size_t i = 0 ; i < vg.size() ; i++)
290  {
292  auto it = vg.get(i).g.getIterator();
293 
294  while (it.isNext())
295  {
296  output_vertex(k,v_out,ft);
297 
298  ++k;
299  ++it;
300  }
301  }
302  // return the vertex list
303  return v_out;
304  }
305 
312  std::string get_point_data_header()
313  {
314  std::string v_out;
315 
316  v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
317 
318  return v_out;
319  }
320 
321 public:
322 
329  {}
330 
339  void add(const typename pair::first & g,
343  {
345 
346  vg.add(t);
347  }
348 
361  template<int prp = -1> bool write(std::string file,
362  const openfpm::vector<std::string> & prop_names,
363  std::string f_name = "grids",
364  file_type ft = file_type::ASCII)
365  {
366  // Header for the vtk
367  std::string vtk_header;
368  // Point list of the VTK
369  std::string point_list;
370  // Vertex list of the VTK
371  std::string vertex_list;
372  // Graph header
373  std::string vtk_binary_or_ascii;
374  // vertex properties header
375  std::string point_prop_header;
376  // edge properties header
377  std::string vertex_prop_header;
378  // Data point header
379  std::string point_data_header;
380  // Data point
381  std::string point_data;
382 
383  // VTK header
384  vtk_header = "# vtk DataFile Version 3.0\n"
385  + f_name + "\n";
386 
387  // Choose if binary or ASCII
388  if (ft == file_type::ASCII)
389  {vtk_header += "ASCII\n";}
390  else
391  {vtk_header += "BINARY\n";}
392 
393  // Data type for graph is DATASET POLYDATA
394  vtk_header += "DATASET POLYDATA\n";
395 
396  // point properties header
397  point_prop_header = get_point_properties_list();
398 
399  // Get point list
400  point_list = get_point_list(ft);
401 
402  // vertex properties header
403  vertex_prop_header = get_vertex_properties_list();
404 
405  // Get vertex list
406  vertex_list = get_vertex_list(ft);
407 
408  // Get the point data header
409  point_data_header = get_point_data_header();
410 
411  // For each property in the vertex type produce a point data
412 
413  prop_out_g< ele_g<typename pair::first,typename pair::second>, typename pair::second > pp(point_data, vg, prop_names, ft);
414 
415  if (prp == -1)
416  {boost::mpl::for_each< boost::mpl::range_c<int,0, pair::first::value_type::max_prop> >(pp);}
417  else
418  {boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);}
419 
420  // Add the last property
421  pp.lastProp();
422 
423 
424  // write the file
425  std::ofstream ofs(file);
426 
427  // Check if the file is open
428  if (ofs.is_open() == false)
429  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
430 
431  ofs << vtk_header << point_prop_header << point_list <<
432  vertex_prop_header << vertex_list << point_data_header << point_data;
433 
434  // Close the file
435 
436  ofs.close();
437 
438  // Completed succefully
439  return true;
440  }
441 };
442 
443 
444 #endif /* VTKWRITER_GRAPH_HPP_ */
std::string get_vertex_list(file_type ft)
Create the VTK vertex definition.
openfpm::vector< ele_g< typename pair::first, typename pair::second > > vg
Vector of grids.
size_t size()
Stub size.
Definition: map_vector.hpp:211
std::string get_point_data_header()
Get the point data header.
std::string dataset
Dataset name.
std::string get_point_properties_list()
It get the vertex properties list.
prop_out_g(std::string &v_out, const openfpm::vector_std< ele_g > &vg, const openfpm::vector< std::string > &prop_names, file_type ft)
constructor
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.
std::string & v_out
property output string
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
std::string get_point_list(file_type ft)
Create the VTK point definition.
file_type ft
File type.
const openfpm::vector< std::string > & prop_names
list of names for the properties
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.
This class represent an N-dimensional box.
Definition: Box.hpp:60
size_t get_total()
Get the total number of points.
const Grid & g
Grid.
bool write(std::string file, const openfpm::vector< std::string > &prop_names, std::string f_name="grids", file_type ft=file_type::ASCII)
It write a VTK file from a graph.
void operator()(T &t) const
this class is a functor for "for_each" algorithm
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:202
std::string get_vertex_properties_list()
It get the vertex properties list.
const openfpm::vector_std< ele_g > & vg
grid that we are processing
void lastProp()
Write the last property.
It store one grid.
check for T to be writable