Module solver

An implementation of a general solver base class

class pysph.solver.solver.Solver(dim=2, integrator=None, kernel=None, n_damp=0, tf=1.0, dt=0.001, adaptive_timestep=False, cfl=0.3, output_at_times=(), fixed_h=False, **kwargs)[source]

Bases: object

Base class for all PySPH Solvers

Constructor

Any additional keyword args are used to set the values of any of the attributes.

Parameters:
  • dim (int) – Dimension of the problem
  • integrator (pysph.sph.integrator.Integrator) – Integrator to use
  • kernel (pysph.base.kernels.Kernel) – SPH kernel to use
  • n_damp (int) – Number of timesteps for which the initial damping is required. This is used to improve stability for problems with strong discontinuity in initial condition. Setting it to zero will disable damping of the timesteps.
  • dt (double) – Suggested initial time step for integration
  • tf (double) – Final time for integration
  • adaptive_timestep (bint) – Flag to use adaptive time steps
  • cfl (double) – CFL number for adaptive time stepping
  • pfreq (int) – Output files dumping frequency.
  • output_at_times (list/array) – Optional list of output times to force dump the output file
  • fixed_h (bint) – Flag for constant smoothing lengths h
  • reorder_freq (int) – The number of iterations after which particles should be re-ordered. If zero, do not do this.

Example

>>> integrator = PECIntegrator(fluid=WCSPHStep())
>>> kernel = CubicSpline(dim=2)
>>> solver = Solver(dim=2, integrator=integrator, kernel=kernel,
...                 n_damp=50, tf=1.0, dt=1e-3, adaptive_timestep=True,
...                 pfreq=100, cfl=0.5, output_at_times=[1e-1, 1.0])
add_post_stage_callback(callback)[source]

These callbacks are called after each integrator stage.

The callbacks are passed (current_time, dt, stage). See the the Integrator.one_timestep methods for examples of how this is called.

Example

>>> def post_stage_callback_function(t, dt, stage):
>>>     # This function is called after every stage of integrator.
>>>     print(t, dt, stage)
>>>     # Do something
>>> solver.add_post_stage_callback(post_stage_callback_function)
add_post_step_callback(callback)[source]

These callbacks are called after each timestep is performed.

The callbacks are passed the solver instance (i.e. self).

Example

>>> def post_step_callback_function(solver):
>>>     # This function is called after every time step.
>>>     print(solver.t, solver.dt)
>>>     # Do something
>>> solver.add_post_step_callback(post_step_callback_function)
add_pre_step_callback(callback)[source]

These callbacks are called before each timestep is performed.

The callbacks are passed the solver instance (i.e. self).

Example

>>> def pre_step_callback_function(solver):
>>>     # This function is called before every time step.
>>>     print(solver.t, solver.dt)
>>>     # Do something
>>> solver.add_pre_step_callback(pre_step_callback_function)
append_particle_arrrays(arrays)[source]

Append the particle arrays to the existing particle arrays

dump_output(**kwargs)

Dump the simulation results to file

The arrays used for printing are determined by the particle array’s output_property_arrays data attribute. For debugging it is sometimes nice to have all the arrays (including accelerations) saved. This can be chosen from using the command line option –detailed-output

Output data Format:

A single file named as: <fname>_<rank>_<iteration_count>.npz

The data is saved as a Python dictionary with two keys:

solver_data : Solver meta data like time, dt and iteration number

arrays : A dictionary keyed on particle array names and with
particle properties as value.

Example:

You can load the data output by PySPH like so:

>>> from pysph.solver.utils import load
>>> data = load('output_directory/filename_x_xxx.npz')
>>> solver_data = data['solver_data']
>>> arrays = data['arrays']
>>> fluid = arrays['fluid']
>>> ...

In the above example, it is assumed that the output file contained an array named fluid.

get_options(arg_parser)[source]

Implement this to add additional options for the application

load_output(count)[source]

Load particle data from dumped output file.

Parameters:count (str) – The iteration time from which to load the data. If time is ‘?’ then list of available data files is returned else the latest available data file is used

Notes

Data is loaded from the output_directory using the same format as stored by the dump_output() method. Proper functioning required that all the relevant properties of arrays be dumped.

reorder_particles(**kwargs)

Re-order particles so as to coalesce memory access.

