 ## ModSlaveSim help v3.06 - 4.3.2. Flow Computer

#### 4.3.2. Flow Computer

##### 4.3.2.1. Introduction and Guide

This example simulates a simple "flow computer", which monitors a flow rate and computes and stores the total and average flow rate for the last period.

GUIDE:

(To load this simulation select the file Flow computer from File->Simulation Examples....)

When ready to start press Run Slave Simulation. Press it again to stop or pause the simulation.

At the end of every flow computer cycle of 10 seconds (stored in the Period register) you will see the flow computer simulation update the Total and Average flow for that cycle (and Elapsed time is reset to zero for the next cycle). During the cycle, as the Elapsed time within the cycle increases, you can see the Running total changing as the simulation calculates the flow from the Flow rate, which is itself changing during the cycle.

For further guidelines see Running Example Simulations

The program listing and explanatory notes for the simulation follow. Before looking at them, please read the short sections Introduction to programming simulations and Language: Quick Start Guide

##### 4.3.2.2. Slave 1: flow computer

The table below shows the simple program underlying the simulation.

Table 3. Slave 1: flow computer

\$100 Flow rate
```if (\$\$ >= 3000 &&
\$\$ <= 4000) then \$\$ + 15
else 3000```
\$101 Period
\$102 Total
```if \$105 >= \$101 then
\$104 - (\$105 - \$101) * \$100```
\$103 Average `\$102 / \$101`
\$104 Running total
```if \$105 >= \$101
then (\$105 - \$101) * \$100
else \$\$ + CycleTime * \$100```
\$105 Elapsed time
```if \$\$ >= \$101
then (\$\$ - \$101)
else \$\$ + CycleTime```

Notes.

• \$100 : Flow rate. The monitored flow rate. This increases gradually in this simulation then drops down and starts to rise again. It could have been varied with less predictability using, for example, a random walk.

• \$101 : Period. The period in seconds for which the total and average are computed.

• \$102 : Total. The total flow for the last complete flow computer cycle. It is copied from the Running total (\$104) at the end of each flow computer cycle, and a correction for the flow in the partial (CycleTime) cycle, at the end of the flow period, is subtracted from RunningTotal(\$104) (see NB below). The code `\$105 >= \$101` tests for the end of the flow computer cycle (see the note on Elapsed time below).

```    if \$105 >= \$101
then \$104 - (\$105 - \$101) * \$100```

NB.  Partial periods. It is common to get partial periods since the cycle in the simulation will often not be synchronised to the `CycleTime`. This can be as a result of operating system variation in how frequently it allocates resources. However, it might also be because, for example, the flow computer cycle Period is not synchronized with a Run Interval: the former may be 10 seconds and the latter .80 second. So the periods do not align precisely and this may occur both at the start and end of a period.

In this simulation, partial periods are also calculated in ElapsedTime and RunningTotal. In this register, the partial period calculation is (Elapsed time - Period):

`    `(\$105 - \$101)``

• \$103 : Average. The average flow rate for the last complete flow computer cycle. It is computed simply by dividing the total flow by the flow computer period.

`    \$102 / \$101`
• \$104 : Running total. The flow so far for the current flow computer cycle. At the end of each flow computer cycle, the running total is initialized with the flow for the partial `CycleTime` cycle at the start of the next flow computer cycle. The running total is then incremented with the flow for each complete `CycleTime` cycle (see NB under \$102 Total for an explanation of partial periods). The code `\$105 >= \$101` tests for the end of the flow computer cycle (see the note on Elapsed time below).

```    if \$105 >= \$101
then (\$105 - \$101) * \$100
else \$\$ + CycleTime * \$100```
• \$105 : Elapsed time. The number of seconds since the start of the current flow computer cycle. The elapsed time is incremented by `CycleTime` on each `CycleTime` cycle. When the elapsed time exceeds Period it is re-initialized to the partial `CycleTime` cycle `\$\$ - \$101` (see NB under \$102 Total for an explanation of partial periods). This register is an example of a Periodic timer

```    if \$\$ >= \$101
then \$\$ - \$101
else \$\$ + CycleTime```