2013-11-20 23:03:05 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
import os,sys,string,vtk
|
2014-11-07 16:37:03 +05:30
|
|
|
import numpy as np
|
2013-11-20 23:03:05 +05:30
|
|
|
import damask
|
2014-11-07 16:37:03 +05:30
|
|
|
from optparse import OptionParser
|
2013-11-20 23:03:05 +05:30
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
identifiers = {
|
2015-04-10 22:29:08 +05:30
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
2013-11-20 23:03:05 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'size': lambda x: float(x),
|
|
|
|
'origin': lambda x: float(x),
|
|
|
|
'microstructures': lambda x: int(x),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [seedsfile[s]]', description = """
|
2014-01-24 02:18:33 +05:30
|
|
|
Produce VTK point mesh from seeds file
|
2013-11-20 23:03:05 +05:30
|
|
|
|
2014-11-07 16:37:03 +05:30
|
|
|
""", version = scriptID)
|
2013-11-20 23:03:05 +05:30
|
|
|
|
2014-02-14 18:47:29 +05:30
|
|
|
parser.add_option('-s', '--size', dest='size', type='float', nargs = 3, metavar='float float float',\
|
2013-11-20 23:03:05 +05:30
|
|
|
help='x,y,z size of hexahedral box [1.0 along largest grid point number]')
|
|
|
|
|
2014-05-26 20:05:25 +05:30
|
|
|
parser.set_defaults(size = [0.0,0.0,0.0])
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2015-06-05 17:20:15 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2013-11-20 23:03:05 +05:30
|
|
|
if filenames == []:
|
2015-06-05 17:20:15 +05:30
|
|
|
filenames = ['STDIN']
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
if name == 'STDIN':
|
|
|
|
file = {'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
|
|
|
else:
|
|
|
|
if not os.path.exists(name): continue
|
|
|
|
file = {'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
|
|
|
|
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered = False)
|
2015-04-10 22:29:08 +05:30
|
|
|
table.head_read()
|
|
|
|
|
2015-04-23 00:14:54 +05:30
|
|
|
|
2015-06-05 17:20:15 +05:30
|
|
|
if np.all(table.label_index(['1_coords','2_coords','3_coords']) != -1):
|
|
|
|
labels = ['1_coords','2_coords','3_coords']
|
|
|
|
elif np.all(table.label_index(['x','y','z']) != -1):
|
|
|
|
labels = ['x','y','z']
|
|
|
|
else:
|
|
|
|
file['croak'].write('no coordinate data (1/2/3_coords | x/y/z) found ...')
|
|
|
|
continue
|
|
|
|
|
|
|
|
hasGrains = table.label_index('microstructure') != -1
|
|
|
|
labels += ['microstructure'] if hasGrains else []
|
2015-04-10 22:29:08 +05:30
|
|
|
|
2015-06-05 17:20:15 +05:30
|
|
|
table.data_readArray(labels) # read ASCIItable columns
|
|
|
|
coords = table.data[:,:3] # assign coordinates
|
|
|
|
grain = table.data[:,3].astype('i') if hasGrains else 1+np.arange(len(coords),dtype='i') # assign grains
|
|
|
|
grainIDs = np.unique(grain).astype('i') # find all grainIDs present
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
#--- interpret header ----------------------------------------------------------------------------
|
|
|
|
info = {
|
2015-04-10 22:29:08 +05:30
|
|
|
'grid': np.zeros(3,'i'),
|
|
|
|
'size': np.array(options.size),
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
2013-11-20 23:03:05 +05:30
|
|
|
}
|
|
|
|
|
2015-04-10 22:29:08 +05:30
|
|
|
for header in table.info:
|
2013-11-20 23:03:05 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
|
|
|
if len(headitems) == 0: continue
|
|
|
|
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])
|
|
|
|
|
2015-04-10 22:29:08 +05:30
|
|
|
if info['microstructures'] != len(grainIDs):
|
|
|
|
file['croak'].write('grain data not matching grain count (%i)...\n'%(len(grainIDs)))
|
|
|
|
info['microstructures'] = len(grainIDs)
|
2014-11-07 16:37:03 +05:30
|
|
|
if np.any(info['grid'] < 1):
|
2013-11-20 23:03:05 +05:30
|
|
|
file['croak'].write('invalid grid a b c.\n')
|
2015-04-10 22:29:08 +05:30
|
|
|
continue
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
for i in xrange(3):
|
|
|
|
if info['size'][i] <= 0.0: # any invalid size?
|
|
|
|
info['size'][i] = float(info['grid'][i])/max(info['grid'])
|
|
|
|
file['croak'].write('rescaling size %s...\n'%{0:'x',1:'y',2:'z'}[i])
|
|
|
|
|
|
|
|
#--- generate grid --------------------------------------------------------------------------------
|
|
|
|
grid = vtk.vtkUnstructuredGrid()
|
|
|
|
pts = vtk.vtkPoints()
|
|
|
|
|
|
|
|
#--- process microstructure information --------------------------------------------------------------
|
|
|
|
IDs = vtk.vtkIntArray()
|
|
|
|
IDs.SetNumberOfComponents(1)
|
|
|
|
IDs.SetName("GrainID")
|
|
|
|
|
2015-04-10 22:29:08 +05:30
|
|
|
for i,item in enumerate(coords):
|
2014-05-26 20:05:25 +05:30
|
|
|
pid = pts.InsertNextPoint(item[0:3])
|
2013-11-20 23:03:05 +05:30
|
|
|
pointIds = vtk.vtkIdList()
|
|
|
|
pointIds.InsertId(0, pid)
|
|
|
|
grid.InsertNextCell(1, pointIds)
|
2015-06-05 17:20:15 +05:30
|
|
|
IDs.InsertNextValue(grain[i])
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
grid.SetPoints(pts)
|
|
|
|
grid.GetCellData().AddArray(IDs)
|
|
|
|
|
|
|
|
#--- write data -----------------------------------------------------------------------------------
|
|
|
|
if file['name'] == 'STDIN':
|
2014-05-26 20:05:25 +05:30
|
|
|
writer = vtk.vtkUnstructuredGridWriter()
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetFileTypeToASCII()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5:
|
|
|
|
writer.SetInput(grid)
|
|
|
|
else:
|
|
|
|
writer.SetInputData(grid)
|
|
|
|
writer.Write()
|
|
|
|
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
2013-11-20 23:03:05 +05:30
|
|
|
else:
|
2015-06-05 17:20:15 +05:30
|
|
|
table.close(dismiss=True)
|
2013-11-20 23:03:05 +05:30
|
|
|
(head,tail) = os.path.split(file['name'])
|
2014-05-26 20:05:25 +05:30
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetFileName(os.path.join(head,'seeds_'+os.path.splitext(tail)[0]
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5:
|
|
|
|
writer.SetInput(grid)
|
|
|
|
else:
|
|
|
|
writer.SetInputData(grid)
|
|
|
|
writer.Write()
|