CartesianGraphFactory.hpp 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * CartesianGraphFactory.hpp
 *
 *  Created on: Nov 28, 2014
 *      Author: i-bird
 */

#ifndef CARTESIANGRAPHFACTORY_HPP_
#define CARTESIANGRAPHFACTORY_HPP_

incardon's avatar
incardon committed
11 12
#include "Vector/map_vector.hpp"
#include "Graph/map_graph.hpp"
13
#include "Grid/grid_sm.hpp"
14
#include "Space/Shape/Box.hpp"
incardon's avatar
incardon committed
15
#include "Space/Shape/HyperCube.hpp"
16

17 18
#define NO_VERTEX_ID -1

19 20 21 22 23 24
/*! \brief Operator to fill the property 'prp' with the linearization of indexes
 *
 *  \tparam dim Dimension of the space
 *  \tparam G_v Graph
 *  \tparam prp Property to fill
 */
incardon's avatar
incardon committed
25 26 27
template<unsigned int dim, typename G_v, int prp>
struct fill_id
{
28
	static inline void fill(G_v & g_v, const grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
incardon's avatar
incardon committed
29 30 31 32
	{
		g_v.template get<prp>() = gs.LinId(gk);
	}
};
33 34 35 36 37
/*! \brief Operator to fill the property in case there are no properties
 *
 *  \tparam dim Dimension of the space
 *  \tparam G_v Graph
 */
incardon's avatar
incardon committed
38
template<unsigned int dim, typename G_v>
39
struct fill_id<dim, G_v, NO_VERTEX_ID>
incardon's avatar
incardon committed
40
{
41
	static inline void fill(G_v & g_v, const grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
incardon's avatar
incardon committed
42 43 44 45
	{
	}
};

incardon's avatar
incardon committed
46
/*! \brief This class work as a functor
47
 *
incardon's avatar
incardon committed
48 49 50 51 52 53 54 55
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
56
 *
incardon's avatar
incardon committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 * \tparam is_stub when is true, produce a trivial operator(),
 *         to use when v is an empty vector to avoid compilation error
71 72 73
 *
 */

74
template<unsigned int dim, int lin_id, typename dT, typename G_v, typename v, int impl>
incardon's avatar
incardon committed
75
class fill_prop
76
{
incardon's avatar
incardon committed
77
	//! Reference to an array containing the spacing
78
	const dT (&szd)[dim];
incardon's avatar
incardon committed
79 80 81 82 83 84

	//! grid_key_dx Reference containing the actual position
	grid_key_dx<dim> & gk;

	//! Vertex object to fill
	G_v & g_v;
85

incardon's avatar
incardon committed
86
	//! grid info
87 88 89 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 134 135 136
	const grid_sm<dim, void> & gs;

public:

	//! Fill the object from where to take the properties
	fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs) :
			szd(szd), gk(gk), g_v(g_v), gs(gs)
	{
	}

	//! It call the function for each property we want to copy
	template<typename T>
	void operator()(T& t) const
	{
		typedef typename boost::fusion::result_of::at<v, boost::mpl::int_<T::value>>::type t_val;

		g_v.template get<t_val::value>() = gk.get(T::value) * szd[T::value];
		fill_id<dim, G_v, lin_id>::fill(g_v, gk, gs);
	}
};

/*! \brief This class work as a functor
 *
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
 *
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 *
 */

template<unsigned int dim, int lin_id, typename dT, typename G_v, typename v>
class fill_prop<dim, lin_id, dT, G_v, v, 0>
{
incardon's avatar
incardon committed
137

138 139
public:

incardon's avatar
incardon committed
140
	//! Fill the object from where to take the properties
141 142 143
	fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs)
	{
	}
incardon's avatar
incardon committed
144 145

	//! It call the function for each property we want to copy
146 147 148 149
	template<typename T>
	void operator()(T& t) const
	{
	}
incardon's avatar
incardon committed
150 151
};

incardon's avatar
incardon committed
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
/*! \brief This class work as a functor
 *
 * For each number in the boost::mpl::vector (for example 3 6) set the properties of the vertex at the
 * specified id (3 6) with pos[d] * spacing[d] with d running from 0 to 1, pos[d] the position id of the vertex
 * spacing the grid spacing
 *
 * Example
 *
 * if we give a grid_key of dimension 2 4x4 the expression "pos[d] * spacing[d]"
 * will assume the value
 *
 * (0.0 0.0) (0.25 0.0) ...... (1.0 0.0)
 * (0.0 0.25)................. (1.0 0.25)
 * ....................................
 * (0.0 1.0).................. (1.0 1.0)
 *
 * and the properties 3 6 will be filled with the numbers 0.0 0.0    .......  1.0 1.0
 * progressively
 *
 * \tparam dim Dimensionality of the cartesian grid
 * \tparam dT type of the domain
 * \tparam G_v vertex type object
 * \tparam v boost::mpl::vector containing all the index to fill
 *
 */

