2012-11-08 21:14:51 +05:30
|
|
|
#!/usr/bin/env python
|
2013-05-13 18:40:31 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
import os,sys,math,string
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2013-07-18 18:58:54 +05:30
|
|
|
|
2014-11-17 03:14:46 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
# MAIN
|
2013-05-13 18:40:31 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2014-10-09 22:33:06 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
|
2012-11-08 21:14:51 +05:30
|
|
|
Generate geometry description and material configuration from EBSD data in given square-gridded 'ang' file.
|
2013-04-12 16:45:17 +05:30
|
|
|
Two phases can be discriminated based on threshold value in a given data column.
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
""", version = scriptID)
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--column', dest='column', type='int', metavar = 'int', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='data column to discriminate between both phases [%default]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('-t','--threshold', dest='threshold', type='float', metavar = 'float', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='threshold value for phase discrimination [%default]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='homogenization index for <microstructure> configuration [%default]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int int', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='phase indices for <microstructure> configuration %default')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='crystallite index for <microstructure> configuration [%default]')
|
2012-11-08 21:14:51 +05:30
|
|
|
parser.add_option('-c', '--configuration', dest='config', action='store_true', \
|
2013-04-12 16:45:17 +05:30
|
|
|
help='output material configuration [%default]')
|
2014-01-30 17:30:20 +05:30
|
|
|
parser.add_option('--compress', dest='compress', action='store_true', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='lump identical microstructure and texture information [%default]')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-a', '--axes', dest='axes', nargs = 3, metavar = 'string string string', \
|
2014-11-14 20:23:48 +05:30
|
|
|
help='Euler angle coordinate system for <texture> configuration x,y,z = %default')
|
2013-07-18 18:58:54 +05:30
|
|
|
|
2013-04-12 16:45:17 +05:30
|
|
|
|
2014-11-14 20:23:48 +05:30
|
|
|
parser.set_defaults(column = 11)
|
|
|
|
parser.set_defaults(threshold = 0.5)
|
2012-11-08 21:14:51 +05:30
|
|
|
parser.set_defaults(homogenization = 1)
|
|
|
|
parser.set_defaults(phase = [1,2])
|
|
|
|
parser.set_defaults(crystallite = 1)
|
2014-11-14 20:23:48 +05:30
|
|
|
parser.set_defaults(config = False)
|
|
|
|
parser.set_defaults(compress = False)
|
2013-07-24 16:39:39 +05:30
|
|
|
parser.set_defaults(axes = ['y','x','-z'])
|
2012-11-08 21:14:51 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2013-07-24 16:39:39 +05:30
|
|
|
for i in options.axes:
|
2013-07-18 18:58:54 +05:30
|
|
|
if i.lower() not in ['x','+x','-x','y','+y','-y','z','+z','-z']:
|
2014-11-14 20:23:48 +05:30
|
|
|
parser.error('invalid axes %s %s %s' %(options.axes[0],options.axes[1],options.axes[2]))
|
2013-07-18 18:58:54 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--- setup file handles ---------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
files = []
|
|
|
|
if filenames == []:
|
|
|
|
files.append({'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'output':sys.stdout,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name,
|
|
|
|
'input':open(name),
|
|
|
|
'output':open(name+'_tmp','w'),
|
|
|
|
'croak':sys.stdout,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
for file in files:
|
2014-11-14 20:23:48 +05:30
|
|
|
file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
info = {
|
2014-01-30 17:30:20 +05:30
|
|
|
'grid': np.ones (3,'i'),
|
|
|
|
'size': np.zeros(3,'d'),
|
|
|
|
'origin': np.zeros(3,'d'),
|
2013-05-13 18:40:31 +05:30
|
|
|
'microstructures': 0,
|
2014-11-14 20:23:48 +05:30
|
|
|
'homogenization': options.homogenization,
|
2013-05-13 18:40:31 +05:30
|
|
|
}
|
|
|
|
|
2014-11-14 20:23:48 +05:30
|
|
|
step = [0,0]
|
|
|
|
point = 0
|
2012-11-08 21:14:51 +05:30
|
|
|
for line in file['input']:
|
|
|
|
words = line.split()
|
2013-05-13 18:40:31 +05:30
|
|
|
if words[0] == '#': # process initial comments block
|
2012-11-08 21:14:51 +05:30
|
|
|
if len(words) > 2:
|
2013-11-14 23:07:42 +05:30
|
|
|
if words[2].lower() == 'hexgrid':
|
|
|
|
file['croak'].write('The file has HexGrid format. Please first convert to SquareGrid...\n')
|
2014-11-14 20:23:48 +05:30
|
|
|
break
|
2013-04-12 16:45:17 +05:30
|
|
|
if words[1] == 'XSTEP:': step[0] = float(words[2])
|
|
|
|
if words[1] == 'YSTEP:': step[1] = float(words[2])
|
2013-05-17 22:14:03 +05:30
|
|
|
if words[1] == 'NCOLS_ODD:':
|
2014-01-30 17:30:20 +05:30
|
|
|
info['grid'][0] = int(words[2])
|
|
|
|
eulerangles = np.zeros((info['grid'][0]*info['grid'][1],3),dtype='f')
|
2014-11-14 20:23:48 +05:30
|
|
|
phase = np.zeros(info['grid'][0]*info['grid'][1],dtype='i')
|
|
|
|
if words[1] == 'NROWS:':
|
2014-01-30 17:30:20 +05:30
|
|
|
info['grid'][1] = int(words[2])
|
|
|
|
eulerangles = np.zeros((info['grid'][0]*info['grid'][1],3),dtype='f')
|
2014-11-14 20:23:48 +05:30
|
|
|
phase = np.zeros(info['grid'][0]*info['grid'][1],dtype='i')
|
2013-05-13 18:40:31 +05:30
|
|
|
else: # finished with comments block
|
2014-11-14 20:23:48 +05:30
|
|
|
phase[point] = options.phase[int(float(words[options.column-1]) > options.threshold)]
|
2014-11-17 03:14:46 +05:30
|
|
|
eulerangles[point,...] = map(lambda x: math.degrees(float(x)), words[:3])
|
2013-11-14 23:07:42 +05:30
|
|
|
point += 1
|
2014-01-30 17:30:20 +05:30
|
|
|
|
|
|
|
if info['grid'].prod() != point:
|
|
|
|
file['croak'].write('Error: found %s microstructures. Header info in ang file might be wrong.\n'%point)
|
2014-11-14 20:23:48 +05:30
|
|
|
continue
|
2014-01-30 17:30:20 +05:30
|
|
|
if options.compress:
|
|
|
|
texture = []
|
|
|
|
microstructure = []
|
2014-11-14 20:23:48 +05:30
|
|
|
otherPoint = -1 # ensure to create first microstructure
|
|
|
|
matPoints = np.zeros(info['grid'].prod(),dtype='i') # index of microstructure in geom file
|
|
|
|
for myPoint in xrange(info['grid'].prod()):
|
|
|
|
myTexture = -1
|
2014-01-30 17:30:20 +05:30
|
|
|
for otherPoint in xrange(len(microstructure)):
|
|
|
|
otherEulers = eulerangles[texture[microstructure[otherPoint][0]]]
|
2014-12-20 18:12:21 +05:30
|
|
|
otherPhase = microstructure[otherPoint][1]
|
|
|
|
if all(abs(eulerangles[myPoint]-otherEulers)<1e-6) and phase[myPoint] == otherPhase: # common microstructure
|
|
|
|
matPoints[myPoint] = otherPoint+1 # use other point's microstructure, +1 because starting with 1 (.config) instead of 0 (python)
|
2014-11-14 20:23:48 +05:30
|
|
|
otherPoint = -2 # never create new microstructure
|
2014-01-30 17:30:20 +05:30
|
|
|
break
|
2014-11-14 20:23:48 +05:30
|
|
|
elif all(eulerangles[myPoint] == otherEulers): # found common texture and store it
|
2014-01-30 17:30:20 +05:30
|
|
|
myTexture = microstructure[otherPoint][0]
|
2014-11-14 19:23:55 +05:30
|
|
|
if otherPoint == len(microstructure)-1: # did not found matching microstructure
|
|
|
|
if myTexture == -1: # did not even found matching texture
|
2014-12-20 18:12:21 +05:30
|
|
|
myTexture = len(texture)
|
2014-01-30 17:30:20 +05:30
|
|
|
texture.append(myPoint)
|
2014-12-20 18:12:21 +05:30
|
|
|
microstructure.append([myTexture,phase[myPoint]])
|
2014-11-14 19:23:55 +05:30
|
|
|
matPoints[myPoint] = len(microstructure) # use the new microstructure
|
2014-01-30 17:30:20 +05:30
|
|
|
else:
|
|
|
|
texture = [i for i in xrange(info['grid'][0]*info['grid'][1])]
|
2014-11-14 19:23:55 +05:30
|
|
|
microstructure = [[i,phase[i]] for i in xrange(info['grid'][0]*info['grid'][1])]
|
2014-01-30 17:30:20 +05:30
|
|
|
|
|
|
|
formatOut = 1+int(math.log10(len(texture)))
|
|
|
|
textureOut =['\n\n<texture>']
|
|
|
|
for i in xrange(len(texture)):
|
2014-12-20 18:12:21 +05:30
|
|
|
textureOut += ['[Texture%s]\n'%str(texture[i]+1).zfill(formatOut) + \
|
2014-01-30 17:30:20 +05:30
|
|
|
'axes %s %s %s\n'%(options.axes[0],options.axes[1],options.axes[2]) +\
|
|
|
|
'(gauss)\tphi1 %4.2f\tPhi %4.2f\tphi2 %4.2f\tscatter 0.0\tfraction 1.0\n'%tuple(eulerangles[texture[i],...])
|
|
|
|
]
|
|
|
|
formatOut = 1+int(math.log10(len(microstructure)))
|
|
|
|
microstructureOut =['<microstructure>']
|
|
|
|
for i in xrange(len(microstructure)):
|
|
|
|
microstructureOut += ['[Grain%s]\n'%str(i+1).zfill(formatOut) + \
|
2013-11-14 23:07:42 +05:30
|
|
|
'crystallite\t%i\n'%options.crystallite + \
|
2014-11-14 20:23:48 +05:30
|
|
|
'(constituent)\tphase %i\ttexture %i\tfraction 1.0\n'%(microstructure[i][1],microstructure[i][0]+1)
|
2014-01-30 17:30:20 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
info['microstructures'] = len(microstructure)
|
2013-05-13 18:40:31 +05:30
|
|
|
info['size'] = step[0]*info['grid'][0],step[1]*info['grid'][1],min(step)
|
|
|
|
|
|
|
|
#--- report ---------------------------------------------------------------------------------------
|
|
|
|
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']))) + \
|
|
|
|
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
|
|
|
|
'homogenization: %i\n'%info['homogenization'] + \
|
|
|
|
'microstructures: %i\n\n'%info['microstructures'])
|
|
|
|
|
2014-01-30 17:30:20 +05:30
|
|
|
if np.any(info['grid'] < 1):
|
2013-05-17 22:14:03 +05:30
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2014-11-14 20:23:48 +05:30
|
|
|
continue
|
2014-01-30 17:30:20 +05:30
|
|
|
if np.any(info['size'] <= 0.0):
|
2013-05-17 22:14:03 +05:30
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2014-11-14 20:23:48 +05:30
|
|
|
continue
|
2014-01-30 17:30:20 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--- write data -----------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
if options.config:
|
2014-11-14 20:23:48 +05:30
|
|
|
file['output'].write('\n'.join(microstructureOut+ textureOut) + '\n')
|
2013-05-13 18:40:31 +05:30
|
|
|
else:
|
2014-11-14 20:23:48 +05:30
|
|
|
header = [' '.join([scriptID] + 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],),
|
|
|
|
"microstructures\t%i"%info['microstructures'],
|
|
|
|
"homogenization\t%i"%info['homogenization'],
|
|
|
|
]
|
|
|
|
file['output'].write('\n'.join(['%i\theader'%(len(header))] + header) + '\n')
|
2014-01-30 17:30:20 +05:30
|
|
|
if options.compress:
|
|
|
|
matPoints = matPoints.reshape((info['grid'][1],info['grid'][0]))
|
|
|
|
np.savetxt(file['output'],matPoints,fmt='%0'+str(1+int(math.log10(np.amax(matPoints))))+'d')
|
|
|
|
else:
|
|
|
|
file['output'].write("1 to %i\n"%(info['microstructures']))
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--- output finalization --------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2014-11-14 20:23:48 +05:30
|
|
|
file['output'].close()
|
|
|
|
os.rename(file['name']+'_tmp',
|
2014-11-17 03:14:46 +05:30
|
|
|
os.path.splitext(file['name'])[0] +'%s'%('_material.config' if options.config else '.geom'))
|