main.cpp 11.5 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6
 /*! \page Vector Vector
 *
 * \subpage Vector_0_simple
 * \subpage Vector_1_celllist
 * \subpage Vector_2_expression
 * \subpage Vector_3_md
7 8
 * \subpage Vector_4_reo_root
 * \subpage Vector_4_cp
Pietro Incardona's avatar
Pietro Incardona committed
9
 * \subpage Vector_4_mp_cl
incardon's avatar
incardon committed
10 11
 * \subpage Vector_5_md_vl_sym
 * \subpage Vector_6_complex_usage
Pietro Incardona's avatar
Pietro Incardona committed
12 13 14
 *
 */

incardon's avatar
incardon committed
15

Pietro Incardona's avatar
Pietro Incardona committed
16 17 18 19 20 21 22 23 24
/*!
 * \page Vector_0_simple Vector 0 simple
 *
 *
 * [TOC]
 *
 *
 * # Simple Vector example # {#simple_vector_example}
 *
incardon's avatar
incardon committed
25
 *
26 27
 * 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
Pietro Incardona's avatar
Pietro Incardona committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * \htmlonly
 * <a href="#" onclick="hide_show('vector-video-1')" >Video 1</a>
 * <div style="display:none" id="vector-video-1">
 * <video id="vid1" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-1.mp4" type="video/mp4"></video>
 * <script>video_anim('vid1',100,230)</script>
 * </div>
 * <a href="#" onclick="hide_show('vector-video-2')" >Video 2</a>
 * <div style="display:none" id="vector-video-2">
 * <video id="vid2" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-2.mp4" type="video/mp4"></video>
 * <script>video_anim('vid2',21,1590)</script>
 * </div>
 * \endhtmlonly
 *
 * ## inclusion ## {#inclusion}
 *
 * In order to use distributed vectors in our code we have to include the file Vector/vector_dist.hpp
 *
 * \snippet Vector/0_simple/main.cpp inclusion
 *
incardon's avatar
incardon committed
48 49
 */

Pietro Incardona's avatar
Pietro Incardona committed
50 51 52 53
//! \cond [inclusion] \endcond
#include "Vector/vector_dist.hpp"
//! \cond [inclusion] \endcond

