Duplicated Components

In this tutorial, we will introduce DuplicatedComponents.

The example system we will use for this will be a model of a forest fire (governed by an Agent-based model), with the growth of each tree informed by an ODE model.

Components

To begin, we need to define our components. These will be an ODE model component for each tree, and an Agent-based model component for handling the forest level properties (in this case, the spread of heat/fire).

ODE Component

To define a DuplicatedComponent, we first need to define the component we want to duplicate. In this case, it is a DEComponent.

using OrdinaryDiffEq
using Sirens

function tree!(du, u, p, t)
    heat, life = u
    du[1] = 0
    du[2] = (life*(1-life/10.0)-heat*life)/10
end
u0 = [4.0, 2.0]
tspan = (0.0, 150.0)
prob = ODEProblem(tree!, u0, tspan)

comp1 = DEComponent(prob, Rodas5P();
    name="tree", state_names=Dict("heat" => 1, "life" => 2),
)

Duplicated Component

Since the ODEProblem is defined for only a single tree, we can efficiently simulate the same ODE system many times by generating a duplicated component. This component stores a single ODEProblem that it will solve across many different states. In this case, we can have a state for each tree in the Agent-based model. Let's have a look at how to define a DuplicatedComponent.

Sirens.DuplicatedComponentType
DuplicatedComponent <: AbstractComponent

Represents a component that is duplicated in the simulation, allowing a single component to have multiple states.

Fields

  • component::AbstractTimeDependentComponent: The original component to be duplicated.
  • instances::Union{Int,Nothing}: Number of instances of the component. If nothing, then the number is variable and determined by the simulation.
  • name::String: Name of the duplicated component.
  • init_states::Vector: Vector of states for the duplicated component, where each state corresponds to a particular instance.
source
dup_comp = DuplicatedComponent(comp1, [copy(u0) for _ in 1:640];
    instances=640,
)

Agents.jl Component

Now that we have created our DEComponent, we can move on to the AgentsComponent, so let's have a look at its documentation.

using Agents, Random, Statistics
@agent struct Tree(GridAgent{2})
    heat::Float64 # Heat is averaged across neighbors, passed to ODE model
    life::Float64 # Life is informed by ODE model
end

function forest_fire(; density=0.4, griddims=(40, 40), seed=2)
    space = GridSpaceSingle(griddims; periodic=false, metric=:chebyshev)
    rng = Random.MersenneTwister(seed)
    forest = StandardABM(Tree, space; rng, agent_step! = tree_step!)
    for _ in 1:floor(density * prod(griddims))
        # Randomly place trees in the grid
        add_agent_single!(forest; heat=rand(), life=rand())
    end
    return forest
end

function tree_step!(tree, forest)
    nearbyheat = mean([getproperty(neighbor, :heat) for neighbor in nearby_agents(tree, forest, 1)])
    if isnan(nearbyheat)
        nearbyheat = 0.0
    end
    tree.heat = tree.heat * 0.9 + nearbyheat * 0.1
    if rand(abmrng(forest)) < 1e-4 # Random chance of fire
        tree.heat = 10.0
    end
    # Simulate tree life cycle
    if tree.heat > 1.0 && tree.life > 1.0
        # Tree on fire
        tree.heat += 1.0
    else
        # Tree not on fire, so heat dissipates
        tree.heat -= 0.05
    end
    if tree.heat < 0.0
        tree.heat = 0.0
    end
end

forest = forest_fire()

comp2 = AgentsComponent(forest;
    name="forest", state_names=Dict("heat" => :heat, "life" => :life)
)
AgentsComponent{StandardABM{GridSpaceSingle{2, false}, Main.Tree, Dict{Int64, Main.Tree}, Tuple{DataType}, typeof(Main.tree_step!), typeof(dummystep), typeof(Agents.Schedulers.fastest), Nothing, Random.MersenneTwister}, String, Dict{String, Symbol}, Int64}(StandardABM with 640 agents of type Tree
 agents container: Dict
 space: GridSpaceSingle with size (40, 40), metric=chebyshev, periodic=false
 scheduler: fastest, "forest", Dict("heat" => :heat, "life" => :life), 1)

Connections

We can now set up the connections between the variables in the two components.

Sirens.ConnectorType
Connector <: AbstractConnector

Represents a connection between multiple ConnectedVariables, possibly with a transformation function.

Fields

  • inputs::Tuple{<:AbstractConnectedVariable}: Input variables for the connector.
  • outputs::Tuple{<:AbstractConnectedVariable}: Output variables for the connector.
  • func::Union{Nothing,Function}: Optional function to transform inputs to outputs.
source

The format for specifying a ConnectedVariable is given in Sirens Interface, but in short, it is a string containing a component name and a variable/state name. It can also contain optional indices for only accessing part of a variable (given at the end of the ConnectedVariable), or for only accessing some subcomponents, such as for DuplicatedComponents (given at the end of the component name).

conn1 = Connector(inputs=["forest.heat[1:640]"], outputs=["tree[1:640].heat"])
conn2 = Connector(inputs=["tree[1:640].life"], outputs=["forest.life[1:640]"])
Connector{Tuple{ConnectedVariable{SubString{String}, SubString{String}, Nothing, Vector{Int64}}}, Tuple{ConnectedVariable{SubString{String}, SubString{String}, Vector{Int64}, Nothing}}}((ConnectedVariable{SubString{String}, SubString{String}, Nothing, Vector{Int64}}("tree", "life", nothing, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10  …  631, 632, 633, 634, 635, 636, 637, 638, 639, 640]),), (ConnectedVariable{SubString{String}, SubString{String}, Vector{Int64}, Nothing}("forest", "life", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10  …  631, 632, 633, 634, 635, 636, 637, 638, 639, 640], nothing),), nothing)

