multisphere : a Python implementation of the M ulti S phere S hape generator (MSS) for DEM simulations Journal: SoftwareX F. Buchele, P. Müller, A. Moradian, T. Pöschel Abstract multisphere is an open-source Python package for generating multi-sphere representations of complex particles for use in DEM simulations. It reconstructs triangulated surface meshes and voxelized volumes as sets of intersecting spheres and provides tools for evaluation, visualization, and export. Metadata Table 1: Code metadata Nr. Code metadata description Metadata C1 Current code version v1.1.0 C2 Permanent link to code/repository used for this code version https://github.com/FelixBuchele/multisphere C3 Permanent link to Reproducible Capsule None C4 Legal Code License GNU GPL 3.0 C5 Code versioning system used git C6 Software code languages, tools, and services used python \geq 3.10 C7 Compilation requirements, operating environments & dependencies numpy, trimesh, scipy, scikit-image, matplotlib, pyvista, manifold3D C8 If available Link to developer documentation/manual https://github.com/FelixBuchele/multisphere/blob/main/README.md C9 Support email for questions felix.buchele@fau.de 1 Motivation and significance The discrete element method (DEM) is a numerical technique for the simulation of granular and particulate systems in fields such as materials science, geomechanics, and process engineering [ 6 , 18 , 15 ] . In many cases, the macroscopic behavior of such systems depends strongly on the shapes of individual particles, which influence packing structure, contact interactions, and flow dynamics [ 13 , 24 ] . The multi-sphere approach is a widely used model for non-spherical particles, whose shape \mathcal{S} is represented by a set of n spheres \tilde{\mathcal{S}}_{i} . Their radii R_{i} and positions \vec{r}_{i} . must be chosen such that \mathcal{\tilde{S}}\equiv\bigcup_{i=0}^{n-1}\mathcal{\tilde{S}}_{i}(R_{i},\vec{r}_{i})\,, (1) approximates \mathcal{S} as closely as possible for given n . A key advantage of the multi-sphere representation is that contact detection and the evaluation of interaction forces for complex-shaped particles reduce to sphere–sphere interactions [ 17 , 4 , 3 , 11 , 8 ] . An example of a multisphere representation of a complex shape is given in fig. 1 . Figure 1: Mesh model of a cow [ 22 ] and its multisphere representation with n=2000 . Color indicates sphere sizes. Determining the set \left\{\left(R_{i},\vec{r}_{i}\right)\right\},i=0,\dots,n-1 is an optimization problem in which the mismatch \left|\mathcal{S}\triangle\tilde{\mathcal{S}}\right|\equiv\left|\left(\mathcal{S}\cup\tilde{\mathcal{S}}\right)\setminus\left({\mathcal{S}}\cap\tilde{\mathcal{S}}\right)\right|\,. (2) between the target shape \mathcal{S} and its multi-sphere approximation \tilde{\mathcal{S}} is minimized. Besides the number of spheres n , additional constraints may be imposed, such as the minimum sphere size and the minimum distance between spheres. This optimization is a challenging problem in computational geometry, and several multi-sphere particle generators have been proposed [ 2 , 5 , 8 , 1 , 10 ] . The M ulti- S phere S hape generator (MSS) algorithm was introduced as a theoretical concept in [ 9 ] . MSS requires fewer spheres than other particle generators to achieve a given mismatch value. Unlike other particle generators, MSS preserves symmetry properties, such that the generated particle model \tilde{\mathcal{S}} exhibits the same symmetries as the target shape {\mathcal{S}} . MSS is computationally efficient [ 9 ] , making it well suited for applications that require frequent particle reconstruction, such as simulations involving particle fracture and plastic deformation. This article presents a Python implementation of MSS. The underlying mathematical and algorithmic concepts are described in [ 9 ] . 2 Software description multisphere is implemented in Python and requires Python version \geq 3.10. Core functionality depends on numpy , scipy , scikit-image , and trimesh . Optional dependencies are used for visualization ( pyvista ) and for mesh-based boolean operations during mesh generation ( manifold3D ). The package is available on the Python Package Index (PyPI) and can be installed with pip . pip install multisphere installs the core package, and pip install multisphere[full] installs the core package together with all optional features. The source code is available in a public GitHub repository (see Table Metadata ). 2.1 Software architecture multisphere follows a three-stage workflow: preprocessing, processing, and postprocessing. Target shapes are provided either as surface meshes or voxelized volumes. Reconstruction is performed on voxel data, so mesh input is voxelized internally when required. The result is stored as a SpherePack , which can then be evaluated, visualized, or exported. Figure 2 summarizes the software architecture and public interface. Details on the MSS algorithm can be found in [ 9 ] . Figure 2: High-level architecture of the multisphere package. There are three main stages of the workflow involving public functions. 2.2 Software functionalities Core data structures multisphere uses two core data structures to connect the different stages of the workflow: VoxelGrid and SpherePack . VoxelGrid is a target particle as a three-dimensional voxel grid. It is used both for voxelized input data and for intermediate voxel-based distance fields required during reconstruction. VoxelGrid stores the voxel size and the spatial origin, allowing the discrete grid to be mapped to physical coordinates. SpherePack stores the positions and radii of the spheres consituting the multisphere particle model, see eq. 1 . All quantities in SpherePack are expressed in physical units. Public interface overview Table 2 lists the main public functions of multisphere , and fig. 4 shows an example workflow. Table 2: Public interface of the multisphere package. Function Stage Description load_mesh 1 Load a triangulated surface mesh from a mesh file. load_voxels_from_npy 1 Load a voxelized target shape stored as a NumPy array. multisphere_from_voxels 2 Generate a multi-sphere representation from voxelized target shape. multisphere_from_mesh 2 Generate a multi-sphere representation from a mesh target shape via voxelization. create_multisphere_mesh 3 Construct a surface mesh representing the union of spheres in a SpherePack . compute_dice_coefficient 3 Compute the Dice similarity coefficient between two meshes. plot_mesh 3 Interactive visualization of surface meshes. plot_sphere_pack 3 Interactive visualization of a multi-sphere representation. Preprocessing Meshes stored in STL, OBJ or PLY format are loaded using load_mesh , which returns a trimesh.Trimesh object. Voxelized particle target shapes stored as NumPy arrays are loaded using load_voxels_from_npy , which returns a VoxelGrid . For voxel inputs, the voxel size and origin can be specified to map the grid to physical coordinates. Processing multisphere can be applied either to voxelized target shape descriptions using multisphere_from_voxels or to surface meshes using multisphere_from_mesh . In the latter case, the input mesh is first voxelized, with the voxel size \Delta=\frac{L_{\min}}{\texttt{div}}, (3) derived from the smallest edge of the axis-aligned bounding box L_{\min} and the specified number div of voxels along this edge. Both functions return a SpherePack . The reconstruction process is controlled by three main termination criteria: a target reconstruction accuracy, a minimum admissible sphere radius, and a maximum number of spheres. At least one of these criteria must be specified. The reconstruction accuracy is expressed through the Dice-Sørensen coefficient [ 7 ] (as cited in [ 12 ] ), D\equiv\frac{2\sum\limits_{i,j,k}\left(S_{ijk}\cdot\tilde{S}_{ijk}\right)}{\sum\limits_{i,j,k}S_{ijk}+\sum\limits_{i,j,k}\tilde{S}_{ijk}}\,, (4) quantifying the similarity of the target particle shape \mathcal{S} and its multi-sphere approximation \tilde{\mathcal{S}} . For mesh-based input, voxelization introduces discretization errors, therefore, the reconstructed particle model, \tilde{\mathcal{S}} may locally extend beyond the target surface mesh. This property can lead to problems in certain applications, for example when the fragments of a granular particle that has just broken are both modeled as multi-sphere particles. If the multi-sphere model of one or both fragments extends beyond the fracture surface, this could result in the DEM behaving in an unphysical manner. The optional flag confine_mesh prevents the extension of \tilde{\mathcal{S}} beyond the target mesh by reducing the sphere radii accordingly. Postprocessing The function create_multisphere_mesh converts the multi-sphere approximation to a trimesh.Trimesh surface mesh. This mesh can be used for visualization and for mesh-based accuracy evaluation with compute_dice_coefficient , which computes the Dice similarity between two surface meshes avoiding voxelization errors of voxel-based comparison, according to eq. 4 . The visualization utilities plot_mesh and plot_sphere_pack provide interactive three-dimensional views based on pyvista . 3 Illustrative examples Figure 3 shows multi-sphere models of the Stanford Bunny [ 20 ] reconstructed with n=\{10,50,629\} spheres. The corresponding Python script is shown in Figure 4 . Figure 3: Surface mesh of the Stanford Bunny [ 20 ] and its multisphere reconstruction with 10, 50, and 629 spheres. ⬇ 1 import multisphere as mss 2 3 # multi-sphere reconstruction 4 mesh = mss . load_mesh ( stanford_bunny . stl ) 5 sphere_pack = mss . multisphere_from_mesh ( 6 mesh = mesh , 7 div =150, 8 min_radius_vox =8 9 precision =0.95, 10 min_center_distance_vox =4, 11 max_spheres =10, 12 confine_mesh = True ) 13 14 # compute mesh-based dice coefficient 15 sphere_mesh = mss . create_multisphere_mesh ( 16 sphere_pack ) 17 18 dice = mss . compute_dice_coefficient ( 19 mesh_1 = mesh , 20 mesh_2 = sphere_mesh ) 21 22 # visualization and export 23 mss . plot_sphere_pack ( sphere_pack ) 24 mss . export_sphere_pack_to_csv ( 25 sphere_pack , path ) 26 mss . export_sphere_pack_to_vtk ( 27 sphere_pack , path ) Figure 4: multisphere script to create the multisphere model of the Stanford Bunny shown in fig. 3 . Figure 5 shows sample grains of five granular materials taken from the Sand Atlas and corresponding multisphere-models. These examples cover a broad range of particle morphologies, including angular, rounded, elongated, and flat shapes. Figure 5: Sample grains from the Sand Atlas [ 16 ] . Top row: surface meshes of Hamburg sand [ 16 ] , couscous grains [ 21 ] , Hostun sand [ 23 ] , Ottawa sand [ 19 ] , and Alveolinella quoyi [ 14 ] . Bottom row: corresponding multi-sphere models generated with multisphere . Figure 6 (left) shows the Dice coefficient, eq. 4 , for the reconstruction of multi-sphere models for all granular particles from the Sand Atlas [ 16 ] as a function of the number n of spheres. The solid line represents the mean value \left over all grains, and the shaded region indicates the standard deviation. Figure 6 (right) shows the computer time (arbitrary units, depending on the hardware). A comparative evaluation against other multi-sphere reconstruction algorithms is presented in [ 9 ] . Figure 6: Dice coefficient and multisphere runtime for the reconstruction of all 402 particles from the Sand Atlas [ 16 ] as a function of the number of spheres. 4 Impact and Conclusions multisphere is an open-source implementation of the MSS algorithm in Python. The software generates multisphere models for use in DEM simulations based on voxelised input data, as well as surface meshes, which allows for the direct use of target particle shapes from tomography, 3D scans or CAD. By generating multi-sphere representations with fewer spheres at a prescribed reconstruction accuracy [ 9 ] , multisphere reduces the computational cost of shape-resolved DEM simulations, where the number of sphere–sphere interactions is a key performance factor. This is particularly relevant for simulations involving large numbers of irregular particles and for simulations in which particle shapes must be reconstructed repeatedly during runtime, such as fragmentation and other plastic deformation of the DEM particles.