incardon's avatar
incardon committed
54 55
int main(int argc, char* argv[])
{
Pietro Incardona's avatar
Pietro Incardona committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Initialization ## {#e0_s_init}
	 *
	 *  Here we
	 *  * Initialize the library
	 *  * we create a Box that define our domain
	 *  * An array that define out boundary conditions
	 *  * A Ghost object that will define the extension of the ghost part in physical units
	 *
	 *
	 *
	 * \snippet Vector/0_simple/main.cpp Initialization and parameters
	 *
	 * \htmlonly
	 * <a href="#" onclick="hide_show('vector-video-5')" >Video 1</a>
	 * <div style="display:none" id="vector-video-5">
	 * <video id="vid5" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-4.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid5',447,513)</script>
	 * </div>
	 * <a href="#" onclick="hide_show('vector-video-4')" >Video 2</a>
	 * <div style="display:none" id="vector-video-4">
	 * <video id="vid4" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-4.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid4',594,1023)</script>
	 * </div>
	 * \endhtmlonly
	 *
	 */

	//! \cond [Initialization and parameters] \endcond

    // initialize the library
90
	openfpm_init(&argc,&argv);
incardon's avatar
incardon committed
91

92
	// 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
93
	Box<2,float> domain({0.0,0.0},{1.0,1.0});
94 95

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

	// extended boundary around the domain, and the processor domain
Pietro Incardona's avatar
Pietro Incardona committed
99
	Ghost<2,float> g(0.01);
incardon's avatar
incardon committed
100
	
Pietro Incardona's avatar
Pietro Incardona committed
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 137 138 139 140 141 142
	//! \cond [Initialization and parameters] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Vector instantiation ## {#e0_s_vector_inst}
	 *
	 * Here we are creating a distributed vector defined by the following parameters
	 *
	 * * 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 structure aggregate<prop1,prop2,prop3, ... >
	 *
	 * vd is the instantiation of the object
	 *
	 * The Constructor instead require:
	 *
	 * * Number of particles 4096 in this case
	 * * Domain where is defined this structure
	 * * bc boundary conditions
	 * * g Ghost
	 *
	 * 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
	 *
	 *
	 * \snippet Vector/0_simple/main.cpp vector instantiation
	 *
	 * \htmlonly
	 * <a href="#" onclick="hide_show('vector-video-3')" >Video</a>
	 * <div style="display:none" id="vector-video-3">
	 * <video id="vid3" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-4.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid3',1047,1370)</script>
	 * </div>
	 * \endhtmlonly
	 *
	 */

	//! \cond [vector instantiation] \endcond

143 144 145 146 147 148 149 150 151 152
	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
153

Pietro Incardona's avatar
Pietro Incardona committed
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
	//! \cond [vector instantiation] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Assign position ## {#e0_s_assign_pos}
	 *
	 * Get an iterator that go through the 4096 particles. Initially all the particles
	 *  has an undefined position state. In this cycle we define its position. In this
	 * example we use iterators. Iterators are convenient way to explore/iterate data-structures in an
	 * convenient and easy way
	 *
	 *  \snippet Vector/0_simple/main.cpp assign position
	 *
	 * \htmlonly
	 * <a href="#" onclick="hide_show('vector-video-13')" >Iterator Video</a>
	 * <div style="display:none" id="vector-video-13">
	 * <video id="vid13" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-8.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid13',31,1362)</script>
	 * </div>
	 * \endhtmlonly
	 *
	 */

	//! \cond [assign position] \endcond

180
	auto it = vd.getDomainIterator();
incardon's avatar
incardon committed
181 182 183 184 185

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

186
		// we define x, assign a random position between 0.0 and 1.0
Pietro Incardona's avatar
Pietro Incardona committed
187
		vd.getPos(key)[0] = (float)rand() / RAND_MAX;
188 189

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

192
		// next particle
incardon's avatar
incardon committed
193 194 195
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
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
	//! \cond [assign position] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Mapping particles ## {#e0_s_map}
	 *
	 * On a parallel program, once we define the position, we distribute the particles according to the underlying space decomposition
	 * The default decomposition is created even before assigning the position to the object, and is calculated
	 * giving to each processor an equal portion of space minimizing the surface to reduce communication.
	 *
	 * \snippet Vector/0_simple/main.cpp map
	 *
	 * \htmlonly
	 * <a href="#" onclick="hide_show('vector-video-11')" >Parallelization Video</a>
	 * <div style="display:none" id="vector-video-11">
	 * <video id="vid11" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-5.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid11',440,995)</script>
	 * </div>
	 * <a href="#" onclick="hide_show('vector-video-8')" >Video 1</a>
	 * <div style="display:none" id="vector-video-8">
	 * <video id="vid8" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-6.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid8',0,483)</script>
	 * </div>
	 * <a href="#" onclick="hide_show('vector-video-9')" >Video 2</a><br>
	 * <div style="display:none" id="vector-video-9">
	 * <video id="vid9" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-6.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid9',1009,1041)</script>
	 * </div>
	 * <a href="#" onclick="hide_show('vector-video-10')" >Video 3</a>
	 * <div style="display:none" id="vector-video-10">
	 * <video id="vid10" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-6.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid10',1739,1948)</script>
	 * </div>
	 * \endhtmlonly
	 *
	 */

	//! \cond [map] \endcond

incardon's avatar
incardon committed
236 237
	vd.map();

Pietro Incardona's avatar
Pietro Incardona committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	//! \cond [map] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Assign values to particles property ## {#assign_prop}
	 *
	 * We Iterate across all the particles, we count them using a local counter and we assign 1.0 to
	 * all the particles properties. Each particle has a scalar, vector and tensor property.
	 *
	 * \snippet Vector/0_simple/main.cpp assign property
	 *
	 *
	 */

	//! \cond [assign property] \endcond

255
	//Counter we use it later
incardon's avatar
incardon committed
256
	size_t cnt = 0;
257 258 259 260 261

	// Get a particle iterator
	it = vd.getDomainIterator();

	// For each particle ...
incardon's avatar
incardon committed
262 263
	while (it.isNext())
	{
264 265 266 267 268
		// ... p
		auto p = it.get();

		// we set the properties of the particle p
		
Pietro Incardona's avatar
Pietro Incardona committed
269
         // the scalar property
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
		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
287 288
		cnt++;

289
		// next particle
incardon's avatar
incardon committed
290 291 292
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	//! \cond [assign property] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Reduce (sum numbers across processors) ## {#e0_s_reduce}
	 *
	 * cnt contain the number of object the local processor contain, if we are interested to count the total number across the processors
	 * we can use the function add, to sum across the processors. First we have to get an instance of Vcluster, queue an operation of add with
	 * the variable count and finally execute. All the operations are asynchronous, execute work like a barrier and ensure that all the
	 * queued operations are executed.
	 *
	 * \snippet Vector/0_simple/main.cpp reduce
	 *
	 */

	//! \cond [reduce] \endcond
310 311
	
	auto & v_cl = create_vcluster();
incardon's avatar
incardon committed
312 313 314
	v_cl.sum(cnt);
	v_cl.execute();
	
Pietro Incardona's avatar
Pietro Incardona committed
315 316 317 318 319 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
	//! \cond [reduce] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Visualization, write VTK files ## {#e0_s_vis_vtk}
	 *
	 * With this function we output the particle position in VTK format. A VTK file
	 * contain information about particle position and properties. Such file can be visualizaed
	 * with program like paraview. In case this program run on several processor Each processor
	 * generate a VTK file
	 *
	 * \snippet Vector/0_simple/main.cpp vtk
	 *
	 * \htmlonly
	 * <a href="#" onclick="hide_show('vector-video-6')" >Video</a>
	 * <div style="display:none" id="vector-video-6">
	 * <video id="vid6" width="1200" height="576" controls> <source src="http://ppmcore.mpi-cbg.de/upload/video/Lesson1-5.mp4" type="video/mp4"></video>
	 * <script>video_anim('vid6',92,400)</script>
	 * </div>
	 * \endhtmlonly
	 *
	 */

	//! \cond [vtk] \endcond

	vd.write("particles");

	//! \cond [vtk] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
348
	 * ## Finalize ## {#finalize_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
349 350 351 352 353 354 355 356 357
	 *
	 *  At the very end of the program we have always to de-initialize the library
	 *
	 * \snippet Vector/0_simple/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
358
	openfpm_finalize();
Pietro Incardona's avatar
Pietro Incardona committed
359 360 361 362 363 364

	//! \cond [finalize] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
365
	 * ## Full code ## {#code_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
366 367 368 369
	 *
	 * \include Vector/0_simple/main.cpp
	 *
	 */
incardon's avatar
incardon committed
370
}