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-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-06-05 17:20:15 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, readonly = True)
|
|
|
|
except: continue
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.report(scriptName,name)
|
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()
|
|
|
|
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
2015-08-08 00:33:26 +05:30
|
|
|
'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):
|
2015-08-21 01:12:05 +05:30
|
|
|
if info['size'][i] <= 0.0: # any invalid size?
|
|
|
|
info['size'][i] = float(info['grid'][i])/max(info['grid']) # normalize to grid
|
2015-08-08 00:33:26 +05:30
|
|
|
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)))
|
2015-10-09 03:00:33 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-21 01:12:05 +05:30
|
|
|
if errors != []:
|
2015-10-09 03:00:33 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
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-09-30 03:57:46 +05:30
|
|
|
coords = table.data[:,:3]*info['size'] # assign coordinates (rescaled to box size)
|
2015-06-05 17:20:15 +05:30
|
|
|
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
|
|
|
|
|
|
|
# --- generate grid --------------------------------------------------------------------------------
|
2013-11-20 23:03:05 +05:30
|
|
|
|
|
|
|
grid = vtk.vtkUnstructuredGrid()
|
|
|
|
pts = vtk.vtkPoints()
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
# --- process microstructure information -----------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
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-10-09 03:00:33 +05:30
|
|
|
#--- write data -----------------------------------------------------------------------------------
|
2015-08-21 01:12:05 +05:30
|
|
|
if name:
|
2015-10-09 03:00:33 +05:30
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
2015-10-09 11:56:18 +05:30
|
|
|
(directory,filename) = os.path.split(name)
|
2014-05-26 20:05:25 +05:30
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2015-10-09 11:56:18 +05:30
|
|
|
writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0]
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
2015-08-21 01:12:05 +05:30
|
|
|
else:
|
2015-10-09 03:00:33 +05:30
|
|
|
writer = vtk.vtkDataSetWriter()
|
2015-08-21 01:12:05 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-10-09 03:00:33 +05:30
|
|
|
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(grid)
|
|
|
|
else: writer.SetInputData(grid)
|
|
|
|
writer.Write()
|
|
|
|
if name == None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.close()
|
2015-10-09 03:00:33 +05:30
|
|
|
|