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)
-
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.
-
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 thedump_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_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_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.
-
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.
Look at solver/fluid_solver.py for an example.
Parameters: options (dict) – options set by the user using commandline (there is no guarantee of existence of any key)