This example shows how several independent models can coexist and interact within a single system.
Two different domains are combined:
- a chemical model
- an epidemiological model
Each is implemented in an external class and evolves independently. The model is responsible for coordinating them and establishing relationships between them.
In this case, the evolution of the chemical system dynamically modifies the beta parameter of the epidemiological model, creating a coupling between the two systems.
The model:
- runs each system separately
- exchanges information between them
- displays their evolution in real time
Each component maintains its own logic; the model defines how they relate to one another.
The example
- The chemical system produces a quantity
C - This quantity affects the spread of the epidemic
- Both systems evolve in parallel
- The model connects them through a simple relationship
Example
/*
MINIMAL UNIVERSE
Chemistry + Epidemiology + MEDIA
*/
SYSTEM {TYPE:OPTIONS,SPEED:8,TIME_DECIMALS:2}
GRAPHIC {NAME:Text1,TYPE:TEXT,X:409,Y:421,TEXT:"Universe",font:"12"}
PLOTTER {NAME:epiPlot,Y_0:S,Y_1:I,Y_2:R,X:TIME_AC1}
PLOTTER {NAME:chemPlot,Y_0:C,Y_1:BETA,X:TIME_AC1}
LOAD_CLASS https://gpss-plus.com/imports_js/ChemistryReactor.js
INSTANCE_CLASS {name:reactor, class:ChemistryReactor}
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}
INSTANCE_MEDIA {name:gaugeC, class:VerticalGauge}
INSTANCE_MEDIA {name:gaugeBeta, class:VerticalGauge}
START 2000
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
;========================================================
PROCEDURE PRE_RUN
; --- Chemical system ---
ASSIGN chemInit,{
operation:"init",
A:10,
B:5,
C:0,
k:0.05
}
EXEC_CLASS {name:reactor,in:V$chemInit,savevalue:chemState}
; --- Epidemiological system ---
ASSIGN epiInit,{
operation:"init",
S:990,
I:10,
R:0,
beta:0.45,
gamma:0.08
}
EXEC_CLASS {name:epidemic,in:V$epiInit,savevalue:epiState}
; --- Epidemiological gauges ---
ASSIGN cfgS,{
operation:"init",
title:"S",
x:100,
y:150,
width:70,
height:220,
value:990,
min_value:0,
max_value:1000,
color:"#f39c12",
background:"rgba(255,255,255,0.85)",
font:"18px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeS,in:V$cfgS}
ASSIGN cfgI,{
operation:"init",
title:"I",
x:190,
y:150,
width:70,
height:220,
value:10,
min_value:0,
max_value:1000,
color:"#3498db",
background:"rgba(255,255,255,0.85)",
font:"18px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeI,in:V$cfgI}
ASSIGN cfgR,{
operation:"init",
title:"R",
x:280,
y:150,
width:70,
height:220,
value:0,
min_value:0,
max_value:1000,
color:"#8e44ad",
background:"rgba(255,255,255,0.85)",
font:"18px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeR,in:V$cfgR}
; --- Chemical gauge ---
ASSIGN cfgC,{
operation:"init",
title:"C",
x:420,
y:150,
width:70,
height:220,
value:0,
min_value:0,
max_value:5,
color:"#27ae60",
background:"rgba(255,255,255,0.85)",
font:"18px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeC,in:V$cfgC}
; --- Effective beta gauge ---
ASSIGN cfgBeta,{
operation:"init",
title:"beta",
x:510,
y:150,
width:70,
height:220,
value:0.45,
min_value:0,
max_value:0.5,
color:"#e74c3c",
background:"rgba(255,255,255,0.85)",
font:"18px Arial",
valueFont:"12px Arial"
}
EXEC_MEDIA {name:gaugeBeta,in:V$cfgBeta}
TERMINATE_VE
ENDPROCEDURE
;========================================================
GENERATE 0,0,0,1 {name:Gen1,x:87,y:527}
SAVEVALUE baseBeta, 0.45
SAVEVALUE alphaChem, 0.30
while (1==1)
;----------------------------------
; 1) Advance chemistry
ASSIGN chemStep,{operation:"step",dt:0.2}
EXEC_CLASS {name:reactor,in:V$chemStep,savevalue:chemState}
ASSIGN chemC, X$(chemState.C)
;----------------------------------
; 2) Coupling
ASSIGN betaEff, X$baseBeta / (1 + X$alphaChem * P$chemC)
;----------------------------------
; 3) Advance epidemiology
ASSIGN epiStep,{
operation:"step",
dt:0.2,
beta:P$betaEff
}
EXEC_CLASS {name:epidemic,in:V$epiStep,savevalue:epiState}
;----------------------------------
; 4) Rounding
ASSIGN t, round(X$(epiState.time),2)
ASSIGN s, round(X$(epiState.S),2)
ASSIGN i, round(X$(epiState.I),2)
ASSIGN r, round(X$(epiState.R),2)
ASSIGN c, round(X$(chemState.C),3)
ASSIGN betaVal, round(P$betaEff,4)
MOVE {
name:Text1,
text:"t=P$t S=P$s I=P$i R=P$r C=P$c beta=P$betaVal"
}
PLOT epiPlot, P$s, P$i, P$r
PLOT chemPlot, P$c, P$betaVal
;----------------------------------
; 5) Update MEDIA
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}
ASSIGN setC,{operation:"set",value:P$c}
EXEC_MEDIA {name:gaugeC,in:V$setC}
ASSIGN setBeta,{operation:"set",value:P$betaVal}
EXEC_MEDIA {name:gaugeBeta,in:V$setBeta}
IF (X$(epiState.I) <= 0.01)
STOP
ENDIF
ADVANCE 0.2,0
endwhile
ENDGENERATE 1