/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/tools/pprocess.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 | """General post-processing utility for solution data"""
TVTK = True
try:
from tvtk.api import tvtk, write_data
except (ImportError, SystemExit):
TVTK = False
if TVTK:
from tvtk.array_handler import array2vtk
from os import path
import numpy as np
import pysph.solver.utils as utils
def get_ke_history(files, array_name):
t, ke = [], []
for sd, array in utils.iter_output(files, array_name):
t.append(sd['t'])
m, u, v, w = array.get('m', 'u', 'v', 'w')
_ke = 0.5 * np.sum( m * (u**2 + v**2 + w**2) )
ke.append(_ke)
return np.asarray(t), np.asarray(ke)
class Results(object):
def __init__(self, dirname=None, fname=None, endswith=".npz"):
self.dirname = dirname
self.fname = fname
self.endswith = endswith
# the starting file number
self.start = 0
if ( (dirname is not None) and (fname is not None) ):
self.load()
def set_dirname(self, dirname):
self.dirname=dirname
def set_fname(self, fname):
self.fname = fname
def load(self):
self.files = files = utils.get_files(
self.dirname, self.fname, self.endswith)
self.nfiles = len(files)
def reload(self):
self.start = self.nfiles
self.load()
def get_ke_history(self, array_name):
self.t, self.ke = get_ke_history(self.files, array_name)
def _write_vtk_snapshot(self, mesh, directory, _fname):
fname = path.join(directory, _fname)
write_data( mesh, fname )
def write_vtk(self, array_name, props):
if not TVTK:
return
# create a list of props
if type(props) != list:
props = [ props ]
# create an output folder for the vtk files
dirname = path.join(self.dirname, 'vtk')
utils.mkdir(dirname)
nfiles = self.nfiles
for i in range(self.start, nfiles):
f = self.files[i]
data = utils.load(f)
array = data['arrays'][array_name]
num_particles = array.num_real_particles
# save the points
points = np.zeros( shape=(num_particles,3) )
points[:, 0] = array.z
points[:, 1] = array.y
points[:, 2] = array.x
mesh = tvtk.PolyData(points=points)
# add the scalar props
for prop in props:
if prop == 'vmag':
u, v, w = array.get('u','v','w')
numpy_array = np.sqrt(u**2 + v**2 + w**2)
else:
numpy_array = array.get(prop)
vtkarray = array2vtk(numpy_array)
vtkarray.SetName(prop)
# add the array as point data
mesh.point_data.add_array(vtkarray)
# set the last prop as the active scalar
mesh.point_data.set_active_scalars(props[-1])
# spit it out
fileno = data['solver_data']['count']
_fname = self.fname + '_%s_%s'%(array_name, fileno)
self._write_vtk_snapshot(mesh, dirname, _fname)
class PySPH2VTK(object):
"""Convert PySPH array data to Paraview legible VTK data"""
def __init__(self, arrays, dirname='.', fileno=None):
self.arrays = arrays
self.dirname = dirname
self.fileno=fileno
array_dict = {}
for array in arrays:
array_dict[ array.name ] = array
self.array_dict = array_dict
def _write_vtk_snapshot(self, mesh, directory, _fname):
fname = path.join(directory, _fname)
write_data( mesh, fname )
def write_vtk(self, array_name, props):
# ceck if it is possible
if not TVTK:
raise RuntimeError('Cannot generate VTK output!')
# check if the array is legal
if array_name not in list(self.array_dict.keys()):
raise RuntimeError('Array %s not defined'%array_name)
# create a list of props
if type(props) != list:
props = [ props ]
# create an output folder for the vtk files
dirname = path.join(self.dirname, 'vtk')
utils.mkdir(dirname)
array = self.array_dict[array_name]
num_particles = array.num_real_particles
# save the points
points = np.zeros( shape=(num_particles,3) )
points[:, 0] = array.z
points[:, 1] = array.y
points[:, 2] = array.x
mesh = tvtk.PolyData(points=points)
# add the scalar props
for prop in props:
if prop == 'vmag':
u, v, w = array.get('u','v','w')
numpy_array = np.sqrt(u**2 + v**2 + w**2)
else:
numpy_array = array.get(prop)
vtkarray = array2vtk(numpy_array)
vtkarray.SetName(prop)
# add the array as point data
mesh.point_data.add_array(vtkarray)
# set the last prop as the active scalar
mesh.point_data.set_active_scalars(props[-1])
# spit it out
if self.fileno is None:
_fname = '%s'%(array_name)
else:
_fname = '%s_%03d'%(array_name, self.fileno)
self._write_vtk_snapshot(mesh, dirname, _fname)
|