2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2019-05-25 13:44:53 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2014-11-18 13:30:45 +05:30
|
|
|
from optparse import OptionParser
|
2019-05-25 13:44:53 +05:30
|
|
|
from io import StringIO
|
2019-05-27 00:06:41 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2019-05-25 13:44:53 +05:30
|
|
|
import damask
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-07-10 14:45:42 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
|
|
|
Translate microstructure indices (shift or substitute) and/or geometry origin.
|
2014-11-18 13:30:45 +05:30
|
|
|
|
|
|
|
""", version=scriptID)
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-o', '--origin',
|
|
|
|
dest = 'origin',
|
|
|
|
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
|
|
|
|
help = 'offset from old to new origin of grid')
|
|
|
|
parser.add_option('-m', '--microstructure',
|
|
|
|
dest = 'microstructure',
|
|
|
|
type = 'int', metavar = 'int',
|
2019-05-27 00:06:41 +05:30
|
|
|
help = 'offset from old to new microstructure indices (after substitution)')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-s', '--substitute',
|
|
|
|
dest = 'substitute',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'substitutions of microstructure indices from,to,from,to,...')
|
|
|
|
|
|
|
|
parser.set_defaults(origin = (0.0,0.0,0.0),
|
|
|
|
microstructure = 0,
|
2019-05-25 13:44:53 +05:30
|
|
|
substitute = []
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2012-09-13 15:42:00 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
sub = {}
|
2018-11-16 13:32:31 +05:30
|
|
|
for i in range(len(options.substitute)//2): # split substitution list into "from" -> "to"
|
2012-09-13 15:42:00 +05:30
|
|
|
sub[int(options.substitute[i*2])] = int(options.substitute[i*2+1])
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2019-05-25 13:44:53 +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()
|
|
|
|
new = np.copy(microstructure)
|
2019-05-25 13:44:53 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
for k, v in sub.items(): new[microstructure==k] = v # substitute microstructure indices and shift
|
|
|
|
|
|
|
|
microstructure += options.microstructure # constant shift
|
2019-05-25 13:44:53 +05:30
|
|
|
|
|
|
|
for i,line in enumerate(geom.comments):
|
|
|
|
if line.lower().strip().startswith('origin'):
|
|
|
|
origin= np.array([float(line.split()[j]) for j in [2,4,6]]) # assume correct order (x,y,z)
|
|
|
|
origin += np.array(origin)
|
|
|
|
geom.comments[i] = 'origin x {} y {} z {}'.format(*origin)
|
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
damask.util.croak(geom.update(microstructure))
|
2019-05-25 13:44:53 +05:30
|
|
|
geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2019-05-25 13:44:53 +05:30
|
|
|
if name is None:
|
|
|
|
sys.stdout.write(str(geom.show()))
|
|
|
|
else:
|
|
|
|
geom.to_file(name)
|