Miscellaneous Tools for PySPH

Input/Output of data files

The following functions are handy functions when processing output generated by PySPH or to generate new files.

pysph.solver.utils.dump(filename, particles, solver_data, detailed_output=False, only_real=True, mpi_comm=None, compress=False)[source]

Dump the given particles and solver data to the given filename.

Parameters:
  • filename (str) – Filename to dump to.
  • particles (sequence(ParticleArray)) – Sequence of particle arrays to dump.
  • solver_data (dict) – Additional information to dump about solver state.
  • detailed_output (bool) – Specifies if all arrays should be dumped.
  • only_real (bool) – Only dump the real particles.
  • mpi_comm (mpi4pi.MPI.Intracomm) – An MPI communicator to use for parallel commmunications.
  • compress (bool) – Specify if the file is to be compressed or not.
  • mpi_comm is not passed or is set to None the local particles alone (If) –
  • dumped, otherwise only rank 0 dumps the output. (are) –
pysph.solver.utils.get_files(dirname=None, fname=None, endswith=('hdf5', 'npz'))[source]

Get all solution files in a given directory, dirname.

Parameters:
  • dirname (str) – Name of directory.
  • fname (str) – An initial part of the filename, if not specified use the first part of the dirname.
  • endswith (str) – The extension of the file to load.
pysph.solver.utils.load(fname)[source]

Load the output data

Parameters:fname (str) – Name of the file or full path

Examples

>>> data = load('elliptical_drop_100.npz')
>>> data.keys()
['arrays', 'solver_data']
>>> arrays = data['arrays']
>>> arrays.keys()
['fluid']
>>> fluid = arrays['fluid']
>>> type(fluid)
pysph.base.particle_array.ParticleArray
>>> data['solver_data']
{'count': 100, 'dt': 4.6416394784204199e-05, 't': 0.0039955855395528766}
pysph.solver.utils.load_and_concatenate(prefix, nprocs=1, directory='.', count=None)[source]

Load the results from multiple files.

Given a filename prefix and the number of processors, return a concatenated version of the dictionary returned via load.

Parameters:
  • prefix (str) – A filename prefix for the output file.
  • nprocs (int) – The number of processors (files) to read
  • directory (str) – The directory for the files
  • count (int) – The file iteration count to read. If None, the last available one is read

Interpolator

This module provides a convenient class called interpolator.Interpolator which can be used to interpolate any scalar values from the points onto either a mesh or a collection of other points. SPH interpolation is performed with a simple Shepard filtering.

class pysph.tools.interpolator.InterpolateFunction(dest, sources)[source]

Bases: pysph.sph.equation.Equation

Parameters:
  • dest (str) – name of the destination particle array
  • sources (list of str or None) – names of the source particle arrays
initialize(d_idx, d_prop, d_number_density)[source]
loop(s_idx, d_idx, s_temp_prop, d_prop, d_number_density, WIJ)[source]
post_loop(d_idx, d_prop, d_number_density)[source]
class pysph.tools.interpolator.Interpolator(particle_arrays, num_points=125000, kernel=None, x=None, y=None, z=None, domain_manager=None, equations=None)[source]

Bases: object

Convenient class to interpolate particle properties onto a uniform grid or given set of particles. This is particularly handy for visualization.

The x, y, z coordinates need not be specified, and if they are not, the bounds of the interpolated domain is automatically computed and num_points number of points are used in this domain uniformly placed.

Parameters:
  • particle_arrays (list) – A list of particle arrays.
  • num_points (int) – the number of points to interpolate on to.
  • kernel (Kernel) – the kernel to use for interpolation.
  • x (ndarray) – the x-coordinate of points on which to interpolate.
  • y (ndarray) – the y-coordinate of points on which to interpolate.
  • z (ndarray) – the z-coordinate of points on which to interpolate.
  • domain_manager (DomainManager) – An optional Domain manager for periodic domains.
  • equations (sequence) – A sequence of equations or groups. Defaults to None. This is used only if the default interpolation equations are inadequate.
interpolate(prop, gradient=False)[source]

Interpolate given property.

Parameters:
  • prop (str) – The name of the property to interpolate.
  • gradient (bool) – Evaluate gradient and not function.
Returns:

Return type:

A numpy array suitably shaped with the property interpolated.

set_domain(bounds, shape)[source]

Set the domain to interpolate into.

Parameters:
  • bounds (tuple) – (xmin, xmax, ymin, ymax, zmin, zmax)
  • shape (tuple) – (nx, ny, nz)
set_interpolation_points(x=None, y=None, z=None)[source]

Set the points on which we must interpolate the arrays.

If any of x, y, z is not passed it is assumed to be 0.0 and shaped like the other non-None arrays.

Parameters:
  • x (ndarray) – the x-coordinate of points on which to interpolate.
  • y (ndarray) – the y-coordinate of points on which to interpolate.
  • z (ndarray) – the z-coordinate of points on which to interpolate.
