polishing, unifying,simplifying

This commit is contained in:
Martin Diehl 2013-05-14 17:19:36 +00:00
parent 563e74c832
commit 2acd737859
5 changed files with 25 additions and 35 deletions

View File

@ -4,7 +4,6 @@
import os,sys,string,math,numpy
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
#--------------------------------------------------------------------------------------------------
class extendedOption(Option):
#--------------------------------------------------------------------------------------------------
@ -84,8 +83,8 @@ if numpy.any(options.size < 0.0):
file['croak'].write('invalid size...\n')
sys.exit()
info = {
'grid': options.grid,
'size': options.size,
'grid': numpy.array(options.grid),
'size': numpy.array(options.size),
'origin': numpy.zeros(3,'d'),
'microstructures': max(options.microstructure),
'homogenization': options.homogenization
@ -105,7 +104,7 @@ header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['
header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2],))
header.append("microstructures\t%i\n"%info['microstructures'])
header.append("homogenization\t%i\n"%info['homogenization'])
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header))
file['output'].write('%i\theader\n'%(len(header))+''.join(header))
#--- write data -----------------------------------------------------------------------------------
for z in xrange(options.grid[2]):

View File

@ -39,7 +39,6 @@ mappings = {
'microstructures': lambda x: int(x),
}
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
Scales a geometry description independently in x, y, and z direction in terms of grid and/or size.
""" + string.replace('$Id$','\n','\\n')
@ -91,15 +90,14 @@ for file in files:
content = file['input'].readlines()
file['input'].close()
#--- interpretate header --------------------------------------------------------------------------
#--- interprete header ----------------------------------------------------------------------------
info = {
'grid': numpy.array(options.grid),
'size': numpy.array(options.size),
'origin': numpy.zeros(3,'d'),
'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'),
'microstructures': 0,
'homogenization': 0
}
newInfo = {
'grid': numpy.array(options.grid),
'size': numpy.array(options.size),
@ -134,11 +132,12 @@ for file in files:
'homogenization: %i\n'%info['homogenization'] + \
'microstructures: %i\n\n'%info['microstructures'])
if options.grid == [0,0,0]:
if numpy.all(info['grid'] == 0):
newInfo['grid'] = info['grid']
if options.size == [0.0,0.0,0.0]:
if numpy.all(info['size'] == 0.0)::
newInfo['size'] = info['size']
#--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'],'i')
i = 0
for line in content:
@ -157,20 +156,17 @@ for file in files:
if (newInfo['microstructures'] != info['microstructures']):
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
#--- assemble header ------------------------------------------------------------------------------
new_header.append('$Id$\n')
new_header.append("grid\ta %i\tb %i\tc %i\n"%(newInfo['grid'][0],newInfo['grid'][1],newInfo['grid'][2]))
new_header.append("size\tx %f\ty %f\tz %f\n"%(newInfo['size'][0],newInfo['size'][1],newInfo['size'][2]))
new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2]))
new_header.append("microstructures\t%i\n"%newInfo['microstructures'])
new_header.append("homogenization\t%i\n"%info['homogenization'])
# ------------------------------------------ assemble header ---------------------------------------
output = '%i\theader\n'%(len(new_header))
output += ''.join(new_header)
# ------------------------------------- regenerate texture information ----------------------------------
#--- scale microstructure -------------------------------------------------------------------------
for c in xrange(options.grid[2]):
z = int(info['grid'][2]*(c+0.5)/options.grid[2])%info['grid'][2]
for b in xrange(options.grid[1]):
@ -179,13 +175,10 @@ for file in files:
x = int(info['grid'][0]*(a+0.5)/options.grid[0])%info['grid'][0]
output += str(microstructure[x,y,z]).rjust(formatwidth) + {True:' ',False:'\n'}[options.twoD]
output += {True:'\n',False:''}[options.twoD]
# ------------------------------------------ output result ---------------------------------------
file['output'].write(output)
#--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN':
file['input'].close()
file['output'].close()
os.rename(file['name']+'_tmp',file['name'])

View File

@ -37,7 +37,6 @@ mappings = {
'homogenization': lambda x: int(x),
}
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
translate microstructure indices (shift or substitute) and/or geometry origin.
""" + string.replace('$Id$','\n','\\n')
@ -96,15 +95,14 @@ for file in files:
content = file['input'].readlines()
file['input'].close()
#--- interpretate header --------------------------------------------------------------------------
#--- interprete header ----------------------------------------------------------------------------
info = {
'grid': numpy.array([0,0,0]),
'size': numpy.array([0.0,0.0,0.0]),
'origin': numpy.zeros(3,'d'),
'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'),
'microstructures': 0,
'homogenization': 0
}
newInfo = {
'origin': numpy.zeros(3,'d'),
'microstructures': 0,

View File

@ -246,7 +246,7 @@ parser.add_option("-s", "--spectral", action="store_const", const="spectral",\
parser.add_option("--homogenization", type="int",\
dest="homogenization",\
help="homogenization index from material.config (only required for spectral file type)")
help="homogenization index from material.config (only required for geom file type)")
parser.set_defaults(filetype = 'geom')

View File

@ -4,10 +4,9 @@
import os,sys,string,re,math,numpy,random
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
# -----------------------------
#------------------------------------------------------------------------------------------------
class extendedOption(Option):
# -----------------------------
#------------------------------------------------------------------------------------------------
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
# taken from online tutorial http://docs.python.org/library/optparse.html
@ -24,8 +23,9 @@ class extendedOption(Option):
Option.take_action(self, action, dest, opt, value, values, parser)
# ----------------------- MAIN -------------------------------
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
identifiers = {
'grid': ['a','b','c'],
}