2012-11-30 03:23:57 +05:30
|
|
|
#!/usr/bin/env python
|
2013-05-13 16:57:59 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2016-03-02 02:42:04 +05:30
|
|
|
import os,sys,math,itertools
|
2014-10-09 22:33:06 +05:30
|
|
|
import numpy as np
|
2012-11-30 03:23:57 +05:30
|
|
|
from scipy import ndimage
|
2014-10-09 22:33:06 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2013-01-18 17:12:27 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-01-18 17:12:27 +05:30
|
|
|
|
|
|
|
def periodic_3Dpad(array, rimdim=(1,1,1)):
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
rimdim = np.array(rimdim,'i')
|
|
|
|
size = np.array(array.shape,'i')
|
|
|
|
padded = np.empty(size+2*rimdim,array.dtype)
|
2013-01-18 17:12:27 +05:30
|
|
|
padded[rimdim[0]:rimdim[0]+size[0],
|
|
|
|
rimdim[1]:rimdim[1]+size[1],
|
|
|
|
rimdim[2]:rimdim[2]+size[2]] = array
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
p = np.zeros(3,'i')
|
2013-01-18 17:12:27 +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
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2013-01-18 17:12:27 +05:30
|
|
|
# MAIN
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
features = [
|
2015-08-08 00:33:26 +05:30
|
|
|
{'aliens': 1, 'alias': ['boundary','biplane'],},
|
|
|
|
{'aliens': 2, 'alias': ['tripleline',],},
|
|
|
|
{'aliens': 3, 'alias': ['quadruplepoint',],}
|
2013-01-18 17:12:27 +05:30
|
|
|
]
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2013-01-18 17:12:27 +05:30
|
|
|
neighborhoods = {
|
2014-10-09 22:33:06 +05:30
|
|
|
'neumann':np.array([
|
2013-01-18 17:12:27 +05:30
|
|
|
[-1, 0, 0],
|
|
|
|
[ 1, 0, 0],
|
|
|
|
[ 0,-1, 0],
|
|
|
|
[ 0, 1, 0],
|
|
|
|
[ 0, 0,-1],
|
|
|
|
[ 0, 0, 1],
|
|
|
|
]),
|
2014-10-09 22:33:06 +05:30
|
|
|
'moore':np.array([
|
2013-01-18 17:12:27 +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-05-14 20:56:06 +05:30
|
|
|
#
|
2013-01-18 17:12:27 +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],
|
2014-05-14 20:56:06 +05:30
|
|
|
#
|
2013-01-18 17:12:27 +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-10-09 22:33:06 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2013-01-18 17:12:27 +05:30
|
|
|
Produce geom files containing Euclidean distance to grain structural features:
|
|
|
|
boundaries, triple lines, and quadruple points.
|
2014-10-09 22:33:06 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-t','--type',
|
|
|
|
dest = 'type',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2015-11-16 16:22:56 +05:30
|
|
|
help = 'feature type {%s} '%(', '.join(map(lambda x:'|'.join(x['alias']),features))) )
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-n','--neighborhood',
|
|
|
|
dest = 'neighborhood',
|
|
|
|
choices = neighborhoods.keys(), metavar = 'string',
|
2015-11-16 16:22:56 +05:30
|
|
|
help = 'type of neighborhood {%s} [neumann]'%(', '.join(neighborhoods.keys())))
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-s', '--scale',
|
|
|
|
dest = 'scale',
|
|
|
|
type = 'float', metavar = 'float',
|
2014-05-14 20:56:06 +05:30
|
|
|
help = 'voxel size [%default]')
|
2013-01-18 17:12:27 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(type = [],
|
|
|
|
neighborhood = 'neumann',
|
|
|
|
scale = 1.0,
|
|
|
|
)
|
2013-01-18 17:12:27 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(options.type) == 0 or \
|
|
|
|
not set(options.type).issubset(set(list(itertools.chain(*map(lambda x: x['alias'],features))))):
|
|
|
|
parser.error('sleect feature type from (%s).'%(', '.join(map(lambda x:'|'.join(x['alias']),features))) )
|
2015-02-08 03:52:28 +05:30
|
|
|
if 'biplane' in options.type and 'boundary' in options.type:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error("only one alias out 'biplane' and 'boundary' required")
|
2013-01-18 17:12:27 +05:30
|
|
|
|
|
|
|
feature_list = []
|
|
|
|
for i,feature in enumerate(features):
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in feature['alias']:
|
2013-05-13 16:57:59 +05:30
|
|
|
for myType in options.type:
|
|
|
|
if name.startswith(myType):
|
2015-08-08 00:33:26 +05:30
|
|
|
feature_list.append(i) # remember selected features
|
2013-01-18 17:12:27 +05:30
|
|
|
break
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2015-08-12 23:39:40 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2013-05-13 16:57:59 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2015-08-12 23:39:40 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, labeled = False, readonly = True)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 21:04:27 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2014-05-14 20:56:06 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2014-05-14 20:56:06 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
|
|
|
|
2015-09-24 21:04:27 +05:30
|
|
|
damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
2015-08-08 00:33:26 +05:30
|
|
|
'size x y z: %s'%(' x '.join(map(str,info['size']))),
|
|
|
|
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
|
|
|
|
'homogenization: %i'%info['homogenization'],
|
|
|
|
'microstructures: %i'%info['microstructures'],
|
|
|
|
])
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
|
|
|
if errors != []:
|
2015-09-24 21:04:27 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
2014-05-14 20:56:06 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid']).reshape(info['grid'],order='F') # read microstructure
|
|
|
|
|
|
|
|
table.close()
|
2013-01-18 17:12:27 +05:30
|
|
|
|
|
|
|
neighborhood = neighborhoods[options.neighborhood]
|
2014-10-09 22:33:06 +05:30
|
|
|
convoluted = np.empty([len(neighborhood)]+list(info['grid']+2),'i')
|
2015-08-08 00:33:26 +05:30
|
|
|
structure = periodic_3Dpad(microstructure)
|
2013-01-18 17:12:27 +05:30
|
|
|
|
|
|
|
for i,p in enumerate(neighborhood):
|
2014-10-09 22:33:06 +05:30
|
|
|
stencil = np.zeros((3,3,3),'i')
|
2013-01-18 17:12:27 +05:30
|
|
|
stencil[1,1,1] = -1
|
|
|
|
stencil[p[0]+1,
|
|
|
|
p[1]+1,
|
|
|
|
p[2]+1] = 1
|
2014-05-14 20:56:06 +05:30
|
|
|
convoluted[i,:,:,:] = ndimage.convolve(structure,stencil)
|
2012-11-30 03:23:57 +05:30
|
|
|
|
2014-10-09 22:33:06 +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])
|
2014-05-14 20:56:06 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for i in xrange(1,len(neighborhood)): # check remaining points in neighborhood
|
2014-10-09 22:33:06 +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
|
|
|
|
|
|
|
|
for feature in feature_list:
|
2015-08-12 23:39:40 +05:30
|
|
|
try:
|
2015-08-21 01:12:05 +05:30
|
|
|
table = damask.ASCIItable(outname = features[feature]['alias'][0]+'_'+name if name else name,
|
2015-08-12 23:39:40 +05:30
|
|
|
buffered = False, labeled = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-08-12 23:39:40 +05:30
|
|
|
|
2015-09-24 21:04:27 +05:30
|
|
|
damask.util.croak(features[feature]['alias'][0])
|
2015-08-12 23:39:40 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
distance = np.where(uniques >= features[feature]['aliens'],0.0,1.0) # seed with 0.0 when enough unique neighbor IDs are present
|
|
|
|
distance = ndimage.morphology.distance_transform_edt(distance)*[options.scale]*3
|
2014-05-14 20:56:06 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
info['microstructures'] = int(math.ceil(distance.max()))
|
2013-05-13 16:57:59 +05:30
|
|
|
|
2014-05-14 20:56:06 +05:30
|
|
|
#--- write header ---------------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
table.info_clear()
|
|
|
|
table.info_append(extra_header+[
|
2014-05-14 20:56:06 +05:30
|
|
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
2015-08-08 00:33:26 +05:30
|
|
|
"grid\ta {grid[0]}\tb {grid[1]}\tc {grid[2]}".format(grid=info['grid']),
|
|
|
|
"size\tx {size[0]}\ty {size[1]}\tz {size[2]}".format(size=info['size']),
|
|
|
|
"origin\tx {origin[0]}\ty {origin[1]}\tz {origin[2]}".format(origin=info['origin']),
|
|
|
|
"homogenization\t{homog}".format(homog=info['homogenization']),
|
|
|
|
"microstructures\t{microstructures}".format(microstructures=info['microstructures']),
|
2014-05-14 20:56:06 +05:30
|
|
|
])
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_clear()
|
2014-10-09 22:33:06 +05:30
|
|
|
table.head_write()
|
2014-05-14 20:56:06 +05:30
|
|
|
|
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
formatwidth = int(math.floor(math.log10(distance.max())+1))
|
|
|
|
table.data = distance.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
2014-10-09 22:33:06 +05:30
|
|
|
table.data_writeArray('%%%ii'%(formatwidth),delimiter=' ')
|
2014-05-14 20:56:06 +05:30
|
|
|
|
|
|
|
#--- output finalization --------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.close()
|