renamed script to new convention
aligned output behavior to that of fromVoronoiTessellation, i.e., standard output is geom, --config gives associated material.config stud
This commit is contained in:
parent
af2aa8500d
commit
025d6c9048
|
@ -0,0 +1,127 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os,sys,math,string,numpy
|
||||
from optparse import OptionParser, Option
|
||||
|
||||
# -----------------------------
|
||||
class extendableOption(Option):
|
||||
# -----------------------------
|
||||
# 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)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
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.
|
||||
Dual phases can be discriminated based on threshold value in a given data column.
|
||||
""" + string.replace('$Id$','\n','\\n')
|
||||
)
|
||||
|
||||
|
||||
parser.add_option('--column', dest='column', type='int', \
|
||||
help='data column to separate phase 1 and 2 [%default]')
|
||||
parser.add_option('-f','--threshold', dest='threshold', type='float', \
|
||||
help='threshold value to discriminate phase 1 from 2')
|
||||
parser.add_option('--homogenization', dest='homogenization', type='int', \
|
||||
help='homogenization index to be used')
|
||||
parser.add_option('--phase', dest='phase', type='int', nargs = 2, \
|
||||
help='two phase indices to be used %default')
|
||||
parser.add_option('--crystallite', dest='crystallite', type='int', \
|
||||
help='crystallite index to be used')
|
||||
parser.add_option('-c', '--configuration', dest='config', action='store_true', \
|
||||
help='output material configuration')
|
||||
|
||||
parser.set_defaults(column = 1)
|
||||
parser.set_defaults(threshold = sys.maxint)
|
||||
parser.set_defaults(homogenization = 1)
|
||||
parser.set_defaults(phase = [1,2])
|
||||
parser.set_defaults(crystallite = 1)
|
||||
parser.set_defaults(config = False)
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
|
||||
# ------------------------------------------ loop over input files ---------------------------------------
|
||||
|
||||
for file in files:
|
||||
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
|
||||
|
||||
point = 0
|
||||
step = [0,0]
|
||||
resolution = [1,1]
|
||||
microstructure = ['<microstructure>']
|
||||
texture = ['<texture>']
|
||||
|
||||
for line in file['input']:
|
||||
words = line.split()
|
||||
if words[0] == '#': # process initial comments block
|
||||
if len(words) > 2:
|
||||
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])
|
||||
if words[1] == 'NCOLS_ODD:': resolution[0] = int(words[2]); formatwidth = 1+int(math.log10(resolution[0]*resolution[1]))
|
||||
if words[1] == 'NROWS:': resolution[1] = int(words[2]); formatwidth = 1+int(math.log10(resolution[0]*resolution[1]))
|
||||
else: # finished with comments block
|
||||
if options.config: # write configuration (line by line)
|
||||
point += 1
|
||||
me = str(point).rjust(formatwidth)
|
||||
microstructure += ['[Grain%s]\n'%me + \
|
||||
'crystallite\t%i\n'%options.crystallite + \
|
||||
'(constituent)\tphase %i\ttexture %s\tfraction 1.0\n'%(options.phase[{True:0,False:1}[float(words[options.column])<options.threshold]],me)
|
||||
]
|
||||
texture += ['[Grain%s]\n'%me + \
|
||||
'(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]))
|
||||
]
|
||||
else:
|
||||
file['output'].write("4 header\n" + \
|
||||
"resolution\ta %i\tb %i\tc 1\n"%(resolution[0],resolution[1]) + \
|
||||
"dimension\tx %g\ty %g\tz %g\n"%(step[0]*resolution[0],step[1]*resolution[1],min(step)) + \
|
||||
"origin\tx 0\ty 0\tz 0\n" + \
|
||||
"homogenization %i\n"%options.homogenization + \
|
||||
"1 to %i\n"%(resolution[0]*resolution[1]))
|
||||
break
|
||||
|
||||
if options.config:
|
||||
file['output'].write('\n'.join(microstructure) + \
|
||||
'\n'.join(texture))
|
||||
|
||||
# ------------------------------------------ output finalization ---------------------------------------
|
||||
|
||||
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])
|
|
@ -1,107 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os,sys,math,string,numpy
|
||||
from optparse import OptionParser, Option
|
||||
|
||||
# -----------------------------
|
||||
class extendableOption(Option):
|
||||
# -----------------------------
|
||||
# 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)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
||||
Converts EBSD data from cubic ang files into description for spectral solver (*.geom + material.config)
|
||||
Can discriminate two phases depending on threshold value
|
||||
|
||||
""" + string.replace('$Id$','\n','\\n')
|
||||
)
|
||||
|
||||
|
||||
parser.add_option('-c','--column', dest='column', type='int', \
|
||||
help='data column to separate phase 1 and 2 [%default]')
|
||||
parser.add_option('-t','--threshold', dest='threshold', type='float', \
|
||||
help='threshold used to separate phases. value < threshold: phase = 1')
|
||||
|
||||
parser.set_defaults(column = 1)
|
||||
parser.set_defaults(threshold = sys.maxint)
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
eulers = numpy.array([0.0,0.0,0.0],'f')
|
||||
geomdim = numpy.array([0.0,0.0,0.0],'f')
|
||||
res = numpy.array([0,0,1],'i')
|
||||
|
||||
files = []
|
||||
if filenames == []:
|
||||
files.append({'name':'STDIN', 'input':sys.stdin, 'material':sys.stdout, 'geom':sys.stdout})
|
||||
else:
|
||||
for name in filenames:
|
||||
if os.path.exists(name):
|
||||
files.append( {'name':name, 'input':open(name),\
|
||||
'material':open(os.path.splitext(name)[0]+'_material.config', 'w'),\
|
||||
'geom':open(os.path.splitext(name)[0]+'.geom', 'w')})
|
||||
|
||||
# ------------------------------------------ loop over input files ---------------------------------------
|
||||
for file in files:
|
||||
point=0
|
||||
if file['name'] != 'STDIN': print file['name']
|
||||
file['material'].write('#---\n<homogenization>\n#---\n'+
|
||||
'\n[SX]\ntype isostrain\nNgrains 1\n\n'+
|
||||
'#---\n<microstructure>\n#---\n\n')
|
||||
tempPart2= '#---\n<texture>\n#---\n\n'
|
||||
for line in file['input']:
|
||||
lineSplit=line.split()
|
||||
|
||||
if line[0]=='#':
|
||||
if len(lineSplit)>2:
|
||||
if line.split()[2]=='HexGrid':
|
||||
print 'The file is a hex grid file. Convert it first to sqr grid'
|
||||
sys.exit()
|
||||
if lineSplit[1]=='XSTEP:': stepSizeX = float(lineSplit[2])
|
||||
if lineSplit[1]=='YSTEP:': stepSizeY = float(lineSplit[2])
|
||||
if lineSplit[1]=='NCOLS_ODD:': res[0] = int(lineSplit[2])
|
||||
if lineSplit[1]=='NROWS:': res[1] = int(lineSplit[2])
|
||||
else:
|
||||
point+=1
|
||||
eulers = (float(lineSplit[0])/2.0/math.pi*360.0, \
|
||||
float(lineSplit[1])/2.0/math.pi*360.0, \
|
||||
float(lineSplit[2])/2.0/math.pi*360.0)
|
||||
|
||||
if float(lineSplit[options.column-1])<options.threshold:
|
||||
phase=1
|
||||
else:
|
||||
phase=2
|
||||
|
||||
file['material'].write(\
|
||||
'[Grain%08d]\ncrystallite 1\n(constituent) phase %1d texture %08d fraction 1.0\n' \
|
||||
%(point,phase,point))
|
||||
tempPart2+=\
|
||||
'[Grain%08d]\n(gauss) phi1 %4.2f Phi %4.2f phi2 %4.2f scatter 0.0 fraction 1.0\n'\
|
||||
%(point,eulers[0],eulers[1],eulers[2])
|
||||
geomdim[0] = stepSizeX*res[0]
|
||||
geomdim[1] = stepSizeY*res[1]
|
||||
geomdim[2] = min(stepSizeX,stepSizeY)
|
||||
|
||||
file['material'].write(tempPart2)
|
||||
file['geom'].write(
|
||||
'3 header\nresolution a %4d b %4d c %1d \ndimension x %5.3f y %5.3f z %5.3f\nhomogenization 1\n'\
|
||||
%(res[0],res[1],res[2],geomdim[0],geomdim[1],geomdim[2]))
|
||||
file['geom'].write('1 to %d\n'%(res[0]*res[1]))
|
Loading…
Reference in New Issue