2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,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
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
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')
|
2016-10-25 00:46:29 +05:30
|
|
|
for side in range(3):
|
|
|
|
for p[(side+2)%3] in range(padded.shape[(side+2)%3]):
|
|
|
|
for p[(side+1)%3] in range(padded.shape[(side+1)%3]):
|
|
|
|
for p[side%3] in range(rimdim[side%3]):
|
2012-12-07 03:19:49 +05:30
|
|
|
spot = (p-rimdim)%size
|
|
|
|
padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]]
|
2016-10-25 00:46:29 +05:30
|
|
|
for p[side%3] in range(rimdim[side%3]+size[side%3],size[side%3]+2*rimdim[side%3]):
|
2012-12-07 03:19:49 +05:30
|
|
|
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],
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2019-02-16 22:11:56 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(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
|
|
|
|
2016-04-24 23:42:18 +05:30
|
|
|
parser.add_option('-p',
|
|
|
|
'--pos', '--position',
|
2016-04-27 02:19:58 +05:30
|
|
|
dest = 'pos', metavar = 'string',
|
2016-04-24 23:42:18 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
|
|
|
parser.add_option('-i',
|
|
|
|
'--id', '--identifier',
|
|
|
|
dest = 'id', metavar = 'string',
|
|
|
|
help='label of grain identifier [%default]')
|
|
|
|
parser.add_option('-t',
|
|
|
|
'--type',
|
|
|
|
dest = 'type', action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'feature type {{{}}} '.format(', '.join(map(lambda x:'/'.join(x['names']),features))) )
|
|
|
|
parser.add_option('-n',
|
|
|
|
'--neighborhood',
|
2018-07-19 20:23:48 +05:30
|
|
|
dest = 'neighborhood', choices = list(neighborhoods.keys()), metavar = 'string',
|
2016-04-24 23:42:18 +05:30
|
|
|
help = 'neighborhood type [neumann] {{{}}}'.format(', '.join(neighborhoods.keys())))
|
|
|
|
parser.add_option('-s',
|
|
|
|
'--scale',
|
|
|
|
dest = 'scale', type = 'float', metavar = 'float',
|
2015-05-09 18:31:31 +05:30
|
|
|
help = 'voxel size [%default]')
|
2016-04-24 23:42:18 +05:30
|
|
|
|
2016-04-27 02:19:58 +05:30
|
|
|
parser.set_defaults(pos = 'pos',
|
2016-04-16 03:55:56 +05:30
|
|
|
id = 'texture',
|
|
|
|
neighborhood = 'neumann',
|
|
|
|
scale = 1.0,
|
|
|
|
)
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
if options.type is 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:
|
2016-04-24 23:42:18 +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:
|
2016-04-16 03:55:56 +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 = []
|
2015-05-10 16:59:11 +05:30
|
|
|
|
2018-07-19 19:56:30 +05:30
|
|
|
if not 3 >= table.label_dimension(options.pos) >= 1:
|
2016-04-27 02:19:58 +05:30
|
|
|
errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
|
2016-04-16 03:55:56 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if table.label_dimension(options.id) != 1: errors.append('grain identifier {} not found.'.format(options.id))
|
|
|
|
else: idCol = table.label_index(options.id)
|
|
|
|
|
2016-04-28 22:54:08 +05:30
|
|
|
if remarks != []:
|
|
|
|
damask.util.croak(remarks)
|
|
|
|
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()
|
|
|
|
|
2018-07-19 19:56:30 +05:30
|
|
|
grid,size = damask.util.coordGridAndSize(table.data[:,table.label_indexrange(options.pos)])
|
2016-04-16 03:55:56 +05:30
|
|
|
N = grid.prod()
|
|
|
|
|
2016-04-24 23:42:18 +05:30
|
|
|
if N != len(table.data): errors.append('data count {} does not match grid {}.'.format(N,'x'.join(map(str,grid))))
|
2016-04-28 22:54:08 +05:30
|
|
|
else: remarks.append('grid: {}x{}x{}'.format(*grid))
|
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2016-04-16 03:55:56 +05:30
|
|
|
if errors != []:
|
|
|
|
damask.util.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
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]
|
2016-04-28 22:54:08 +05:30
|
|
|
diffToNeighbor = np.empty(list(grid+2)+[len(neighborhood)],'i')
|
|
|
|
microstructure = periodic_3Dpad(table.data[:,idCol].astype('i').reshape(grid,order='F'))
|
|
|
|
|
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
|
2016-04-28 22:54:08 +05:30
|
|
|
diffToNeighbor[:,:,:,i] = ndimage.convolve(microstructure,stencil) # compare ID at each point...
|
|
|
|
# ...to every one in the specified neighborhood
|
|
|
|
# for same IDs at both locations ==> 0
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2016-04-28 22:54:08 +05:30
|
|
|
diffToNeighbor = np.sort(diffToNeighbor) # sort diff such that number of changes in diff (steps)...
|
|
|
|
# ...reflects number of unique neighbors
|
|
|
|
uniques = np.where(diffToNeighbor[1:-1,1:-1,1:-1,0] != 0, 1,0) # initialize unique value counter (exclude myself [= 0])
|
2015-02-07 22:41:46 +05:30
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(1,len(neighborhood)): # check remaining points in neighborhood
|
2015-02-07 22:41:46 +05:30
|
|
|
uniques += np.where(np.logical_and(
|
2016-04-28 22:54:08 +05:30
|
|
|
diffToNeighbor[1:-1,1:-1,1:-1,i] != 0, # not myself?
|
|
|
|
diffToNeighbor[1:-1,1:-1,1:-1,i] != diffToNeighbor[1:-1,1:-1,1:-1,i-1],
|
|
|
|
), # flip of ID difference detected?
|
|
|
|
1,0) # count that flip
|
|
|
|
|
|
|
|
distance = np.ones((len(feature_list),grid[0],grid[1],grid[2]),'d')
|
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
|
|
|
|
2016-04-28 22:54:08 +05:30
|
|
|
distance = distance.reshape([len(feature_list),grid.prod(),1],order='F')
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(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 -----------------------------------
|
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
table.close() # close input ASCII table (works for stdin)
|