2018-11-16 13:32:31 +05:30
|
|
|
#!/usr/bin/env python
|
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',
|
|
|
|
dest = 'real',
|
|
|
|
action = 'store_true',
|
2016-04-24 21:54:00 +05:30
|
|
|
help = 'use float input')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
parser.set_defaults(grid = ['0','0','0'],
|
|
|
|
offset = (0,0,0),
|
|
|
|
fill = 0,
|
2016-03-18 03:39:22 +05:30
|
|
|
real = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2012-06-21 18:27:14 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2016-03-18 03:39:22 +05:30
|
|
|
datatype = 'f' if options.real else 'i'
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
2015-02-14 16:53:50 +05:30
|
|
|
table.head_read()
|
2015-08-08 00:33:26 +05:30
|
|
|
info,extra_header = table.head_getGeom()
|
2016-04-24 21:54:00 +05:30
|
|
|
damask.util.report_geom(info)
|
2015-08-08 00:33:26 +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 != []:
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
|
|
|
|
2016-03-18 03:39:22 +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)
|
|
|
|
microstructure_cropped.fill(options.fill if options.real or options.fill > 0 else microstructure.max()+1)
|
2016-10-25 00:46:29 +05:30
|
|
|
xindex = list(set(range(options.offset[0],options.offset[0]+newInfo['grid'][0])) & \
|
|
|
|
set(range(info['grid'][0])))
|
|
|
|
yindex = list(set(range(options.offset[1],options.offset[1]+newInfo['grid'][1])) & \
|
|
|
|
set(range(info['grid'][1])))
|
|
|
|
zindex = list(set(range(options.offset[2],options.offset[2]+newInfo['grid'][2])) & \
|
|
|
|
set(range(info['grid'][2])))
|
2012-06-21 18:27:14 +05:30
|
|
|
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]
|
2015-10-15 03:05:56 +05:30
|
|
|
if 0 in map(len,[xindex,yindex,zindex,translate_x,translate_y,translate_z]):
|
2016-04-24 21:54:00 +05:30
|
|
|
damask.util.croak('invaldid grid-offset combination.')
|
2015-10-15 03:05:56 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2013-04-25 21:23:41 +05:30
|
|
|
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)]
|
2013-06-30 06:04:16 +05:30
|
|
|
|
2013-06-27 21:05:49 +05:30
|
|
|
newInfo['size'] = info['size']/info['grid']*newInfo['grid']
|
2013-05-15 22:53:21 +05:30
|
|
|
newInfo['origin'] = info['origin']+info['size']/info['grid']*options.offset
|
2013-06-30 06:04:16 +05:30
|
|
|
newInfo['microstructures'] = microstructure_cropped.max()
|
|
|
|
|
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
|
|
|
|
2016-03-18 03:39:22 +05:30
|
|
|
format = '%g' if options.real else '%{}i'.format(int(math.floor(math.log10(microstructure_cropped.max())+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
|