1 // Voronoi calculation example code
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
10 // Set up constants for the container geometry
13 // Set up the number of blocks that the container is divided into
16 // Set the number of particles that are going to be randomly introduced
17 const int particles
=4000;
19 // Set the number of Voronoi faces to bin
22 // This function returns a random double between 0 and 1
23 double rnd() {return double(rand())/RAND_MAX
;}
27 double x
,y
,z
,r
,dx
,dy
,dz
;
29 double p
[3*particles
];
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(-boxl
,boxl
,-boxl
,boxl
,-boxl
,boxl
,bl
,bl
,bl
,false,false,false,8);
36 // Randomly add particles into the container
37 for(i
=0;i
<particles
;i
++) {
47 for(fp
=faces
;fp
<faces
+nface
;fp
++) *fp
=0;
48 if(vl
.start()) do if(con
.compute_cell(c
,vl
)) {
55 i
=c
.number_of_faces()-4;
56 if(i
<0) i
=0;if(i
>=nface
) i
=nface
-1;
60 for(i
=0;i
<particles
;i
++) con
.put(i
,p
[3*i
],p
[3*i
+1],p
[3*i
+2]);
62 for(fp
=faces
;fp
<faces
+nface
;fp
++) printf(" %d",*fp
);
66 // Output the particle positions in gnuplot format
67 con
.draw_particles("sphere_mesh_p.gnu");
69 // Output the Voronoi cells in gnuplot format
70 con
.draw_cells_gnuplot("sphere_mesh_v.gnu");
72 // Output the neighbor mesh in gnuplot format
73 FILE *ff
=safe_fopen("sphere_mesh.net","w");
75 voronoicell_neighbor c
;
77 if(vl
.start()) do if(con
.compute_cell(c
,vl
)) {
80 for(l
=0;l
<(signed int) vi
.size();l
++) if(vi
[l
]>i
)
81 fprintf(ff
,"%g %g %g\n%g %g %g\n\n\n",
82 p
[3*i
],p
[3*i
+1],p
[3*i
+2],
83 p
[3*vi
[l
]],p
[3*vi
[l
]+1],p
[3*vi
[l
]+2]);