2014-01-27 19:40:29 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2014-01-27 19:40:29 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
import os,sys,vtk
|
2014-12-19 00:56:52 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2014-01-27 19:40:29 +05:30
|
|
|
import damask
|
|
|
|
|
2014-04-01 22:28:54 +05:30
|
|
|
scriptID = '$Id$'
|
2014-11-18 20:56:59 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2014-01-27 19:40:29 +05:30
|
|
|
|
2014-11-18 20:56:59 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
2014-11-18 20:56:59 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-01-27 19:40:29 +05:30
|
|
|
Create hexahedral voxels around points in an ASCIItable.
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2014-01-27 19:40:29 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
parser.add_option('-d', '--deformed',
|
|
|
|
dest = 'deformed',
|
2015-08-24 19:31:24 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2015-10-09 16:54:26 +05:30
|
|
|
help = 'deformed coordinate label [%default]')
|
|
|
|
parser.add_option('-c','--coordinates',
|
|
|
|
dest = 'coords',
|
|
|
|
type = 'string', metavar='string',
|
|
|
|
help = 'undeformed coordinates label [%default]')
|
|
|
|
parser.set_defaults(deformed = 'ipdeformedcoord',
|
|
|
|
coords = 'ipinitialcoord',
|
2015-08-24 19:31:24 +05:30
|
|
|
)
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2014-01-27 19:40:29 +05:30
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-24 19:31:24 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, readonly = True)
|
|
|
|
except: continue
|
2015-10-09 16:54:26 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2014-11-18 20:56:59 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
# --------------- interprete header -----------------------------------------------------------------
|
2015-08-24 19:31:24 +05:30
|
|
|
table.head_read()
|
2015-10-09 16:54:26 +05:30
|
|
|
errors=[]
|
|
|
|
if table.label_dimension(options.deformed) != 3: errors.append('columns "{}" have dimension {}'.format(options.deformed,
|
|
|
|
table.label_dimension(options.deformed)))
|
|
|
|
if table.label_dimension(options.coords) != 3: errors.append('coordinates {} are not a vector.'.format(options.coords))
|
|
|
|
|
|
|
|
table.data_readArray([options.coords,options.deformed])
|
|
|
|
|
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
|
|
|
coords = [{},{},{}]
|
|
|
|
for i in xrange(len(table.data)):
|
|
|
|
for j in xrange(3):
|
|
|
|
coords[j][str(table.data[i,table.label_index(options.coords)+j])] = True
|
|
|
|
grid = np.array(map(len,coords),'i')
|
|
|
|
size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
|
|
|
|
np.array([max(map(float,coords[0].keys()))-min(map(float,coords[0].keys())),\
|
|
|
|
max(map(float,coords[1].keys()))-min(map(float,coords[1].keys())),\
|
|
|
|
max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
|
|
|
|
],'d') # size from bounding box, corrected for cell-centeredness
|
|
|
|
|
|
|
|
size = np.where(grid > 1, size, min(size[grid > 1]/grid[grid > 1])) # spacing for grid==1 equal to smallest among other spacings
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
2015-08-24 19:31:24 +05:30
|
|
|
hexPoints = np.array([[-1,-1,-1],
|
|
|
|
[ 1,-1,-1],
|
|
|
|
[ 1, 1,-1],
|
|
|
|
[-1, 1,-1],
|
|
|
|
[-1,-1, 1],
|
|
|
|
[ 1,-1, 1],
|
|
|
|
[ 1, 1, 1],
|
|
|
|
[-1, 1, 1],
|
|
|
|
])
|
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
halfDelta = 0.5*size/grid
|
2015-08-24 19:31:24 +05:30
|
|
|
|
|
|
|
Points = vtk.vtkPoints()
|
|
|
|
Hex = vtk.vtkHexahedron()
|
|
|
|
uGrid = vtk.vtkUnstructuredGrid()
|
2015-10-09 16:54:26 +05:30
|
|
|
|
2014-01-27 19:40:29 +05:30
|
|
|
for p in table.data:
|
|
|
|
for i,h in enumerate(hexPoints):
|
2015-10-09 16:54:26 +05:30
|
|
|
pointID = Points.InsertNextPoint(p[table.label_index(options.deformed):table.label_index(options.deformed)+3]+h*halfDelta)
|
|
|
|
Hex.GetPointIds().SetId(i,pointID)
|
2014-01-27 19:40:29 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
uGrid.InsertNextCell(Hex.GetCellType(), Hex.GetPointIds())
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
uGrid.SetPoints(Points)
|
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
if name:
|
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
2015-10-09 16:54:26 +05:30
|
|
|
(directory,filename) = os.path.split(name)
|
2015-08-24 19:31:24 +05:30
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
2015-10-09 16:54:26 +05:30
|
|
|
writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0]
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
2014-01-27 19:40:29 +05:30
|
|
|
else:
|
2015-10-09 16:54:26 +05:30
|
|
|
writer = vtk.vtkDataSetWriter()
|
2015-08-24 19:31:24 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-10-09 16:54:26 +05:30
|
|
|
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
|
|
|
|
else: writer.SetInputData(uGrid)
|
|
|
|
writer.Write()
|
|
|
|
if name == None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
2015-08-24 19:31:24 +05:30
|
|
|
|
2015-10-09 16:54:26 +05:30
|
|
|
table.close() # close input ASCII table
|
2014-01-27 19:40:29 +05:30
|
|
|
|