StockFlow
StockFlow.TheoryCausalLoop
— Constant- src ->
E V - vname -> Name
- tgt ->
StockFlow.TheoryStockAndFlow0
— Constantdefine the sub-schema of c0, which includes the three objects: stocks(S), sum-auxiliary-variables(SV), and the linkages between them (LS) to be composed
StockFlow.TheoryStockAndFlowF
— Constantdefine the schema of a general stock and flow diagram
StockFlow.TheoryStockAndFlowStructure
— Constantdefine the schema of a general stock and flow diagram, not includes the functions of auxiliary variables
StockFlow.TheoryStockAndFlowStructureF
— Constantdefine the schema of a general stock and flow diagram
StockFlow.CausalLoop
— MethodCreate a CausalLoop (Graph with named vertices) with a vector of vertices, and a vector of pairs of vertices.
CausalLoop([:A, :B], [:A => :B, :B => :B]) will create a CausalLoop with vertices A and B, an edge A => B and an edge B => B.
StockFlow.CausalLoopPM
— MethodCreate CausalLoopPM from vector of symbols, vector of pairs of symbols, and vector of polarities.
StockFlow.StockAndFlow0
— MethodStockAndFlow0(s,sv,ssv)
for an instance of the sub-schema, the program supports only have stocks, or only have sum auxiliary variables, or both stocks and sum auxiliary variables, or have both
StockFlow.StockAndFlowF
— MethodStockAndFlowF(s,p,v,f,sv)
EXAMPLE: when define the dynamical variables, need to define with the correct order
sir_StockAndFlow=StockAndFlowF((:S=>(:F_NONE,:inf,:N), :I=>(:inf,:F_NONE,:N)),
(:c, :beta),
(:v_prevalence=>(:I,:N,:/),:v_meanInfectiousContactsPerS=>(:c,:v_prevalence,:*),:v_perSIncidenceRate=>(:beta,:v_meanInfectiousContactsPerS,:*),:v_newInfetions=>(:S,:v_perSIncidenceRate,:*)),
(:inf=>:v_newInfetions),
(:N))
StockFlow.GraphCL
— MethodGraph a CausalLoopPM.
StockFlow.GraphCL
— MethodGraph a CausalLoopPol.
StockFlow.GraphCL
— MethodGraph a Causal Loop diagram (no polarities).
StockFlow.GraphRB
— MethodGraph reinforcing and balancing loops of a causal loop diagram.
StockFlow.Open
— MethodConstruct an OpenCausalLoopPM with a CausalLoopPM, and any number of additional CausalLoopPM to act as feet.
StockFlow.add_prefix!
— Methodadd_prefix!(sf::AbstractStockAndFlowStructureF, prefix)
Modify a AbstractStockAndFlowStructureF so named elements begin with prefix Prefix can be anything which can be cast to a Symbol.For feet.
StockFlow.add_prefix!
— Methodadd_prefix!(sf::AbstractStockAndFlowStructureF, prefix)
Modify a AbstractStockAndFlowStructureF so named elements begin with prefix Prefix can be anything which can be cast to a Symbol.
StockFlow.add_suffix!
— Methodadd_suffix!(sf::AbstractStockAndFlow0, suffix)
Modify a AbstractStockAndFlow0 so named elements end with suffix. Suffix can be anything which can be cast to a Symbol. For feet.
StockFlow.add_suffix!
— Methodadd_suffix!(sf::AbstractStockAndFlow0, suffix)
Modify a AbstractStockAndFlow0 so named elements end with suffix. Suffix can be anything which can be cast to a Symbol.
StockFlow.all_shortest_paths
— MethodReturn Matrix{Vector{Vector{Int}}} of all shortest paths. First index is src, second is tgt, and it points to a vector of all shortest paths, represented as a sequence of edge indices.
Shortest path to self is empty. [Vector{Int}()]
An empty Vector at i, j represents there being no path i -> j.
StockFlow.all_shortest_paths
— MethodReturn Matrix{Vector{Vector{Int}}} of all shortest paths. First index is src, second is tgt, and it points to a vector of all shortest paths, represented as a sequence of edge indices.
Shortest path to self is empty. [Vector{Int}()]
An empty Vector at i, j represents there being no path i -> j.
StockFlow.args
— Methodargs(p::AbstractStockAndFlowF,v)
Return a Vector of Symbols of flattened stocks, sums, parameters and source dynamic variables a dynamic variable at index v links to.
StockFlow.args
— Methodargs(p::AbstractStockAndFlowStructureF,v)
Return a Vector of Symbols of flattened stocks, sums, parameters and source dynamic variables a dynamic variable at index v links to.
StockFlow.args_vname
— MethodReturn a Tuple of Vectors of Symbols of flattened stocks, sums, parameters and source dynamic variables a dynamic variable at index v links to.
StockFlow.betweenness
— MethodCalculate betweenness centrality.
StockFlow.betweenness
— MethodCalculate betweenness centrality.
StockFlow.cl_cycles
— MethodCausalLoopPM, return all cycles of a causal loop as a vector of vectors of int, where positive edges come before negative.
Each cycle will include each edge at most once.
Don't count empty vector as a cycle.
StockFlow.cl_cycles
— MethodCausalLoopPol, return all cycles of a causal loop as a vector of vectors of int.
Each cycle will include each edge at most once.
Don't count empty vector as a cycle.
StockFlow.convertStockFlowToSystemStructure
— MethodReturn a new StockAndFlowStructureF with flattened names, operators and positions from an AbstractStockAndFlowF.
StockFlow.convertSystemStructureToStockFlow
— MethodReturn a new stock flow with flattened names, operators and positions from an AbstractStockAndFlowStructureF. Need to provide dynamic variable definitions, eg
convertSystemStructureToStockFlow(MyStockFlowStructure, (:v_prevalence=>(:I,:N,:/),:v_meanInfectiousContactsPerS=>(:c,:v_prevalence,:*)))
StockFlow.convertToCausalLoop
— MethodConvert StockFlow to CausalLoop. Nodes: stocks, flows, sum variables, parameters, nonflow dynamic variables Edges: morphisms in stock flow
StockFlow.convertToCausalLoop
— MethodConstruct a CausalLoop from a StockFlow.
StockFlow.epol
— MethodCausalLoopPM, return polarity of edge with index e.
StockFlow.epol
— MethodCausalLoopPol, return Polarity of edge e.
StockFlow.epols
— MethodCausalLoopPM, polarities of edges.
StockFlow.epols
— MethodCausalLoopPol, return vector of Polarities.
StockFlow.extracFlowsStructureAndFlatten
— MethodReturn flow names as Symbol, along with the linked flow variables
StockFlow.extracPsStructureAndFlatten
— MethodReturn parameter names as Symbol
StockFlow.extracStocksStructureAndFlatten
— MethodReturn stock names as Symbol, along with the linked flows and sum variables
StockFlow.extracSumVStructureAndFlatten
— MethodReturn sum variable names as Symbol, along with the linked dynamic variables
StockFlow.extracVAndAttrStructureAndFlatten
— MethodConvert dynamic variable names to Symbol, convert all operators to a single operator if they are equal else throw an error, and
StockFlow.extracVStructureAndFlatten
— MethodReturn dynamic variable definitions as Vector with elements of form :dv => [:arg1, :arg2]
StockFlow.extract_all_nonduplicate_paths
— MethodCausalLoopPol, return all dictionary of path => Polarity, for all paths with nonduplicate edges. Each path therefore will be at most |E|.
StockFlow.extract_all_nonduplicate_paths
— MethodCausalLoopPM, return all dictionary of path => Polarity, for all paths with nonduplicate edges. Each path therefore will be at most |E|.
StockFlow.extract_loops
— MethodReturn dict of pairs of edges => polarity.
StockFlow.flowVariableIndex
— Methodreturn the auxiliary variable's index that related to the flow with index of f
StockFlow.fname
— Methodreturn the flows name with index of f
StockFlow.fnames
— Methodreturn flow names
StockFlow.from_clp
— MethodConvert a CausalLoopPol to a CausalLoopPM.
StockFlow.funcDynam
— MethodfuncDynam(p::AbstractStockAndFlow,v)
return the functions of variables give index v
StockFlow.funcDynam
— MethodfuncDynam(sf::AbstractStockAndFlowF,v)
return the functions of variables give index v
StockFlow.funcFlow
— Methodgenerate the function substituting sum variables in with flow index fn
StockFlow.funcFlowRaw
— Methodreturn the functions (not substitutes the function of sum variables yet) of flow index f
StockFlow.funcFlows
— Methodreturn the LVector of pairs: fname => function (with function of sum variables substitue in)
StockFlow.funcFlowsRaw
— Methodreturn the LVector of pairs: fname => function (raw: not substitutes the function of sum variables yet)
StockFlow.funcSV
— Methodgenerate the function of a sum auxiliary variable (index sv) with the sum of all stocks links to it
StockFlow.funcSVs
— Methodreturn the LVector of pairs: svname => function
StockFlow.fv
— Methodget flow variable of flow
StockFlow.fvs
— Methodget ordered list of indices of dynamic variable for each flow
StockFlow.incoming_edges
— MethodCausalLoopPol, indices of all edges with tgt vertex with index n.
StockFlow.inflows
— Methodreturn inflows of stock index s
StockFlow.inflowsAll
— Methodreturn all inflows
StockFlow.instock
— Methodreturn stocks of flow index f flow in
StockFlow.is_circuit
— MethodReturn true if a given list of edges is a walk, and the target of the final edge is the source of the first.
Empty list of edges does not count as a circuit.
StockFlow.is_walk
— MethodReturn true if a given list of edges is a walk; that the target for each edge is the source of the next. Empty list of edges counts as a walk.
Note, negative edges come after positive edges:
julia> using StockFlow; using StockFlow.Syntax;
julia> cl = (@cl A => +B, B => -C, C => +D, D => -E);
julia> is_walk(cl, [1,3,2,4])
true
StockFlow.leg
— MethodCausalLoopPM, used for composition.
StockFlow.lpvvposition
— Methodreturn argument position of source constant parameter of an auxiliary variable
StockFlow.lsnames
— Methodreturn the pair of names of (stock, sum-auxiliary-variable) for all linkages between them
StockFlow.lsvvposition
— Methodreturn argument position of source sum dynamical variable of an auxiliary variable
StockFlow.lvtgtposition
— Methodreturn argument position of source auxiliary variable of an auxiliary variable
StockFlow.lvvposition
— Methodreturn argument position of source stock variable of an auxiliary variable
StockFlow.make_v_expr
— Methodcreate expresision of an auxiliary variable v
StockFlow.make_v_expr_nonrecursive
— MethodGenerates the expression of an auxiliary variable, only going one layer deep If other dynamic variables make up its definition, they will not be substituted.
StockFlow.nedges
— MethodReturn total number of edges of CausalLoopPM (# pos + # neg).
StockFlow.nedges
— MethodReturn count of edges of CausalLoop.
StockFlow.nf
— Methodflow count
StockFlow.ni
— Methodinflow count
StockFlow.nlpv
— Methodlink dynamic variable parameter count
StockFlow.nls
— Methodlink stock sum count
StockFlow.nlsv
— Methodlink sum variable dynamic variable count
StockFlow.nlv
— Methodlink stock dynamic variable count
StockFlow.nlvv
— Methodlink dynamic variable dynamic variable count
StockFlow.nm
— MethodReturn count of negative edges of CausalLoopPM.
StockFlow.no
— Methodoutflow count
StockFlow.np
— MethodReturn count of positive edges of CausalLoopPM.
StockFlow.np
— Methodparameter count
StockFlow.ns
— Methodstock count
StockFlow.nsv
— Methodsum variable count
StockFlow.num_indep_loops_var_on
— MethodCount how many loops a variable is on, ignoring when two vertices have more than one edge between them.
A => +B, B => +C, C => +A, C => -A will be treated as 1 loop.
Takes a CausalLoopPM or a CausalLoopPol, and a Symbol. Throws an error if that Symbol is the name for more than one variable.
StockFlow.num_inputs_outputs
— MethodReturn vector of tuple of (name, num inputs, num outputs)
StockFlow.num_inputs_outputs
— MethodReturn vector of tuple of (name, num inputs, num outputs)
StockFlow.num_inputs_outputs_pols
— MethodReturn vector of tuple of (name, num pos inputs, num pos outputs, num neg inputs, num neg outputs)
StockFlow.num_inputs_outputs_pols
— MethodReturn vector of tuple of (name, num pos inputs, num pos outputs, num neg inputs, num neg outputs)
StockFlow.num_loops_var_on
— MethodCount how many loops a variable is on.
A => +B, B => +C, C => +A, C => -A will be treated as 2 loops: [1,2,3] and [1,2,4]. Each variable will be treated as being on 2 loops.
Takes a CausalLoopPM or a CausalLoopPol, and a Symbol. Throws an error if that Symbol is the name for more than one variable.
StockFlow.nvb
— Methoddynamic variable count
StockFlow.nvert
— MethodReturn count of vertices of CausalLoopPM.
StockFlow.nvert
— MethodReturn count of vertices of CausalLoop.
StockFlow.outflows
— Methodreturn outflows of stock index s
StockFlow.outflowsAll
— Methodreturn all outflows
StockFlow.outgoing_edges
— MethodCausalLoopPol, indices of all edges with src vertex with index n.
StockFlow.outstock
— Methodreturn stocks that flow index f flow out from
StockFlow.pname
— MethodCausalLoopPM, name a positive edge by its source and target. For composition.
StockFlow.pname
— Methodreturn the auxiliary variables name with index of v
StockFlow.pnames
— MethodCausalLoopPM, pairs of source, target for positive edges. Used for composition.
StockFlow.pnames
— Methodreturn parameter names
StockFlow.rebuildStratifiedModelByFlattenSymbols
— MethodReturn a new stock flow with flattened names, operators and positions from the old
StockFlow.sedge
— MethodReturn source vertex index of an edge of CausalLoopPM by index. Negative edges come after positive edges.
julia-repl julia> using StockFlow; StockFlow.Syntax; julia> cl = (@cl A => +B, B => -C, C => +D); julia> sedge(cl, 3) 2
` The nodes are ordered A, B, C. The edges are ordered A => +B, C => +D, B => -C; so, the source index of the third edge is B, which has index 2.
StockFlow.sedge
— MethodCausalLoopPol, return edge's name with src number e
StockFlow.set_fnames!
— Methodset flow names to vector of symbols
StockFlow.set_pnames!
— Methodset parameter names to vector of symbols
StockFlow.set_snames!
— Methodset stock names to vector of symbols
StockFlow.set_svnames!
— Methodset sum variable names to vector of symbols
StockFlow.set_vnames!
— Methodset dynamic variable names to vector of symbols
StockFlow.shortest_paths
— MethodReturn vector of all shortest paths between two nodes. Takes node indices as args.
StockFlow.shortest_paths
— MethodReturn vector of all shortest paths between two nodes. Takes node names as args.
StockFlow.sname
— Methodreturn the stocks name with index of s
StockFlow.snames
— Methodreturn stock names
StockFlow.stockssv
— Methodreturn stocks that sum variable index sv link to
StockFlow.stocksv
— Methodreturn stocks that auxiliary variable index v link to
StockFlow.svname
— Methodreturn the sum auxiliary variables name with index of sv
StockFlow.svnames
— Methodreturn sum variable names
StockFlow.svsstock
— Methodreturn sum auxiliary variables that stock s links to
StockFlow.svsstockAllF
— Methodreturn sum auxiliary variables all stocks link (frequency)
StockFlow.svsv
— Methodreturn sum variables that auxiliary variable index v link to
StockFlow.tedge
— MethodReturn target vertex index of an edge of CausalLoopPM by index. Negative edges come after positive edges. See sedge.
StockFlow.tedge
— MethodCausalLoopPol, return edge's name with tgt number e
StockFlow.to_clp
— MethodConvert CausalLoopPM to CausalLoopPol.
StockFlow.to_clp
— MethodCreate a CausalLoopPol from a vector of node names, and two vectors indicating which indices for vertices will act as edges.
to_clp([:A, :B], [1 => 2], Vector{Pair{Int, Int}}()) will create a CausalLoopPol with a positive polarity edge from A to B.
StockFlow.to_graphs_graph
— MethodConvert CausalLoopPM to a Graphs' library graph. Note, this removes duplicate edges!
StockFlow.to_graphs_graph
— MethodConvert a CausalLoopPol to a Graphs package Graph.
StockFlow.to_simple_cl
— MethodConvert a CausalLoopPM to a CausalLoop (forget polarities).
StockFlow.to_simple_cl
— MethodConvert a CausalLoopPol to a CausalLoop (forget polarities).
StockFlow.to_simple_cl
— MethodIdentity function on CausalLoop.
StockFlow.vname
— Methodreturn the auxiliary variables name with index of v
StockFlow.vnames
— Methodreturn variable names
StockFlow.vop
— Methodreturn operator of an auxiliary variable
StockFlow.vpsrc
— Methodreturn auxiliary variable's source constant parameters
StockFlow.vptgt
— Methodreturn constant parameters's link auxiliary variables
StockFlow.vsrc
— Methodreturn auxiliary variable's target auxiliary variables it links to
StockFlow.vsstock
— Methodreturn auxiliary variables that stock s links to
StockFlow.vsstockAllF
— Methodreturn auxiliary variables all stocks link (frequency)
StockFlow.vssv
— Methodreturn auxiliary variables that sum auxiliary variable sv links to
StockFlow.vssvAllF
— Methodreturn auxiliary variables all sum auxiliary variables link (frequency)
StockFlow.vtgt
— Methodreturn auxiliary variable's source auxiliary variables that it links to
StockFlow.walk_polarity
— MethodReturn polarity of walk. Empty walk is positive.
StockFlow.Syntax
— ModuleAlternative syntax for use in the definition of stock and flow models.
Examples
# An S-I-R model of infection
SIR = @stock_and_flow begin
:stocks
S
I
R
:parameters
c
beta
tRec
:dynamic_variables
v_prevalence = I / N
v_meanInfectiousContactsPerS = c * v_prevalence
v_perSIncidenceRate = beta * v_meanInfectiousContactsPerS
v_newInfections = S * v_perSIncidenceRate
v_newRecovery = I / tRec
:flows
S => inf(v_newInfections) => I
I => rec(v_newRecovery) => R
:sums
N = [S, I, R]
end
# Generates:
# SIR = StockAndFlowF(
# # stocks
# (:S => (:F_NONE, :inf, :N), :I => (:inf, :rec, :N), :R => (:rec, :F_NONE, :N)),
# # parameters
# (:c, :beta, :tRec),
# # dynamical variables
# ( :v_prevalence => ((:I, :N) => :/),
# :v_meanInfectiousContactsPerS => ((:c, :v_prevalence) => :*),
# :v_perSIncidenceRate => ((:beta, :v_meanInfectiousContactsPerS) => :*),
# :v_newInfections => ((:S, :v_perSIncidenceRate) => :*),
# :v_newRecovery => ((:I, :tRec) => :/),
# ),
# # flows
# (:inf => :v_newInfections, :rec => :v_newRecovery),
# # sum dynamical variables
# (:N),
# )
# The same model as before, but with the dynamic variables inferred
SIR_2 = @stock_and_flow begin
:stocks
S
I
R
:parameters
c
beta
tRec
# We can leave out dynamic variables and let them be inferred from flows entirely!
:flows
S => inf(S * beta * (c * (I / N))) => I
I => rec(I / tRec) => R
:sums
N = [S, I, R]
end
# Another possible S-I-R model definition
SIR_3 = @stock_and_flow begin
:stocks
S
I
R
:parameters
c
beta
tRec
omega
alpha
:dynamic_variables
v_prevalence = I / totalPopulation
v_forceOfInfection = c * v_prevalence * beta
:flows
S => inf(S * v_forceOfInfection) => I
☁ => births(totalPopulation * alpha) => S
S => deathsS(S * omega) => ☁
I => rec(I / tRec) => R
I => deathsI(I * omega) => ☁
R => deathsR(R * omega) => ☁
:sums
totalPopulation = [S, I, R]
end
StockFlow.Syntax.cl_macro
— FunctionCreate a causal loop with polarities with a block of statements in a tuple, consisting of A => +B, A => -B and A.
Will create a causal loop without polarities if you use A => B without a + or -.
StockFlow.Syntax.create_foot
— Methodcreate_foot(block :: Expr)
Create foot (StockAndFlow0) using format A => B, where A is a stock and B is a sum variable. Use () to represent no stock or sum variable. To have multiple stocks or sum variables, chain together multiple pairs with commas. Repeated occurences of the same symbol will be treated as the same stock or sum variable. You cannot create distinct stocks or sum variables with the same name using this format.
standard_foot = @foot A => N
emptyfoot = @foot () => ()
all_seir_links = @foot S => N, E => N, I => N, R => N
no_stocks = @foot () => N, () => NI, () => NS
no_sum = @foot A => ()
multiple_links = @foot A => B, A => B # will have two links from A to B.
StockFlow.Syntax.infer_links
— Methodinfer_links(sfsrc :: StockAndFlowF, sftgt :: StockAndFlowF, NecMaps :: Dict{Symbol, Vector{Int64}})
Infer LS, I, O, LV, LSV, LVV, LPV mappings for an ACSetTransformation. Returns dictionary of Symbols to lists of indices, corresponding to an ACSetTransformation argument. If there exist no such mappings (eg, no LVV), that pairing will not be included in the returned dictionary.
If A <- C -> B, and we have A -> A' and B -> B' and a unique C' such that A' <- C' -> B', we can assume C -> C'.
:S => [2,4,1,3], :F => [1,2,4,3], ...
NecMaps must contain keys S, F, SV, P, V, each pointing to a (possibly empty) array of indices
StockFlow.Syntax.@causal_loop
— MacroCreate a causal loop diagram, with or without Polarities. X => +Y creates a positive edge, X => -Y a negative, and X => Y creates a causal loop without polarities. Defaults to creating with polarities, if there are no edges.
julia> (@causal_loop begin
:nodes
A
B
:edges
A => +B
end) == CausalLoopPM([:A,:B], [:A => :B], [POL_POSITIVE])
true
julia> ((@causal_loop begin
:nodes
λ
β
:edges
λ => β
β => -λ
end) ==
(@causal_loop begin
:nodes
λ
β
:edges
λ => β
β => λ
end) ==
CausalLoop([:λ, :β], [:λ => :β, :β => :λ]))
true
julia> (@causal_loop begin end) == CausalLoopPM()
true
StockFlow.Syntax.@cl
— MacroCompressed notation to create causal loop. Using A => B without a + or - creates a CausalLoop without polarities.
julia> (@cl A => +B, C => -D) == CausalLoopPM([:A, :B, :C, :D], [:A => :B, :C => :D], [POL_POSITIVE, POL_NEGATIVE])
true
julia> (@cl A, B, C => -A, C => +B, D) == CausalLoopPM([:A, :B, :C, :D], [:C => :A, :C => :B], [POL_NEGATIVE, POL_POSITIVE])
true
StockFlow.Syntax.@cl
— MacroCompressed notation to create causal loop with polarities.
No argument function for empty causal loop.
julia> @cl
CausalLoopPM {V:0, P:0, M:0, Name:0}
StockFlow.Syntax.@feet
— Macrofeet(block :: Expr)
Create Vector of feet using same notation for foot macro. Separated by newlines. First argument is stock, second is sum variable.
feetses = @feet begin
A => B
() => N
C => ()
D => E
() => ()
P => NI, R => NI, () => N
end
StockFlow.Syntax.@foot
— Macrofoot(block :: Expr)
Create a foot with S => N syntax, where S is stock, N is sum variable.
@foot P => Q
@foot S1 => ()
@foot () => N
@foot () => ()
@foot A => N, () => NI
StockFlow.Syntax.@stock_and_flow
— Macrostock_and_flow(block :: Expr)
Compiles stock and flow syntax of the line-based block form
:stocks
symbol_1
symbol_2
...
symbol_n
:parameters
param_1
param_2
...
param_n
:dynamic_variables
dyvar_1 = symbol_h * param_g ... - symbol_x / param_y
...
dyvar_n = symbol_k * param_j - dyvar_a ... - symbol_p / param_q
:flows
symbol_r => flow_name_1(dyvar_k) => symbol_q
symbol_z => flow_name_2(dyvar_g * param_v) => symbol_p
☁ => flow_name_3(symbol_c + dyvar_b) => symbol_r
symbol_j => flow_name_4(param_l + symbol_m) => CLOUD
...
symbol_y => flow_name_n(dyvar_f) => ☁
into a StockAndFlowF data type for use with the StockFlow.jl modelling system.
StockFlow.PremadeModels.seir
— MethodReturn a new SEIR model
StockFlow.PremadeModels.sir
— MethodReturn a new SIR model
StockFlow.PremadeModels.sis
— MethodReturn a new SIS model
StockFlow.PremadeModels.svi
— MethodReturn a new SVI model