from ase.lattice.cubic import FaceCenteredCubic, BodyCenteredCubic
from . import quat_utils
import numpy as np
import itertools
# DELAYED IMPORTS (may not always be available):
#
# scipy.spatial
# matplotlib
# mpl_toolkits
[docs]
def random_rotations(ngrains):
"""Return random grain rotations for all grains.
Parameters
----------
ngrains : int
The number of grains.
Returns
-------
list of numpy.ndarray
A list of 3x3 rotation matrices taken from a uniform distribution of
rotations.
"""
return [quat_utils.random_rotation_matrix() for i in range(ngrains)]
[docs]
def bcc_grains(gridsize, perturbation=0.0):
"""Create a grain layout based on a perturbed BCC lattice.
Creates a set of grain centers in scaled space, i.e. with coordinates
between 0.0 and 1.0, based on a regular BCC lattice, possibly with a
random perturbation added.
Parameters
----------
gridsize : int or tuple of int
Size of the BCC lattice in lattice units.
perturbation : float, optional
If non-zero, all grain centers are perturbed by a random amount drawn
from a normal distribution with this spread. The default is 0.0.
Returns
-------
numpy.ndarray
An N x 3 array of grain centers.
"""
graincenters = BodyCenteredCubic(symbol='H', latticeconstant=1.0, size=grainsize)
graincenters = graincenters.get_positions() + 0.25
graincenters /= grainsize
pert = np.random.standard_normal(graincenters.shape) * perturbation
graincenters += pert
return graincenters
###
### OPTIMIZING VORONOI TESSELATIONS.
###
[docs]
def lloyd_step(centers, box, resolution=20, alpha=1.0, verbose=True):
"""Take a single step of Lloyd's algorithm.
Moves the Voronoi generators towards a centroid Voronoi tesselation.
Parameters
----------
centers : numpy.ndarray
The Voronoi centers (generators), as an N x 3 array.
box : sequence of float
Three numbers giving the size of the computational box (with periodic
boundary conditions).
resolution : int, optional
Use ``2**resolution`` points in the Monte Carlo evaluation of the
centroids. The default is 20.
alpha : float, optional
Acceleration factor in Lloyd's algorithm. Values slightly below 2
are supposedly good. The default, 1.0, gives slow convergence.
verbose : bool, optional
Print progress information. The default is ``True``.
Returns
-------
numpy.ndarray
The updated Voronoi centers, with the same shape as ``centers``.
References
----------
https://en.wikipedia.org/wiki/Lloyds_algorithm
"""
npoints = len(centers)
assert centers.shape == (npoints, 3)
box = np.array(box)
assert box.shape == (3,)
# Generate the periodic images
generators = periodic_images(centers, box)
assert generators.shape == (27*npoints, 3)
# Generate random point distribution
randsize = (1 << resolution)
if verbose:
print(f"Size of random pool: {randsize} ({randsize:.1e})")
rpoints = np.transpose([np.random.uniform(-box[i], 2*box[i], randsize) for i in range(3)])
assert rpoints.shape == (randsize, 3), rpoints.shape
#print(rpoints)
if verbose:
print("Assigning to Voronoi cells.")
belongs_to = [np.argmin( ((p - generators)**2).sum(axis=1)) for p in rpoints]
belongs_to = np.array(belongs_to)
# Calculate the new centroids (only for the original Voronoi centers)
if verbose:
print("Calculating centroids.")
centroids = np.zeros((npoints, 3))
for i in range(npoints):
mypoints = (belongs_to == i)
centroids[i] = rpoints[mypoints].sum(axis=0) / mypoints.sum()
# Acceleration of convergence
centroids = alpha * centroids - (alpha-1) * centers
# Wrap by PBC - not attempting to do it elegantly
for i in range(len(centroids)):
for j in range(3):
if centroids[i,j] < 0.0:
centroids[i,j] += box[j]
if centroids[i,j] >= box[j]:
centroids[i,j] -= box[j]
return centroids
###
### EVALUATING AND PLOTTING VORONOI TESSELATIONS
###
[docs]
def evaluate_voronoi_cells(centers, box):
"""Calculate four parameters evaluating a Voronoi tesselation.
Parameters
----------
centers : numpy.ndarray
An N x 3 array of the grain centers, in real space.
box : tuple of float
The size of the computational box. It is assumed that the centers
are inside the box, or at most slightly outside.
Returns
-------
area : float
The total interfacial area. The cell surface area is a measure of
the quality/regularity of the tesselation, and should decrease if
Lloyd's algorithm is applied.
volume : float
The total cell volume. This should be the volume of the box, and is
only included as a sanity check.
maxdist : float
The largest lateral scaled distance from a cell center to an edge.
It measures how close the most critical Voronoi cell is to touching
itself through the periodic boundary conditions. The value is 0.5 if
a cell touches itself; lower values show how far it is from doing so.
biggrains : int
The number of Voronoi cells touching themselves through the periodic
boundary conditions.
"""
import scipy.spatial
npoints = len(centers)
generators = periodic_images(centers, box)
voronoi = scipy.spatial.Voronoi(generators)
regions = [voronoi.regions[voronoi.point_region[i]] for i in range(npoints)]
for r in regions:
assert -1 not in r
# Calculate surface volume and area
volume = area = 0.0
for reg in regions:
cell_vertices = np.array([voronoi.vertices[v] for v in reg])
hull = scipy.spatial.ConvexHull(cell_vertices)
volume += hull.volume
area += hull.area
# Check if any cell sees itself by calculating the
# max distance to the vertices in the L1 norm
maxdist = 0.0
biggrains = 0
for i in range(npoints):
reg = regions[i]
ctr = centers[i]
cell_vertices = np.array([voronoi.vertices[v] for v in reg])
maxdistgrain = 0
for vert in cell_vertices:
d = np.abs((vert - ctr)/box).max()
if d > maxdistgrain:
maxdistgrain = d
if maxdistgrain > maxdist:
maxdist = maxdistgrain
if maxdistgrain > 0.4999:
biggrains += 1
return area, volume, maxdist, biggrains
[docs]
def plot_voronoi_cells(centers, box, show=True, cellperplot=5):
"""Plot a Voronoi tesselation.
Plots a Voronoi tesselation (nanocrystalline grain structure) using
matplotlib.
Parameters
----------
centers : numpy.ndarray
An N x 3 array of the grain centers, in real space.
box : tuple of float
The size of the computational box. It is assumed that the centers
are inside the box, or at most slightly outside.
show : bool, optional
Set to ``False`` to skip the call to ``matplotlib.pyplot.show``. The
default is ``True``.
cellperplot : int, optional
The number of Voronoi cells drawn in each figure. The default is 5.
Returns
-------
figures : list
The matplotlib Figure objects.
axes : list
The matplotlib Axes3D objects.
"""
import scipy.spatial
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
from matplotlib.colors import colorConverter
npoints = len(centers)
generators = periodic_images(centers, box)
voronoi = scipy.spatial.Voronoi(generators)
regions = [voronoi.regions[voronoi.point_region[i]] for i in range(len(centers))]
for r in regions:
assert -1 not in r
colors = 'rbgyc'
axlist = []
figlist = []
for i, reg in enumerate(regions[:npoints]):
if i % cellperplot == 0:
fig = plt.figure()
ax = Axes3D(fig)
figlist.append(fig)
axlist.append(ax)
cell_vertices = np.array([voronoi.vertices[v] for v in reg])
hull = scipy.spatial.ConvexHull(cell_vertices)
verts = [hull.points[simplex] for simplex in hull.simplices]
poly = Poly3DCollection(verts, facecolors = colorConverter.to_rgba(
colors[i % cellperplot], alpha=0.6))
ax.add_collection3d(poly)
ax.set_xlim3d(0,100.)
ax.set_ylim3d(0,100.)
ax.set_zlim3d(0,100.)
plt.show()
return figlist, axlist
###
### Helper tools
###
def periodic_images(points, box):
points = np.array(points)
box = np.array(box)
result = [points]
for delta in itertools.product((-1,0,1), repeat=3):
if delta == (0, 0, 0):
continue # Do not repeat original points.
newpoints = points + (delta * box)
result.append(newpoints)
return np.concatenate(result)