2018-11-17 13:16:58 +05:30
|
|
|
#!/usr/bin/env python3
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2020-03-17 15:54:15 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2015-05-27 01:43:35 +05:30
|
|
|
from optparse import OptionParser,OptionGroup
|
2020-03-17 15:54:15 +05:30
|
|
|
|
|
|
|
import numpy as np
|
2015-05-27 01:43:35 +05:30
|
|
|
from scipy import spatial
|
|
|
|
|
2020-03-17 15:54:15 +05:30
|
|
|
import damask
|
|
|
|
|
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
|
|
|
|
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 = """
|
2020-03-21 19:47:02 +05:30
|
|
|
Distribute given number of points randomly within rectangular cuboid.
|
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]')
|
2020-03-21 19:47:02 +05:30
|
|
|
parser.add_option('-s',
|
|
|
|
'--size',
|
|
|
|
dest = 'size',
|
2016-09-15 05:12:05 +05:30
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
2020-03-21 19:47:02 +05:30
|
|
|
help='size 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]')
|
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
|
|
|
)
|
2020-03-21 19:47:02 +05:30
|
|
|
group.add_option( '--selective',
|
2015-08-08 00:33:26 +05:30
|
|
|
action = 'store_true',
|
|
|
|
dest = 'selective',
|
2019-02-24 15:01:08 +05:30
|
|
|
help = 'selective picking of seed points from random seed points')
|
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',
|
2020-03-17 15:19:30 +05:30
|
|
|
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),
|
2020-03-21 19:47:02 +05:30
|
|
|
size = (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,
|
|
|
|
distance = 0.2,
|
|
|
|
numCandidates = 10,
|
|
|
|
)
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
2020-03-17 15:19:30 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2020-03-21 19:47:02 +05:30
|
|
|
size = np.array(options.size)
|
|
|
|
grid = np.array(options.grid)
|
|
|
|
np.random.seed(int(os.urandom(4).hex(),16) if options.randomSeed is None else options.randomSeed)
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2020-03-20 05:06:48 +05:30
|
|
|
damask.util.report(scriptName,name)
|
|
|
|
|
2020-03-21 19:47:02 +05:30
|
|
|
if options.N > np.prod(grid):
|
|
|
|
damask.util.croak('More seeds than grid positions.')
|
2020-03-20 05:06:48 +05:30
|
|
|
sys.exit()
|
2020-03-22 01:51:30 +05:30
|
|
|
if options.selective and options.distance < min(size/grid):
|
|
|
|
damask.util.croak('Distance must be larger than grid spacing.')
|
|
|
|
sys.exit()
|
2020-03-22 01:40:14 +05:30
|
|
|
if options.selective and options.distance**3*options.N > 0.5*np.prod(size):
|
|
|
|
damask.util.croak('Number of seeds for given size and distance should be < {}.'\
|
|
|
|
.format(int(0.5*np.prod(size)/options.distance**3)))
|
2020-03-20 05:06:48 +05:30
|
|
|
|
|
|
|
eulers = np.random.rand(options.N,3) # create random Euler triplets
|
|
|
|
eulers[:,0] *= 360.0 # phi_1 is uniformly distributed
|
2020-03-20 10:54:41 +05:30
|
|
|
eulers[:,1] = np.degrees(np.arccos(2*eulers[:,1]-1.0)) # cos(Phi) is uniformly distributed
|
2020-03-20 05:06:48 +05:30
|
|
|
eulers[:,2] *= 360.0 # phi_2 is uniformly distributed
|
|
|
|
|
2020-03-21 19:47:02 +05:30
|
|
|
|
2020-03-20 05:06:48 +05:30
|
|
|
if not options.selective:
|
2020-03-22 11:38:54 +05:30
|
|
|
coords = damask.grid_filters.cell_coord0(grid,size).reshape(-1,3)
|
|
|
|
seeds = coords[np.random.choice(np.prod(grid), options.N, replace=False)] \
|
2020-03-22 02:43:40 +05:30
|
|
|
+ np.broadcast_to(size/grid,(options.N,3))*(np.random.rand(options.N,3)*.5-.25) # wobble without leaving grid
|
2020-03-20 14:57:46 +05:30
|
|
|
else:
|
2020-03-21 19:47:02 +05:30
|
|
|
seeds = np.empty((options.N,3))
|
2020-03-22 01:51:30 +05:30
|
|
|
seeds[0] = np.random.random(3) * size
|
2020-03-21 19:47:02 +05:30
|
|
|
|
|
|
|
i = 1
|
|
|
|
progress = damask.util._ProgressBar(options.N,'',50)
|
2020-03-20 05:06:48 +05:30
|
|
|
while i < options.N:
|
2020-03-22 01:51:30 +05:30
|
|
|
candidates = np.random.rand(options.numCandidates,3)*np.broadcast_to(size,(options.numCandidates,3))
|
2020-03-21 19:47:02 +05:30
|
|
|
tree = spatial.cKDTree(seeds[:i])
|
2020-03-22 01:51:30 +05:30
|
|
|
distances, dev_null = tree.query(candidates)
|
2020-03-21 19:47:02 +05:30
|
|
|
best = distances.argmax()
|
|
|
|
if distances[best] > options.distance: # require minimum separation
|
2020-03-22 01:51:30 +05:30
|
|
|
seeds[i] = candidates[best] # maximum separation to existing point cloud
|
2020-03-21 19:47:02 +05:30
|
|
|
i += 1
|
|
|
|
progress.update(i)
|
|
|
|
|
2020-03-17 15:40:02 +05:30
|
|
|
|
2020-03-20 05:06:48 +05:30
|
|
|
comments = [scriptID + ' ' + ' '.join(sys.argv[1:]),
|
|
|
|
'grid\ta {}\tb {}\tc {}'.format(*grid),
|
2020-03-21 19:47:02 +05:30
|
|
|
'size\tx {}\ty {}\tz {}'.format(*size),
|
2020-03-20 05:06:48 +05:30
|
|
|
'randomSeed\t{}'.format(options.randomSeed),
|
|
|
|
]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-03-20 05:06:48 +05:30
|
|
|
table = damask.Table(np.hstack((seeds,eulers)),{'pos':(3,),'euler':(3,)},comments)
|
|
|
|
table.add('microstructure',np.arange(options.microstructure,options.microstructure + options.N,dtype=int))
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-03-20 05:06:48 +05:30
|
|
|
if options.weights:
|
|
|
|
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)
|
|
|
|
table.add('weight',weights)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2020-03-20 05:06:48 +05:30
|
|
|
table.to_ASCII(sys.stdout if name is None else name)
|