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

Dump XDMF

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.InterpolateSPH(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)[source]
loop(d_idx, s_idx, s_rho, s_m, s_temp_prop, d_prop, WIJ)[source]
class pysph.tools.interpolator.Interpolator(particle_arrays, num_points=125000, kernel=None, x=None, y=None, z=None, domain_manager=None, equations=None, method='shepard')[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.
  • method (str) – String with the following allowed methods: ‘shepard’, ‘sph’, ‘order1’
interpolate(prop, comp=0)[source]

Interpolate given property.

Parameters:
  • prop (str) – The name of the property to interpolate.
  • comp (int) – The component of the gradient required
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(update_domain=True)[source]

Update the NNPS when particles have moved.

If the update_domain is False, the domain is not updated.

Use this when the arrays are the same but the particles have themselves changed. If the particle arrays themselves change use the update_particle_arrays method instead.

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.

class pysph.tools.interpolator.SPHFirstOrderApproximation(dest, sources, dim=1)[source]

Bases: pysph.sph.equation.Equation

First order SPH approximation.

The method used to solve the linear system in this function is not same as in the reference. In the function \(Ax=b\) is solved where \(A := moment\) (Moment matrix) and \(b := p_sph\) (Property calculated using basic SPH). The calculation need the “moment” to be evaluated before this step which is done in SPHFirstOrderApproximationPreStep

References

[Liu2006]M.B. Liu, G.R. Liu, “Restoring particle consistency in smoothed particle hydrodynamics”, Applied Numerical Mathematics Volume 56, Issue 1 2006, Pages 19-36, ISSN 0168-9274
initialize(d_idx, d_prop, d_p_sph)[source]
loop(d_idx, d_h, s_h, s_x, s_y, s_z, d_x, d_y, d_z, s_rho, s_m, WIJ, DWIJ, s_temp_prop, d_p_sph, s_idx)[source]
post_loop(d_idx, d_moment, d_prop, d_p_sph)[source]
class pysph.tools.interpolator.SPHFirstOrderApproximationPreStep(dest, sources, dim=1)[source]

Bases: pysph.sph.equation.Equation

initialize(d_idx, d_moment)[source]
loop(d_idx, s_idx, d_h, s_h, s_x, s_y, s_z, d_x, d_y, d_z, s_rho, s_m, WIJ, XIJ, DWIJ, d_moment)[source]
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]

SPH Evaluator

This module provides a class that allows one to evaluate a set of equations on a collection of particle arrays. This is very handy for non-trivial post-processing that needs to be quick.

A convenience class that combines an AccelerationEval and an SPHCompiler to allow a user to specify particle arrays, equations, an optional domain and kernel to produce an SPH evaluation.

This is handy for post-processing.

class pysph.tools.sph_evaluator.SPHEvaluator(arrays, equations, dim, kernel=None, domain_manager=None, backend=None, nnps_factory=<class 'pysph.base.linked_list_nnps.LinkedListNNPS'>)[source]

Bases: object

Constructor.

Parameters:
  • arrays (list(ParticleArray)) –
  • equations (list) –
  • dim (int) –
  • kernel (kernel instance.) –
  • domain_manager (DomainManager) –
  • backend (str: indicates the backend to use.) – one of (‘opencl’, ‘cython’, ‘’, None)
  • nnps_factory (A factory that creates an NNPSBase instance.) –
evaluate(t=0.0, dt=0.1)[source]

Evalute the SPH equations, dummy t and dt values can be passed.

update(update_domain=True)[source]

Update the NNPS when particles have moved.

If the update_domain is False, the domain is not updated.

Use this when the arrays are the same but the particles have themselves changed. If the particle arrays themselves change use the update_particle_arrays method instead.

