OptiMic: A tool to generate optimized polycrystalline microstructures for materials simulations

Polycrystal microstructures, with their distinct physical, chemical, structural and topological entities, play an important role in determining the effective properties of materials. Particularly for computational studies, the well-known Voronoi tessellation technique is regularly used for obtaining microstructures. Standard Voronoi tessellations, however, exhibit statistics that are generally far removed from those in real microstructures. Nevertheless, such tessellations can be optimized to obtain certain key features and statistics seen in real microstructures. In this work, we develop the open-source software package OptiMic that enables the generation of optimized microstructures for both finite element as well as atomistic simulations. OptiMic allows for both monodispersive grains as well as irregular grains obtained currently via Voronoi tessellations. These initial microstructures can then be optimized to reflect desired statistical features. A key feature of the tool is that it gives the user extensive control on the optimization process via customizable cost functions. The software currently performs tessellations with the Voronoi method and can be easily extended to include other methods like grain-growth, phase-field etc.


Introduction
Computer simulations have now become an integral part of studies on material behavior. Simulations are increasingly being used over the entire length scale to investigate and explain various phenomena [3,4]. Particularly in studies dealing with the deformation behavior, multiscale simulation techniques are being regularly used to describe, for example, the thermomechanical response of a body subjected to various complex initial and boundary conditions [5]. Notwithstanding the underlying model used to describe the physics of the material as authentically as possible, the predictive power of such simulations depends on two further factors [6]: i) the reproduction of the real-world boundary conditions in the simulated model, and ii) the inclusion of all relevant statistical and topological information in the underlying microstructure so as to accurately describe the effective material behavior.
The microstructure that we refer to is, at the most fundamental level, comprised of individual cells called grains. Each grain is characterized by a distinct orientation, size and shape [7]. The microstructure is made up of further topological entities of varying dimensions: zero-dimensional (0-D) entities called vertex or quadruple points (0-D) are formed at the contact point of four or more grains, one-dimensional (1-D) entities called triple junctions (TJs) or lines (1-D) are formed at the intersection of three or more grains, and two-dimensional (2-D) entities called grain boundaries (GBs) are the interfaces between two grains [8,9]. Further entities include textural characteristics like orientation of individual grains, mis-(dis)orientation and orientation relationship between grains. Both structural and textural characteristics can significantly influence GB properties, topology and network, and thus affect the overall anisotropic response of the polycrystal.
It hence follows that inclusion of microstructure in simulations refers not only to the packing of cells, but also to the inclusion of relevant statistical information on further topological entities. Four kinds of models are generally used to describe the microstructure [10,11]: a) Monodispersive grain size models (e.g., [12][13][14], where all grains have the same shape like hexagons or cubes; b) Voronoi based tessellations (e.g., [15,16]), c) microstructures obtained from physics based models of grain-growth, like e.g. phase-field, vertex grain growth, Potts monte carlo etc. [7,[17][18][19], and d) digitization of realistic microstructures via e.g. serial sectioning [20,21].
Of these, the usage of Voronoi based microstructures is almost ubiquitous in both finite element as well as atomistic simulations, due to the relative ease in generating such structures. Although the grains obtained by Voronoi tessellations look similar to those seen in experimental micrographs, closer quantitative analysis shows that many topological and statistical properties are quite removed from those observed in real microstructures [10,19]. Nonetheless, some microstructural statistics can be tailored by using optimization algorithms so as to obtain optimal initial structures for further simulations. This is done by minimizing a cost function, which penalizes any deviation of the desired statistic from the target distribution.
In this work, we develop a software tool called OptiMic that generates optimized microstructures for both finite element as well as atomistic simulations. The tool generates both mono-dispersive as well as Voronoi tessellations, and allows for the optimization of desired statistics so as to reflect probability distributions as specified by the user. The software computes a variety of statistical information than can be used for the optimization process. OptiMic is a part of growing ecosystem of tools, e.g. Neper [22], Kanapy [23], dream3d [24], MicroStructPy [25] that enable the generation of optimal microstructures mainly for finite element simulations. A key feature of OptiMic is that it gives the user extensive control on the optimization via customizable cost functions, which can in turn be used to treat specific microstructural features. Additionally, the modular approach used in the development allows an easy extension of the code for further tessellation methods like vertex grain growth, phase field etc.

