2012-11-06 02:49:12 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import os,sys,string,re,math,numpy,random
|
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
2013-07-18 18:58:54 +05:30
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
|
|
|
|
2013-05-14 22:49:36 +05:30
|
|
|
#------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
class extendedOption(Option):
|
2013-05-14 22:49:36 +05:30
|
|
|
#------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
# 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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2013-05-14 22:49:36 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
identifiers = {
|
2013-04-12 15:57:05 +05:30
|
|
|
'grid': ['a','b','c'],
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
2013-04-12 15:57:05 +05:30
|
|
|
'grid': lambda x: int(x),
|
2012-11-06 02:49:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=extendedOption, 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.
|
2013-07-18 18:58:54 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
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]')
|
|
|
|
|
2014-05-03 08:06:23 +05:30
|
|
|
parser.set_defaults(randomSeed = None)
|
2013-04-12 15:57:05 +05:30
|
|
|
parser.set_defaults(grid = [16,16,16])
|
2012-11-06 02:49:12 +05:30
|
|
|
parser.set_defaults(N = 20)
|
|
|
|
|
|
|
|
(options, extras) = parser.parse_args()
|
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
Npoints = reduce(lambda x, y: x * y, options.grid)
|
|
|
|
if 0 in options.grid:
|
2013-06-04 18:26:57 +05:30
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-05-15 21:32:38 +05:30
|
|
|
sys.exit()
|
2012-11-06 02:49:12 +05:30
|
|
|
if options.N > Npoints:
|
|
|
|
sys.stderr.write('Warning: more seeds than grid points at minimum resolution.\n')
|
|
|
|
options.N = Npoints
|
2014-05-03 08:06:23 +05:30
|
|
|
if options.randomSeed == None:
|
|
|
|
options.randomSeed = int(os.urandom(4).encode('hex'), 16)
|
2012-11-06 02:49:12 +05:30
|
|
|
|
|
|
|
seeds = numpy.zeros((3,options.N),float)
|
|
|
|
numpy.random.seed(options.randomSeed)
|
|
|
|
|
|
|
|
grainEuler = numpy.random.rand(3,options.N)
|
|
|
|
grainEuler[0,:] *= 360.0
|
|
|
|
grainEuler[1,:] = numpy.arccos(2*grainEuler[1,:]-1)*180.0/math.pi
|
|
|
|
grainEuler[2,:] *= 360.0
|
|
|
|
|
|
|
|
seedpoint = numpy.random.permutation(Npoints)[:options.N]
|
2013-04-12 15:57:05 +05:30
|
|
|
seeds[0,:]=(numpy.mod(seedpoint ,options.grid[0])\
|
|
|
|
+numpy.random.random())/options.grid[0]
|
|
|
|
seeds[1,:]=(numpy.mod(seedpoint// options.grid[0],options.grid[1])\
|
|
|
|
+numpy.random.random())/options.grid[1]
|
|
|
|
seeds[2,:]=(numpy.mod(seedpoint//(options.grid[1]*options.grid[0]),options.grid[2])\
|
|
|
|
+numpy.random.random())/options.grid[2]
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2013-04-12 16:45:17 +05:30
|
|
|
print "5\theader"
|
2013-07-18 18:58:54 +05:30
|
|
|
print scriptID
|
2013-04-12 15:57:05 +05:30
|
|
|
print "grid\ta %i\tb %i\tc %i"%(options.grid[0],options.grid[1],options.grid[2],)
|
2013-07-10 14:44:53 +05:30
|
|
|
print "microstructures\t%i"%options.N
|
2013-04-12 15:57:05 +05:30
|
|
|
print "randomSeed\t%i"%(options.randomSeed)
|
2012-11-06 02:49:12 +05:30
|
|
|
print "x\ty\tz\tphi1\tPhi\tphi2"
|
|
|
|
|
|
|
|
numpy.savetxt(sys.stdout,numpy.transpose(numpy.concatenate((seeds,grainEuler),axis = 0)),fmt='%10.6f',delimiter='\t')
|