Hybrid simulation

Updated: 2026-01-27

In the previous chapters we simulated a tank fill using continuous functions, recorded its evolution with PLOTTER, and improved accuracy with numerical integration (INTEGRATE, RK4). Now we combine all of that into a realistic hybrid system where continuous and discrete logic coexist.

Example: solar station with users connected

This model represents a hybrid system where:

  • There is a solar energy source (continuous model)
  • There are discrete users who arrive, decide whether they need to charge, and connect if needed

Continuous part: solar generation

Solar input is modeled as a sinusoid that simulates the day cycle: it oscillates smoothly from 0 to 1.5 over 1440 minutes (24 hours). Each time unit represents one minute, and each timer tick adds energy to the system battery.

INTEGRATE {
  EXPRESSION: "0.75 + 0.75 * SIN((T / 1440) * 6.2832)",
  DT: 1,
  SAVEVALUE: solarFlow
}

The result is accumulated into batteryLevel with a maximum cap of 100. A PLOTTER records both battery level and solar flow every cycle, producing a full-day evolution curve in the report.

Discrete part: users requesting charge

Users arrive using GENERATE. Each one decides whether to charge using a Poisson-based decision function:

Function {Name:chargeDecision, TYPE:POISSON, LAMBDA:1/4}

If they decide to charge, they move to the charger resource (RESTROOM), wait there, and are released automatically via WAKE once they reach the required charge level (requiredCharge).

How both worlds are coordinated

Every time solarCycle runs:

  • Solar flow is updated (continuous)
  • The system battery is recharged with that flow
  • All users currently connected to the charger are iterated (FOREACH ... IN_RESOURCE) and receive energy if available
  • Fully charged users are released using WAKE

Visually, the model shows a battery tank level, an arc representing current solar intensity, and a plot of the recorded evolution.

Conclusion

This is a continuous/discrete hybrid model: a continuous environmental evolution (solar generation) combined with discrete arrivals, decisions, and resource occupancy (users charging). This structure is typical in energy systems, shared infrastructure, and any scenario with intermittent production and variable demand.

Example

/*

 Hybrid simulation
 
*/
SYSTEM {type:OPTIONS, TIME_DECIMALS:0, SPEED:5}                 ; Integer time units (no decimals) and slow visual simulation
SYSTEM {TYPE:ON_TIMER, TRIGGER:solarCycle, INTERVAL: 1}         ; Run procedure 'solarCycle' every 1 time unit

;--- Result plots ---
PLOTTER {NAME:batteryPlot, Y_0:batteryLevel, Y_1:solarFlow, X:TIME_AC1}      ; Curves: battery level and solar flow

;--- Random user decision function ---
Function {Name:chargeDecision, TYPE:POISSON, LAMBDA:1/4}                    ; Probability of deciding to charge

;--- Graphic positions ---
Position {NAME:PosIn, X:431, Y:511}                                         ; Entry to the charging area
Position {NAME:PosOut, X:690, Y:510}                                        ; Exit
Position {NAME:PosCharge, X:578, Y:173}                                     ; Charging point (visual)

;--- User charging resource ---
Restroom {NAME:Charger, X:555, Y:360, R_BIN_SIZE:1, R_BIN_COUNT:40}         ; Allows 40 concurrent charging "slots"

;--- Visual indicators ---
Graphic {NAME:txtBattery, Type:TEXT, X:329, Y:305, Text:"Battery"}          ; Text: battery level
Graphic {NAME:gSolarArc, TYPE:ARC, X:411, Y:173,
  fCOLOR:#FFFF99, RADIUS:50, START_ANGLE:0, END_ANGLE:45, CLOSE:1}          ; Arc: solar intensity
Graphic {NAME:txtSolar, Type:TEXT, X:411, Y:173, Text:"Solar"}              ; Text: solar flow
Graphic {NAME:Box1, Type:LINE,
  POINTS:"[100,100],[100,500],[200,500],[200,100]",
  Close:1, fcolor:#666666}                                                  ; Battery tank
Graphic {NAME:LevelLine, Type:LINE, color:#00FFFF, X1:98, Y1:100, X2:202, Y2:100} ; Battery level line

;--- Initial variables ---
INITIAL batteryLevel, 50
INITIAL solarFlow, 0
INITIAL requiredCharge, 15

START 2000

;==============================
; SOLAR CYCLE PROCEDURE
;==============================
PROCEDURE solarCycle
    ; Solar flow: normalized sinusoid between 0 and 1.5 over a 24h cycle (1440 minutes)
    INTEGRATE { EXPRESSION: "0.75 + 0.75 * SIN((T / 1440) * 6.2832)", DT: 1, SAVEVALUE: solarFlow }

    ; Increase battery (cap at 100)
    SAVEVALUE batteryLevel, MIN(100, X$batteryLevel + X$solarFlow)

    ; Iterate entities currently inside the Charger resource
    FOREACH NUMBER, IN_RESOURCE, Charger
        if (X$batteryLevel > 1)
            ; Increase user's internal battery
            assign current, P$(battery, P$NUMBER) + 0.5
            assign battery, P$current, P$NUMBER

            ; Decrease system battery
            savevalue batteryLevel, X$batteryLevel - 0.5

            ; If user has enough charge, release them
            if (P$current > X$requiredCharge)
                wake Charger, 0, P$NUMBER
            endif
        endif
    ENDFOREACH

    ; Solar arc: proportional to current flow
    ASSIGN sunAngle, round((X$solarFlow / 1.5) * 360, 1)
    MOVE {NAME:gSolarArc, END_ANGLE:P$sunAngle}

    ; Update battery level line
    MOVE {NAME:LevelLine, X1:98, Y1:(100 + X$batteryLevel * 4), X2:202, Y2:(100 + X$batteryLevel * 4)}

    ; Show rounded values
    assign tBatteryRound, round(X$batteryLevel, 2)
    assign tSolarRound, round(X$solarFlow, 2)
    move {name:txtBattery, text:"Current level: P$tBatteryRound"}
    move {name:txtSolar, text:"Solar flow: P$tSolarRound / 1.5"}

    ; Store data for report
    PLOT batteryPlot, X$batteryLevel, X$solarFlow
TERMINATE
ENDPROCEDURE 1

;==============================
; USERS
;==============================
GENERATE 5,5 {NAME:userGen, X:294, Y:514, ECOLOR:#000000}                   ; One user every ~5 time units

assign battery, X$requiredCharge                                             ; Required energy for this user

ASSIGN decision, FN$chargeDecision                                            ; Decide whether they need to charge (Poisson)
ADVANCE 10 {to:PosIn}                                                        ; Connection / approach time

IF (P$decision >= 1)
    assign battery, 0                                                        ; Start charging counter
    ADVANCE 10 {to:Charger}                                                   ; Move to charger
    rest Charger                                                             ; Wait until fully charged (released by WAKE)
ENDIF

ADVANCE 10 {to:PosOut}                                                       ; Exit
TERMINATE 1