improved search for double microstructures (was N^2 bogosort) in geom_fromAng and introduced it in geom_fromVPSC

This commit is contained in:
Martin Diehl 2015-04-10 12:57:25 +00:00
parent b08141d2b3
commit e01e270300
2 changed files with 135 additions and 104 deletions

View File

@ -19,23 +19,24 @@ Two phases can be discriminated based on threshold value in a given data column.
""", version = scriptID) """, version = scriptID)
parser.add_option('--column', dest='column', type='int', metavar = 'int', \ parser.add_option('--column', dest='column', type='int', metavar = 'int',
help='data column to discriminate between both phases [%default]') help='data column to discriminate between both phases [%default]')
parser.add_option('-t','--threshold', dest='threshold', type='float', metavar = 'float', \ parser.add_option('-t','--threshold', dest='threshold', type='float', metavar = 'float',
help='threshold value for phase discrimination [%default]') help='threshold value for phase discrimination [%default]')
parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int', \ parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int',
help='homogenization index for <microstructure> configuration [%default]') help='homogenization index for <microstructure> configuration [%default]')
parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int int', \ parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int int',
help='phase indices for <microstructure> configuration %default') help='phase indices for <microstructure> configuration %default')
parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int', \ parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int',
help='crystallite index for <microstructure> configuration [%default]') help='crystallite index for <microstructure> configuration [%default]')
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.add_option('--compress', dest='compress', action='store_true', \ parser.add_option('--compress', dest='compress', action='store_true',
help='lump identical microstructure and texture information [%default]') help='lump identical microstructure and texture information [%default]')
parser.add_option('-a', '--axes', dest='axes', nargs = 3, metavar = 'string string string', \ parser.add_option('-a', '--axes', dest='axes', nargs = 3, metavar = 'string string string',
help='Euler angle coordinate system for <texture> configuration x,y,z = %default') help='Euler angle coordinate system for <texture> configuration x,y,z = %default')
parser.add_option('-p', '--precision', dest='precision', choices=['0','1','2','3'], metavar = 'int',
help = 'euler angles decimal places for output format and compressing (0,1,2,3) [2]')
parser.set_defaults(column = 11) parser.set_defaults(column = 11)
parser.set_defaults(threshold = 0.5) parser.set_defaults(threshold = 0.5)
@ -45,8 +46,10 @@ parser.set_defaults(crystallite = 1)
parser.set_defaults(config = False) parser.set_defaults(config = False)
parser.set_defaults(compress = False) parser.set_defaults(compress = False)
parser.set_defaults(axes = ['y','x','-z']) parser.set_defaults(axes = ['y','x','-z'])
parser.set_defaults(precision = '2')
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
for i in options.axes: for i in options.axes:
if i.lower() not in ['x','+x','-x','y','+y','-y','z','+z','-z']: if i.lower() not in ['x','+x','-x','y','+y','-y','z','+z','-z']:
parser.error('invalid axes %s %s %s' %(options.axes[0],options.axes[1],options.axes[2])) parser.error('invalid axes %s %s %s' %(options.axes[0],options.axes[1],options.axes[2]))
@ -68,7 +71,6 @@ else:
'croak':sys.stdout, 'croak':sys.stdout,
}) })
#--- loop over input files ------------------------------------------------------------------------ #--- loop over input files ------------------------------------------------------------------------
for file in files: for file in files:
file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n') file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
@ -86,78 +88,70 @@ for file in files:
for line in file['input']: for line in file['input']:
words = line.split() words = line.split()
if len(words) == 0: continue # ignore empty lines if len(words) == 0: continue # ignore empty lines
if words[0] == '#': # process initial comments block if words[0] == '#': # process initial comments/header block
if len(words) > 2: if len(words) > 2:
if words[2].lower() == 'hexgrid': if words[2].lower() == 'hexgrid':
file['croak'].write('The file has HexGrid format. Please first convert to SquareGrid...\n') file['croak'].write('The file has HexGrid format. Please first convert to SquareGrid...\n')
break break
if words[1] == 'XSTEP:': step[0] = float(words[2]) if words[1] == 'XSTEP:': step[0] = float(words[2])
if words[1] == 'YSTEP:': step[1] = float(words[2]) if words[1] == 'YSTEP:': step[1] = float(words[2])
if words[1] == 'NCOLS_ODD:': if words[1] == 'NCOLS_ODD:': # ignore order of NROWS/NCOLS
info['grid'][0] = int(words[2]) info['grid'][0] = int(words[2])
eulerangles = np.zeros((info['grid'][0]*info['grid'][1],3),dtype='f') eulerangles = np.empty((info['grid'].prod(),3),dtype='f')
phase = np.zeros(info['grid'][0]*info['grid'][1],dtype='i') phase = np.empty(info['grid'].prod(),dtype='i')
if words[1] == 'NROWS:': if words[1] == 'NROWS:': # ignore order of NROWS/NCOLS
info['grid'][1] = int(words[2]) info['grid'][1] = int(words[2])
eulerangles = np.zeros((info['grid'][0]*info['grid'][1],3),dtype='f') eulerangles = np.empty((info['grid'].prod(),3),dtype='f')
phase = np.zeros(info['grid'][0]*info['grid'][1],dtype='i') phase = np.empty(info['grid'].prod(),dtype='i')
else: # finished with comments block else: # finished with comments block
phase[point] = options.phase[int(float(words[options.column-1]) > options.threshold)] phase[point] = options.phase[int(float(words[options.column-1]) > options.threshold)]
eulerangles[point,...] = map(lambda x: math.degrees(float(x)), words[:3]) eulerangles[point,...] = map(lambda x: math.degrees(float(x)), words[:3])
point += 1 point += 1
if info['grid'].prod() != point: if info['grid'].prod() != point:
file['croak'].write('Error: found %s microstructures. Header info in ang file might be wrong.\n'%point) file['croak'].write('Error: found %s microstructures. Header info in ang file might be wrong.\n'%point)
continue continue
if np.any(np.max(eulerangles[0,:])>=360) or np.any(np.max(eulerangles[1,:])>=180) or np.any(np.max(eulerangles[2,:])>=360):
file['croak'].write('Error: euler angles out of bound. Ang file might contain unidexed poins.\n')
#continue
if options.compress: if options.compress:
texture = [] formatString='{0:0>'+str(int(options.precision)+3)+'}'
microstructure = [] euleranglesRadInt = (eulerangles*10**int(options.precision)).astype('int') # scale by desired precision and convert to int
otherPoint = -1 # ensure to create first microstructure eulerKeys = np.array([int(''.join(map(formatString.format,euleranglesRadInt[i,:]))) \
matPoints = np.zeros(info['grid'].prod(),dtype='i') # index of microstructure in geom file for i in xrange(info['grid'].prod())]) # create unique integer key from three euler angles by concatenating the string representation with leading zeros and store as integer
for myPoint in xrange(info['grid'].prod()): devNull, texture, eulerKeys_idx = np.unique(eulerKeys, return_index = True, return_inverse=True)# search unique euler angle keys. Texture IDs are the indices of the first occurence, the inverse is used to construct the microstructure
myTexture = -1 msFull = np.array([[eulerKeys_idx[i],phase[i]] for i in xrange(info['grid'].prod())],'i8') # create a microstructure (texture/phase pair) for each point using unique texture IDs. Use longInt (64bit, i8) because the keys might be long
for otherPoint in xrange(len(microstructure)): devNull,msUnique,matPoints = np.unique(msFull.view('c16'),True,True)
otherEulers = eulerangles[texture[microstructure[otherPoint][0]]] matPoints+=1
otherPhase = microstructure[otherPoint][1] microstructure = np.array([msFull[i] for i in msUnique]) # pick only unique microstructures
if all(abs(eulerangles[myPoint]-otherEulers)<1e-6) and phase[myPoint] == otherPhase: # common microstructure
matPoints[myPoint] = otherPoint+1 # use other point's microstructure, +1 because starting with 1 (.config) instead of 0 (python)
otherPoint = -2 # never create new microstructure
break
elif all(eulerangles[myPoint] == otherEulers): # found common texture and store it
myTexture = microstructure[otherPoint][0]
if otherPoint == len(microstructure)-1: # did not found matching microstructure
if myTexture == -1: # did not even found matching texture
myTexture = len(texture)
texture.append(myPoint)
microstructure.append([myTexture,phase[myPoint]])
matPoints[myPoint] = len(microstructure) # use the new microstructure
else: else:
texture = [i for i in xrange(info['grid'][0]*info['grid'][1])] texture = np.arange(info['grid'].prod())
microstructure = [[i,phase[i]] for i in xrange(info['grid'][0]*info['grid'][1])] microstructure = np.hstack( zip(texture,phase) ).reshape(info['grid'].prod(),2) # create texture/phase pairs
formatOut = 1+int(math.log10(len(texture))) formatOut = 1+int(math.log10(len(texture)))
textureOut =['\n\n<texture>'] textureOut =['\n\n<texture>']
eulerFormatOut='%%%i.%if'%(int(options.precision)+4,int(options.precision))
outStringAngles='(gauss) phi1 '+eulerFormatOut+' Phi '+eulerFormatOut+' phi2 '+eulerFormatOut+' scatter 0.0 fraction 1.0\n'
for i in xrange(len(texture)): for i in xrange(len(texture)):
textureOut += ['[Texture%s]\n'%str(texture[i]+1).zfill(formatOut) + \ textureOut += ['[Texture%s]\n'%str(i+1).zfill(formatOut) +
'axes %s %s %s\n'%(options.axes[0],options.axes[1],options.axes[2]) +\ 'axes %s %s %s\n'%(options.axes[0],options.axes[1],options.axes[2]) +
'(gauss)\tphi1 %4.2f\tPhi %4.2f\tphi2 %4.2f\tscatter 0.0\tfraction 1.0\n'%tuple(eulerangles[texture[i],...]) outStringAngles%tuple(eulerangles[texture[i],...])
] ]
formatOut = 1+int(math.log10(len(microstructure))) formatOut = 1+int(math.log10(len(microstructure)))
microstructureOut =['<microstructure>'] microstructureOut =['<microstructure>']
for i in xrange(len(microstructure)): for i in xrange(len(microstructure)):
microstructureOut += ['[Grain%s]\n'%str(i+1).zfill(formatOut) + \ microstructureOut += ['[Grain%s]\n'%str(i+1).zfill(formatOut) +
'crystallite\t%i\n'%options.crystallite + \ 'crystallite\t%i\n'%options.crystallite +
'(constituent)\tphase %i\ttexture %i\tfraction 1.0\n'%(microstructure[i][1],microstructure[i][0]+1) '(constituent)\tphase %i\ttexture %i\tfraction 1.0\n'%(microstructure[i,1],microstructure[i,0]+1)
] ]
info['microstructures'] = len(microstructure) info['microstructures'] = len(microstructure)
info['size'] = step[0]*info['grid'][0],step[1]*info['grid'][1],min(step) info['size'] = step[0]*info['grid'][0],step[1]*info['grid'][1],min(step)
#--- report --------------------------------------------------------------------------------------- #--- report ---------------------------------------------------------------------------------------
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ 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']))) + \ 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) +
'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'] +
'microstructures: %i\n\n'%info['microstructures']) 'microstructures: %i\n\n'%info['microstructures'])
if np.any(info['grid'] < 1): if np.any(info['grid'] < 1):