178 179
template<unsigned int dim, int lin_id, typename dT, typename G_v, typename v>
class fill_prop<dim, lin_id, dT, G_v, v, 2>
incardon's avatar
incardon committed
180 181
{

182 183 184 185 186 187 188 189 190 191 192 193
	//! Reference to an array containing the spacing
	const dT (&szd)[dim];

	//! grid_key_dx Reference containing the actual position
	grid_key_dx<dim> & gk;

	//! Vertex object to fill
	G_v & g_v;

	//! grid info
	const grid_sm<dim, void> & gs;

incardon's avatar
incardon committed
194 195 196
public:

	//! Fill the object from where to take the properties
197 198 199 200
	fill_prop(G_v & g_v, const dT (&szd)[dim], grid_key_dx<dim> & gk, const grid_sm<dim, void> & gs) :
			szd(szd), gk(gk), g_v(g_v), gs(gs)
	{
	}
incardon's avatar
incardon committed
201 202

	//! It call the function for each property we want to copy
203 204 205 206
	template<typename T>
	void operator()(T& t) const
	{
		typedef typename boost::fusion::result_of::at<v, boost::mpl::int_<0>>::type t_val;
207 208 209 210 211 212 213
		typedef typename boost::mpl::at<typename G_v::T_type::type,t_val>::type s_type;

		for (size_t i = 0 ; i < std::extent<s_type>::value ; i++)
			g_v.template get<t_val::value>()[i] = 0.0;

		for (size_t i = 0 ; i < dim ; i++)
			g_v.template get<t_val::value>()[i] = gk.get(i) * static_cast<float>(szd[i]);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

		fill_id<dim, G_v, lin_id>::fill(g_v, gk, gs);
	}
};

/*! \brief Operator for vector and scalar property
 *
 * \tparam i Size of the property
 * \tparam p Type of the property
 * \tparam Graph Graph
 * \tparam pos Array of properties
 */
template<int i, typename p, typename Graph, int ... pos>
struct fill_prop_by_type
{

	typedef typename boost::mpl::at<p, boost::mpl::int_<0>>::type v_element;
	typedef typename boost::mpl::at<typename Graph::V_type::type, v_element>::type pos_prop_type;

	enum
	{
		value = ((sizeof...(pos) != 0) * (std::is_array<pos_prop_type>::value + 1))
	};

incardon's avatar
incardon committed
238 239
};

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
/*! \brief Operator for vector and scalar property in the case there are no properties
 *
 * \tparam i Size of the property
 * \tparam p Type of the property
 * \tparam Graph Graph
 * \tparam pos Array of properties
 */
template<typename p, typename Graph, int ... pos>
struct fill_prop_by_type<0, p, Graph, pos...>
{
	enum
	{
		value = 0
	};

};
incardon's avatar
incardon committed
256

incardon's avatar
incardon committed
257 258 259 260 261 262 263 264
/*! \brief Graph constructor function specialization
 *
 * On C++ partial function specialization is not allowed, so we need a class to do it
 *
 * \see CartesianGraphFactory method construct
 *
 */

