OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
VTKWriter_grids_util.hpp
1 /*
2  * VTKWriter_grids_util.hpp
3  *
4  * Created on: Aug 10, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef SRC_VTKWRITER_GRIDS_UTIL_HPP_
9 #define SRC_VTKWRITER_GRIDS_UTIL_HPP_
10 
11 #include "util/util_debug.hpp"
12 #include "is_vtk_writable.hpp"
13 #include "byteswap_portable.hpp"
14 
19 template<typename ele_g, bool has_attributes>
21 {
31  static inline std::string get(size_t i, const openfpm::vector<std::string> & prop_names, const std::string & oprp)
32  {
33  return ele_g::value_type::value_type::attributes::name[i] + oprp;
34  }
35 };
36 
42 template<typename ele_g>
43 struct getAttrName<ele_g,false>
44 {
54  static inline std::string get(size_t i, const openfpm::vector<std::string> & prop_names, const std::string & oprp)
55  {
56  if (i >= prop_names.size())
57  {
58  return std::string("attr" + std::to_string(i) + oprp);
59  }
60  else
61  {
62  return prop_names.get(i) + oprp;
63  }
64  }
65 };
66 
73 template<unsigned int i, typename ele_g, bool has_attributes> std::string get_point_property_header_impl(const std::string & oprp, const openfpm::vector<std::string> & prop_names)
74 {
76  std::string v_out;
77 
78  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
79 
80  // Check if T is a supported format
81  // for now we support only scalar of native type
82  if (std::rank<ctype>::value == 1)
83  {
84  if (std::extent<ctype>::value <= 3)
85  {
86  //Get type of the property
87  std::string type = getType<typename std::remove_all_extents<ctype>::type>();
88 
89  // if the type is not supported skip-it
90  if (type.size() == 0)
91  {
92  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
93  return "";
94  }
95 
96  // Create point data properties
97  v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
98  }
99  }
100  else
101  {
102  std::string type = getType<typename std::remove_all_extents<ctype>::type>();
103 
104  // if the type is not supported return
105  if (type.size() == 0)
106  {
107  // We check if is a custom vtk writable object
108 
109  if (is_vtk_writable<ctype>::value == true)
110  {
111  type = getType<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
112 
113  // We check if it is a vector or scalar like type
114  if (vtk_dims<ctype>::value == 1)
115  v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
116  else
117  v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
118  }
119 
120  return v_out;
121  }
122 
123  // Create point data properties
124  v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
125 
126  // Default lookup table
127  v_out += "LOOKUP_TABLE default\n";
128 
129  }
130 
131  // return the vertex list
132  return v_out;
133 }
134 
140 template<unsigned int dim, typename T>
142 {
143 public:
144 
145  template<typename vector, typename iterator, typename I> static void write(std::string & v_out, vector & vg, size_t k, iterator & it, file_type ft)
146  {
147 
148  if (ft == file_type::ASCII)
149  {
150  // Print the properties
151  for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
152  {
153  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1)) + " ";
154  }
155  if (vtk_dims<T>::value == 2)
156  {
157  v_out += "0.0";
158  }
159  v_out += "\n";
160  }
161  else
162  {
163  typedef decltype(vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(0)) ctype_;
164  typedef typename std::remove_reference<ctype_>::type ctype;
165 
166  // Print the properties
167  for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
168  {
169  typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1);
170  tmp = swap_endian_lt(tmp);
171  v_out.append((const char *)&tmp,sizeof(tmp));
172  }
173  if (vtk_dims<T>::value == 2)
174  {
175  typename is_vtk_writable<ctype>::base zero = 0.0;
176  zero = swap_endian_lt(zero);
177  v_out.append((const char *)&zero,sizeof(zero));
178  }
179  }
180  }
181 };
182 
187 template<typename T>
188 class prop_write_out<1,T>
189 {
190 public:
191 
201  template<typename vector, typename iterator, typename I> static void write(std::string & v_out, vector & vg, size_t k, iterator & it, file_type ft)
202  {
203  typedef decltype(vg.get(k).g.get_o(it.get()).template get<I::value>()) ctype_;
204  typedef typename std::remove_reference<ctype_>::type ctype;
205 
206  if (ft == file_type::ASCII)
207  {
208  // Print the property
209  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()) + "\n";
210  }
211  else
212  {
213  typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.get_o(it.get()).template get<I::value>();
214  tmp = swap_endian_lt(tmp);
215  v_out.append((const char *)&tmp,sizeof(tmp));
216  }
217  }
218 };
219 
220 
230 template<typename I, typename ele_g, typename St, typename T, bool is_writable>
231 struct meta_prop
232 {
241  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
242  {
243  // actual string size
244  size_t sz = v_out.size();
245 
246  // Produce the point properties header
247  v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
248 
249  // If the output has changed, we have to write the properties
250  if (v_out.size() != sz)
251  {
252  // Produce point data
253 
254  for (size_t k = 0 ; k < vg.size() ; k++)
255  {
257  auto it = vg.get(k).g.getIterator();
258 
259  // if there is the next element
260  while (it.isNext())
261  {
262  prop_write_out<vtk_dims<T>::value,T>::template write<decltype(vg),decltype(it),I>(v_out,vg,k,it,ft);
263 
264  // increment the iterator and counter
265  ++it;
266  }
267  }
268 
269  if (ft == file_type::BINARY)
270  v_out += "\n";
271  }
272  }
273 };
274 
276 template<typename I, typename ele_g, typename St, typename T, size_t N1, bool is_writable>
277 struct meta_prop<I, ele_g,St,T[N1],is_writable>
278 {
287  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names , file_type ft)
288  {
289  // actual string size
290  size_t sz = v_out.size();
291 
292  // Produce the point properties header
293  v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
294 
295  // If the output has changed, we have to write the properties
296  if (v_out.size() != sz)
297  {
298  // Produce point data
299 
300  for (size_t k = 0 ; k < vg.size() ; k++)
301  {
303  auto it = vg.get(k).g.getIterator();
304 
305  // if there is the next element
306  while (it.isNext())
307  {
308  if (ft == file_type::ASCII)
309  {
310  // Print the properties
311  for (size_t i1 = 0 ; i1 < N1 ; i1++)
312  {
313  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1]) + " ";
314  }
315  if (N1 == 2)
316  {
317  v_out += "0.0";
318  }
319  v_out += "\n";
320  }
321  else
322  {
323  T tmp;
324 
325  // Print the properties
326  for (size_t i1 = 0 ; i1 < N1 ; i1++)
327  {
328  tmp = vg.get(k).g.get_o(it.get()).template get<I::value>()[i1];
329  tmp = swap_endian_lt(tmp);
330  v_out.append((const char *)&tmp,sizeof(T));
331  }
332  if (N1 == 2)
333  {
334  tmp = 0.0;
335  tmp = swap_endian_lt(tmp);
336  v_out.append((const char *)&tmp,sizeof(T));
337  }
338  }
339 
340  // increment the iterator and counter
341  ++it;
342  }
343  }
344  if (ft == file_type::BINARY)
345  v_out += "\n";
346  }
347  }
348 };
349 
351 template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, bool is_writable>
352 struct meta_prop<I, ele_g,St, T[N1][N2],is_writable>
353 {
354 
363  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
364  {
365  for (size_t i1 = 0 ; i1 < N1 ; i1++)
366  {
367  for (size_t i2 = 0 ; i2 < N2 ; i2++)
368  {
369  // actual string size
370  size_t sz = v_out.size();
371 
372  // Produce the point properties header
373  v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2),prop_names);
374 
375  // If the output has changed, we have to write the properties
376  if (v_out.size() != sz)
377  {
378  // Produce point data
379 
380  for (size_t k = 0 ; k < vg.size() ; k++)
381  {
383  auto it = vg.get(k).g.getIterator();
384 
385  // if there is the next element
386  while (it.isNext())
387  {
388  T tmp;
389 
390  if (ft == file_type::ASCII)
391  {
392  // Print the property
393  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2]) + "\n";
394  }
395  else
396  {
397  tmp = vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2];
398  tmp = swap_endian_lt(tmp);
399  v_out.append((const char *)&tmp,sizeof(tmp));
400  }
401 
402  // increment the iterator and counter
403  ++it;
404  }
405  }
406 
407  if (ft == file_type::BINARY)
408  v_out += "\n";
409  }
410  }
411  }
412  }
413 };
414 
415 
417 template<typename I, typename ele_g, typename St, typename T>
418 struct meta_prop<I,ele_g,St,T,false>
419 {
420 
429  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
430  {
431  }
432 };
433 
434 template<unsigned int dims,typename T> inline void output_point(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
435 {
436  if (ft == file_type::ASCII)
437  {
438  v_out << p.toString();
439  size_t i = dims;
440  for ( ; i < 3 ; i++)
441  {v_out << " 0.0";}
442  v_out << "\n";
443  }
444  else
445  {
446  size_t i = 0;
447  for ( ; i < dims ; i++)
448  {
449  // we use float so we have to convert to float
450  auto tmp = p.get(i);
451  tmp = swap_endian_lt(tmp);
452  v_out.write((const char *)&tmp,sizeof(tmp));
453  }
454  for ( ; i < 3 ; i++)
455  {
456  // we use float so we have to convert to float
457 
458  /* coverity[dead_error_begin] */
459  T tmp = 0.0;
460  tmp = swap_endian_lt(tmp);
461  v_out.write((const char *)&tmp,sizeof(tmp));
462  }
463  }
464 }
465 
466 inline void output_vertex(size_t k,std::string & v_out, file_type ft)
467 {
468  if (ft == file_type::ASCII)
469  v_out += "1 " + std::to_string(k) + "\n";
470  else
471  {
472  int tmp;
473  tmp = 1;
474  tmp = swap_endian_lt(tmp);
475  v_out.append((const char *)&tmp,sizeof(int));
476  tmp = k;
477  tmp = swap_endian_lt(tmp);
478  v_out.append((const char *)&tmp,sizeof(int));
479  }
480 }
481 
482 #endif /* SRC_VTKWRITER_GRIDS_UTIL_HPP_ */
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:371
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
size_t size()
Stub size.
Definition: map_vector.hpp:70
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
static std::string get(size_t i, const openfpm::vector< std::string > &prop_names, const std::string &oprp)
Get attribute name.
If it has not dims property defined the object is considered scalar.
Write the vectror property.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
Return the Attributes name from the type.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
This class is an helper to create properties output from scalar and compile-time array elements...
static void write(std::string &v_out, vector &vg, size_t k, iterator &it, file_type ft)
Write the property.
It store one grid.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
check for T to be writable