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
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
import os,sys,string,re,vtk
|
|
|
|
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-08-24 19:31:24 +05:30
|
|
|
parser.add_option('-p', '--positions',
|
|
|
|
dest = 'position',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 16:26:02 +05:30
|
|
|
help = 'coordinate label [%default]')
|
2015-08-24 19:31:24 +05:30
|
|
|
parser.add_option('-s', '--size',
|
|
|
|
dest = 'size',
|
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
2014-01-27 19:40:29 +05:30
|
|
|
help = 'x,y,z size of voxel')
|
2015-08-24 19:31:24 +05:30
|
|
|
parser.add_option('-o', '--origin',
|
|
|
|
dest = 'origin',
|
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
2014-11-18 20:56:59 +05:30
|
|
|
help = 'x,y,z origin of coordinate system')
|
2015-08-24 19:31:24 +05:30
|
|
|
parser.add_option('-g', '--geom',
|
|
|
|
dest = 'geom', action='store_true',
|
|
|
|
help = 'derive geometry from geom-file header information [%default]')
|
|
|
|
parser.set_defaults(position = 'pos',
|
|
|
|
origin = (0.0,0.0,0.0),
|
|
|
|
geom = False,
|
|
|
|
)
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2014-11-18 20:56:59 +05:30
|
|
|
if options.size == None and not options.geom:
|
2015-08-24 19:31:24 +05:30
|
|
|
parser.error('no size specified.')
|
2014-01-27 19:40:29 +05:30
|
|
|
|
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
|
|
|
|
table.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
|
2014-11-18 20:56:59 +05:30
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
2014-11-18 20:56:59 +05:30
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
if options.geom:
|
|
|
|
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'],
|
|
|
|
])
|
2014-11-18 20:56:59 +05:30
|
|
|
else:
|
2015-08-24 19:31:24 +05:30
|
|
|
info = {}
|
2014-12-19 00:56:52 +05:30
|
|
|
info['size'] = np.ones(3)
|
2014-11-18 20:56:59 +05:30
|
|
|
info['grid'] = info['size'] / options.size
|
|
|
|
info['origin'] = options.origin
|
2015-08-24 19:31:24 +05:30
|
|
|
|
|
|
|
errors = []
|
|
|
|
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position,
|
|
|
|
table.label_dimension(options.position)))
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
table.data_readArray(options.position)
|
2014-11-18 20:56:59 +05:30
|
|
|
table.data[:,0:3] *= info['size']
|
|
|
|
table.data[:,0:3] += info['origin']
|
|
|
|
|
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],
|
|
|
|
])
|
|
|
|
|
|
|
|
halfDelta = 0.5*info['size']/info['grid']
|
|
|
|
|
|
|
|
Points = vtk.vtkPoints()
|
|
|
|
Hex = vtk.vtkHexahedron()
|
|
|
|
uGrid = vtk.vtkUnstructuredGrid()
|
2014-01-27 19:40:29 +05:30
|
|
|
|
|
|
|
for p in table.data:
|
|
|
|
for i,h in enumerate(hexPoints):
|
2015-08-24 19:31:24 +05:30
|
|
|
id = Points.InsertNextPoint(p+h*halfDelta)
|
2014-01-27 19:40:29 +05:30
|
|
|
Hex.GetPointIds().SetId(i,id)
|
|
|
|
|
|
|
|
uGrid.InsertNextCell(Hex.GetCellType(), Hex.GetPointIds())
|
|
|
|
|
|
|
|
uGrid.SetPoints(Points)
|
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2015-08-24 19:31:24 +05:30
|
|
|
if name:
|
|
|
|
(dir,filename) = os.path.split(name)
|
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetFileName(os.path.join(dir,os.path.splitext(filename)[0]+'_{}'.format(options.position) \
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
|
|
|
|
else: writer.SetInputData(uGrid)
|
|
|
|
writer.Write()
|
|
|
|
|
2014-01-27 19:40:29 +05:30
|
|
|
else:
|
2015-08-24 19:31:24 +05:30
|
|
|
writer = vtk.vtkUnstructuredGridWriter()
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetFileTypeToASCII()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(uGrid)
|
|
|
|
else: writer.SetInputData(uGrid)
|
|
|
|
writer.Write()
|
|
|
|
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
|
|
|
|
|
|
|
table.close() # close input ASCII table
|
2014-01-27 19:40:29 +05:30
|
|
|
|