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
|
|
|
|
|
|
|
import os,sys,math,string,numpy
|
2013-07-18 18:58:54 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
class extendableOption(Option):
|
2013-05-13 18:40:31 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
|
|
|
# taken from online tutorial http://docs.python.org/library/optparse.html
|
|
|
|
|
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
lvalue = value.split(",")
|
|
|
|
values.ensure_value(dest, []).extend(lvalue)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
|
|
|
|
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
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
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.
|
2013-07-18 18:58:54 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
2012-11-08 21:14:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('--column', dest='column', type='int', \
|
2013-04-12 16:45:17 +05:30
|
|
|
help='data column to discriminate phase 1 from 2 [%default]')
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('-t','--threshold', dest='threshold', type='float', \
|
2013-04-12 16:45:17 +05:30
|
|
|
help='threshold value to discriminate phase 1 from 2 [%default]')
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('--homogenization', dest='homogenization', type='int', \
|
2013-04-12 16:45:17 +05:30
|
|
|
help='homogenization index to be used [%default]')
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('--phase', dest='phase', type='int', nargs = 2, \
|
2013-05-17 22:14:03 +05:30
|
|
|
help='two phase indices to be used %default')
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('--crystallite', dest='crystallite', type='int', \
|
2013-04-12 16:45:17 +05:30
|
|
|
help='crystallite index to be used [%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]')
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.add_option('-a', '--axis', dest='axis', type='string', nargs = 3, \
|
|
|
|
help='axis assignement of eulerangles x,y,z = %default')
|
|
|
|
|
2013-04-12 16:45:17 +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)
|
|
|
|
parser.set_defaults(config = False)
|
2013-07-18 18:58:54 +05:30
|
|
|
parser.set_defaults(axis = ['y','x','-z'])
|
2012-11-08 21:14:51 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2013-07-18 18:58:54 +05:30
|
|
|
for i in options.axis:
|
|
|
|
if i.lower() not in ['x','+x','-x','y','+y','-y','z','+z','-z']:
|
|
|
|
file['croak'].write('invalid axis %s %s %s' %(options.axis[0],options.axis[1],options.axis[2]))
|
|
|
|
sys.exit()
|
|
|
|
|
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:
|
2013-07-18 18:58:54 +05:30
|
|
|
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')
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
info = {
|
2013-05-17 22:14:03 +05:30
|
|
|
'grid': numpy.ones (3,'i'),
|
2013-05-15 02:39:37 +05:30
|
|
|
'size': numpy.zeros(3,'d'),
|
2013-05-13 18:40:31 +05:30
|
|
|
'origin': numpy.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
|
|
|
'homogenization': options.homogenization
|
|
|
|
}
|
|
|
|
|
2012-11-08 21:14:51 +05:30
|
|
|
microstructure = ['<microstructure>']
|
|
|
|
texture = ['<texture>']
|
2013-05-13 18:40:31 +05:30
|
|
|
step = [0,0]
|
2012-11-08 21:14:51 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
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-04-12 16:45:17 +05:30
|
|
|
if words[1] == 'HexGrid':
|
|
|
|
file['croak'].write('The file has HexGrid format. Please first convert to SquareGrid...\n'); break
|
|
|
|
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:':
|
|
|
|
info['grid'][0] = int(words[2]); formatwidth = 1+int(math.log10(info['grid'][0]*info['grid'][1]))
|
|
|
|
if words[1] == 'NROWS:':
|
|
|
|
info['grid'][1] = int(words[2]); formatwidth = 1+int(math.log10(info['grid'][0]*info['grid'][1]))
|
2013-05-13 18:40:31 +05:30
|
|
|
else: # finished with comments block
|
|
|
|
if options.config: # write configuration (line by line)
|
2012-11-08 21:14:51 +05:30
|
|
|
point += 1
|
2013-02-20 20:20:01 +05:30
|
|
|
me = str(point).zfill(formatwidth)
|
2012-11-08 21:14:51 +05:30
|
|
|
microstructure += ['[Grain%s]\n'%me + \
|
|
|
|
'crystallite\t%i\n'%options.crystallite + \
|
2012-11-20 15:57:09 +05:30
|
|
|
'(constituent)\tphase %i\ttexture %s\tfraction 1.0\n'%(options.phase[{True:0,False:1}[float(words[options.column-1])<options.threshold]],me)
|
2012-11-08 21:14:51 +05:30
|
|
|
]
|
|
|
|
texture += ['[Grain%s]\n'%me + \
|
2013-07-23 20:03:30 +05:30
|
|
|
'rotation %s %s %s\n'%(options.axis[0],options.axis[1],options.axis[2]) +\
|
2012-11-08 21:14:51 +05:30
|
|
|
'(gauss)\tphi1 %4.2f\tPhi %4.2f\tphi2 %4.2f\tscatter 0.0\tfraction 1.0\n'%tuple(map(lambda x: float(x)*180.0/math.pi, words[:3]))
|
|
|
|
]
|
2013-05-13 18:40:31 +05:30
|
|
|
else: # only info from header needed
|
2012-11-08 21:14:51 +05:30
|
|
|
break
|
2013-05-13 18:40:31 +05:30
|
|
|
|
|
|
|
info['microstructures'] = info['grid'][0]*info['grid'][1]
|
|
|
|
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'])
|
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
if numpy.any(info['grid'] < 1):
|
2013-05-17 22:14:03 +05:30
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-05-15 02:39:37 +05:30
|
|
|
sys.exit()
|
|
|
|
if numpy.any(info['size'] <= 0.0):
|
2013-05-17 22:14:03 +05:30
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2013-05-15 02:39:37 +05:30
|
|
|
sys.exit()
|
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--- write data -----------------------------------------------------------------------------------
|
2012-11-08 21:14:51 +05:30
|
|
|
if options.config:
|
|
|
|
file['output'].write('\n'.join(microstructure) + \
|
|
|
|
'\n'.join(texture))
|
2013-05-13 18:40:31 +05:30
|
|
|
else:
|
2013-07-18 18:58:54 +05:30
|
|
|
header = [scriptID+'\n']
|
2013-05-13 18:40:31 +05:30
|
|
|
header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],))
|
|
|
|
header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][2],))
|
|
|
|
header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2],))
|
|
|
|
header.append("microstructures\t%i\n"%info['microstructures'])
|
|
|
|
header.append("homogenization\t%i\n"%info['homogenization'])
|
2013-05-14 23:21:53 +05:30
|
|
|
file['output'].write('%i\theader\n'%(len(header))+''.join(header))
|
2013-05-13 18:40:31 +05:30
|
|
|
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':
|
|
|
|
file['output'].close()
|
|
|
|
os.rename(file['name']+'_tmp',os.path.splitext(file['name'])[0] + \
|
|
|
|
{True: '_material.config',
|
|
|
|
False:'.geom'}[options.config])
|