Software description
Generation of a microstructure with OptiMic involves the following steps: • Generate seeds/grain centers in a user defined simulation box • Tessellate simulation box via the Voronoi method using seed positions generated previously • Construct cost function using desired metric • Optimize microstructure by moving the position of seeds • Write out seed coordinates, structural and textural statistics, and other relevant information • Generate finite element mesh • Output surface mesh in VTK [26] and OBJ [27,28] file format for generation of atomistic structures and for further processing The initial step in the generation of the desired microstructure is to tessellate the simulation box with a pre-defined number of seeds or grain centers, which are either placed at regular intervals or at random positions. In the current version of OptiMic, the well-known Voronoi method is used for the tessellation. The Voronoi tessellation generates grain domains T that enclose all points with closest distances to the corresponding grain center. Formally, this can be represented as: where x denotes the coordinates of any point in the n-dimensional simulation box D, N g is the total number of grains, and S denotes the set of coordinates of grain centers. With grain seeds placed at regular intervals, this method results in mono-dispersive grains; with random seeds, one obtains the standard Poisson-Voronoi tessellation.
The so-obtained tessellation is then optimized in a subsequent step. In general, the objective of the optimization process is to achieve a target distribution of a particular metric. The cost function C for optimization is defined simply as the sum of squared differences between the current and target distributions, Y andŶ , respectively: where m is the number of points (or bins) chosen to discretize the distribution. In the current version of OptiMic, the metric chosen for optimization can be distributions of any of the following: i) grain sizes, ii) number of neighbors, iii) grain boundary areas, iv) triple junction lengths, v) angles at a triple junction, vi) disorientation angles, vii) type of GB (in terms of sigma values), and viii) maximum Schmid factors. Equation (2) is the cost function used by default in OptiMic; the user may, however, also define a custom cost function. To this end, in addition to the above mentioned metrics in terms of distributions, the distance between grain centers is also made available. An example of a custom cost function is shown as part of application examples in section 3.2.
The cost function is now minimized using optimization algorithms, which essentially move the grain centers to more favorable positions so as to reflect the desired distribution of the chosen metric. Both gradient-based and gradient-free algorithms can be used for this purpose. The final optimized microstructure is then meshed and/or filled with atoms for further simulations.

Software details
OptiMic is written in Python and uses the following libraries to accomplish various tasks: • Voro++ [29] for generation of Voronoi tessellations. The Python package Tess [30] is used to interface with Voro++.
• scipy.optimize [31] for optimization of the microstructure • Gmsh [32], together with its Python API, for generation of tetrahedral and hexahedral meshes for finite element simulations.
The generated microstructure is periodic in all directions. OptiMic can generate microstructures in both 2D and 3D. The precise execution instructions, including all options of the program, can be found on the wiki page [2] of OptiMic. Although all optimization algorithms in scipy.optimize [31] are available to the user, currently OptiMic has only been tested with the Cobyla [33] and SLSQP [34] algorithms.
Once the microstructure has been optimized, the volume defining individual grains is written out as a surface mesh in the VTK [26] and OBJ [27] file formats; these files are then used for generating atomistic configurations using nanosculpt [11]. Furthermore, all relevant statistical information is written out for further processing by the user. The seed coordinates and orientations of individual grains are written out every iteration, thus enabling a restart of the optimization process, if needed.

Application examples
The following examples were generated on a system with AMD Ryzen 5 2500U (4 physical cores, 8 virtual cores) with Radeon Vega Mobile Gfx processor with 8 Gigabytes (GB) of RAM. The CPU architecture is equipped with L1d, L1i, L2 and L3 cache memory of 32 Kilobytes (KB), 64 KB, 512 KB and 4096 KB respectively. All examples were generated with the simplexbased Cobyla optimization algorithm [33].

3D microstructure with log-normal grain size distribution
In this example, we generate a 3D microstructure with 500 grains in a simulation box of size 15 x 15 x 15 units. The aim of the optimization process in this example is to ensure a (close to) log-normal grain size distribution; herein, the maximum function evaluations were set to 2000. Each grain has a face-centered cubic (fcc) crystal structure and is given a random orientation. The following command is used for execution: By default with random seeds, the Voronoi method generates a tessellation with Poisson-Voronoi grain size distribution (cf. fig. 1a,b). The Cobyla algorithm [33] is able to successfully reduce the cost function and result in a microstructure with log-normal grain size distribution (see fig. 1b,c). The final microstructure along with the meshed and atomistic configurations are shown in fig. 1d,e,f. The optimization process is shown as a movie in supplementary movie S1. Various characteristics of the microstructure generated by OptiMic are shown in fig. 2. These statistics indicate that the optimization process results in an increase of small GBs and TJs ( fig. 2a,b). The distribution of the angles at TJs (fig. 2c), however, remains almost identical. The optimization process also results in slight changes in the neighborhood with a mean of 12 neighbors observed in the optmized configuration. Additionally, the distribution of the nearest neighor distances is also broader, with a peak value around 75% of the mean grain size.
No siginificant changes can be observed in the textural statistics. The disorientation distribution remains close to a Mackenzie type distribution [35] after optimization, which is expected in a polycrystal with random orientations. A slight change of less than 1% is observed in the number of Σ9, Σ11, Σ17, Σ19 GBs. The distribution of Schmid factors in individual grains, considering the global x, y and z as loading directions, also shows no significant changes.

