Making Graphs over System Dynamics Models
Anton Antonov
​MathematicaForPrediction at WordPress​
​SystemModeling at GitHub​
December 2020

Introduction

In this document we give usage examples for the functions of the package, "SystemDynamicsModelGraph.m", [AAp1]. The package provides functions for making dependency graphs for the stocks in System Dynamics (SD) models. The primary motivation for creating the functions in this package is to have the ability to introspect, proofread, and verify the (typical) ODE models made in SD.
A more detailed explanation is:
◼
  • For a given SD system
    S
    of Ordinary Differential Equations (ODEs) we make Mathematica graph objects that represent the interaction of variable dependent functions in
    S
    .
  • ◼
  • Those graph objects give alternative (and hopefully convenient) way of visualizing the model of
    S
    .
  • Load packages

    The following commands load the packages [AAp1, AAp2, AAp3]:
    In[]:=
    Import["https://raw.githubusercontent.com/antononcube/SystemModeling/master/WL/SystemDynamicsModelGraph.m"]​​Import["https://raw.githubusercontent.com/antononcube/SystemModeling/master/Projects/Coronavirus-propagation-dynamics/WL/EpidemiologyModels.m"]​​Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/CallGraph.m"]

    Usage examples

    Equations

    Here is a system of ODEs of a slightly modified SEIR model:
    In[]:=
    lsEqs={Derivative[1][SP][t]-((IP[t]SP[t]β[IP])/TP[t])-SP[t]μ[TP],Derivative[1][EP][t](IP[t]SP[t]β[IP])/TP[t]-EP[t](1/aincp+μ[TP]),Derivative[1][IP][t]EP[t]/aincp-IP[t]/aip-IP[t]μ[IP],Derivative[1][RP][t]IP[t]/aip-RP[t]μ[TP],TP[t]Max[0,EP[t]+IP[t]+RP[t]+SP[t]]};​​ResourceFunction["GridTableForm"][List/@lsEqs,TableHeadings{"Equations"}]
    Out[]=
    #
    Equations
    1
    ′
    SP
    [t]-
    IP[t]SP[t]β[IP]
    TP[t]
    -SP[t]μ[TP]
    2
    ′
    EP
    [t]
    IP[t]SP[t]β[IP]
    TP[t]
    -EP[t]
    1
    aincp
    +μ[TP]
    3
    ′
    IP
    [t]
    EP[t]
    aincp
    -
    IP[t]
    aip
    -IP[t]μ[IP]
    4
    ′
    RP
    [t]
    IP[t]
    aip
    -RP[t]μ[TP]
    5
    TP[t]Max[0,EP[t]+IP[t]+RP[t]+SP[t]]

    Model graph

    Here is a graph of the dependencies between the populations:
    In[]:=
    ModelDependencyGraph[lsEqs,{EP,IP,RP,SP,TP},t]
    Out[]=
    When the second argument given to ModelDependencyGraph is Automatic the stocks in the equations are heuristically found with the function ModelHeuristicStocks:
    In[]:=
    ModelHeuristicStocks[lsEqs,t]
    Out[]=
    {EP,IP,RP,SP,TP}
    Also, the function ModelDependencyGraph takes all options of Graph:
    In[]:=
    ModelDependencyGraph[lsEqs,Automatic,t,​​GraphLayout"GravityEmbedding",VertexLabels"Name",VertexLabelStyleDirective[Red,Bold,16],EdgeLabelStyleDirective[Blue,16],ImageSizeLarge]
    Out[]=

    Dependencies only

    The dependencies in the model can be found with the function ModelDependencyGraphEdges:
    In[]:=
    lsEdges=ModelDependencyGraphEdges[lsEqs,Automatic,t]
    Out[]=
    EP
    -
    1
    aincp
    -μ[TP]
    
    EP,IP
    SP[t]β[IP]
    TP[t]
    
    EP,SP
    IP[t]β[IP]
    TP[t]
    
    EP,EP
    1
    aincp
    
    IP,IP
    -
    1
    aip
    
    IP,IP
    -μ[IP]
    
    IP,IP
    1
    aip
    
    RP,RP
    -μ[TP]
    
    RP,IP
    -
    SP[t]β[IP]
    TP[t]
    
    SP,SP
    -
    IP[t]β[IP]
    TP[t]
    
    SP,SP
    -μ[TP]
    
    SP,EP
    1
    
    TP,IP
    1
    
    TP,RP
    1
    
    TP,SP
    1
    
    TP
    In[]:=
    lsEdges〚4〛//FullForm
    Out[]//FullForm=
    DirectedEdge[EP,IP,Power[aincp,-1]]

    Focus stocks

    Here is a graph for a set of “focus” stocks-sources to a set of “focus” stocks-destinations:
    In[]:=
    gr=ModelDependencyGraph[lsEqs,{IP,SP},{EP},t]
    Out[]=
    Compare with the graph in which the argument positions of sources and destinations of the previous command are swapped:
    In[]:=
    ModelDependencyGraph[lsEqs,{EP},{IP,SP},t]
    Out[]=

    Additional interfacing

    The functions of this package work with the models from the package "EpidemiologyModels.m", [AAp2].
    Here is a model from [AAp2]:
    Here we make the corresponding graph:

    Generating equations from graph specifications

    A related, dual, or inverse task to the generation of graphs from systems of ODEs is the generation of system of ODEs from graphs.
    Here is a model specifications through graph edges (using DirectedEdge):
    Here is the corresponding graph:
    Here we generate the system of ODEs using the function ModelGraphEquations:
    Remark: ModelGraphEquations works with both graph and list of edges as a first argument.
    Here we replace the symbolically represented rates with concrete values:
    Here we solve the system of ODEs:
    Here we plot the results:

    Call graph

    The functionalities provided by the presented package "SystemDynamicsModelGraph.m", [AAp1], resemble in spirit those of the package "CallGraph.m”, [AAp3].
    Here is call graph for the functions in the package [AAp1] made with the function CallGraph from the package [AAp3]:

    References

    Packages

    [AAp1] Anton Antonov, "System Dynamics Model Graph Mathematica package", (2020), SystemsModeling at GitHub/antononcube.
    [AAp2] Anton Antonov, "Epidemiology models Mathematica package", (2020), SystemsModeling at GitHub/antononcube.
    [AAp3] Anton Antonov, "Call graph generation for context functions Mathematica package", (2018), MathematicaForPrediction at GitHub/antononcube.

    Articles

    [AA1] Anton Antonov, "Call graph generation for context functions", (2019), MathematicaForPrediction at WordPress.