2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2019-05-30 05:23:45 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2019-05-29 11:29:17 +05:30
|
|
|
import numpy as np
|
|
|
|
import damask
|
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
from io import StringIO
|
|
|
|
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])
|
2014-11-07 16:37:03 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
2013-05-15 22:53:21 +05:30
|
|
|
# MAIN
|
2014-11-07 16:37:03 +05:30
|
|
|
# --------------------------------------------------------------------
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [geomfile(s)]', description = """
|
|
|
|
Changes the (three-dimensional) canvas of a spectral geometry description.
|
2016-04-24 21:54:00 +05:30
|
|
|
Grid can be given as absolute or relative values, e.g. 16 16 16 or 2x 0.5x 32.
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
""", version = scriptID)
|
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
parser.add_option('-g',
|
|
|
|
'--grid',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'grid',
|
|
|
|
type = 'string', nargs = 3, metavar = ' '.join(['string']*3),
|
2019-05-30 05:23:45 +05:30
|
|
|
help = 'a,b,c grid of hexahedral box. [auto]')
|
|
|
|
parser.add_option('-o',
|
|
|
|
'--offset',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'offset',
|
|
|
|
type = 'int', nargs = 3, metavar = ' '.join(['int']*3),
|
2015-11-16 16:22:56 +05:30
|
|
|
help = 'a,b,c offset from old to new origin of grid [%default]')
|
2019-05-30 05:23:45 +05:30
|
|
|
parser.add_option('-f',
|
|
|
|
'--fill',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'fill',
|
2019-05-30 05:23:45 +05:30
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
help = '(background) canvas grain index. "0" selects maximum microstructure index + 1 [%default]')
|
2019-05-26 16:36:22 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
parser.set_defaults(grid = ['0','0','0'],
|
|
|
|
offset = (0,0,0),
|
|
|
|
)
|
2012-06-21 18:27:14 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 00:13:11 +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-02-28 06:06:02 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
|
|
|
|
|
2019-05-27 00:06:41 +05:30
|
|
|
grid = geom.get_grid()
|
2019-05-30 05:23:45 +05:30
|
|
|
size = geom.get_size()
|
|
|
|
origin = geom.get_origin()
|
|
|
|
microstructure = geom.get_microstructure()
|
|
|
|
fill = np.nanmax(microstructure)+1 if options.fill is None else options.fill
|
|
|
|
dtype = float if np.isnan(fill) or int(fill) != fill or microstructure.dtype==np.float else int
|
2019-05-28 06:44:09 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
new_grid = 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_grid = np.where(new_grid > 0, new_grid,grid)
|
|
|
|
|
|
|
|
microstructure_cropped = np.zeros(new_grid,dtype=dtype)
|
|
|
|
microstructure_cropped.fill(fill)
|
2019-05-27 00:06:41 +05:30
|
|
|
|
2019-05-30 05:23:45 +05:30
|
|
|
xindex = np.arange(max(options.offset[0],0),min(options.offset[0]+new_grid[0],grid[0]))
|
|
|
|
yindex = np.arange(max(options.offset[1],0),min(options.offset[1]+new_grid[1],grid[1]))
|
|
|
|
zindex = np.arange(max(options.offset[2],0),min(options.offset[2]+new_grid[2],grid[2]))
|
|
|
|
translate_x = [i - options.offset[0] for i in xindex]
|
|
|
|
translate_y = [i - options.offset[1] for i in yindex]
|
|
|
|
translate_z = [i - options.offset[2] for i in zindex]
|
|
|
|
if 0 in map(len,[xindex,yindex,zindex,translate_x,translate_y,translate_z]):
|
|
|
|
damask.util.croak('invaldid combination of grid and offset.')
|
|
|
|
continue
|
|
|
|
microstructure_cropped[min(translate_x):max(translate_x)+1,
|
|
|
|
min(translate_y):max(translate_y)+1,
|
|
|
|
min(translate_z):max(translate_z)+1] \
|
|
|
|
= microstructure[min(xindex):max(xindex)+1,
|
|
|
|
min(yindex):max(yindex)+1,
|
|
|
|
min(zindex):max(zindex)+1]
|
|
|
|
|
|
|
|
new_size = size/grid*new_grid if np.all(grid > 0) else new_grid
|
|
|
|
new_origin = origin + (size/grid if np.all(grid > 0) else new_size/new_grid) * options.offset
|
|
|
|
|
|
|
|
damask.util.croak(geom.update(microstructure=microstructure_cropped,
|
|
|
|
size=new_size,
|
|
|
|
origin=new_origin,
|
|
|
|
))
|
2019-05-28 06:44:09 +05:30
|
|
|
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
2019-05-27 00:06:41 +05:30
|
|
|
|
|
|
|
if name is None:
|
|
|
|
sys.stdout.write(str(geom.show()))
|
|
|
|
else:
|
|
|
|
geom.to_file(name)
|