265
template<unsigned int dim, int lin_id, typename Graph, int se, typename T, unsigned int dim_c, int ... pos>
incardon's avatar
incardon committed
266 267 268 269
class Graph_constructor_impl
{
public:
	//! Construct cartesian graph
270
	static Graph construct(const size_t (& sz)[dim], Box<dim,T> dom, const size_t(& bc)[dim])
271 272 273 274
	{
		// Calculate the size of the hyper-cubes on each dimension
		T szd[dim];

275 276 277 278
		for (size_t i = 0; i < dim; i++)
		{
			szd[i] = (dom.getHigh(i) - dom.getLow(i)) / sz[i];
		}
279 280 281 282 283 284 285

		//! Construct an hyper-cube of dimension dim

		HyperCube<dim> hc;

		// Construct a grid info

286
		grid_sm<dim, void> g(sz);
287 288 289 290 291 292 293 294 295 296

		// Create a graph with the number of vertices equal to the number of
		// grid point

		//! Graph to construct

		Graph gp(g.size());

		/******************
		 *
incardon's avatar
incardon committed
297 298
		 * Create the edges and fill spatial
		 * information properties
299 300 301 302 303 304 305 306
		 *
		 ******************/

		//! Construct a key iterator
		grid_key_dx_iterator<dim> k_it(g);

		//! Iterate through all the elements

incardon's avatar
incardon committed
307
		while (k_it.isNext())
308 309 310
		{
			grid_key_dx<dim> key = k_it.get();

incardon's avatar
incardon committed
311
			// Vertex object
incardon's avatar
incardon committed
312

incardon's avatar
incardon committed
313 314
			auto obj = gp.vertex(g.LinId(key));

315 316
			typedef typename to_boost_vmpl<pos...>::type p;

incardon's avatar
incardon committed
317
			// vertex spatial properties functor
incardon's avatar
incardon committed
318

319
			fill_prop<dim, lin_id, T, decltype(gp.vertex(g.LinId(key))), typename to_boost_vmpl<pos...>::type, fill_prop_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);
incardon's avatar
incardon committed
320 321 322

			// fill properties

323
			boost::mpl::for_each<boost::mpl::range_c<int, 0, sizeof...(pos)> >(flp);
incardon's avatar
incardon committed
324

325 326
			// Get the combinations of dimension d

327
			for (long int d = dim-1 ; d >= dim_c ; d--)
328 329 330 331 332 333 334
			{
				// create the edges for that dimension

				std::vector<comb<dim>> c = hc.getCombinations_R(d);

				// for each combination calculate a safe linearization and create an edge

335
				for (size_t j = 0; j < c.size(); j++)
336 337 338 339 340 341 342
				{
					// Calculate the element size

					T ele_sz = 0;

					// for each dimension multiply and reduce

343

344
					for (size_t s = 0 ; s < dim ; s++)
incardon's avatar
incardon committed
345
						ele_sz += szd[s] * abs(c[j][s]);
346 347 348 349 350

					// Calculate the end point vertex id
					// Calculate the start point id

					size_t start_v = g.LinId(key);
351

352
					size_t end_v = g.template LinId<CheckExistence>(key,c[j].getComb(),bc);
353

incardon's avatar
incardon committed
354
					// Add an edge and set the the edge property to the size of the face (communication weight)
355
					gp.template addEdge<CheckExistence>(start_v, end_v).template get<se>() = ele_sz;
356 357 358
				}
			}

incardon's avatar
incardon committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
			// Fill vertex properties

			++k_it;
		}

		return gp;
	}
};

/*! \brief Graph constructor function specialization
 *
 * On C++ partial function specialization is not allowed, so we need a class to do it
 * This specialization handle the case when we have NO_EDGE option active
 *
 * \see CartesianGraphFactory method construct
 *
 */

377 378
template<unsigned int dim, int lin_id, typename Graph, typename T, unsigned int dim_c, int ... pos>
class Graph_constructor_impl<dim, lin_id, Graph, NO_EDGE, T, dim_c, pos...>
incardon's avatar
incardon committed
379 380 381
{
public:
	//! Construct cartesian graph
382
	static Graph construct(const size_t ( & sz)[dim], Box<dim,T> dom, const size_t(& bc)[dim])
incardon's avatar
incardon committed
383 384 385 386 387
	{
		// Calculate the size of the hyper-cubes on each dimension

		T szd[dim];

388 389 390 391
		for (size_t i = 0; i < dim; i++)
		{
			szd[i] = (dom.getHigh(i) - dom.getLow(i)) / sz[i];
		}
incardon's avatar
incardon committed
392 393 394 395 396 397 398

		//! Construct an hyper-cube of dimension dim

		HyperCube<dim> hc;

		// Construct a grid info

399
		grid_sm<dim, void> g(sz);
incardon's avatar
incardon committed
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

		// Create a graph with the number of vertices equal to the number of
		// grid point

		//! Graph to construct

		Graph gp(g.size());

		/******************
		 *
		 * Create the edges and fill spatial
		 * information properties
		 *
		 ******************/

		//! Construct a key iterator
		grid_key_dx_iterator<dim> k_it(g);

		//! Iterate through all the elements

		while (k_it.isNext())
		{
			grid_key_dx<dim> key = k_it.get();

			// Vertex object
			auto obj = gp.vertex(g.LinId(key));

427 428
			typedef typename to_boost_vmpl<pos...>::type p;

incardon's avatar
incardon committed
429
			// vertex spatial properties functor
430 431

			fill_prop<dim, lin_id, T, decltype(gp.vertex(g.LinId(key))), typename to_boost_vmpl<pos...>::type, fill_prop_by_type<sizeof...(pos), p, Graph, pos...>::value> flp(obj, szd, key, g);
incardon's avatar
incardon committed
432 433 434

			// fill properties

435
			boost::mpl::for_each_ref<boost::mpl::range_c<int, 0, sizeof...(pos)> >(flp);
incardon's avatar
incardon committed
436 437 438

			// Get the combinations of dimension d

439
			for (long int d = dim-1 ; d >= dim_c ; d--)
incardon's avatar
incardon committed
440 441 442 443 444 445 446
			{
				// create the edges for that dimension

				std::vector<comb<dim>> c = hc.getCombinations_R(d);

				// for each combination calculate a safe linearization and create an edge

447
				for (size_t j = 0; j < c.size(); j++)
incardon's avatar
incardon committed
448 449 450 451 452
				{
					// Calculate the end point vertex id
					// Calculate the start point id

					size_t start_v = g.LinId(key);
453

Pietro Incardona's avatar
Pietro Incardona committed
454
					size_t end_v = g.template LinId<CheckExistence>(key,c[j].getComb(),bc);
incardon's avatar
incardon committed
455 456

					// Add an edge and set the the edge property to the size of the face (communication weight)
457
					gp.template addEdge<CheckExistence>(start_v, end_v);
incardon's avatar
incardon committed
458 459 460 461 462
				}
			}

			// Fill vertex properties

463 464 465
			++k_it;
		}

incardon's avatar
incardon committed
466
		return gp;
incardon's avatar
incardon committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	}
};

