VoroPlex: A Python Framework for Multi-Threaded and Differentiable Voronoi Tessellation
Authors: Peihang Lin, Shanmei Wanyan, Mucheng Zhu, Ying Jiang, Chris H. Rycroft, Chenfanfu Jiang, Jiayin Lu
Organizations: Department of Mathematics, University of California, Los Angeles, Los Angeles, CA 90095, United States · Department of Statistics and Data Science, Yale University, New Haven, CT 06511, United States · Department of Mathematics, University of Wisconsin–Madison, Madison, WI 53706, United States · Mathematics Group, Lawrence Berkeley National Laboratory, Berkeley, CA 94720, United States
Voronoi tessellations are widely used in computational physics, for example to measure local structure in particle systems and to generate meshes. We present VoroPlex, an open-source Python package for computing Voronoi tessellations in two and three dimensions. VoroPlex is built on the multi-threaded version of the C++ library Voro++, so that large tessellations can be computed in parallel. The package also makes the tessellation differentiable. Each Voronoi vertex is labeled by the generators that define it, and VoroPlex uses these labels to compute the derivative of the vertex with respect to the positions of these generators. PyTorch can therefore compute gradients of quantities such as cell areas, volumes, and interface areas with respect to the generator positions, so that these quantities can be optimized using standard gradient-based methods. The package also includes tools for extracting interfaces between groups of cells and for exporting meshes. For one million points using 28 threads, VoroPlex is 46 times faster than SciPy in 2D and 39 times faster in 3D. We demonstrate the package with three applications: centroidal Voronoi tessellations with a variable density, meshing of 2D shapes defined by signed distance functions, and the design of foams with a preferred cell orientation.
Figures & tables
Figure 1: Voronoi tessellations with random generators in (a) 2D, consisting of polygonal cells, and (b) 3D, consisting of polyhedral cells. (c) Centroidal Voronoi tessellation in 3D with a uniform density field.
Figure 2: Computing a Voronoi cell separately. (a) The cell of a generator (purple) starts as the whole domain. (b) The cell is cut by the perpendicular bisector between the generator and a neighboring generator (gray). (c) Further cuts by other neighbors produce the final cell. (d) Since each cell is computed separately, many cells can be computed in parallel.
Function
Purpose
compute_voronoi_2d , compute_voronoi_3d
Compute the tessellation, either standard or radical, and return the result as a VoronoiResult object.
compute_properties
Compute further properties of the cells for an existing result.
cell_vertices
Return the vertices of every cell as a differentiable PyTorch tensor.
cell_fan_simplices
Split every cell into triangles (2D) or tetrahedra (3D), each made from the cell’s generator and one edge or part of a face.
compute_unique_vertices
Compute the positions of the unique vertices, each shared between cells.
cell_polytopes
List the vertices of each cell in order (2D), or its faces (3D), using the indices of the unique vertices.
Table 1: The main functions in VoroPlex . The functions after the first two work in both 2D and 3D, and find the dimension from the result. Those in the middle group build meshes in which each vertex is shared between the cells that meet there. compute_unique_vertices requires the topology representation; the others also work with the default geometry representation, except in periodic domains.
Figure 3: A Voronoi vertex v (orange) is usually defined by (a) three generators in two dimensions, or (b) four generators in three dimensions. The generators are blue. The vertex is equidistant from its generators: in (a), it is the center of the circle (dashed) through them. In (a), three Voronoi edges meet at v , each on the perpendicular bisector of two generators. In (b), four edges meet at v , each equidistant from three generators, together with parts of the six faces (transparent) between pairs of generators. The IDs of the generators form the signature of the vertex.
Figure 4: The interface between inner and outer cells, extracted by VoroPlex . There are N=200 generators, placed at random in the unit square. The 52 generators inside the dashed circle, of radius 0.3, are inner (blue), and the rest are outer (gray). The inner generators are listed first, and the tessellation is computed with n_inner=52 . The interface (orange) is the set of edges between an inner cell (shaded) and an outer cell, as returned by surface_mesh . It follows the circle only roughly, because it is made of edges of the Voronoi cells.
Figure 5: Uniform-density CVT optimization in the unit square with N=200 generators, computed with Listing 4 . (a) The initial generators (blue), placed at random, and their Voronoi cells. Each cell’s centroid is shown in orange, joined to its generator by a gray line. (b) After 600 iterations, each generator lies at the centroid of its cell, which is the defining property of a CVT, and most cells are close to regular hexagons. (c) The energy E of Eq. ( 2 ), relative to Ehex=5/(183N) , the energy of N regular hexagonal cells of area 1/N . A hexagonal lattice gives the lowest possible energy in two dimensions [ 20 ] .
Figure 6: Tessellations of 153 non-overlapping disks with radii between 0.012 and 0.07 in the unit square. (a) The standard Voronoi tessellation of the disk centers. (b) The radical tessellation. (c) The radical tessellation with every radius increased by t=0.04 , with the enlarged disks shown as circles. The 20 disks with empty cells are shaded.
N=105
N=106
Library
Threads
2D
3D
2D
3D
SciPy
1
1.02
5.05
13.53
57.17
pyvoronoi
1
3.25
–
46.60
–
pyvoro
1
19.34
20.72
236.5
268.2
VoroPlex (geometry)
1
0.229
2.53
2.62
26.01
28
0.027
0.132
0.295
1.47
Table 2: Time to compute the Voronoi tessellation of N generators, in seconds. pyvoronoi has no 3D version. The four VoroPlex rows are the geometry representation with and without neighbor information, and the per-cell and topology representations. Each value is the median of five independent trials, each with different random generators.
2D
3D
N
Time (s)
Memory (GiB)
Time (s)
Memory (GiB)
105
19.34
21.1
20.72
21.8
2×105
38.76
42.1
41.66
43.6
5×105
96.70
106.8
105.1
108.9
106
236.5
213.5
268.2
217.7
Table 3: Time and peak memory used by pyvoro to compute the Voronoi tessellation of N generators. Each value is the median of five independent trials, each with different random generators.
Figure 7: Parallel performance of VoroPlex in 2D and 3D. (a) Wall time with Turbo Boost on, against the number of threads p , for N=5×106 generators and the four ways of computing the tessellation. The dotted lines show perfect scaling. (b) Parallel efficiency T1/(pTp) of a whole call at a fixed clock frequency (Turbo Boost off), where Tp is the wall time on p threads, for the geometry and per-cell representations. (c) Parallel efficiency of the loop over the cells, and of the Voro++ cell computation within it, for the geometry representation: adjusted for the clock frequency with Turbo Boost on (lines, Eq. ( 8 )), and measured at a fixed clock frequency (crosses). (d) Adjusted parallel efficiency of the loop over the cells on 28 threads, against the number of generators N . Each value is the median of five independent trials, each with different random generators.
Figure 8: Where the time goes in a call of VoroPlex , for the geometry representation with N=5×106 generators, at a fixed clock frequency (Turbo Boost off). Each bar is the core time of one part of the calculation, which is its wall time multiplied by the number of threads p , divided by the wall time of the whole call on one thread. The parts are stacked, so that the total height of the bars for each p is 1/E , where E is the parallel efficiency, and the dotted line at 1 shows perfect scaling. A part that scales perfectly keeps the same height as p increases. Each value is the median of five independent trials, each with different random generators.
VoroPlex
Dim.
Device
Tessellation
Forward
Backward
PyTorch
Total
2D
CPU
23
1.5
5.0
41
71
2D
GPU
25
4.8
7.6
8.2
46
3D
CPU
124
20
33
1248
1425
3D
GPU
123
38
88
58
306
Table 4: Time per iteration, in milliseconds, of the CVT optimization with N=105 generators. For each of five independent trials, each with different random generators, the median over the 500 iterations was found, and the median of these five values is reported. The tessellation uses 28 threads. The VoroPlex columns are the tessellation, the forward pass ( cell_vertices and cell_fan_simplices ), and the vertex derivative in the backward pass (Section 2.5 ). The PyTorch column is the rest: the energy, its derivatives with respect to the vertices, and the optimizer step.
Figure 9: Two-dimensional density-weighted CVTs with N=400 generators. (a) ρ(x)=h(x)−4 for the linear sizing field h of Eq. ( 11 ). (b) Two Gaussian peaks (Eq. ( 13 )). (c) A band around a five-petal curve (Eq. ( 15 )). Red points denote the optimized generators, and white lines denote the Voronoi cell boundaries. In each panel, the background density is normalized by its own maximum, ρ=ρ/ρmax , and displayed using a shared logarithmic color scale.
Figure 10: Three-dimensional density-weighted CVTs with N=1500 generators. (a) Uniform density. (b) Linear sizing field (Eq. ( 18 )). (c) Sizing field that is smallest on the edges of the cube (Eq. ( 20 )). The top row shows the whole tessellation. In the bottom row, the whole cells on one side of a diagonal plane are removed, so that the cells inside can be seen.
Figure 11: The classification of cells used to mesh a shape given by an SDF. Teal cells are inner cells, and the boundary cells, which are inner cells next to the interface, are marked with purple hatching. Gray cells are outer cells. Coral points are the generators, solid coral segments are the interface edges EΓ , and the black dashed curve is the boundary Γ of the shape.
Figure 12: Meshing a flower-shaped domain. (a) Initial interface for random generator positions, over the SDF ϕ . (b) Interface after 600 iterations of the Adam optimizer, over the density ρΓ , normalized and shown on a logarithmic scale. The gray dashed curve is the boundary Γ , green segments are the interface edges, and blue points are the inner generators. (c) The objective function and its weighted terms during the optimization, with weights λmid=10 , λend=2 , λin=5×10−4 , λarea=0.05 , and λcirc=0.02 .
Figure 13: Meshes of five shapes after 600 iterations of the Adam optimizer: from left to right, a circle, a star, a rounded square, a heart, and a peanut. Each panel shows the density ρΓ , normalized and on a logarithmic scale. The gray dashed curve is the boundary Γ , green segments are the interface edges, and blue points are the inner generators.
Figure 14: Directions associated with the walls of a Voronoi cell. (a) A cell with generator pc . For the highlighted wall f , nf is the outward unit normal, tf is a direction within the wall, and the length of the wall is its weight wf in Eq. ( 32 ). (b) Cells with the same size in all directions, for which the wall normals are spread evenly over all directions and A=21I . (c) Cells elongated along y . The long walls are vertical, so their normals are mostly along x , and Axx>Ayy . The principal axis of A (blue) is perpendicular to the direction in which the cells are elongated (orange). The tensors in (b) and (c) are computed from the cells that are drawn. Each interior wall is shared by two cells, so in (b) and (c) it has two arrows, one for the outward normal of each cell. These point in opposite directions, and give the same nfnfT .
Figure 15: Two-dimensional foam optimization with N=120 generators, constant axis u=(1,0) , and strength α=0.75 . (a) Initial foam. (b) Optimized foam. In both panels each wall is colored by (nf⋅u)2 , so that walls whose normal is along u are bright. Panels (a) and (b) use the same axes, and the color bar beside (b) applies to both. (c) The objective function L and its first term Lmain , the mismatch in the fabric tensor, during the optimization, with the raw values faint and their running median in bold.
M
Mean uTAcu
Target
Initial
Optimized
Initial
Optimized
α
Lamellar
0.322
0.994
0.328
0.607
0.65
Columnar
0.343
0.992
0.336
0.121
0
Radial
0.308
0.993
0.315
0.605
0.65
Table 5: The alignment M of Eq. ( 37 ) and the mean of u(c)TAcu(c) for the three three-dimensional targets, before and after optimization. The second quantity approaches the target strength α .
Figure 16: Three-dimensional foam optimization with N=800 generators and the lamellar target, with the constant axis u=(1,0,0) and strength α=0.65 . (a) Initial generator positions. (b) Optimized generator positions. (c) A slice through the optimized foam at z=0.5 , with each wall colored by (nf⋅u)2 , with the color bar beside it. (d) The objective function L and its first term Lmain during the optimization, with the raw values faint and their running median in bold.
Figure 17: Three-dimensional foam optimization with N=800 generators: (a) lamellar target, u=(1,0,0) with α=0.65 ; (b) columnar target, u=(0,0,1) with α=0 ; (c) radial axis of Eq. ( 38 ), with α=0.65 . The top row shows the whole foams, and the bottom row shows them with the cells in one corner removed, so that the cells inside can be seen.