VTKWriter_grids_st.hpp 14.1 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * VTKWriter_grids_st.hpp
 *
 *  Created on: Sep 3, 2015
 *      Author: Pietro Incardona
 */

#ifndef SRC_VTKWRITER_GRIDS_ST_HPP_
#define SRC_VTKWRITER_GRIDS_ST_HPP_


#include <boost/mpl/pair.hpp>
#include "VTKWriter_grids_util.hpp"
#include "util/util_debug.hpp"
#include "util/convert.hpp"

/*! \brief for each combination in the cell grid you can have different grids
 *
 * \param grid
 * \param cg combination
 *
 */
template <typename Grid>
struct cell_grid
{
	// vector of fused grids
	openfpm::vector<const Grid *> grids;

	// combination
incardon's avatar
incardon committed
30
	comb<Grid::dims> cmb;
incardon's avatar
incardon committed
31
32
33
34
35

	//! construct a cell grid
	cell_grid(const comb<Grid::dims> & cmb)
	:cmb(cmb)
	{}
incardon's avatar
incardon committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

	//! Copy constructor
	inline cell_grid(const cell_grid<Grid> & ele)
	{
		this->operator=(ele);
	}

	//! Copy constructor
	inline cell_grid(cell_grid<Grid> && ele)
	{
		this->operator=(ele);
	}

	//! Copy constructor
	inline cell_grid<Grid> & operator=(const cell_grid<Grid> & ele)
	{
		cmb = ele.cmb;
		grids = ele.grids;

		return *this;
	}

	//! Copy constructor
	inline cell_grid<Grid> & operator=(cell_grid<Grid> && ele)
	{
		cmb = ele.cmb;
		grids = ele.grids;

		return *this;
	}
incardon's avatar
incardon committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
};

template <typename Grid, typename St>
class ele_g_st
{
public:

	typedef Grid value_type;

	ele_g_st(){};

	ele_g_st(const Point<Grid::dims,St> & offset, const Point<Grid::dims,St> & spacing, const Box<Grid::dims,St> & dom)
	:offset(offset),spacing(spacing),dom(dom)
	{}

	std::string dataset;
	//! fused grids
	openfpm::vector<cell_grid<Grid>> g;
	//! offset where it start
	Point<Grid::dims,St> offset;
	// spacing of the grid
	Point<Grid::dims,St> spacing;
	// Part of the grid that is real domain
	Box<Grid::dims,size_t> dom;
incardon's avatar
incardon committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

	//! Copy constructor
	inline ele_g_st(const ele_g_st & ele)
	{
		this->operator=(ele);
	}

	//! Copy constructor
	inline ele_g_st(ele_g_st && ele)
	{
		this->operator=(ele);
	}

	/*! \brief Copy the object
	 *
	 * \patam ele ele_g_st to copy
	 *
	 */
	ele_g_st<Grid,St> & operator=(const ele_g_st & ele)
	{
		dataset = ele.dataset;
		g = ele.g;
		offset = ele.offset;
		spacing = ele.spacing;
		dom = ele.dom;

		return *this;
	}

	/*! \brief Copy the object
	 *
	 * \patam ele ele_g_st to copy
	 *
	 */
	ele_g_st<Grid,St> & operator=(ele_g_st && ele)
	{
		dataset = ele.dataset;
		g = ele.g;
		offset = ele.offset;
		spacing = ele.spacing;
		dom = ele.dom;

		return *this;
	}
incardon's avatar
incardon committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
};

/*!
 *
 * It write a VTK format file in case of grids defined on a space
 *
 * \tparam boost::mpl::pair<G,S>
 *
 * where G is the type of grid S is the type of space, float, double ...
 *
 */
template <typename pair>
class VTKWriter<pair,VECTOR_ST_GRIDS>
{
	//! Vector of grids
	openfpm::vector< ele_g_st<typename pair::first,typename pair::second> > vg;