set_adaptive_timestep(value)[source]

Set it to True to use adaptive timestepping based on cfl, viscous and force factor.

Look at pysph.sph.integrator.compute_time_step for more details.

set_arrays_to_print(array_names=None)[source]

Only print the arrays with the given names.

set_cfl(value)[source]

Set the CFL number for adaptive time stepping

set_command_handler(callable, command_interval=1)[source]

set the callable to be called at every command_interval iteration

the callable is called with the solver instance as an argument

set_compress_output(compress)[source]

Compress the dumped output files.

set_disable_output(value)[source]

Disable file output.

set_final_time(tf)[source]

Set the final time for the simulation

set_max_steps(max_steps)[source]

Set the maximum number of iterations to perform.

set_n_damp(ndamp)[source]

Set the number of timesteps for which the timestep should be initially damped.

set_output_at_times(output_at_times)[source]

Set a list of output times

set_output_directory(path)[source]

Set the output directory

set_output_fname(fname)[source]

Set the output file name

set_output_only_real(output_only_real)[source]

Set the flag to save out only real particles

set_output_printing_level(detailed_output)[source]

Set the output printing level

set_parallel_output_mode(mode='collected')[source]

Set the default solver dump mode in parallel.

The available modes are:

collected : Collect array data from all processors on root and
dump a single file.

distributed : Each processor dumps a file locally.

set_print_freq(n)[source]

Set the output print frequency

set_reorder_freq(freq)[source]

Set the reorder frequency in number of iterations.

set_time_step(dt)[source]

Set the time step to use

setup(particles, equations, nnps, kernel=None, fixed_h=False)[source]

Setup the solver.

The solver’s processor id is set if the in_parallel flag is set to true.

The order of the integrating calcs is determined by the solver’s order attribute.

This is usually called at the start of a PySPH simulation.

setup_solver(options=None)[source]

Implement the basic solvers here

All subclasses of Solver may implement this function to add the necessary operations for the problem at hand.

Parameters:options (dict) – options set by the user using commandline (there is no guarantee of existence of any key)
solve(show_progress=True)[source]

Solve the system

Notes

Pre-stepping functions are those that need to be called before the integrator is called.

Similarly, post step functions are those that are called after the stepping within the integrator.

Module solver tools

class pysph.solver.tools.DensityCorrection(app, arr_names, corr='shepard', freq=10, kernel=None)[source]

Bases: pysph.solver.tools.Tool

A tool to reinitialize the density of the fluid particles

Parameters:
  • app (pysph.solver.application.Application.) – The application instance.
  • arr_names (array) – Names of the particle arrays whose densities needs to be reinitialized.
  • corr (str) – Name of the density reinitialization operation. corr=’shepard’ for using zeroth order shepard filter
  • freq (int) – Frequency of reinitialization.
  • kernel (any kernel from pysph.base.kernels) –
post_step(solver)[source]

If overloaded, this is called automatically after each integrator step. The method is passed the solver instance.

class pysph.solver.tools.SimpleRemesher(app, array_name, props, freq=100, xi=None, yi=None, zi=None, kernel=None, equations=None)[source]

Bases: pysph.solver.tools.Tool

A simple tool to periodically remesh a given array of particles onto an initial set of points.

Constructor.

Parameters:
  • app (pysph.solver.application.Application) – The application instance.
  • array_name (str) – Name of the particle array that needs to be remeshed.
  • props (list(str)) – List of properties to interpolate.
  • freq (int) – Frequency of remeshing operation.
  • yi, zi (xi,) – Positions to remesh the properties onto. If not specified they are taken from the particle arrays at the time of construction.
  • kernel (any kernel from pysph.base.kernels) –
  • equations (list or None) – Equations to use for the interpolation, passed to the interpolator.
post_step(solver)[source]

If overloaded, this is called automatically after each integrator step. The method is passed the solver instance.

class pysph.solver.tools.Tool[source]

Bases: object

A tool is typically an object that can be used to perform a specific task on the solver’s pre_step/post_step or post_stage callbacks. This can be used for a variety of things. For example, one could save a plot, print debug statistics or perform remeshing etc.

To create a new tool, simply subclass this class and overload any of its desired methods.

post_stage(current_time, dt, stage)[source]

If overloaded, this is called automatically after each integrator stage, i.e. if the integrator is a two stage integrator it will be called after the first and second stages.

