DAMASK_EICMD/processing/pre/seeds_fromRandom.py

201 lines
9.0 KiB
Python
Raw Normal View History

2018-11-17 13:16:58 +05:30
#!/usr/bin/env python3
2020-03-17 15:54:15 +05:30
import os
import sys
import random
from optparse import OptionParser,OptionGroup
2020-03-17 15:54:15 +05:30
import numpy as np
from scipy import spatial
2020-03-17 15:54:15 +05:30
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
def kdtree_search(cloud, queryPoints):
"""Find distances to nearest neighbor among cloud (N,d) for each of the queryPoints (n,d)."""
n = queryPoints.shape[0]
distances = np.zeros(n,dtype=float)
tree = spatial.cKDTree(cloud)
2020-03-17 15:19:30 +05:30
2016-10-25 00:46:29 +05:30
for i in range(n):
distances[i], index = tree.query(queryPoints[i])
return distances
# --------------------------------------------------------------------
2013-05-14 22:49:36 +05:30
# MAIN
# --------------------------------------------------------------------
2019-02-17 12:30:26 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options', description = """
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].
Reports positions with random crystal orientations in seeds file format to STDOUT.
""", version = scriptID)
2016-04-24 20:44:16 +05:30
parser.add_option('-N',
dest = 'N',
type = 'int', metavar = 'int',
2016-04-24 20:44:16 +05:30
help = 'number of seed points [%default]')
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',
dest = 'grid',
type = 'int', nargs = 3, metavar = 'int int int',
help='min a,b,c grid of hexahedral box %default')
2016-04-24 20:44:16 +05:30
parser.add_option('-m',
'--microstructure',
dest = 'microstructure',
2016-04-24 20:44:16 +05:30
type = 'int', metavar = 'int',
help = 'first microstructure index [%default]')
2016-04-24 20:44:16 +05:30
parser.add_option('-r',
'--rnd',
dest = 'randomSeed', type = 'int', metavar = 'int',
help = 'seed of random number generator [%default]')
2015-05-27 01:52:11 +05:30
group = OptionGroup(parser, "Laguerre Tessellation",
"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',
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',
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',
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)
group = OptionGroup(parser, "Selective Seeding",
"More uniform distribution of seed points using Mitchell's Best Candidate Algorithm"
)
2016-04-24 20:44:16 +05:30
group.add_option( '-s',
'--selective',
action = 'store_true',
dest = 'selective',
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]')
parser.add_option_group(group)
parser.set_defaults(randomSeed = None,
grid = (16,16,16),
fraction = (1.0,1.0,1.0),
N = 20,
weights = False,
max = 0.0,
mean = 0.2,
sigma = 0.05,
microstructure = 1,
selective = False,
distance = 0.2,
numCandidates = 10,
)
(options,filenames) = parser.parse_args()
2020-03-17 15:19:30 +05:30
if filenames == []: filenames = [None]
2020-03-17 15:54:15 +05:30
fraction = np.array(options.fraction)
grid = np.array(options.grid)
if options.randomSeed is None: options.randomSeed = int(os.urandom(4).hex(), 16)
np.random.seed(options.randomSeed) # init random generators
random.seed(options.randomSeed)
for name in filenames:
damask.util.report(scriptName,name)
# --- sanity checks -------------------------------------------------------------------------
remarks = []
errors = []
2020-03-17 15:54:15 +05:30
if any(grid==0):
errors.append('zero grid dimension for {}.'.format(', '.join([['a','b','c'][x] for x in np.where(grid == 0)[0]])))
if options.N > grid.prod()/10.:
remarks.append('seed count exceeds 0.1 of grid points.')
2020-03-17 15:54:15 +05:30
if options.selective and 4./3.*np.pi*(options.distance/2.)**3*options.N > 0.5:
remarks.append('maximum recommended seed point count for given distance is {}'.
format(int(3./8./np.pi/(options.distance/2.)**3)))
if remarks != []: damask.util.croak(remarks)
if errors != []:
damask.util.croak(errors)
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:
2020-03-17 15:54:15 +05:30
n = np.maximum(np.ones(3),np.array(grid*fraction),
dtype=int,casting='unsafe') # find max grid indices within fraction
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))\
/ \
2020-03-17 15:54:15 +05:30
(n/fraction)).T # pick options.N of those, rattle position,
# and rescale to fall within fraction
else:
seeds = np.zeros((options.N,3),dtype=float) # seed positions array
2020-03-17 15:54:15 +05:30
seeds[0] = np.random.random(3)*grid/max(grid)
i = 1 # start out with one given point
if i%(options.N/100.) < 1: damask.util.croak('.',False)
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
i += 1
if i%(options.N/100.) < 1: damask.util.croak('.',False)
damask.util.croak('')
seeds = seeds.T # prepare shape for stacking
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)]
2020-03-17 15:40:02 +05:30
data = np.transpose(np.vstack(tuple([seeds,
grainEuler,
np.arange(options.microstructure,options.microstructure + options.N),
] + (weights if options.weights else [])
)))
2020-03-17 15:40:02 +05:30
comments = [scriptID + ' ' + ' '.join(sys.argv[1:]),
2020-03-17 15:54:15 +05:30
'grid\ta {}\tb {}\tc {}'.format(*grid),
2020-03-17 15:40:02 +05:30
'randomSeed\t{}'.format(options.randomSeed),
]
2020-03-17 15:54:15 +05:30
shapes = {'pos':(3,),'euler':(3,),'microstructure':(1,)}
if options.weights: shapes['weight'] = (1,)
2020-03-17 15:54:15 +05:30
table = damask.Table(data,shapes,comments)
table.to_ASCII(sys.stdout if name is None else name)