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

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()[source]

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.

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_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 simple_inlet_outlet

Simple Inlet and Outlet support for PySPH. The inlet and outlets are axis aligned.

The Inlet lets a user stack a given set of inlet particles along a particular coordinate axis. As the particles move they are copied over to a specified destination particle array. The particles are also wrapped back into the inlet.

The Outlet lets a user specify a region where any particles enter will be added to a specified “outlet” particle array and removed from the source array. When outlet particles leave the outlet region (specified with a bounding box) they are removed from the simulation.

class pysph.sph.simple_inlet_outlet.SimpleInlet(inlet_pa, dest_pa, spacing, n=5, axis='x', xmin=-1.0, xmax=1.0, ymin=-1.0, ymax=1.0, zmin=-1.0, zmax=1.0)[source]

Bases: object

This inlet has particles stacked along a particular axis (defaults to ‘x’). These particles can move along any direction and as they flow out of the domain they are copied into the destination particle array at each timestep.

Inlet particles are stacked by subtracting the spacing amount from the existing inlet array. These are copied when the inlet is created. The particles that cross the inlet domain are copied over to the destination particle array and moved back to the other side of the inlet.

The motion of the particles can be along any direction required. One can set the ‘u’ velocity to have a parabolic profile in the ‘y’ direction if so desired.

Constructor.

Note that the inlet must be defined such that the spacing times the number of stacks of particles is equal to the length of the domain in the stacked direction. For example, if particles are stacked along the ‘x’ axis and n=5 with spacing 0.1, then xmax - xmin should be 0.5.

Parameters:
  • inlet_pa (ParticleArray) – Particle array for the inlet particles.
  • dest_pa (ParticleArray) – Particle array for the destination into which inlet flows.
  • spacing (float) – Spacing of particles in the inlet domain.
  • n (int) – Total number of copies of the initial particles.
  • axis (str) – Axis along which to stack particles, one of ‘x’, ‘y’, ‘z’.
  • xmax, ymin, ymax, zmin, zmax (xmin,) – Domain of the outlet.
update(solver=None)[source]

This is called by the solver after each timestep and is passed the solver instance.

class pysph.sph.simple_inlet_outlet.SimpleOutlet(outlet_pa, source_pa, xmin=-1.0, xmax=1.0, ymin=-1.0, ymax=1.0, zmin=-1.0, zmax=1.0)[source]

Bases: object

This outlet simply moves the particles that comes into it from the source and removes any that leave the box.

Constructor.

Parameters:
  • outlet_pa (ParticleArray) – Particle array for the outlet particles.
  • source_pa (ParticleArray) – Particle array from which the particles flow in.
  • xmax, ymin, ymax, zmin, zmax (xmin,) – Domain of the outlet.
update(solver=None)[source]

This is called by the solver after each timestep and is passed the solver instance.