DAMASK_EICMD/processing/pre/geom_vicinityOffset.py

96 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-25 11:48:48 +05:30
import os
import sys
from io import StringIO
2019-05-27 00:06:41 +05:30
from optparse import OptionParser
from scipy import ndimage
import numpy as np
import damask
2019-05-27 00:06:41 +05:30
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
2019-05-27 00:06:41 +05:30
def taintedNeighborhood(stencil,trigger=[],size=1):
me = stencil[stencil.shape[0]//2]
if len(trigger) == 0:
return np.any(stencil != me)
if me in trigger:
trigger = set(trigger)
trigger.remove(me)
trigger = list(trigger)
return np.any(np.in1d(stencil,np.array(trigger)))
2019-05-27 00:06:41 +05:30
2013-05-14 23:21:53 +05:30
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
2019-05-27 00:06:41 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
Offset microstructure index for points which see a microstructure different from themselves
(or listed as triggers) within a given (cubic) vicinity, i.e. within the region close to a grain/phase boundary.
""", version = scriptID)
parser.add_option('-v', '--vicinity',
dest = 'vicinity',
type = 'int', metavar = 'int',
help = 'voxel distance checked for presence of other microstructure [%default]')
parser.add_option('-m', '--microstructureoffset',
dest='offset',
type = 'int', metavar = 'int',
2019-05-27 00:06:41 +05:30
help='offset (positive or negative) to tag microstructure indices, defaults to max microstructure index + 1')
parser.add_option('-t', '--trigger',
2019-05-27 00:06:41 +05:30
dest = 'trigger',
action = 'extend', metavar = '<int LIST>',
help = 'list of microstructure indices triggering a change')
parser.add_option('-n', '--nonperiodic',
dest = 'mode',
action = 'store_const', const = 'nearest',
help = 'assume geometry to be non-periodic')
parser.set_defaults(vicinity = 1,
trigger = [],
mode = 'wrap',
)
(options, filenames) = parser.parse_args()
2019-05-27 00:06:41 +05:30
options.trigger = np.array(options.trigger, dtype=int)
if filenames == []: filenames = [None]
for name in filenames:
2019-05-25 13:44:53 +05:30
damask.util.report(scriptName,name)
2019-05-25 11:48:48 +05:30
if name is None:
virt_file = StringIO(''.join(sys.stdin.read()))
geom = damask.Geom.from_file(virt_file)
else:
geom = damask.Geom.from_file(name)
2019-05-27 00:06:41 +05:30
damask.util.croak(geom)
microstructure = geom.get_microstructure()
2019-05-27 00:06:41 +05:30
offset = options.offset if options.offset is not None else np.nanmax(microstructure)
microstructure = np.where(ndimage.filters.generic_filter(microstructure,
taintedNeighborhood,
size=1+2*options.vicinity,mode=options.mode,
extra_arguments=(),
extra_keywords={"trigger":options.trigger,"size":1+2*options.vicinity}),
2019-05-27 00:06:41 +05:30
microstructure + offset,microstructure)
2019-05-27 00:06:41 +05:30
damask.util.croak(geom.update(microstructure))
2019-05-25 11:48:48 +05:30
geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
2019-05-25 11:48:48 +05:30
if name is None:
2019-05-25 13:44:53 +05:30
sys.stdout.write(str(geom.show()))
2019-05-25 11:48:48 +05:30
else:
geom.to_file(name)