2020-03-12 04:24:36 +05:30
|
|
|
import sys
|
2019-05-25 02:00:25 +05:30
|
|
|
from io import StringIO
|
2020-03-29 22:42:23 +05:30
|
|
|
import multiprocessing
|
|
|
|
from functools import partial
|
2019-05-25 02:00:25 +05:30
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
import numpy as np
|
2020-03-29 22:42:23 +05:30
|
|
|
from scipy import ndimage,spatial
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
from . import VTK
|
2019-05-28 01:30:26 +05:30
|
|
|
from . import util
|
2020-03-29 22:42:23 +05:30
|
|
|
from . import Environment
|
|
|
|
from . import grid_filters
|
2019-05-28 01:30:26 +05:30
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
2020-03-18 18:19:53 +05:30
|
|
|
class Geom:
|
2019-11-23 01:22:36 +05:30
|
|
|
"""Geometry definition for grid solvers."""
|
|
|
|
|
|
|
|
def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]):
|
|
|
|
"""
|
|
|
|
New geometry definition from array of microstructures and size.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
microstructure : numpy.ndarray
|
|
|
|
microstructure array (3D)
|
|
|
|
size : list or numpy.ndarray
|
|
|
|
physical size of the microstructure in meter.
|
|
|
|
origin : list or numpy.ndarray, optional
|
|
|
|
physical origin of the microstructure in meter.
|
|
|
|
homogenization : integer, optional
|
|
|
|
homogenization index.
|
|
|
|
comments : list of str, optional
|
|
|
|
comments lines.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.set_microstructure(microstructure)
|
|
|
|
self.set_size(size)
|
|
|
|
self.set_origin(origin)
|
|
|
|
self.set_homogenization(homogenization)
|
|
|
|
self.set_comments(comments)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-05-30 19:05:45 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def __repr__(self):
|
|
|
|
"""Basic information on geometry definition."""
|
|
|
|
return util.srepr([
|
2020-06-24 21:35:12 +05:30
|
|
|
f'grid a b c: {util.srepr(self.get_grid ()," x ")}',
|
|
|
|
f'size x y z: {util.srepr(self.get_size ()," x ")}',
|
|
|
|
f'origin x y z: {util.srepr(self.get_origin()," ")}',
|
|
|
|
f'homogenization: {self.get_homogenization()}',
|
|
|
|
f'# microstructures: {self.N_microstructure}',
|
|
|
|
f'max microstructure: {np.nanmax(self.microstructure)}',
|
2019-11-23 01:22:36 +05:30
|
|
|
])
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def update(self,microstructure=None,size=None,origin=None,rescale=False):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Update microstructure and size.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
microstructure : numpy.ndarray, optional
|
|
|
|
microstructure array (3D).
|
|
|
|
size : list or numpy.ndarray, optional
|
|
|
|
physical size of the microstructure in meter.
|
|
|
|
origin : list or numpy.ndarray, optional
|
|
|
|
physical origin of the microstructure in meter.
|
|
|
|
rescale : bool, optional
|
|
|
|
ignore size parameter and rescale according to change of grid points.
|
|
|
|
|
|
|
|
"""
|
|
|
|
grid_old = self.get_grid()
|
|
|
|
size_old = self.get_size()
|
|
|
|
origin_old = self.get_origin()
|
2020-06-24 21:35:12 +05:30
|
|
|
unique_old = self.N_microstructure
|
2019-11-23 01:22:36 +05:30
|
|
|
max_old = np.nanmax(self.microstructure)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
if size is not None and rescale:
|
|
|
|
raise ValueError('Either set size explicitly or rescale automatically')
|
|
|
|
|
|
|
|
self.set_microstructure(microstructure)
|
|
|
|
self.set_origin(origin)
|
|
|
|
|
|
|
|
if size is not None:
|
|
|
|
self.set_size(size)
|
|
|
|
elif rescale:
|
2020-02-22 05:24:15 +05:30
|
|
|
self.set_size(self.get_grid()/grid_old*self.size)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message = [f'grid a b c: {util.srepr(grid_old," x ")}']
|
2019-11-23 01:22:36 +05:30
|
|
|
if np.any(grid_old != self.get_grid()):
|
|
|
|
message[-1] = util.delete(message[-1])
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(util.emph(f'grid a b c: {util.srepr(self.get_grid()," x ")}'))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(f'size x y z: {util.srepr(size_old," x ")}')
|
2019-11-23 01:22:36 +05:30
|
|
|
if np.any(size_old != self.get_size()):
|
|
|
|
message[-1] = util.delete(message[-1])
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(util.emph(f'size x y z: {util.srepr(self.get_size()," x ")}'))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(f'origin x y z: {util.srepr(origin_old," ")}')
|
2019-11-23 01:22:36 +05:30
|
|
|
if np.any(origin_old != self.get_origin()):
|
|
|
|
message[-1] = util.delete(message[-1])
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(util.emph(f'origin x y z: {util.srepr(self.get_origin()," ")}'))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(f'homogenization: {self.get_homogenization()}')
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(f'# microstructures: {unique_old}')
|
|
|
|
if unique_old != self.N_microstructure:
|
2019-11-23 01:22:36 +05:30
|
|
|
message[-1] = util.delete(message[-1])
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(util.emph(f'# microstructures: {self.N_microstructure}'))
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(f'max microstructure: {max_old}')
|
2019-11-23 01:22:36 +05:30
|
|
|
if max_old != np.nanmax(self.microstructure):
|
|
|
|
message[-1] = util.delete(message[-1])
|
2020-06-24 21:35:12 +05:30
|
|
|
message.append(util.emph(f'max microstructure: {np.nanmax(self.microstructure)}'))
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
return util.return_message(message)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def set_comments(self,comments):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Replace all existing comments.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
comments : list of str
|
|
|
|
new comments.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.comments = []
|
|
|
|
self.add_comments(comments)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def add_comments(self,comments):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Append comments to existing comments.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
comments : list of str
|
|
|
|
new comments.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)]
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def set_microstructure(self,microstructure):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Replace the existing microstructure representation.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
microstructure : numpy.ndarray
|
|
|
|
microstructure array (3D).
|
|
|
|
|
|
|
|
"""
|
|
|
|
if microstructure is not None:
|
|
|
|
if len(microstructure.shape) != 3:
|
2020-06-24 21:35:12 +05:30
|
|
|
raise ValueError(f'Invalid microstructure shape {microstructure.shape}')
|
2019-11-23 01:22:36 +05:30
|
|
|
elif microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']:
|
2020-06-24 21:35:12 +05:30
|
|
|
raise TypeError(f'Invalid microstructue data type {microstructure.dtype}')
|
2019-11-23 01:22:36 +05:30
|
|
|
else:
|
|
|
|
self.microstructure = np.copy(microstructure)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def set_size(self,size):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Replace the existing size information.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
size : list or numpy.ndarray
|
|
|
|
physical size of the microstructure in meter.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if size is None:
|
|
|
|
grid = np.asarray(self.microstructure.shape)
|
|
|
|
self.size = grid/np.max(grid)
|
|
|
|
else:
|
2020-06-24 21:35:12 +05:30
|
|
|
if len(size) != 3 or any(np.array(size) <= 0):
|
|
|
|
raise ValueError(f'Invalid size {size}')
|
2019-11-23 01:22:36 +05:30
|
|
|
else:
|
|
|
|
self.size = np.array(size)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def set_origin(self,origin):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Replace the existing origin information.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
origin : list or numpy.ndarray
|
|
|
|
physical origin of the microstructure in meter
|
|
|
|
|
|
|
|
"""
|
|
|
|
if origin is not None:
|
|
|
|
if len(origin) != 3:
|
2020-06-24 21:35:12 +05:30
|
|
|
raise ValueError(f'Invalid origin {origin}')
|
2019-11-23 01:22:36 +05:30
|
|
|
else:
|
|
|
|
self.origin = np.array(origin)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def set_homogenization(self,homogenization):
|
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Replace the existing homogenization index.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
homogenization : integer
|
|
|
|
homogenization index
|
|
|
|
|
|
|
|
"""
|
|
|
|
if homogenization is not None:
|
|
|
|
if not isinstance(homogenization,int) or homogenization < 1:
|
2020-06-24 21:35:12 +05:30
|
|
|
raise TypeError(f'Invalid homogenization {homogenization}')
|
2019-11-23 01:22:36 +05:30
|
|
|
else:
|
|
|
|
self.homogenization = homogenization
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-12-08 13:47:57 +05:30
|
|
|
@property
|
|
|
|
def grid(self):
|
|
|
|
return self.get_grid()
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2020-03-18 18:19:53 +05:30
|
|
|
@property
|
|
|
|
def N_microstructure(self):
|
2020-06-24 21:35:12 +05:30
|
|
|
return np.unique(self.microstructure).size
|
2020-03-18 18:19:53 +05:30
|
|
|
|
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_microstructure(self):
|
|
|
|
"""Return the microstructure representation."""
|
|
|
|
return np.copy(self.microstructure)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
def get_size(self):
|
2019-11-23 01:22:36 +05:30
|
|
|
"""Return the physical size in meter."""
|
|
|
|
return np.copy(self.size)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_origin(self):
|
|
|
|
"""Return the origin in meter."""
|
|
|
|
return np.copy(self.origin)
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_grid(self):
|
|
|
|
"""Return the grid discretization."""
|
2020-05-25 20:11:23 +05:30
|
|
|
return np.asarray(self.microstructure.shape)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_homogenization(self):
|
|
|
|
"""Return the homogenization index."""
|
|
|
|
return self.homogenization
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_comments(self):
|
|
|
|
"""Return the comments."""
|
|
|
|
return self.comments[:]
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def get_header(self):
|
|
|
|
"""Return the full header (grid, size, origin, homogenization, comments)."""
|
2020-06-25 01:04:51 +05:30
|
|
|
header = [f'{len(self.comments)+4} header'] + self.comments
|
2019-11-23 01:22:36 +05:30
|
|
|
header.append('grid a {} b {} c {}'.format(*self.get_grid()))
|
|
|
|
header.append('size x {} y {} z {}'.format(*self.get_size()))
|
|
|
|
header.append('origin x {} y {} z {}'.format(*self.get_origin()))
|
2020-06-25 01:04:51 +05:30
|
|
|
header.append(f'homogenization {self.get_homogenization()}')
|
2019-11-23 01:22:36 +05:30
|
|
|
return header
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2019-11-27 01:02:54 +05:30
|
|
|
@staticmethod
|
|
|
|
def from_file(fname):
|
2019-11-23 01:22:36 +05:30
|
|
|
"""
|
2020-03-29 22:42:23 +05:30
|
|
|
Read a geom file.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : str or file handle
|
|
|
|
geometry file to read.
|
|
|
|
|
|
|
|
"""
|
2019-11-25 18:17:14 +05:30
|
|
|
try:
|
|
|
|
f = open(fname)
|
|
|
|
except TypeError:
|
|
|
|
f = fname
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
header_length,keyword = f.readline().split()[:2]
|
|
|
|
header_length = int(header_length)
|
|
|
|
content = f.readlines()
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
if not keyword.startswith('head') or header_length < 3:
|
|
|
|
raise TypeError('Header length information missing or invalid')
|
|
|
|
|
2020-03-21 15:37:21 +05:30
|
|
|
comments = []
|
2019-11-23 01:22:36 +05:30
|
|
|
for i,line in enumerate(content[:header_length]):
|
2020-04-02 15:24:34 +05:30
|
|
|
items = line.split('#')[0].lower().strip().split()
|
2020-02-22 05:24:15 +05:30
|
|
|
key = items[0] if items else ''
|
2019-11-23 01:22:36 +05:30
|
|
|
if key == 'grid':
|
|
|
|
grid = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']])
|
|
|
|
elif key == 'size':
|
|
|
|
size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
elif key == 'origin':
|
|
|
|
origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
elif key == 'homogenization':
|
|
|
|
homogenization = int(items[1])
|
|
|
|
else:
|
|
|
|
comments.append(line.strip())
|
|
|
|
|
|
|
|
microstructure = np.empty(grid.prod()) # initialize as flat array
|
|
|
|
i = 0
|
|
|
|
for line in content[header_length:]:
|
2020-04-02 15:24:34 +05:30
|
|
|
items = line.split('#')[0].split()
|
2019-11-23 01:22:36 +05:30
|
|
|
if len(items) == 3:
|
|
|
|
if items[1].lower() == 'of':
|
|
|
|
items = np.ones(int(items[0]))*float(items[2])
|
|
|
|
elif items[1].lower() == 'to':
|
|
|
|
items = np.linspace(int(items[0]),int(items[2]),
|
|
|
|
abs(int(items[2])-int(items[0]))+1,dtype=float)
|
|
|
|
else: items = list(map(float,items))
|
|
|
|
else: items = list(map(float,items))
|
|
|
|
microstructure[i:i+len(items)] = items
|
|
|
|
i += len(items)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
if i != grid.prod():
|
2020-06-24 21:35:12 +05:30
|
|
|
raise TypeError(f'Invalid file: expected {grid.prod()} entries, found {i}')
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-04-20 23:44:27 +05:30
|
|
|
if not np.any(np.mod(microstructure,1) != 0.0): # no float present
|
2019-11-23 01:22:36 +05:30
|
|
|
microstructure = microstructure.astype('int')
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-04-20 23:44:27 +05:30
|
|
|
return Geom(microstructure.reshape(grid,order='F'),size,origin,homogenization,comments)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
@staticmethod
|
|
|
|
def _find_closest_seed(seeds, weights, point):
|
|
|
|
return np.argmin(np.sum((np.broadcast_to(point,(len(seeds),3))-seeds)**2,axis=1) - weights)
|
2020-06-24 21:35:12 +05:30
|
|
|
|
2020-03-29 22:42:23 +05:30
|
|
|
@staticmethod
|
|
|
|
def from_Laguerre_tessellation(grid,size,seeds,weights,periodic=True):
|
|
|
|
"""
|
|
|
|
Generate geometry from Laguerre tessellation.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
grid : numpy.ndarray of shape (3)
|
|
|
|
number of grid points in x,y,z direction.
|
|
|
|
size : list or numpy.ndarray of shape (3)
|
|
|
|
physical size of the microstructure in meter.
|
|
|
|
seeds : numpy.ndarray of shape (:,3)
|
|
|
|
position of the seed points in meter. All points need to lay within the box.
|
|
|
|
weights : numpy.ndarray of shape (seeds.shape[0])
|
|
|
|
weights of the seeds. Setting all weights to 1.0 gives a standard Voronoi tessellation.
|
|
|
|
periodic : Boolean, optional
|
|
|
|
perform a periodic tessellation. Defaults to True.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if periodic:
|
2020-04-20 23:46:25 +05:30
|
|
|
weights_p = np.tile(weights,27) # Laguerre weights (1,2,3,1,2,3,...,1,2,3)
|
2020-03-29 22:42:23 +05:30
|
|
|
seeds_p = np.vstack((seeds -np.array([size[0],0.,0.]),seeds, seeds +np.array([size[0],0.,0.])))
|
|
|
|
seeds_p = np.vstack((seeds_p-np.array([0.,size[1],0.]),seeds_p,seeds_p+np.array([0.,size[1],0.])))
|
|
|
|
seeds_p = np.vstack((seeds_p-np.array([0.,0.,size[2]]),seeds_p,seeds_p+np.array([0.,0.,size[2]])))
|
2020-04-20 23:54:55 +05:30
|
|
|
coords = grid_filters.cell_coord0(grid*3,size*3,-size).reshape(-1,3)
|
2020-03-29 22:42:23 +05:30
|
|
|
else:
|
2020-04-20 23:46:25 +05:30
|
|
|
weights_p = weights
|
2020-03-29 22:42:23 +05:30
|
|
|
seeds_p = seeds
|
2020-04-20 23:54:55 +05:30
|
|
|
coords = grid_filters.cell_coord0(grid,size).reshape(-1,3)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
pool = multiprocessing.Pool(processes = int(Environment().options['DAMASK_NUM_THREADS']))
|
|
|
|
result = pool.map_async(partial(Geom._find_closest_seed,seeds_p,weights_p), [coord for coord in coords])
|
|
|
|
pool.close()
|
|
|
|
pool.join()
|
|
|
|
microstructure = np.array(result.get())
|
|
|
|
|
|
|
|
if periodic:
|
2020-04-20 23:54:55 +05:30
|
|
|
microstructure = microstructure.reshape(grid*3)
|
2020-03-29 22:42:23 +05:30
|
|
|
microstructure = microstructure[grid[0]:grid[0]*2,grid[1]:grid[1]*2,grid[2]:grid[2]*2]%seeds.shape[0]
|
|
|
|
else:
|
2020-04-20 23:54:55 +05:30
|
|
|
microstructure = microstructure.reshape(grid)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: comments = 'geom.py:from_Laguerre_tessellation v{}'.format(version)
|
2020-03-29 22:42:23 +05:30
|
|
|
return Geom(microstructure+1,size,homogenization=1)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_Voronoi_tessellation(grid,size,seeds,periodic=True):
|
|
|
|
"""
|
|
|
|
Generate geometry from Voronoi tessellation.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
grid : numpy.ndarray of shape (3)
|
|
|
|
number of grid points in x,y,z direction.
|
|
|
|
size : list or numpy.ndarray of shape (3)
|
|
|
|
physical size of the microstructure in meter.
|
|
|
|
seeds : numpy.ndarray of shape (:,3)
|
|
|
|
position of the seed points in meter. All points need to lay within the box.
|
|
|
|
periodic : Boolean, optional
|
|
|
|
perform a periodic tessellation. Defaults to True.
|
|
|
|
|
|
|
|
"""
|
2020-04-20 23:54:55 +05:30
|
|
|
coords = grid_filters.cell_coord0(grid,size).reshape(-1,3)
|
2020-03-29 22:42:23 +05:30
|
|
|
KDTree = spatial.cKDTree(seeds,boxsize=size) if periodic else spatial.cKDTree(seeds)
|
|
|
|
devNull,microstructure = KDTree.query(coords)
|
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: comments = 'geom.py:from_Voronoi_tessellation v{}'.format(version)
|
2020-04-20 23:54:55 +05:30
|
|
|
return Geom(microstructure.reshape(grid)+1,size,homogenization=1)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
2020-03-18 18:59:59 +05:30
|
|
|
def to_file(self,fname,pack=None):
|
2019-11-23 01:22:36 +05:30
|
|
|
"""
|
|
|
|
Writes a geom file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : str or file handle
|
|
|
|
geometry file to write.
|
2019-11-24 18:57:24 +05:30
|
|
|
pack : bool, optional
|
|
|
|
compress geometry with 'x of y' and 'a to b'.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
header = self.get_header()
|
|
|
|
grid = self.get_grid()
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-03-18 18:59:59 +05:30
|
|
|
if pack is None:
|
2020-06-24 21:35:12 +05:30
|
|
|
plain = grid.prod()/self.N_microstructure < 250
|
2019-11-24 18:57:24 +05:30
|
|
|
else:
|
|
|
|
plain = not pack
|
|
|
|
|
|
|
|
if plain:
|
2019-11-25 18:17:14 +05:30
|
|
|
format_string = '%g' if self.microstructure.dtype in np.sctypes['float'] else \
|
2019-11-24 18:57:24 +05:30
|
|
|
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure)))))
|
|
|
|
np.savetxt(fname,
|
|
|
|
self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T,
|
|
|
|
header='\n'.join(header), fmt=format_string, comments='')
|
|
|
|
else:
|
2019-11-25 18:17:14 +05:30
|
|
|
try:
|
2019-11-24 18:57:24 +05:30
|
|
|
f = open(fname,'w')
|
2019-11-25 18:17:14 +05:30
|
|
|
except TypeError:
|
2019-11-24 18:57:24 +05:30
|
|
|
f = fname
|
|
|
|
|
|
|
|
compressType = None
|
|
|
|
former = start = -1
|
|
|
|
reps = 0
|
|
|
|
for current in self.microstructure.flatten('F'):
|
|
|
|
if abs(current - former) == 1 and (start - current) == reps*(former - current):
|
|
|
|
compressType = 'to'
|
|
|
|
reps += 1
|
|
|
|
elif current == former and start == former:
|
|
|
|
compressType = 'of'
|
|
|
|
reps += 1
|
|
|
|
else:
|
|
|
|
if compressType is None:
|
|
|
|
f.write('\n'.join(self.get_header())+'\n')
|
|
|
|
elif compressType == '.':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
elif compressType == 'to':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{start} to {former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
elif compressType == 'of':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{reps} of {former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
|
|
|
|
compressType = '.'
|
|
|
|
start = current
|
|
|
|
reps = 1
|
|
|
|
|
|
|
|
former = current
|
|
|
|
|
|
|
|
if compressType == '.':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
elif compressType == 'to':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{start} to {former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
elif compressType == 'of':
|
2020-06-24 21:35:12 +05:30
|
|
|
f.write(f'{reps} of {former}\n')
|
2019-11-24 18:57:24 +05:30
|
|
|
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def to_vtk(self,fname=None):
|
|
|
|
"""
|
|
|
|
Generates vtk file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : str, optional
|
|
|
|
vtk file to write. If no file is given, a string is returned.
|
|
|
|
|
|
|
|
"""
|
2020-03-11 12:02:03 +05:30
|
|
|
v = VTK.from_rectilinearGrid(self.grid,self.size,self.origin)
|
2020-03-12 04:24:36 +05:30
|
|
|
v.add(self.microstructure.flatten(order='F'),'microstructure')
|
2019-11-23 01:22:36 +05:30
|
|
|
|
2020-03-12 04:24:36 +05:30
|
|
|
if fname:
|
|
|
|
v.write(fname)
|
2019-11-23 01:22:36 +05:30
|
|
|
else:
|
2020-03-12 04:24:36 +05:30
|
|
|
sys.stdout.write(v.__repr__())
|
2020-03-15 02:23:48 +05:30
|
|
|
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2019-11-23 01:22:36 +05:30
|
|
|
def show(self):
|
|
|
|
"""Show raw content (as in file)."""
|
|
|
|
f=StringIO()
|
|
|
|
self.to_file(f)
|
|
|
|
f.seek(0)
|
|
|
|
return ''.join(f.readlines())
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
|
|
def mirror(self,directions,reflect=False):
|
|
|
|
"""
|
|
|
|
Mirror microstructure along given directions.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
directions : iterable containing str
|
|
|
|
direction(s) along which the microstructure is mirrored. Valid entries are 'x', 'y', 'z'.
|
|
|
|
reflect : bool, optional
|
|
|
|
reflect (include) outermost layers.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
"""
|
|
|
|
valid = {'x','y','z'}
|
|
|
|
if not all(isinstance(d, str) for d in directions):
|
|
|
|
raise TypeError('Directions are not of type str.')
|
|
|
|
elif not set(directions).issubset(valid):
|
2020-06-24 21:35:12 +05:30
|
|
|
raise ValueError(f'Invalid direction {set(directions).difference(valid)} specified.')
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
limits = [None,None] if reflect else [-2,0]
|
|
|
|
ms = self.get_microstructure()
|
|
|
|
|
|
|
|
if 'z' in directions:
|
|
|
|
ms = np.concatenate([ms,ms[:,:,limits[0]:limits[1]:-1]],2)
|
|
|
|
if 'y' in directions:
|
|
|
|
ms = np.concatenate([ms,ms[:,limits[0]:limits[1]:-1,:]],1)
|
|
|
|
if 'x' in directions:
|
|
|
|
ms = np.concatenate([ms,ms[limits[0]:limits[1]:-1,:,:]],0)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:mirror v{}'.format(version)
|
2019-11-23 02:18:41 +05:30
|
|
|
return self.update(ms,rescale=True)
|
|
|
|
|
|
|
|
|
2019-11-24 19:43:26 +05:30
|
|
|
def scale(self,grid):
|
|
|
|
"""
|
2019-11-24 23:55:01 +05:30
|
|
|
Scale microstructure to new grid.
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-05-30 21:01:50 +05:30
|
|
|
grid : numpy.ndarray of shape (3)
|
|
|
|
number of grid points in x,y,z direction.
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
"""
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:scale v{}'.format(version)
|
2019-11-24 19:43:26 +05:30
|
|
|
return self.update(
|
|
|
|
ndimage.interpolation.zoom(
|
|
|
|
self.microstructure,
|
|
|
|
grid/self.get_grid(),
|
|
|
|
output=self.microstructure.dtype,
|
|
|
|
order=0,
|
|
|
|
mode='nearest',
|
|
|
|
prefilter=False
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
def clean(self,stencil=3):
|
|
|
|
"""
|
|
|
|
Smooth microstructure by selecting most frequent index within given stencil at each location.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
stencil : int, optional
|
|
|
|
size of smoothing stencil.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
"""
|
|
|
|
def mostFrequent(arr):
|
|
|
|
unique, inverse = np.unique(arr, return_inverse=True)
|
|
|
|
return unique[np.argmax(np.bincount(inverse))]
|
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:clean v{}'.format(version)
|
2019-11-24 22:51:05 +05:30
|
|
|
return self.update(ndimage.filters.generic_filter(
|
|
|
|
self.microstructure,
|
2019-11-23 02:18:41 +05:30
|
|
|
mostFrequent,
|
2019-11-24 22:51:05 +05:30
|
|
|
size=(stencil,)*3
|
|
|
|
).astype(self.microstructure.dtype)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def renumber(self):
|
|
|
|
"""Renumber sorted microstructure indices to 1,...,N."""
|
|
|
|
renumbered = np.empty(self.get_grid(),dtype=self.microstructure.dtype)
|
|
|
|
for i, oldID in enumerate(np.unique(self.microstructure)):
|
2020-02-22 05:24:15 +05:30
|
|
|
renumbered = np.where(self.microstructure == oldID, i+1, renumbered)
|
2019-11-24 22:51:05 +05:30
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:renumber v{}'.format(version)
|
2019-11-24 23:32:19 +05:30
|
|
|
return self.update(renumbered)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
def rotate(self,R,fill=None):
|
2020-05-30 21:01:50 +05:30
|
|
|
"""
|
|
|
|
Rotate microstructure (pad if required).
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
R : damask.Rotation
|
|
|
|
rotation to apply to the microstructure.
|
|
|
|
fill : int or float, optional
|
|
|
|
microstructure index to fill the corners. Defaults to microstructure.max() + 1.
|
|
|
|
|
|
|
|
"""
|
2020-05-24 12:36:42 +05:30
|
|
|
if fill is None: fill = np.nanmax(self.microstructure) + 1
|
|
|
|
dtype = float if np.isnan(fill) or int(fill) != fill or self.microstructure.dtype==np.float else int
|
|
|
|
|
|
|
|
Eulers = R.as_Eulers(degrees=True)
|
2020-05-24 22:00:45 +05:30
|
|
|
microstructure_in = self.get_microstructure()
|
2020-05-25 00:22:19 +05:30
|
|
|
|
2020-05-24 12:36:42 +05:30
|
|
|
# These rotations are always applied in the reference coordinate system, i.e. (z,x,z) not (z,x',z'')
|
2020-05-24 22:00:45 +05:30
|
|
|
# see https://www.cs.utexas.edu/~theshark/courses/cs354/lectures/cs354-14.pdf
|
2020-05-25 00:22:19 +05:30
|
|
|
for angle,axes in zip(Eulers[::-1], [(0,1),(1,2),(0,1)]):
|
|
|
|
microstructure_out = ndimage.rotate(microstructure_in,angle,axes,order=0,
|
2020-05-24 22:00:45 +05:30
|
|
|
prefilter=False,output=dtype,cval=fill)
|
2020-05-25 00:22:19 +05:30
|
|
|
if np.prod(microstructure_in.shape) == np.prod(microstructure_out.shape):
|
|
|
|
# avoid scipy interpolation errors for rotations close to multiples of 90°
|
|
|
|
microstructure_in = np.rot90(microstructure_in,k=np.rint(angle/90.).astype(int),axes=axes)
|
|
|
|
else:
|
|
|
|
microstructure_in = microstructure_out
|
2020-05-24 12:36:42 +05:30
|
|
|
|
2020-05-25 00:22:19 +05:30
|
|
|
origin = self.origin-(np.asarray(microstructure_in.shape)-self.grid)*.5 * self.size/self.grid
|
2020-05-24 12:36:42 +05:30
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:rotate v{}'.format(version)
|
2020-05-25 00:22:19 +05:30
|
|
|
return self.update(microstructure_in,origin=origin,rescale=True)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
def canvas(self,grid=None,offset=None,fill=None):
|
2020-05-30 21:01:50 +05:30
|
|
|
"""
|
|
|
|
Crop or enlarge/pad microstructure.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
grid : numpy.ndarray of shape (3)
|
|
|
|
number of grid points in x,y,z direction.
|
|
|
|
offset : numpy.ndarray of shape (3)
|
|
|
|
offset (measured in grid points) from old to new microstructue[0,0,0].
|
|
|
|
fill : int or float, optional
|
|
|
|
microstructure index to fill the corners. Defaults to microstructure.max() + 1.
|
|
|
|
|
|
|
|
"""
|
2020-05-24 12:36:42 +05:30
|
|
|
if fill is None: fill = np.nanmax(self.microstructure) + 1
|
2020-05-25 02:22:00 +05:30
|
|
|
if offset is None: offset = 0
|
2020-05-25 19:24:22 +05:30
|
|
|
dtype = float if int(fill) != fill or self.microstructure.dtype==np.float else int
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
canvas = np.full(self.grid if grid is None else grid,
|
2020-05-25 19:24:22 +05:30
|
|
|
fill if fill is not None else np.nanmax(self.microstructure)+1,dtype)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
l = np.clip( offset, 0,np.minimum(self.grid +offset,grid)) # noqa
|
|
|
|
r = np.clip( offset+self.grid,0,np.minimum(self.grid*2+offset,grid))
|
|
|
|
L = np.clip(-offset, 0,np.minimum(grid -offset,self.grid))
|
|
|
|
R = np.clip(-offset+grid, 0,np.minimum(grid*2 -offset,self.grid))
|
|
|
|
|
|
|
|
canvas[l[0]:r[0],l[1]:r[1],l[2]:r[2]] = self.microstructure[L[0]:R[0],L[1]:R[1],L[2]:R[2]]
|
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:canvas v{}'.format(version)
|
2020-05-24 12:36:42 +05:30
|
|
|
return self.update(canvas,origin=self.origin+offset*self.size/self.grid,rescale=True)
|
|
|
|
|
|
|
|
|
|
|
|
def substitute(self,from_microstructure,to_microstructure):
|
2020-05-30 21:01:50 +05:30
|
|
|
"""
|
|
|
|
Substitude microstructure indices.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
from_microstructure : iterable of ints
|
|
|
|
microstructure indices to be substituted.
|
|
|
|
to_microstructure : iterable of ints
|
|
|
|
new microstructure indices.
|
|
|
|
|
|
|
|
"""
|
2020-05-24 12:36:42 +05:30
|
|
|
substituted = self.get_microstructure()
|
|
|
|
for from_ms,to_ms in zip(from_microstructure,to_microstructure):
|
|
|
|
substituted[self.microstructure==from_ms] = to_ms
|
|
|
|
|
2020-06-25 11:49:07 +05:30
|
|
|
#ToDo: self.add_comments('geom.py:substitute v{}'.format(version)
|
2020-05-24 12:36:42 +05:30
|
|
|
return self.update(substituted)
|