The method is passed (current_time, dt, stage). See the the Integrator.one_timestep methods for examples of how this is called.

post_step(solver)[source]

If overloaded, this is called automatically after each integrator step. The method is passed the solver instance.

pre_step(solver)[source]

If overloaded, this is called automatically before each integrator step. The method is passed the solver instance.

Module boundary conditions

Inlet Outlet Manager

class pysph.sph.bc.inlet_outlet_manager.CopyNormalsandDistances(dest, sources)[source]

Bases: pysph.sph.equation.Equation

Copy normals and distances from outlet/inlet particles to ghosts

Parameters:
  • dest (str) – name of the destination particle array
  • sources (list of str or None) – names of the source particle arrays
initialize_pair(d_idx, d_xn, d_yn, d_zn, s_xn, s_yn, s_zn, d_disp, s_disp)[source]
class pysph.sph.bc.inlet_outlet_manager.IOEvaluate(dest, sources, x, y, z, xn, yn, zn, maxdist=1000.0)[source]

Bases: pysph.sph.equation.Equation

Compute ioid for the particles
0 : particle is in fluid 1 : particle is inside the inlet/outlet 2 : particle is out of inlet/outlet
Parameters:
  • dest (str) – destination particle array name
  • sources (list) – List of source particle arrays
  • x (float) – x coordinate of interface point
  • y (float) – y coordinate of interface point
  • z (float) – z coordinate of interface point
  • xn (float) – x component of interface outward normal
  • yn (float) – y component of interface outward normal
  • zn (float) – z component of interface outward normal
  • maxdist (float) – Maximum length of inlet/outlet
initialize(d_ioid, d_idx)[source]
loop(d_idx, d_x, d_y, d_z, d_ioid, d_disp)[source]
class pysph.sph.bc.inlet_outlet_manager.InletBase(inlet_pa, dest_pa, inletinfo, kernel, dim, active_stages=[1], callback=None, ghost_pa=None)[source]

Bases: object

An API to add/delete particle when moving between inlet-fluid

Parameters:
  • inlet_pa (particle_array) – particle array for inlet
  • dest_pa (particle_array) – particle_array of the fluid
  • inletinfo (InletInfo instance) – contains information fo inlet
  • kernel (Kernel instance) – Kernel to be used for computations
  • dim (int) – dimension of the problem
  • active_stages (list) – stages of integrator at which update should be active
  • callback (function) – callback after the update function
  • ghost_pa (particle_array) – particle_array of the ghost_inlet
initialize()[source]

Function to initialize the class variables after evaluation in SimpleInletOutlet class

update(time, dt, stage)[source]

Update function called after each stage

class pysph.sph.bc.inlet_outlet_manager.InletInfo(pa_name, normal, refpoint, has_ghost=True, update_cls=None, equations=None, umax=1.0, props_to_copy=None)[source]

Bases: object

Create object with information of inlets, all the others parameters which
are not passed here get evaluated by InletOutletManager once the inlet is created.
Parameters:
  • pa_name (str) – Name of the inlet
  • normal (list) – Components of normal (float)
  • refpoint (list) – Point at the fluid-inlet interface (float)
  • has_ghost (bool) – if True, the ghost particles will be created
  • update_cls (class_name) – the class which is to be used to update the inlet/outlet
  • equations (list) – List of equations (optional)
  • props_to_copy (array) – properties to copy
class pysph.sph.bc.inlet_outlet_manager.InletOutletManager(fluid_arrays, inletinfo, outletinfo, extraeqns=None)[source]

Bases: object

Create the object to manage inlet outlet boundary conditions.
Most of the variables are evaluated after the scheme and particles are created.
Parameters:
  • fluid_arrays (list) – List of fluid particles array names (str)
  • inletinfo (list) – List of inlets (InletInfo)
  • outletinfo (list) – List of outlet (OutletInfo)
  • extraeqns (dict) – Dict of custom equations
add_io_properties(pa, scheme=None)[source]

Add properties to be used in inlet/outlet equations

Parameters:
  • pa (particle_array) – Particle array of inlet/outlet
  • scheme (pysph.sph.scheme) – The insance of scheme class
create_ghost(pa_arr, inlet=True)[source]

Creates ghosts for the given inlet/outlet particles

