2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-12-04 04:02:28 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 02:42:04 +05:30
|
|
|
import os,sys,math
|
2015-12-04 04:02:28 +05:30
|
|
|
import numpy as np
|
|
|
|
import damask
|
|
|
|
from scipy import ndimage
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
def mostFrequent(arr):
|
2016-07-25 23:09:09 +05:30
|
|
|
return np.argmax(np.bincount(arr.astype('int')))
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
2019-02-17 12:30:26 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
2015-12-04 04:02:28 +05:30
|
|
|
Smooth geometry by selecting most frequent microstructure index within given stencil at each location.
|
|
|
|
|
|
|
|
""", version=scriptID)
|
|
|
|
|
|
|
|
|
|
|
|
parser.add_option('-s','--stencil',
|
|
|
|
dest = 'stencil',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'size of smoothing stencil [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(stencil = 3,
|
|
|
|
)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
2016-04-16 04:24:09 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
labeled = False)
|
2015-12-04 04:02:28 +05:30
|
|
|
except: continue
|
|
|
|
damask.util.report(scriptName,name)
|
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
2019-05-23 22:33:24 +05:30
|
|
|
damask.util.report_geom(info)
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
errors = []
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
|
|
|
if errors != []:
|
|
|
|
damask.util.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
|
|
|
|
2016-04-16 04:24:09 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid']).reshape(info['grid'],order='F') # read microstructure
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
2016-07-25 23:09:09 +05:30
|
|
|
microstructure = ndimage.filters.generic_filter(microstructure,mostFrequent,size=(options.stencil,)*3).astype('int_')
|
2019-05-23 22:33:24 +05:30
|
|
|
newInfo = {'microstructures': len(np.unique(microstructure))}
|
2016-07-25 23:09:09 +05:30
|
|
|
|
|
|
|
# --- report ---------------------------------------------------------------------------------------
|
|
|
|
if ( newInfo['microstructures'] != info['microstructures']):
|
|
|
|
damask.util.croak('--> microstructures: %i'%newInfo['microstructures'])
|
|
|
|
info['microstructures'] == newInfo['microstructures']
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
# --- write header ---------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.info_clear()
|
|
|
|
table.info_append([scriptID + ' ' + ' '.join(sys.argv[1:]),])
|
|
|
|
table.head_putGeom(info)
|
|
|
|
table.info_append([extra_header])
|
|
|
|
table.labels_clear()
|
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
|
|
|
|
2019-05-23 22:33:24 +05:30
|
|
|
formatwidth = int(math.floor(math.log10(np.nanmax(microstructure))+1))
|
2015-12-04 04:02:28 +05:30
|
|
|
table.data = microstructure.reshape((info['grid'][0],np.prod(info['grid'][1:])),order='F').transpose()
|
2019-05-23 22:33:24 +05:30
|
|
|
table.data_writeArray('%{}i'.format(formatwidth),delimiter = ' ')
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|