2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2013-04-12 15:57:05 +05:30
|
|
|
|
2019-05-27 02:19:05 +05:30
|
|
|
import os
|
2019-05-30 17:37:49 +05:30
|
|
|
import sys
|
2015-08-08 00:33:26 +05:30
|
|
|
import multiprocessing
|
2020-03-19 16:21:30 +05:30
|
|
|
from io import StringIO
|
2016-04-09 03:17:02 +05:30
|
|
|
from optparse import OptionParser,OptionGroup
|
2019-05-27 02:19:05 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
from scipy import spatial
|
|
|
|
|
2014-08-25 18:09:12 +05:30
|
|
|
import damask
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2019-05-30 13:14:40 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-07-10 14:42:00 +05:30
|
|
|
|
2014-10-09 22:33:06 +05:30
|
|
|
|
2020-03-19 16:21:30 +05:30
|
|
|
def Laguerre_tessellation(grid, seeds, grains, size, periodic, weights, cpus):
|
2019-05-27 02:19:05 +05:30
|
|
|
|
|
|
|
def findClosestSeed(fargs):
|
2016-07-15 19:18:53 +05:30
|
|
|
point, seeds, myWeights = fargs
|
2015-08-08 00:33:26 +05:30
|
|
|
tmp = np.repeat(point.reshape(3,1), len(seeds), axis=1).T
|
2016-07-15 19:18:53 +05:30
|
|
|
dist = np.sum((tmp - seeds)**2,axis=1) -myWeights
|
2015-08-08 00:33:26 +05:30
|
|
|
return np.argmin(dist) # seed point closest to point
|
2019-05-30 17:37:49 +05:30
|
|
|
|
2019-05-27 02:19:05 +05:30
|
|
|
copies = \
|
|
|
|
np.array([
|
|
|
|
[ -1,-1,-1 ],
|
|
|
|
[ 0,-1,-1 ],
|
|
|
|
[ 1,-1,-1 ],
|
|
|
|
[ -1, 0,-1 ],
|
|
|
|
[ 0, 0,-1 ],
|
|
|
|
[ 1, 0,-1 ],
|
|
|
|
[ -1, 1,-1 ],
|
|
|
|
[ 0, 1,-1 ],
|
|
|
|
[ 1, 1,-1 ],
|
|
|
|
[ -1,-1, 0 ],
|
|
|
|
[ 0,-1, 0 ],
|
|
|
|
[ 1,-1, 0 ],
|
|
|
|
[ -1, 0, 0 ],
|
|
|
|
[ 0, 0, 0 ],
|
|
|
|
[ 1, 0, 0 ],
|
|
|
|
[ -1, 1, 0 ],
|
|
|
|
[ 0, 1, 0 ],
|
|
|
|
[ 1, 1, 0 ],
|
|
|
|
[ -1,-1, 1 ],
|
|
|
|
[ 0,-1, 1 ],
|
|
|
|
[ 1,-1, 1 ],
|
|
|
|
[ -1, 0, 1 ],
|
|
|
|
[ 0, 0, 1 ],
|
|
|
|
[ 1, 0, 1 ],
|
|
|
|
[ -1, 1, 1 ],
|
|
|
|
[ 0, 1, 1 ],
|
|
|
|
[ 1, 1, 1 ],
|
2020-03-19 16:21:30 +05:30
|
|
|
],dtype=np.float)*size if periodic else \
|
2019-05-27 02:19:05 +05:30
|
|
|
np.array([
|
|
|
|
[ 0, 0, 0 ],
|
2020-03-19 16:21:30 +05:30
|
|
|
],dtype=np.float)
|
2019-05-27 02:19:05 +05:30
|
|
|
|
|
|
|
repeatweights = np.tile(weights,len(copies)).flatten(order='F') # Laguerre weights (1,2,3,1,2,3,...,1,2,3)
|
2020-03-19 11:19:45 +05:30
|
|
|
for vec in copies: # periodic copies of seed points ...
|
|
|
|
try: seeds = np.append(seeds, seeds+vec, axis=0) # ... (1+a,2+a,3+a,...,1+z,2+z,3+z)
|
|
|
|
except NameError: seeds = seeds+vec
|
2019-05-27 02:19:05 +05:30
|
|
|
|
2020-03-17 15:09:33 +05:30
|
|
|
damask.util.croak('...using {} cpu{}'.format(options.cpus, 's' if options.cpus > 1 else ''))
|
|
|
|
arguments = [[arg,seeds,repeatweights] for arg in list(grid)]
|
|
|
|
|
|
|
|
if cpus > 1: # use multithreading
|
|
|
|
pool = multiprocessing.Pool(processes = cpus) # initialize workers
|
|
|
|
result = pool.map_async(findClosestSeed, arguments) # evaluate function in parallel
|
|
|
|
pool.close()
|
|
|
|
pool.join()
|
|
|
|
closestSeeds = np.array(result.get()).flatten()
|
2019-05-27 02:19:05 +05:30
|
|
|
else:
|
2020-03-17 15:09:33 +05:30
|
|
|
closestSeeds = np.zeros(len(arguments),dtype='i')
|
|
|
|
for i,arg in enumerate(arguments):
|
|
|
|
closestSeeds[i] = findClosestSeed(arg)
|
2015-10-06 23:33:06 +05:30
|
|
|
|
2016-03-02 15:59:07 +05:30
|
|
|
# closestSeed is modulo number of original seed points (i.e. excluding periodic copies)
|
2020-03-19 11:19:45 +05:30
|
|
|
return grains[closestSeeds%seeds.shape[0]]
|
2013-06-30 06:00:06 +05:30
|
|
|
|
2019-05-27 02:22:23 +05:30
|
|
|
|
2020-03-19 11:19:45 +05:30
|
|
|
def Voronoi_tessellation(grid, seeds, grains, size, periodic = True):
|
2020-03-17 15:09:33 +05:30
|
|
|
|
2020-03-19 11:19:45 +05:30
|
|
|
KDTree = spatial.cKDTree(seeds,boxsize=size) if periodic else spatial.cKDTree(seeds)
|
2020-03-17 15:09:33 +05:30
|
|
|
devNull,closestSeeds = KDTree.query(grid)
|
|
|
|
return grains[closestSeeds]
|
|
|
|
|
|
|
|
|
2014-08-25 18:09:12 +05:30
|
|
|
# --------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
# MAIN
|
2014-08-25 18:09:12 +05:30
|
|
|
# --------------------------------------------------------------------
|
2014-10-09 22:33:06 +05:30
|
|
|
|
2016-05-12 12:24:34 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [seedfile(s)]', description = """
|
|
|
|
Generate geometry description and material configuration by tessellation of given seeds file.
|
2014-10-10 14:24:48 +05:30
|
|
|
|
2014-08-25 18:09:12 +05:30
|
|
|
""", version = scriptID)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2016-04-09 03:17:02 +05:30
|
|
|
|
|
|
|
group = OptionGroup(parser, "Tessellation","")
|
|
|
|
|
2016-04-24 20:39:28 +05:30
|
|
|
group.add_option('-l',
|
|
|
|
'--laguerre',
|
|
|
|
dest = 'laguerre',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'use Laguerre (weighted Voronoi) tessellation')
|
2016-04-09 03:17:02 +05:30
|
|
|
group.add_option('--cpus',
|
2016-04-24 20:39:28 +05:30
|
|
|
dest = 'cpus',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'number of parallel processes to use for Laguerre tessellation [%default]')
|
2016-04-09 03:17:02 +05:30
|
|
|
group.add_option('--nonperiodic',
|
2017-08-17 01:54:45 +05:30
|
|
|
dest = 'periodic',
|
|
|
|
action = 'store_false',
|
2016-04-24 20:39:28 +05:30
|
|
|
help = 'nonperiodic tessellation')
|
2016-04-09 03:17:02 +05:30
|
|
|
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
group = OptionGroup(parser, "Geometry","")
|
|
|
|
|
2016-04-24 20:39:28 +05:30
|
|
|
group.add_option('-g',
|
|
|
|
'--grid',
|
|
|
|
dest = 'grid',
|
|
|
|
type = 'int', nargs = 3, metavar = ' '.join(['int']*3),
|
2016-07-15 14:04:17 +05:30
|
|
|
help = 'a,b,c grid of hexahedral box')
|
2016-04-24 20:39:28 +05:30
|
|
|
group.add_option('-s',
|
|
|
|
'--size',
|
|
|
|
dest = 'size',
|
|
|
|
type = 'float', nargs = 3, metavar=' '.join(['float']*3),
|
2020-03-21 16:06:34 +05:30
|
|
|
help = 'x,y,z size of hexahedral box [1.0 1.0 1.0]')
|
2016-04-24 20:39:28 +05:30
|
|
|
group.add_option('-o',
|
|
|
|
'--origin',
|
|
|
|
dest = 'origin',
|
|
|
|
type = 'float', nargs = 3, metavar=' '.join(['float']*3),
|
2020-03-21 16:06:34 +05:30
|
|
|
help = 'origin of grid [0.0 0.0 0.0]')
|
2016-04-09 03:17:02 +05:30
|
|
|
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
group = OptionGroup(parser, "Seeds","")
|
|
|
|
|
2016-04-24 20:39:28 +05:30
|
|
|
group.add_option('-p',
|
|
|
|
'--pos', '--seedposition',
|
|
|
|
dest = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-24 20:39:28 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
|
|
|
group.add_option('-w',
|
|
|
|
'--weight',
|
|
|
|
dest = 'weight',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'label of weights [%default]')
|
|
|
|
group.add_option('-m',
|
|
|
|
'--microstructure',
|
|
|
|
dest = 'microstructure',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'label of microstructures [%default]')
|
|
|
|
group.add_option('-e',
|
|
|
|
'--eulers',
|
|
|
|
dest = 'eulers',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'label of Euler angles [%default]')
|
2016-04-09 03:17:02 +05:30
|
|
|
group.add_option('--axes',
|
2016-04-24 20:39:28 +05:30
|
|
|
dest = 'axes',
|
|
|
|
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
|
|
|
|
help = 'orientation coordinate frame in terms of position coordinate frame')
|
2016-04-09 03:17:02 +05:30
|
|
|
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
group = OptionGroup(parser, "Configuration","")
|
|
|
|
|
2016-12-24 04:16:16 +05:30
|
|
|
group.add_option('--without-config',
|
2016-12-07 08:06:25 +05:30
|
|
|
dest = 'config',
|
|
|
|
action = 'store_false',
|
|
|
|
help = 'omit material configuration header')
|
2016-04-09 03:17:02 +05:30
|
|
|
group.add_option('--homogenization',
|
2016-04-24 20:39:28 +05:30
|
|
|
dest = 'homogenization',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'homogenization index to be used [%default]')
|
2016-04-09 03:17:02 +05:30
|
|
|
group.add_option('--phase',
|
2016-04-24 20:39:28 +05:30
|
|
|
dest = 'phase',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'phase index to be used [%default]')
|
2016-04-09 03:17:02 +05:30
|
|
|
|
|
|
|
parser.add_option_group(group)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-04-24 20:39:28 +05:30
|
|
|
parser.set_defaults(pos = 'pos',
|
2015-08-08 00:33:26 +05:30
|
|
|
weight = 'weight',
|
|
|
|
microstructure = 'microstructure',
|
2016-04-24 20:39:28 +05:30
|
|
|
eulers = 'euler',
|
2015-06-27 14:11:08 +05:30
|
|
|
homogenization = 1,
|
2015-08-08 00:33:26 +05:30
|
|
|
phase = 1,
|
|
|
|
cpus = 2,
|
|
|
|
laguerre = False,
|
2017-08-17 01:54:45 +05:30
|
|
|
periodic = True,
|
2016-12-07 08:06:25 +05:30
|
|
|
config = True,
|
2015-06-27 14:11:08 +05:30
|
|
|
)
|
2019-05-30 14:15:17 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
2015-08-13 02:25:53 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2015-06-27 14:11:08 +05:30
|
|
|
for name in filenames:
|
2020-03-19 11:19:45 +05:30
|
|
|
damask.util.report(scriptName,name)
|
|
|
|
|
|
|
|
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
2020-03-21 16:06:34 +05:30
|
|
|
|
|
|
|
size = np.ones(3)
|
2020-03-19 11:19:45 +05:30
|
|
|
origin = np.zeros(3)
|
|
|
|
for line in table.comments:
|
|
|
|
items = line.lower().strip().split()
|
|
|
|
key = items[0] if items else ''
|
|
|
|
if key == 'grid':
|
|
|
|
grid = np.array([ int(dict(zip(items[1::2],items[2::2]))[i]) for i in ['a','b','c']])
|
|
|
|
elif key == 'size':
|
|
|
|
size = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
elif key == 'origin':
|
|
|
|
origin = np.array([float(dict(zip(items[1::2],items[2::2]))[i]) for i in ['x','y','z']])
|
|
|
|
if options.grid: grid = np.array(options.grid)
|
|
|
|
if options.size: size = np.array(options.size)
|
|
|
|
if options.origin: origin = np.array(options.origin)
|
|
|
|
|
|
|
|
|
2020-03-21 16:06:34 +05:30
|
|
|
seeds = table.get(options.pos)
|
2020-03-19 11:19:45 +05:30
|
|
|
|
2020-03-21 16:06:34 +05:30
|
|
|
grains = table.get(options.microstructure) if options.microstructure in table.labels else np.arange(len(seeds))+1
|
2020-03-19 11:19:45 +05:30
|
|
|
grainIDs = np.unique(grains).astype('i')
|
|
|
|
NgrainIDs = len(grainIDs)
|
|
|
|
|
2020-03-21 16:06:34 +05:30
|
|
|
if options.eulers in table.labels:
|
|
|
|
eulers = table.get(options.eulers)
|
|
|
|
|
|
|
|
coords = damask.grid_filters.cell_coord0(grid,size,-origin).reshape(-1,3,order='F')
|
2020-03-19 11:19:45 +05:30
|
|
|
|
|
|
|
if options.laguerre:
|
2020-03-19 16:21:30 +05:30
|
|
|
indices = Laguerre_tessellation(coords,seeds,grains,size,options.periodic,
|
|
|
|
table.get(options.weight),options.cpus)
|
2020-03-19 11:19:45 +05:30
|
|
|
else:
|
2020-03-19 16:21:30 +05:30
|
|
|
indices = Voronoi_tessellation (coords,seeds,grains,size,options.periodic)
|
2020-03-19 11:19:45 +05:30
|
|
|
|
|
|
|
config_header = []
|
|
|
|
if options.config:
|
|
|
|
|
|
|
|
if options.eulers in table.labels:
|
|
|
|
config_header += ['<texture>']
|
|
|
|
for ID in grainIDs:
|
2020-03-21 16:06:34 +05:30
|
|
|
eulerID = np.nonzero(grains == ID)[0][0] # find first occurrence of this grain id
|
2020-03-19 11:19:45 +05:30
|
|
|
config_header += ['[Grain{}]'.format(ID),
|
|
|
|
'(gauss)\tphi1 {:.2f}\tPhi {:.2f}\tphi2 {:.2f}'.format(*eulers[eulerID])
|
|
|
|
]
|
|
|
|
if options.axes: config_header += ['axes\t{} {} {}'.format(*options.axes)]
|
|
|
|
|
|
|
|
config_header += ['<microstructure>']
|
|
|
|
for ID in grainIDs:
|
|
|
|
config_header += ['[Grain{}]'.format(ID),
|
|
|
|
'(constituent)\tphase {}\ttexture {}\tfraction 1.0'.format(options.phase,ID)
|
|
|
|
]
|
|
|
|
|
|
|
|
config_header += ['<!skip>']
|
|
|
|
|
|
|
|
header = [scriptID + ' ' + ' '.join(sys.argv[1:])]\
|
|
|
|
+ config_header
|
2020-03-21 16:06:34 +05:30
|
|
|
geom = damask.Geom(indices.reshape(grid),size,origin,
|
2020-03-19 11:19:45 +05:30
|
|
|
homogenization=options.homogenization,comments=header)
|
|
|
|
damask.util.croak(geom)
|
|
|
|
|
|
|
|
geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False)
|