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_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 
26 template<bool has_attributes, typename St, typename ele_g, unsigned int i>
28 {
29 public:
30 
36  static std::string get_point_data(const openfpm::vector< ele_g > & vg)
37  {
39  std::string v_out;
40 
41  for (size_t k = 0 ; k < vg.size() ; k++)
42  {
44  auto it = vg.get(k).g.getIterator();
45 
46  // if there is the next element
47  while (it.isNext())
48  {
49  // Print the property
50  v_out += std::to_string(vg.get(k).get_o(it.get()).template get<i>()) + "\n";
51 
52  // increment the iterator and counter
53  ++it;
54  }
55  }
56 
57  return v_out;
58  }
59 
67  static std::string get_point_property_header(const std::string & oprp)
68  {
70  std::string v_out;
71 
72  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
73 
74  // Check if T is a supported format
75  // for now we support only scalar of native type
76  if (std::rank<ctype>::value == 1)
77  {
78  //Get type of the property
79  std::string type = getType<typename std::remove_all_extents<ctype>::type>();
80 
81  // if the type is not supported skip-it
82  if (type.size() == 0)
83  {
84  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
85  return "";
86  }
87 
88  // Create point data properties
89  v_out += "VECTORS " + get_attributes("") + " " + type + "\n";
90  }
91  else
92  {
93  std::string type = getType<ctype>();
94 
95  // if the type is not supported return
96  if (type.size() == 0)
97  return v_out;
98 
99  // Create point data properties
100  v_out += "SCALARS " + get_attributes(oprp) + " " + type + "\n";
101 
102  // Default lookup table
103  v_out += "LOOKUP_TABLE default\n";
104 
105  }
106 
107  // return the vertex list
108  return v_out;
109  }
110 
115  static std::string get_attributes(const std::string & out)
116  {
117  return ele_g::value_type::value_type::attributes::name[i] + out;
118  }
119 };
120 
121 
122 
136 template<typename ele_g, typename St, unsigned int i>
137 class prop_output_g<false,St,ele_g,i>
138 {
139 public:
140 
146  static std::string get_point_property_header(const std::string & oprp)
147  {
149  std::string v_out;
150 
151  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
152 
153  // Check if T is a supported format
154  // for now we support only scalar of native type
155  if (std::rank<ctype>::value == 1)
156  {
157  //Get type of the property
158  std::string type = getType<typename std::remove_all_extents<ctype>::type>();
159 
160  // if the type is not supported skip-it
161  if (type.size() == 0)
162  {
163  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
164  return "";
165  }
166 
167  // Create point data properties
168  v_out += "VECTORS " + get_attributes("") + " " + type + "\n";
169  }
170  else
171  {
172  std::string type = getType<typename std::remove_all_extents<ctype>::type>();
173 
174  // if the type is not supported return
175  if (type.size() == 0)
176  return v_out;
177 
178  // Create point data properties
179  v_out += "SCALARS " + get_attributes(oprp) + " " + type + "\n";
180 
181  // Default lookup table
182  v_out += "LOOKUP_TABLE default\n";
183 
184  }
185 
186  // return the vertex list
187  return v_out;
188  }
189 
193  static std::string get_attributes(const std::string & oprp)
194  {
195  return std::string("attr" + std::to_string(i) + oprp);
196  }
197 };
198 
207 template<typename I, typename ele_g, typename St, typename T>
208 struct meta_prop
209 {
210  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
211  {
212  // actual string size
213  size_t sz = v_out.size();
214 
215  // Produce the point properties header
216  v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("");
217 
218  // If the output has changed, we have to write the properties
219  if (v_out.size() != sz)
220  {
221  // Produce point data
222 
223  for (size_t k = 0 ; k < vg.size() ; k++)
224  {
226  auto it = vg.get(k).g.getIterator();
227 
228  // if there is the next element
229  while (it.isNext())
230  {
231  // Print the property
232  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()) + "\n";
233 
234  // increment the iterator and counter
235  ++it;
236  }
237  }
238  }
239  }
240 };
241 
243 template<typename I, typename ele_g, typename St, typename T, size_t N1>
244 struct meta_prop<I, ele_g,St,T[N1]>
245 {
246  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
247  {
248  // actual string size
249  size_t sz = v_out.size();
250 
251  // Produce the point properties header
252  v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("");
253 
254  // If the output has changed, we have to write the properties
255  if (v_out.size() != sz)
256  {
257  // Produce point data
258 
259  for (size_t k = 0 ; k < vg.size() ; k++)
260  {
262  auto it = vg.get(k).g.getIterator();
263 
264  // if there is the next element
265  while (it.isNext())
266  {
267  // Print the properties
268  for (size_t i1 = 0 ; i1 < N1 ; i1++)
269  {
270  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1]) + " ";
271  }
272  if (N1 == 2)
273  {
274  v_out += "0.0";
275  }
276  v_out += "\n";
277 
278  // increment the iterator and counter
279  ++it;
280  }
281  }
282  }
283  }
284 };
285 
287 template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2>
288 struct meta_prop<I, ele_g,St, T[N1][N2]>
289 {
290  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
291  {
292  for (size_t i1 = 0 ; i1 < N1 ; i1++)
293  {
294  for (size_t i2 = 0 ; i2 < N2 ; i2++)
295  {
296  // actual string size
297  size_t sz = v_out.size();
298 
299  // Produce the point properties header
300  v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("_" + std::to_string(i1) + "_" + std::to_string(i2));
301 
302  // If the output has changed, we have to write the properties
303  if (v_out.size() != sz)
304  {
305  // Produce point data
306 
307  for (size_t k = 0 ; k < vg.size() ; k++)
308  {
310  auto it = vg.get(k).g.getIterator();
311 
312  // if there is the next element
313  while (it.isNext())
314  {
315  // Print the property
316  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2]) + "\n";
317 
318  // increment the iterator and counter
319  ++it;
320  }
321  }
322  }
323  }
324  }
325  }
326 };
327 
329 template<typename I, typename ele_g, typename St,typename T,size_t N1,size_t N2,size_t N3>
330 struct meta_prop<I,ele_g,St,T[N1][N2][N3]>
331 {
332  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
333  {
334  for (size_t i1 = 0 ; i1 < N1 ; i1++)
335  {
336  for (size_t i2 = 0 ; i2 < N2 ; i2++)
337  {
338  for (size_t i3 = 0 ; i3 < N3 ; i3++)
339  {
340  // actual string size
341  size_t sz = v_out.size();
342 
343  // Produce the point properties header
344  v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("_" + std::to_string(i1) + "_" + std::to_string(i2) + "_" + std::to_string(i3));
345 
346  // If the output has changed, we have to write the properties
347  if (v_out.size() != sz)
348  {
349  std::string attr = prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value, St,ele_g,I::value>::get_attributes() + "_";
350 
351  // Produce point data
352 
353  for (size_t k = 0 ; k < vg.size() ; k++)
354  {
356  auto it = vg.get(k).g.getIterator();
357 
358  // if there is the next element
359  while (it.isNext())
360  {
361  // Print the property
362  v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2][i3]) + "\n";
363 
364  // increment the iterator and counter
365  ++it;
366  }
367  }
368  }
369  }
370  }
371  }
372  }
373 };
374 
375 
376 #endif /* SRC_VTKWRITER_GRIDS_UTIL_HPP_ */
This class specialize functions in the case the type T has or not defined attributes.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out)
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out)
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out)
static std::string get_attributes(const std::string &oprp)
Get the attributes name.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out)
static std::string get_point_data(const openfpm::vector< ele_g > &vg)
For each vertex set the value.
static std::string get_point_property_header(const std::string &oprp)
Get the vtk properties header appending a prefix at the end.
static std::string get_point_property_header(const std::string &oprp)
Given a Graph return the point data header for a typename T.
This class is an helper to create properties output from scalar and compile-time array elements...
static std::string get_attributes(const std::string &out)
Get the attributes name.
It store one grid.