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
16 // Set up the number of blocks that the container is divided into
19 // Set the number of particles that are going to be randomly introduced
20 const int particles
=4000;
22 // Set the number of Voronoi faces to bin
25 // This function returns a random double between 0 and 1
26 double rnd() {return double(rand())/RAND_MAX
;}
30 double x
,y
,z
,r
,dx
,dy
,dz
;
32 double p
[3*particles
];
34 // Create a container with the geometry given above, and make it
35 // non-periodic in each of the three coordinates. Allocate space for
36 // eight particles within each computational block
37 container
con(-boxl
,boxl
,-boxl
,boxl
,-boxl
,boxl
,bl
,bl
,bl
,false,false,false,8);
39 // Randomly add particles into the container
40 for(i
=0;i
<particles
;i
++) {
50 for(fp
=faces
;fp
<faces
+nface
;fp
++) *fp
=0;
51 if(vl
.start()) do if(con
.compute_cell(c
,vl
)) {
58 i
=c
.number_of_faces()-4;
59 if(i
<0) i
=0;if(i
>=nface
) i
=nface
-1;
63 for(i
=0;i
<particles
;i
++) con
.put(i
,p
[3*i
],p
[3*i
+1],p
[3*i
+2]);
65 for(fp
=faces
;fp
<faces
+nface
;fp
++) printf(" %d",*fp
);
69 // Output the particle positions in gnuplot format
70 con
.draw_particles("sphere_mesh_p.gnu");
72 // Output the Voronoi cells in gnuplot format
73 con
.draw_cells_gnuplot("sphere_mesh_v.gnu");
75 // Output the neighbor mesh in gnuplot format
76 FILE *ff
=safe_fopen("sphere_mesh.net","w");
78 voronoicell_neighbor c
;
80 if(vl
.start()) do if(con
.compute_cell(c
,vl
)) {
83 for(l
=0;l
<(signed int) vi
.size();l
++) if(vi
[l
]>i
)
84 fprintf(ff
,"%g %g %g\n%g %g %g\n\n\n",
85 p
[3*i
],p
[3*i
+1],p
[3*i
+2],
86 p
[3*vi
[l
]],p
[3*vi
[l
]+1],p
[3*vi
[l
]+2]);