update_particle_arrays(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.

Mesh Converter

The following functions can be used to convert a mesh file supported by meshio to a set of surface points.

Particle Packer

The following functions can be used to create a domain with particle packed around a solid surface in both 2D and 3D.

pysph.tools.geometry.get_packed_periodic_packed_particles(add_opt_func, folder, dx, L, B, H=0, dim=2, dfreq=-1, pb=None, nu=None, k=None, tol=0.01)[source]

Creates a periodic packed 2D or 3D domain. It creates particles which are not aligned but packed such that the number density is uniform.

Parameters:
  • add_opt_func (options function from the parent Application class) –
  • folder (Application class output directory) –
  • dx (float) – required particle spacing
  • L (float) – length of the domain
  • B (float) – Width of the domain
  • H (float) – Height of the domain
  • dim (int) – dimensionality of the problem
  • dfreq (int) – projection frequency of particles
  • pb (float) – background pressure (default: 1.0)
  • nu (float) – viscosity coefficient (default: 0.3/dx)
  • k (float) – coefficient of repulsion (default: 0.005*dx)
  • tol (float) – tolerance value for convergence (default: 1e-2)
Returns:

  • xs (float) – x coordinate of solid particles
  • ys (float) – y coordinate of solid particles
  • zs (float) – z coordinate of solid particles
  • xf (float) – x coordinate of fluid particles
  • yf (float) – y coordinate of fluid particles
  • zf (float) – z coordinate of fluid particles

pysph.tools.geometry.get_packed_2d_particles_from_surface_coordinates(add_opt_func, folder, dx, x, y, pb=None, nu=None, k=None, scale=1.0, shift=False, dfreq=-1, invert_normal=False, hardpoints=None, use_prediction=False, filter_layers=False, reduce_dfreq=False, tol=0.01)[source]

Creates a packed configuration of particles around the given coordinates of a 2D geometry.

Parameters:
  • add_opt_func (method) – options function from the parent Application class
  • folder (string) – Application class output directory
  • dx (float) – required particle spacing
  • x (array) – x coordinates of the geometry
  • y (array) – y coordinates of the geometry
  • pb (float) – background pressure (default: 1.0)
  • nu (float) – viscosity coefficient (default: 0.3/dx)
  • k (float) – coefficient of repulsion (default: 0.005*dx)
  • scale (float) – the scaling factor for the coordinates
  • dfreq (int) – projection frequency of particles
  • invert_normal (bool) – if True the computed normals are inverted
  • hardpoints (dict) – the dictionary of hardpoints
  • use_prediction (bool) – if True, points are projected quickly to reach prediction points
  • filter_layers (bool) – if True, particles away from boundary are frozen
  • reduce_dfreq (bool) – if True, reduce projection frequency
  • tol (float) – tolerance value for convergence (default: 1e-2)
Returns:

  • xs (float) – x coordinate of solid particles
  • ys (float) – y coordinate of solid particles
  • zs (float) – z coordinate of solid particles
  • xf (float) – x coordinate of fluid particles
  • yf (float) – y coordinate of fluid particles
  • zf (float) – z coordinate of fluid particles

pysph.tools.geometry.get_packed_2d_particles_from_surface_file(add_opt_func, folder, dx, filename, pb=None, nu=None, k=None, scale=1.0, shift=False, dfreq=-1, invert_normal=False, hardpoints=None, use_prediction=False, filter_layers=False, reduce_dfreq=False, tol=0.01)[source]

Creates a packed configuration of particles around the given geometry file containing the x, y coordinates.

Parameters:
  • add_opt_func (method) – options function from the parent Application class
  • folder (string) – Application class output directory
  • dx (float) – required particle spacing
  • filename (string) – file containing the x, y coordinates of the geometry
  • pb (float) – background pressure (default: 1.0)
  • nu (float) – viscosity coefficient (default: 0.3/dx)
  • k (float) – coefficient of repulsion (default: 0.005*dx)
  • scale (float) – the scaling factor for the coordinates
  • dfreq (int) – projection frequency of particles
  • invert_normal (bool) – if True the computed normals are inverted
  • hardpoints (dict) – the dictionary of hardpoints
  • use_prediction (bool) – if True, points are projected quickly to reach prediction points
  • filter_layers (bool) – if True, particles away from boundary are frozen
  • reduce_dfreq (bool) – if True, reduce projection frequency
  • tol (float) – tolerance value for convergence (default: 1e-2)
Returns:

  • xs (float) – x coordinate of solid particles
  • ys (float) – y coordinate of solid particles
  • zs (float) – z coordinate of solid particles
  • xf (float) – x coordinate of fluid particles
  • yf (float) – y coordinate of fluid particles
  • zf (float) – z coordinate of fluid particles

pysph.tools.geometry.get_packed_3d_particles_from_surface_file(add_opt_func, folder, dx, filename, pb=None, nu=None, k=None, scale=1.0, shift=False, dfreq=-1, invert_normal=False, hardpoints=None, use_prediction=False, filter_layers=False, reduce_dfreq=False, tol=0.01)[source]

Creates a packed configuration of particles around the given STL file containing the x, y, z coordinates and normals.

Parameters:
  • add_opt_func (method) – options function from the parent Application class
  • folder (string) – Application class output directory
  • dx (float) – required particle spacing
  • filename (string) – the STL filename
  • pb (float) – background pressure (default: 1.0)
  • nu (float) – viscosity coefficient (default: 0.3/dx)
  • k (float) – coefficient of repulsion (default: 0.005*dx)
  • scale (float) – the scaling factor for the coordinates
  • dfreq (int) – projection frequency of particles
  • invert_normal (bool) – if True the computed normals are inverted
  • hardpoints (dict) – the dictionary of hardpoints
  • use_prediction (bool) – if True, points are projected quickly to reach prediction points
  • filter_layers (bool) – if True, particles away from boundary are frozen
  • reduce_dfreq (bool) – if True, reduce projection frequency
  • tol (float) – tolerance value for convergence (default: 1e-2)
Returns:

  • xs (float) – x coordinate of solid particles
  • ys (float) – y coordinate of solid particles
  • zs (float) – z coordinate of solid particles
  • xf (float) – x coordinate of fluid particles
  • yf (float) – y coordinate of fluid particles
  • zf (float) – z coordinate of fluid particles

pysph.tools.geometry.create_fluid_around_packing(dx, xf, yf, L, B, zf=[0.0], H=0.0, **props)[source]

Create the outer fluid particles around the generated packing. It adds the packed fluid particles and generate a concatenated particle array

Parameters:
  • dx (float) – particle spacing
  • xf (array) – x coordinate of fluid particles
  • yf (array) – y coordinate of fluid particles
  • L (float) – length of the domain
  • B (float) – width of the domain
  • zf (array) – z coordinate of fluid particles
  • H (float) – height of the domain
Returns:

Return type:

Particle array of fluid