2012-12-07 03:19:49 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
import os,sys,string,itertools
|
2014-07-22 19:51:49 +05:30
|
|
|
import numpy as np
|
2012-12-07 03:19:49 +05:30
|
|
|
from scipy import ndimage
|
2015-02-07 22:41:46 +05:30
|
|
|
from optparse import OptionParser
|
2014-07-22 19:51:49 +05:30
|
|
|
import damask
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
def periodic_3Dpad(array, rimdim=(1,1,1)):
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
rimdim = np.array(rimdim,'i')
|
|
|
|
size = np.array(array.shape,'i')
|
|
|
|
padded = np.empty(size+2*rimdim,array.dtype)
|
2012-12-07 03:19:49 +05:30
|
|
|
padded[rimdim[0]:rimdim[0]+size[0],
|
|
|
|
rimdim[1]:rimdim[1]+size[1],
|
|
|
|
rimdim[2]:rimdim[2]+size[2]] = array
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
p = np.zeros(3,'i')
|
2012-12-07 03:19:49 +05:30
|
|
|
for side in xrange(3):
|
|
|
|
for p[(side+2)%3] in xrange(padded.shape[(side+2)%3]):
|
|
|
|
for p[(side+1)%3] in xrange(padded.shape[(side+1)%3]):
|
|
|
|
for p[side%3] in xrange(rimdim[side%3]):
|
|
|
|
spot = (p-rimdim)%size
|
|
|
|
padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]]
|
|
|
|
for p[side%3] in xrange(rimdim[side%3]+size[side%3],size[side%3]+2*rimdim[side%3]):
|
|
|
|
spot = (p-rimdim)%size
|
|
|
|
padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]]
|
|
|
|
return padded
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2015-02-07 22:41:46 +05:30
|
|
|
features = [
|
|
|
|
{'aliens': 1, 'names': ['boundary','biplane'],},
|
|
|
|
{'aliens': 2, 'names': ['tripleline',],},
|
|
|
|
{'aliens': 3, 'names': ['quadruplepoint',],}
|
2012-12-07 03:19:49 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
neighborhoods = {
|
2014-07-22 19:51:49 +05:30
|
|
|
'neumann':np.array([
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1, 0, 0],
|
|
|
|
[ 1, 0, 0],
|
|
|
|
[ 0,-1, 0],
|
|
|
|
[ 0, 1, 0],
|
|
|
|
[ 0, 0,-1],
|
|
|
|
[ 0, 0, 1],
|
|
|
|
]),
|
2014-07-22 19:51:49 +05:30
|
|
|
'moore':np.array([
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1,-1,-1],
|
|
|
|
[ 0,-1,-1],
|
|
|
|
[ 1,-1,-1],
|
|
|
|
[-1, 0,-1],
|
|
|
|
[ 0, 0,-1],
|
|
|
|
[ 1, 0,-1],
|
|
|
|
[-1, 1,-1],
|
|
|
|
[ 0, 1,-1],
|
|
|
|
[ 1, 1,-1],
|
2015-02-07 22:41:46 +05:30
|
|
|
#
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1,-1, 0],
|
|
|
|
[ 0,-1, 0],
|
|
|
|
[ 1,-1, 0],
|
|
|
|
[-1, 0, 0],
|
|
|
|
#
|
|
|
|
[ 1, 0, 0],
|
|
|
|
[-1, 1, 0],
|
|
|
|
[ 0, 1, 0],
|
|
|
|
[ 1, 1, 0],
|
2015-02-07 22:41:46 +05:30
|
|
|
#
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1,-1, 1],
|
|
|
|
[ 0,-1, 1],
|
|
|
|
[ 1,-1, 1],
|
|
|
|
[-1, 0, 1],
|
|
|
|
[ 0, 0, 1],
|
|
|
|
[ 1, 0, 1],
|
|
|
|
[-1, 1, 1],
|
|
|
|
[ 0, 1, 1],
|
|
|
|
[ 1, 1, 1],
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-08-06 18:57:09 +05:30
|
|
|
Add column(s) containing Euclidean distance to grain structural features: boundaries, triple lines, and quadruple points.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-c','--coordinates', dest='coords', metavar='string',
|
2015-05-09 18:31:31 +05:30
|
|
|
help='column heading for coordinates [%default]')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-i','--identifier', dest='id', metavar = 'string',
|
2015-05-09 18:31:31 +05:30
|
|
|
help='heading of column containing grain identifier [%default]')
|
|
|
|
parser.add_option('-t','--type', dest = 'type', action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'feature type {%s} '%(', '.join(map(lambda x:'/'.join(x['names']),features))) )
|
2015-03-11 12:52:11 +05:30
|
|
|
parser.add_option('-n','--neighborhood',dest='neighborhood', choices = neighborhoods.keys(), metavar = 'string',
|
2015-05-09 18:31:31 +05:30
|
|
|
help = 'type of neighborhood [neumann] {%s}'%(', '.join(neighborhoods.keys())))
|
2015-03-11 12:52:11 +05:30
|
|
|
parser.add_option('-s', '--scale', dest = 'scale', type = 'float', metavar='float',
|
2015-05-09 18:31:31 +05:30
|
|
|
help = 'voxel size [%default]')
|
2015-03-11 12:52:11 +05:30
|
|
|
parser.set_defaults(coords = 'ipinitialcoord')
|
2012-12-07 03:19:49 +05:30
|
|
|
parser.set_defaults(id = 'texture')
|
|
|
|
parser.set_defaults(neighborhood = 'neumann')
|
2015-02-07 22:41:46 +05:30
|
|
|
parser.set_defaults(scale = 1.0)
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-05-09 18:31:31 +05:30
|
|
|
if options.type == None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no feature type selected.')
|
2015-02-09 14:27:14 +05:30
|
|
|
if not set(options.type).issubset(set(list(itertools.chain(*map(lambda x: x['names'],features))))):
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('type must be chosen from (%s).'%(', '.join(map(lambda x:'|'.join(x['names']),features))) )
|
2014-07-25 00:17:09 +05:30
|
|
|
if 'biplane' in options.type and 'boundary' in options.type:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error("only one from aliases 'biplane' and 'boundary' possible.")
|
2014-07-25 00:17:09 +05:30
|
|
|
|
2012-12-07 03:19:49 +05:30
|
|
|
feature_list = []
|
|
|
|
for i,feature in enumerate(features):
|
2015-02-07 22:41:46 +05:30
|
|
|
for name in feature['names']:
|
|
|
|
for myType in options.type:
|
|
|
|
if name.startswith(myType):
|
|
|
|
feature_list.append(i) # remember valid features
|
|
|
|
break
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-18 22:54:15 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
for name in filenames:
|
2015-08-18 22:54:15 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name, buffered = False)
|
|
|
|
except:
|
|
|
|
continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
2015-05-10 16:59:11 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
column = {}
|
2015-05-10 16:59:11 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
|
|
|
|
else: coordCol = table.label_index(options.coords)
|
|
|
|
|
|
|
|
if table.label_dimension(options.id) != 1: errors.append('grain identifier {} not found.'.format(options.id))
|
|
|
|
else: idCol = table.label_index(options.id)
|
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2012-12-07 03:19:49 +05:30
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-05-10 16:59:11 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2012-12-07 03:19:49 +05:30
|
|
|
for feature in feature_list:
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_append('ED_{}({})'.format(features[feature]['names'][0],options.id)) # extend ASCII header with new labels
|
2012-12-07 03:19:49 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
|
|
|
|
|
|
|
table.data_readArray()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
coords = [{},{},{}]
|
2015-08-08 00:33:26 +05:30
|
|
|
for i in xrange(len(table.data)):
|
2012-12-07 03:19:49 +05:30
|
|
|
for j in xrange(3):
|
2015-08-08 00:33:26 +05:30
|
|
|
coords[j][str(table.data[i,coordCol+j])] = True
|
2014-08-06 20:55:18 +05:30
|
|
|
grid = np.array(map(len,coords),'i')
|
2015-08-08 00:33:26 +05:30
|
|
|
size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
|
|
|
|
np.array([max(map(float,coords[0].keys()))-min(map(float,coords[0].keys())),\
|
|
|
|
max(map(float,coords[1].keys()))-min(map(float,coords[1].keys())),\
|
|
|
|
max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
|
|
|
|
],'d') # size from bounding box, corrected for cell-centeredness
|
|
|
|
|
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other spacings
|
2015-05-10 16:59:11 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process value field -----------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
stack = [table.data]
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
neighborhood = neighborhoods[options.neighborhood]
|
2014-08-06 20:55:18 +05:30
|
|
|
convoluted = np.empty([len(neighborhood)]+list(grid+2),'i')
|
2015-08-08 00:33:26 +05:30
|
|
|
microstructure = periodic_3Dpad(np.array(table.data[:,idCol].reshape(grid),'i'))
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2012-12-07 03:19:49 +05:30
|
|
|
for i,p in enumerate(neighborhood):
|
2014-07-22 19:51:49 +05:30
|
|
|
stencil = np.zeros((3,3,3),'i')
|
2012-12-07 03:19:49 +05:30
|
|
|
stencil[1,1,1] = -1
|
|
|
|
stencil[p[0]+1,
|
|
|
|
p[1]+1,
|
|
|
|
p[2]+1] = 1
|
|
|
|
convoluted[i,:,:,:] = ndimage.convolve(microstructure,stencil)
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2015-02-07 23:43:51 +05:30
|
|
|
distance = np.ones((len(feature_list),grid[0],grid[1],grid[2]),'d')
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2015-02-07 22:41:46 +05:30
|
|
|
convoluted = np.sort(convoluted,axis = 0)
|
2015-08-08 00:33:26 +05:30
|
|
|
uniques = np.where(convoluted[0,1:-1,1:-1,1:-1] != 0, 1,0) # initialize unique value counter (exclude myself [= 0])
|
2015-02-07 22:41:46 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for i in xrange(1,len(neighborhood)): # check remaining points in neighborhood
|
2015-02-07 22:41:46 +05:30
|
|
|
uniques += np.where(np.logical_and(
|
2015-08-08 00:33:26 +05:30
|
|
|
convoluted[i,1:-1,1:-1,1:-1] != convoluted[i-1,1:-1,1:-1,1:-1], # flip of ID difference detected?
|
|
|
|
convoluted[i,1:-1,1:-1,1:-1] != 0), # not myself?
|
|
|
|
1,0) # count flip
|
2015-02-07 22:41:46 +05:30
|
|
|
|
2012-12-07 16:20:34 +05:30
|
|
|
for i,feature_id in enumerate(feature_list):
|
2015-08-08 00:33:26 +05:30
|
|
|
distance[i,:,:,:] = np.where(uniques >= features[feature_id]['aliens'],0.0,1.0) # seed with 0.0 when enough unique neighbor IDs are present
|
2015-02-07 22:41:46 +05:30
|
|
|
distance[i,:,:,:] = ndimage.morphology.distance_transform_edt(distance[i,:,:,:])*[options.scale]*3
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-31 16:30:45 +05:30
|
|
|
distance.shape = ([len(feature_list),grid.prod(),1])
|
2015-05-10 16:59:11 +05:30
|
|
|
for i in xrange(len(feature_list)):
|
2015-08-08 00:33:26 +05:30
|
|
|
stack.append(distance[i,:])
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
if len(stack) > 1: table.data = np.hstack(tuple(stack))
|
2015-05-10 16:59:11 +05:30
|
|
|
table.data_writeArray('%.12g')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close input ASCII table (works for stdin)
|