2012-11-06 02:49:12 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:59:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2013-06-30 06:16:52 +05:30
|
|
|
import os,sys,math,string,re,numpy
|
2013-06-30 06:00:06 +05:30
|
|
|
import damask
|
2012-11-06 02:49:12 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
2013-07-10 14:42:00 +05:30
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
class extendedOption(Option):
|
2013-05-15 21:32:38 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +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-06-30 06:00:06 +05:30
|
|
|
def meshgrid2(*arrs):
|
|
|
|
'''
|
|
|
|
code inspired by http://stackoverflow.com/questions/1827489/numpy-meshgrid-in-3d
|
|
|
|
'''
|
|
|
|
arrs = tuple(reversed(arrs))
|
|
|
|
arrs = tuple(arrs)
|
|
|
|
lens = numpy.array(map(len, arrs))
|
|
|
|
dim = len(arrs)
|
|
|
|
ans = []
|
|
|
|
for i, arr in enumerate(arrs):
|
|
|
|
slc = numpy.ones(dim,'i')
|
|
|
|
slc[i] = lens[i]
|
|
|
|
arr2 = numpy.asarray(arr).reshape(slc)
|
|
|
|
for j, sz in enumerate(lens):
|
|
|
|
if j != i:
|
|
|
|
arr2 = arr2.repeat(sz, axis=j)
|
|
|
|
|
|
|
|
ans.insert(0,arr2)
|
|
|
|
return tuple(ans)
|
|
|
|
|
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
# MAIN
|
2013-05-15 21:32:38 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2013-07-10 14:42:00 +05:30
|
|
|
synonyms = {
|
2013-10-15 15:20:20 +05:30
|
|
|
'grid': ['resolution'],
|
|
|
|
'size': ['dimension'],
|
|
|
|
'microstructures': ['grains'],
|
2013-07-10 14:42:00 +05:30
|
|
|
}
|
2012-11-06 02:49:12 +05:30
|
|
|
identifiers = {
|
2013-07-10 14:42:00 +05:30
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
2013-07-10 14:42:00 +05:30
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'size': lambda x: float(x),
|
|
|
|
'origin': lambda x: float(x),
|
|
|
|
'homogenization': lambda x: int(x),
|
|
|
|
'microstructures': 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.
|
2013-07-10 14:42:00 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
2012-11-06 02:49:12 +05:30
|
|
|
)
|
|
|
|
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('-g', '--grid', dest='grid', type='int', nargs = 3, metavar = 'int int int', \
|
2013-06-04 18:26:57 +05:30
|
|
|
help='a,b,c grid of hexahedral box [from seeds file]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('-s', '--size', dest='size', type='float', nargs = 3, metavar = 'float float float', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='x,y,z size of hexahedral box [1.0 along largest grid point number]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='homogenization index to be used [%default]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--phase', dest='phase', type='int', metavar = 'int', \
|
2013-04-12 15:57:05 +05:30
|
|
|
help='phase index to be used [%default]')
|
2014-01-20 20:11:56 +05:30
|
|
|
parser.add_option('--crystallite', dest='crystallite', type='int', metavar = '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
|
|
|
|
2013-04-12 15:57:05 +05:30
|
|
|
parser.set_defaults(grid = [0,0,0])
|
2014-01-01 02:36:32 +05:30
|
|
|
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)
|
2013-06-30 06:00:06 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--- setup file handles ---------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
files = []
|
|
|
|
if filenames == []:
|
2014-01-01 02:36:32 +05:30
|
|
|
files.append({'name': 'STDIN',
|
|
|
|
'input': sys.stdin,
|
|
|
|
'output': sys.stdout,
|
|
|
|
'croak': sys.stderr,
|
2012-11-06 02:49:12 +05:30
|
|
|
})
|
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
2014-01-01 02:36:32 +05:30
|
|
|
files.append({'name': name,
|
|
|
|
'input': open(name),
|
|
|
|
'output': open(name+'_tmp','w'),
|
|
|
|
'croak': sys.stdout,
|
2012-11-06 02:49:12 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
for file in files:
|
2013-07-10 14:42:00 +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-06 02:49:12 +05:30
|
|
|
|
2014-02-04 05:14:29 +05:30
|
|
|
theTable = damask.ASCIItable(file['input'],file['output'],buffered = False)
|
2013-06-30 06:00:06 +05:30
|
|
|
theTable.head_read()
|
2014-01-01 02:36:32 +05:30
|
|
|
|
|
|
|
labels = ['x','y','z']
|
|
|
|
index = 0
|
2014-02-04 05:14:29 +05:30
|
|
|
|
|
|
|
hasEulers = numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1
|
|
|
|
hasGrains = theTable.labels_index('microstructure') != -1
|
|
|
|
|
|
|
|
if hasEulers:
|
2014-01-01 02:36:32 +05:30
|
|
|
labels += ['phi1','Phi','phi2']
|
|
|
|
index += 3
|
2014-02-04 05:14:29 +05:30
|
|
|
|
2014-01-01 02:36:32 +05:30
|
|
|
eulerCol = index
|
|
|
|
|
2014-02-04 05:14:29 +05:30
|
|
|
if hasGrains:
|
2014-01-01 02:36:32 +05:30
|
|
|
labels += ['microstructure']
|
|
|
|
index += 1
|
2014-02-04 05:14:29 +05:30
|
|
|
|
2014-01-01 02:36:32 +05:30
|
|
|
grainCol = index
|
|
|
|
|
|
|
|
theTable.data_readArray(labels)
|
|
|
|
coords = theTable.data[:,0:3]
|
|
|
|
eulers = theTable.data[:,eulerCol:eulerCol+3] if hasEulers else numpy.zeros(3*len(coords))
|
|
|
|
grain = theTable.data[:,grainCol] if hasGrains else 1+numpy.arange(len(eulers))
|
|
|
|
grainIDs = numpy.unique(grain).astype('i')
|
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-06-30 06:00:06 +05:30
|
|
|
#--- interpret header ----------------------------------------------------------------------------
|
2013-05-15 21:32:38 +05:30
|
|
|
info = {
|
|
|
|
'grid': numpy.zeros(3,'i'),
|
2013-04-12 15:57:05 +05:30
|
|
|
'size': numpy.array(options.size),
|
2013-05-15 21:32:38 +05:30
|
|
|
'origin': numpy.zeros(3,'d'),
|
2013-07-10 14:42:00 +05:30
|
|
|
'microstructures': 0,
|
2013-06-30 06:00:06 +05:30
|
|
|
'homogenization': options.homogenization,
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
2013-07-10 14:42:00 +05:30
|
|
|
newInfo = {
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
|
|
|
extra_header = []
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-06-30 06:00:06 +05:30
|
|
|
for header in theTable.info:
|
2012-11-06 02:49:12 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
2013-07-10 14:42:00 +05:30
|
|
|
if len(headitems) == 0: continue
|
|
|
|
for synonym,alternatives in synonyms.iteritems():
|
|
|
|
if headitems[0] in alternatives: headitems[0] = synonym
|
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])
|
2013-07-10 14:42:00 +05:30
|
|
|
else:
|
|
|
|
extra_header.append(header)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-07-10 14:42:00 +05:30
|
|
|
if info['microstructures'] != len(grainIDs):
|
2013-10-15 18:06:52 +05:30
|
|
|
file['croak'].write('grain data not matching grain count (%i)...\n'%(len(grainIDs)))
|
|
|
|
info['microstructures'] = len(grainIDs)
|
2013-05-15 21:32:38 +05:30
|
|
|
|
|
|
|
if 0 not in options.grid: # user-specified grid
|
2013-04-12 15:57:05 +05:30
|
|
|
info['grid'] = numpy.array(options.grid)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
|
|
|
for i in xrange(3):
|
2013-05-15 21:32:38 +05:30
|
|
|
if info['size'][i] <= 0.0: # any invalid size?
|
2013-04-12 15:57:05 +05:30
|
|
|
info['size'][i] = float(info['grid'][i])/max(info['grid'])
|
2013-06-30 19:17:01 +05:30
|
|
|
file['croak'].write('rescaling size %s...\n'%{0:'x',1:'y',2:'z'}[i])
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2013-07-10 14:42:00 +05:30
|
|
|
file['croak'].write('grains to map: %i\n'%info['microstructures'] + \
|
2013-04-12 15:57:05 +05:30
|
|
|
'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'])
|
2013-05-15 21:32:38 +05:30
|
|
|
|
|
|
|
if numpy.any(info['grid'] < 1):
|
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-06-30 06:00:06 +05:30
|
|
|
continue
|
2013-05-15 21:32:38 +05:30
|
|
|
if numpy.any(info['size'] <= 0.0):
|
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2013-06-30 06:00:06 +05:30
|
|
|
continue
|
2013-07-10 14:42:00 +05:30
|
|
|
if info['microstructures'] == 0:
|
2013-05-15 21:32:38 +05:30
|
|
|
file['croak'].write('no grain info found.\n')
|
2013-06-30 06:00:06 +05:30
|
|
|
continue
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--- prepare data ---------------------------------------------------------------------------------
|
2013-07-10 14:42:00 +05:30
|
|
|
coords = (coords*info['size']).transpose()
|
|
|
|
eulers = eulers.transpose()
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--- switch according to task ---------------------------------------------------------------------
|
|
|
|
if options.config: # write config file
|
2013-07-10 14:42:00 +05:30
|
|
|
formatwidth = 1+int(math.log10(info['microstructures']))
|
2012-11-06 02:49:12 +05:30
|
|
|
file['output'].write('<microstructure>\n')
|
2013-07-10 14:42:00 +05:30
|
|
|
for i in grainIDs:
|
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \
|
2012-11-08 21:13:38 +05:30
|
|
|
'crystallite %i\n'%options.crystallite + \
|
2013-07-10 14:42:00 +05:30
|
|
|
'(constituent)\tphase %i\ttexture %s\tfraction 1.0\n'%(options.phase,str(i).rjust(formatwidth)))
|
2012-11-06 02:49:12 +05:30
|
|
|
|
|
|
|
file['output'].write('\n<texture>\n')
|
2013-07-10 14:42:00 +05:30
|
|
|
for i in grainIDs:
|
|
|
|
eulerID = numpy.nonzero(grain == i)[0][0] # find first occurrence of this grain id
|
2013-11-13 18:21:48 +05:30
|
|
|
file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \
|
2013-11-13 18:16:20 +05:30
|
|
|
'(gauss)\tphi1 %g\tPhi %g\tphi2 %g\tscatter 0.0\tfraction 1.0\n'%(eulers[0,eulerID],
|
|
|
|
eulers[1,eulerID],
|
|
|
|
eulers[2,eulerID]))
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2013-06-30 06:00:06 +05:30
|
|
|
else: # write geometry file
|
|
|
|
x = (numpy.arange(info['grid'][0])+0.5)*info['size'][0]/info['grid'][0]
|
|
|
|
y = (numpy.arange(info['grid'][1])+0.5)*info['size'][1]/info['grid'][1]
|
|
|
|
z = (numpy.arange(info['grid'][2])+0.5)*info['size'][2]/info['grid'][2]
|
|
|
|
undeformed = numpy.vstack(map(numpy.ravel, meshgrid2(x, y, z)))
|
|
|
|
|
2013-07-10 14:42:00 +05:30
|
|
|
file['croak'].write('tessellating...\n')
|
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-05-15 21:32:38 +05:30
|
|
|
undeformed,coords)//3**3 + 1 # floor division to kill periodic images
|
2013-07-10 14:42:00 +05:30
|
|
|
indices = grain[indices-1]
|
|
|
|
|
|
|
|
newInfo['microstructures'] = info['microstructures']
|
|
|
|
for i in grainIDs:
|
|
|
|
if i not in indices: newInfo['microstructures'] -= 1
|
|
|
|
file['croak'].write({True:'all',False:'only'}[newInfo['microstructures'] == info['microstructures'] ] +
|
|
|
|
' %i'%newInfo['microstructures'] +
|
|
|
|
{True:'',False:' out of %i'%info['microstructures']}[newInfo['microstructures'] == info['microstructures']] +
|
|
|
|
' grains mapped.\n')
|
2013-05-15 21:32:38 +05:30
|
|
|
|
|
|
|
#--- write header ---------------------------------------------------------------------------------
|
2013-06-30 06:00:06 +05:30
|
|
|
theTable.labels_clear()
|
|
|
|
theTable.info_clear()
|
2013-07-10 14:42:00 +05:30
|
|
|
theTable.info_append(extra_header+[
|
2014-01-20 20:11:56 +05:30
|
|
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
2013-06-30 06:00:06 +05:30
|
|
|
"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'],
|
2013-07-10 14:42:00 +05:30
|
|
|
"microstructures\t%i"%(newInfo['microstructures']),
|
2013-06-30 06:00:06 +05:30
|
|
|
])
|
|
|
|
theTable.head_write()
|
|
|
|
|
2013-05-17 22:14:03 +05:30
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
2013-07-10 14:42:00 +05:30
|
|
|
formatwidth = 1+int(math.log10(newInfo['microstructures']))
|
2013-06-30 06:00:06 +05:30
|
|
|
theTable.data = indices.reshape(info['grid'][1]*info['grid'][2],info['grid'][0])
|
2013-12-17 13:46:29 +05:30
|
|
|
theTable.data_writeArray('%%%ii'%(formatwidth),delimiter=' ')
|
2013-06-30 06:00:06 +05:30
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
#--- output finalization --------------------------------------------------------------------------
|
2013-06-30 06:00:06 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2013-06-30 06:00:06 +05:30
|
|
|
file['input'].close()
|
2012-11-06 02:49:12 +05:30
|
|
|
file['output'].close()
|
|
|
|
os.rename(file['name']+'_tmp',os.path.splitext(file['name'])[0] + \
|
|
|
|
{True: '_material.config',
|
|
|
|
False:'.geom'}[options.config])
|