2020-08-22 23:25:18 +05:30
|
|
|
|
import copy
|
2020-09-18 20:02:08 +05:30
|
|
|
|
import multiprocessing as mp
|
2020-03-29 22:42:23 +05:30
|
|
|
|
from functools import partial
|
2020-10-08 22:03:40 +05:30
|
|
|
|
from os import path
|
2019-05-25 02:00:25 +05:30
|
|
|
|
|
2019-05-26 15:33:21 +05:30
|
|
|
|
import numpy as np
|
2020-10-28 16:11:34 +05:30
|
|
|
|
import pandas as pd
|
2020-10-08 22:03:40 +05:30
|
|
|
|
import h5py
|
2020-03-29 22:42:23 +05:30
|
|
|
|
from scipy import ndimage,spatial
|
2019-05-26 15:33:21 +05:30
|
|
|
|
|
2020-08-08 22:11:47 +05:30
|
|
|
|
from . import environment
|
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 grid_filters
|
2020-10-08 22:03:40 +05:30
|
|
|
|
from . import Rotation
|
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."""
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
def __init__(self,material,size,origin=[0.0,0.0,0.0],comments=[]):
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
New geometry definition from array of materials, size, and origin.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material : numpy.ndarray
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Material index array (3D).
|
2019-11-23 01:22:36 +05:30
|
|
|
|
size : list or numpy.ndarray
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Physical size of the geometry in meter.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
origin : list or numpy.ndarray, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Physical origin of the geometry in meter.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
comments : list of str, optional
|
2020-08-24 04:16:01 +05:30
|
|
|
|
Comment lines.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
self.material = material
|
|
|
|
|
self.size = size
|
|
|
|
|
self.origin = origin
|
|
|
|
|
self.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-09-23 00:52:58 +05:30
|
|
|
|
f'grid a b c: {util.srepr(self.grid, " x ")}',
|
|
|
|
|
f'size x y z: {util.srepr(self.size, " x ")}',
|
|
|
|
|
f'origin x y z: {util.srepr(self.origin," ")}',
|
|
|
|
|
f'# materials: {self.N_materials}',
|
2020-09-24 02:57:15 +05:30
|
|
|
|
f'max material: {np.nanmax(self.material)}',
|
2019-11-23 01:22:36 +05:30
|
|
|
|
])
|
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2020-08-22 23:25:18 +05:30
|
|
|
|
def __copy__(self):
|
|
|
|
|
"""Copy geometry."""
|
|
|
|
|
return copy.deepcopy(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
|
"""Copy geometry."""
|
|
|
|
|
return self.__copy__()
|
|
|
|
|
|
|
|
|
|
|
2020-08-24 04:16:01 +05:30
|
|
|
|
def diff(self,other):
|
|
|
|
|
"""
|
|
|
|
|
Report property differences of self relative to other.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
other : Geom
|
|
|
|
|
Geometry to compare self against.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
message = []
|
2020-09-21 20:43:53 +05:30
|
|
|
|
if np.any(other.grid != self.grid):
|
|
|
|
|
message.append(util.delete(f'grid a b c: {util.srepr(other.grid," x ")}'))
|
|
|
|
|
message.append(util.emph( f'grid a b c: {util.srepr( self.grid," x ")}'))
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if not np.allclose(other.size,self.size):
|
2020-09-21 20:43:53 +05:30
|
|
|
|
message.append(util.delete(f'size x y z: {util.srepr(other.size," x ")}'))
|
|
|
|
|
message.append(util.emph( f'size x y z: {util.srepr( self.size," x ")}'))
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if not np.allclose(other.origin,self.origin):
|
2020-09-21 20:43:53 +05:30
|
|
|
|
message.append(util.delete(f'origin x y z: {util.srepr(other.origin," ")}'))
|
|
|
|
|
message.append(util.emph( f'origin x y z: {util.srepr( self.origin," ")}'))
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if other.N_materials != self.N_materials:
|
2020-09-23 00:52:58 +05:30
|
|
|
|
message.append(util.delete(f'# materials: {other.N_materials}'))
|
|
|
|
|
message.append(util.emph( f'# materials: { self.N_materials}'))
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
if np.nanmax(other.material) != np.nanmax(self.material):
|
|
|
|
|
message.append(util.delete(f'max material: {np.nanmax(other.material)}'))
|
|
|
|
|
message.append(util.emph( f'max material: {np.nanmax( self.material)}'))
|
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
|
|
|
|
|
2020-11-08 22:41:30 +05:30
|
|
|
|
@property
|
|
|
|
|
def material(self):
|
|
|
|
|
"""Material indices."""
|
|
|
|
|
return self._material
|
|
|
|
|
|
|
|
|
|
@material.setter
|
|
|
|
|
def material(self,material):
|
|
|
|
|
if len(material.shape) != 3:
|
|
|
|
|
raise ValueError(f'Invalid material shape {material.shape}.')
|
|
|
|
|
elif material.dtype not in np.sctypes['float'] + np.sctypes['int']:
|
|
|
|
|
raise TypeError(f'Invalid material data type {material.dtype}.')
|
|
|
|
|
else:
|
|
|
|
|
self._material = np.copy(material)
|
|
|
|
|
|
|
|
|
|
if self.material.dtype in np.sctypes['float'] and \
|
|
|
|
|
np.all(self.material == self.material.astype(int).astype(float)):
|
|
|
|
|
self._material = self.material.astype(int)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def size(self):
|
|
|
|
|
"""Physical size of geometry in meter."""
|
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
|
|
@size.setter
|
|
|
|
|
def size(self,size):
|
|
|
|
|
if len(size) != 3 or any(np.array(size) <= 0):
|
|
|
|
|
raise ValueError(f'Invalid size {size}.')
|
|
|
|
|
else:
|
|
|
|
|
self._size = np.array(size)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def origin(self):
|
|
|
|
|
"""Coordinates of geometry origin in meter."""
|
|
|
|
|
return self._origin
|
|
|
|
|
|
|
|
|
|
@origin.setter
|
|
|
|
|
def origin(self,origin):
|
|
|
|
|
if len(origin) != 3:
|
|
|
|
|
raise ValueError(f'Invalid origin {origin}.')
|
|
|
|
|
else:
|
|
|
|
|
self._origin = np.array(origin)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def comments(self):
|
|
|
|
|
"""Comments/history of geometry."""
|
|
|
|
|
return self._comments
|
|
|
|
|
|
|
|
|
|
@comments.setter
|
|
|
|
|
def comments(self,comments):
|
|
|
|
|
self._comments = [str(c) for c in comments] if isinstance(comments,list) else [str(comments)]
|
|
|
|
|
|
|
|
|
|
|
2019-12-08 13:47:57 +05:30
|
|
|
|
@property
|
|
|
|
|
def grid(self):
|
2020-11-08 22:41:30 +05:30
|
|
|
|
"""Grid dimension of geometry."""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return np.asarray(self.material.shape)
|
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
|
2020-09-23 00:17:08 +05:30
|
|
|
|
def N_materials(self):
|
2020-11-08 22:41:30 +05:30
|
|
|
|
"""Number of (unique) material indices within geometry."""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return np.unique(self.material).size
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
2020-03-15 02:23:48 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
@staticmethod
|
|
|
|
|
def load(fname):
|
|
|
|
|
"""
|
|
|
|
|
Read a VTK rectilinear grid.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str or or pathlib.Path
|
|
|
|
|
Geometry file to read.
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Valid extension is .vtr, which will be appended if not given.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
v = VTK.load(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr')
|
|
|
|
|
comments = v.get_comments()
|
|
|
|
|
grid = np.array(v.vtk_data.GetDimensions())-1
|
|
|
|
|
bbox = np.array(v.vtk_data.GetBounds()).reshape(3,2).T
|
|
|
|
|
|
|
|
|
|
return Geom(material = v.get('material').reshape(grid,order='F'),
|
|
|
|
|
size = bbox[1] - bbox[0],
|
|
|
|
|
origin = bbox[0],
|
|
|
|
|
comments=comments)
|
|
|
|
|
|
|
|
|
|
|
2019-11-27 01:02:54 +05:30
|
|
|
|
@staticmethod
|
2020-09-15 10:28:06 +05:30
|
|
|
|
def load_ASCII(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
|
|
|
|
|
----------
|
2020-11-08 22:41:30 +05:30
|
|
|
|
fname : str, pathlib.Path, or file handle
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Geometry file to read.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2019-11-25 18:17:14 +05:30
|
|
|
|
try:
|
|
|
|
|
f = open(fname)
|
|
|
|
|
except TypeError:
|
|
|
|
|
f = fname
|
|
|
|
|
|
|
|
|
|
f.seek(0)
|
2020-08-27 03:24:56 +05:30
|
|
|
|
try:
|
|
|
|
|
header_length,keyword = f.readline().split()[:2]
|
|
|
|
|
header_length = int(header_length)
|
|
|
|
|
except ValueError:
|
|
|
|
|
header_length,keyword = (-1, 'invalid')
|
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-08-27 03:24:56 +05:30
|
|
|
|
content = f.readlines()
|
|
|
|
|
|
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']])
|
|
|
|
|
else:
|
|
|
|
|
comments.append(line.strip())
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material = np.empty(grid.prod()) # initialize as flat array
|
2019-11-23 01:22:36 +05:30
|
|
|
|
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))
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material[i:i+len(items)] = items
|
2019-11-23 01:22:36 +05:30
|
|
|
|
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-09-24 02:57:15 +05:30
|
|
|
|
if not np.any(np.mod(material,1) != 0.0): # no float present
|
|
|
|
|
material = material.astype('int')
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material.reshape(grid,order='F'),size,origin,comments)
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def load_DREAM3D(fname,base_group,point_data=None,material='FeatureIds'):
|
|
|
|
|
"""
|
|
|
|
|
Load a DREAM.3D file.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str
|
|
|
|
|
Filename of the DREAM.3D file
|
|
|
|
|
base_group : str
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Name of the group (folder) below 'DataContainers',
|
|
|
|
|
for example 'SyntheticVolumeDataContainer'.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
point_data : str, optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Name of the group (folder) containing the pointwise material data,
|
|
|
|
|
for example 'CellData'. Defaults to None, in which case points are consecutively numbered.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
material : str, optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Name of the dataset containing the material ID.
|
|
|
|
|
Defaults to 'FeatureIds'.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
root_dir ='DataContainers'
|
|
|
|
|
f = h5py.File(fname, 'r')
|
|
|
|
|
g = path.join(root_dir,base_group,'_SIMPL_GEOMETRY')
|
|
|
|
|
grid = f[path.join(g,'DIMENSIONS')][()]
|
2020-11-08 22:41:30 +05:30
|
|
|
|
size = f[path.join(g,'SPACING')][()] * grid
|
2020-10-09 17:49:19 +05:30
|
|
|
|
origin = f[path.join(g,'ORIGIN')][()]
|
|
|
|
|
|
2020-11-08 22:41:30 +05:30
|
|
|
|
ma = np.arange(grid.prod(),dtype=int) \
|
|
|
|
|
if point_data is None else \
|
|
|
|
|
np.reshape(f[path.join(root_dir,base_group,point_data,material)],grid.prod())
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
return Geom(ma.reshape(grid,order='F'),size,origin,util.execution_stamp('Geom','load_DREAM3D'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2020-10-09 22:49:05 +05:30
|
|
|
|
def from_table(table,coordinates,labels):
|
2020-10-09 17:49:19 +05:30
|
|
|
|
"""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Derive geometry from an ASCII table.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-10-09 22:49:05 +05:30
|
|
|
|
table : damask.Table
|
|
|
|
|
Table that contains material information.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
coordinates : str
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Label of the vector column containing the spatial coordinates.
|
2020-10-29 11:47:41 +05:30
|
|
|
|
Need to be ordered (1./x fast, 3./z slow).
|
2020-10-09 17:49:19 +05:30
|
|
|
|
labels : str or list of str
|
|
|
|
|
Label(s) of the columns containing the material definition.
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Each unique combintation of values results in one material ID.
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-10-29 12:12:41 +05:30
|
|
|
|
grid,size,origin = grid_filters.cell_coord0_gridSizeOrigin(table.get(coordinates))
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
|
|
|
|
labels_ = [labels] if isinstance(labels,str) else labels
|
2020-10-28 16:11:34 +05:30
|
|
|
|
unique,unique_inverse = np.unique(np.hstack([table.get(l) for l in labels_]),return_inverse=True,axis=0)
|
2020-10-31 21:53:58 +05:30
|
|
|
|
|
|
|
|
|
ma = np.arange(grid.prod()) if len(unique) == grid.prod() else \
|
|
|
|
|
np.arange(unique.size)[np.argsort(pd.unique(unique_inverse))][unique_inverse]
|
2020-10-09 17:49:19 +05:30
|
|
|
|
|
2020-10-28 16:11:34 +05:30
|
|
|
|
return Geom(ma.reshape(grid,order='F'),size,origin,util.execution_stamp('Geom','from_table'))
|
2020-10-09 17:49:19 +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
|
2020-09-25 00:26:32 +05:30
|
|
|
|
def from_Laguerre_tessellation(grid,size,seeds,weights,material=None,periodic=True):
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
|
|
|
|
Generate geometry from Laguerre tessellation.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-09 00:26:17 +05:30
|
|
|
|
grid : int numpy.ndarray of shape (3)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Number of grid points in x,y,z direction.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
size : list or numpy.ndarray of shape (3)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Physical size of the geometry in meter.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
seeds : numpy.ndarray of shape (:,3)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Position of the seed points in meter. All points need to lay within the box.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
weights : numpy.ndarray of shape (seeds.shape[0])
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Weights of the seeds. Setting all weights to 1.0 gives a standard Voronoi tessellation.
|
2020-09-25 00:26:32 +05:30
|
|
|
|
material : numpy.ndarray of shape (seeds.shape[0]), optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Material ID of the seeds.
|
|
|
|
|
Defaults to None, in which case materials are consecutively numbered.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
periodic : Boolean, optional
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Perform a periodic tessellation. Defaults to True.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
2020-09-18 20:02:08 +05:30
|
|
|
|
pool = mp.Pool(processes = int(environment.options['DAMASK_NUM_THREADS']))
|
2020-03-29 22:42:23 +05:30
|
|
|
|
result = pool.map_async(partial(Geom._find_closest_seed,seeds_p,weights_p), [coord for coord in coords])
|
|
|
|
|
pool.close()
|
|
|
|
|
pool.join()
|
2020-09-25 00:26:32 +05:30
|
|
|
|
material_ = np.array(result.get())
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
if periodic:
|
2020-09-25 00:26:32 +05:30
|
|
|
|
material_ = material_.reshape(grid*3)
|
|
|
|
|
material_ = material_[grid[0]:grid[0]*2,grid[1]:grid[1]*2,grid[2]:grid[2]*2]%seeds.shape[0]
|
2020-03-29 22:42:23 +05:30
|
|
|
|
else:
|
2020-09-25 00:26:32 +05:30
|
|
|
|
material_ = material_.reshape(grid)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2020-10-10 13:27:18 +05:30
|
|
|
|
return Geom(material = material_ if material is None else material[material_],
|
2020-09-24 02:57:15 +05:30
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('Geom','from_Laguerre_tessellation'),
|
2020-08-24 04:16:01 +05:30
|
|
|
|
)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2020-09-25 00:26:32 +05:30
|
|
|
|
def from_Voronoi_tessellation(grid,size,seeds,material=None,periodic=True):
|
2020-03-29 22:42:23 +05:30
|
|
|
|
"""
|
|
|
|
|
Generate geometry from Voronoi tessellation.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-08-09 00:26:17 +05:30
|
|
|
|
grid : int numpy.ndarray of shape (3)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Number of grid points in x,y,z direction.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
size : list or numpy.ndarray of shape (3)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Physical size of the geometry in meter.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
seeds : numpy.ndarray of shape (:,3)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Position of the seed points in meter. All points need to lay within the box.
|
2020-09-25 00:26:32 +05:30
|
|
|
|
material : numpy.ndarray of shape (seeds.shape[0]), optional
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Material ID of the seeds.
|
|
|
|
|
Defaults to None, in which case materials are consecutively numbered.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
periodic : Boolean, optional
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Perform a periodic tessellation. Defaults to True.
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
"""
|
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)
|
2020-09-25 00:26:32 +05:30
|
|
|
|
devNull,material_ = KDTree.query(coords)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
2020-10-10 13:27:18 +05:30
|
|
|
|
return Geom(material = (material_ if material is None else material[material_]).reshape(grid),
|
2020-09-24 02:57:15 +05:30
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('Geom','from_Voronoi_tessellation'),
|
2020-08-24 04:16:01 +05:30
|
|
|
|
)
|
2020-03-29 22:42:23 +05:30
|
|
|
|
|
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
_minimal_surface = \
|
|
|
|
|
{'Schwarz P': lambda x,y,z: np.cos(x) + np.cos(y) + np.cos(z),
|
|
|
|
|
'Double Primitive': lambda x,y,z: ( 0.5 * (np.cos(x)*np.cos(y) + np.cos(y)*np.cos(z) + np.cos(z)*np.cos(x))
|
|
|
|
|
+ 0.2 * (np.cos(2*x) + np.cos(2*y) + np.cos(2*z)) ),
|
|
|
|
|
'Schwarz D': lambda x,y,z: ( np.sin(x)*np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.cos(y)*np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.cos(x)*np.sin(y)*np.cos(z) ),
|
|
|
|
|
'Complementary D': lambda x,y,z: ( np.cos(3*x+y)*np.cos(z) - np.sin(3*x-y)*np.sin(z) + np.cos(x+3*y)*np.cos(z)
|
|
|
|
|
+ np.sin(x-3*y)*np.sin(z) + np.cos(x-y)*np.cos(3*z) - np.sin(x+y)*np.sin(3*z) ),
|
|
|
|
|
'Double Diamond': lambda x,y,z: 0.5 * (np.sin(x)*np.sin(y)
|
|
|
|
|
+ np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(z)*np.sin(x)
|
|
|
|
|
+ np.cos(x) * np.cos(y) * np.cos(z) ),
|
|
|
|
|
'Dprime': lambda x,y,z: 0.5 * ( np.cos(x)*np.cos(y)*np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.sin(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(x)*np.sin(y)*np.cos(z)
|
|
|
|
|
- np.sin(2*x)*np.sin(2*y)
|
|
|
|
|
- np.sin(2*y)*np.sin(2*z)
|
|
|
|
|
- np.sin(2*z)*np.sin(2*x) ) - 0.2,
|
|
|
|
|
'Gyroid': lambda x,y,z: np.cos(x)*np.sin(y) + np.cos(y)*np.sin(z) + np.cos(z)*np.sin(x),
|
|
|
|
|
'Gprime': lambda x,y,z : ( np.sin(2*x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(2*y)*np.cos(z)*np.sin(x)
|
|
|
|
|
+ np.sin(2*z)*np.cos(x)*np.sin(y) ) + 0.32,
|
|
|
|
|
'Karcher K': lambda x,y,z: ( 0.3 * ( np.cos(x) + np.cos(y) + np.cos(z)
|
|
|
|
|
+ np.cos(x)*np.cos(y) + np.cos(y)*np.cos(z) + np.cos(z)*np.cos(x) )
|
|
|
|
|
- 0.4 * ( np.cos(2*x) + np.cos(2*y) + np.cos(2*z) ) ) + 0.2,
|
|
|
|
|
'Lidinoid': lambda x,y,z: 0.5 * ( np.sin(2*x)*np.cos(y)*np.sin(z)
|
|
|
|
|
+ np.sin(2*y)*np.cos(z)*np.sin(x)
|
|
|
|
|
+ np.sin(2*z)*np.cos(x)*np.sin(y)
|
|
|
|
|
- np.cos(2*x)*np.cos(2*y)
|
|
|
|
|
- np.cos(2*y)*np.cos(2*z)
|
|
|
|
|
- np.cos(2*z)*np.cos(2*x) ) + 0.15,
|
|
|
|
|
'Neovius': lambda x,y,z: ( 3 * (np.cos(x)+np.cos(y)+np.cos(z))
|
|
|
|
|
+ 4 * np.cos(x)*np.cos(y)*np.cos(z) ),
|
|
|
|
|
'Fisher-Koch S': lambda x,y,z: ( np.cos(2*x)*np.sin( y)*np.cos( z)
|
|
|
|
|
+ np.cos( x)*np.cos(2*y)*np.sin( z)
|
|
|
|
|
+ np.sin( x)*np.cos( y)*np.cos(2*z) ),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-30 10:40:15 +05:30
|
|
|
|
@staticmethod
|
2020-10-10 13:27:18 +05:30
|
|
|
|
def from_minimal_surface(grid,size,surface,threshold=0.0,periods=1,materials=(0,1)):
|
2020-09-30 10:40:15 +05:30
|
|
|
|
"""
|
2020-10-01 02:57:49 +05:30
|
|
|
|
Generate geometry from definition of triply periodic minimal surface.
|
2020-09-30 10:40:15 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
grid : int 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 geometry in meter.
|
2020-10-01 12:55:32 +05:30
|
|
|
|
surface : str
|
|
|
|
|
Type of the minimal surface. See notes for details.
|
2020-09-30 10:40:15 +05:30
|
|
|
|
threshold : float, optional.
|
|
|
|
|
Threshold of the minimal surface. Defaults to 0.0.
|
|
|
|
|
periods : integer, optional.
|
|
|
|
|
Number of periods per unit cell. Defaults to 1.
|
|
|
|
|
materials : (int, int), optional
|
|
|
|
|
Material IDs. Defaults to (1,2).
|
2020-10-28 14:01:55 +05:30
|
|
|
|
|
2020-10-01 12:55:32 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
The following triply-periodic minimal surfaces are implemented:
|
|
|
|
|
- Schwarz P
|
|
|
|
|
- Double Primitive
|
|
|
|
|
- Schwarz D
|
|
|
|
|
- Complementary D
|
|
|
|
|
- Double Diamond
|
|
|
|
|
- Dprime
|
|
|
|
|
- Gyroid
|
|
|
|
|
- Gprime
|
|
|
|
|
- Karcher K
|
|
|
|
|
- Lidinoid
|
|
|
|
|
- Neovius
|
|
|
|
|
- Fisher-Koch S
|
2020-09-30 10:40:15 +05:30
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
References
|
|
|
|
|
----------
|
|
|
|
|
Sébastien B G Blanquer, Maike Werner, Markus Hannula, Shahriar Sharifi,
|
|
|
|
|
Guillaume P R Lajoinie, David Eglin, Jari Hyttinen, André A Poot, and Dirk W Grijpma
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Surface curvature in triply-periodic minimal surface architectures as
|
|
|
|
|
a distinct design parameter in preparing advanced tissue engineering scaffolds
|
|
|
|
|
https://doi.org/10.1088/1758-5090/aa6553
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
Meinhard Wohlgemuth, Nataliya Yufa, James Hoffman, and Edwin L. Thomas
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Triply Periodic Bicontinuous Cubic Microdomain Morphologies by Symmetries
|
|
|
|
|
https://doi.org/10.1021/ma0019499
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
Meng-Ting Hsieh, Lorenzo Valdevit
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Minisurf – A minimal surface generator for finite element modeling and additive manufacturing
|
|
|
|
|
https://doi.org/10.1016/j.simpa.2020.100026
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
2020-09-30 10:40:15 +05:30
|
|
|
|
"""
|
|
|
|
|
x,y,z = np.meshgrid(periods*2.0*np.pi*(np.arange(grid[0])+0.5)/grid[0],
|
|
|
|
|
periods*2.0*np.pi*(np.arange(grid[1])+0.5)/grid[1],
|
|
|
|
|
periods*2.0*np.pi*(np.arange(grid[2])+0.5)/grid[2],
|
|
|
|
|
indexing='ij',sparse=True)
|
2020-10-01 02:57:49 +05:30
|
|
|
|
return Geom(material = np.where(threshold < Geom._minimal_surface[surface](x,y,z),materials[1],materials[0]),
|
2020-09-30 10:40:15 +05:30
|
|
|
|
size = size,
|
|
|
|
|
comments = util.execution_stamp('Geom','from_minimal_surface'),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2020-10-01 02:57:49 +05:30
|
|
|
|
def save(self,fname,compress=True):
|
|
|
|
|
"""
|
2020-11-08 22:41:30 +05:30
|
|
|
|
Store as VTK rectilinear grid.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-10-10 13:11:11 +05:30
|
|
|
|
fname : str or or pathlib.Path
|
|
|
|
|
Filename to write. Valid extension is .vtr, it will be appended if not given.
|
2020-10-01 02:57:49 +05:30
|
|
|
|
compress : bool, optional
|
|
|
|
|
Compress with zlib algorithm. Defaults to True.
|
|
|
|
|
|
|
|
|
|
"""
|
2020-10-27 18:12:49 +05:30
|
|
|
|
v = VTK.from_rectilinear_grid(self.grid,self.size,self.origin)
|
2020-10-01 02:57:49 +05:30
|
|
|
|
v.add(self.material.flatten(order='F'),'material')
|
|
|
|
|
v.add_comments(self.comments)
|
|
|
|
|
|
|
|
|
|
v.save(fname if str(fname).endswith('.vtr') else str(fname)+'.vtr',parallel=False,compress=compress)
|
|
|
|
|
|
|
|
|
|
|
2020-10-10 13:11:11 +05:30
|
|
|
|
def save_ASCII(self,fname):
|
2019-11-23 01:22:36 +05:30
|
|
|
|
"""
|
2020-10-10 13:11:11 +05:30
|
|
|
|
Write a geom file.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : str or file handle
|
2020-09-15 10:28:06 +05:30
|
|
|
|
Geometry file to write with extension '.geom'.
|
2020-09-18 20:02:08 +05:30
|
|
|
|
compress : bool, optional
|
2020-09-15 10:28:06 +05:30
|
|
|
|
Compress geometry with 'x of y' and 'a to b'.
|
2019-11-23 01:22:36 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-09-23 00:52:58 +05:30
|
|
|
|
header = [f'{len(self.comments)+4} header'] + self.comments \
|
|
|
|
|
+ ['grid a {} b {} c {}'.format(*self.grid),
|
|
|
|
|
'size x {} y {} z {}'.format(*self.size),
|
|
|
|
|
'origin x {} y {} z {}'.format(*self.origin),
|
|
|
|
|
'homogenization 1',
|
|
|
|
|
]
|
2020-09-15 10:28:06 +05:30
|
|
|
|
|
2020-10-10 13:11:11 +05:30
|
|
|
|
format_string = '%g' if self.material.dtype in np.sctypes['float'] else \
|
|
|
|
|
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.material)))))
|
|
|
|
|
np.savetxt(fname,
|
|
|
|
|
self.material.reshape([self.grid[0],np.prod(self.grid[1:])],order='F').T,
|
|
|
|
|
header='\n'.join(header), fmt=format_string, comments='')
|
2020-09-15 10:28:06 +05:30
|
|
|
|
|
|
|
|
|
|
2020-09-18 20:02:08 +05:30
|
|
|
|
def show(self):
|
|
|
|
|
"""Show on screen."""
|
2020-10-27 18:12:49 +05:30
|
|
|
|
v = VTK.from_rectilinear_grid(self.grid,self.size,self.origin)
|
2020-09-18 20:02:08 +05:30
|
|
|
|
v.show()
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
|
2020-08-08 23:44:30 +05:30
|
|
|
|
def add_primitive(self,dimension,center,exponent,
|
|
|
|
|
fill=None,R=Rotation(),inverse=False,periodic=True):
|
|
|
|
|
"""
|
2020-10-10 13:11:11 +05:30
|
|
|
|
Insert a primitive geometric object at a given position.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-11-08 22:41:30 +05:30
|
|
|
|
dimension : int or float numpy.ndarray of shape (3)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
Dimension (diameter/side length) of the primitive. If given as
|
|
|
|
|
integers, grid point locations (cell centers) are addressed.
|
|
|
|
|
If given as floats, coordinates are addressed.
|
2020-11-08 22:41:30 +05:30
|
|
|
|
center : int or float numpy.ndarray of shape (3)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
Center of the primitive. If given as integers, grid point
|
|
|
|
|
locations (cell centers) are addressed.
|
|
|
|
|
If given as floats, coordinates are addressed.
|
2020-11-08 22:41:30 +05:30
|
|
|
|
exponent : numpy.ndarray of shape (3) or float
|
2020-09-26 21:32:25 +05:30
|
|
|
|
Exponents for the three axes.
|
2020-09-23 19:51:20 +05:30
|
|
|
|
0 gives octahedron (ǀxǀ^(2^0) + ǀyǀ^(2^0) + ǀzǀ^(2^0) < 1)
|
2020-09-26 21:32:25 +05:30
|
|
|
|
1 gives sphere (ǀxǀ^(2^1) + ǀyǀ^(2^1) + ǀzǀ^(2^1) < 1)
|
2020-08-09 00:26:17 +05:30
|
|
|
|
fill : int, optional
|
2020-09-24 02:57:15 +05:30
|
|
|
|
Fill value for primitive. Defaults to material.max() + 1.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
R : damask.Rotation, optional
|
|
|
|
|
Rotation of primitive. Defaults to no rotation.
|
|
|
|
|
inverse : Boolean, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Retain original materials within primitive and fill outside.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
Defaults to False.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
periodic : Boolean, optional
|
2020-08-24 04:16:01 +05:30
|
|
|
|
Repeat primitive over boundaries. Defaults to True.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# normalized 'radius' and center
|
|
|
|
|
r = np.array(dimension)/self.grid/2.0 if np.array(dimension).dtype in np.sctypes['int'] else \
|
|
|
|
|
np.array(dimension)/self.size/2.0
|
|
|
|
|
c = (np.array(center) + .5)/self.grid if np.array(center).dtype in np.sctypes['int'] else \
|
|
|
|
|
(np.array(center) - self.origin)/self.size
|
|
|
|
|
|
|
|
|
|
coords = grid_filters.cell_coord0(self.grid,np.ones(3)) \
|
2020-08-24 04:16:01 +05:30
|
|
|
|
- ((np.ones(3)-(1./self.grid if np.array(center).dtype in np.sctypes['int'] else 0))*0.5 if periodic else c) # periodic center is always at CoG
|
2020-08-08 23:44:30 +05:30
|
|
|
|
coords_rot = R.broadcast_to(tuple(self.grid))@coords
|
|
|
|
|
|
2020-08-27 13:02:49 +05:30
|
|
|
|
with np.errstate(all='ignore'):
|
2020-10-08 22:03:40 +05:30
|
|
|
|
mask = np.sum(np.power(coords_rot/r,2.0**np.array(exponent)),axis=-1) > 1.0
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
if periodic: # translate back to center
|
|
|
|
|
mask = np.roll(mask,((c-np.ones(3)*.5)*self.grid).astype(int),(0,1,2))
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
fill_ = np.full_like(self.material,np.nanmax(self.material)+1 if fill is None else fill)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = np.where(np.logical_not(mask) if inverse else mask, self.material,fill_),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','add_primitive')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2020-08-08 23:44:30 +05:30
|
|
|
|
|
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
def mirror(self,directions,reflect=False):
|
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Mirror geometry along given directions.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
directions : iterable containing str
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Direction(s) along which the geometry is mirrored.
|
2020-08-08 23:44:30 +05:30
|
|
|
|
Valid entries are 'x', 'y', 'z'.
|
2019-11-23 02:18:41 +05:30
|
|
|
|
reflect : bool, optional
|
2020-08-25 12:04:04 +05:30
|
|
|
|
Reflect (include) outermost layers. Defaults to False.
|
2019-11-24 13:22:46 +05:30
|
|
|
|
|
2019-11-23 02:18:41 +05:30
|
|
|
|
"""
|
2020-08-25 12:04:04 +05:30
|
|
|
|
valid = ['x','y','z']
|
2020-08-23 14:35:56 +05:30
|
|
|
|
if 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]
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = self.material.copy()
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
if 'x' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[limits[0]:limits[1]:-1,:,:]],0)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if 'y' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[:,limits[0]:limits[1]:-1,:]],1)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
if 'z' in directions:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.concatenate([mat,mat[:,:,limits[0]:limits[1]:-1]],2)
|
2020-03-21 15:37:21 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = mat,
|
|
|
|
|
size = self.size/self.grid*np.asarray(mat.shape),
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','mirror')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flip(self,directions):
|
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Flip geometry along given directions.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
directions : iterable containing str
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Direction(s) along which the geometry is flipped.
|
2020-08-24 04:16:01 +05:30
|
|
|
|
Valid entries are 'x', 'y', 'z'.
|
|
|
|
|
|
|
|
|
|
"""
|
2020-08-25 12:04:04 +05:30
|
|
|
|
valid = ['x','y','z']
|
2020-08-25 02:53:47 +05:30
|
|
|
|
if not set(directions).issubset(valid):
|
2020-08-24 04:16:01 +05:30
|
|
|
|
raise ValueError(f'Invalid direction {set(directions).difference(valid)} specified.')
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mat = np.flip(self.material, (valid.index(d) for d in directions if d in valid))
|
2020-08-24 04:16:01 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = mat,
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','flip')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
|
|
|
|
|
2020-08-23 13:32:22 +05:30
|
|
|
|
def scale(self,grid,periodic=True):
|
2019-11-24 19:43:26 +05:30
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Scale geometry 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)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Number of grid points in x,y,z direction.
|
2020-08-23 13:32:22 +05:30
|
|
|
|
periodic : Boolean, optional
|
|
|
|
|
Assume geometry to be periodic. Defaults to True.
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = ndimage.interpolation.zoom(
|
|
|
|
|
self.material,
|
2020-09-23 00:17:08 +05:30
|
|
|
|
grid/self.grid,
|
2020-09-24 02:57:15 +05:30
|
|
|
|
output=self.material.dtype,
|
2020-09-23 00:17:08 +05:30
|
|
|
|
order=0,
|
|
|
|
|
mode=('wrap' if periodic else 'nearest'),
|
|
|
|
|
prefilter=False
|
|
|
|
|
),
|
2020-09-24 02:57:15 +05:30
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','scale')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2019-11-24 19:43:26 +05:30
|
|
|
|
|
|
|
|
|
|
2020-08-23 13:32:22 +05:30
|
|
|
|
def clean(self,stencil=3,selection=None,periodic=True):
|
2019-11-23 02:18:41 +05:30
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Smooth geometry by selecting most frequent material 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
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Size of smoothing stencil.
|
2020-08-23 07:03:38 +05:30
|
|
|
|
selection : list, optional
|
|
|
|
|
Field values that can be altered. Defaults to all.
|
2020-08-23 13:32:22 +05:30
|
|
|
|
periodic : Boolean, optional
|
|
|
|
|
Assume geometry to be periodic. Defaults to True.
|
2020-08-23 07:03:38 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def mostFrequent(arr,selection=None):
|
|
|
|
|
me = arr[arr.size//2]
|
|
|
|
|
if selection is None or me in selection:
|
|
|
|
|
unique, inverse = np.unique(arr, return_inverse=True)
|
|
|
|
|
return unique[np.argmax(np.bincount(inverse))]
|
|
|
|
|
else:
|
|
|
|
|
return me
|
2019-11-23 02:18:41 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = ndimage.filters.generic_filter(
|
|
|
|
|
self.material,
|
2020-09-23 00:17:08 +05:30
|
|
|
|
mostFrequent,
|
|
|
|
|
size=(stencil if selection is None else stencil//2*2+1,)*3,
|
|
|
|
|
mode=('wrap' if periodic else 'nearest'),
|
|
|
|
|
extra_keywords=dict(selection=selection),
|
2020-09-24 02:57:15 +05:30
|
|
|
|
).astype(self.material.dtype),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','clean')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2019-11-24 22:51:05 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def renumber(self):
|
2020-11-08 22:41:30 +05:30
|
|
|
|
"""Renumber sorted material indices as 0,...,N-1."""
|
2020-10-28 14:13:20 +05:30
|
|
|
|
_,renumbered = np.unique(self.material,return_inverse=True)
|
2020-09-24 02:57:15 +05:30
|
|
|
|
|
2020-10-28 14:13:20 +05:30
|
|
|
|
return Geom(material = renumbered.reshape(self.grid),
|
2020-09-24 02:57:15 +05:30
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','renumber')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rotate(self,R,fill=None):
|
2020-05-30 21:01:50 +05:30
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Rotate geometry (pad if required).
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
R : damask.Rotation
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Rotation to apply to the geometry.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
fill : int or float, optional
|
2020-09-24 02:57:15 +05:30
|
|
|
|
Material index to fill the corners. Defaults to material.max() + 1.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-09-24 02:57:15 +05:30
|
|
|
|
if fill is None: fill = np.nanmax(self.material) + 1
|
|
|
|
|
dtype = float if np.isnan(fill) or int(fill) != fill or self.material.dtype==np.float else int
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
|
|
Eulers = R.as_Eulers(degrees=True)
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material_in = self.material.copy()
|
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)]):
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material_out = ndimage.rotate(material_in,angle,axes,order=0,
|
2020-05-24 22:00:45 +05:30
|
|
|
|
prefilter=False,output=dtype,cval=fill)
|
2020-09-24 02:57:15 +05:30
|
|
|
|
if np.prod(material_in.shape) == np.prod(material_out.shape):
|
2020-05-25 00:22:19 +05:30
|
|
|
|
# avoid scipy interpolation errors for rotations close to multiples of 90°
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material_in = np.rot90(material_in,k=np.rint(angle/90.).astype(int),axes=axes)
|
2020-05-25 00:22:19 +05:30
|
|
|
|
else:
|
2020-09-24 02:57:15 +05:30
|
|
|
|
material_in = material_out
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
origin = self.origin-(np.asarray(material_in.shape)-self.grid)*.5 * self.size/self.grid
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = material_in,
|
|
|
|
|
size = self.size/self.grid*np.asarray(material_in.shape),
|
|
|
|
|
origin = origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','rotate')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
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
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Crop or enlarge/pad geometry.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
grid : numpy.ndarray of shape (3)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Number of grid points in x,y,z direction.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
offset : numpy.ndarray of shape (3)
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Offset (measured in grid points) from old to new geometry [0,0,0].
|
2020-05-30 21:01:50 +05:30
|
|
|
|
fill : int or float, optional
|
2020-09-24 02:57:15 +05:30
|
|
|
|
Material index to fill the background. Defaults to material.max() + 1.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-05-25 02:22:00 +05:30
|
|
|
|
if offset is None: offset = 0
|
2020-09-24 02:57:15 +05:30
|
|
|
|
if fill is None: fill = np.nanmax(self.material) + 1
|
|
|
|
|
dtype = float if int(fill) != fill or self.material.dtype in np.sctypes['float'] else int
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-08-25 12:16:08 +05:30
|
|
|
|
canvas = np.full(self.grid if grid is None else grid,fill,dtype)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-08-23 12:47:08 +05:30
|
|
|
|
LL = np.clip( offset, 0,np.minimum(self.grid, grid+offset))
|
2020-08-23 07:03:38 +05:30
|
|
|
|
UR = np.clip( offset+grid, 0,np.minimum(self.grid, grid+offset))
|
|
|
|
|
ll = np.clip(-offset, 0,np.minimum( grid,self.grid-offset))
|
|
|
|
|
ur = np.clip(-offset+self.grid,0,np.minimum( grid,self.grid-offset))
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
canvas[ll[0]:ur[0],ll[1]:ur[1],ll[2]:ur[2]] = self.material[LL[0]:UR[0],LL[1]:UR[1],LL[2]:UR[2]]
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = canvas,
|
|
|
|
|
size = self.size/self.grid*np.asarray(canvas.shape),
|
|
|
|
|
origin = self.origin+offset*self.size/self.grid,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','canvas')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2020-05-24 12:36:42 +05:30
|
|
|
|
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
def substitute(self,from_material,to_material):
|
2020-05-30 21:01:50 +05:30
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Substitute material indices.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2020-09-24 02:57:15 +05:30
|
|
|
|
from_material : iterable of ints
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Material indices to be substituted.
|
2020-09-24 02:57:15 +05:30
|
|
|
|
to_material : iterable of ints
|
2020-09-23 00:17:08 +05:30
|
|
|
|
New material indices.
|
2020-05-30 21:01:50 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2020-10-29 02:21:20 +05:30
|
|
|
|
def mp(entry,mapper):
|
2020-10-28 19:09:41 +05:30
|
|
|
|
return mapper[entry] if entry in mapper else entry
|
2020-11-03 04:50:52 +05:30
|
|
|
|
|
2020-10-28 19:09:41 +05:30
|
|
|
|
mp = np.vectorize(mp)
|
2020-10-29 02:21:20 +05:30
|
|
|
|
mapper = dict(zip(from_material,to_material))
|
2020-10-28 19:09:41 +05:30
|
|
|
|
|
2020-10-29 02:21:20 +05:30
|
|
|
|
return Geom(material = mp(self.material,mapper).reshape(self.grid),
|
2020-09-24 02:57:15 +05:30
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','substitute')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
|
|
|
|
|
2020-11-01 01:16:21 +05:30
|
|
|
|
def sort(self):
|
|
|
|
|
"""Sort material indices such that min(material) is located at (0,0,0)."""
|
|
|
|
|
a = self.material.flatten(order='F')
|
|
|
|
|
from_ma = pd.unique(a)
|
|
|
|
|
sort_idx = np.argsort(from_ma)
|
|
|
|
|
ma = np.unique(a)[sort_idx][np.searchsorted(from_ma,a,sorter = sort_idx)]
|
|
|
|
|
|
|
|
|
|
return Geom(material = ma.reshape(self.grid,order='F'),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','sort')],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2020-08-08 23:12:34 +05:30
|
|
|
|
def vicinity_offset(self,vicinity=1,offset=None,trigger=[],periodic=True):
|
|
|
|
|
"""
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Offset material index of points in the vicinity of xxx.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
|
|
|
|
Different from themselves (or listed as triggers) within a given (cubic) vicinity,
|
|
|
|
|
i.e. within the region close to a grain/phase boundary.
|
|
|
|
|
ToDo: use include/exclude as in seeds.from_geom
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
vicinity : int, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Voxel distance checked for presence of other materials.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
Defaults to 1.
|
|
|
|
|
offset : int, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
Offset (positive or negative) to tag material indices,
|
2020-11-08 22:41:30 +05:30
|
|
|
|
defaults to material.max()+1.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
trigger : list of ints, optional
|
2020-09-23 00:17:08 +05:30
|
|
|
|
List of material indices that trigger a change.
|
|
|
|
|
Defaults to [], meaning that any different neighbor triggers a change.
|
2020-08-08 23:12:34 +05:30
|
|
|
|
periodic : Boolean, optional
|
|
|
|
|
Assume geometry to be periodic. Defaults to True.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def tainted_neighborhood(stencil,trigger):
|
|
|
|
|
|
|
|
|
|
me = stencil[stencil.shape[0]//2]
|
2020-11-03 04:50:52 +05:30
|
|
|
|
return np.any(stencil != me
|
|
|
|
|
if len(trigger) == 0 else
|
|
|
|
|
np.in1d(stencil,np.array(list(set(trigger) - {me}))))
|
2020-08-08 23:12:34 +05:30
|
|
|
|
|
2020-11-04 04:13:57 +05:30
|
|
|
|
offset_ = np.nanmax(self.material)+1 if offset is None else offset
|
2020-09-24 02:57:15 +05:30
|
|
|
|
mask = ndimage.filters.generic_filter(self.material,
|
2020-08-08 23:12:34 +05:30
|
|
|
|
tainted_neighborhood,
|
|
|
|
|
size=1+2*vicinity,
|
2020-08-24 04:16:01 +05:30
|
|
|
|
mode='wrap' if periodic else 'nearest',
|
2020-08-08 23:12:34 +05:30
|
|
|
|
extra_keywords={'trigger':trigger})
|
|
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
|
return Geom(material = np.where(mask, self.material + offset_,self.material),
|
|
|
|
|
size = self.size,
|
|
|
|
|
origin = self.origin,
|
|
|
|
|
comments = self.comments+[util.execution_stamp('Geom','vicinity_offset')],
|
2020-09-23 00:17:08 +05:30
|
|
|
|
)
|