significant speed improvement for large grids.
polished messages and fixed error reporting bug.
This commit is contained in:
parent
1c38d42a0b
commit
b85797c109
|
@ -31,38 +31,55 @@ parser.set_defaults(grid = (16,16,16))
|
||||||
parser.set_defaults(N = 20)
|
parser.set_defaults(N = 20)
|
||||||
|
|
||||||
(options, extras) = parser.parse_args()
|
(options, extras) = parser.parse_args()
|
||||||
|
options.grid = np.array(options.grid)
|
||||||
|
|
||||||
Npoints = reduce(lambda x, y: x * y, options.grid)
|
sys.stderr.write('\033[1m'+scriptName+'\033[0m\n')
|
||||||
if 0 in options.grid:
|
|
||||||
file['croak'].write('invalid grid a b c.\n')
|
gridSize = options.grid.prod()
|
||||||
|
if gridSize == 0:
|
||||||
|
sys.stderr.write('zero grid dimension for %s.\n'%(', '.join([['a','b','c'][x] for x in np.where(options.grid == 0)[0]])))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if options.N > Npoints:
|
if options.N > gridSize:
|
||||||
sys.stderr.write('Warning: more seeds than grid points at minimum resolution.\n')
|
sys.stderr.write('accommodating only %i seeds on grid.\n'%gridSize)
|
||||||
options.N = Npoints
|
options.N = gridSize
|
||||||
if options.randomSeed == None:
|
if options.randomSeed == None:
|
||||||
options.randomSeed = int(os.urandom(4).encode('hex'), 16)
|
options.randomSeed = int(os.urandom(4).encode('hex'), 16)
|
||||||
|
np.random.seed(options.randomSeed) # init random generators
|
||||||
|
random.seed(options.randomSeed)
|
||||||
|
|
||||||
seeds = np.zeros((3,options.N),float)
|
grainEuler = np.random.rand(3,options.N) # create random Euler triplets
|
||||||
np.random.seed(options.randomSeed)
|
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
|
||||||
|
|
||||||
grainEuler = np.random.rand(3,options.N)
|
seedpoints = -np.ones(options.N,dtype='int') # init grid positions of seed points
|
||||||
grainEuler[0,:] *= 360.0
|
|
||||||
grainEuler[1,:] = np.arccos(2*grainEuler[1,:]-1)*180.0/math.pi
|
|
||||||
grainEuler[2,:] *= 360.0
|
|
||||||
|
|
||||||
seedpoint = np.random.permutation(Npoints)[:options.N]
|
if options.N * 1024 < gridSize: # heuristic limit for random search
|
||||||
seeds[0,:]=(np.mod(seedpoint ,options.grid[0])\
|
i = 0
|
||||||
+np.random.random())/options.grid[0]
|
while i < options.N: # until all (unique) points determined
|
||||||
seeds[1,:]=(np.mod(seedpoint// options.grid[0] ,options.grid[1])\
|
p = np.random.randint(gridSize) # pick a location
|
||||||
+np.random.random())/options.grid[1]
|
if p not in seedpoints: # not yet taken?
|
||||||
seeds[2,:]=(np.mod(seedpoint//(options.grid[1]*options.grid[0]),options.grid[2])\
|
seedpoints[i] = p # take it
|
||||||
+np.random.random())/options.grid[2]
|
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
|
||||||
|
|
||||||
sys.stdout.write("5\theader\n")
|
seeds = np.zeros((3,options.N),float) # init seed positions
|
||||||
sys.stdout.write(scriptID + " " + " ".join(sys.argv[1:])+"\n")
|
seeds[0,:] = (np.mod(seedpoints ,options.grid[0])\
|
||||||
sys.stdout.write("grid\ta {}\tb {}\tc {}\n".format(options.grid[0],options.grid[1],options.grid[2]))
|
+np.random.random())/options.grid[0]
|
||||||
sys.stdout.write("microstructures\t{}\n".format(options.N))
|
seeds[1,:] = (np.mod(seedpoints// options.grid[0] ,options.grid[1])\
|
||||||
sys.stdout.write("randomSeed\t{}\n".format(options.randomSeed))
|
+np.random.random())/options.grid[1]
|
||||||
sys.stdout.write("x\ty\tz\tphi1\tPhi\tphi2\n")
|
seeds[2,:] = (np.mod(seedpoints//(options.grid[1]*options.grid[0]),options.grid[2])\
|
||||||
|
+np.random.random())/options.grid[2]
|
||||||
|
|
||||||
|
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),
|
||||||
|
"randomSeed\t{}".format(options.randomSeed),
|
||||||
|
"x\ty\tz\tphi1\tPhi\tphi2",
|
||||||
|
]
|
||||||
|
|
||||||
|
for line in header:
|
||||||
|
sys.stdout.write(line+"\n")
|
||||||
np.savetxt(sys.stdout,np.transpose(np.concatenate((seeds,grainEuler),axis = 0)),fmt='%10.6f',delimiter='\t')
|
np.savetxt(sys.stdout,np.transpose(np.concatenate((seeds,grainEuler),axis = 0)),fmt='%10.6f',delimiter='\t')
|
||||||
|
|
Loading…
Reference in New Issue