	/*! \brief Get the total number of points
	 *
	 * \return the total number
	 *
	 */
	size_t get_total()
	{
		size_t tot = 0;

		//! Calculate the full number of vertices
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				if (vg.get(i).g.get(j).grids.size() != 0)
					tot += vg.get(i).g.get(j).grids.get(0)->size();
			}
		}
		return tot;
	}

	/*! \brief It get the vertex properties list
	 *
	 * It get the vertex properties list of the vertex defined as VTK header
	 *
	 * \return a string that define the vertex properties in graphML format
	 *
	 */

	std::string get_vertex_properties_list()
	{
		//! vertex property output string
		std::string v_out;

		// write the number of vertex
		v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";

		// return the vertex properties string
		return v_out;
	}

	/*! \brief It get the vertex properties list
	 *
	 * It get the vertex properties list of the vertex defined as a VTK header
	 *
	 * \return a string that define the vertex properties in graphML format
	 *
	 */

	std::string get_point_properties_list()
	{
		//! vertex property output string
		std::string v_out;

		// write the number of vertex
		v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";

		// return the vertex properties string
		return v_out;
	}

	/*! \brief Create the VTK point definition
	 *
	 */
	std::string get_point_list()
	{
		//! vertex node output string
		std::stringstream v_out;

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				// If there are no grid in this position
				if (vg.get(i).g.get(j).grids.size() == 0)
					continue;

				//! Get the iterator
				auto it = vg.get(i).g.get(j).grids.get(0)->getIterator();

				//! Where the grid is defined
				Box<pair::first::dims,typename pair::second> dom;

				// Calculate the offset of the grid considering its cell position
				Point<pair::first::dims,typename pair::second> middle = vg.get(i).spacing / 2;
				Point<pair::first::dims,typename pair::second> one;
				one.one();
				one = one + toPoint<pair::first::dims,typename pair::second>::convert(vg.get(i).g.get(j).cmb);
				Point<pair::first::dims,typename pair::second> offset = middle * one + vg.get(i).offset;

				// if there is the next element
				while (it.isNext())
				{
					Point<pair::first::dims,typename pair::second> p;
					p = it.get().toPoint();
					p = p * vg.get(i).spacing + offset;

					if (pair::first::dims == 2)
						v_out << p.toString() << " 0.0" << "\n";
					else
						v_out << p.toString() << "\n";

					// increment the iterator
					++it;
				}
			}
		}

		// return the vertex list
		return v_out.str();
	}

incardon's avatar
incardon committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
	/* \brief Get the properties components
	 *
	 * \return the components printed
	 *
	 */
	std::string get_prop_components(size_t k)
	{
		std::stringstream v_out;

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				if (k < vg.get(i).g.get(j).grids.size())
				{
					// get the combination string
					v_out << vg.get(i).g.get(j).cmb.to_string();
				}
			}
		}

		return v_out.str();
	}

incardon's avatar
incardon committed
291
292
293
	/*! \brief Create the VTK properties output
	 *
	 * \param k component
incardon's avatar
incardon committed
294
	 * \param prop property name
incardon's avatar
incardon committed
295
296
	 *
	 */
incardon's avatar
incardon committed
297
	std::string get_properties_output(size_t k, std::string prop_name)