Solving the hybrid model

To create the hybrid model, we need to create a SirenProblem. We can then solve this using the CommonSolve interface.

Sirens.SirenProblemType
SirenProblem <: AbstractSirenProblem
SirenProblem(components, connectors, tspan, timescales=ones(length(components)))
SirenProblem(;
    components::Union{Tuple, Vector},
    connectors::Union{Tuple, Vector},
    tspan::Tuple{Float64, Float64},
    timescales::Vector{Float64}=ones(length(components)))

Defines a Sirens hybrid simulation problem.

Arguments

  • components::Union{Tuple, Vector}: Tuple or Vector of Components. Order is significant because it determines stepping order when multiple components can be stepped together. Component names must be unique. Using a tuple preserves type information for each element.
  • connectors::Union{Tuple, Vector}: Tuple or Vector of Connectors. Order is significant; connectors are applied in order, and later connectors can observe changes made by earlier ones. Using a tuple preserves type information for each element.
  • tspan::Tuple{Float64, Float64}: The time span of the simulation, from start to end time.
  • timescales::Vector{Float64}=ones(length(components)): Timescales for each component. For component i, global time is computed as t_global[i] = timescales[i] * t_local[i]. A component with timescale 0.1 advances ten local time units per one global time unit. This allows components using different time units to be connected in a single simulation.

Notes on Ordering

  • components order determines stepping priority when multiple components are ready.
  • connectors order is critical. Connectors are applied before component steps, in the order given. A connector is eligible only when every input is no later than every output in global time. Later connectors see changes made by earlier connectors. This is particularly important for setting #ids and #init_states in a DuplicatedComponent.
