DynSim Documentation
Contents
Structure of this Documentation
There are four elements of documentation for DynSim:
Tutorial - a walkthrough of constructing a simple simulation and running it.
Overview Reference (this page) containing reference material for DynSim as a whole.
Built-in Documentation - the primary documentation; see the next section.
Auxiliary Documentation - additional reference material on individual classes and functions not directly pertaining to the use of the class or function. This is the place to find details on assumptions and philosophy of some of the more complex model and algorithm classes. You can find links to these companion pages in the list of "Classes Available" below as well as other areas of this page.
Built-in Documentation
The Python docstrings ("built-in documentation") are the primary documentation of DynSim, and contain detailed information on the use of individual modules, classes and functions within DynSim. These docstrings are available via different mechanisms for the most convenience:
an interactive web page (generated by pydoc) which provides convenient browsing through the package, modules and classes - under construction!
- a single, plain web page (or .pdf document?) suited to printing as a paper manual - coming soon!
- direct access to doc strings, available during model development (interactive or development environments; check how your environment displays these)
The content of each of these mechanisms is generated from the doc-strings in the package and each module, class and function.
Introduction
DynSim is a simulation environment created within the Python language aimed at fast construction and testing of control schemes in the field of Process Control Engineering.
It is essentially a simple set of classes which allow quick simulation of analog and hybrid control schemes and the process they are applied to. The framework includes a library of "block" or component classes from which we can build the simulation, but allows the user to pull it all together with his own python scripting to do all the housekeeping and analysis or to interface with other data or input/output systems.
There is no GUI for execution. For rapid development of models, a text-based programming approach is chosen. Of course, there is graphical output for trends and output data exploration. It is also easy to add graphical controls and animation outputs.
Installation
Prerequisites
python (refer to "Prepackaged Python(x,y)" on the SciPy page)
- Pydee (this is included in python(x,y)) or similar python editor/IDE
dynsim
You can find the latest version of DynSim in the Weblog at the bottom of the main DynSim page. Look for the latest (top-most) entry with the title "Uploaded new version (dynsim_YYYYMMDD.zip)" . The zip file will contain all of files you need. Find your installed version of Python and navigate to "...\python\Lib\site-packages". Unzip the dynsim directory there. That's it! You should now be able to start up Python and from dynsim import control etc.
Classes Available
For an overview of the dynsim classes, the structure of dynsim is outlined here. Some modules/classes have links in the dot-points below (click on the name) to an overview of that component and these are intended to complement the docstrings held within each module/class. Note that capitalisation of the class name here is for clarity and doesn't necessarily match the actual class name.
Control Classes (from dynsim import control) basic simulation control and control/dynamics building blocks:
PID controller
Smith-Predictor controller (PID controller with deadtime compensation)
Constraint (building block for linear model-based constraint control)
Selector (PID controller output selector for constraint control (aka cross-over or override control) )
Deadtime (process dynamics, especially for transport delays such as conveyors or long pipes)
VDeadtime (process dynamics for a variable speed transport delay of a parameter, e.g. conveyor or long pipe with variable speed or flow)
FlowConveyor (process dynamics of variable speed transport delay of flow onto a conveyor, where the density or loading of material on part of a conveyor will, multiplied by the conveyor speed, determine the outflow)
Lag (First-order time-constant)
FOPDT (first-order plus deadtime dynamics, with option of integrating)
Moving-Average (for filtering signals or for process dynamics)
Integrator (for integrating processes or integral-only control)
SimpleTank (specialisation of Integrator to specifically represent level in a tank, with convenient handling of inflow, outflow and time units)
Replay (to allow incorporation of existing time-series data into a simulation)
Log (logger function storing data and later plotting and saving in a file)
timesteps (the main generator of time and dt tuples iterating your model using a "for" loop)
largesteps (allowing specific elements of the simulation to occur at large time-steps, e.g. for discrete-time control)
smallsteps (allowing specific elements of the simulation code to be simulated at a smaller time-step, e.g. for stiff equations)
Process Classes (from dynsim import process) presently aimed at simple slurry streams:
Stream (liquid and solids mass-flow, with methods to calculate %solids etc., includes overloaded +,-,*,/,negate functions allowing streams to be added etc.)
Tank (with demanded discharge rate, including calculation of overflow flows)
Thickener (with reasonable "compression" dynamics describing inventory, UF density, mud level)
Hydrocyclone (calculates cut-size d50c and flow distribution based on Plitt model, no size partitions are calculated)
Conveyor (conveyor handling 2-phase flow input/output streams)
LongPipe (represents a long pipe where propagation delay of density is related to the flow through the pipe)
Pump (simple linear, no dynamics)
Valve (linear only)
Mechanics Classes (from dynsim import mechanics)
- (mechanics requires pyODE (physics engine) and visual (from Vpython) installed for complete functionality)
CartPole (classical cart and pole balancing control problem)
Finance Classes (from dynsim import finance) for personal finance simulation (beware - specific to Australian taxation law):
- in progress!
InputOutput Classes (from dynsim import io) providing real-time Input-Output functionality, especially using OPC:
- not started yet!
Discrete Classes (from dynsim import discrete) if necessary for integrating SimPy discrete-event elements to form a hybrid simulation:
- not started yet!
How are these models different to what you'd find in other dynamic simulation systems? Here's a comparison with SysCAD (from their published documentation, and from a Process Control perspective).
Example simulation models (included in zip file)
These example simulation scripts provide a wide range of concise Process Control simulations to learn from, or use as a starting point template to ease the construction of a new simulation. These are distributed with the DynSim source.
simple.py - a simple example of a PID controller operating with process dynamics represented by the First-Order-Plus-DeadTime (FOPDT) class
PIDwithNonLinearProcess.py - example with a non-linear process dynamic
SmithPredictor.py - a Smith-Predictor control demo
SmithPredictorWithIntegrators.py - Smith-Predictor control of an unstable process
SmithPredictorWith2Integrators.py - modified Smith-Predictor to handle unstable processes
MovingAverageFilterDemo.py - highlights the difference between 1st-order and MA filters in a control loop.
HierarchicalSmithPredictorDemo.py - illustrates how to create a new class out of DynSim2 classes, to create hierarchical models or to minimise code repetition.
CartPoleControl.py - illustrates control of a pole-cart system
replay.py - demonstrates how to replay external data as part of the simulation
simple_real-time-plot.py - demonstrates how to use the real-time plotting capability of dynsim
SmithPredictorAssembly.py - Smith Predictor control constructed from separate dynamic units
SmithPredictorHierarchical.py - illustrates how to create a new class out of DynSim2 classes, to create hierarchical models or to minimise code repetition.
stiff.py - illustrates the effect of a stiff equation in a simulation
template-sim.py - a starting template for constructing a simulation (includes lots of in-situ guidance text on where to put everything)
template-class.py - a starting template for constructing a new class for use in dynsim (in progress!)
Advanced Simulation Topics
Simulation Initialisation
Initialisation of a model at the start of a simulation can be done by setting "dt" to be -ve (the DynSim2 library module objects recognise this), so that the simulation is executed (with blocks in "track" mode) for several timesteps in order for the initialisation of output values to be fully propagated through all blocks. DynSim's "timesteps" generator (see the examples) is designed to do this automatically and easily. The number of initialisation timesteps required will depend on how well the blocks are ordered and the number of signal "recycles" in the model. At most there will need to be N initialisation steps for a model which contains a cascaded sequence of N objects/blocks executed in the worst-case order. However, the default of two "timesteps" of initialisation are usually sufficient.
If you need to heed these initialisation "timesteps" in your own script code, the following template is useful:
if dt<0.0: # code initialise variables else: # normal execution code
Stiff Models
Wikipedia defines stiff differential equations as those "for which certain numerical methods for solving the equation are numerically unstable, unless the step size is taken to be extremely small."
However, there seems to be two main types of "stiffness" with regard to how they can be treated in a simulation:
- a type arising from instability in one "period" of the simulation time or transitioning through a "range" of values at one part of the simulation time; and
- a type arising from isolated elements (differential equations) of the simulation being persistently unstable compared to the remainder of the simulation.
Type 1 is described in detail in the Wikipedia entry on "stiff equations" and can be solved by taking small steps when the values are changing rapidly. In dynsim this is difficult and would require drastic customisation of the timestep over the unstable periods with a priori knowledge of when these periods are. In dynsim, these are best handled by simply reducing the timestep until the solution doesn't change (to a tolerance).
Type 2 is essentially a particular form of Type 1, one which can be better solved by applying smaller timesteps to only the problematic equations of the simulation code (as opposed to different periods of simulation time). Type 2 cannot be effectively solved using the solution to Type 1 because the whole simulation would have to be slowed down.
The Stiff systems solution described here are for Type 2 systems that have some segments of the simulation code (equations) that need to have a very small timestep while the rest of the simulation can tolerate a very large timestep. If the entire simulation is run at the very small timestep, the progress is very slow. If the entire simulation is run at the very large timestep, the simulation accuracy is inadequate. Multiple time-step intervals may, as well as being required for stiff systems, may also be needed for a sampled PLC/DCS system (e.g. 5 second control scan-time) plus continuous plant (very small timesteps).
The principle behind this solution is achieved by including a nested "for" loop with smaller "dt" set (e.g. inner loop dt = 0.1, outer loop dt = 1.0), such that only the "stiff" (or high resolution) components are executed at a smaller time-step. In practice, this is automatically performed by using the generator function "smalltimesteps" for a "for" loop within the main simulation loop. Look at the documentation for "smalltimesteps" for further details.
Discrete-Time execution
Some parts of a simulation may need to be executed at a very slow interval to simulate discrete-time control (such as PID loop execution in a PLC or DCS at 1 second intervals). The "largesteps" generator function should be used as an inner execution loop within the main simulation "for" loop. Note that, for discrete-time implementation, the same solution can be achieved by using either "smallsteps" or "largesteps" depending on your perspective.
Selecting the Timestep
As with any sequential-modular simulator, selecting the timestep or "dt" is important. You want dt to be as large as possible to make your simulation run quickly; but you want the simulation to be accurate. The most common technique for determining the largest discretisation is to create a working simulation, then decrease dt by factors of 2 until no changes occur to the results (to the desired accuracy). If there is no change on the first decrease, then try increasing dt until you get a change and backtrack.