fixed small bug ('theTable' instead of 'table') and simplified

This commit is contained in:
Martin Diehl 2014-08-25 12:39:12 +00:00
parent 06988353ee
commit 1ba7cbb046
1 changed files with 44 additions and 62 deletions

View File

@ -1,45 +1,27 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,sys,math,string,re,numpy import os,re,sys,math,string
import damask import numpy as np
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP from optparse import OptionParser
import damask
scriptID = '$Id$' scriptID = '$Id$'
scriptName = scriptID.split()[1] scriptName = scriptID.split()[1]
#--------------------------------------------------------------------------------------------------
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)
def meshgrid2(*arrs): def meshgrid2(*arrs):
''' '''
code inspired by http://stackoverflow.com/questions/1827489/numpy-meshgrid-in-3d code inspired by http://stackoverflow.com/questions/1827489/np-meshgrid-in-3d
''' '''
arrs = tuple(reversed(arrs)) arrs = tuple(reversed(arrs))
arrs = tuple(arrs) arrs = tuple(arrs)
lens = numpy.array(map(len, arrs)) lens = np.array(map(len, arrs))
dim = len(arrs) dim = len(arrs)
ans = [] ans = []
for i, arr in enumerate(arrs): for i, arr in enumerate(arrs):
slc = numpy.ones(dim,'i') slc = np.ones(dim,'i')
slc[i] = lens[i] slc[i] = lens[i]
arr2 = numpy.asarray(arr).reshape(slc) arr2 = np.asarray(arr).reshape(slc)
for j, sz in enumerate(lens): for j, sz in enumerate(lens):
if j != i: if j != i:
arr2 = arr2.repeat(sz, axis=j) arr2 = arr2.repeat(sz, axis=j)
@ -48,9 +30,9 @@ def meshgrid2(*arrs):
return tuple(ans) return tuple(ans)
#-------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------
# MAIN # MAIN
#-------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------
synonyms = { synonyms = {
'grid': ['resolution'], 'grid': ['resolution'],
'size': ['dimension'], 'size': ['dimension'],
@ -68,11 +50,11 @@ mappings = {
'homogenization': lambda x: int(x), 'homogenization': lambda x: int(x),
'microstructures': lambda x: int(x), 'microstructures': lambda x: int(x),
} }
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Generate geometry description and material configuration by standard Voronoi tessellation of given seeds file. Generate geometry description and material configuration by standard Voronoi tessellation of given seeds file.
""" + string.replace(scriptID,'\n','\\n')
) """, version = scriptID)
parser.add_option('-g', '--grid', dest='grid', type='int', nargs = 3, metavar = 'int int int', \ parser.add_option('-g', '--grid', dest='grid', type='int', nargs = 3, metavar = 'int int int', \
help='a,b,c grid of hexahedral box [from seeds file]') help='a,b,c grid of hexahedral box [from seeds file]')
@ -87,8 +69,8 @@ parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'in
parser.add_option('-c', '--configuration', dest='config', action='store_true', \ parser.add_option('-c', '--configuration', dest='config', action='store_true', \
help='output material configuration [%default]') help='output material configuration [%default]')
parser.set_defaults(grid = [0,0,0]) parser.set_defaults(grid = (0,0,0))
parser.set_defaults(size = [0.0,0.0,0.0]) parser.set_defaults(size = (0.0,0.0,0.0))
parser.set_defaults(homogenization = 1) parser.set_defaults(homogenization = 1)
parser.set_defaults(phase = 1) parser.set_defaults(phase = 1)
parser.set_defaults(crystallite = 1) parser.set_defaults(crystallite = 1)
@ -119,14 +101,14 @@ for file in files:
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n') 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') else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
theTable = damask.ASCIItable(file['input'],file['output'],buffered = False) table = damask.ASCIItable(file['input'],file['output'],buffered = False)
theTable.head_read() table.head_read()
labels = ['x','y','z'] labels = ['x','y','z']
index = 0 index = 0
hasEulers = numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1 hasEulers = np.all(table.labels_index(['phi1','Phi','phi2'])) != -1
hasGrains = theTable.labels_index('microstructure') != -1 hasGrains = table.labels_index('microstructure') != -1
if hasEulers: if hasEulers:
labels += ['phi1','Phi','phi2'] labels += ['phi1','Phi','phi2']
@ -140,18 +122,18 @@ for file in files:
grainCol = index grainCol = index
theTable.data_readArray(labels) table.data_readArray(labels)
coords = theTable.data[:,0:3] coords = table.data[:,0:3]
eulers = theTable.data[:,eulerCol:eulerCol+3] if hasEulers else numpy.zeros(3*len(coords)) eulers = table.data[:,eulerCol:eulerCol+3] if hasEulers else np.zeros(3*len(coords))
grain = theTable.data[:,grainCol] if hasGrains else 1+numpy.arange(len(eulers)) grain = table.data[:,grainCol] if hasGrains else 1+np.arange(len(eulers))
grainIDs = numpy.unique(grain).astype('i') grainIDs = np.unique(grain).astype('i')
#--- interpret header ---------------------------------------------------------------------------- #--- interpret header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': np.zeros(3,'i'),
'size': numpy.array(options.size), 'size': np.array(options.size),
'origin': numpy.zeros(3,'d'), 'origin': np.zeros(3,'d'),
'microstructures': 0, 'microstructures': 0,
'homogenization': options.homogenization, 'homogenization': options.homogenization,
} }
@ -160,7 +142,7 @@ for file in files:
} }
extra_header = [] extra_header = []
for header in theTable.info: for header in table.info:
headitems = map(str.lower,header.split()) headitems = map(str.lower,header.split())
if len(headitems) == 0: continue if len(headitems) == 0: continue
for synonym,alternatives in synonyms.iteritems(): for synonym,alternatives in synonyms.iteritems():
@ -180,7 +162,7 @@ for file in files:
info['microstructures'] = len(grainIDs) info['microstructures'] = len(grainIDs)
if 0 not in options.grid: # user-specified grid if 0 not in options.grid: # user-specified grid
info['grid'] = numpy.array(options.grid) info['grid'] = np.array(options.grid)
for i in xrange(3): for i in xrange(3):
if info['size'][i] <= 0.0: # any invalid size? if info['size'][i] <= 0.0: # any invalid size?
@ -193,10 +175,10 @@ for file in files:
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \ 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
'homogenization: %i\n'%info['homogenization']) 'homogenization: %i\n'%info['homogenization'])
if numpy.any(info['grid'] < 1): if np.any(info['grid'] < 1):
file['croak'].write('invalid grid a b c.\n') file['croak'].write('invalid grid a b c.\n')
continue continue
if numpy.any(info['size'] <= 0.0): if np.any(info['size'] <= 0.0):
file['croak'].write('invalid size x y z.\n') file['croak'].write('invalid size x y z.\n')
continue continue
if info['microstructures'] == 0: if info['microstructures'] == 0:
@ -218,22 +200,22 @@ for file in files:
file['output'].write('\n<texture>\n') file['output'].write('\n<texture>\n')
for i in grainIDs: for i in grainIDs:
eulerID = numpy.nonzero(grain == i)[0][0] # find first occurrence of this grain id eulerID = np.nonzero(grain == i)[0][0] # find first occurrence of this grain id
file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \ file['output'].write('\n[Grain%s]\n'%(str(i).zfill(formatwidth)) + \
'(gauss)\tphi1 %g\tPhi %g\tphi2 %g\tscatter 0.0\tfraction 1.0\n'%(eulers[0,eulerID], '(gauss)\tphi1 %g\tPhi %g\tphi2 %g\tscatter 0.0\tfraction 1.0\n'%(eulers[0,eulerID],
eulers[1,eulerID], eulers[1,eulerID],
eulers[2,eulerID])) eulers[2,eulerID]))
else: # write geometry file else: # write geometry file
x = (numpy.arange(info['grid'][0])+0.5)*info['size'][0]/info['grid'][0] x = (np.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] y = (np.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] z = (np.arange(info['grid'][2])+0.5)*info['size'][2]/info['grid'][2]
undeformed = numpy.vstack(map(numpy.ravel, meshgrid2(x, y, z))) undeformed = np.vstack(map(np.ravel, meshgrid2(x, y, z)))
file['croak'].write('tessellating...\n') file['croak'].write('tessellating...\n')
indices = damask.core.math.periodicNearestNeighbor(\ indices = damask.core.math.periodicNearestNeighbor(\
info['size'],\ info['size'],\
numpy.eye(3),\ np.eye(3),\
undeformed,coords)//3**3 + 1 # floor division to kill periodic images undeformed,coords)//3**3 + 1 # floor division to kill periodic images
indices = grain[indices-1] indices = grain[indices-1]
@ -246,9 +228,9 @@ for file in files:
' grains mapped.\n') ' grains mapped.\n')
#--- write header --------------------------------------------------------------------------------- #--- write header ---------------------------------------------------------------------------------
theTable.labels_clear() table.labels_clear()
theTable.info_clear() table.info_clear()
theTable.info_append(extra_header+[ table.info_append(extra_header+[
scriptID + ' ' + ' '.join(sys.argv[1:]), scriptID + ' ' + ' '.join(sys.argv[1:]),
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],), "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],), "size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],),
@ -256,12 +238,12 @@ for file in files:
"homogenization\t%i"%info['homogenization'], "homogenization\t%i"%info['homogenization'],
"microstructures\t%i"%(newInfo['microstructures']), "microstructures\t%i"%(newInfo['microstructures']),
]) ])
theTable.head_write() table.head_write()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
formatwidth = 1+int(math.log10(newInfo['microstructures'])) formatwidth = 1+int(math.log10(newInfo['microstructures']))
theTable.data = indices.reshape(info['grid'][1]*info['grid'][2],info['grid'][0]) table.data = indices.reshape(info['grid'][1]*info['grid'][2],info['grid'][0])
theTable.data_writeArray('%%%ii'%(formatwidth),delimiter=' ') table.data_writeArray('%%%ii'%(formatwidth),delimiter=' ')
#--- output finalization -------------------------------------------------------------------------- #--- output finalization --------------------------------------------------------------------------