2012-11-06 02:49:12 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
import os,sys,string,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
|
|
|
|
2014-09-10 21:44:37 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2013-07-18 18:58:54 +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
|
|
|
|
2014-09-10 21:44:37 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [options]', description = """
|
2013-06-04 18:26:57 +05:30
|
|
|
Distribute given number of points randomly within 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
|
|
|
|
2014-02-14 18:47:29 +05:30
|
|
|
parser.add_option('-N', dest='N', type='int', metavar='int', \
|
2012-11-06 02:49:12 +05:30
|
|
|
help='number of seed points to distribute [%default]')
|
2014-02-14 18:47:29 +05:30
|
|
|
parser.add_option('-g','--grid', 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')
|
2014-02-14 18:47:29 +05:30
|
|
|
parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int', \
|
2012-11-06 02:49:12 +05:30
|
|
|
help='seed of random number generator [%default]')
|
2015-05-02 13:11:14 +05:30
|
|
|
parser.add_option('-w', '--weights', dest='weights', action='store_true',
|
|
|
|
help = 'assign random weigts (Gaussian Distribution) to seed points for laguerre tessellation [%default]')
|
2015-05-21 15:34:52 +05:30
|
|
|
parser.add_option('-m', '--microstructure', dest='microstructure', type='int',
|
|
|
|
help='first microstructure index [%default]', metavar='int')
|
2015-05-27 01:43:35 +05:30
|
|
|
parser.add_option('-s','--selective', dest='selective', action='store_true',
|
|
|
|
help = 'selective picking of seed points from random seed points [%default]')
|
2015-05-27 01:52:11 +05:30
|
|
|
|
2015-05-29 02:27:03 +05:30
|
|
|
group = OptionGroup(parser, "Laguerre Tessellation Options",
|
2015-05-27 01:52:11 +05:30
|
|
|
"Parameters determining shape of weight distribution of seed points "
|
|
|
|
)
|
|
|
|
group.add_option('--mean', dest='mean', type='float', metavar='float', \
|
|
|
|
help='mean of Gaussian Distribution for weights [%default]')
|
|
|
|
group.add_option('--sigma', dest='sigma', type='float', metavar='float', \
|
|
|
|
help='standard deviation of Gaussian Distribution for weights [%default]')
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
2015-05-27 01:43:35 +05:30
|
|
|
group = OptionGroup(parser, "Selective Seeding Options",
|
2015-05-27 01:52:11 +05:30
|
|
|
"More uniform distribution of seed points using Mitchell\'s Best Candidate Algorithm"
|
2015-05-27 01:43:35 +05:30
|
|
|
)
|
|
|
|
group.add_option('--distance', dest='bestDistance', type='float', metavar='float', \
|
|
|
|
help='minimum distance to the next neighbor [%default]')
|
|
|
|
group.add_option('--numCandidates', dest='numCandidates', type='int', metavar='int', \
|
|
|
|
help='maximum number of point to consider for initial random points generation [%default]')
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
2014-05-03 08:06:23 +05:30
|
|
|
parser.set_defaults(randomSeed = None)
|
2014-09-10 21:44:37 +05:30
|
|
|
parser.set_defaults(grid = (16,16,16))
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.set_defaults(N = 20)
|
2015-05-02 13:11:14 +05:30
|
|
|
parser.set_defaults(weights=False)
|
|
|
|
parser.set_defaults(mean = 0.0)
|
|
|
|
parser.set_defaults(sigma = 1.0)
|
2015-05-21 15:34:52 +05:30
|
|
|
parser.set_defaults(microstructure = 1)
|
2015-05-27 01:43:35 +05:30
|
|
|
parser.set_defaults(selective = False)
|
|
|
|
parser.set_defaults(bestDistance = 0.2)
|
|
|
|
parser.set_defaults(numCandidates = 10)
|
|
|
|
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2014-10-10 17:41:10 +05:30
|
|
|
(options,filename) = parser.parse_args()
|
2014-09-25 05:17:52 +05:30
|
|
|
options.grid = np.array(options.grid)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2015-05-21 15:34:52 +05:30
|
|
|
labels = "1_coords\t2_coords\t3_coords\tphi1\tPhi\tphi2\tmicrostructure"
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2015-05-27 01:43:35 +05:30
|
|
|
# ------------------------------------------ Functions Definitions ---------------------------------
|
|
|
|
|
|
|
|
def kdtree_search(xyz, point) :
|
|
|
|
dist, index = spatial.cKDTree(xyz).query(np.array(point))
|
|
|
|
return dist
|
|
|
|
|
|
|
|
def generatePoint() :
|
|
|
|
return np.array([random.uniform(0,options.grid[0]/max(options.grid)), \
|
|
|
|
random.uniform(0,options.grid[1]/max(options.grid)), \
|
|
|
|
random.uniform(0,options.grid[2]/max(options.grid))])
|
|
|
|
|
|
|
|
|
2014-10-10 17:41:10 +05:30
|
|
|
# ------------------------------------------ setup file handle -------------------------------------
|
|
|
|
if filename == []:
|
|
|
|
file = {'output':sys.stdout, 'croak':sys.stderr}
|
|
|
|
else:
|
|
|
|
file = {'output':open(filename[0],'w'), 'croak':sys.stderr}
|
2014-09-25 05:17:52 +05:30
|
|
|
|
|
|
|
gridSize = options.grid.prod()
|
|
|
|
if gridSize == 0:
|
2014-10-10 17:41:10 +05:30
|
|
|
file['croak'].write('zero grid dimension for %s.\n'%(', '.join([['a','b','c'][x] for x in np.where(options.grid == 0)[0]])))
|
2013-05-15 21:32:38 +05:30
|
|
|
sys.exit()
|
2014-09-25 05:17:52 +05:30
|
|
|
if options.N > gridSize:
|
2014-10-10 17:41:10 +05:30
|
|
|
file['croak'].write('accommodating only %i seeds on grid.\n'%gridSize)
|
2014-09-25 05:17:52 +05:30
|
|
|
options.N = gridSize
|
2015-05-22 11:26:49 +05:30
|
|
|
randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed
|
|
|
|
np.random.seed(randomSeed) # init random generators
|
|
|
|
random.seed(randomSeed)
|
2014-09-25 05:17:52 +05:30
|
|
|
|
|
|
|
grainEuler = np.random.rand(3,options.N) # create random Euler triplets
|
|
|
|
grainEuler[0,:] *= 360.0 # phi_1 is uniformly distributed
|
|
|
|
grainEuler[1,:] = np.arccos(2*grainEuler[1,:]-1)*180.0/math.pi # cos(Phi) is uniformly distributed
|
|
|
|
grainEuler[2,:] *= 360.0 # phi_2 is uniformly distributed
|
|
|
|
|
2015-05-21 15:34:52 +05:30
|
|
|
microstructure=np.arange(options.microstructure,options.microstructure+options.N).reshape(1,options.N)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2015-05-27 01:43:35 +05:30
|
|
|
if options.selective == False :
|
|
|
|
seedpoints = -np.ones(options.N,dtype='int') # init grid positions of seed points
|
|
|
|
|
|
|
|
if options.N * 1024 < gridSize: # heuristic limit for random search
|
|
|
|
i = 0
|
|
|
|
while i < options.N: # until all (unique) points determined
|
|
|
|
p = np.random.randint(gridSize) # pick a location
|
|
|
|
if p not in seedpoints: # not yet taken?
|
|
|
|
seedpoints[i] = p # take it
|
|
|
|
i += 1 # advance stepper
|
|
|
|
else:
|
|
|
|
seedpoints = np.array(random.sample(range(gridSize),options.N)) # create random permutation of all grid positions and choose first N
|
|
|
|
|
|
|
|
seeds = np.zeros((3,options.N),float) # init seed positions
|
|
|
|
seeds[0,:] = (np.mod(seedpoints ,options.grid[0])\
|
|
|
|
+np.random.random())/options.grid[0]
|
|
|
|
seeds[1,:] = (np.mod(seedpoints// options.grid[0] ,options.grid[1])\
|
|
|
|
+np.random.random())/options.grid[1]
|
|
|
|
seeds[2,:] = (np.mod(seedpoints//(options.grid[1]*options.grid[0]),options.grid[2])\
|
|
|
|
+np.random.random())/options.grid[2]
|
|
|
|
table = np.transpose(np.concatenate((seeds,grainEuler,microstructure),axis = 0))
|
|
|
|
else :
|
|
|
|
samples = generatePoint().reshape(1,3)
|
|
|
|
|
|
|
|
while samples.shape[0] < options.N :
|
|
|
|
bestDistance = options.bestDistance
|
|
|
|
for i in xrange(options.numCandidates) :
|
|
|
|
c = generatePoint()
|
|
|
|
d = kdtree_search(samples, c)
|
|
|
|
if (d > bestDistance) :
|
|
|
|
bestDistance = d
|
|
|
|
bestCandidate = c
|
|
|
|
if kdtree_search(samples,bestCandidate) != 0.0 :
|
|
|
|
samples = np.append(samples,bestCandidate.reshape(1,3),axis=0)
|
|
|
|
else :
|
|
|
|
continue
|
|
|
|
table = np.transpose(np.concatenate((samples.T,grainEuler,microstructure),axis = 0))
|
2015-05-02 13:11:14 +05:30
|
|
|
|
|
|
|
if options.weights :
|
|
|
|
weight = np.random.normal(loc=options.mean, scale=options.sigma, size=options.N)
|
|
|
|
weight /= np.sum(weight)
|
|
|
|
table = np.append(table, weight.reshape(options.N,1), axis=1)
|
|
|
|
labels += "\tweight"
|
|
|
|
|
2015-05-27 01:43:35 +05:30
|
|
|
# -------------------------------------- Write Data --------------------------------------------------
|
2015-05-02 13:11:14 +05:30
|
|
|
|
2014-09-25 05:17:52 +05:30
|
|
|
header = ["5\theader",
|
|
|
|
scriptID + " " + " ".join(sys.argv[1:]),
|
|
|
|
"grid\ta {}\tb {}\tc {}".format(options.grid[0],options.grid[1],options.grid[2]),
|
|
|
|
"microstructures\t{}".format(options.N),
|
2015-05-22 11:26:49 +05:30
|
|
|
"randomSeed\t{}".format(randomSeed),
|
2015-05-02 13:11:14 +05:30
|
|
|
"%s"%labels,
|
2014-09-25 05:17:52 +05:30
|
|
|
]
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2014-09-25 05:17:52 +05:30
|
|
|
for line in header:
|
2014-10-10 17:41:10 +05:30
|
|
|
file['output'].write(line+"\n")
|
2015-05-02 13:11:14 +05:30
|
|
|
np.savetxt(file['output'], table, fmt='%10.6f', delimiter='\t')
|