Parameters:
  • pa_arr (Particle array) – particles array for which ghost is required
  • inlet (bool) – if True, inlet info will be used for ghost creation
get_equations(scheme, **kw)[source]

Returns the equations for inlet/outlet

Parameters:
  • scheme (pysph.sph.scheme) – The instance of the scheme class
  • **kw (extra arguments) – Extra arguments depending upon the scheme used
get_equations_post_compute_acceleration()[source]

Returns the equations for inlet/outlet used post acceleration computation

get_inlet_outlet(particle_array)[source]
Returns list of Inlet and Outlet instances which
performs the change in inlet particles to outlet particles.
Parameters:particle_array (list) – List of all particle_arrays
get_io_names(ghost=False)[source]

return all the names of inlets and outlets :param ghost: if True, return the names of ghost also :type ghost: bool

get_stepper(scheme, integrator, **kw)[source]

Returns the steppers for inlet/outlet

Parameters:
  • scheme (pysph.sph.scheme) – The instance of the scheme class
  • intergrator (pysph.sph.integrator) – The parent class of the integrator
  • **kw (extra arguments) – Extra arguments depending upon the scheme used
setup_iom(dim, kernel)[source]

Essential data passed

Parameters:
  • dim (int) – dimension of the problem
  • kernel (pysph.base.kernel) – the kernel instance
update_dx(dx)[source]

Update the discretisation length

Parameters:dx (float) – The discretisation length
class pysph.sph.bc.inlet_outlet_manager.InletStep[source]

Bases: pysph.sph.integrator_step.IntegratorStep

initialize(d_x0, d_idx, d_x)[source]
stage1(d_idx, d_x, d_x0, d_u, dt)[source]
stage2(d_idx, d_x, d_x0, d_u, dt)[source]
class pysph.sph.bc.inlet_outlet_manager.OutletBase(outlet_pa, source_pa, outletinfo, kernel, dim, active_stages=[1], callback=None, ghost_pa=None)[source]

Bases: object

An API to add/delete particle when moving between fluid-outlet

Parameters:
  • outlet_pa (particle_array) – particle array for outlet
  • source_pa (particle_array) – particle_array of the fluid
  • ghost_pa (particle_array) – particle_array of the outlet ghost
  • outletinfo (OutletInfo instance) – contains information fo outlet
  • kernel (Kernel instance) – Kernel to be used for computations
  • dim (int) – dimnesion of the problem
  • active_stages (list) – stages of integrator at which update should be active
  • callback (function) – callback after the update function
initialize()[source]

Function to initialize the class variables after evaluation in SimpleInletOutlet class

update(time, dt, stage)[source]

Update function called after each stage

class pysph.sph.bc.inlet_outlet_manager.OutletInfo(pa_name, normal, refpoint, has_ghost=False, update_cls=None, equations=None, umax=1.0, props_to_copy=None)[source]

Bases: pysph.sph.bc.inlet_outlet_manager.InletInfo

Create object with information of outlet

The name is kept different for distinction only.

class pysph.sph.bc.inlet_outlet_manager.OutletStep[source]

Bases: pysph.sph.bc.inlet_outlet_manager.InletStep

class pysph.sph.bc.inlet_outlet_manager.OutletStepWithUhat[source]

Bases: pysph.sph.integrator_step.IntegratorStep

initialize(d_x0, d_idx, d_x)[source]
stage1(d_idx, d_x, d_x0, d_uhat, dt)[source]
stage2(d_idx, d_x, d_x0, d_uhat, dt)[source]
class pysph.sph.bc.inlet_outlet_manager.UpdateNormalsAndDisplacements(dest, sources, xn, yn, zn, xo, yo, zo)[source]

Bases: pysph.sph.equation.Equation

Update normal and perpendicular distance from the interface for the inlet/outlet particles

Parameters:
  • dest (str) – destination particle array name
  • sources (list) – List of source particle arrays
  • xn (float) – x component of interface outward normal
  • yn (float) – y component of interface outward normal
  • zn (float) – z component of interface outward normal
  • xo (float) – x coordinate of interface point
  • yo (float) – y coordinate of interface point
  • zo (float) – z coordinate of interface point
loop(d_idx, d_xn, d_yn, d_zn, d_x, d_y, d_z, d_disp)[source]