View File

@ -13,29 +13,35 @@ scriptName = os.path.splitext(scriptID.split()[1])[0]
# MAIN # MAIN
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Generate geometry description and material configuration from input files used by R.A. Lebensohn Generate geometry description and material configuration from input files used by R.A. Lebensohn.
""", version = scriptID) """, version = scriptID)
parser.add_option('--column', dest='column', type='int', metavar = 'int', \ parser.add_option('--column', dest='column', type='int', metavar = 'int',
help='data column to discriminate phase 1 from 2 [%default]') help='data column to discriminate between both phases [%default]')
parser.add_option('-t','--treshold', dest='threshold', type='float', metavar = 'float', \ parser.add_option('-t','--threshold', dest='threshold', type='float', metavar = 'float',
help='threshold value to discriminate phase 1 from 2 [%default]') help='threshold value for phase discrimination [%default]')
parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int', \ parser.add_option('--homogenization', dest='homogenization', type='int', metavar = 'int',
help='homogenization index to be used [%default]') help='homogenization index for <microstructure> configuration [%default]')
parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int int', \ parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int int',
help='two phase indices to be used %default') help='phase indices for <microstructure> configuration %default')
parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int', \ parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int',
help='crystallite index to be used [%default]') help='crystallite index for <microstructure> configuration [%default]')
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.add_option('--compress', dest='compress', action='store_true',
help='lump identical microstructure and texture information [%default]')
parser.add_option('-p', '--precision', dest='precision', choices=['0','1','2','3'], metavar = 'int',
help = 'euler angles decimal places for output format and compressing (0,1,2,3) [2]')
parser.set_defaults(column = 7) parser.set_defaults(column = 7)
parser.set_defaults(threshold = 1.0) parser.set_defaults(threshold = 1.0)
parser.set_defaults(homogenization = 1) parser.set_defaults(homogenization = 1)
parser.set_defaults(phase = [1,2]) parser.set_defaults(phase = [1,2])
parser.set_defaults(crystallite = 1) parser.set_defaults(crystallite = 1)
parser.set_defaults(config = False) parser.set_defaults(config = False)
parser.set_defaults(compress = False)
parser.set_defaults(precision = '2')
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
@ -69,62 +75,93 @@ for file in files:
'homogenization': options.homogenization 'homogenization': options.homogenization
} }
needInfo = [True,True,True] phase = []
microstructure = ['<microstructure>'] eulerangles = []
texture = ['<texture>']
point = 0 point = 0
for line in file['input']: for line in file['input']:
if line.strip(): if line.strip():
point += 1
words = line.split() words = line.split()
currPos = map(float,words[3:6]) currPos = map(float,words[3:6])
for i in xrange(3): for i in xrange(3):
if currPos[i] > info['grid'][i]: if currPos[i] > info['grid'][i]:
info['size'][i] = currPos[i] info['size'][i] = currPos[i]
info['grid'][i]+=1 info['grid'][i]+=1
if options.config: # write configuration (line by line) eulerangles.append(map(float,words[:3]))
me = str(point) phase.append(options.phase[int(float(words[options.column-1]) > options.threshold)])
microstructure += ['[Grain%s]\n'%me + \
'crystallite\t%i\n'%options.crystallite + \ eulerangles = np.array(eulerangles,dtype='f').reshape(info['grid'].prod(),3)
'(constituent)\tphase %s\ttexture %s\tfraction 1.0\n'%(options.phase[{True:0,False:1}[float(words[options.column-1])<options.threshold]],me) phase = np.array(phase,dtype='i').reshape(info['grid'].prod())
]
texture += ['[Grain%s]\n'%me + \ if np.any(np.max(eulerangles[0,:])>=360) or np.any(np.max(eulerangles[1,:])>=180) or np.any(np.max(eulerangles[2,:])>=360):
'(gauss)\tphi1 %s\tPhi %s\tphi2 %s\tscatter 0.0\tfraction 1.0\n'%tuple(words[:3]) file['croak'].write('Error: euler angles out of bounds.\n')
] continue
info['microstructures'] = info['grid'][0]*info['grid'][1]*info['grid'][2] if options.compress:
formatString='{0:0>'+str(int(options.precision)+3)+'}'
euleranglesRadInt = (eulerangles*10**int(options.precision)).astype('int') # scale by desired precision and convert to int
eulerKeys = np.array([int(''.join(map(formatString.format,euleranglesRadInt[i,:]))) \
for i in xrange(info['grid'].prod())]) # create unique integer key from three euler angles by concatenating the string representation with leading zeros and store as integer
devNull, texture, eulerKeys_idx = np.unique(eulerKeys, return_index = True, return_inverse=True)# search unique euler angle keys. Texture IDs are the indices of the first occurence, the inverse is used to construct the microstructure
msFull = np.array([[eulerKeys_idx[i],phase[i]] for i in xrange(info['grid'].prod())],'i8') # create a microstructure (texture/phase pair) for each point using unique texture IDs. Use longInt (64bit, i8) because the keys might be long
devNull,msUnique,matPoints = np.unique(msFull.view('c16'),True,True)
matPoints+=1
microstructure = np.array([msFull[i] for i in msUnique]) # pick only unique microstructures
else:
texture = np.arange(info['grid'].prod())
microstructure = np.hstack( zip(texture,phase) ).reshape(info['grid'].prod(),2) # create texture/phase pairs
formatOut = 1+int(math.log10(len(texture)))
textureOut =['\n\n<texture>']
eulerFormatOut='%%%i.%if'%(int(options.precision)+4,int(options.precision))
outStringAngles='(gauss) phi1 '+eulerFormatOut+' Phi '+eulerFormatOut+' phi2 '+eulerFormatOut+' scatter 0.0 fraction 1.0\n'
for i in xrange(len(texture)):
textureOut += ['[Texture%s]\n'%str(i+1).zfill(formatOut) +
outStringAngles%tuple(eulerangles[texture[i],...])
]
formatOut = 1+int(math.log10(len(microstructure)))
microstructureOut =['<microstructure>']
for i in xrange(len(microstructure)):
microstructureOut += ['[Grain%s]\n'%str(i+1).zfill(formatOut) +
'crystallite\t%i\n'%options.crystallite +
'(constituent)\tphase %i\ttexture %i\tfraction 1.0\n'%(microstructure[i,1],microstructure[i,0]+1)
]
info['microstructures'] = len(microstructure)
#--- report --------------------------------------------------------------------------------------- #--- report ---------------------------------------------------------------------------------------
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ 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']))) + \ 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) +
'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'] +
'microstructures: %i\n\n'%info['microstructures']) 'microstructures: %i\n\n'%info['microstructures'])
if np.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')
sys.exit() continue
if np.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')
sys.exit() continue
#--- write data ----------------------------------------------------------------------------------- #--- write data -----------------------------------------------------------------------------------
if options.config: if options.config:
file['output'].write('\n'.join(microstructure) + \ file['output'].write('\n'.join(microstructureOut+ textureOut) + '\n')
'\n'.join(texture))
else: else:
header = [scriptID + ' ' + ' '.join(sys.argv[1:])+'\n'] header = [' '.join([scriptID] + sys.argv[1:]),
header.append("grid\ta %i\tb %i\tc %i\n"%(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],),
header.append("size\tx %f\ty %f\tz %f\n"%(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],),
header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2],)) "origin\tx %f\ty %f\tz %f"%(info['origin'][0],info['origin'][1],info['origin'][2],),
header.append("microstructures\t%i\n"%info['microstructures']) "microstructures\t%i"%info['microstructures'],
header.append("homogenization\t%i\n"%info['homogenization']) "homogenization\t%i"%info['homogenization'],
file['output'].write('%i\theader\n'%(len(header))+''.join(header)) ]
file['output'].write("1 to %i\n"%(info['microstructures'])) file['output'].write('\n'.join(['%i\theader'%(len(header))] + header) + '\n')
if options.compress:
#--- output finalization -------------------------------------------------------------------------- matPoints = matPoints.reshape((info['grid'][1],info['grid'][0]))
np.savetxt(file['output'],matPoints,fmt='%0'+str(1+int(math.log10(np.amax(matPoints))))+'d')
else:
file['output'].write("1 to %i\n"%(info['microstructures']))
#--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['output'].close() file['output'].close()
os.rename(file['name']+'_tmp',os.path.splitext(file['name'])[0] + \ os.rename(file['name']+'_tmp',
{True: '_material.config', os.path.splitext(file['name'])[0] +'%s'%('_material.config' if options.config else '.geom'))
False:'.geom'}[options.config])