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

incardon's avatar
incardon committed
19

Pietro Incardona's avatar
Pietro Incardona committed
20 21 22 23 24 25 26 27 28
/*!
 * \page Vector_0_simple Vector 0 simple
 *
 *
 * [TOC]
 *
 *
 * # Simple Vector example # {#simple_vector_example}
 *
incardon's avatar
incardon committed
29
 *
30 31
 * 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * \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
 *
46
 * ## inclusion ## {#e0_v_inclusion}
Pietro Incardona's avatar
Pietro Incardona committed
47 48 49 50 51
 *
 * 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
52 53
 */

Pietro Incardona's avatar
Pietro Incardona committed
54
//! \cond [inclusion] \endcond
incardon's avatar
incardon committed
55
#include <stddef.h>
Pietro Incardona's avatar
Pietro Incardona committed
56 57 58
#include "Vector/vector_dist.hpp"
//! \cond [inclusion] \endcond

incardon's avatar
incardon committed
59 60
int main(int argc, char* argv[])
{
Pietro Incardona's avatar
Pietro Incardona committed
61 62 63 64 65 66 67 68 69

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
	 * ## Initialization ## {#e0_s_init}
	 *
	 *  Here we
	 *  * Initialize the library
	 *  * we create a Box that define our domain
70
	 *  * An array that define our boundary conditions
Pietro Incardona's avatar
Pietro Incardona committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	 *  * 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
95
	openfpm_init(&argc,&argv);
incardon's avatar
incardon committed
96

97
	// 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
98
	Box<2,float> domain({0.0,0.0},{1.0,1.0});
99 100

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

	// extended boundary around the domain, and the processor domain
Pietro Incardona's avatar
Pietro Incardona committed
104
	Ghost<2,float> g(0.01);
incardon's avatar
incardon committed
105
	
Pietro Incardona's avatar
Pietro Incardona committed
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 143 144 145 146 147
	//! \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

148 149 150 151 152 153 154 155 156 157
	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
158

Pietro Incardona's avatar
Pietro Incardona committed
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
	//! \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

185
	auto it = vd.getDomainIterator();
incardon's avatar
incardon committed
186 187 188 189 190

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

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

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

197
		// next particle
incardon's avatar
incardon committed
198 199 200
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
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
	//! \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
241 242
	vd.map();

Pietro Incardona's avatar
Pietro Incardona committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	//! \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

260
	//Counter we use it later
incardon's avatar
incardon committed
261
	size_t cnt = 0;
262 263 264 265 266

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

	// For each particle ...
incardon's avatar
incardon committed
267 268
	while (it.isNext())
	{
269 270 271 272 273
		// ... p
		auto p = it.get();

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

294
		// next particle
incardon's avatar
incardon committed
295 296 297
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	//! \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
315 316
	
	auto & v_cl = create_vcluster();
incardon's avatar
incardon committed
317 318 319
	v_cl.sum(cnt);
	v_cl.execute();
	
Pietro Incardona's avatar
Pietro Incardona committed
320 321 322 323 324 325 326 327 328 329
	//! \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
incardon's avatar
incardon committed
330 331 332
	 * generate a VTK file. VTK has two format one is the ASCII that is human readable but produce
	 * bigger file the other is the binary that produce not-human readable files, but smaller
	 * and more efficent files to read for Paraview. In order to create a Binary VTK file use the
incardon's avatar
incardon committed
333
	 * option VTK_WRITER in combination with FORMAT_BINARY
Pietro Incardona's avatar
Pietro Incardona committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	 *
	 * \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

incardon's avatar
incardon committed
349
	// save vtk format (vtk is always the default)
Pietro Incardona's avatar
Pietro Incardona committed
350
	vd.write("particles");
incardon's avatar
incardon committed
351 352 353

	// save in vtk binary format
	vd.write("particles_bin",VTK_WRITER | FORMAT_BINARY);
Pietro Incardona's avatar
Pietro Incardona committed
354 355 356 357 358 359

	//! \cond [vtk] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
360
	 * ## Finalize ## {#finalize_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
361
	 *
incardon's avatar
incardon committed
362
	 *  At the very end of the program we have always de-initialize the library
Pietro Incardona's avatar
Pietro Incardona committed
363 364 365 366 367 368 369
	 *
	 * \snippet Vector/0_simple/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
370
	openfpm_finalize();
Pietro Incardona's avatar
Pietro Incardona committed
371 372 373 374 375 376

	//! \cond [finalize] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
377
	 * ## Full code ## {#code_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
378 379 380 381
	 *
	 * \include Vector/0_simple/main.cpp
	 *
	 */
incardon's avatar
incardon committed
382
}