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]
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)
- 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
- arraysA 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_directoryusing the same format as stored by thedump_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_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_n_damp(ndamp)[source]
Set the number of timesteps for which the timestep should be initially damped.
- set_parallel_output_mode(mode='collected')[source]
Set the default solver dump mode in parallel.
The available modes are:
- collectedCollect 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.
Module solver tools
- class pysph.solver.tools.DensityCorrection(app, arr_names, corr='shepard', freq=10, kernel=None)[source]
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)
- class pysph.solver.tools.SimpleRemesher(app, array_name, props, freq=100, xi=None, yi=None, zi=None, kernel=None, equations=None)[source]
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.
xi (ndarray) – Positions to remesh the properties onto. If not specified they are taken from the particle arrays at the time of construction.
yi (ndarray) – Positions to remesh the properties onto. If not specified they are taken from the particle arrays at the time of construction.
zi (ndarray) – 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.
- class pysph.solver.tools.Tool[source]
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.
Module boundary conditions
Inlet Outlet Manager
- class pysph.sph.bc.inlet_outlet_manager.CopyNormalsandDistances(dest, sources)[source]
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
- class pysph.sph.bc.inlet_outlet_manager.IOEvaluate(dest, sources, x, y, z, xn, yn, zn, maxdist=1000.0)[source]
- 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
- class pysph.sph.bc.inlet_outlet_manager.InletBase(inlet_pa, dest_pa, inletinfo, kernel, dim, active_stages=[1], callback=None, ghost_pa=None)[source]
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
- 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]
- 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]
- 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
- class pysph.sph.bc.inlet_outlet_manager.OutletBase(outlet_pa, source_pa, outletinfo, kernel, dim, active_stages=[1], callback=None, ghost_pa=None)[source]
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
- 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]
Create object with information of outlet
The name is kept different for distinction only.
- class pysph.sph.bc.inlet_outlet_manager.UpdateNormalsAndDisplacements(dest, sources, xn, yn, zn, xo, yo, zo)[source]
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