main.cpp 11.6 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
Pietro Incardona's avatar
Pietro Incardona committed
14 15 16
 *
 */

incardon's avatar
incardon committed
17

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

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

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

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

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

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

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

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

Pietro Incardona's avatar
Pietro Incardona committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	//! \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

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

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

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

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

194
		// next particle
incardon's avatar
incardon committed
195 196 197
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
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 236 237
	//! \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
238 239
	vd.map();

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

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

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

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

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

291
		// next particle
incardon's avatar
incardon committed
292 293 294
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	//! \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
312 313
	
	auto & v_cl = create_vcluster();
incardon's avatar
incardon committed
314 315 316
	v_cl.sum(cnt);
	v_cl.execute();
	
Pietro Incardona's avatar
Pietro Incardona committed
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 348 349
	//! \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
350
	 * ## Finalize ## {#finalize_e0_sim}
Pietro Incardona's avatar
Pietro Incardona committed
351
	 *
incardon's avatar
incardon committed
352
	 *  At the very end of the program we have always de-initialize the library
Pietro Incardona's avatar
Pietro Incardona committed
353 354 355 356 357 358 359
	 *
	 * \snippet Vector/0_simple/main.cpp finalize
	 *
	 */

	//! \cond [finalize] \endcond

Pietro Incardona's avatar
Pietro Incardona committed
360
	openfpm_finalize();
Pietro Incardona's avatar
Pietro Incardona committed
361 362 363 364 365 366

	//! \cond [finalize] \endcond

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