clearer structure and faster

This commit is contained in:
Martin Diehl 2020-03-17 10:39:33 +01:00
parent 41ca00a020
commit ba8eab646b
2 changed files with 30 additions and 23 deletions

2
examples/.gitignore vendored
View File

@ -3,4 +3,6 @@
*.xdmf *.xdmf
*.sta *.sta
*.vt* *.vt*
*.geom
*.seeds
postProc postProc

View File

@ -15,7 +15,7 @@ scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version]) scriptID = ' '.join([scriptName,damask.version])
def laguerreTessellation(undeformed, coords, weights, grains, periodic = True, cpus = 2): def Laguerre_tesselation(grid, coords, weights, grains, periodic = True, cpus = 2):
def findClosestSeed(fargs): def findClosestSeed(fargs):
point, seeds, myWeights = fargs point, seeds, myWeights = fargs
@ -62,28 +62,31 @@ def laguerreTessellation(undeformed, coords, weights, grains, periodic = True, c
try: seeds = np.append(seeds, coords+vec, axis=0) # ... (1+a,2+a,3+a,...,1+z,2+z,3+z) try: seeds = np.append(seeds, coords+vec, axis=0) # ... (1+a,2+a,3+a,...,1+z,2+z,3+z)
except NameError: seeds = coords+vec except NameError: seeds = coords+vec
if (repeatweights == 0.0).all(): # standard Voronoi (no weights, KD tree) damask.util.croak('...using {} cpu{}'.format(options.cpus, 's' if options.cpus > 1 else ''))
myKDTree = spatial.cKDTree(seeds) arguments = [[arg,seeds,repeatweights] for arg in list(grid)]
devNull,closestSeeds = myKDTree.query(undeformed)
else:
damask.util.croak('...using {} cpu{}'.format(options.cpus, 's' if options.cpus > 1 else ''))
arguments = [[arg,seeds,repeatweights] for arg in list(undeformed)]
if cpus > 1: # use multithreading if cpus > 1: # use multithreading
pool = multiprocessing.Pool(processes = cpus) # initialize workers pool = multiprocessing.Pool(processes = cpus) # initialize workers
result = pool.map_async(findClosestSeed, arguments) # evaluate function in parallel result = pool.map_async(findClosestSeed, arguments) # evaluate function in parallel
pool.close() pool.close()
pool.join() pool.join()
closestSeeds = np.array(result.get()).flatten() closestSeeds = np.array(result.get()).flatten()
else: else:
closestSeeds = np.zeros(len(arguments),dtype='i') closestSeeds = np.zeros(len(arguments),dtype='i')
for i,arg in enumerate(arguments): for i,arg in enumerate(arguments):
closestSeeds[i] = findClosestSeed(arg) closestSeeds[i] = findClosestSeed(arg)
# closestSeed is modulo number of original seed points (i.e. excluding periodic copies) # closestSeed is modulo number of original seed points (i.e. excluding periodic copies)
return grains[closestSeeds%coords.shape[0]] return grains[closestSeeds%coords.shape[0]]
def Voronoi_tessellation(grid, coords, grains, size, periodic = True):
KDTree = spatial.cKDTree(coords,boxsize=size) if periodic else spatial.cKDTree(coords)
devNull,closestSeeds = KDTree.query(grid)
return grains[closestSeeds]
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# MAIN # MAIN
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -221,7 +224,8 @@ for name in filenames:
hasGrains = table.label_dimension(options.microstructure) == 1 hasGrains = table.label_dimension(options.microstructure) == 1
hasEulers = table.label_dimension(options.eulers) == 3 hasEulers = table.label_dimension(options.eulers) == 3
hasWeights = table.label_dimension(options.weight) == 1 and options.laguerre if options.laguerre and table.label_dimension(options.weight) != 1:
errors.append('missing seed weights...')
for i in range(3): for i in range(3):
if info['size'][i] <= 0.0: # any invalid size? if info['size'][i] <= 0.0: # any invalid size?
@ -239,8 +243,6 @@ for name in filenames:
else: labels += [options.eulers] else: labels += [options.eulers]
if not hasGrains: remarks.append('missing seed microstructure indices...') if not hasGrains: remarks.append('missing seed microstructure indices...')
else: labels += [options.microstructure] else: labels += [options.microstructure]
if options.laguerre and not hasWeights: remarks.append('missing seed weights...')
else: labels += [options.weight]
if remarks != []: damask.util.croak(remarks) if remarks != []: damask.util.croak(remarks)
if errors != []: if errors != []:
@ -257,15 +259,18 @@ for name in filenames:
else np.zeros(3*len(coords)) else np.zeros(3*len(coords))
grains = table.data[:,table.label_indexrange(options.microstructure)].astype(int) if hasGrains \ grains = table.data[:,table.label_indexrange(options.microstructure)].astype(int) if hasGrains \
else np.arange(len(coords))+1 else np.arange(len(coords))+1
weights = table.data[:,table.label_indexrange(options.weight)] if hasWeights \
else np.zeros(len(coords))
grainIDs = np.unique(grains).astype('i') grainIDs = np.unique(grains).astype('i')
NgrainIDs = len(grainIDs) NgrainIDs = len(grainIDs)
# --- tessellate microstructure ------------------------------------------------------------ # --- tessellate microstructure ------------------------------------------------------------
grid = damask.grid_filters.cell_coord0(info['grid'],info['size']).reshape(-1,3) grid = damask.grid_filters.cell_coord0(info['grid'],info['size']).reshape(-1,3)
damask.util.croak('tessellating...') damask.util.croak('tessellating...')
indices = laguerreTessellation(grid, coords, weights, grains, options.periodic, options.cpus) if options.laguerre:
weights = table.data[:,table.label_indexrange(options.weight)]
indices = Laguerre_tessellation(grid, coords, weights, grains, options.periodic, options.cpus)
else:
indices = Voronoi_tessellation(grid, coords, grains, info['size'], options.periodic)
config_header = [] config_header = []
if options.config: if options.config: