2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2016-05-11 14:31:39 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,time,copy
|
2015-07-24 19:00:33 +05:30
|
|
|
import numpy as np
|
|
|
|
import damask
|
2015-09-24 14:54:42 +05:30
|
|
|
from optparse import OptionParser
|
2015-07-24 19:00:33 +05:30
|
|
|
from scipy import spatial
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-07-24 19:00:33 +05:30
|
|
|
|
|
|
|
|
2016-05-17 14:35:50 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2015-07-24 19:00:33 +05:30
|
|
|
Add grain index based on similiarity of crystal lattice orientation.
|
2015-09-24 14:54:42 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-04-23 00:50:36 +05:30
|
|
|
parser.add_option('-r',
|
|
|
|
'--radius',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'radius',
|
|
|
|
type = 'float', metavar = 'float',
|
2015-07-24 19:00:33 +05:30
|
|
|
help = 'search radius')
|
2016-04-23 00:50:36 +05:30
|
|
|
parser.add_option('-d',
|
|
|
|
'--disorientation',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'disorientation',
|
|
|
|
type = 'float', metavar = 'float',
|
2016-04-14 05:03:46 +05:30
|
|
|
help = 'disorientation threshold in degrees [%default]')
|
2016-04-23 00:50:36 +05:30
|
|
|
parser.add_option('-s',
|
|
|
|
'--symmetry',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'symmetry',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-07-24 19:00:33 +05:30
|
|
|
help = 'crystal symmetry [%default]')
|
2016-04-23 00:50:36 +05:30
|
|
|
parser.add_option('-e',
|
|
|
|
'--eulers',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'eulers',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of Euler angles')
|
|
|
|
parser.add_option('--degrees',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
2015-07-24 19:00:33 +05:30
|
|
|
help = 'Euler angles are given in degrees [%default]')
|
2016-04-23 00:50:36 +05:30
|
|
|
parser.add_option('-m',
|
|
|
|
'--matrix',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'matrix',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of orientation matrix')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-a',
|
|
|
|
dest = 'a',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of crystal frame a vector')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-b',
|
|
|
|
dest = 'b',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of crystal frame b vector')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-c',
|
|
|
|
dest = 'c',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of crystal frame c vector')
|
|
|
|
parser.add_option('-q',
|
|
|
|
'--quaternion',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'quaternion',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of quaternion')
|
|
|
|
parser.add_option('-p',
|
|
|
|
'--pos', '--position',
|
|
|
|
dest = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-04-14 05:03:46 +05:30
|
|
|
parser.set_defaults(disorientation = 5,
|
|
|
|
symmetry = 'cubic',
|
2016-04-23 00:50:36 +05:30
|
|
|
pos = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
degrees = False,
|
|
|
|
)
|
2015-07-24 19:00:33 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
if options.radius is None:
|
2015-07-24 19:00:33 +05:30
|
|
|
parser.error('no radius specified.')
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
input = [options.eulers is not None,
|
|
|
|
options.a is not None and \
|
|
|
|
options.b is not None and \
|
|
|
|
options.c is not None,
|
|
|
|
options.matrix is not None,
|
|
|
|
options.quaternion is not None,
|
2015-08-08 00:33:26 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
if np.sum(input) != 1: parser.error('needs exactly one input format.')
|
|
|
|
|
|
|
|
(label,dim,inputtype) = [(options.eulers,3,'eulers'),
|
|
|
|
([options.a,options.b,options.c],[3,3,3],'frame'),
|
|
|
|
(options.matrix,9,'matrix'),
|
|
|
|
(options.quaternion,4,'quaternion'),
|
|
|
|
][np.where(input)[0][0]] # select input label that was requested
|
2016-04-14 05:03:46 +05:30
|
|
|
toRadians = np.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
|
|
|
cos_disorientation = np.cos(np.radians(options.disorientation/2.)) # cos of half the disorientation angle
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-04-14 05:03:46 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header -------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks -----------------------------------------
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
|
2016-04-23 00:50:36 +05:30
|
|
|
if not 3 >= table.label_dimension(options.pos) >= 1:
|
|
|
|
errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
|
2016-04-14 05:03:46 +05:30
|
|
|
if not np.all(table.label_dimension(label) == dim):
|
2016-04-23 00:50:36 +05:30
|
|
|
errors.append('input "{}" does not have dimension {}.'.format(label,dim))
|
2015-08-08 00:33:26 +05:30
|
|
|
else: column = table.label_index(label)
|
|
|
|
|
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)
|
|
|
|
continue
|
2015-07-24 19:00:33 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2016-04-14 05:03:46 +05:30
|
|
|
table.labels_append('grainID_{}@{:g}'.format('+'.join(label)
|
|
|
|
if isinstance(label, (list,tuple))
|
|
|
|
else label,
|
|
|
|
options.disorientation)) # report orientation source and disorientation
|
2015-07-24 19:00:33 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ build KD tree -----------------------------------------
|
2015-07-24 19:00:33 +05:30
|
|
|
|
|
|
|
# --- start background messaging
|
|
|
|
|
2015-08-24 04:49:40 +05:30
|
|
|
bg = damask.util.backgroundMessage()
|
2015-07-24 19:00:33 +05:30
|
|
|
bg.start()
|
|
|
|
|
|
|
|
bg.set_message('reading positions...')
|
|
|
|
|
2016-04-23 00:50:36 +05:30
|
|
|
table.data_readArray(options.pos) # read position vectors
|
2015-07-24 19:00:33 +05:30
|
|
|
grainID = -np.ones(len(table.data),dtype=int)
|
|
|
|
|
|
|
|
start = tick = time.clock()
|
|
|
|
bg.set_message('building KD tree...')
|
|
|
|
kdtree = spatial.KDTree(copy.deepcopy(table.data))
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ assign grain IDs --------------------------------------
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
tick = time.clock()
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
orientations = [] # quaternions found for grain
|
|
|
|
memberCounts = [] # number of voxels in grain
|
|
|
|
p = 0 # point counter
|
|
|
|
g = 0 # grain counter
|
2015-07-24 19:00:33 +05:30
|
|
|
matchedID = -1
|
2015-08-08 00:33:26 +05:30
|
|
|
lastDistance = np.dot(kdtree.data[-1]-kdtree.data[0],kdtree.data[-1]-kdtree.data[0]) # (arbitrarily) use diagonal of cloud
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data_rewind()
|
|
|
|
while table.data_read(): # read next data line of ASCII table
|
2015-07-24 19:00:33 +05:30
|
|
|
|
|
|
|
if p > 0 and p % 1000 == 0:
|
|
|
|
|
|
|
|
time_delta = (time.clock()-tick) * (len(grainID) - p) / p
|
2016-03-02 15:22:24 +05:30
|
|
|
bg.set_message('(%02i:%02i:%02i) processing point %i of %i (grain count %i)...'\
|
2016-04-14 05:03:46 +05:30
|
|
|
%(time_delta//3600,time_delta%3600//60,time_delta%60,p,len(grainID),np.count_nonzero(memberCounts)))
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if inputtype == 'eulers':
|
|
|
|
o = damask.Orientation(Eulers = np.array(map(float,table.data[column:column+3]))*toRadians,
|
|
|
|
symmetry = options.symmetry).reduced()
|
|
|
|
elif inputtype == 'matrix':
|
|
|
|
o = damask.Orientation(matrix = np.array(map(float,table.data[column:column+9])).reshape(3,3).transpose(),
|
|
|
|
symmetry = options.symmetry).reduced()
|
|
|
|
elif inputtype == 'frame':
|
|
|
|
o = damask.Orientation(matrix = np.array(map(float,table.data[column[0]:column[0]+3] + \
|
|
|
|
table.data[column[1]:column[1]+3] + \
|
|
|
|
table.data[column[2]:column[2]+3])).reshape(3,3),
|
|
|
|
symmetry = options.symmetry).reduced()
|
|
|
|
elif inputtype == 'quaternion':
|
|
|
|
o = damask.Orientation(quaternion = np.array(map(float,table.data[column:column+4])),
|
|
|
|
symmetry = options.symmetry).reduced()
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-04-14 05:03:46 +05:30
|
|
|
matched = False
|
|
|
|
alreadyChecked = {}
|
|
|
|
candidates = []
|
|
|
|
bestDisorientation = damask.Quaternion([0,0,0,1]) # initialize to 180 deg rotation as worst case
|
|
|
|
|
|
|
|
for i in kdtree.query_ball_point(kdtree.data[p],options.radius): # check all neighboring points
|
|
|
|
gID = grainID[i]
|
|
|
|
if gID != -1 and gID not in alreadyChecked: # indexed point belonging to a grain not yet tested?
|
|
|
|
alreadyChecked[gID] = True # remember not to check again
|
|
|
|
disorientation = o.disorientation(orientations[gID],SST = False)[0] # compare against other orientation
|
|
|
|
if disorientation.quaternion.w > cos_disorientation: # within threshold ...
|
|
|
|
candidates.append(gID) # remember as potential candidate
|
|
|
|
if disorientation.quaternion.w >= bestDisorientation.w: # ... and better than current best?
|
2015-07-24 19:00:33 +05:30
|
|
|
matched = True
|
2015-08-08 00:33:26 +05:30
|
|
|
matchedID = gID # remember that grain
|
2015-09-16 00:32:59 +05:30
|
|
|
bestDisorientation = disorientation.quaternion
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-04-14 05:03:46 +05:30
|
|
|
if matched: # did match existing grain
|
|
|
|
memberCounts[matchedID] += 1
|
|
|
|
if len(candidates) > 1: # ambiguity in grain identification?
|
|
|
|
largestGrain = sorted(candidates,key=lambda x:memberCounts[x])[-1] # find largest among potential candidate grains
|
|
|
|
matchedID = largestGrain
|
|
|
|
for c in [c for c in candidates if c != largestGrain]: # loop over smaller candidates
|
|
|
|
memberCounts[largestGrain] += memberCounts[c] # reassign member count of smaller to largest
|
|
|
|
memberCounts[c] = 0
|
|
|
|
grainID = np.where(np.in1d(grainID,candidates), largestGrain, grainID) # relabel grid points of smaller candidates as largest one
|
|
|
|
|
|
|
|
else: # no match -> new grain found
|
2015-08-08 00:33:26 +05:30
|
|
|
orientations += [o] # initialize with current orientation
|
2016-04-14 05:03:46 +05:30
|
|
|
memberCounts += [1] # start new membership counter
|
2015-07-24 19:00:33 +05:30
|
|
|
matchedID = g
|
2015-08-08 00:33:26 +05:30
|
|
|
g += 1 # increment grain counter
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
grainID[p] = matchedID # remember grain index assigned to point
|
|
|
|
p += 1 # increment point
|
|
|
|
|
2016-04-14 05:03:46 +05:30
|
|
|
grainIDs = np.where(np.array(memberCounts) > 0)[0] # identify "live" grain identifiers
|
|
|
|
packingMap = dict(zip(list(grainIDs),range(len(grainIDs)))) # map to condense into consecutive IDs
|
|
|
|
|
2015-07-24 19:00:33 +05:30
|
|
|
table.data_rewind()
|
2015-08-24 19:50:09 +05:30
|
|
|
|
|
|
|
outputAlive = True
|
2015-07-24 19:00:33 +05:30
|
|
|
p = 0
|
2015-08-24 19:50:09 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2016-04-14 05:03:46 +05:30
|
|
|
table.data_append(1+packingMap[grainID[p]]) # add (condensed) grain ID
|
2015-08-24 19:50:09 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2015-07-24 19:00:33 +05:30
|
|
|
p += 1
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
bg.set_message('done after {} seconds'.format(time.clock()-start))
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2015-07-24 19:00:33 +05:30
|
|
|
|
2016-03-02 15:22:24 +05:30
|
|
|
table.close() # close ASCII tables
|