incardon's avatar
incardon committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	{
		//! vertex node output string
		std::stringstream v_out;

		// Check if T is a supported format
		// for now we support only scalar of native type

		typedef typename boost::mpl::at<typename pair::first::value_type::type,boost::mpl::int_<0>>::type ctype;

		std::string type = getType<ctype>();

		// if the type is not supported return
		if (type.size() == 0)
		{
			std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
			return "";
		}

incardon's avatar
incardon committed
316
317
		std::string prp_cp = get_prop_components(k);

incardon's avatar
incardon committed
318
		// Create point data properties
incardon's avatar
incardon committed
319
		v_out << "SCALARS " << prop_name << "_" << prp_cp << " " << type + "\n";
incardon's avatar
incardon committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

		// Default lookup table
		v_out << "LOOKUP_TABLE default\n";

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				// If there are no grid in this position
				if (vg.get(i).g.get(j).grids.size() == 0)
					continue;

				if (k < vg.get(i).g.get(j).grids.size())
				{
					// Grid source
					auto & g_src = *vg.get(i).g.get(j).grids.get(k);

					//! Get the iterator
					auto it = g_src.getIterator();

					//! Where the grid is defined
					Box<pair::first::dims,typename pair::second> dom;

					// if there is the next element
					while (it.isNext())
					{
						auto key = it.get();

						v_out << std::to_string(g_src.template get<0>(key))  << "\n";

						// increment the iterator
						++it;
					}
				}
				else
				{
					// Grid source
					auto & g_src = *vg.get(i).g.get(j).grids.get(0);

					//! Get the iterator
					auto it = g_src.getIterator();

					//! Where the grid is defined
					Box<pair::first::dims,typename pair::second> dom;

					// if there is the next element
					while (it.isNext())
					{
						v_out << "0\n";

						// increment the iterator
						++it;
					}
				}
			}
		}

		// return the vertex list
		return v_out.str();
	}

	/*! \brief Return the output of the domain property
	 *
	 * \return vtk output
	 *
	 */
    std::string lastProp()
	{
		//! vertex node output string
		std::stringstream v_out;

		// Create point data properties
		v_out << "SCALARS domain float\n";

		// Default lookup table
		v_out << "LOOKUP_TABLE default\n";

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				// If there are no grid in this position
				if (vg.get(i).g.get(j).grids.size() == 0)
					continue;

				//! Get the iterator
				auto it = vg.get(i).g.get(j).grids.get(0)->getIterator();

				// if there is the next element
				while (it.isNext())
				{
					if (vg.get(i).dom.isInside(it.get().toPoint()) == true)
						v_out << "1.0\n";
					else
						v_out << "0.0\n";

					// increment the iterator and counter
					++it;
				}
			}
		}

		return v_out.str();
	}

	/*! \brief Get the maximum number of fused grid
	 *
	 * \return the maximum number of fused grids
	 *
	 */
	size_t getMaxFused()
	{
		size_t max = 0;

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				// If there are no grid in this position
				if (vg.get(i).g.get(j).grids.size() > max)
						max = vg.get(i).g.get(j).grids.size();
			}
		}

		return max;
	}

	/*! \brief Create the VTK vertex definition
	 *
	 */
	std::string get_vertex_list()
	{
		//! vertex node output string
		std::string v_out;

		size_t k = 0;

		//! For each sub-domain
		for (size_t i = 0 ; i < vg.size() ; i++)
		{
			// For each position in the cell
			for (size_t j = 0 ; j < vg.get(i).g.size() ; j++)
			{
				// If there are no grid in this position
				if (vg.get(i).g.get(j).grids.size() == 0)
						continue;
				//! For each grid point create a vertex
				auto it = vg.get(i).g.get(j).grids.get(0)->getIterator();

				while (it.isNext())
				{
					v_out += "1 " + std::to_string(k) + "\n";

					++k;
					++it;
				}
			}
		}
		// return the vertex list
		return v_out;
	}

	/*! \brief Get the point data header
	 *
	 * \return a string with the point data header for VTK format
	 *
	 */

	std::string get_point_data_header()
	{
		std::string v_out;

		v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";

		return v_out;
	}

	/*! \brief Append the grid to the sub-domain, if for a sub-domain we have a grid that is overlapping
	 *         fuse them, otherwise create a new combination and grid
	 *
	 * \param id sub-domain id
	 * \param location in the cell of the grid
	 *
	 * \return a valid slot, if does not exist it append the grid at the end with the new combination
	 *
	 */
	void append_grid(size_t id, const typename pair::first & g, const comb<pair::first::dims> & cmb)
	{
		for(size_t i = 0 ; i < vg.get(id).g.size() ; i++)
		{
			// for each defined grid if exist the combination fuse
			if (cmb == vg.get(id).g.get(i).cmb)
			{
				vg.get(id).g.get(i).grids.add(&g);
				return;
			}
		}

		// if the combination does not exist add the grid
		cell_grid< typename pair::first> cg(cmb);
		vg.get(id).g.add(cg);
		vg.get(id).g.last().grids.add(&g);
	}

