main.cpp 12.4 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
6
 * \subpage Vector_1_HDF5
Pietro Incardona's avatar
Pietro Incardona committed
7 8
 * \subpage Vector_2_expression
 * \subpage Vector_3_md
9 10
 * \subpage Vector_4_reo_root
 * \subpage Vector_4_cp
Pietro Incardona's avatar
Pietro Incardona committed
11
 * \subpage Vector_4_mp_cl
incardon's avatar
incardon committed
12
 * \subpage Vector_5_md_vl_sym
incardon's avatar
incardon committed
13
 * \subpage Vector_5_md_vl_sym_crs
incardon's avatar
incardon committed
14
 * \subpage Vector_6_complex_usage
incardon's avatar
incardon committed
15
 * \subpage Vector_7_sph_dlb
incardon's avatar
incardon committed
16
 * \subpage Vector_7_sph_dlb_opt
incardon's avatar
incardon committed
17
 * \subpage Vector_8_DEM
Pietro Incardona's avatar
Pietro Incardona committed
18 19 20
 *
 */

21

Pietro Incardona's avatar
Pietro Incardona committed
22 23 24 25 26 27 28 29 30
/*!
 * \page Vector_0_simple Vector 0 simple
 *
 *
 * [TOC]
 *
 *
 * # Simple Vector example # {#simple_vector_example}
 *
31
 *
32 33
 * 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
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
 *
48
 * ## inclusion ## {#e0_v_inclusion}
Pietro Incardona's avatar
Pietro Incardona committed
49 50 51 52 53
 *
 * 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
 *
54 55
 */

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

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

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

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

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

	// extended boundary around the domain, and the processor domain
Pietro Incardona's avatar
Pietro Incardona committed
106
	Ghost<2,float> g(0.01);
107
	
Pietro Incardona's avatar
Pietro Incardona committed
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 148 149
	//! \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

150
    vector_dist<2,float, aggregate<float,float[3],float[3][3]> > vd(4096,domain,bc,g);
151 152 153 154 155 156 157 158 159

	// 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;
160

Pietro Incardona's avatar
Pietro Incardona committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	//! \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

187
	auto it = vd.getDomainIterator();
188 189 190 191 192

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

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

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

199
		// next particle
200 201 202
		++it;
	}

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

243 244
	vd.map();

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

262
	//Counter we use it later
263
	size_t cnt = 0;
264 265 266 267 268

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

	// For each particle ...
269 270
	while (it.isNext())
	{
271 272 273 274 275
		// ... p
		auto p = it.get();

		// we set the properties of the particle p
		
Pietro Incardona's avatar
Pietro Incardona committed
276
         // the scalar property
277 278 279 280 281 282 283 284 285 286 287 288 289 290
		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;
291
        vd.template getProp<tensor>(p)[2][2] = 1.0;
292 293

		// increment the counter
294 295
		cnt++;

296
		// next particle
297 298 299
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	//! \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
317 318
	
	auto & v_cl = create_vcluster();
319 320 321
	v_cl.sum(cnt);
	v_cl.execute();
	
Pietro Incardona's avatar
Pietro Incardona committed
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
330 331
	 * contain information about particle position and properties. Such file can be visualized
	 * with a program like paraview. In case this program run on several processor each processor
incardon's avatar
incardon committed
332 333 334
	 * 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
335 336 337
	 * option VTK_WRITER in combination with FORMAT_BINARY. By default properties does not have name
	 * the VTK writer will label them "attr0" for the first property "attr1" for the second and so on
	 * in case we want to assign names to the properties we can use the function setPropNames
Pietro Incardona's avatar
Pietro Incardona committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	 *
	 * \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

353 354 355
	openfpm::vector<std::string> names({"scalar","vector","tensor"});
	vd.setPropNames(names);

incardon's avatar
incardon committed
356
	// save vtk format (vtk is always the default)
Pietro Incardona's avatar
Pietro Incardona committed
357
	vd.write("particles");
incardon's avatar
incardon committed
358 359 360

	// save in vtk binary format
	vd.write("particles_bin",VTK_WRITER | FORMAT_BINARY);
Pietro Incardona's avatar
Pietro Incardona committed
361 362 363 364 365 366

	//! \cond [vtk] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
367
	 * ## Finalize ## {#finalize_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
368
	 *
incardon's avatar
incardon committed
369
	 *  At the very end of the program we have always de-initialize the library
Pietro Incardona's avatar
Pietro Incardona committed
370 371 372 373 374 375 376
	 *
	 * \snippet Vector/0_simple/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
377
	openfpm_finalize();
Pietro Incardona's avatar
Pietro Incardona committed
378 379 380 381 382 383

	//! \cond [finalize] \endcond

	/*!
	 * \page Vector_0_simple Vector 0 simple
	 *
Pietro Incardona's avatar
Pietro Incardona committed
384
	 * ## Full code ## {#code_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
385 386 387 388
	 *
	 * \include Vector/0_simple/main.cpp
	 *
	 */
389
}