update_particle_arrays(particle_arrays)[source]

Call this for a new set of particle arrays which have the same properties as before.

For example, if you are reading the particle array data from files, each time you load a new file a new particle array is read with the same properties. Call this function to reset the arrays.

pysph.tools.interpolator.get_bounding_box(particle_arrays, tight=False, stretch=0.05)[source]

Find the size of the domain given a sequence of particle arrays.

If tight is True, the bounds are tight, if not the domain is stretched along each dimension by an amount stretch specified as a percentage of the length along that dimension is added in each dimension.

pysph.tools.interpolator.get_nx_ny_nz(num_points, bounds)[source]

Given a number of points to use and the bounds, return a triplet of integers for a uniform mesh with approximately that many points.

pysph.tools.interpolator.main(fname, prop, npoint)[source]

GMsh input/output

Utility module to read input mesh files. This is primarily for meshes generated using Gmsh. This module also provides some simple classes that allow one to create extruded 3D surfaces by generating a gmsh file in Python.

There is also a function to read VTK dataset and produce points from them. This is very useful as Gmsh can generate VTK datasets from its meshes and thus the meshes can be imported as point clouds that may be used in an SPH simulation.

class pysph.tools.gmsh.Extrude(dx=0.0, dy=0.0, dz=1.0, surfaces=None)[source]

Bases: object

Extrude a given set of surfaces by the displacements given along each directions.

Parameters:
  • dx (float) – Extrusion along x.
  • dy (float) – Extrusion along y.
  • dz (float) – Extrusion along z.
  • surfaces (list) – List of surfaces to extrude.
write(fp, point_id_base=0, elem_id_base=0)[source]
class pysph.tools.gmsh.Gmsh(gmsh=None)[source]

Bases: object

Construct a Gmsh helper object that can be used to mesh objects.

Parameters:gmsh (str) – Path to gmsh executable.
get_points(entities, vertices=True, cell_centers=False)[source]

Given a list of entities, return x, y, z arrays for the position.

Parameters:
  • entities (list) – List of entities.
  • vertices (bool) – If True, it converts the vertices to points.
  • cell_centers (bool) – If True, converts the cell centers to points.
get_points_from_geo(geo_file_name, vertices=True, cell_centers=False)[source]

Given a .geo file, generate a mesh and get the points from the mesh.

Parameters:
  • geo_file_name (str) – Filename of the .geo file.
  • vertices (bool) – If True, it converts the vertices to points.
  • cell_centers (bool) – If True, converts the cell centers to points.
write_geo(entities, fp)[source]

Write a list of given entities to the given file pointer.

write_vtk_mesh(entities, fname)[source]

Write a list of given entities to the given file name.

class pysph.tools.gmsh.Loop(start, mesh_size=0.1)[source]

Bases: object

Create a Line Loop in Gmsh parlance but using a turtle-graphics like approach.

Use this to create a 2D closed surface. The surface is always in the x-y plane.

Examples

Here is a simple example:

>>> l1 = Loop((0.0, 0.0), mesh_size=0.1)
>>> l1.move(1.0).turn(90).move(1.0).turn(90).move(1.0).turn(90).move(1.0)

This will create a square shape.

arc(radius, angle=180)[source]

Create a circular arc given the radius and for an angle as specified.

move(dist)[source]

Move by given distance at the current angle.

turn(angle)[source]

Turn by angle (in degrees).

write(fp, point_id_base=0, elem_id_base=0)[source]

Write data to given file object fp.

class pysph.tools.gmsh.Surface(*loops)[source]

Bases: object

Constructor.

Parameters:loops (tuple(Loop)) – Any additional positional arguments are treated as loop objects.
write(fp, point_id_base=0, elem_id_base=0)[source]
pysph.tools.gmsh.example_3d_p(fp=<open file '<stdout>', mode 'w'>)[source]

Creates a 3D “P” with a hole inside it.

pysph.tools.gmsh.example_cube(fp=<open file '<stdout>', mode 'w'>)[source]

Simple example of a cube.

pysph.tools.gmsh.example_plot_3d_p(gmsh)[source]

Note: this will only work if you have gmsh installed.

pysph.tools.gmsh.transform_points(x, y, z, transform)[source]

Given the coordinates, x, y, z and the TVTK transform instance, return the transformed coordinates.

pysph.tools.gmsh.vtk_file_to_points(fname, vertices=True, cell_centers=True)[source]

Given a file containing a VTK dataset (currently only an old style .vtk file), convert it to a set of points that can be used for simulation with SPH.

Parameters:
  • fname (str) – File name.
  • vertices (bool) – If True, it converts the vertices to points.
  • cell_centers (bool) – If True, converts the cell centers to points.
Returns:

Return type:

x, y, z of the points.