Setting up a system for simulation

Before running a simulation with a system it is possible to configure the system. The configuration generally involves:

  • Setting parameters for the simulation

  • Setting any non connected inputs

  • Setting start values for the states

  • Selecting what states and variables to store

  • Connecting the system to other systems involved in the simulation

Setting parameters

Parameters are set by assigning a value to the parameter. If the parameter is a vector the input must also be a list, tuple or numpy vector, otherwise an exception will be thrown.

>>> = 2.0

or for a vector:

>>> = (2.0,1.0,0.0)

Setting inputs

Inputs can be connected to other outputs (see connecting systems below), but they can also be set before the simulation. If they are not connected that set value will be used by the system during the simulation. To set an input type

>>> sys.inputs.s = 2.0

for a matrix

>>> = [[1,2],[3,4]]

a vector is simply a column matrix and can be set in the same way as matrices.

Setting start values

Start values can be set by setting the state or input variable of the system

>>> sys.state.x = 2.0
>>> sys.inputs.a = 2.0

This value is then used as the first value for the state x.


A state or a variable can be stored during each step of the simulation. To do this call the store command of the system, with the name of the state or variable to be stored as an argument.


Connecting Systems

The Systems can be connected so that the output from one system is used as input to another. Say for example that you want to use a square wave to drive the force applied to a mass in the mass spring damper system, then the following code would be used

from pysim.simulation import Sim
from import MassSpringDamper,SquareWave
import matplotlib.pyplot as plt

#Create Simulation
sim = Sim()

#Setup Square Wave system
wavesys = SquareWave()"signal") = 0.2 = 100

#Setup a Mass Spring Damper system
sys = MassSpringDamper()"x1") = i = 0

#Connect variable "signal" from Square wave system to parameter f in
#MassSpringDamper system.

#Add both systems to simulation

#Simulate and plot the results
sim.simulate(50, 0.01)