2012-11-06 02:49:12 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
import os,sys,math,string,re,numpy, damask
|
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
class extendedOption(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
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
identifiers = {
|
2013-04-12 15:57:05 +05:30
|
|
|
'grid': ['a','b','c'],
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
2013-04-12 15:57:05 +05:30
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'grains': lambda x: int(x),
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
|
2012-11-08 21:13:38 +05:30
|
|
|
Generate geometry description and material configuration by standard Voronoi tessellation of given seeds file.
|
2012-11-06 02:49:12 +05:30
|
|
|
""" + string.replace('$Id$','\n','\\n')
|
|
|
|
)
|
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
parser.add_option('-g', '--grid', dest='grid', type='int', nargs = 3, \
|
|
|
|
help='a,b,c grid of hexahedral box [from seed file]')
|
|
|
|
parser.add_option('-s', '--size', dest='size', type='float', nargs = 3, \
|
|
|
|
help='x,y,z size of hexahedral box [1.0 along largest grid point number]')
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.add_option('--homogenization', dest='homogenization', type='int', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='homogenization index to be used [%default]')
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.add_option('--phase', dest='phase', type='int', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='phase index to be used [%default]')
|
2012-11-08 21:13:38 +05:30
|
|
|
parser.add_option('--crystallite', dest='crystallite', type='int', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='crystallite index to be used [%default]')
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.add_option('-c', '--configuration', dest='config', action='store_true', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='output material configuration [%default]')
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='output geom file with two-dimensional data arrangement [%default]')
|
2012-11-06 02:49:12 +05:30
|
|
|
|
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
parser.set_defaults(grid = [0,0,0])
|
|
|
|
parser.set_defaults(size = [0.0,0.0,0.0])
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.set_defaults(homogenization = 1)
|
|
|
|
parser.set_defaults(phase = 1)
|
2012-11-08 21:13:38 +05:30
|
|
|
parser.set_defaults(crystallite = 1)
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.set_defaults(config = False)
|
|
|
|
parser.set_defaults(twoD = 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')
|
|
|
|
|
|
|
|
firstline = file['input'].readline()
|
|
|
|
m = re.search('(\d+)\s*head', firstline.lower())
|
|
|
|
if m:
|
|
|
|
headerlines = int(m.group(1))
|
|
|
|
headers = [firstline]+[file['input'].readline() for i in range(headerlines)]
|
|
|
|
else:
|
|
|
|
headerlines = 1
|
|
|
|
headers = firstline
|
|
|
|
|
|
|
|
content = file['input'].readlines()
|
|
|
|
file['input'].close()
|
|
|
|
|
|
|
|
info = {'grains': 0,
|
2013-04-12 15:57:05 +05:30
|
|
|
'grid': numpy.array([0,0,0]),
|
|
|
|
'size': numpy.array(options.size),
|
|
|
|
'origin': numpy.array([0.0,0.0,0.0]),
|
2012-11-06 02:49:12 +05:30
|
|
|
'homogenization': options.homogenization,
|
|
|
|
}
|
|
|
|
|
|
|
|
new_header = []
|
|
|
|
for header in headers:
|
|
|
|
headitems = map(str.lower,header.split())
|
2013-04-12 15:57:05 +05:30
|
|
|
if headitems[0] == 'resolution': headitems[0] = 'grid'
|
2012-11-06 02:49:12 +05:30
|
|
|
if headitems[0] in mappings.keys():
|
|
|
|
if headitems[0] in identifiers.keys():
|
|
|
|
for i in xrange(len(identifiers[headitems[0]])):
|
|
|
|
info[headitems[0]][i] = \
|
|
|
|
mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1])
|
|
|
|
else:
|
|
|
|
info[headitems[0]] = mappings[headitems[0]](headitems[1])
|
|
|
|
|
|
|
|
if info['grains'] == 0:
|
|
|
|
file['croak'].write('no grains found.\n')
|
|
|
|
continue
|
|
|
|
if info['grains'] != len(content):
|
|
|
|
file['croak'].write('grain data not matching grain count...\n')
|
|
|
|
info['grains'] = min(info['grains'],len(content))
|
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
if 0 not in options.grid: # user-specified grid
|
|
|
|
info['grid'] = numpy.array(options.grid)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
if numpy.any(info['grid'] < 1):
|
|
|
|
file['croak'].write('no valid grid info found.\n')
|
2012-11-06 02:49:12 +05:30
|
|
|
continue
|
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
twoD = info['grid'][2] < 2
|
2012-11-06 02:49:12 +05:30
|
|
|
|
|
|
|
for i in xrange(3):
|
2013-04-12 15:57:05 +05:30
|
|
|
if info['size'][i] <= 0.0: # any invalid size?
|
|
|
|
info['size'][i] = float(info['grid'][i])/max(info['grid'])
|
|
|
|
file['croak'].write('rescaling size %i...\n'%i)
|
|
|
|
|
|
|
|
file['croak'].write('grains to map: %i\n'%info['grains'] + \
|
|
|
|
'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']))) + \
|
2012-11-06 02:49:12 +05:30
|
|
|
'homogenization: %i\n'%info['homogenization'])
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------- prepare data ----------------------------------
|
|
|
|
|
|
|
|
formatwidth = 1+int(math.log10(info['grains']))
|
|
|
|
coords = numpy.zeros((3,info['grains']),'d')
|
|
|
|
eulers = numpy.zeros((3,info['grains']),'d')
|
|
|
|
|
|
|
|
for i in xrange(info['grains']):
|
2013-04-12 15:57:05 +05:30
|
|
|
coords[:,i] = map(float,content[i].split()[:3])*info['size']
|
2012-11-06 02:49:12 +05:30
|
|
|
eulers[:,i] = map(float,content[i].split()[3:6])
|
|
|
|
|
|
|
|
# -------------------------------------- switch according to task ----------------------------------
|
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
if options.config: # write config file
|
2012-11-06 02:49:12 +05:30
|
|
|
file['output'].write('<microstructure>\n')
|
|
|
|
for i in xrange(info['grains']):
|
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i+1).zfill(formatwidth)) + \
|
2012-11-08 21:13:38 +05:30
|
|
|
'crystallite %i\n'%options.crystallite + \
|
2012-11-06 02:49:12 +05:30
|
|
|
'(constituent)\tphase %i\ttexture %s\tfraction 1.0\n'%(options.phase,str(i+1).rjust(formatwidth)))
|
|
|
|
|
|
|
|
file['output'].write('\n<texture>\n')
|
|
|
|
for i in xrange(info['grains']):
|
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i+1).zfill(formatwidth)) + \
|
|
|
|
'(gauss)\tphi1 %g\tPhi %g\tphi2 %g\tscatter 0.0\tfraction 1.0\n'%(eulers[0,i],eulers[1,i],eulers[2,i]))
|
2013-04-12 15:57:05 +05:30
|
|
|
|
|
|
|
else: # write geometry file
|
|
|
|
N = info['grid'].prod()
|
|
|
|
shift = 0.5*info['size']/info['grid'] # shift by half of side length to center of element
|
2012-11-06 02:49:12 +05:30
|
|
|
undeformed = numpy.zeros((3,N),'d')
|
|
|
|
|
|
|
|
for i in xrange(N):
|
2013-04-12 15:57:05 +05:30
|
|
|
undeformed[0,i] = info['size'][0]\
|
|
|
|
* float(i % info['grid'][0])\
|
|
|
|
/float(info['grid'][0])
|
|
|
|
undeformed[1,i] = info['size'][1]\
|
|
|
|
* float(i//info['grid'][0] % info['grid'][1])\
|
|
|
|
/float(info['grid'][1])
|
|
|
|
undeformed[2,i] = info['size'][2]\
|
|
|
|
* float(i//info['grid'][0]//info['grid'][1] % info['grid'][2])\
|
|
|
|
/float(info['grid'][2])
|
2012-11-06 02:49:12 +05:30
|
|
|
undeformed[:,i] += shift
|
|
|
|
|
2013-01-31 21:58:08 +05:30
|
|
|
indices = damask.core.math.periodicNearestNeighbor(\
|
2013-04-12 15:57:05 +05:30
|
|
|
info['size'],\
|
2013-01-31 21:58:08 +05:30
|
|
|
numpy.eye(3),\
|
2013-04-12 15:57:05 +05:30
|
|
|
undeformed,coords)//3**3 + 1 # floor division to kill periodic images
|
2012-11-06 02:49:12 +05:30
|
|
|
missing = 0
|
|
|
|
for i in xrange(info['grains']):
|
|
|
|
if i+1 not in indices: missing += 1
|
|
|
|
file['croak'].write({True:'all',False:'only'}[missing == 0] + ' %i grains mapped.\n'%(info['grains']-missing))
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2013-04-12 16:45:17 +05:30
|
|
|
new_header.append("$Id$ \n")
|
|
|
|
new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],))
|
|
|
|
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],))
|
2013-04-12 15:57:05 +05:30
|
|
|
new_header.append("microstructures\t%i\n"%(info['grains']-missing))
|
|
|
|
new_header.append("homogenization\t%i\n"%info['homogenization'])
|
2013-04-12 16:45:17 +05:30
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
file['output'].write('%i\theader\n'%(len(new_header)) + ''.join(new_header))
|
|
|
|
|
|
|
|
for n in xrange(info['grid'][1:3].prod()): # loop over 2nd and 3rd size
|
|
|
|
file['output'].write({ True: ' ',
|
|
|
|
False:'\n'}[options.twoD].\
|
|
|
|
join(map(lambda x: str(x).rjust(formatwidth),\
|
|
|
|
indices[n*info['grid'][0]:(n+1)*info['grid'][0]]))+'\n')
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
# ------------------------------------------ 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])
|
|
|
|
|