2011-06-01 14:14:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
import os,sys,string,re,numpy,vtk
|
2011-06-01 14:14:14 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2011-06-01 14:14:14 +05:30
|
|
|
class extendedOption(Option):
|
2013-05-15 02:39:37 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2011-06-01 14:14:14 +05:30
|
|
|
# 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
|
|
|
|
|
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
lvalue = value.split(",")
|
|
|
|
values.ensure_value(dest, []).extend(lvalue)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2011-10-11 22:55:22 +05:30
|
|
|
identifiers = {
|
2013-04-12 18:18:39 +05:30
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
2011-10-11 22:55:22 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
2013-04-12 18:18:39 +05:30
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'size': lambda x: float(x),
|
|
|
|
'origin': lambda x: float(x),
|
|
|
|
'homogenization': lambda x: int(x),
|
|
|
|
'microstructures': lambda x: int(x),
|
2011-06-01 14:14:14 +05:30
|
|
|
}
|
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
parser = OptionParser(option_class=extendedOption, usage='%prog [geomfile[s]]', description = """
|
2011-11-03 17:37:41 +05:30
|
|
|
Produce VTK rectilinear mesh of structure data from geom description
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-08-18 13:23:07 +05:30
|
|
|
""" + string.replace('$Id$','\n','\\n')
|
|
|
|
)
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
#--- setup file handles --------------------------------------------------------------------------
|
2011-10-11 22:55:22 +05:30
|
|
|
files = []
|
|
|
|
if filenames == []:
|
2012-11-07 22:19:47 +05:30
|
|
|
files.append({'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
})
|
2011-10-11 22:55:22 +05:30
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
2012-11-07 22:19:47 +05:30
|
|
|
files.append({'name':name,
|
|
|
|
'input':open(name),
|
|
|
|
'croak':sys.stdout,
|
|
|
|
})
|
2011-10-11 22:55:22 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
2011-10-11 22:55:22 +05:30
|
|
|
for file in files:
|
2012-11-07 22:19:47 +05:30
|
|
|
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
|
2011-10-11 22:55:22 +05:30
|
|
|
|
|
|
|
firstline = file['input'].readline()
|
|
|
|
m = re.search('(\d+)\s*head', firstline.lower())
|
|
|
|
if m:
|
|
|
|
headerlines = int(m.group(1))
|
|
|
|
headers = [file['input'].readline() for i in range(headerlines)]
|
2011-06-01 14:14:14 +05:30
|
|
|
else:
|
2011-10-11 22:55:22 +05:30
|
|
|
headerlines = 1
|
|
|
|
headers = firstline
|
|
|
|
|
|
|
|
content = file['input'].readlines()
|
|
|
|
file['input'].close()
|
2012-06-26 23:37:38 +05:30
|
|
|
|
2013-05-15 02:39:37 +05:30
|
|
|
#--- interprete header ----------------------------------------------------------------------------
|
|
|
|
info = {
|
|
|
|
'grid': numpy.zeros(3,'i'),
|
|
|
|
'size': numpy.zeros(3,'d'),
|
|
|
|
'origin': numpy.zeros(3,'d'),
|
2013-04-12 18:18:39 +05:30
|
|
|
'microstructures': 0,
|
2013-05-15 02:39:37 +05:30
|
|
|
'homogenization': 0
|
2012-06-26 23:37:38 +05:30
|
|
|
}
|
2013-04-12 18:18:39 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
for header in headers:
|
2012-06-26 23:37:38 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
2013-04-12 18:18:39 +05:30
|
|
|
if headitems[0] == 'resolution': headitems[0] = 'grid'
|
|
|
|
if headitems[0] == 'dimension': headitems[0] = 'size'
|
|
|
|
if headitems[0] in mappings.keys():
|
|
|
|
if headitems[0] in identifiers.keys():
|
|
|
|
for i in xrange(len(identifiers[headitems[0]])):
|
|
|
|
info[headitems[0]][i] = \
|
|
|
|
mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1])
|
|
|
|
else:
|
|
|
|
info[headitems[0]] = mappings[headitems[0]](headitems[1])
|
|
|
|
|
|
|
|
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
|
|
|
|
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
|
|
|
|
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
|
|
|
|
'homogenization: %i\n'%info['homogenization'] + \
|
|
|
|
'microstructures: %i\n'%info['microstructures'])
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
if numpy.all(any['grid'] < 1):
|
|
|
|
file['croak'].write('no valid grid info found.\n')
|
|
|
|
sys.exit()
|
|
|
|
if numpy.any(info['size'] <= 0.0):
|
|
|
|
file['croak'].write('no valid size info found.\n')
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
#--- generate grid --------------------------------------------------------------------------------
|
|
|
|
grid = vtk.vtkRectilinearGrid()
|
|
|
|
grid.SetDimensions([x+1 for x in info['grid']])
|
|
|
|
temp = vtk.vtkDoubleArray()
|
|
|
|
for i in xrange(3):
|
|
|
|
temp.SetNumberOfTuples(info['grid'][i]+1)
|
|
|
|
for j in range(info['grid'][i]+1):
|
|
|
|
temp.InsertTuple1(j,j*info['size'][i]/info['grid'][i]+info['origin'][i])
|
|
|
|
if i == 0: grid.SetXCoordinates(temp)
|
|
|
|
if i == 1: grid.SetYCoordinates(temp)
|
|
|
|
if i == 2: grid.SetZCoordinates(temp)
|
|
|
|
|
|
|
|
#--- read microstructure information --------------------------------------------------------------
|
|
|
|
structure = vtk.vtkIntArray()
|
|
|
|
structure.SetName('Microstructures')
|
2011-10-11 22:55:22 +05:30
|
|
|
for line in content:
|
|
|
|
for item in map(int,line.split()):
|
2013-05-15 02:39:37 +05:30
|
|
|
structure.InsertNextValue(item)
|
|
|
|
|
|
|
|
grid.GetCellData().AddArray(structure)
|
|
|
|
|
|
|
|
#--- write data -----------------------------------------------------------------------------------
|
|
|
|
if file['name'] == 'STDIN':
|
|
|
|
outWriter = vtk.vtkRectilinearGridWriter()
|
|
|
|
outWriter.WriteToOutputStringOn()
|
|
|
|
outWriter.SetFileTypeToASCII()
|
|
|
|
outWriter.SetHeader('# powered by $Id$')
|
|
|
|
outWriter.SetInput(grid)
|
|
|
|
outWriter.Write()
|
|
|
|
sys.stdout.write(outWriter.GetOutputString()[0:outWriter.GetOutputStringLength()])
|
|
|
|
else:
|
|
|
|
(head,tail) = os.path.split(file['name'])
|
|
|
|
outWriter = vtk.vtkXMLRectilinearGridWriter()
|
|
|
|
outWriter.SetDataModeToBinary()
|
|
|
|
outWriter.SetCompressorTypeToZLib()
|
|
|
|
outWriter.SetFileName(os.path.join(head,'mesh_'+os.path.splitext(tail)[0]
|
|
|
|
+'.'+outWriter.GetDefaultFileExtension()))
|
|
|
|
outWriter.SetInput(grid)
|
|
|
|
outWriter.Write()
|