In previous chapters we saw how GPSS-Plus can simulate systems with an apparently continuous behavior thanks to frequent execution of timed procedures (TIMER) and the use of SAVEVALUE to accumulate values such as filling a tank.
Up to now we used a simple formula to simulate the filling:
SAVEVALUE level, X$level + X$flow * 0.1
This method is effective, but basic. It assumes the flow is constant during each time interval, which is not entirely true if the flow changes rapidly, for example with a sine function as in this case.
And when we talk about interval, here we are talking about that "0.1". SIN(0) is not the same as SIN(0.1). And if we want a more accurate estimate of the average value, it would probably be better to use SIN(0.05), i.e., the midpoint of the interval.
What happens if the flow varies during the time step?
When the value you are accumulating changes within the same interval, the system is not perfectly exact: you are adding an approximate average value, not the real one.
To improve this, GPSS-Plus includes the INTEGRATE block, which uses a numerical integration method called fourth-order Runge-Kutta (RK4) to compute a much more accurate estimate.
What does INTEGRATE do?
INTEGRATE { EXPRESSION: "SIN(T) + 2", DT: 0.1, SAVEVALUE: flow_RK4 }
This block:
-
Evaluates the expression at four key points of the interval: at the start (
X), twice at the midpoint (X + DT/2), and at the end (X + DT). -
Applies the fourth-order Runge-Kutta (RK4) method, combining those values with specific weights to accurately estimate how the function varies over that segment.
-
Stores the result in the indicated
SAVEVALUE, as a better estimate of the average value of the expression over that interval.
The resulting value will be a better estimate of the mean flow in that time interval, and we can use it to fill the tank more accurately.
Comparing the traditional method (Euler) and RK4
We will build an example that uses both methods in parallel:
flow: using the simple methodflow_RK4: using RK4 numerical integrationlevel: accumulated with the classic flowlevel_RK4: accumulated with the RK4 flow
In addition, we will plot the 4 curves using PLOTTER to visually compare the results.
What will you see in the plot?
level_RK4rises slightly more smoothly and a bit faster thanlevel, because it accounts for the fact that the flow grows within the interval.flow_RK4matches peaks and valleys better thanflow, which only evaluates a single point.The white line on screen (RK4 level) goes a bit ahead of the blue line (simple level).
When is it valid to use RK4?
The fourth-order Runge-Kutta (RK4) method, implemented by the INTEGRATE block in GPSS-Plus, is a numerical algorithm designed to solve ordinary differential equations of the form:
dy/dt = f(t)
And it does so assuming that the function f(t) is continuous and smooth over the interval being integrated.
But… GPSS-Plus is a discrete-event system
This means there are many variables (X$level, X$entitiesInQueue, X$facility, etc.) that can change abruptly, without continuity or smoothness.
Those changes depend on events that fire when other entities arrive, seize resources, or finish.
Therefore:
You should not use INTEGRATE with expressions that depend on discrete system elements.
INTEGRATE with RK4 is a powerful and accurate tool, as long as it is used with purely mathematical functions. It is not a magical simulation of the future: it is an intelligent estimator for a well-defined function.
Example
/*
Improving results with INTEGRATE
*/
SYSTEM {type:OPTIONS, TIME_DECIMALS:1, SPEED:5}
SYSTEM {TYPE:ON_TIMER, TRIGGER:filling, INTERVAL: 0.1}
PLOTTER {NAME:thePlot, Y_0:level, Y_1:flow,X:TIME_AC1}
PLOTTER {NAME:thePlot_RK4, Y_0:level, Y_1:flow,X:TIME_AC1}
PLOTTER {NAME:thePlot_ALL, Y_0:level, Y_1:flow, Y_2:level_RK4, Y_3:flow_RK4,X:TIME_AC1}
Graphic {NAME:warning,Type:TEXT,X:291,Y:342,Text:"warning"}
Graphic {NAME:warning_RK4,Type:TEXT,X:291,Y:302,Text:"Warning2"}
Graphic {NAME:Cube1,Type:LINE,POINTS:"[100,100],[100,500],[200,500],[200,100]"
, Close:1
, color:#00FFFF
, Fcolor:#FFFFFF}
Graphic {NAME:Line1,Type:LINE,fcolor:#FF6666, X1:102,Y1:100,X2:198,Y2:100}
Graphic {NAME:Line_RK4,Type:LINE,color:#333300, X1:98,Y1:100,X2:202,Y2:100}
INITIAL level, 0
INITIAL flow, 1.2
INITIAL level_RK4, 0
INITIAL flow_RK4, 1.2
INITIAL phase, 0
INITIAL constant, 0.6
START 100
;*****************************************************
PROCEDURE filling
; Oscillate flow as a function of simulated time
SAVEVALUE flow, X$constant + SIN(X$phase) * 4
; Smoothly increase the phase
SAVEVALUE phase, X$phase + 0.1
; Continuous integration of the level
INTEGRATE { EXPRESSION: "X$constant + SIN(T) * 4", METHOD: RK4, DT: 0.1, SAVEVALUE: flow_RK4 }
SAVEVALUE level, X$level + X$flow * 0.1
SAVEVALUE level_RK4, X$level_RK4 + X$flow_RK4 * 0.1
PLOT thePlot,X$level,X$flow
PLOT thePlot_RK4,X$level_RK4,X$flow_RK4
PLOT thePlot_ALL,X$level,X$flow,X$level_RK4,X$flow_RK4
MOVE {NAME:Line1, X1:98,Y1:(100+X$level*4) ,X2:202,Y2:(100+X$level*4)}
MOVE {NAME:Line_RK4, X1:98,Y1:(100+X$level_RK4*4) ,X2:202,Y2:(100+X$level_RK4*4)}
ASSIGN tFlow, round(X$flow, 2)
ASSIGN tLevel, round(X$level, 2)
ASSIGN tFlow_RK4, round(X$flow_RK4, 2)
ASSIGN tLevel_RK4, round(X$level_RK4, 2)
; State display
IF (X$level>=100)
MOVE {name:warning, text:"TANK FULL!"}
stop
ELSE
MOVE {name:warning, text:"Level: P$tLevel - Flow: P$tFlow"}
MOVE {name:warning_RK4, text:"Level: P$tLevel_RK4 - Flow: P$tFlow_RK4"}
ENDIF
TERMINATE
ENDPROCEDURE 1