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
|
2013-06-30 06:01:44 +05:30
|
|
|
import damask
|
2011-06-01 14:14:14 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
2013-07-10 14:45:42 +05:30
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
|
|
|
|
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
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2013-06-30 19:17:01 +05:30
|
|
|
synonyms = {
|
|
|
|
'grid': ['resolution'],
|
|
|
|
'size': ['dimension'],
|
|
|
|
}
|
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
|
|
|
|
2013-07-10 14:45:42 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
2011-08-18 13:23:07 +05:30
|
|
|
)
|
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,
|
2013-06-30 06:01:44 +05:30
|
|
|
'output':sys.stdout,
|
2012-11-07 22:19:47 +05:30
|
|
|
'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),
|
2013-06-30 06:01:44 +05:30
|
|
|
'output':sys.stdout,
|
2012-11-07 22:19:47 +05:30
|
|
|
'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:
|
2013-07-10 14:45:42 +05:30
|
|
|
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
|
|
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
2011-10-11 22:55:22 +05:30
|
|
|
|
2013-06-30 06:01:44 +05:30
|
|
|
theTable = damask.ASCIItable(file['input'],file['output'],labels=False)
|
|
|
|
theTable.head_read()
|
2012-06-26 23:37:38 +05:30
|
|
|
|
2013-06-30 06:01:44 +05:30
|
|
|
#--- interpret header ----------------------------------------------------------------------------
|
2013-05-15 02:39:37 +05:30
|
|
|
info = {
|
|
|
|
'grid': numpy.zeros(3,'i'),
|
|
|
|
'size': numpy.zeros(3,'d'),
|
|
|
|
'origin': numpy.zeros(3,'d'),
|
2013-06-30 19:17:01 +05:30
|
|
|
'homogenization': 0,
|
2013-04-12 18:18:39 +05:30
|
|
|
'microstructures': 0,
|
2012-06-26 23:37:38 +05:30
|
|
|
}
|
2013-04-12 18:18:39 +05:30
|
|
|
|
2013-06-30 06:01:44 +05:30
|
|
|
for header in theTable.info:
|
2012-06-26 23:37:38 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
2013-06-30 06:01:44 +05:30
|
|
|
if len(headitems) == 0: continue
|
2013-06-30 19:17:01 +05:30
|
|
|
for synonym,alternatives in synonyms.iteritems():
|
|
|
|
if headitems[0] in alternatives: headitems[0] = synonym
|
2013-04-12 18:18:39 +05:30
|
|
|
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
|
|
|
|
2013-05-15 21:32:38 +05:30
|
|
|
if numpy.any(info['grid'] < 1):
|
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2013-06-30 06:01:44 +05:30
|
|
|
continue
|
2013-05-15 02:39:37 +05:30
|
|
|
if numpy.any(info['size'] <= 0.0):
|
2013-05-15 21:32:38 +05:30
|
|
|
file['croak'].write('invalid size x y z.\n')
|
2013-06-30 06:01:44 +05:30
|
|
|
continue
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
|
|
|
|
#--- generate grid --------------------------------------------------------------------------------
|
|
|
|
grid = vtk.vtkRectilinearGrid()
|
|
|
|
grid.SetDimensions([x+1 for x in info['grid']])
|
|
|
|
for i in xrange(3):
|
2013-06-30 06:01:44 +05:30
|
|
|
temp = vtk.vtkDoubleArray()
|
|
|
|
temp.SetNumberOfTuples(info['grid'][i]+1)
|
|
|
|
for j in xrange(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)
|
2013-05-15 02:39:37 +05:30
|
|
|
|
|
|
|
#--- read microstructure information --------------------------------------------------------------
|
|
|
|
structure = vtk.vtkIntArray()
|
|
|
|
structure.SetName('Microstructures')
|
2013-06-30 06:01:44 +05:30
|
|
|
theTable.data_rewind()
|
|
|
|
while theTable.data_read():
|
|
|
|
items = theTable.data
|
2013-06-22 02:49:14 +05:30
|
|
|
if len(items) > 2:
|
|
|
|
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
|
|
|
|
elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2]))
|
2013-06-30 06:01:44 +05:30
|
|
|
else: items = map(int,items)
|
|
|
|
else: items = map(int,items)
|
2013-06-22 02:49:14 +05:30
|
|
|
|
|
|
|
for item in items:
|
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()
|
2013-07-10 14:45:42 +05:30
|
|
|
outWriter.SetHeader('# powered by '+scriptID)
|
2013-05-15 02:39:37 +05:30
|
|
|
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()
|