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
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
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
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-s', '--size',
|
|
|
|
dest = 'size',
|
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
|
|
|
help = 'x,y,z size of hexahedral box [1.0 along largest grid point number]')
|
|
|
|
parser.add_option('-p','--position',
|
|
|
|
dest = 'position',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'column label for coordinates [%default]')
|
2013-11-20 23:03:05 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(size = [0.0,0.0,0.0],
|
|
|
|
position = 'pos',
|
|
|
|
)
|
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 -------------------------------------------------------------------------
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if filenames == []: filenames = ['STDIN']
|
2015-06-05 17:20:15 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = None,
|
|
|
|
buffered = False, readonly = True)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
2015-04-10 22:29:08 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2015-04-23 00:14:54 +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'],
|
|
|
|
])
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
if np.any(info['grid'] < 1): remarks.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0) \
|
|
|
|
and np.all(info['grid'] < 1): errors.append('invalid size x y z.')
|
2015-06-05 17:20:15 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +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']) # normalize to grid
|
|
|
|
remarks.append('rescaling size {} to {}...'.format({0:'x',1:'y',2:'z'}[i],info['size'][i]))
|
|
|
|
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position,
|
|
|
|
table.label_dimension(options.position)))
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss=True)
|
2015-06-05 17:20:15 +05:30
|
|
|
continue
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
labels = ['{dim}_{label}'.format(dim = 1+i,label = options.position) for i in xrange(3)]
|
2015-06-05 17:20:15 +05:30
|
|
|
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
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-06-05 17:20:15 +05:30
|
|
|
coords = table.data[:,:3] # assign coordinates
|
|
|
|
grain = table.data[:,3].astype('i') if hasGrains else 1+np.arange(len(coords),dtype='i') # assign grains
|
2015-08-08 00:33:26 +05:30
|
|
|
# grainIDs = np.unique(grain).astype('i') # find all grainIDs present
|
|
|
|
|
|
|
|
# --- generate grid --------------------------------------------------------------------------------
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
grid = vtk.vtkUnstructuredGrid()
|
|
|
|
pts = vtk.vtkPoints()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- process microstructure information --------------------------------------------------------------
|
|
|
|
|
2013-11-20 23:03:05 +05:30
|
|
|
IDs = vtk.vtkIntArray()
|
|
|
|
IDs.SetNumberOfComponents(1)
|
|
|
|
IDs.SetName("GrainID")
|
|
|
|
|
2015-04-10 22:29:08 +05:30
|
|
|
for i,item in enumerate(coords):
|
2015-08-08 00:33:26 +05:30
|
|
|
IDs.InsertNextValue(grain[i])
|
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)
|
|
|
|
|
|
|
|
grid.SetPoints(pts)
|
|
|
|
grid.GetCellData().AddArray(IDs)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- write data -----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if name == 'STDIN':
|
2014-05-26 20:05:25 +05:30
|
|
|
writer = vtk.vtkUnstructuredGridWriter()
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetFileTypeToASCII()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-08-08 00:33:26 +05:30
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(grid)
|
|
|
|
else: writer.SetInputData(grid)
|
2014-05-26 20:05:25 +05:30
|
|
|
writer.Write()
|
|
|
|
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
2013-11-20 23:03:05 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
(dir,filename) = os.path.split(name)
|
2014-05-26 20:05:25 +05:30
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2015-08-08 00:33:26 +05:30
|
|
|
writer.SetFileName(os.path.join(dir,'seeds_'+os.path.splitext(filename)[0]
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(grid)
|
|
|
|
else: writer.SetInputData(grid)
|
2014-05-26 20:05:25 +05:30
|
|
|
writer.Write()
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.close()
|