source
sp = SirenProblem(components=[dup_comp, comp2], connectors=[conn1, conn2], tspan=tspan)
alg = MinimumTimeStepper()
sol = solve(sp, alg)
SirenSolution{Vector{Float64}, Sirens.SirenSolutionData{NTuple{4, ConnectedVariable{String, String, Nothing, Nothing}}, Tuple{Vector{Vector{Any}}, Vector{Vector{Any}}, Vector{Vector{Float64}}, Vector{Vector{Float64}}}}}([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0  …  141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 147.0, 148.0, 149.0, 150.0], Sirens.SirenSolutionData{NTuple{4, ConnectedVariable{String, String, Nothing, Nothing}}, Tuple{Vector{Vector{Any}}, Vector{Vector{Any}}, Vector{Vector{Float64}}, Vector{Vector{Float64}}}}((ConnectedVariable{String, String, Nothing, Nothing}("tree", "heat", nothing, nothing), Vector{Any}[[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0  …  4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0], [0.020814790823794893, 0.3205422419116597, 0.22069394295718958, 0.13795604364953706, 0.8924583331874884, 0.10054268354416929, 0.6693335951573127, 0.3204170037413884, 0.9584218729578631, 0.5885062927096877  …  0.13354684131451589, 0.7715936650405604, 0.11575452549465537, 0.34876761824413816, 0.9789301546317334, 0.8164332356828596, 0.780892890214564, 0.7062540398762451, 0.45844028093051814, 0.8883833532499802], [0.0015074839549006139, 0.27254942222088485, 0.1740285400050724, 0.1528465435835228, 0.8040500657382794, 0.07930310194199279, 0.5672458566844779, 0.28543933602881794, 0.8630921744823187, 0.5262834067020185  …  0.12469019885319939, 0.6840917106596659, 0.06146145151112642, 0.33618583104269645, 0.8794046508457208, 0.756875072415106, 0.6661632809440137, 0.6298953579948442, 0.3970569034856915, 0.8128911512018042], [0.0, 0.2254836034291151, 0.12557172936016536, 0.15633122451178816, 0.7195959761610159, 0.05342128359693209, 0.4738107560960369, 0.247279481928655, 0.7731881100316533, 0.46573111305591414  …  0.10889535363488421, 0.6013020821834539, 0.010224661592895715, 0.3181260479459768, 0.7858159850832774, 0.6982178830033509, 0.5636783949437815, 0.5557543916743359, 0.33731412437958697, 0.7394237593989619], [0.0, 0.17905070403520695, 0.07784079128162986, 0.1504348544472464, 0.6385732563415086, 0.023671169451019228, 0.3875837405723017, 0.20655914571784045, 0.6879810518069265, 0.4065903800549276  …  0.0872975908163162, 0.5225843157316343, 0.0, 0.29536467707970454, 0.6973797429943874, 0.6403007118700851, 0.47119211743653594, 0.4836201270466762, 0.2789445318718908, 0.6677516690808466], [0.0, 0.13310649142980335, 0.03199312616280525, 0.13681172924017454, 0.5605417996872157, 0.0, 0.3073590016346387, 0.16378310607661994, 0.6068430660472116, 0.34865435131366446  …  0.06084602777418939, 0.4473859283697051, 0.0, 0.2685646211437786, 0.6134168308163459, 0.5829903620110343, 0.3868556289398816, 0.41330240012726904, 0.22172246760050857, 0.5976844058341343], [0.0, 0.08811105305320026, 0.0, 0.11681463891313258, 0.4851296564162996, 0.0, 0.23213010679858187, 0.11936036388126302, 0.5292327476803653, 0.29174784684239563  …  0.030334113955299627, 0.37523021988100574, 0.0, 0.23829233853310006, 0.5333392078896648, 0.5261768347253005, 0.3091487389109289, 0.3446362019793963, 0.16545760477852195, 0.5290635217643708], [0.0, 0.04462028232054589, 0.0, 0.09155087272984623, 0.41202260564254384, 0.0, 0.16123586409469076, 0.07362157840416116, 0.45468307625457366, 0.2357229726529672  …  0.0, 0.3057057829833174, 0.0, 0.20503222655226117, 0.45664486012560074, 0.4697694939869947, 0.23682320551995611, 0.2775026524108895, 0.10998950961411656, 0.46176834772472736], [0.0, 0.0024364626348087753, 0.0, 0.061927711404840144, 0.34097227182644707, 0.0, 0.09561656204400047, 0.026833433889858566, 0.38279153812586586, 0.1809936385843996  …  0.0, 0.2384575014648052, 0.0, 0.1691988401529385, 0.3829107689299933, 0.41369384289843963, 0.16885602794540433, 0.2118386646856497, 0.056148116621898614, 0.3957085801247422], [0.0, 0.0, 0.0, 0.028689391086695507, 0.27193905463994617, 0.0, 0.03605490583960043, 0.0, 0.3132113834504928, 0.12763545074572685  …  0.0, 0.173912745553441, 0.0, 0.1311472719819593, 0.31177201975691377, 0.35788881685642665, 0.10444896396403762, 0.14760803351311141, 0.004162305533128477, 0.33163444216528]  …  [0.16400026217519342, 0.0, 0.0, 0.0, 4.194240970266494, 4.150664342307108, 2.8938872228996724, 0.0, 0.0, 0.0  …  5.115042578169678, 0.0, 0.0, 0.0, 0.0, 1.4014063482425354, 0.0, 0.0, 0.0, 0.0], [0.14360633338725404, 0.0, 0.0, 0.0, 5.104301627316801, 4.097135988360083, 2.8266499987795415, 0.0, 0.0, 0.0  …  5.037225725859947, 0.0, 0.0, 0.0, 0.0, 1.342496956389519, 0.0, 0.0, 0.0, 0.0], [0.12556230775087662, 0.0, 0.0, 0.0, 5.94546266401363, 4.043611283004962, 2.761282705314569, 0.0, 0.0, 0.0  …  4.960064064507046, 0.0, 0.0, 0.0, 0.0, 1.284140991142033, 0.0, 0.0, 0.0, 0.0], [0.11010449171718024, 0.0, 0.0, 0.0, 6.725777630214801, 3.990089291056859, 2.6976478244995175, 0.0, 0.0, 0.0  …  4.883534669317904, 0.0, 0.0, 0.0, 0.0, 1.2263326664266199, 0.0, 0.0, 0.0, 0.0], [0.09737735309997202, 0.0, 0.0, 0.0, 7.452304422279733, 3.9365689660378584, 2.6356208788684796, 0.0, 0.0, 0.0  …  4.807615728256618, 0.0, 0.0, 0.0, 0.0, 1.1690656580434708, 0.0, 0.0, 0.0, 0.003093472778653239], [0.08745248175571772, 0.0, 0.0, 0.0, 7.0547807621758, 3.8830492197871855, 2.575088988466701, 0.0, 0.0, 0.0  …  4.7322865204201765, 0.0, 0.0, 0.0, 0.0, 1.1123332196114415, 0.0, 0.0, 0.0, 0.03388426608461419], [0.07960266289777725, 0.0, 0.0, 0.0, 6.695241459367954, 3.8295289835546957, 2.5159496046705563, 0.0, 0.0, 0.0  …  4.657527389945749, 0.0, 0.0, 0.0, 0.0, 1.0561282806097876, 0.0, 0.0, 0.0, 0.08789600832544166], [0.0949358457124705, 0.0, 0.0, 0.0, 6.422964864931978, 3.7760072609118165, 2.458109398007511, 0.0, 0.0, 0.0  …  4.583319716005155, 0.0, 0.0, 0.0, 0.0, 1.0004435291043705, 0.0, 0.0, 0.0, 0.16128891374174414], [0.12937125756638285, 0.0, 0.0, 0.0, 6.225574088073212, 3.7224831707472203, 2.4014832801328385, 0.0, 0.0, 0.0  …  4.509645879503848, 0.0, 0.0, 0.0, 0.0, 0.9452714813810397, 0.0, 0.0, 0.0, 0.2507708356405692], [0.17945501962980187, 0.0, 0.0, 0.0, 6.093110342642303, 3.6689560399117305, 2.3459935427314313, 0.0, 0.0, 0.0  …  4.436489227118252, 0.0, 0.0, 0.0, 0.0, 0.8906045403946548, 0.0, 0.0, 0.0, 0.35352080489586735]]), (ConnectedVariable{String, String, Nothing, Nothing}("tree", "life", nothing, nothing), Vector{Any}[[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0  …  2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [2.1603526332203904, 2.097212494165046, 2.1180401443316974, 2.1354542087706365, 1.981775011496152, 2.1433753062216683, 2.026040633732896, 2.0972384901461854, 1.9688734913233052, 2.0423170894477454  …  2.1363862050338884, 2.005632695373012, 2.140151165522213, 2.0913618421344053, 1.9648793948777632, 1.9967486293964585, 2.0037870182190187, 2.0186487757055667, 2.068781971734814, 1.982574767603608], [2.3341692821310023, 2.207440283329771, 2.250717070098712, 2.27357671255104, 1.9813379512951486, 2.2984808419715606, 2.0727269904649965, 2.2046540956562435, 1.9572142082110886, 2.0975242463312105  …  2.2808895838991137, 2.028666608597139, 2.2991519968584293, 2.1875976201988903, 1.9501679500305247, 2.005356850200829, 2.0304375139386277, 2.0525496095679654, 2.151476347066443, 1.9803874346131127], [2.517846224087386, 2.331689038929486, 2.399939428534257, 2.4163925032741966, 1.9975435203766774, 2.4672013974548666, 2.139185333547934, 2.3237990952734306, 1.9632493417952148, 2.1659688298619377  …  2.435373385596737, 2.068375013653261, 2.4784602051493376, 2.2901212545474143, 1.9538731602001436, 2.025558549055502, 2.0778672999521834, 2.1016784157151274, 2.248859046563239, 1.9926882471814487], [2.7108655628843943, 2.471135301970838, 2.567235089649495, 2.565940189050551, 2.029773406948398, 2.6515142982717, 2.2251983116849936, 2.456286187672417, 1.9858699303630365, 2.248220056785387  …  2.6017656312716304, 2.124508968178156, 2.669537458576475, 2.400358411083132, 1.9747315868461142, 2.0573133446902445, 2.14493532772609, 2.1663304445069773, 2.361925254595945, 2.0190969570119344], [2.9129211692872543, 2.6270997953198374, 2.7539358171703525, 2.7242609698520295, 2.0778464573274658, 2.8508763270307584, 2.3310780661459907, 2.6038101049838867, 2.0244962563260223, 2.345054306316476  …  2.7820625639867282, 2.197222758253758, 2.8697254100003984, 2.519759337230204, 2.0120554825380825, 2.1007856694253295, 2.2312107393009315, 2.2470950821683924, 2.491883755865253, 2.0595523526138253], [3.12358335030463, 2.8008832771755503, 2.957898974744918, 2.893399055076685, 2.141958306664422, 3.05898818318333, 2.457581141017999, 2.7681648475065064, 2.0789864542744385, 2.457447966974004  …  2.978332659526112, 2.2870236273642406, 3.0786207029049937, 2.6498126907410033, 2.065628932311911, 2.156331410971791, 2.3368208598139226, 2.344838703489967, 2.640153285464936, 2.1142780248486934], [3.342295678812361, 2.993687727865868, 3.1703588315662294, 3.0754079490229205, 2.222641308692744, 3.2753319137050143, 2.605803793917825, 2.9512556942629056, 2.1495748951384335, 2.5865728997452764  …  3.1915950362180365, 2.394738140346334, 3.2956936477999967, 2.792059083439531, 2.1356363411513084, 2.2244903256424524, 2.4623500214428775, 2.4606881653036146, 2.8083589380103158, 2.183757485612464], [3.568375202871041, 3.206778052286635, 3.3907313754276305, 3.2723583564632843, 2.3207347388649535, 3.4992625951771665, 2.7767247961600408, 3.1551049241626505, 2.23683118275145, 2.7336476313906317  …  3.4127061044216727, 2.521489633547194, 3.520287684674029, 2.94810322931057, 2.222613250404705, 2.3059828327488843, 2.6087702386494755, 2.596013153942997, 2.9980414736929437, 2.2687179703491616], [3.8010164197956673, 3.4284113137558543, 3.6183066010731464, 3.4863444479230155, 2.4373272970311417, 3.7300111373641087, 2.971104502304764, 3.374941124998, 2.341633474351926, 2.89999602657224  …  3.640943719155482, 2.6684891350819124, 3.7516225412625994, 3.1196236682006133, 2.3274152936675923, 2.4017100348893186, 2.777380735464794, 2.752413243097515, 3.2107535974903403, 2.369928883377522]  …  [6.781439503875134, 9.999964527749281, 9.999967360613647, 9.999965424710371, 3.4122707577109948, 1.6762797491333528e-9, 1.5908947443509126e-5, 9.999963672559868, 9.999934417222203, 1.071067543153515  …  1.7553345893923212e-9, 9.99994797446724, 9.99996917808533, 9.99995827766524, 9.999933969554183, 2.6049580463979364e-11, 9.999951577012705, 9.999950493083068, 9.999960868569342, 9.999933941210667], [6.899300543380511, 9.999967903369411, 9.999970466652755, 9.999968714973912, 2.2019540070564507, 1.2298144499624295e-9, 1.3252901876757167e-5, 9.999967129561504, 9.999940658211633, 1.1705272765895465  …  1.1722640049475818e-9, 9.99995292532796, 9.999972111170132, 9.99996224805535, 9.999940253144352, 2.5172495936989286e-11, 9.999956185049015, 9.999955204268005, 9.999964592404135, 9.999940227498046], [7.023383146576621, 9.999970957758777, 9.99997327711482, 9.999971692129341, 1.3199283447422936, 9.071043156293587e-10, 1.1112694972491149e-5, 9.999970257587998, 9.999946305299114, 1.2779010486778983  …  7.889364778177553e-10, 9.999957405056216, 9.999974765136493, 9.999965840615603, 9.99994593877866, 2.446730822371851e-11, 9.99996035457635, 9.999959467128242, 9.999967961871581, 9.999945915572898], [7.151723701283716, 9.999973721486175, 9.999975820127421, 9.999974385972504, 0.7371247069823086, 6.726657343990711e-10, 9.377594323313633e-6, 9.9999730879451, 9.999951415000663, 1.3935697717289186  …  5.35035098374459e-10, 9.999961458485425, 9.999977166545776, 9.99996909130077, 9.999951083358903, 2.391975296893738e-11, 9.99996412732369, 9.999963324326826, 9.999971010693764, 9.999951062361438], [7.282250265390559, 9.999976222211453, 9.999978121141492, 9.999976823463847, 0.38456427229182394, 5.014939933138896e-10, 7.962643852835072e-6, 9.999975648959493, 9.99995603845432, 1.5178861859935044  …  3.65611313313326e-10, 9.999965126182675, 9.999979339431746, 9.999972032644166, 9.999955738372167, 2.3518751422398694e-11, 9.99996754104911, 9.999966814466996, 9.999973769383757, 9.99701232200612], [7.412836597446561, 9.999978484962336, 9.999980203186036, 9.999979028998245, 0.20929670235510528, 3.7588630155784196e-10, 6.802240985126469e-6, 9.999977966262275, 9.999960221931872, 1.6511648879320777  …  2.517262375187957e-10, 9.999968444854703, 9.999981305541091, 9.999974694083223, 9.999959950406087, 2.3256036061270048e-11, 9.99997062991761, 9.999969972478516, 9.999976265550998, 9.965109886540763], [7.54189199398193, 9.999980532384884, 9.999982087098592, 9.999981024649133, 0.11823032231439541, 2.832511077795829e-10, 5.845412063839499e-6, 9.999980063045466, 9.999964007301916, 1.7936714964087348  …  1.7461602222838827e-10, 9.999971447715229, 9.999983084551058, 9.999977102254089, 9.999963761615039, 2.3125869572262387e-11, 9.999973424843056, 9.999972829967225, 9.999978524177596, 9.885362675580586], [7.652275130731491, 9.99998238497014, 9.99998379173378, 9.999982830389413, 0.06867780631438561, 2.1459082722215414e-10, 5.052312689515522e-6, 9.999981960294116, 9.999967432448843, 1.9456111599285864  …  1.220288433659882e-10, 9.999974164817347, 9.999984694266391, 9.999979281258195, 9.99996721014201, 2.3124843895381167e-11, 9.999975953797525, 9.999975415531342, 9.999980567868334, 9.745308739229634], [7.730307381259976, 9.999984061259195, 9.999985334151978, 9.999984464291348, 0.04070431167344028, 1.6344634468259045e-10, 4.391617989133125e-6, 9.999983676996305, 9.999970531651966, 2.107116688042338  …  8.590936577094078e-11, 9.99997662335429, 9.999986150797502, 9.999981252903464, 9.9999703305003, 2.3251749691899213e-11, 9.99997824209126, 9.99997775504765, 9.999982417076906, 9.538263113601417], [7.765732405097235, 9.999985578028733, 9.999986729790093, 9.999985942707417, 0.024451807913336645, 1.2515953031380372e-10, 3.838563953737327e-6, 9.999985230333188, 9.999973335928576, 2.2782366732837627  …  6.092502044860978e-11, 9.999978847931546, 9.999987468721717, 9.999983036922549, 9.999973153918916, 2.3507509674715094e-11, 9.999980312625958, 9.999979871930492, 9.999984090310603, 9.26258710920829]]), (ConnectedVariable{String, String, Nothing, Nothing}("forest", "heat", nothing, nothing), [[0.4458295485360022, 0.39071674350014884, 0.59269355085044, 0.4371585136559232, 0.3125476061840221, 0.7062540398762451, 0.7062188430177962, 0.10998025488865293, 0.7753478383645597, 0.4211887211771358  …  0.7552307493309283, 0.4361937766981161, 0.4829064844738784, 0.48221206783139825, 0.42704766603473876, 0.7702913783226624, 0.9324830423598301, 0.12078024457873726, 0.3251816029662724, 0.7016587198178067], [0.4152540263438119, 0.35578533278593405, 0.5349565011799253, 0.35522791048582064, 0.27733019010913784, 0.6298953579948442, 0.604877592302262, 0.08066455740565733, 0.6478130545281037, 0.3722009010086254  …  0.6784951848136404, 0.3492937801396808, 0.447236361789149, 0.4346024913502448, 0.3921156398040628, 0.7075127782681526, 0.8555945125721862, 0.10281858503718617, 0.3048978872072698, 0.6446294747811058], [0.3804406383690707, 0.3179660836434899, 0.4774131858011222, 0.27962104760917256, 0.23866914999106686, 0.5557543916743359, 0.510320392813511, 0.04915418408537031, 0.5330317490752933, 0.32272937768073245  …  0.604453108156167, 0.2711079553243019, 0.40944552853984334, 0.38545326394449503, 0.353978887805324, 0.6458860484707002, 0.7797638486473493, 0.08161273856751648, 0.2808584481405198, 0.5876220114425207], [0.34212854478873744, 0.27775310064583325, 0.4201083082782723, 0.20913890829363474, 0.19713697006269754, 0.4836201270466762, 0.4215053534593715, 0.015655064776490785, 0.42972857416776394, 0.27283649152092315  …  0.5327709874611373, 0.2000342191322525, 0.3697860996670026, 0.33506386407308203, 0.3131924581311973, 0.5852872037585435, 0.7049827686068295, 0.05747616573186236, 0.25352226334777367, 0.5306715760186127], [0.30092905573397605, 0.23555628389590022, 0.3630707153934685, 0.14282201491596158, 0.15321468029814717, 0.41330240012726904, 0.33756383459109224, 0.0, 0.3367557167509876, 0.2225770760176734  …  0.46315930781744, 0.13474034749481362, 0.328479628871562, 0.2836807558232848, 0.27021797420267074, 0.5256037747402216, 0.6312404094921599, 0.030693287477160713, 0.22328957177582298, 0.4738066420522781], [0.2573483110807356, 0.19171561245290414, 0.3063169270624452, 0.08093879663992574, 0.10730746201093326, 0.3446362019793963, 0.2578833851348306, 0.0, 0.25308014507588883, 0.17200332875022672  …  0.3953666811358107, 0.07411976059658305, 0.28572090199384453, 0.2315082137067529, 0.22543907528947332, 0.46673414347830305, 0.55852375963099, 0.0022378258222884306, 0.19050972010041523, 0.4170534971701155], [0.21180586386903621, 0.14651301414396312, 0.24985407308976376, 0.023967240415538382, 0.059844759532533987, 0.2775026524108895, 0.18209504662134757, 0.0, 0.17777213056829994, 0.12118818977087124  …  0.32917557275620135, 0.017254509965665105, 0.24169218798992892, 0.17871742198026636, 0.17917433525010018, 0.40858681150506626, 0.4868711896451143, 0.0, 0.15548792495336922, 0.36045595794613366], [0.16467168847683444, 0.10018222836152811, 0.1936824945944587, 0.0, 0.011256094808369886, 0.2118386646856497, 0.11388554195921281, 0.0, 0.10999491751146993, 0.07024810736186217  …  0.2648099419557865, 0.0, 0.19657136915551554, 0.12547305773658812, 0.131688068150992, 0.3510796378152703, 0.41632459526332166, 0.0, 0.11849110193049593, 0.3040494490005527], [0.11647269041043971, 0.052917000119836244, 0.13817491265473764, 0.0, 0.0, 0.14760803351311141, 0.052496987763291536, 0.0, 0.04899542576032294, 0.02056460244360829  …  0.2026920938474806, 0.0, 0.15051668224887238, 0.07200169896218889, 0.08366724904399174, 0.29413907415867996, 0.3469199672117051, 0.0, 0.07975349086569249, 0.2478625171447399], [0.06866515965942248, 0.004877886775259618, 0.08409327435371937, 0.0, 0.0, 0.08546362608296275, 0.0, 0.0, 0.0, 0.0  …  0.14312746130551413, 0.0, 0.10366894805653122, 0.019728949550559344, 0.03553587395250775, 0.23769941700066893, 0.2786886716355357, 0.0, 0.03948122624550805, 0.19191837471431106]  …  [7.845237343488007, 0.0, 0.0, 4.1778510247134575, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 3.4038568262688127, 0.0, 2.4070061530291453, 0.0, 0.0, 0.0, 2.4308068537774083], [7.6632589955661485, 0.0, 0.0, 4.10980296874686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 3.310941807520402, 0.0, 2.339420687176112, 0.0, 0.0, 0.0, 2.3623586098335068], [7.54680300758811, 0.0, 0.0, 4.04211559139785, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 3.2194451262257986, 0.0, 2.2720716505034724, 0.0, 0.0, 0.0, 2.2941953923310696], [7.487976927017802, 0.0, 0.0, 3.974790452208919, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 3.1293380273960585, 0.0, 2.2049626585241695, 0.0, 0.0, 0.0, 2.226320993008937], [7.4456264476419065, 0.0, 0.0, 3.9078291888694396, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 3.040591460180753, 0.0, 2.138097062853711, 0.0, 0.0, 0.0, 2.1587387840328964], [7.382457763179176, 0.0, 0.0, 3.841233487583423, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 2.953176272545101, 0.0, 2.0714779566078483, 0.0, 0.0, 0.0, 2.091451738189744], [7.303349630063357, 0.0, 0.0, 3.775005061006129, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 2.8670633674273507, 0.0, 2.0051081818243173, 0.0, 0.0, 0.0, 2.0244624502516535], [7.214135861425428, 0.0, 0.0, 3.7091455829059226, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 2.782223827274727, 0.0, 1.9389903383589417, 0.0, 0.0, 0.0, 1.9577731589699365], [7.120405748049786, 0.0, 0.0, 3.643657958009828, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 2.6986290126612724, 0.0, 1.873126793811757, 0.0, 0.0, 0.0, 1.8913857692674347], [7.026578956407458, 0.0, 0.0, 3.578546962810849, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 2.6162506396994867, 0.0, 1.8075196941259548, 0.0, 0.0, 0.0, 1.8253018742903802]]), (ConnectedVariable{String, String, Nothing, Nothing}("forest", "life", nothing, nothing), [[0.8440207030407509, 0.26293186405396063, 0.8320852752822149, 0.28354394301956887, 0.7200327831238205, 0.9973949045055581, 0.05067472445275967, 0.38056659918279334, 0.17148845110518496, 0.6955835960364704  …  0.005514770963778615, 0.030378640682133584, 0.8841782487006502, 0.8375243754164284, 0.6435841419434323, 0.219739614098431, 0.44079413408748835, 0.4648965821939499, 0.6675478926234618, 0.6210738647419471], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0  …  2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [2.0713659532375166, 2.0826964190680726, 2.041470706894109, 2.0731445369355086, 2.0988726014337598, 2.0186487757055667, 2.0186558097117167, 2.1413744512250226, 2.0048873772280063, 2.0764241958098673  …  2.0088844495791727, 2.073342515407766, 2.063777891522109, 2.06391975487674, 2.0752203673591882, 2.0058913033784664, 1.9739367696408396, 2.139087026124443, 2.0962497165556435, 2.019567345128159], [2.150232432679152, 2.1745038996186183, 2.094873854128528, 2.1648594338116545, 2.208105903039789, 2.0525496095679654, 2.0576458560007977, 2.2960731274845405, 2.0352240167567337, 2.1645742520849276  …  2.0330159517456936, 2.1663333685648465, 2.135749744438942, 2.138565328206264, 2.1590871932056332, 2.024224124005198, 1.9636067382090978, 2.288654777355032, 2.199399017694634, 2.050472030762137], [2.2380208729904854, 2.2767515755188565, 2.160790723169823, 2.2754871084844703, 2.3293379006003487, 2.1016784157151274, 2.1162829218763854, 2.465717079370136, 2.0889924780411664, 2.2655153861389175  …  2.072073043705887, 2.27892001322649, 2.216902583909352, 2.2250361158458865, 2.252924591742381, 2.0548490097879197, 1.9682546702239934, 2.4500668487253203, 2.3106995148838254, 2.0929834581028848], [2.3361629128797388, 2.390825994321052, 2.239965670382156, 2.405787716831283, 2.4642951922563836, 2.1663304445069773, 2.1945075800153457, 2.6520571742369734, 2.165042020095311, 2.3804541644913733  …  2.126084244084694, 2.4115019692671673, 2.3082917777013643, 2.3245364420290118, 2.358101375949482, 2.0978440676689947, 1.9874850770451236, 2.62478702244435, 2.431455472212875, 2.1475356866641735], [2.4461306589073897, 2.518191781550554, 2.333311446169667, 2.5568765219758554, 2.6148142860394894, 2.2470950821683924, 2.292783531998975, 2.85144418217337, 2.2628532548086935, 2.510747976374103  …  2.195388914595035, 2.564846666236137, 2.4110558632279475, 2.4383953572432997, 2.476055167160996, 2.1534976310201133, 2.0212222271083484, 2.81437813429358, 2.563033900756327, 2.2147278177677205], [2.569463518386207, 2.6604092726308757, 2.441915106578541, 2.7298980978643894, 2.7828592473446805, 2.344838703489967, 2.4120018101639493, 3.0595797508660754, 2.3823686126094783, 2.6579121010409863  …  2.2806116110954506, 2.7400279508219034, 2.5264303269742245, 2.568083401104926, 2.608313433153076, 2.2222973097667293, 2.069683337188792, 3.020285415753738, 2.7068742294111403, 2.2953257746108826], [2.7077912671353235, 2.8191485995514682, 2.567043912117531, 2.9258802352251827, 2.9705089278353753, 2.4606881653036146, 2.553396213923501, 3.2759455731320033, 2.523863207546285, 2.823617849624561  …  2.382645802944446, 2.938377337676113, 2.655757168796593, 2.7152259320000094, 2.7565110680828195, 2.3049238821678513, 2.1333506566505362, 3.2351677334961093, 2.8644967384641085, 2.3902610976460026], [2.8628467844668126, 2.9961995163188635, 2.710150342488719, 3.137064767839812, 3.179938856326101, 2.596013153942997, 2.7174299038237923, 3.4998963739486566, 2.687839687992005, 3.009677922830706  …  2.502542169330773, 3.150062224843041, 2.80049037347342, 2.8816068211535533, 2.9224039443611223, 2.4022487349472335, 2.2129595887869473, 3.4577643939269214, 3.0375088995153945, 2.5006359477564617]  …  [0.014379742770283647, 9.999956376706646, 9.999948411697904, 7.050761962421759e-8, 9.999960310044893, 9.999945286423651, 9.99995004162253, 9.999965630245182, 9.999949312530848, 9.999956889303983  …  9.999940920238412, 9.999959758090625, 9.999950203268208, 6.695732239000045e-8, 9.99995440348577, 1.2149681796439436e-17, 9.999924766451398, 9.999964985970438, 9.999956830887406, 5.851522479933156e-14], [0.007037399943526662, 9.999960527995487, 9.999953320951015, 5.09627489697409e-8, 9.999964087029932, 9.999950493083068, 9.999954795769233, 9.99996890094962, 9.999954136059161, 9.999960991813124  …  9.999946542391013, 9.999963587600678, 9.999954942032426, 5.215570633492583e-8, 9.99995874254989, 1.0483688397151612e-17, 9.999931925821397, 9.99996831798534, 9.999960938955574, 5.0366858227162426e-14], [0.0035489947203163814, 9.999964284239935, 9.999957763031077, 3.708860771576637e-8, 9.999967504589787, 9.999955204268005, 9.999959097502952, 9.99997186040722, 9.999958500572076, 9.999964703919803  …  9.999951629530502, 9.999967052687197, 9.999959229847475, 4.1011311311021855e-8, 9.99996266870071, 9.107697617345853e-18, 9.999938403896094, 9.999971332919014, 9.999964656092276, 4.3652157073453494e-14], [0.0018226889040241256, 9.999967683032894, 9.999961782394733, 2.7175872998128293e-8, 9.999970596927835, 9.999959467128242, 9.999962989875774, 9.999974538236705, 9.999962449749956, 9.9999680627752  …  9.999956232569126, 9.999970188029204, 9.999963109626144, 3.254923068282003e-8, 9.999966221231537, 7.96596331269849e-18, 9.99994426550771, 9.999974060945384, 9.999968019499036, 3.8092478105111166e-14], [0.0009470715618490968, 9.999970758389932, 9.999965419268149, 2.0047771487736637e-8, 9.999973394992654, 9.999963324326826, 9.99996651184296, 9.999976961238257, 9.999966023116562, 9.999971101995191  …  9.999960397574357, 9.999973025005664, 9.99996662019765, 2.6070625499355802e-8, 9.999969435696535, 7.014438827096166e-18, 9.999949569319156, 9.999976529367002, 9.999971062837275, 3.346825234692227e-14], [0.0004950061598007933, 9.999973541089686, 9.999968710049579, 1.4889242254970304e-8, 9.999975926787753, 9.999966814466996, 9.999969698652794, 9.999979153661739, 9.999969256434575, 9.999973851996753  …  9.999964166229928, 9.99997559200951, 9.99996979669623, 2.107053076192861e-8, 9.999972344266526, 6.2181625203197715e-18, 9.999954368411055, 9.999978762888299, 9.999973816565184, 2.9605649335664613e-14], [0.0002598239052136806, 9.99997605898188, 9.999971687673618, 1.1132358394203077e-8, 9.999978217651796, 9.999969972478516, 9.999972582199328, 9.999981137449373, 9.999972182063503, 9.99997634030237  …  9.999967576252951, 9.999977914731776, 9.999972670912747, 1.7181208745877927e-8, 9.999974976050943, 5.5492608687336655e-18, 9.99995871081295, 9.999980783862798, 9.999976308242543, 2.6366422368704278e-14], [0.00013724348427930958, 9.999978337266043, 9.999974381940794, 8.379034476908199e-9, 9.999980290512207, 9.999972829967225, 9.99997519134156, 9.99998293245533, 9.999974829283502, 9.999978591815465  …  9.999970661771385, 9.999980016418725, 9.999975271612822, 1.4132804345589597e-8, 9.999977357389152, 4.98541647930972e-18, 9.999962639983918, 9.999982612516847, 9.999978562806522, 2.3640140760021227e-14], [7.30702129222187e-5, 9.999980398743695, 9.999976819815801, 6.348586470391751e-9, 9.999982166114611, 9.999975415531342, 9.999977552192254, 9.99998455664444, 9.999977224588417, 9.999980629069634  …  9.99997345366556, 9.999981918104476, 9.99997762482473, 1.1725808974945363e-8, 9.999979512114047, 4.508687666999638e-18, 9.999966195247492, 9.999984267152033, 9.999980602821244, 2.1338221511832755e-14], [3.925217394137189e-5, 9.999982264046547, 9.999979025697353, 4.841950461501886e-9, 9.999983863230453, 9.99997775504765, 9.99997968837926, 9.999986026271978, 9.99997939195092, 9.999982472454153  …  9.999975979877215, 9.999983638821526, 9.999979754099869, 9.81164479729674e-9, 9.999981461790558, 4.104595067898149e-18, 9.99996941218518, 9.999985764328335, 9.999982448703618, 1.9389323623849977e-14]])))

Plotting the solution

After running solve, we get sol, a SirenSolution instance. This stores all variables given in state_names at each timepoint.

Sirens.SirenSolutionType
SirenSolution{X, Y<:SirenSolutionData} <: AbstractSirenSolution

Stores the solution of a SirenProblem over time.

Fields

  • t::X: Time points at which the solution is saved.
  • u::Y<:SirenSolutionData: A dictionary-like structure storing the saved states for each variable in the problem.

Interpolation

A solution can be interpolated at arbitrary times using callable syntax:

(sol::AbstractSirenSolution)(t::Real)

This returns a new SirenSolution with interpolated states at time t.

Interpolation Rules:

  • For numeric states and numeric arrays: Uses linear interpolation between saved time points.
  • For non-numeric states (e.g., Agents.jl models, objects): Uses constant interpolation (returns the state from the last saved time point before or at t).

The time t must be within [sol.t[1], sol.t[end]], otherwise a BoundsError is thrown.

Examples

sol(2.5)  # Interpolate solution at time t=2.5
source
using Plots

plot(sol.t, sol["forest.life[1]"], color=:green, label="Life")
plot!(sol.t, sol["forest.heat[1]"], color=:red, label="Heat")

Advanced Visualisations

While we can plot the variables from the ODE component easily, the Agent-based model is a bit more challenging. By default, we only store the variables given in state_names in the solution. This can be changed by providing save_vars=["forest.#model"] to solve, in which case the full agent-based model state will be visible in the solution at all time points.

#model and Special Variables

"#model" is a special variable for AgentsComponents. Special variables, denoted by starting with # are not saved by default but can be used with connectors, getstate, setstate!, or save_vars. To view the special variables of a component, you can call variables(component).

However, this can be wasteful if we know we only want an animation of the model (which can be generated during simulation). We will set up a Connector which takes an input of the model's current state, and instead of a transformation, we will use a function which adds the current state to a video.

using Makie
using CairoMakie

groupcolor(tree) = tree.heat > 1 ? :red : :green
groupmarker(a) = a.life > 1 ? :utriangle : :circle
fig, ax = abmplot(forest; agent_color=groupcolor, agent_marker=groupmarker, agent_size=10)
io = VideoStream(fig)
function plot_input(model)
    empty!(ax)
    abmplot!(ax, model; agent_color=groupcolor, agent_marker=groupmarker, agent_size=10)
    recordframe!(io)
end

conn3 = Connector(
    inputs=["forest.#model"],
    outputs=Vector{String}(),
    func=(model) -> plot_input(model)
)

sp = SirenProblem(components=[dup_comp, comp2], connectors=[conn1, conn2, conn3], tspan=tspan)
sol = solve(sp, alg)

save("forest_fire.mp4", io)