1 // Voronoi calculation example code
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
13 // Set up constants for the container geometry
14 const double x_min
=-1,x_max
=1;
15 const double y_min
=-1,y_max
=1;
16 const double z_min
=-1,z_max
=1;
18 // Set up the number of blocks that the container is divided into
19 const int n_x
=6,n_y
=6,n_z
=6;
21 // Set the number of particles that are going to be randomly introduced
22 const int particles
=1;
24 // This function returns a random double between 0 and 1
25 double rnd() {return double(rand())/RAND_MAX
;}
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 // Sum up the volumes, and check that this matches the container volume
50 if(cl
.start()) do if(con
.compute_cell(c
,cl
)) {
51 cl
.pos(x
,y
,z
);id
=cl
.pid();
52 printf("Particle %d:\n",id
);
54 // Gather information about the computed Voronoi cell
55 c
.face_vertices(f_vert
);
58 // Print vertex positions
59 for(i
=0;i
<v
.size();i
+=3) printf("Vertex %d : (%g,%g,%g)\n",i
/3,v
[i
],v
[i
+1],v
[i
+2]);
62 // Loop over all faces of the Voronoi cell
64 while(j
<f_vert
.size()) {
66 // Number of vertices in this face
71 printf("Triangle : (%d,%d,%d)\n",f_vert
[j
+1],f_vert
[j
+i
],f_vert
[j
+i
+1]);
73 // Move j to point at the next face
79 // Output the particle positions in gnuplot format
80 con
.draw_particles("random_points_p.gnu");
82 // Output the Voronoi cells in gnuplot format
83 con
.draw_cells_gnuplot("random_points_v.gnu");