This example shows a very simple case of integration between the model and an external JavaScript class in the context of continuous simulation.
The EpidemicModel class contains the mathematical logic governing the evolution of an epidemic.
The model does not implement these equations because they are provided by the external class. The model is responsible for:
- initializing the class with a set of parameters
- executing successive evolution steps
- retrieving the returned state
- and displaying it graphically
In this way, the algorithmic part is encapsulated in the class, while GPSS-Plus continues to control system execution, time, and visualization.
In the example
A classical epidemiological model with three quantities will be used:
S: susceptible populationI: infected populationR: recovered population
The external class calculates their evolution at each time step.
Example
SYSTEM {TYPE:OPTIONS,SPEED:8,TIME_DECIMALS:2}
GRAPHIC {NAME:Text1,TYPE:TEXT,X:233,Y:244,TEXT:"Epidemic model",font:"12"}
PLOTTER {NAME:epiPlot,Y_0:S,Y_1:I,Y_2:R,X:TIME_AC1}
LOAD_CLASS https://gpss-plus.com/imports_js/EpidemicModel.js
INSTANCE_CLASS {name:epidemic, class:EpidemicModel}
LOAD_MEDIA https://gpss-plus.com/imports_js/VerticalGauge.js
INSTANCE_MEDIA {name:gaugeS, class:VerticalGauge}
INSTANCE_MEDIA {name:gaugeI, class:VerticalGauge}
INSTANCE_MEDIA {name:gaugeR, class:VerticalGauge}
START 2000
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
PROCEDURE PRE_RUN
ASSIGN epiInit,{
operation:"init",
S:990,
I:10,
R:0,
beta:0.4,
gamma:0.08
}
EXEC_CLASS {name:epidemic,in:V$epiInit,savevalue:epiState}
ASSIGN cfgS,{
operation:"init",
title:"S",
x:120,
y:400,
width:80,
height:260,
value:990,
min_value:0,
max_value:1000,
color:"#f39c12",
background:"rgba(255,255,255,0.85)",
font:"12px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeS,in:V$cfgS}
ASSIGN cfgI,{
operation:"init",
title:"I",
x:260,
y:400,
width:80,
height:260,
value:10,
min_value:0,
max_value:1000,
color:"#3498db",
background:"rgba(255,255,255,0.85)",
font:"12px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeI,in:V$cfgI}
ASSIGN cfgR,{
operation:"init",
title:"R",
x:400,
y:400,
width:80,
height:260,
value:0,
min_value:0,
max_value:1000,
color:"#8e44ad",
background:"rgba(255,255,255,0.85)",
font:"12px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeR,in:V$cfgR}
ASSIGN epiInit,{
operation:"init",
S:990,
I:10,
R:0,
beta:0.4,
gamma:0.08
}
EXEC_CLASS {name:epidemic,in:V$epiInit,savevalue:epiState}
TERMINATE_VE
ENDPROCEDURE
GENERATE 0,0,0,1
while (1==1)
ASSIGN epiStep,{operation:"step",dt:0.2}
EXEC_CLASS {name:epidemic,in:V$epiStep,savevalue:epiState}
ASSIGN s,round(X$(epiState.S),2)
ASSIGN i,round(X$(epiState.I),2)
ASSIGN r,round(X$(epiState.R),2)
ASSIGN t,round(X$(epiState.time),2)
MOVE {name:Text1,text:"t=P$t S=P$s I=P$i R=P$r"}
PLOT epiPlot, P$s, P$i, P$r
ASSIGN setS,{operation:"set",value:P$s}
EXEC_MEDIA {name:gaugeS,in:V$setS}
ASSIGN setI,{operation:"set",value:P$i}
EXEC_MEDIA {name:gaugeI,in:V$setI}
ASSIGN setR,{operation:"set",value:P$r}
EXEC_MEDIA {name:gaugeR,in:V$setR}
IF (X$(epiState.I) <= 0.01)
stop
ENDIF
ADVANCE 0.2,0
endwhile
ENDGENERATE 1