2012-06-21 18:27:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
import os,sys,string,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
|
|
|
|
2014-11-07 16:43:14 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
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
|
|
|
|
2014-11-07 16:43:14 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[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]')
|
|
|
|
|
|
|
|
parser.set_defaults(renumber = False,
|
|
|
|
grid = ['0','0','0'],
|
|
|
|
size = ['0.0','0.0','0.0'],
|
|
|
|
)
|
2012-06-21 18:27:14 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2012-11-06 02:49:12 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
|
|
buffered = False, labeled = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
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()
|
|
|
|
|
|
|
|
table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
|
|
|
'size x y z: %s'%(' x '.join(map(str,info['size']))),
|
|
|
|
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
|
|
|
|
'homogenization: %i'%info['homogenization'],
|
|
|
|
'microstructures: %i'%info['microstructures'],
|
|
|
|
])
|
|
|
|
|
|
|
|
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 != []:
|
|
|
|
table.croak(errors)
|
|
|
|
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
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid']) # read microstructure
|
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
newInfo = {
|
|
|
|
'grid': np.zeros(3,'i'),
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2014-11-07 16:43:14 +05:30
|
|
|
newInfo['grid'] = np.array([{True:round(o*float(n.translate(None,'xX'))), False: round(float(n.translate(None,'xX')))}[n[-1].lower() == 'x'] for o,n in zip(info['grid'],options.grid)],'i')
|
|
|
|
newInfo['size'] = np.array([{True: o*float(n.translate(None,'xX')) , False: float(n.translate(None,'xX')) }[n[-1].lower() == 'x'] for o,n in zip(info['size'],options.size)],'d')
|
|
|
|
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 = []
|
|
|
|
for j in xrange(3):
|
|
|
|
multiplicity.append([])
|
|
|
|
last = 0
|
|
|
|
for i in xrange(info['grid'][j]):
|
|
|
|
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')
|
2014-11-07 16:43:14 +05:30
|
|
|
microstructure = np.repeat(
|
|
|
|
np.repeat(
|
|
|
|
np.repeat(microstructure,multiplicity[0], axis=0),
|
2015-08-08 00:33:26 +05:30
|
|
|
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
|
|
|
|
|
|
|
newInfo['microstructures'] = microstructure.max()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- report ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
|
|
|
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.')
|
|
|
|
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
table.output_flush()
|
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
|
|
|
|
2013-07-01 00:41:16 +05:30
|
|
|
formatwidth = int(math.floor(math.log10(microstructure.max())+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()
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data_writeArray('%%%ii'%(formatwidth),delimiter = ' ')
|
2013-07-01 00:41:16 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|