public:

	/*!
	 *
	 * VTKWriter constructor
	 *
	 */
	VTKWriter()
	{}

	/*! \brief Add grid dataset
	 *
	 * \param i sub-domain id
	 * \param g Grid to add
	 * \param offset grid offset
	 * \param spacing spacing of the grid
	 * \param dom part of the spacethat is the domain
	 *
	 */
	void add(size_t i, 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, const comb<pair::first::dims> & cmb)
	{
		//! Increase the size
		if (i >= vg.size())
			vg.resize(i+1);

		vg.get(i).offset = offset;
		vg.get(i).spacing = spacing;
		vg.get(i).dom = dom;

		// append the grid
		append_grid(i,g,cmb);
	}

	/*! \brief It write a VTK file from a graph
	 *
	 * \tparam prp_out which properties to output [default = -1 (all)]
	 *
	 * \param file path where to write
incardon's avatar
incardon committed
568
	 * \param g_name of the set of grids
incardon's avatar
incardon committed
569
570
571
572
	 * \param file_type specify if it is a VTK BINARY or ASCII file [default = ASCII]
	 *
	 */

incardon's avatar
incardon committed
573
	template<int prp = -1> bool write(std::string file, std::string g_name = "grids" , file_type ft = file_type::ASCII)
incardon's avatar
incardon committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
	{
		// Header for the vtk
		std::string vtk_header;
		// Point list of the VTK
		std::string point_list;
		// Vertex list of the VTK
		std::string vertex_list;
		// Graph header
		std::string vtk_binary_or_ascii;
		// vertex properties header
		std::string point_prop_header;
		// edge properties header
		std::string vertex_prop_header;
		// Data point header
		std::string point_data_header;
		// Data point
		std::string point_data;

		// VTK header
		vtk_header = "# vtk DataFile Version 3.0\n"
incardon's avatar
incardon committed
594
				     + g_name + "\n";
incardon's avatar
incardon committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

		// Choose if binary or ASCII
		if (ft == file_type::ASCII)
		{vtk_header += "ASCII\n";}
		else
		{vtk_header += "BINARY\n";}

		// Data type for graph is DATASET POLYDATA
		vtk_header += "DATASET POLYDATA\n";

		// point properties header
		point_prop_header = get_point_properties_list();

		// Get point list
		point_list = get_point_list();

		// vertex properties header
		vertex_prop_header = get_vertex_properties_list();

		// Get vertex list
		vertex_list = get_vertex_list();

		// Get the point data header
		point_data_header = get_point_data_header();

		// Get the maximum number of fused grids
		size_t mf = getMaxFused();

		// For each property in the vertex type produce a point data
		for (size_t i = 0 ; i < mf ; i++)
incardon's avatar
incardon committed
625
			point_data += get_properties_output(i,g_name);
incardon's avatar
incardon committed
626
627
628
629
630
631
632
633
634

		lastProp();


		// write the file
		std::ofstream ofs(file);

		// Check if the file is open
		if (ofs.is_open() == false)
incardon's avatar
incardon committed
635
		{std::cerr << "Error cannot create the VTK file: " + file + "\n";}
incardon's avatar
incardon committed
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

		ofs << vtk_header << point_prop_header << point_list <<
				vertex_prop_header << vertex_list << point_data_header << point_data;

		// Close the file

		ofs.close();

		// Completed succefully
		return true;
	}
};


#endif /* SRC_VTKWRITER_GRIDS_ST_HPP_ */