2013-06-21 01:15:25 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2013-07-02 10:02:32 +05:30
|
|
|
import os,sys,string,re,math,numpy,itertools
|
2013-06-30 19:21:21 +05:30
|
|
|
import damask
|
2013-06-21 01:15:25 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
2013-06-21 22:29:49 +05:30
|
|
|
from scipy import ndimage
|
2013-07-01 22:45:24 +05:30
|
|
|
from multiprocessing import Pool
|
2013-06-21 01:15:25 +05:30
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
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 grainCoarsenLocal(microLocal,ix,iy,iz,window):
|
2013-07-02 10:02:32 +05:30
|
|
|
interfacialEnergy = lambda A,B: 1.0
|
2013-07-03 06:18:21 +05:30
|
|
|
struc = ndimage.generate_binary_structure(3,3)
|
|
|
|
winner = numpy.where(numpy.in1d(microLocal,options.black).reshape(microLocal.shape),
|
2013-07-01 22:45:24 +05:30
|
|
|
microLocal,0) # zero out non-blacklisted microstructures
|
|
|
|
diffusedMax = (winner > 0).astype(float) # concentration of immutable microstructures
|
|
|
|
boundingSlice = ndimage.measurements.find_objects(microLocal) # bounding boxes of each distinct microstructure region
|
2013-07-03 06:18:21 +05:30
|
|
|
diffused = {}
|
|
|
|
for grain in set(numpy.unique(microLocal)) - set(options.black) - (set([0])): # all microstructures except immutable ones
|
2013-07-01 22:45:24 +05:30
|
|
|
mini = [max(0, boundingSlice[grain-1][i].start - window) for i in range(3)] # upper right of expanded bounding box
|
|
|
|
maxi = [min(microLocal.shape[i], boundingSlice[grain-1][i].stop + window) for i in range(3)] # lower left of expanded bounding box
|
2013-07-02 10:02:32 +05:30
|
|
|
microWindow = microLocal[mini[0]:maxi[0],mini[1]:maxi[1],mini[2]:maxi[2]]
|
2013-07-03 06:18:21 +05:30
|
|
|
grainCharFunc = microWindow==grain
|
|
|
|
neighbours = set(numpy.unique(microWindow[ndimage.morphology.binary_dilation(grainCharFunc,structure=struc)]))\
|
|
|
|
- set([grain]) - set(options.black) # who is on my boundary?
|
|
|
|
try: # has grain been diffused previously?
|
|
|
|
diff = diffused[grain].copy() # yes: use previously diffused grain
|
|
|
|
except:
|
|
|
|
diffused[grain] = ndimage.filters.gaussian_filter((grainCharFunc).astype(float),options.d) # no: diffuse grain with unit speed
|
|
|
|
diff = diffused[grain].copy()
|
|
|
|
if len(neighbours) == 1:
|
|
|
|
speed = interfacialEnergy(grain,neighbours.pop()) # speed proportional to interfacial energy between me and neighbour
|
|
|
|
diff = speed*diff + (1.-speed)*(grainCharFunc) # rescale diffused microstructure by speed
|
|
|
|
else:
|
|
|
|
tiny = 0.001
|
2013-07-02 10:02:32 +05:30
|
|
|
numerator = numpy.zeros(microWindow.shape)
|
|
|
|
denominator = numpy.zeros(microWindow.shape)
|
2013-07-03 06:18:21 +05:30
|
|
|
weights = {grain: diff + tiny}
|
2013-07-02 10:02:32 +05:30
|
|
|
for i in neighbours:
|
2013-07-03 06:18:21 +05:30
|
|
|
miniI = [max(0, boundingSlice[i-1][j].start - window) for j in range(3)] # bounding box of neighbour
|
|
|
|
maxiI = [min(microLocal.shape[j], boundingSlice[i-1][j].stop + window) for j in range(3)]
|
|
|
|
miniCommon = [max(mini[j], boundingSlice[i-1][j].start - window) for j in range(3)] # intersection of mine and neighbouring bounding box
|
|
|
|
maxiCommon = [min(maxi[j], boundingSlice[i-1][j].stop + window) for j in range(3)]
|
|
|
|
weights[i] = tiny*numpy.ones(microWindow.shape)
|
|
|
|
try: # has neighbouring grain been diffused previously?
|
|
|
|
weights[i][miniCommon[0] - mini[0]:maxiCommon[0] - mini[0],\
|
|
|
|
miniCommon[1] - mini[1]:maxiCommon[1] - mini[1],\
|
|
|
|
miniCommon[2] - mini[2]:maxiCommon[2] - mini[2]] = diffused[i][miniCommon[0] - miniI[0]:maxiCommon[0] - miniI[0],\
|
|
|
|
miniCommon[1] - miniI[1]:maxiCommon[1] - miniI[1],\
|
|
|
|
miniCommon[2] - miniI[2]:maxiCommon[2] - miniI[2]] + tiny
|
|
|
|
except: # no: diffuse neighbouring grain with unit speed
|
|
|
|
diffused[i] = ndimage.filters.gaussian_filter((microLocal[miniI[0]:maxiI[0],\
|
|
|
|
miniI[1]:maxiI[1],\
|
|
|
|
miniI[2]:maxiI[2]]==i).astype(float),options.d)
|
|
|
|
weights[i][miniCommon[0] - mini[0]:maxiCommon[0] - mini[0],\
|
|
|
|
miniCommon[1] - mini[1]:maxiCommon[1] - mini[1],\
|
|
|
|
miniCommon[2] - mini[2]:maxiCommon[2] - mini[2]] = diffused[i][miniCommon[0] - miniI[0]:maxiCommon[0] - miniI[0],\
|
|
|
|
miniCommon[1] - miniI[1]:maxiCommon[1] - miniI[1],\
|
|
|
|
miniCommon[2] - miniI[2]:maxiCommon[2] - miniI[2]] + tiny
|
|
|
|
for grainA,grainB in itertools.combinations(neighbours,2): # combinations of possible triple junctions
|
2013-07-02 10:02:32 +05:30
|
|
|
speed = interfacialEnergy(grain,grainA) +\
|
|
|
|
interfacialEnergy(grain,grainB) -\
|
|
|
|
interfacialEnergy(grainA,grainB) # speed of the triple junction
|
2013-07-03 06:18:21 +05:30
|
|
|
weight = weights[grainA] * weights[grainB]
|
|
|
|
if numpy.any(weight > 0.01): # strongly interacting triple junction?
|
|
|
|
weight *= weights[grain]
|
|
|
|
numerator += weight*(speed*diff + (1.-speed)*(grainCharFunc))
|
|
|
|
denominator += weight
|
2013-07-02 10:02:32 +05:30
|
|
|
|
2013-07-03 06:18:21 +05:30
|
|
|
diff = numerator/denominator # weighted sum of strongly interacting triple junctions
|
2013-07-02 10:02:32 +05:30
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
winner[mini[0]:maxi[0],\
|
|
|
|
mini[1]:maxi[1],\
|
2013-07-03 06:18:21 +05:30
|
|
|
mini[2]:maxi[2]][diff > diffusedMax[mini[0]:maxi[0],\
|
|
|
|
mini[1]:maxi[1],\
|
|
|
|
mini[2]:maxi[2]]] = grain # remember me ...
|
2013-07-01 22:45:24 +05:30
|
|
|
diffusedMax[mini[0]:maxi[0],\
|
|
|
|
mini[1]:maxi[1],\
|
2013-07-03 06:18:21 +05:30
|
|
|
mini[2]:maxi[2]] = numpy.maximum(diff,diffusedMax[mini[0]:maxi[0],\
|
|
|
|
mini[1]:maxi[1],\
|
|
|
|
mini[2]:maxi[2]]) # ... and my concentration
|
2013-07-01 22:45:24 +05:30
|
|
|
|
|
|
|
return [winner[window:-window,window:-window,window:-window],ix,iy,iz]
|
|
|
|
|
|
|
|
def log_result(result):
|
|
|
|
ix = result[1]; iy = result[2]; iz = result[3]
|
|
|
|
microstructure[ix*stride[0]:(ix+1)*stride[0],iy*stride[1]:(iy+1)*stride[1],iz*stride[2]:(iz+1)*stride[2]] = \
|
|
|
|
result[0]
|
|
|
|
|
2013-06-21 01:15:25 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2013-06-30 19:21:21 +05:30
|
|
|
synonyms = {
|
|
|
|
'grid': ['resolution'],
|
|
|
|
'size': ['dimension'],
|
|
|
|
}
|
2013-06-21 01:15:25 +05:30
|
|
|
identifiers = {
|
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
|
|
|
}
|
|
|
|
mappings = {
|
2013-06-30 19:21:21 +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),
|
2013-06-21 01:15:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
|
2013-06-22 00:38:20 +05:30
|
|
|
Smoothens out interface roughness by simulated curvature flow.
|
2013-06-30 19:21:21 +05:30
|
|
|
This is achieved by the diffusion of each initially sharply bounded grain volume within the periodic domain
|
|
|
|
up to a given distance 'd' voxels.
|
|
|
|
The final geometry is assembled by selecting at each voxel that grain index for which the concentration remains largest.
|
2013-06-21 01:15:25 +05:30
|
|
|
""" + string.replace('$Id$','\n','\\n')
|
|
|
|
)
|
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
parser.add_option('-d', '--distance', dest='d', type='int', \
|
2013-06-30 19:21:21 +05:30
|
|
|
help='diffusion distance in voxels [%default]', metavar='float')
|
2013-07-01 22:45:24 +05:30
|
|
|
parser.add_option('-N', '--smooth', dest='N', type='int', \
|
|
|
|
help='N for curvature flow [%default]')
|
|
|
|
parser.add_option('-p', '--processors', dest='p', type='int', nargs = 3, \
|
|
|
|
help='number of threads in x,y,z direction')
|
2013-06-22 02:47:03 +05:30
|
|
|
parser.add_option('-b', '--black', dest='black', action='extend', type='string', \
|
2013-07-01 22:45:24 +05:30
|
|
|
help='indices of stationary microstructures', metavar='<LIST>')
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
parser.set_defaults(d = 1)
|
|
|
|
parser.set_defaults(N = 1)
|
|
|
|
parser.set_defaults(p = [1,1,1])
|
2013-06-22 02:47:03 +05:30
|
|
|
parser.set_defaults(black = [])
|
2013-06-21 01:15:25 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2013-06-22 02:47:03 +05:30
|
|
|
options.black = map(int,options.black)
|
|
|
|
|
2013-06-21 01:15:25 +05:30
|
|
|
#--- setup file handles --------------------------------------------------------------------------
|
|
|
|
files = []
|
|
|
|
if filenames == []:
|
2013-07-01 22:45:24 +05:30
|
|
|
files.append({'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'output':sys.stdout,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
})
|
2013-06-21 01:15:25 +05:30
|
|
|
else:
|
2013-07-01 22:45:24 +05:30
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name,
|
|
|
|
'input':open(name),
|
|
|
|
'output':open(name+'_tmp','w'),
|
|
|
|
'croak':sys.stdout,
|
|
|
|
})
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
2013-06-21 01:15:25 +05:30
|
|
|
for file in files:
|
|
|
|
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
|
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
theTable = damask.ASCIItable(file['input'],file['output'],labels=False)
|
|
|
|
theTable.head_read()
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
#--- interpret header ----------------------------------------------------------------------------
|
2013-06-21 01:15:25 +05:30
|
|
|
info = {
|
2013-06-30 19:21:21 +05:30
|
|
|
'grid': numpy.zeros(3,'i'),
|
|
|
|
'size': numpy.zeros(3,'d'),
|
|
|
|
'origin': numpy.zeros(3,'d'),
|
2013-06-21 01:15:25 +05:30
|
|
|
'homogenization': 0,
|
2013-06-30 19:21:21 +05:30
|
|
|
'microstructures': 0,
|
|
|
|
}
|
|
|
|
newInfo = {
|
|
|
|
'microstructures': 0,
|
2013-06-21 01:15:25 +05:30
|
|
|
}
|
2013-06-30 19:21:21 +05:30
|
|
|
extra_header = []
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
for header in theTable.info:
|
2013-06-21 01:15:25 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
2013-06-30 19:21:21 +05:30
|
|
|
if len(headitems) == 0: continue
|
|
|
|
for synonym,alternatives in synonyms.iteritems():
|
|
|
|
if headitems[0] in alternatives: headitems[0] = synonym
|
2013-06-21 01:15:25 +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-06-30 19:21:21 +05:30
|
|
|
else:
|
|
|
|
extra_header.append(header)
|
2013-06-21 01:15:25 +05:30
|
|
|
|
|
|
|
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']))) + \
|
|
|
|
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
|
|
|
|
'homogenization: %i\n'%info['homogenization'] + \
|
|
|
|
'microstructures: %i\n'%info['microstructures'])
|
|
|
|
|
|
|
|
if numpy.any(info['grid'] < 1):
|
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-06-30 19:21:21 +05:30
|
|
|
continue
|
2013-06-21 01:15:25 +05:30
|
|
|
if numpy.any(info['size'] <= 0.0):
|
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2013-06-30 19:21:21 +05:30
|
|
|
continue
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
#--- read data ------------------------------------------------------------------------------------
|
|
|
|
microstructure = numpy.zeros(info['grid'].prod(),'i')
|
2013-06-21 01:15:25 +05:30
|
|
|
i = 0
|
2013-06-30 19:21:21 +05:30
|
|
|
theTable.data_rewind()
|
|
|
|
while theTable.data_read():
|
|
|
|
items = theTable.data
|
2013-06-22 02:47:03 +05:30
|
|
|
if len(items) > 2:
|
|
|
|
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
|
|
|
|
elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2]))
|
2013-06-30 19:21:21 +05:30
|
|
|
else: items = map(int,items)
|
|
|
|
else: items = map(int,items)
|
2013-06-22 02:47:03 +05:30
|
|
|
|
2013-06-30 19:21:21 +05:30
|
|
|
s = len(items)
|
|
|
|
microstructure[i:i+s] = items
|
|
|
|
i += s
|
2013-06-21 01:15:25 +05:30
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
#--- do work -------------------------------------------------------------------------------------
|
2013-06-30 19:21:21 +05:30
|
|
|
microstructure = microstructure.reshape(info['grid'],order='F')
|
2013-06-22 00:38:20 +05:30
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
#--- domain decomposition -------------------------------------------------------------------------
|
|
|
|
numProc = int(options.p[0]*options.p[1]*options.p[2])
|
|
|
|
stride = info['grid']/options.p
|
|
|
|
if numpy.any(numpy.floor(stride) != stride):
|
|
|
|
file['croak'].write('invalid domain decomposition.\n')
|
|
|
|
continue
|
2013-06-30 19:21:21 +05:30
|
|
|
|
2013-07-01 22:45:24 +05:30
|
|
|
#--- initialize helper data -----------------------------------------------------------------------
|
|
|
|
window = 4*options.d
|
|
|
|
for smoothIter in xrange(options.N):
|
|
|
|
extendedMicro = numpy.tile(microstructure,[3,3,3])
|
|
|
|
extendedMicro = extendedMicro[(info['grid'][0]-window):-(info['grid'][0]-window),
|
|
|
|
(info['grid'][1]-window):-(info['grid'][1]-window),
|
|
|
|
(info['grid'][2]-window):-(info['grid'][2]-window)]
|
|
|
|
pool = Pool(processes=numProc)
|
|
|
|
for iz in xrange(options.p[2]):
|
|
|
|
for iy in xrange(options.p[1]):
|
|
|
|
for ix in xrange(options.p[0]):
|
|
|
|
pool.apply_async(grainCoarsenLocal,(extendedMicro[ix*stride[0]:(ix+1)*stride[0]+2*window,\
|
|
|
|
iy*stride[1]:(iy+1)*stride[1]+2*window,\
|
|
|
|
iz*stride[2]:(iz+1)*stride[2]+2*window],\
|
|
|
|
ix,iy,iz,window), callback=log_result)
|
|
|
|
|
|
|
|
pool.close()
|
|
|
|
pool.join()
|
|
|
|
|
|
|
|
# --- assemble header -----------------------------------------------------------------------------
|
|
|
|
newInfo['microstructures'] = microstructure.max()
|
2013-06-30 19:21:21 +05:30
|
|
|
|
|
|
|
#--- report ---------------------------------------------------------------------------------------
|
|
|
|
if (newInfo['microstructures'] != info['microstructures']):
|
|
|
|
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
|
|
|
|
|
|
|
|
#--- write header ---------------------------------------------------------------------------------
|
|
|
|
theTable.labels_clear()
|
|
|
|
theTable.info_clear()
|
|
|
|
theTable.info_append(extra_header+[
|
|
|
|
"$Id$",
|
|
|
|
"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'],
|
|
|
|
"microstructures\t%i"%(newInfo['microstructures']),
|
|
|
|
])
|
|
|
|
theTable.head_write()
|
|
|
|
theTable.output_flush()
|
|
|
|
|
2013-06-21 01:15:25 +05:30
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
2013-07-01 22:45:24 +05:30
|
|
|
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
|
|
|
theTable.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
2013-06-30 19:21:21 +05:30
|
|
|
theTable.data_writeArray('%%%ii'%(formatwidth))
|
|
|
|
|
2013-06-21 01:15:25 +05:30
|
|
|
#--- output finalization --------------------------------------------------------------------------
|
|
|
|
if file['name'] != 'STDIN':
|
2013-06-30 19:21:21 +05:30
|
|
|
file['input'].close()
|
2013-06-21 01:15:25 +05:30
|
|
|
file['output'].close()
|
|
|
|
os.rename(file['name']+'_tmp',file['name'])
|