– Importing particle positions from a text file

Voronoi cells for 1000 particles in a cube of side length 10

This example script demonstrates the ability of the code to import a list of particles from a text file into a container object. This code makes use of a sample list of 1000 particles poured into a cube of side length 10, that is provided in the distribution as “pack_ten_cube”. Each of the lines of this file has the following form:

<Numerical ID label> <x coordinate> <y coordinate> <z coordinate>

The numerical IDs are integers from 1 to 1000 that can be used to track individual particles.

On line 23 of the code, the container class is constructed using the geometry constants that were specified in the header. The container is divided into a subgrid of six blocks in coordinate direction, giving 216 regions in total, and approximately 1000/216=4.6 particles per region, a good number for efficiency. Storage space for eight particles is allocated to each region, but the specification of this does not have to be precise, as the code will dynamically allocate more in case of overflow.

On line 27, the code imports the particle positions from the input file. On lines 31 and 32, the Voronoi cells are computed in gnuplot and POV-Ray formats and saved to file, and on line 35, the particle positions themselves are outputted in the POV-Ray format. The POV-Ray header file “import.pov” can be used to render these files into the image shown above. A basic render of size 640 × 640 can be carried out using the command:

povray +H640 +W640 +Oimport.png import.pov

For a higher quality render, anti-aliasing can be switched on using the flag +A0.3. For even higher quality, the flags +A0.0001 +R9 can be used to increase the oversampling. Disabling the anti-aliasing jitter with -J can also be helpful for mathematical renderings. For more details on POV-Ray rendering, consult the tracing options section of the POV-Ray manual.

Code listing

 1: // File import example code
 2: //
 3: // Author   : Chris H. Rycroft (LBL / UC Berkeley)
 4: // Email    :
 5: // Date     : August 30th 2011
 7: #include "voro++.hh"
 8: using namespace voro;
10: // Set up constants for the container geometry
11: const double x_min=-5,x_max=5;
12: const double y_min=-5,y_max=5;
13: const double z_min=0,z_max=10;
15: // Set up the number of blocks that the container is divided into
16: const int n_x=6,n_y=6,n_z=6;
18: int main() {
20:         // Create a container with the geometry given above, and make it
21:         // non-periodic in each of the three coordinates. Allocate space for
22:         // eight particles within each computational block
23:         container con(x_min,x_max,y_min,y_max,z_min,z_max,n_x,n_y,n_z,
24:                         false,false,false,8);
26:         //Randomly add particles into the container
27:         con.import("pack_ten_cube");
29:         // Save the Voronoi network of all the particles to text files
30:         // in gnuplot and POV-Ray formats
31:         con.draw_cells_gnuplot("pack_ten_cube.gnu");
32:         con.draw_cells_pov("pack_ten_cube_v.pov");
34:         // Output the particles in POV-Ray format
35:         con.draw_particles_pov("pack_ten_cube_p.pov");
36: }