Introduction
to simulation with

Activities are shown in blue boxes as this one. 
Most of the activities are videobased. In the former tutorial, based on LabVIEW 8.5, there were no videos, but in stead detailed explanations. I have removed the explanation in the present tutorial where the videos give the same information.
If  for some reason  the videos does not display correctly in the current player, try some other. Windows Media Player is probably the default video player on your PC. Alternative players are RealPlayer and QuickTime (both can be downloaded from the Internet for free).
More tutorials that may be relevant for you as a LabVIEW user are available from Finn's LabVIEW Page.
The LabVIEW Control and Simulation Module contains a block diagram based environment for simulation of linear and nonlinear continuoustime and discretetime dynamic systems. Many simulation algorithms (i.e. numerical methods for solving the underlying differential equations) are available, e.g. various RungeKutta methods. The mathematical model to be simulated must be represented in a simulation loop, which in many ways is similar to the ordinary while loop in LabVIEW. You can make the simulation run as fast as the computer allows, or you can make it run with a real or scaled time axis, thus simulating realtime behaviour, with the possibility of the user to interact with the simulated process. The simulation loop can run in parallel with while loops within the same VI.
Once the Control and Simulation Module is installed, the Simulation palette is available from the Functions palette. The video below gives overview over the contents of the Simulation Palette.
Play the video sim_palette_overview (17 minutes, the video will be opened in a new window) to get an overview over the contents of the Simulation Palette. It is assumed that you have the block diagram of any VI opened. 
In this section we will study and partly develop a simulator of a liquid tank. Actually, we will play with a premade example, but not develop it from scratch. I think this is an effective way to learn using the tools, assuming that you have basic skills in LabVIEW programming. You are supposed to have basic knowledge about modeling of dynamic systems, as described in e.g. Dynamic Systems  modelling, analysis and simulation or in any other book about dynamic systems theory.
The system to be simulated is a liquid tank, see the figure below. The simulator will calculate and display the level h at any instant of time. The simulation will run in real time, with the possibility of scaled real time, thereby giving the feeling of a "real" system. The user can adjust the inlet by adjusting the pump control signal, u.
Liquid tank
Any simulator is based on a mathematical model of the system to be simulated. Thus, we start by developing a mathematical model of the tank.
We assume the following (the parameters used in the expressions below are defined in the figure above):
m(t) = ρAh(t)
q_{in}(t) = K_{u}u(t)
q_{out}(t) = K_{v}sqrt[ρgh(t)]
Mass balance (i.e., rate of change of the mass is equal to the inflow minus the outflow) yields the following differential equation:
dm(t)/dt = ρq_{in}(t)  ρq_{out}(t)] (1)
or, using the above relations,
d[ρAh(t)]/dt = ρK_{u}u(t)  ρK_{v}sqrt[ρgh(t)] (2)
We will now draw a mathematical block diagram of the model. This block diagram will then be implemented in the block diagram of the simulator VI. As a proper starting point of drawing the mathematical block diagram, we write the differential equation as a statespace model, that is, as a differential equation having the first order time derivative alone on the left side. This can be done by pulling ρ and A outside the differentiation, then dividing both sides by ρA. The resulting differential equation becomes
d[h(t)]/dt = (1/A)*{K_{u}u(t)  K_{v}sqrt[ρgh(t)]} (3)
This is a differential equation for h(t). It tells how the time derivative dh(t)/dt can be calculated. h(t) is calculated (by the simulator) by integrating dh(t)/dt with respect to time, from time 0 to time t, with initial value h(0), which we here denote h_{init}. To draw a block diagram of the model (3), we may start by adding an integrator to the empty block diagram. The input to this integrator is dh/dt, and the output is h(t). Then we add mathematical function blocks to construct the expression for dh/dt, which is the right side of the differential equation (3). The resulting block diagram for the model (3) can be as shown in the figure below.
Mathematical block diagram of Differential Equation (3)
The numerical values of the parameters are shown in the front panel picture below.
We will assume that there are level alarm limits to be displayed in the simulator. The limits are
AH_h = 0.9m (Alarm High)
AL_h = 0.1m (Alarm Low)
The block diagram developed above will be implemented in a Simulation Loop in the Block diagram of our simulation VI.
The subsequent figures show the front panel and the block diagram of the complete VI, tanksim.vi.
Front panel of tanksim.vi.
Block diagram of tanksim.vi.
Open the Front panel of
tanksim.vi. Play the video tanksim_frontpanel (20 minutes, the video is opened in a new window) to 
Open the Block diagram of
tanksim.vi. Play the video: tanksim_blockdiagram (42 minutes, the video is opened in a new window). Note: In the beginning of the video I refer to four videos, but I have compiled these four videos into one video. 
Play the video
tanksim_config_sim (28 minutes, the video is opened in a new window). Note: In the video a figure illustrating the time step (or step size) is shown. In the video I say that this figure exists in the tutorial, but it does not (but you see it in the video). 
(There are no blue activity boxes in Chapter.)
A statespace model is a set of first order differential equations constituting the model of the system. Statespace models is a standardized model form. It is common that mathematical models of dynamic systems are written as statespace models. To be a little more specific, here is a general second order statespace model (the dots represents the arguments of the functions):
dx_{1}/dt = f_{1}(x_{1},x_{2},...)
dx_{2}/dt = f_{2}(x_{1},x_{2},...)
y = g(x_{1},x_{2},...)
where f_{1}(•) and f_{2}(•) are functions containing the righthand part of the first order differential equations. The arguments may be state variables, input variables, and parameters. These functions may be linear or nonlinear. They are the timederivatives of the states, x_{1} and x_{2}, respectively. Sometimes one or more output variables are defined. Above, the output variable is y, and the output function is g(•).
To implement the block diagram of a statespace model, you may start by adding one Integrator block for each of the state variables on the block diagram. The output of the integrators are the state variables. The inputs to the integrators are the time derivatives, and the f_{1}(•) and f_{2}(•) functions in the representative model shown are these time derivatives. To implement the functions you have the following two options (which also may be combined):
Here is a simple example of using the Formula node. Given the following state space model:
dx_{1}/dt = x2
dx_{2}/dt = x_{1} + u
y = x_{1}
(which is a state space model of an oscillator). u is the input variable, and y is the output variable. ssformulanode.vi shown below implements a simulator for this system. A Formula node is used to represent the right side of the differential equations. The integration of the time derivatives are performed by Integrator blocks from the Continuous palette.
Front panel and block diagram of ssformulanode.vi.
Using Formula Node in stead of block functions to calculate the time derivatives may give a simpler block diagram. However, if the expressions for the time derivatives (i.e. the righthand sides of the differential equations) contains nonlinear functions, it may be more difficult to implement these in the Formula Node than with function blocks.
You can create a subsystem of a part of a simulation diagram. The first step is to select or mark the part of interest, see the figure below, which shows the block diagram of tanksim.vi.
The first step in creating a subsystem in the simulation diagram is to select the part of interest
Then the subsystem is created using the menu
. The resulting diagram is shown in the figure below.
The resulting simulation diagram, including the subsystem
Note that you can change the size of the subsystem icon using the cursor.
If you want you can open the front panel of the subsystem by doubleclicking the subsystem icon, see the figure below.
The front panel of the subsystem
You can open the block diagram of the subsystem via the
menu, see the figure below.
The block diagram of the subsystem
LabVIEW can create a linear state space model from a linear or nonlinear subsystem. (Creating subsystems is described in the previous section.) The procedure is to select or mark the subsystem of interest, and then create the linear model by using the following menu:
. You are given the option of saving the linear model as a model (to be used by functions in the Control Design Toolkit) or as a VI containing the state space model in the form of a cluster of coefficient arrays. Perhaps the most flexible choice is VI.Simulating control systems is done in the same way as simulating dynamic systems. You can include virtually every control function in a model block diagram inside the Simulation Loop. In Guidelines to PID Control with LabVIEW there is an example of a simulator of a PID control system.
You can convert models between the Simulation Module and the Control Design Toolkit using the conversion functions on the Model Conversion palette on the Control Design Tookit. The two conversion functions are shown in the figure below.
The conversion functions on the Control Design Toolkit / Model Conversion palette
It is possible to put almost any LabVIEW code for e.g. analysis and design of into a simulation diagram inside a Simulation loop, but doing so may give an unnecessary large or complicated simulation code, and the simulation execution may be delayed. Therefore, you should not put more code inside the Simulation loop than is strictly necessary for representing the model to be simulated. Other parts of the total code, e.g. optimal control design functions (as the LQR function) or Kaman filter (state estimator) design functions (as the Kalman Gain function), may be put into one or more ordinary While loops running in parallel with the Simulation loop. These While loops may be programmed to run slower than the Simulation loop. Data can be exchanged between the loops using local variables (local variables are described in Introduction to LabVIEW).
Here is one example: kalmanfilter_tank.vi is a simulator of a Kalman Filter which estimates the outflow of a simulated liquid tank. (You can run this simulator if you have the LabVIEW Simulation Module installed.) The simulator is implemented with a Simulation Loop which contains a model of the tank and the expressions constituting the Kalman Filter algorithm. The Kalman gains are calculated using the Kalman Gain function of the Control Design Toolkit. This function is relatively computational demanding, and it is therefore put into a While loop which runs in parallell with the Simulation loop with a cycle time of 0.5 sec. The Kalman gains are made available inside the Simulation loop using local variables.
The simulation time step is 0.1 sec, and the Period (which is the actual, real time that LabVIEW used to proceed one simulation time step) is 0.025 sec. (Having the Period smaller than the simulation time step makes the simulator run faster than real time. This is favourable here since the process itself is a slow system, and we do not have time to sit waiting for responses to come.) If we had put the Kalman Gain function inside the Simulation loop, the specified Period of 0.025 sec could not have been obtained because it takes about 0.3 sec (this is however computerdependent) to execute Kalman Gain function.
Below are the front panel and the block diagram of kalmanfilter_tank.vi. Click on the figures to see them in full sizes. Note how local variables are used to exchange values across the loops. Note also how the Stop button and its local variable is used to stop both loops. The Mechanical Action property of the Stop button must be set to Switch until Released. If it is set to one of the Latch... properties, it it will not be possible to create local variable for it.)
Front panel of kalmanfilter_tank.vi (click on the figure to see it in full size)
Block diagram of kalmanfilter_tank.vi (click on the figure to see it in full size)
You can translate SIMULINK (MathWorks) models into LabVIEW simulation models using the menu Tools / Simulation Tools / SIMULINK Translator in LabVIEW.