VTKWriter_grids_st.hpp 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
 *
incardon's avatar
incardon committed
19
 * \tparam Grid type of grid
20 21 22 23 24
 *
 */
template <typename Grid>
struct cell_grid
{
25
	//! vector of fused grids
26 27
	openfpm::vector<const Grid *> grids;

28 29
	//! combination
	//! (used to calculate the grid shift from the starting point of the cell)
30
	comb<Grid::dims> cmb;
31

incardon's avatar
incardon committed
32 33
	cell_grid() {}

34 35 36 37
	//! construct a cell grid
	cell_grid(const comb<Grid::dims> & cmb)
	:cmb(cmb)
	{}
38

39
	//! copy contructor
incardon's avatar
incardon committed
40
	cell_grid(const cell_grid<Grid> & cg)
41
	{
incardon's avatar
incardon committed
42
		this->operator=(cg);
43 44
	}

45
	//! copy constructor
incardon's avatar
incardon committed
46
	cell_grid(cell_grid<Grid> && cg)
47
	{
incardon's avatar
incardon committed
48
		this->operator=(cg);
49 50
	}

incardon's avatar
incardon committed
51 52 53 54 55 56 57 58
	/*! \brief Copy the cell grid
	 *
	 * \param cg cell_grid to copy
	 *
	 * \return itself
	 *
	 */
	cell_grid<Grid> & operator=(const cell_grid<Grid> & cg)
59
	{
incardon's avatar
incardon committed
60 61
		cmb = cg.cmb;
		grids = cg.grids;
62 63 64 65

		return *this;
	}

incardon's avatar
incardon committed
66 67 68 69 70 71 72 73
	/*! \brief Copy the cell grid
	 *
	 * \param cg cell_grid to copy
	 *
	 * \return itself
	 *
	 */
	cell_grid<Grid> & operator=(cell_grid<Grid> && cg)
74
	{
incardon's avatar
incardon committed
75 76
		cmb = cg.cmb;
		grids = cg.grids;
77 78 79

		return *this;
	}
80 81
};

82 83 84 85 86 87
/*! \brief convert a staggered element into a string for vtk write
 *
 * \tparam Grid type of the grid
 * \tparam St space type
 *
 */
88 89 90 91 92
template <typename Grid, typename St>
class ele_g_st
{
public:

93
	//! grid type
94 95
	typedef Grid value_type;

96
	//! constructor
97 98
	ele_g_st(){};

99 100 101 102 103 104 105 106 107 108
	/*! \brief convert a staggered grid property into a string
	 *
	 * \param offset shift of the staggered element
	 * \param spacing of the grid
	 * \param Part of the grid that is real domain
	 *
	 */
	ele_g_st(const Point<Grid::dims,St> & offset,
			 const Point<Grid::dims,St> & spacing,
			 const Box<Grid::dims,St> & dom)
109 110 111
	:offset(offset),spacing(spacing),dom(dom)
	{}

112
	//! output string
113 114 115
	std::string dataset;
	//! fused grids
	openfpm::vector<cell_grid<Grid>> g;
116
	//! offset where it start the grid
117
	Point<Grid::dims,St> offset;
118
	//! spacing of the grid
119
	Point<Grid::dims,St> spacing;
120
	//! Part of the grid that is real domain
121
	Box<Grid::dims,size_t> dom;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	//! 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
	 *
137 138 139
	 * \param ele ele_g_st to copy
	 *
	 * \return itself
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	 *
	 */
	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
	 *
155 156 157
	 * \param ele ele_g_st to copy
	 *
	 * \return itself
158 159 160 161 162 163 164 165 166 167 168 169
	 *
	 */
	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;
	}
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
};

/*!
 *
 * 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
249 250
	 *
	 * \return the list of points
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	 *
	 */
	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);
Pietro Incardona's avatar
Pietro Incardona committed
279
				Point<pair::first::dims,typename pair::second> offset = pmul(middle,one) + vg.get(i).offset;
280 281 282 283 284 285

				// if there is the next element
				while (it.isNext())
				{
					Point<pair::first::dims,typename pair::second> p;
					p = it.get().toPoint();
Pietro Incardona's avatar
Pietro Incardona committed
286
					p = pmul(p,vg.get(i).spacing) + offset;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

					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();
	}

303 304 305
	/* \brief It generate a name for the property cell component
	 *
	 * \param k component in the cell
incardon's avatar
incardon committed
306
	 *
307
	 * \return property name
incardon's avatar
incardon committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	 *
	 */
	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();
	}

331 332 333
	/*! \brief Create the VTK properties output
	 *
	 * \param k component
incardon's avatar
incardon committed
334
	 * \param prop property name
335
	 *
336 337
	 * \return the property output string for the grid
	 *
338
	 */
incardon's avatar
incardon committed
339
	std::string get_properties_output(size_t k, std::string prop_name)
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	{
		//! 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
358 359
		std::string prp_cp = get_prop_components(k);

360
		// Create point data properties
incardon's avatar
incardon committed
361
		v_out << "SCALARS " << prop_name << "_" << prp_cp << " " << type + "\n";
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

		// 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
496 497
	 *
	 * \return the string with the vertices as string
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
	 *
	 */
	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
551 552
	 * \param g grid to output
	 * \param cmb position of the grid
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	 *
	 * \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
592
	 * \param cmb position of the grid
593 594
	 *
	 */
595 596 597 598 599 600
	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)
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	{
		//! 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
619
	 * \param g_name of the set of grids
620 621 622
	 * \param ft specify if it is a VTK BINARY or ASCII file [default = ASCII]
	 *
	 * \return true if the file is succeful written
623 624 625
	 *
	 */

626 627 628
	template<int prp = -1> bool write(std::string file,
			                          std::string g_name = "grids",
									  file_type ft = file_type::ASCII)
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	{
		// 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
649
				     + g_name + "\n";
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

		// 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
680
			point_data += get_properties_output(i,g_name);
681 682 683 684 685 686 687 688 689

		lastProp();


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

		// Check if the file is open
		if (ofs.is_open() == false)
incardon's avatar
incardon committed
690
		{std::cerr << "Error cannot create the VTK file: " + file + "\n";}
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

		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_ */