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 15:59:07 +05:30
|
|
|
import os,sys,math
|
2014-11-07 16:43:14 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2013-07-01 00:41:16 +05:30
|
|
|
import damask
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2013-05-13 18:40:31 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2016-05-12 12:24:34 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
2013-04-16 23:30:14 +05:30
|
|
|
Scales a geometry description independently in x, y, and z direction in terms of grid and/or size.
|
2013-07-01 00:41:16 +05:30
|
|
|
Either absolute values or relative factors (like "0.25x") can be used.
|
2014-11-07 16:43:14 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-06-21 18:27:14 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-g', '--grid',
|
|
|
|
dest = 'grid',
|
|
|
|
type = 'string', nargs = 3, metavar = 'string string string',
|
|
|
|
help = 'a,b,c grid of hexahedral box [unchanged]')
|
|
|
|
parser.add_option('-s', '--size',
|
|
|
|
dest = 'size',
|
|
|
|
type = 'string', nargs = 3, metavar = 'string string string',
|
|
|
|
help = 'x,y,z size of hexahedral box [unchanged]')
|
|
|
|
parser.add_option('-r', '--renumber',
|
|
|
|
dest = 'renumber',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'renumber microstructure indices from 1..N [%default]')
|
2019-05-23 22:33:24 +05:30
|
|
|
parser.add_option('--float',
|
|
|
|
dest = 'float',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'use float input')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
parser.set_defaults(renumber = False,
|
|
|
|
grid = ['0','0','0'],
|
|
|
|
size = ['0.0','0.0','0.0'],
|
2019-05-23 22:33:24 +05:30
|
|
|
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'
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2012-11-06 02:49:12 +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-08-21 01:12:05 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
2019-05-23 22:33:24 +05:30
|
|
|
buffered = False,
|
|
|
|
labeled = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2013-05-14 22:49:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2015-03-27 19:27:34 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
2019-05-23 22:33:24 +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
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2019-05-23 22:33:24 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid'],datatype) # read microstructure
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
newInfo = {
|
|
|
|
'grid': np.zeros(3,'i'),
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2018-11-16 16:53:45 +05:30
|
|
|
newInfo['grid'] = np.array([{True: round(o*float(n.lower().replace('x',''))),
|
|
|
|
False: round(float(n.lower().replace('x','')))}[n[-1].lower() == 'x']
|
2016-03-02 15:59:07 +05:30
|
|
|
for o,n in zip(info['grid'],options.grid)],'i')
|
2018-11-16 16:53:45 +05:30
|
|
|
newInfo['size'] = np.array([{True: o*float(n.lower().replace('x','')),
|
|
|
|
False: float(n.lower().replace('x',''))}[n[-1].lower() == 'x']
|
2016-03-02 15:59:07 +05:30
|
|
|
for o,n in zip(info['size'],options.size)],'d')
|
2014-11-07 16:43:14 +05:30
|
|
|
newInfo['grid'] = np.where(newInfo['grid'] <= 0 , info['grid'],newInfo['grid'])
|
|
|
|
newInfo['size'] = np.where(newInfo['size'] <= 0.0, info['size'],newInfo['size'])
|
2013-07-01 00:41:16 +05:30
|
|
|
|
|
|
|
multiplicity = []
|
2016-10-25 00:46:29 +05:30
|
|
|
for j in range(3):
|
2013-07-01 00:41:16 +05:30
|
|
|
multiplicity.append([])
|
|
|
|
last = 0
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(info['grid'][j]):
|
2013-07-01 00:41:16 +05:30
|
|
|
this = int((i+1)*float(newInfo['grid'][j])/info['grid'][j])
|
|
|
|
multiplicity[j].append(this-last)
|
|
|
|
last = this
|
2013-07-17 02:41:44 +05:30
|
|
|
|
2013-07-01 00:41:16 +05:30
|
|
|
microstructure = microstructure.reshape(info['grid'],order='F')
|
2016-03-02 15:59:07 +05:30
|
|
|
microstructure = np.repeat(np.repeat(np.repeat(microstructure,
|
|
|
|
multiplicity[0], axis=0),multiplicity[1], axis=1),multiplicity[2], axis=2)
|
2014-02-14 18:47:29 +05:30
|
|
|
# --- renumber to sequence 1...Ngrains if requested ------------------------------------------------
|
2014-11-07 16:43:14 +05:30
|
|
|
# http://stackoverflow.com/questions/10741346/np-frequency-counts-for-unique-values-in-an-array
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-02-14 18:47:29 +05:30
|
|
|
if options.renumber:
|
2015-08-08 00:33:26 +05:30
|
|
|
newID = 0
|
2014-11-07 16:43:14 +05:30
|
|
|
for microstructureID,count in enumerate(np.bincount(microstructure.reshape(newInfo['grid'].prod()))):
|
2014-02-14 18:47:29 +05:30
|
|
|
if count != 0:
|
2015-08-08 00:33:26 +05:30
|
|
|
newID += 1
|
|
|
|
microstructure = np.where(microstructure == microstructureID, newID,microstructure).reshape(microstructure.shape)
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2019-05-23 22:33:24 +05:30
|
|
|
newInfo['microstructures'] = len(np.unique(microstructure))
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- report ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
|
2016-03-02 15:59:07 +05:30
|
|
|
if (any(newInfo['grid'] != info['grid'])):
|
|
|
|
remarks.append('--> grid a b c: %s'%(' x '.join(map(str,newInfo['grid']))))
|
|
|
|
if (any(newInfo['size'] != info['size'])):
|
|
|
|
remarks.append('--> size x y z: %s'%(' x '.join(map(str,newInfo['size']))))
|
|
|
|
if ( newInfo['microstructures'] != info['microstructures']):
|
|
|
|
remarks.append('--> microstructures: %i'%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-07-01 00:41:16 +05:30
|
|
|
continue
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- write header ---------------------------------------------------------------------------------
|
|
|
|
|
2014-09-03 20:23:12 +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:]),
|
2015-08-08 00:33:26 +05:30
|
|
|
"grid\ta {grid[0]}\tb {grid[1]}\tc {grid[2]}".format(grid=newInfo['grid']),
|
|
|
|
"size\tx {size[0]}\ty {size[1]}\tz {size[2]}".format(size=newInfo['size']),
|
|
|
|
"origin\tx {origin[0]}\ty {origin[1]}\tz {origin[2]}".format(origin=info['origin']),
|
|
|
|
"homogenization\t{homog}".format(homog=info['homogenization']),
|
|
|
|
"microstructures\t{microstructures}".format(microstructures=newInfo['microstructures']),
|
2013-07-01 00:41:16 +05:30
|
|
|
])
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_clear()
|
2014-09-03 20:23:12 +05:30
|
|
|
table.head_write()
|
2013-05-14 22:49:36 +05:30
|
|
|
|
2013-07-01 00:41:16 +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))+1)))
|
2014-09-03 20:23:12 +05:30
|
|
|
table.data = microstructure.reshape((newInfo['grid'][0],newInfo['grid'][1]*newInfo['grid'][2]),order='F').transpose()
|
2019-05-23 22:33:24 +05:30
|
|
|
table.data_writeArray(format,delimiter=' ')
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|