2019-02-15 04:54:09 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import random
|
2014-09-29 15:49:49 +05:30
|
|
|
from optparse import OptionParser
|
2019-02-15 04:54:09 +05:30
|
|
|
from io import StringIO
|
2020-03-18 18:17:09 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
import damask
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-04-11 02:17:20 +05:30
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
mismatch = None
|
|
|
|
currentSeedsName = None
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------------------
|
|
|
|
class myThread (threading.Thread):
|
2020-02-21 15:30:53 +05:30
|
|
|
"""Perturb seed in seed file, performes Voronoi tessellation, evaluates, and updates best match."""
|
2016-03-03 19:14:51 +05:30
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
def __init__(self, threadID):
|
2020-02-21 15:30:53 +05:30
|
|
|
"""Threading class with thread ID."""
|
2014-09-29 15:49:49 +05:30
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.threadID = threadID
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
global bestSeedsUpdate
|
|
|
|
global bestSeedsVFile
|
2020-09-23 05:33:41 +05:30
|
|
|
global nMaterials
|
2014-09-29 15:49:49 +05:30
|
|
|
global delta
|
|
|
|
global points
|
|
|
|
global target
|
|
|
|
global match
|
|
|
|
global baseFile
|
2015-04-10 23:38:17 +05:30
|
|
|
global maxSeeds
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
s.acquire()
|
2014-09-29 21:25:17 +05:30
|
|
|
bestMatch = match
|
2014-09-29 15:49:49 +05:30
|
|
|
s.release()
|
2020-03-18 18:17:09 +05:30
|
|
|
|
2014-10-09 16:31:07 +05:30
|
|
|
random.seed(options.randomSeed+self.threadID) # initializes to given seeds
|
2016-03-03 19:14:51 +05:30
|
|
|
knownSeedsUpdate = bestSeedsUpdate -1.0 # trigger update of local best seeds
|
2014-10-09 16:31:07 +05:30
|
|
|
randReset = True # aquire new direction
|
2020-03-18 18:17:09 +05:30
|
|
|
|
2016-03-03 19:14:51 +05:30
|
|
|
myBestSeedsVFile = StringIO() # store local copy of best seeds file
|
|
|
|
perturbedSeedsVFile = StringIO() # perturbed best seeds file
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2014-10-09 16:31:07 +05:30
|
|
|
#--- still not matching desired bin class ----------------------------------------------------------
|
2014-09-29 21:25:17 +05:30
|
|
|
while bestMatch < options.threshold:
|
2016-03-03 19:14:51 +05:30
|
|
|
s.acquire() # ensure only one thread acces global data
|
|
|
|
if bestSeedsUpdate > knownSeedsUpdate: # write best fit to virtual file
|
2014-09-29 15:49:49 +05:30
|
|
|
knownSeedsUpdate = bestSeedsUpdate
|
2020-03-18 18:17:09 +05:30
|
|
|
bestSeedsVFile.seek(0)
|
2014-09-29 15:49:49 +05:30
|
|
|
myBestSeedsVFile.close()
|
|
|
|
myBestSeedsVFile = StringIO()
|
|
|
|
i=0
|
2020-03-20 11:12:13 +05:30
|
|
|
myBestSeedsVFile.writelines(bestSeedsVFile.readlines())
|
2014-09-29 15:49:49 +05:30
|
|
|
s.release()
|
2020-03-18 18:17:09 +05:30
|
|
|
|
2014-10-09 16:31:07 +05:30
|
|
|
if randReset: # new direction because current one led to worse fit
|
2015-09-21 02:33:10 +05:30
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
randReset = False
|
2015-04-14 18:12:35 +05:30
|
|
|
|
|
|
|
NmoveGrains = random.randrange(1,maxSeeds)
|
|
|
|
selectedMs = []
|
|
|
|
direction = []
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(NmoveGrains):
|
2020-09-23 05:33:41 +05:30
|
|
|
selectedMs.append(random.randrange(1,nMaterials))
|
2015-04-14 18:12:35 +05:30
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
direction.append((np.random.random()-0.5)*delta)
|
|
|
|
|
2014-10-09 16:31:07 +05:30
|
|
|
perturbedSeedsVFile.close() # reset virtual file
|
2014-09-29 15:49:49 +05:30
|
|
|
perturbedSeedsVFile = StringIO()
|
2020-03-18 18:17:09 +05:30
|
|
|
myBestSeedsVFile.seek(0)
|
2014-10-09 16:31:07 +05:30
|
|
|
|
2020-09-21 00:15:06 +05:30
|
|
|
perturbedSeedsTable = damask.Table.load(myBestSeedsVFile)
|
2020-03-18 18:17:09 +05:30
|
|
|
coords = perturbedSeedsTable.get('pos')
|
2015-09-21 02:33:10 +05:30
|
|
|
i = 0
|
2020-03-18 18:17:09 +05:30
|
|
|
for ms,coord in enumerate(coords):
|
2015-04-14 18:12:35 +05:30
|
|
|
if ms in selectedMs:
|
2020-03-18 18:17:09 +05:30
|
|
|
newCoords=coord+direction[i]
|
2016-03-03 19:14:51 +05:30
|
|
|
newCoords=np.where(newCoords>=1.0,newCoords-1.0,newCoords) # ensure that the seeds remain in the box
|
2014-12-05 16:05:56 +05:30
|
|
|
newCoords=np.where(newCoords <0.0,newCoords+1.0,newCoords)
|
2020-03-18 18:17:09 +05:30
|
|
|
coords[i]=newCoords
|
2015-09-21 02:33:10 +05:30
|
|
|
direction[i]*=2.
|
|
|
|
i+= 1
|
2020-09-21 00:15:06 +05:30
|
|
|
perturbedSeedsTable.set('pos',coords).save(perturbedSeedsVFile,legacy=True)
|
2020-03-18 18:17:09 +05:30
|
|
|
|
|
|
|
#--- do tesselation with perturbed seed file ------------------------------------------------------
|
2020-09-25 17:38:29 +05:30
|
|
|
perturbedGeom = damask.Geom.from_Voronoi_tessellation(options.grid,np.ones(3),coords)
|
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
|
|
|
|
#--- evaluate current seeds file ------------------------------------------------------------------
|
2020-09-24 02:57:15 +05:30
|
|
|
myNmaterials = len(np.unique(perturbedGeom.material))
|
|
|
|
currentData = np.bincount(perturbedGeom.material.ravel())[1:]/points
|
2014-09-29 15:49:49 +05:30
|
|
|
currentError=[]
|
|
|
|
currentHist=[]
|
2020-09-23 05:33:41 +05:30
|
|
|
for i in range(nMaterials): # calculate the deviation in all bins per histogram
|
2014-09-29 15:49:49 +05:30
|
|
|
currentHist.append(np.histogram(currentData,bins=target[i]['bins'])[0])
|
|
|
|
currentError.append(np.sqrt(np.square(np.array(target[i]['histogram']-currentHist[i])).sum()))
|
2016-03-03 19:14:51 +05:30
|
|
|
|
|
|
|
# as long as not all grains are within the range of the target, use the deviation to left and right as error
|
2020-03-18 18:17:09 +05:30
|
|
|
if currentError[0]>0.0:
|
|
|
|
currentError[0] *=((target[0]['bins'][0]-np.min(currentData))**2.0+
|
2015-04-14 18:12:35 +05:30
|
|
|
(target[0]['bins'][1]-np.max(currentData))**2.0)**0.5 # norm of deviations by number of usual bin deviation
|
2015-04-11 02:17:20 +05:30
|
|
|
s.acquire() # do the evaluation serially
|
2014-09-29 21:25:17 +05:30
|
|
|
bestMatch = match
|
2014-10-09 16:31:07 +05:30
|
|
|
#--- count bin classes with no mismatch ----------------------------------------------------------------------
|
2014-09-29 21:25:17 +05:30
|
|
|
myMatch=0
|
2020-09-23 05:33:41 +05:30
|
|
|
for i in range(nMaterials):
|
2014-09-29 21:25:17 +05:30
|
|
|
if currentError[i] > 0.0: break
|
|
|
|
myMatch = i+1
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2020-09-23 05:33:41 +05:30
|
|
|
if myNmaterials == nMaterials:
|
|
|
|
for i in range(min(nMaterials,myMatch+options.bins)):
|
2015-04-11 02:17:20 +05:30
|
|
|
if currentError[i] > target[i]['error']: # worse fitting, next try
|
2014-09-29 15:49:49 +05:30
|
|
|
randReset = True
|
|
|
|
break
|
2015-04-11 02:17:20 +05:30
|
|
|
elif currentError[i] < target[i]['error']: # better fit
|
|
|
|
bestSeedsUpdate = time.time() # save time of better fit
|
2016-03-23 21:32:51 +05:30
|
|
|
damask.util.croak('Thread {:d}: Better match ({:d} bins, {:6.4f} --> {:6.4f})'\
|
|
|
|
.format(self.threadID,i+1,target[i]['error'],currentError[i]))
|
|
|
|
damask.util.croak(' target: '+np.array_str(target[i]['histogram']))
|
|
|
|
damask.util.croak(' best: '+np.array_str(currentHist[i]))
|
2015-04-11 02:17:20 +05:30
|
|
|
currentSeedsName = baseFile+'_'+str(bestSeedsUpdate).replace('.','-') # name of new seed file (use time as unique identifier)
|
2020-03-18 18:17:09 +05:30
|
|
|
perturbedSeedsVFile.seek(0)
|
2014-09-29 15:49:49 +05:30
|
|
|
bestSeedsVFile.close()
|
|
|
|
bestSeedsVFile = StringIO()
|
2014-09-29 21:25:17 +05:30
|
|
|
sys.stdout.flush()
|
2015-04-11 02:17:20 +05:30
|
|
|
with open(currentSeedsName+'.seeds','w') as currentSeedsFile: # write to new file
|
2014-09-29 15:49:49 +05:30
|
|
|
for line in perturbedSeedsVFile:
|
|
|
|
currentSeedsFile.write(line)
|
|
|
|
bestSeedsVFile.write(line)
|
2020-09-23 05:33:41 +05:30
|
|
|
for j in range(nMaterials): # save new errors for all bins
|
2014-09-29 15:49:49 +05:30
|
|
|
target[j]['error'] = currentError[j]
|
2015-04-11 02:17:20 +05:30
|
|
|
if myMatch > match: # one or more new bins have no deviation
|
2016-03-23 21:32:51 +05:30
|
|
|
damask.util.croak( 'Stage {:d} cleared'.format(myMatch))
|
2014-09-29 21:25:17 +05:30
|
|
|
match=myMatch
|
|
|
|
sys.stdout.flush()
|
2014-09-29 15:49:49 +05:30
|
|
|
break
|
2020-09-23 05:33:41 +05:30
|
|
|
if i == min(nMaterials,myMatch+options.bins)-1: # same quality as before: take it to keep on moving
|
2014-12-18 23:33:36 +05:30
|
|
|
bestSeedsUpdate = time.time()
|
2020-03-18 18:17:09 +05:30
|
|
|
perturbedSeedsVFile.seek(0)
|
2014-12-18 23:33:36 +05:30
|
|
|
bestSeedsVFile.close()
|
|
|
|
bestSeedsVFile = StringIO()
|
2020-03-20 11:12:13 +05:30
|
|
|
bestSeedsVFile.writelines(perturbedSeedsVFile.readlines())
|
2020-09-23 05:33:41 +05:30
|
|
|
for j in range(nMaterials):
|
2014-12-18 23:33:36 +05:30
|
|
|
target[j]['error'] = currentError[j]
|
|
|
|
randReset = True
|
2015-04-11 02:17:20 +05:30
|
|
|
else: #--- not all grains are tessellated
|
2020-09-23 05:33:41 +05:30
|
|
|
damask.util.croak('Thread {:d}: Material mismatch ({:d} material indices mapped)'\
|
|
|
|
.format(self.threadID,myNmaterials))
|
2014-10-09 16:31:07 +05:30
|
|
|
randReset = True
|
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
s.release()
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2020-11-14 23:30:51 +05:30
|
|
|
parser = OptionParser(usage='%prog options [file[s]]', description = """
|
2014-09-29 15:49:49 +05:30
|
|
|
Monte Carlo simulation to produce seed file that gives same size distribution like given geometry file.
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
""", version = scriptID)
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
parser.add_option('-s','--seeds', dest='seedFile', metavar='string',
|
|
|
|
help='name of the intial seed file. If not found, a new one is created [%default]')
|
|
|
|
parser.add_option('-g','--grid', dest='grid', type='int', nargs=3, metavar='int int int',
|
|
|
|
help='a,b,c grid of hexahedral box [%default]')
|
|
|
|
parser.add_option('-t','--threads', dest='threads', type='int', metavar='int',
|
|
|
|
help='number of parallel executions [%default]')
|
|
|
|
parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int',
|
|
|
|
help='seed of random number generator [%default]')
|
|
|
|
parser.add_option('--target', dest='target', metavar='string',
|
|
|
|
help='name of the geom file with target distribution [%default]')
|
2014-09-29 21:25:17 +05:30
|
|
|
parser.add_option('--tolerance', dest='threshold', type='int', metavar='int',
|
|
|
|
help='stopping criterion (bin number) [%default]')
|
2020-03-18 18:17:09 +05:30
|
|
|
parser.add_option('--scale', dest='scale',type='float', metavar='float',
|
2014-09-29 15:49:49 +05:30
|
|
|
help='maximum moving distance of perturbed seed in pixel [%default]')
|
2014-12-18 23:33:36 +05:30
|
|
|
parser.add_option('--bins', dest='bins', type='int', metavar='int',
|
|
|
|
help='bins to sort beyond current best fit [%default]')
|
2015-04-10 23:38:17 +05:30
|
|
|
parser.add_option('--maxseeds', dest='maxseeds', type='int', metavar='int',
|
|
|
|
help='maximum number of seeds to move simulateneously [number of seeds]')
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2016-10-28 02:36:17 +05:30
|
|
|
parser.set_defaults(seedFile = 'seeds',
|
|
|
|
grid = (64,64,64),
|
|
|
|
threads = 2,
|
|
|
|
randomSeed = None,
|
|
|
|
target = 'geom',
|
|
|
|
threshold = 20,
|
|
|
|
bins = 15,
|
|
|
|
scale = 1.0,
|
|
|
|
maxseeds = 0)
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
options = parser.parse_args()[0]
|
|
|
|
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.report(scriptName,options.seedFile)
|
|
|
|
|
2016-03-03 19:14:51 +05:30
|
|
|
if options.randomSeed is None:
|
2020-02-21 15:30:53 +05:30
|
|
|
options.randomSeed = int(os.urandom(4).hex(),16)
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.croak(options.randomSeed)
|
2020-03-18 18:17:09 +05:30
|
|
|
delta = options.scale/np.array(options.grid)
|
2020-09-15 10:28:06 +05:30
|
|
|
baseFile = os.path.splitext(os.path.basename(options.seedFile))[0]
|
2016-10-25 00:46:29 +05:30
|
|
|
points = np.array(options.grid).prod().astype('float')
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
# ----------- calculate target distribution and bin edges
|
2020-09-15 10:28:06 +05:30
|
|
|
targetGeom = damask.Geom.load_ASCII(os.path.splitext(os.path.basename(options.target))[0]+'.geom')
|
2020-09-24 02:57:15 +05:30
|
|
|
nMaterials = len(np.unique(targetGeom.material))
|
2020-12-04 02:28:24 +05:30
|
|
|
targetVolFrac = np.bincount(targetGeom.material.flatten())/targetGeom.cells.prod().astype(np.float)
|
2020-09-15 10:28:06 +05:30
|
|
|
target = []
|
2020-09-23 05:33:41 +05:30
|
|
|
for i in range(1,nMaterials+1):
|
2015-09-17 01:14:11 +05:30
|
|
|
targetHist,targetBins = np.histogram(targetVolFrac,bins=i) #bin boundaries
|
|
|
|
target.append({'histogram':targetHist,'bins':targetBins})
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
# ----------- create initial seed file or open existing one
|
|
|
|
bestSeedsVFile = StringIO()
|
|
|
|
if os.path.isfile(os.path.splitext(options.seedFile)[0]+'.seeds'):
|
2020-09-25 17:38:29 +05:30
|
|
|
initial_seeds = damask.Table.load(os.path.splitext(options.seedFile)[0]+'.seeds').get('pos')
|
2014-09-29 15:49:49 +05:30
|
|
|
else:
|
2020-09-25 17:38:29 +05:30
|
|
|
initial_seeds = damask.seeds.from_random(np.ones(3),nMaterials,options.grid,options.randomSeed)
|
|
|
|
|
2014-09-29 15:49:49 +05:30
|
|
|
bestSeedsUpdate = time.time()
|
|
|
|
|
|
|
|
# ----------- tessellate initial seed file to get and evaluate geom file
|
2019-02-15 04:54:09 +05:30
|
|
|
bestSeedsVFile.seek(0)
|
2020-09-25 17:38:29 +05:30
|
|
|
initialGeom = damask.Geom.from_Voronoi_tessellation(options.grid,np.ones(3),initial_seeds)
|
2015-09-18 21:42:27 +05:30
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
if len(np.unique(targetGeom.material)) != nMaterials:
|
2020-09-23 05:33:41 +05:30
|
|
|
damask.util.croak('error. Material count mismatch')
|
2015-09-18 21:42:27 +05:30
|
|
|
|
2020-09-24 02:57:15 +05:30
|
|
|
initialData = np.bincount(initialGeom.material.flatten())/points
|
2020-09-23 05:33:41 +05:30
|
|
|
for i in range(nMaterials):
|
2014-09-29 15:49:49 +05:30
|
|
|
initialHist = np.histogram(initialData,bins=target[i]['bins'])[0]
|
|
|
|
target[i]['error']=np.sqrt(np.square(np.array(target[i]['histogram']-initialHist)).sum())
|
2014-09-29 21:25:17 +05:30
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
# as long as not all grain sizes are within the range, the error is the deviation to left and right
|
2015-04-11 02:17:20 +05:30
|
|
|
if target[0]['error'] > 0.0:
|
2015-04-14 18:12:35 +05:30
|
|
|
target[0]['error'] *=((target[0]['bins'][0]-np.min(initialData))**2.0+
|
|
|
|
(target[0]['bins'][1]-np.max(initialData))**2.0)**0.5
|
2014-09-29 21:25:17 +05:30
|
|
|
match=0
|
2020-09-23 05:33:41 +05:30
|
|
|
for i in range(nMaterials):
|
2014-09-29 21:25:17 +05:30
|
|
|
if target[i]['error'] > 0.0: break
|
|
|
|
match = i+1
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2015-04-10 23:38:17 +05:30
|
|
|
|
2020-03-18 18:17:09 +05:30
|
|
|
if options.maxseeds < 1:
|
2020-09-24 02:57:15 +05:30
|
|
|
maxSeeds = len(np.unique(initialGeom.material))
|
2015-04-14 18:12:35 +05:30
|
|
|
else:
|
|
|
|
maxSeeds = options.maxseeds
|
2015-04-10 23:38:17 +05:30
|
|
|
|
2016-03-23 21:32:51 +05:30
|
|
|
if match >0: damask.util.croak('Stage {:d} cleared'.format(match))
|
2014-12-18 23:33:36 +05:30
|
|
|
sys.stdout.flush()
|
2014-09-29 15:49:49 +05:30
|
|
|
|
2014-12-05 16:05:56 +05:30
|
|
|
# start mulithreaded monte carlo simulation
|
2020-09-15 10:28:06 +05:30
|
|
|
threads = []
|
|
|
|
s = threading.Semaphore(1)
|
2014-09-29 15:49:49 +05:30
|
|
|
|
|
|
|
for i in range(options.threads):
|
|
|
|
threads.append(myThread(i))
|
|
|
|
threads[i].start()
|
|
|
|
for i in range(options.threads):
|
|
|
|
threads[i].join()
|