Ejemplo compuesto — sistema multidominio

Actualizado: 2026-03-31

Este ejemplo muestra cómo varios modelos independientes pueden coexistir e interactuar dentro de un mismo sistema.

Se combinan dos dominios distintos:

  • un modelo químico
  • un modelo epidemiológico

Cada uno de ellos está implementado en una clase externa y evoluciona de forma independiente. El modelo se encarga de coordinarlos y establecer relaciones entre ellos.

En este caso, la evolución del sistema químico modifica dinámicamente el parámetro beta del modelo epidemiológico, generando un acoplamiento entre ambos sistemas.

El modelo:

  • ejecuta cada sistema por separado
  • intercambia información entre ellos
  • representa su evolución en tiempo real

Cada componente mantiene su lógica; el modelo define cómo se relacionan.

El ejemplo

  • La química produce una magnitud C
  • Esa magnitud modifica la propagación de la epidemia
  • Ambos sistemas evolucionan en paralelo
  • El modelo los conecta mediante una relación simple

 

 

 

 

Ejemplo

/*
    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}

    ; --- Epidemiology 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