/*! \brief This class construct a cartesian graph
 *
 * This class construct a cartesian graph
 *
 * \param dim dimensionality of the cartesian grid
 *
 */

template<unsigned int dim, typename Graph>
class CartesianGraphFactory
{

public:
483

incardon's avatar
incardon committed
484 485 486 487 488 489 490 491
	/*!
	 *
	 * \brief Construct a cartesian graph, with V and E edge properties
	 *
	 * Construct a cartesian graph, with V and E edge properties
	 *
	 * Each vertex is a subspace (Hyper-cube) of dimension dim, each vertex is
	 * connected with an edge if two vertex (Hyper-cube) share a element of dimension grater than
incardon's avatar
incardon committed
492 493
	 * dim_c. One property can be used to store the contact size or the d-dimensional
	 * surface in common between two connected hyper-cube.
incardon's avatar
incardon committed
494 495 496 497
	 *
	 * \param sz Vector that store the size of the grid on each dimension
	 * \param dom Box enclosing the physical domain
	 *
incardon's avatar
incardon committed
498 499 500
	 * \tparam se Indicate which properties fill with the contact size. The
	 *           contact size is the point, line , surface, d-dimensional object size
	 *           in contact (in common) between two hyper-cube. NO_EDGE indicate
incardon's avatar
incardon committed
501 502 503
	 *           no property will store this information
	 * \tparam T type of the domain like (int real complex ... )
	 * \tparam dim_c Connectivity dimension
incardon's avatar
incardon committed
504
	 * \tparam pos... (optional)one or more integer indicating the spatial properties
incardon's avatar
incardon committed
505 506
	 *
	 */
507

508 509
/*	template <int se,typename T, unsigned int dim_c, int... pos>
	static Graph construct(const size_t (& sz)[dim], Box<dim,T> dom )
incardon's avatar
incardon committed
510
	{
511 512
		return Graph_constructor_impl<dim,Graph,se,T,dim_c,pos...>::construct(sz,dom,bc);
	}*/
incardon's avatar
incardon committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526

	/*!
	 *
	 * \brief Construct a cartesian graph, with V and E edge properties
	 *
	 * Construct a cartesian graph, with V and E edge properties
	 *
	 * Each vertex is a subspace (Hyper-cube) of dimension dim, each vertex is
	 * connected with an edge if two vertex (Hyper-cube) share a element of dimension grater than
	 * dim_c. One property can be used to store the contact size or the d-dimensional
	 * surface in common between two connected hyper-cube.
	 *
	 * \param sz Vector that store the size of the grid on each dimension
	 * \param dom Box enclosing the physical domain
527
	 * \param bc boundary conditions {PERIODIC and NON_PERIODIC}
incardon's avatar
incardon committed
528 529 530 531 532
	 *
	 * \tparam se Indicate which properties fill with the contact size. The
	 *           contact size is the point, line , surface, d-dimensional object size
	 *           in contact (in common) between two hyper-cube. NO_EDGE indicate
	 *           no property will store this information
533
	 * \tparam id_prp property 'id' that stores the vertex id (with -1 it skip)
incardon's avatar
incardon committed
534 535 536 537 538
	 * \tparam T type of the domain like (int real complex ... )
	 * \tparam dim_c Connectivity dimension
	 * \tparam pos... (optional)one or more integer indicating the spatial properties
	 *
	 */
539
	template<int se, int id_prp, typename T, unsigned int dim_c, int ... pos>
540
	static Graph construct(const size_t (&sz)[dim], Box<dim, T> dom, const size_t (& bc)[dim])
incardon's avatar
incardon committed
541
	{
542
		return Graph_constructor_impl<dim, id_prp, Graph, se, T, dim_c, pos...>::construct(sz, dom, bc);
543 544 545 546
	}
};

#endif /* CARTESIANGRAPHFACTORY_HPP_ */