2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2012-06-21 18:27:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 02:42:04 +05:30
|
|
|
import os,sys,math
|
2014-11-07 16:37:03 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2013-06-30 06:04:16 +05:30
|
|
|
import damask
|
2014-11-07 16:37:03 +05:30
|
|
|
|
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
|
|
|
|
2016-05-12 12:24:34 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog option(s) [geomfile(s)]', description = """
|
2012-06-21 18:27:14 +05:30
|
|
|
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)
|
|
|
|
|
2016-04-24 21:54:00 +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),
|
2016-04-24 21:54:00 +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]')
|
2016-04-24 21:54:00 +05:30
|
|
|
parser.add_option('-f',
|
|
|
|
'--fill',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'fill',
|
2016-03-18 03:39:22 +05:30
|
|
|
type = 'float', metavar = 'float',
|
2015-08-08 00:33:26 +05:30
|
|
|
help = '(background) canvas grain index. "0" selects maximum microstructure index + 1 [%default]')
|
2016-03-18 03:39:22 +05:30
|
|
|
parser.add_option('--float',
|
2019-05-23 22:33:24 +05:30
|
|
|
dest = 'float',
|
2016-03-18 03:39:22 +05:30
|
|
|
action = 'store_true',
|
2016-04-24 21:54:00 +05:30
|
|
|
help = 'use float input')
|
2019-02-28 06:06:02 +05:30
|
|
|
parser.add_option('--blank',
|
|
|
|
dest = 'blank',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'blank out (optional) input canvas content')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
parser.set_defaults(grid = ['0','0','0'],
|
|
|
|
offset = (0,0,0),
|
2019-05-23 22:33:24 +05:30
|
|
|
fill = 0.0,
|
|
|
|
float = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2012-06-21 18:27:14 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2019-05-23 22:33:24 +05:30
|
|
|
datatype = 'f' if options.float else 'i'
|
2019-02-28 06:06:02 +05:30
|
|
|
options.grid = ['1','1','1'] if options.blank and options.grid == ['0','0','0'] else options.grid
|
|
|
|
options.fill = 1 if options.blank and options.fill == 0 else options.fill
|
2016-03-18 03:39:22 +05:30
|
|
|
|
2015-08-08 00:33:26 +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:
|
2016-03-18 03:39:22 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
labeled = False)
|
2015-08-13 00:13:11 +05:30
|
|
|
except: continue
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2019-02-28 06:06:02 +05:30
|
|
|
if options.blank:
|
|
|
|
extra_header = []
|
|
|
|
info = {}
|
|
|
|
info['grid'] = np.zeros(3,dtype=int)
|
|
|
|
info['size'] = np.zeros(3,dtype=float)
|
|
|
|
info['origin'] = np.zeros(3,dtype=float)
|
|
|
|
info['microstructures'] = 0
|
|
|
|
info['homogenization'] = 1
|
|
|
|
else:
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
2019-02-28 06:06:02 +05:30
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
|
|
|
damask.util.report_geom(info)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2019-02-28 06:06:02 +05:30
|
|
|
errors = []
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
|
|
|
if errors != []:
|
|
|
|
damask.util.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
|
|
|
|
2019-02-28 06:06:02 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid'],datatype).reshape(info['grid'],order='F') # read microstructure
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
2013-05-15 22:53:21 +05:30
|
|
|
newInfo = {
|
2015-08-08 00:33:26 +05:30
|
|
|
'grid': np.zeros(3,'i'),
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
2013-05-15 22:53:21 +05:30
|
|
|
|
2016-03-02 02:42:04 +05:30
|
|
|
newInfo['grid'] = np.array([int(o*float(n.translate(None,'xX'))) if n[-1].lower() == 'x'\
|
|
|
|
else int(n) for o,n in zip(info['grid'],options.grid)],'i')
|
2015-08-20 00:00:50 +05:30
|
|
|
newInfo['grid'] = np.where(newInfo['grid'] > 0, newInfo['grid'],info['grid'])
|
2013-06-30 06:04:16 +05:30
|
|
|
|
2016-03-18 03:39:22 +05:30
|
|
|
microstructure_cropped = np.zeros(newInfo['grid'],datatype)
|
2019-05-23 22:33:24 +05:30
|
|
|
microstructure_cropped.fill(options.fill if options.float or options.fill > 0 else np.nanmax(microstructure)+1)
|
2019-02-28 06:06:02 +05:30
|
|
|
|
|
|
|
if not options.blank:
|
|
|
|
xindex = np.arange(max(options.offset[0],0),min(options.offset[0]+newInfo['grid'][0],info['grid'][0]))
|
|
|
|
yindex = np.arange(max(options.offset[1],0),min(options.offset[1]+newInfo['grid'][1],info['grid'][1]))
|
|
|
|
zindex = np.arange(max(options.offset[2],0),min(options.offset[2]+newInfo['grid'][2],info['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.')
|
|
|
|
table.close(dismiss = True)
|
|
|
|
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]
|
|
|
|
|
|
|
|
newInfo['size'] = info['size']/info['grid']*newInfo['grid'] if np.all(info['grid'] > 0) else newInfo['grid']
|
|
|
|
newInfo['origin'] = info['origin']+(info['size']/info['grid'] if np.all(info['grid'] > 0) \
|
|
|
|
else newInfo['size']/newInfo['grid'])*options.offset
|
2019-05-23 22:33:24 +05:30
|
|
|
newInfo['microstructures'] = len(np.unique(microstructure_cropped))
|
2013-06-30 06:04:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- report ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
|
2015-10-15 03:05:56 +05:30
|
|
|
if (any(newInfo['grid'] != info['grid'])):
|
2016-04-24 21:54:00 +05:30
|
|
|
remarks.append('--> grid a b c: {}'.format(' x '.join(map(str,newInfo['grid']))))
|
2015-10-15 03:05:56 +05:30
|
|
|
if (any(newInfo['size'] != info['size'])):
|
2016-04-24 21:54:00 +05:30
|
|
|
remarks.append('--> size x y z: {}'.format(' x '.join(map(str,newInfo['size']))))
|
2015-10-15 03:05:56 +05:30
|
|
|
if (any(newInfo['origin'] != info['origin'])):
|
2016-04-24 21:54:00 +05:30
|
|
|
remarks.append('--> origin x y z: {}'.format(' : '.join(map(str,newInfo['origin']))))
|
2015-10-15 03:05:56 +05:30
|
|
|
if ( newInfo['microstructures'] != info['microstructures']):
|
2016-04-24 21:54:00 +05:30
|
|
|
remarks.append('--> microstructures: {}'.format(newInfo['microstructures']))
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
if np.any(newInfo['grid'] < 1): errors.append('invalid new grid a b c.')
|
|
|
|
if np.any(newInfo['size'] <= 0.0): errors.append('invalid new size x y z.')
|
|
|
|
|
2015-09-24 22:22:58 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2013-06-30 06:04:16 +05:30
|
|
|
continue
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- write header ---------------------------------------------------------------------------------
|
|
|
|
|
2015-02-14 16:53:50 +05:30
|
|
|
table.info_clear()
|
2017-08-23 23:39:33 +05:30
|
|
|
table.info_append(extra_header+[
|
2014-01-20 20:11:56 +05:30
|
|
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
2016-04-24 21:54:00 +05:30
|
|
|
"grid\ta {}\tb {}\tc {}".format(*newInfo['grid']),
|
|
|
|
"size\tx {}\ty {}\tz {}".format(*newInfo['size']),
|
|
|
|
"origin\tx {}\ty {}\tz {}".format(*newInfo['origin']),
|
|
|
|
"homogenization\t{}".format(info['homogenization']),
|
|
|
|
"microstructures\t{}".format(newInfo['microstructures']),
|
2013-06-30 06:04:16 +05:30
|
|
|
])
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_clear()
|
2015-02-14 16:53:50 +05:30
|
|
|
table.head_write()
|
|
|
|
table.output_flush()
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2013-05-15 22:53:21 +05:30
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2019-05-23 22:33:24 +05:30
|
|
|
format = '%g' if options.float else '%{}i'.format(int(math.floor(math.log10(np.nanmax(microstructure_cropped))+1)))
|
2015-02-14 16:53:50 +05:30
|
|
|
table.data = microstructure_cropped.reshape((newInfo['grid'][0],newInfo['grid'][1]*newInfo['grid'][2]),order='F').transpose()
|
2016-03-18 03:39:22 +05:30
|
|
|
table.data_writeArray(format,delimiter=' ')
|
2013-06-30 06:04:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|