DAMASK_EICMD/processing/pre/geom_rescale.py

61 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-25 14:06:46 +05:30
import os
import sys
from io import StringIO
from optparse import OptionParser
2019-11-24 23:55:01 +05:30
import numpy as np
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
Scales independently in x, y, and z direction in terms of grid and/or size.
Either absolute values or relative factors (like "0.25x") can be used.
""", version = scriptID)
parser.add_option('-g', '--grid',
dest = 'grid',
type = 'string', nargs = 3, metavar = 'string string string',
help = 'a,b,c grid of hexahedral box')
parser.add_option('-s', '--size',
dest = 'size',
type = 'string', nargs = 3, metavar = 'string string string',
help = 'x,y,z size of hexahedral box')
(options, filenames) = parser.parse_args()
if filenames == []: filenames = [None]
for name in filenames:
2015-09-24 22:22:58 +05:30
damask.util.report(scriptName,name)
2019-05-30 17:37:49 +05:30
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
2019-05-30 17:00:18 +05:30
grid = geom.get_grid()
2019-05-25 14:06:46 +05:30
size = geom.get_size()
new_grid = grid if options.grid is None else \
np.array([int(o*float(n.lower().replace('x',''))) if n.lower().endswith('x') \
else int(n) for o,n in zip(grid,options.grid)],dtype=int)
new_size = size if options.size is None else \
np.array([o*float(n.lower().replace('x','')) if n.lower().endswith('x') \
else float(n) for o,n in zip(size,options.size)],dtype=float)
2019-11-24 19:43:26 +05:30
geom.scale(new_grid)
2019-11-24 22:51:05 +05:30
damask.util.croak(geom.update(microstructure = None,size = new_size))
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
2019-11-24 23:32:19 +05:30
geom.to_file(sys.stdout if name is None else name,pack=False)