2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-12-04 04:02:28 +05:30
|
|
|
|
2019-05-25 14:06:46 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from io import StringIO
|
2015-12-04 04:02:28 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
|
2019-05-26 15:41:30 +05:30
|
|
|
from scipy import ndimage
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
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-12-04 04:02:28 +05:30
|
|
|
|
2019-05-26 15:41:30 +05:30
|
|
|
|
2015-12-04 04:02:28 +05:30
|
|
|
def mostFrequent(arr):
|
2019-05-25 14:06:46 +05:30
|
|
|
unique, inverse = np.unique(arr, return_inverse=True)
|
|
|
|
return unique[np.argmax(np.bincount(inverse))]
|
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 = """
|
2019-05-26 15:41:30 +05:30
|
|
|
Smooth microstructure by selecting most frequent index within given stencil at each location.
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
""", version=scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-s','--stencil',
|
|
|
|
dest = 'stencil',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'size of smoothing stencil [%default]')
|
|
|
|
|
2019-05-25 14:06:46 +05:30
|
|
|
parser.set_defaults(stencil = 3)
|
2015-12-04 04:02:28 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
damask.util.report(scriptName,name)
|
2019-05-25 14:06:46 +05:30
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
2019-05-26 15:41:30 +05:30
|
|
|
microstructure = geom.get_microstructure()
|
2019-05-25 14:06:46 +05:30
|
|
|
|
|
|
|
microstructure = ndimage.filters.generic_filter(microstructure,mostFrequent,
|
|
|
|
size=(options.stencil,)*3).astype(microstructure.dtype)
|
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
damask.util.croak(geom.update(microstructure))
|
2019-05-28 06:44:09 +05:30
|
|
|
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2019-05-25 14:06:46 +05:30
|
|
|
if name is None:
|
|
|
|
sys.stdout.write(str(geom.show()))
|
|
|
|
else:
|
|
|
|
geom.to_file(name)
|