2D microstructure with user-defined cost function
Generation of 2D-columnar microstructures with OptiMic follows in principle the same procedure as in the 3D case. The seed coordinates are, however, defined only in the global x and y directions. The grains are finally extruded in the z direction to obtain a columnar microstructure of a particular thickness.
Voronoi tessellated microstructures constructed with random grain centers can result in extremely small GBs/TJs (see fig. 3a) which pose a problem during meshing of the configuration. A common method to overcome this problem is regularization, which involves the elimination of such GBs/TJs and collapsing corresponding vertices to a single vertex [22]. This, however, results in spurous topological features such as quintuple or higher order junctions, as against triple junctions that are generally observed in real microstructures, and can lead to erroneous results particularly in atomistic simulations [36].
To overcome such undesired features, we define a cost function based on GB areas (here, lengths) and neighbor distances. The function is formally expressed as: whered andĀ GB denote the arrays of neighbor distances and GB areas, respectively. The parameters d c and A c GB denote critical values for distance and GB area, and are taken to be 12 and 6 units, respectively. The function  f (·) takes the form: where the summation is performed over the given array. The symbol * indicates the product of two terms. The parameters α and β work as scaling constants and are given values of 0.6 in the current work. The exponential term results in repulsion of points that are very close to each other, whilst the linear term leads to an attraction of points that are far from one another, albeit to a lesser extent than the repulsion of close points. Upon optimization, the reduction in the overall cost function ( fig. 3b) results in an increase in the smallest nearest neighbor distance to approximately 8 units. Simultaneously, the smallest GB length increases tto approximately 2.5 units ((fig. 3c). The optimized configuration shown in fig. 3d exhibits no spurious features that are visible in the initial configuration (see supplementary movie S2 for more details). Each grain is then assigned a 111 orientation along the thickness and a random in-plane rotation. A fcc crystal structure is used for generating individual crystals.
The command used for this example is: $> ./optimic main --size 180 120 15 --dimension 2 --number seed 122 --target user_grain_size_distribution.txt --characteristic 2 --characteristic 5 --material example_2_2D_10k_so --stress direction 1 0 0 --sharp orientation 1 1 1 --seed spacing random_3d --rand seed 2 --optimization method COBYLA --max iter 10000 --number bins 10 --user cost func user_cost_function_2.py --mesh hex --mesh size 1.0 The statistics of the microstructure before and after optimization are shown in fig. 4. The optimization has almost no effect on the grain size distribution (cf. fig. 4a). The neighborhood distribution ( fig. 4b) shows that grains with less than 3 and more than 8 neighbors are eliminated during the optimization process. Furthermore, very small and very large GBs are reduced. The distribution in TJ lengths shows no change since the length of any TJ is identical to the thickness of the film. The TJ angle distribution ( fig. 4f) is more compact with reduced numbers of both large and small angles at TJs, indicating the manifestation of circular grains (cf. fig. 3d) and is also seen in the distribution of neighbor distances ( fig. 4f). No large differences can be observed in the textural statistics shown in fig. 4g-i. .

Optimized TJ angle distribution in a monodispersive microstructure
In this example, we demonstrate the optimization of a 2D microstructure starting from a mono-dispersive tessellation (hexagons) (fig. 5a). The aim is to obtain a microstructure that looks more realistic than a simple hexagonal tessellation. To this end, we define a target distribution of TJ angles with a spread of ±20 • around a mean TJ angle of 120 • found in the initial microstructure. The command used is: The reduction of the cost function and the final distribution of the TJ angles are shown in fig. 5b,c respectively, and as supplementary movie S3. The optimized Voronoi tessellation ( fig. 5d) shows grains of different sizes and shapes, unlike example 2 where the optimized tessellation manifested more-or-less circular grains. In this example, the meshed configuration is generated as a conforming mesh, i.e. mesh respecting the boundaries of individual grains, as seen in fig. 5e. The atomistic configuration generated using a fcc crystal structure and 110 orientation along the thickness is shown in fig. 5f. For the sake of completeness, a few relevant statistics are shown in fig. 6.

Conclusions
In this work, we have developed an open source software OptiMic that enables the generation of optimized microstructures for simulations of polycrystalline material behavior. The tool is able to generate both monodispersive as well as Voronoi microstructures, which can further be optimized to obtain certain key features and statistics seen in real microstructures. Op-tiMic is highly flexible and allows users to define customized cost functions using a wide variety of statistics that are computed by the program. The functioning of the software has been demonstrated using three different examples, and encompassed both 3D and 2D microstructures, Voronoi and monodispersive microstructures, and the usability of custom cost functions. The code is programmed in a modular fashion and allows easy extension to other tessellation methods.

Open Source Repository
OptiMic is available as open-source code and can be downloaded from the official Gitlab [1] repository. The code is distributed under GNU General Public License, version 3.0.

Conflict of Interest
The authors confirm that there are no known conflicts of interest associated with this publication and there has been no significant financial support for this work that could have influenced its outcome.