fixed typo that prevented selection of "Moore" neighborhood.

made "boundary" and "biplane" synonyms.
adopted more modern treatment of geom-files.
introduced scale for voxel size (independent of physical size and resolution given in geom-file).
This commit is contained in:
Philip Eisenlohr 2014-05-14 15:26:06 +00:00
parent 4da866b29b
commit 8a80f5ec7a
1 changed files with 90 additions and 85 deletions

View File

@ -51,12 +51,15 @@ def periodic_3Dpad(array, rimdim=(1,1,1)):
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# MAIN # MAIN
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
synonyms = {
'grid': ['resolution'],
'size': ['dimension'],
}
identifiers = { identifiers = {
'grid': ['a','b','c'], 'grid': ['a','b','c'],
'size': ['x','y','z'], 'size': ['x','y','z'],
'origin': ['x','y','z'], 'origin': ['x','y','z'],
} }
mappings = { mappings = {
'grid': lambda x: int(x), 'grid': lambda x: int(x),
'size': lambda x: float(x), 'size': lambda x: float(x),
@ -66,9 +69,9 @@ mappings = {
} }
features = [ features = [
{'aliens': 1, 'names': ['boundary(biplane)'],}, {'aliens': 1, 'names': ['boundary','biplane'],},
{'aliens': 2, 'names': ['tripleline'],}, {'aliens': 2, 'names': ['tripleline',],},
{'aliens': 3, 'names': ['quadruplepoint'],} {'aliens': 3, 'names': ['quadruplepoint',],}
] ]
neighborhoods = { neighborhoods = {
@ -90,6 +93,7 @@ neighborhoods = {
[-1, 1,-1], [-1, 1,-1],
[ 0, 1,-1], [ 0, 1,-1],
[ 1, 1,-1], [ 1, 1,-1],
#
[-1,-1, 0], [-1,-1, 0],
[ 0,-1, 0], [ 0,-1, 0],
[ 1,-1, 0], [ 1,-1, 0],
@ -99,6 +103,7 @@ neighborhoods = {
[-1, 1, 0], [-1, 1, 0],
[ 0, 1, 0], [ 0, 1, 0],
[ 1, 1, 0], [ 1, 1, 0],
#
[-1,-1, 1], [-1,-1, 1],
[ 0,-1, 1], [ 0,-1, 1],
[ 1,-1, 1], [ 1,-1, 1],
@ -117,16 +122,16 @@ boundaries, triple lines, and quadruple points.
""" + string.replace(scriptID,'\n','\\n') """ + string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar = '<string LIST>', \ parser.add_option('-t','--type', dest = 'type', action = 'extend', type = 'string', metavar = '<string LIST>',
help='feature type (%s) '%(', '.join(map(lambda x:', '.join(x['names']),features))) ) help = 'feature type (%s) '%(', '.join(map(lambda x:'|'.join(x['names']),features))) )
parser.add_option('-n','--neighborhood', dest='neigborhood', choices=neighborhoods.keys(), metavar = 'string', \ parser.add_option('-n','--neighborhood', dest='neighborhood', choices = neighborhoods.keys(), metavar = 'string',
help='type of neighborhood (%s) [neumann]'%(', '.join(neighborhoods.keys()))) help = 'type of neighborhood (%s) [neumann]'%(', '.join(neighborhoods.keys())))
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \ parser.add_option('-s', '--scale', dest = 'scale', type = 'float',
help='output geom file with two-dimensional data arrangement [%default]') help = 'voxel size [%default]')
parser.set_defaults(type = []) parser.set_defaults(type = [])
parser.set_defaults(neighborhood = 'neumann') parser.set_defaults(neighborhood = 'neumann')
parser.set_defaults(twoD = False) parser.set_defaults(scale = 1.0)
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
@ -152,8 +157,7 @@ else:
if os.path.exists(name): if os.path.exists(name):
files.append({'name':name, files.append({'name':name,
'input':open(name), 'input':open(name),
'output':[open(string.split(''.join((features[feature]['names'])),sep='(')[0]+'_'+name,'w') 'output':[open(features[feature]['names'][0]+'_'+name,'w') for feature in feature_list],
for feature in feature_list],
'croak':sys.stdout, 'croak':sys.stdout,
}) })
@ -162,35 +166,29 @@ for file in files:
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n') if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n') else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
firstline = file['input'].readline() theTable = damask.ASCIItable(file['input'],file['output'][0],labels = False)
m = re.search('(\d+)\s*head', firstline.lower()) theTable.head_read()
if m:
headerlines = int(m.group(1))
headers = [file['input'].readline() for i in range(headerlines)]
else:
headerlines = 1
headers = firstline
content = file['input'].readlines() #--- interpret header ----------------------------------------------------------------------------
file['input'].close()
#--- interprete header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'), 'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'homogenization': 0,
'homogenization': 0 'microstructures': 0,
} }
newInfo = { newInfo = {
'microstructures': 0, 'grid': numpy.zeros(3,'i'),
} 'origin': numpy.zeros(3,'d'),
'microstructures': 0,
}
extra_header = []
new_header = [] for header in theTable.info:
for header in headers:
headitems = map(str.lower,header.split()) headitems = map(str.lower,header.split())
if headitems[0] == 'resolution': headitems[0] = 'grid' if len(headitems) == 0: continue # skip blank lines
if headitems[0] == 'dimension': headitems[0] = 'size' for synonym,alternatives in synonyms.iteritems():
if headitems[0] in alternatives: headitems[0] = synonym
if headitems[0] in mappings.keys(): if headitems[0] in mappings.keys():
if headitems[0] in identifiers.keys(): if headitems[0] in identifiers.keys():
for i in xrange(len(identifiers[headitems[0]])): for i in xrange(len(identifiers[headitems[0]])):
@ -199,40 +197,41 @@ for file in files:
else: else:
info[headitems[0]] = mappings[headitems[0]](headitems[1]) info[headitems[0]] = mappings[headitems[0]](headitems[1])
else: else:
new_header.append(header) extra_header.append(header)
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \ 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \ 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
'homogenization: %i\n'%info['homogenization'] + \ 'homogenization: %i\n'%info['homogenization'] + \
'microstructures: %i\n'%info['microstructures']) 'microstructures: %i\n'%info['microstructures'])
if numpy.any(info['grid'] < 1): if numpy.any(info['grid'] < 1):
file['croak'].write('invalid grid a b c.\n') file['croak'].write('invalid grid a b c.\n')
sys.exit() continue
if numpy.any(info['size'] <= 0.0): if numpy.any(info['size'] <= 0.0):
file['croak'].write('invalid size x y z.\n') file['croak'].write('invalid size x y z.\n')
sys.exit() continue
new_header.append(scriptID + ' ' + ' '.join(sys.argv[1:]) + '\n') #--- read data ------------------------------------------------------------------------------------
new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],)) microstructure = numpy.zeros(info['grid'].prod(),'i') # initialize as flat array
new_header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][2],))
new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2],))
new_header.append("homogenization\t%i\n"%info['homogenization'])
#--- process input --------------------------------------------------------------------------------
structure = numpy.zeros(info['grid'],'i')
i = 0 i = 0
for line in content:
for item in map(int,line.split()): while theTable.data_read():
structure[i%info['grid'][0], items = theTable.data
(i/info['grid'][0])%info['grid'][1], if len(items) > 2:
i/info['grid'][0] /info['grid'][1]] = item if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
i += 1 elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2]))
else: items = map(int,items)
else: items = map(int,items)
s = len(items)
microstructure[i:i+s] = items
i += s
neighborhood = neighborhoods[options.neighborhood] neighborhood = neighborhoods[options.neighborhood]
convoluted = numpy.empty([len(neighborhood)]+list(info['grid']+2),'i') convoluted = numpy.empty([len(neighborhood)]+list(info['grid']+2),'i')
microstructure = periodic_3Dpad(structure) structure = periodic_3Dpad(microstructure.reshape(info['grid'],order='F'))
for i,p in enumerate(neighborhood): for i,p in enumerate(neighborhood):
stencil = numpy.zeros((3,3,3),'i') stencil = numpy.zeros((3,3,3),'i')
@ -240,43 +239,49 @@ for file in files:
stencil[p[0]+1, stencil[p[0]+1,
p[1]+1, p[1]+1,
p[2]+1] = 1 p[2]+1] = 1
convoluted[i,:,:,:] = ndimage.convolve(structure,stencil)
convoluted[i,:,:,:] = ndimage.convolve(microstructure,stencil)
distance = numpy.ones((len(feature_list),info['grid'][0],info['grid'][1],info['grid'][2]),'d') distance = numpy.ones((len(feature_list),info['grid'][0],info['grid'][1],info['grid'][2]),'d')
convoluted = numpy.sort(convoluted,axis=0) convoluted = numpy.sort(convoluted,axis = 0)
uniques = numpy.zeros(info['grid']) uniques = numpy.where(convoluted[0,1:-1,1:-1,1:-1] != 0, 1,0) # initialize unique value counter (exclude myself [= 0])
check = numpy.empty(info['grid'])
check[:,:,:] = numpy.nan for i in xrange(1,len(neighborhood)): # check remaining points in neighborhood
for i in xrange(len(neighborhood)): uniques += numpy.where(numpy.logical_and(
uniques += numpy.where(convoluted[i,1:-1,1:-1,1:-1] == check,0,1) convoluted[i,1:-1,1:-1,1:-1] != convoluted[i-1,1:-1,1:-1,1:-1], # flip of ID difference detected?
check = convoluted[i,1:-1,1:-1,1:-1] convoluted[i,1:-1,1:-1,1:-1] != 0), # not myself?
1,0) # count flip
for i,feature_id in enumerate(feature_list): for i,feature_id in enumerate(feature_list):
distance[i,:,:,:] = numpy.where(uniques > features[feature_id]['aliens'],0.0,1.0) distance[i,:,:,:] = numpy.where(uniques >= features[feature_id]['aliens'],0.0,1.0) # seed with 0.0 when enough unique neighbor IDs are present
for i in xrange(len(feature_list)): for i in xrange(len(feature_list)):
distance[i,:,:,:] = ndimage.morphology.distance_transform_edt(distance[i,:,:,:])*\ distance[i,:,:,:] = ndimage.morphology.distance_transform_edt(distance[i,:,:,:])*[options.scale]*3
[max(info['size']/info['grid'])]*3
for i,feature in enumerate(feature_list): for i,feature in enumerate(feature_list):
newInfo['microstructures'] = int(math.ceil(distance[i,:,:,:].max())) newInfo['microstructures'] = int(math.ceil(distance[i,:,:,:].max()))
#--- write header ---------------------------------------------------------------------------------
theTable = damask.ASCIItable(file['input'],file['output'][i],labels = False)
theTable.labels_clear()
theTable.info_clear()
theTable.info_append(extra_header+[
scriptID + ' ' + ' '.join(sys.argv[1:]),
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
"size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],),
"origin\tx %f\ty %f\tz %f"%(info['origin'][0],info['origin'][1],info['origin'][2],),
"homogenization\t%i"%info['homogenization'],
"microstructures\t%i"%(newInfo['microstructures']),
])
theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------
formatwidth = int(math.floor(math.log10(distance[i,:,:,:].max())+1)) formatwidth = int(math.floor(math.log10(distance[i,:,:,:].max())+1))
theTable.data = distance[i,:,:,:].reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
#--- assemble header and report changes ----------------------------------------------------------- theTable.data_writeArray('%%%ii'%(formatwidth),delimiter=' ')
output = '%i\theader\n'%(len(new_header)+1)+''.join(new_header) file['output'][i].close()
output += "microstructures\t%i\n"%newInfo['microstructures']
file['croak'].write('\n'+features[i]['names'][0]+'\n') #--- output finalization --------------------------------------------------------------------------
if (newInfo['microstructures'] != info['microstructures']):
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
#--- write new data -------------------------------------------------------------------------------
for z in xrange(info['grid'][2]):
for y in xrange(info['grid'][1]):
output += {True:' ',False:'\n'}[options.twoD].join(map(lambda x: \
('%%%ii'%formatwidth)%(round(x)), distance[i,:,y,z])) + '\n'
file['output'][i].write(output)
if file['name'] != 'STDIN':
file['output'][i].close()
#--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['input'].close() file['input'].close()