2018-11-17 13:16:58 +05:30
|
|
|
#!/usr/bin/env python3
|
2012-11-06 02:49:12 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 17:13:09 +05:30
|
|
|
import os,sys,math,random
|
2014-09-10 21:44:37 +05:30
|
|
|
import numpy as np
|
|
|
|
import damask
|
2015-05-27 01:43:35 +05:30
|
|
|
from optparse import OptionParser,OptionGroup
|
|
|
|
from scipy import spatial
|
|
|
|
|
2012-11-06 02:49:12 +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-18 18:58:54 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ aux functions ---------------------------------
|
|
|
|
|
|
|
|
def kdtree_search(cloud, queryPoints):
|
2016-10-25 00:46:29 +05:30
|
|
|
"""Find distances to nearest neighbor among cloud (N,d) for each of the queryPoints (n,d)"""
|
2015-08-08 00:33:26 +05:30
|
|
|
n = queryPoints.shape[0]
|
|
|
|
distances = np.zeros(n,dtype=float)
|
|
|
|
tree = spatial.cKDTree(cloud)
|
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(n):
|
2015-08-08 00:33:26 +05:30
|
|
|
distances[i], index = tree.query(queryPoints[i])
|
|
|
|
|
|
|
|
return distances
|
|
|
|
|
2014-09-10 21:44:37 +05:30
|
|
|
# --------------------------------------------------------------------
|
2013-05-14 22:49:36 +05:30
|
|
|
# MAIN
|
2014-09-10 21:44:37 +05:30
|
|
|
# --------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2019-02-17 12:30:26 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options', description = """
|
2016-09-15 05:12:05 +05:30
|
|
|
Distribute given number of points randomly within (a fraction of) the three-dimensional cube [0.0,0.0,0.0]--[1.0,1.0,1.0].
|
2012-11-06 02:49:12 +05:30
|
|
|
Reports positions with random crystal orientations in seeds file format to STDOUT.
|
2014-09-10 21:44:37 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2016-04-24 20:44:16 +05:30
|
|
|
parser.add_option('-N',
|
|
|
|
dest = 'N',
|
2015-08-08 00:33:26 +05:30
|
|
|
type = 'int', metavar = 'int',
|
2016-04-24 20:44:16 +05:30
|
|
|
help = 'number of seed points [%default]')
|
2016-09-15 05:12:05 +05:30
|
|
|
parser.add_option('-f',
|
|
|
|
'--fraction',
|
|
|
|
dest = 'fraction',
|
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
|
|
|
help='fractions along x,y,z of unit cube to fill %default')
|
2016-04-24 20:44:16 +05:30
|
|
|
parser.add_option('-g',
|
|
|
|
'--grid',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'grid',
|
|
|
|
type = 'int', nargs = 3, metavar = 'int int int',
|
2013-04-12 15:57:05 +05:30
|
|
|
help='min a,b,c grid of hexahedral box %default')
|
2016-04-24 20:44:16 +05:30
|
|
|
parser.add_option('-m',
|
|
|
|
'--microstructure',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'microstructure',
|
2016-04-24 20:44:16 +05:30
|
|
|
type = 'int', metavar = 'int',
|
2015-08-08 00:33:26 +05:30
|
|
|
help = 'first microstructure index [%default]')
|
2016-04-24 20:44:16 +05:30
|
|
|
parser.add_option('-r',
|
|
|
|
'--rnd',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'randomSeed', type = 'int', metavar = 'int',
|
|
|
|
help = 'seed of random number generator [%default]')
|
2016-04-09 03:16:06 +05:30
|
|
|
parser.add_option('--format',
|
|
|
|
dest = 'format', type = 'string', metavar = 'string',
|
2016-04-24 20:44:16 +05:30
|
|
|
help = 'output number format [auto]')
|
2015-05-27 01:52:11 +05:30
|
|
|
|
2016-04-09 03:16:06 +05:30
|
|
|
group = OptionGroup(parser, "Laguerre Tessellation",
|
2015-08-08 00:33:26 +05:30
|
|
|
"Parameters determining shape of weight distribution of seed points"
|
2015-05-27 01:52:11 +05:30
|
|
|
)
|
2016-04-24 20:44:16 +05:30
|
|
|
group.add_option( '-w',
|
|
|
|
'--weights',
|
2015-08-08 00:33:26 +05:30
|
|
|
action = 'store_true',
|
|
|
|
dest = 'weights',
|
2016-04-24 20:44:16 +05:30
|
|
|
help = 'assign random weights to seed points for Laguerre tessellation [%default]')
|
|
|
|
group.add_option( '--max',
|
2015-08-12 23:17:38 +05:30
|
|
|
dest = 'max',
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help = 'max of uniform distribution for weights [%default]')
|
2016-04-24 20:44:16 +05:30
|
|
|
group.add_option( '--mean',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'mean',
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help = 'mean of normal distribution for weights [%default]')
|
2016-04-24 20:44:16 +05:30
|
|
|
group.add_option( '--sigma',
|
|
|
|
dest = 'sigma',
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help='standard deviation of normal distribution for weights [%default]')
|
2015-05-27 01:52:11 +05:30
|
|
|
parser.add_option_group(group)
|
|
|
|
|
2016-04-09 03:16:06 +05:30
|
|
|
group = OptionGroup(parser, "Selective Seeding",
|
|
|
|
"More uniform distribution of seed points using Mitchell's Best Candidate Algorithm"
|
2015-05-27 01:43:35 +05:30
|
|
|
)
|
2016-04-24 20:44:16 +05:30
|
|
|
group.add_option( '-s',
|
|
|
|
'--selective',
|
2015-08-08 00:33:26 +05:30
|
|
|
action = 'store_true',
|
|
|
|
dest = 'selective',
|
|
|
|
help = 'selective picking of seed points from random seed points [%default]')
|
2016-09-15 05:12:05 +05:30
|
|
|
group.add_option( '--force',
|
2015-08-09 01:50:53 +05:30
|
|
|
action = 'store_true',
|
|
|
|
dest = 'force',
|
|
|
|
help = 'try selective picking despite large seed point number [%default]')
|
2016-04-24 20:44:16 +05:30
|
|
|
group.add_option( '--distance',
|
|
|
|
dest = 'distance',
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help = 'minimum distance to next neighbor [%default]')
|
|
|
|
group.add_option( '--numCandidates',
|
|
|
|
dest = 'numCandidates',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'size of point group to select best distance from [%default]')
|
2015-05-27 01:43:35 +05:30
|
|
|
parser.add_option_group(group)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(randomSeed = None,
|
|
|
|
grid = (16,16,16),
|
2016-09-15 05:12:05 +05:30
|
|
|
fraction = (1.0,1.0,1.0),
|
2015-08-08 00:33:26 +05:30
|
|
|
N = 20,
|
|
|
|
weights = False,
|
2015-08-12 23:17:38 +05:30
|
|
|
max = 0.0,
|
|
|
|
mean = 0.2,
|
|
|
|
sigma = 0.05,
|
2015-08-08 00:33:26 +05:30
|
|
|
microstructure = 1,
|
|
|
|
selective = False,
|
2015-08-09 01:50:53 +05:30
|
|
|
force = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
distance = 0.2,
|
|
|
|
numCandidates = 10,
|
2016-04-09 03:16:06 +05:30
|
|
|
format = None,
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2016-09-15 05:12:05 +05:30
|
|
|
options.fraction = np.array(options.fraction)
|
2014-09-25 05:17:52 +05:30
|
|
|
options.grid = np.array(options.grid)
|
2015-08-08 00:33:26 +05:30
|
|
|
gridSize = options.grid.prod()
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2018-11-24 14:37:47 +05:30
|
|
|
if options.randomSeed is None: options.randomSeed = int(os.urandom(4).hex(), 16)
|
2015-08-08 00:33:26 +05:30
|
|
|
np.random.seed(options.randomSeed) # init random generators
|
|
|
|
random.seed(options.randomSeed)
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2015-05-27 01:43:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over output files -------------------------------------------------------------------------
|
2015-05-27 01:43:35 +05:30
|
|
|
|
2015-08-12 23:17:38 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-05-27 01:43:35 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2016-04-24 20:44:16 +05:30
|
|
|
try: table = damask.ASCIItable(outname = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- sanity checks -------------------------------------------------------------------------
|
|
|
|
|
2015-08-09 01:50:53 +05:30
|
|
|
remarks = []
|
|
|
|
errors = []
|
2016-03-02 17:13:09 +05:30
|
|
|
if gridSize == 0:
|
2016-04-24 20:44:16 +05:30
|
|
|
errors.append('zero grid dimension for {}.'.format(', '.join([['a','b','c'][x] for x in np.where(options.grid == 0)[0]])))
|
2015-08-08 00:33:26 +05:30
|
|
|
if options.N > gridSize/10.: errors.append('seed count exceeds 0.1 of grid points.')
|
|
|
|
if options.selective and 4./3.*math.pi*(options.distance/2.)**3*options.N > 0.5:
|
2016-03-02 17:13:09 +05:30
|
|
|
(remarks if options.force else errors).append('maximum recommended seed point count for given distance is {}.{}'.
|
|
|
|
format(int(3./8./math.pi/(options.distance/2.)**3),'..'*options.force))
|
2015-08-09 01:50:53 +05:30
|
|
|
|
2015-09-24 18:51:44 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-09 01:50:53 +05:30
|
|
|
if errors != []:
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
grainEuler = np.random.rand(3,options.N) # create random Euler triplets
|
|
|
|
grainEuler[0,:] *= 360.0 # phi_1 is uniformly distributed
|
|
|
|
grainEuler[1,:] = np.degrees(np.arccos(2*grainEuler[1,:]-1)) # cos(Phi) is uniformly distributed
|
|
|
|
grainEuler[2,:] *= 360.0 # phi_2 is uniformly distributed
|
|
|
|
|
|
|
|
if not options.selective:
|
2018-03-08 05:28:28 +05:30
|
|
|
n = np.maximum(np.ones(3),np.array(options.grid*options.fraction),
|
|
|
|
dtype=int,casting='unsafe') # find max grid indices within fraction
|
2018-03-08 04:15:22 +05:30
|
|
|
meshgrid = np.meshgrid(*map(np.arange,n),indexing='ij') # create a meshgrid within fraction
|
|
|
|
coords = np.vstack((meshgrid[0],meshgrid[1],meshgrid[2])).reshape(3,n.prod()).T # assemble list of 3D coordinates
|
2018-11-17 13:16:58 +05:30
|
|
|
seeds = ((random.sample(list(coords),options.N)+np.random.random(options.N*3).reshape(options.N,3))\
|
2018-03-08 05:28:28 +05:30
|
|
|
/ \
|
|
|
|
(n/options.fraction)).T # pick options.N of those, rattle position,
|
|
|
|
# and rescale to fall within fraction
|
2015-05-27 01:43:35 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
seeds = np.zeros((options.N,3),dtype=float) # seed positions array
|
|
|
|
seeds[0] = np.random.random(3)*options.grid/max(options.grid)
|
|
|
|
i = 1 # start out with one given point
|
2015-09-24 18:51:44 +05:30
|
|
|
if i%(options.N/100.) < 1: damask.util.croak('.',False)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
while i < options.N:
|
|
|
|
candidates = np.random.random(options.numCandidates*3).reshape(options.numCandidates,3)
|
|
|
|
distances = kdtree_search(seeds[:i],candidates)
|
|
|
|
best = distances.argmax()
|
|
|
|
if distances[best] > options.distance: # require minimum separation
|
2016-03-02 17:13:09 +05:30
|
|
|
seeds[i] = candidates[best] # maximum separation to existing point cloud
|
2015-08-08 00:33:26 +05:30
|
|
|
i += 1
|
2015-09-24 18:51:44 +05:30
|
|
|
if i%(options.N/100.) < 1: damask.util.croak('.',False)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.croak('')
|
2015-08-12 23:17:38 +05:30
|
|
|
seeds = seeds.T # prepare shape for stacking
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
if options.weights:
|
2016-04-24 20:44:16 +05:30
|
|
|
weights = [np.random.uniform(low = 0, high = options.max, size = options.N)] if options.max > 0.0 \
|
|
|
|
else [np.random.normal(loc = options.mean, scale = options.sigma, size = options.N)]
|
2015-08-08 00:33:26 +05:30
|
|
|
else:
|
2015-08-12 23:17:38 +05:30
|
|
|
weights = []
|
|
|
|
seeds = np.transpose(np.vstack(tuple([seeds,
|
|
|
|
grainEuler,
|
|
|
|
np.arange(options.microstructure,
|
|
|
|
options.microstructure + options.N),
|
|
|
|
] + weights
|
|
|
|
)))
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
|
|
|
table.info_clear()
|
|
|
|
table.info_append([
|
|
|
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
2016-04-24 20:44:16 +05:30
|
|
|
"grid\ta {}\tb {}\tc {}".format(*options.grid),
|
2015-08-08 00:33:26 +05:30
|
|
|
"microstructures\t{}".format(options.N),
|
|
|
|
"randomSeed\t{}".format(options.randomSeed),
|
|
|
|
])
|
|
|
|
table.labels_clear()
|
2016-10-25 00:46:29 +05:30
|
|
|
table.labels_append( ['{dim}_{label}'.format(dim = 1+k,label = 'pos') for k in range(3)] +
|
|
|
|
['{dim}_{label}'.format(dim = 1+k,label = 'euler') for k in range(3)] +
|
2015-08-08 00:33:26 +05:30
|
|
|
['microstructure'] +
|
|
|
|
(['weight'] if options.weights else []))
|
|
|
|
table.head_write()
|
|
|
|
table.output_flush()
|
|
|
|
|
|
|
|
# --- write seeds information ------------------------------------------------------------
|
|
|
|
|
|
|
|
table.data = seeds
|
2016-04-09 03:16:06 +05:30
|
|
|
table.data_writeArray(fmt = options.format)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|