/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/solver/vtk_output.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | """ Dumps VTK output files.
It takes a hdf or npz file as an input and output vtu file.
"""
from pysph import has_tvtk, has_pyvisfile
from pysph.solver.output import Output, load, output_formats
from pysph.solver.utils import remove_irrelevant_files
import numpy as np
import argparse
import sys
import os
class VTKOutput(Output):
def __init__(self, scalars=None, **vectors):
self.set_output_scalar(scalars)
self.set_output_vector(**vectors)
super(VTKOutput, self).__init__(True)
def set_output_vector(self, **vectors):
"""
Set the vector to dump in VTK output
Parameter
----------
vectors:
Vectors to dump
Example V=['u', 'v', 'z']
"""
self.vectors = {}
for name, vector in vectors.items():
assert (len(vector) is 3)
self.vectors[name] = vector
def set_output_scalar(self, scalars=None):
"""
Set the scalars to dump in VTK output
Parameter
---------
scalar_array: list
The set of properties to dump
"""
self.scalars = scalars
def _get_scalars(self, arrays):
if self.scalars is None:
self.scalars = list(arrays.keys())
scalars = []
for prop_name in self.scalars:
scalars.append((prop_name, arrays[prop_name]))
return scalars
def _get_vectors(self, arrays):
vectors = []
for prop_name, prop_list in self.vectors.items():
vec = np.array([arrays[prop_list[0]], arrays[prop_list[1]],
arrays[prop_list[2]]])
data = (prop_name, vec)
vectors.append(data)
return vectors
def _dump(self, filename):
for ptype, pdata in self.all_array_data.items():
self._setup_data(pdata)
try:
fname, seq = filename.rsplit('_', 1)
self._dump_arrays(fname + '_' + ptype + '_' + seq)
except ValueError:
self._dump_arrays(filename + '_' + ptype)
def _setup_data(self, arrays):
self.numPoints = arrays['x'].size
self.points = np.array([arrays['x'], arrays['y'],
arrays['z']])
self.data = []
self.data.extend(self._get_scalars(arrays))
self.data.extend(self._get_vectors(arrays))
class PyVisFileOutput(VTKOutput):
def _dump_arrays(self, filename):
from pyvisfile.vtk import (UnstructuredGrid, DataArray,
AppendedDataXMLGenerator, VTK_VERTEX)
n = self.numPoints
da = DataArray("points", self.points)
grid = UnstructuredGrid((n, da), cells=np.arange(n),
cell_types=np.asarray([VTK_VERTEX] * n))
for name, field in self.data:
da = DataArray(name, field)
grid.add_pointdata(da)
with open(filename + '.vtu', "w") as f:
AppendedDataXMLGenerator(None)(grid).write(f)
class TVTKOutput(VTKOutput):
def _dump_arrays(self, filename):
from tvtk.api import tvtk
n = self.numPoints
cells = np.arange(n)
cells.shape = (n, 1)
cell_type = tvtk.Vertex().cell_type
ug = tvtk.UnstructuredGrid(points=self.points.transpose())
ug.set_cells(cell_type, cells)
from mayavi.core.dataset_manager import DatasetManager
dsm = DatasetManager(dataset=ug)
for name, field in self.data:
dsm.add_array(field.transpose(), name)
dsm.activate(name)
from tvtk.api import write_data
write_data(ug, filename)
def dump_vtk(filename, particles, scalars=None, **vectors):
"""
Parameter
----------
filename: str
Filename to dump to
particles: sequence(ParticleArray)
Sequence if particles arrays to dump
scalars: list
list of scalars to dump.
vectors:
Vectors to dump
Example V=['u', 'v', 'z']
"""
if has_pyvisfile():
output = PyVisFileOutput(scalars, **vectors)
elif has_tvtk():
output = TVTKOutput(scalars, **vectors)
else:
msg = 'TVTK and pyvisfile Not present'
raise ImportError(msg)
output.dump(filename, particles, {})
def run(options):
for fname in options.inputfile:
if os.path.isdir(fname):
files = [os.path.join(fname, file) for file in os.listdir(fname)
if file.endswith(output_formats)]
files = remove_irrelevant_files(files)
options.inputfile.extend(files)
continue
data = load(fname)
particles = []
for ptype, pdata in data['arrays'].items():
particles.append(pdata)
filename = os.path.splitext(fname)[0]
if options.outdir is not None:
filename = options.os_dir + os.path.split(filename)[1]
dump_vtk(filename, particles, scalars=options.scalars,
V=['u', 'v', 'w'])
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
prog='dump_vtk', description=__doc__, add_help=False
)
parser.add_argument(
"-h", "--help", action="store_true", default=False, dest="help",
help="show this help message and exit"
)
parser.add_argument(
"-s", "--scalars", metavar="scalars", type=str, default=None,
help="scalar variables to dump in VTK output, provide a " +
"comma-separated list, for example: -s rho,p,m"
)
parser.add_argument(
"-d", "--outdir", metavar="outdir", type=str, default=None,
help="Directory to output VTK files"
)
parser.add_argument(
"inputfile", type=str, nargs='+',
help=" list of input files or/and directories (hdf5 or npz format)"
)
if len(argv) > 0 and argv[0] in ['-h', '--help']:
parser.print_help()
sys.exit()
options, extra = parser.parse_known_args(argv)
if options.scalars is not None:
options.scalars = options.scalars.split(',')
run(options)
if __name__ == '__main__':
main()
|