1 // Voronoi calculation example code
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : April 14th 2013
10 // Set up constants for the container geometry
11 const double x_min
=-1,x_max
=1;
12 const double y_min
=-1,y_max
=1;
13 const double z_min
=-1,z_max
=1;
14 const double cvol
=(x_max
-x_min
)*(y_max
-y_min
)*(x_max
-x_min
);
16 // Set up the number of blocks that the container is divided into
17 const int n_x
=6,n_y
=6,n_z
=6;
19 // Set the number of particles that are going to be randomly introduced
20 const int particles
=40;
22 // This function returns a random double between 0 and 1
23 double rnd() {return double(rand())/RAND_MAX
;}
28 voronoicell_neighbor c
;
29 std::vector
<int> neigh
;
31 // Create a container with the geometry given above, and make it
32 // non-periodic in each of the three coordinates. Allocate space for
33 // eight particles within each computational block
34 container
con(x_min
,x_max
,y_min
,y_max
,z_min
,z_max
,n_x
,n_y
,n_z
,
37 // Randomly add particles into the container
38 for(i
=0;i
<particles
;i
++) {
39 x
=x_min
+rnd()*(x_max
-x_min
);
40 y
=y_min
+rnd()*(y_max
-y_min
);
41 z
=z_min
+rnd()*(z_max
-z_min
);
45 // Output the particle positions in gnuplot format
46 con
.draw_particles("random_points_p.gnu");
48 // Output the Voronoi cells in gnuplot format
49 con
.draw_cells_gnuplot("random_points_v.gnu");
51 // Loop over all of the particles and compute their Voronoi cells
53 if(cl
.start()) do if(con
.compute_cell(c
,cl
)) {
54 cl
.pos(x
,y
,z
);i
=cl
.pid();
57 // Print out the information about neighbors
58 printf("Particle %2d at (% 1.3f,% 1.3f,% 1.3f):",i
,x
,y
,z
);
59 for(unsigned int j
=0;j
<neigh
.size();j
++) printf(" %d",neigh
[j
]);