main.cpp 4.83 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7
#include "Vector/vector_dist.hpp"

/*
 * ### WIKI 1 ###
 *
 * ## Simple example
 * 
8 9
 * This example show several basic functionalities of the distributed vector, A distributed vector is nothing else than
 * a set of particles in an N dimensional space
incardon's avatar
incardon committed
10 11 12 13 14 15 16 17 18 19 20
 * 
 * ### WIKI END ###
 * 
 */

int main(int argc, char* argv[])
{
	//
	// ### WIKI 2 ###
	//
	// Here we Initialize the library, than we create a uniform random generator between 0 and 1 to to generate particles
Pietro Incardona's avatar
Pietro Incardona committed
21
	// randomly in the domain, we create a Box that define our domain, boundary conditions, and ghost
incardon's avatar
incardon committed
22 23
	//
	
24 25
        // initialize the library
	openfpm_init(&argc,&argv);
incardon's avatar
incardon committed
26

27
	// Here we define our domain a 2D box with internals from 0 to 1.0 for x and y
Pietro Incardona's avatar
Pietro Incardona committed
28
	Box<2,float> domain({0.0,0.0},{1.0,1.0});
29 30

	// Here we define the boundary conditions of our problem
Pietro Incardona's avatar
Pietro Incardona committed
31
    size_t bc[2]={PERIODIC,PERIODIC};
32 33

	// extended boundary around the domain, and the processor domain
Pietro Incardona's avatar
Pietro Incardona committed
34
	Ghost<2,float> g(0.01);
incardon's avatar
incardon committed
35 36 37 38 39
	
	//
	// ### WIKI 3 ###
	//
	// Here we are creating a distributed vector defined by the following parameters
40 41 42 43 44
	//
	// * 2 is the Dimensionality of the space where the objects live
	// * float is the type used for the spatial coordinate of the particles
	// * float,float[3],float[3][3] is the information stored by each particle a scalar float, a vector float[3] and a tensor of rank 2 float[3][3]
	//   the list of properties must be put into an aggregate data astructure aggregate<prop1,prop2,prop3, ... >
incardon's avatar
incardon committed
45
	// 
46 47 48
	// vd is the instantiation of the object
	//
	// The Constructor instead require:
incardon's avatar
incardon committed
49 50 51
	//
	// * Number of particles 4096 in this case
	// * Domain where is defined this structure
52 53
	// * bc boundary conditions
	// * g Ghost
incardon's avatar
incardon committed
54 55 56 57 58
	//
	// The following construct a vector where each processor has 4096 / N_proc (N_proc = number of processor)
	// objects with an undefined position in space. This non-space decomposition is also called data-driven
	// decomposition
	//
59 60 61 62 63 64 65 66 67 68
	vector_dist<2,float, aggregate<float,float[3],float[3][3]> > vd(4096,domain,bc,g);

	// the scalar is the element at position 0 in the aggregate
	const int scalar = 0;

	// the vector is the element at position 1 in the aggregate
	const int vector = 1;

	// the tensor is the element at position 2 in the aggregate
	const int tensor = 2;
incardon's avatar
incardon committed
69 70 71 72

	//
	// ### WIKI 5 ###
	//
73
	// Get an iterator that go through the 4096 particles, in an undefined position state and define its position
incardon's avatar
incardon committed
74
	//
75
	auto it = vd.getDomainIterator();
incardon's avatar
incardon committed
76 77 78 79 80

	while (it.isNext())
	{
		auto key = it.get();

81 82 83 84 85
		// we define x, assign a random position between 0.0 and 1.0
		vd.getPos(key)[0] = rand() / RAND_MAX;

		// we define y, assign a random position between 0.0 and 1.0
		vd.getPos(key)[1] = rand() / RAND_MAX;
incardon's avatar
incardon committed
86

87
		// next particle
incardon's avatar
incardon committed
88 89 90 91 92 93
		++it;
	}

	//
	// ### WIKI 6 ###
	//
94 95 96
	// Once we define the position, we distribute them according to the default space decomposition
	// The default decomposition is created even before assigning the position to the object. It determine
	// which part of space each processor manage
incardon's avatar
incardon committed
97 98 99 100 101 102 103 104 105
	//
	vd.map();

	//
	// ### WIKI 7 ###
	//
	// We get the object that store the decomposition, than we iterate again across all the objects, we count them
	// and we confirm that all the particles are local
	//
106
	//Counter we use it later
incardon's avatar
incardon committed
107
	size_t cnt = 0;
108 109

	// Get the space decomposition
incardon's avatar
incardon committed
110 111
	auto & ct = vd.getDecomposition();

112 113 114 115
	// Get a particle iterator
	it = vd.getDomainIterator();

	// For each particle ...
incardon's avatar
incardon committed
116 117
	while (it.isNext())
	{
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		// ... p
		auto p = it.get();

		// we set the properties of the particle p
		
                // the scalar property
		vd.template getProp<scalar>(p) = 1.0;

		vd.template getProp<vector>(p)[0] = 1.0;
		vd.template getProp<vector>(p)[1] = 1.0;
		vd.template getProp<vector>(p)[2] = 1.0;

		vd.template getProp<tensor>(p)[0][0] = 1.0;
		vd.template getProp<tensor>(p)[0][1] = 1.0;
		vd.template getProp<tensor>(p)[0][2] = 1.0;
		vd.template getProp<tensor>(p)[1][0] = 1.0;
		vd.template getProp<tensor>(p)[1][1] = 1.0;
		vd.template getProp<tensor>(p)[1][2] = 1.0;
		vd.template getProp<tensor>(p)[2][0] = 1.0;
		vd.template getProp<tensor>(p)[2][1] = 1.0;
		vd.template getProp<tensor>(p)[2][2] = 1.0;

		// increment the counter
incardon's avatar
incardon committed
141 142
		cnt++;

143
		// next particle
incardon's avatar
incardon committed
144 145 146 147 148 149 150 151 152 153 154
		++it;
	}

	//
	// ### WIKI 8 ###
	//
	// cnt contain the number of object the local processor contain, if we are interested to count the total number across the processor
	// we can use the function add, to sum across processors. First we have to get an instance of Vcluster, queue an operation of add with
	// the variable count and finaly execute. All the operations are asynchronous, execute work like a barrier and ensure that all the 
	// queued operations are executed
	//
155 156
	
	auto & v_cl = create_vcluster();
incardon's avatar
incardon committed
157 158 159 160
	v_cl.sum(cnt);
	v_cl.execute();
	
	//
161 162 163 164 165
	// ### WIKI 9 ###
	//
	// Output the particle position for each processor
	//

166
	vd.write("output",VTK_WRITER);
167 168 169

	//
	// ### WIKI 10 ###
incardon's avatar
incardon committed
170 171 172
	//
	// Deinitialize the library
	//
Pietro Incardona's avatar
Pietro Incardona committed
173
	openfpm_finalize();
incardon's avatar
incardon committed
174
}