StockFlow

StockFlow.TheoryStockAndFlow0Constant

define 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

source
StockFlow.CausalLoopMethod
CausalLoop(ns,es)

Create causal loop diagram from collection of nodes and collection of edges.

source
StockFlow.StockAndFlow0Method
StockAndFlow0(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

source
StockFlow.StockAndFlowFMethod
StockAndFlowF(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))
source
StockFlow.add_prefix!Method
add_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.

source
StockFlow.add_prefix!Method
add_prefix!(sf::AbstractStockAndFlowStructureF, prefix)

Modify a AbstractStockAndFlowStructureF so named elements begin with prefix Prefix can be anything which can be cast to a Symbol.

source
StockFlow.add_suffix!Method
add_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.

source
StockFlow.add_suffix!Method
add_suffix!(sf::AbstractStockAndFlow0, suffix)

Modify a AbstractStockAndFlow0 so named elements end with suffix. Suffix can be anything which can be cast to a Symbol.

source
StockFlow.argsMethod
args(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.

source
StockFlow.argsMethod
args(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.

source
StockFlow.args_vnameMethod

Return a Tuple of Vectors of Symbols of flattened stocks, sums, parameters and source dynamic variables a dynamic variable at index v links to.

source
StockFlow.convertSystemStructureToStockFlowMethod

Return 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,:*)))
source
StockFlow.convertToCausalLoopMethod

Convert StockFlow to CLD. Nodes: stocks, flows, sum variables, parameters, nonflow dynamic variables Edges: morphisms in stock flow

source
StockFlow.extract_loopsMethod

Cycles are uniquely characterized by sets of edges, not sets of nodes We construct a simple graph, then for each edge, we check if there exist multiple edges with the same start and end node We then product all of them, to get every cycle.

This could be made more efficient, but it should be fine for now.

source
StockFlow.funcFlowsMethod

return the LVector of pairs: fname => function (with function of sum variables substitue in)

source
StockFlow.funcFlowsRawMethod

return the LVector of pairs: fname => function (raw: not substitutes the function of sum variables yet)

source
StockFlow.funcSVMethod

generate the function of a sum auxiliary variable (index sv) with the sum of all stocks links to it

source
StockFlow.lsnamesMethod

return the pair of names of (stock, sum-auxiliary-variable) for all linkages between them

source
StockFlow.make_v_expr_nonrecursiveMethod

Generates the expression of an auxiliary variable, only going one layer deep If other dynamic variables make up its definition, they will not be substituted.

source
StockFlow.vtgtMethod

return auxiliary variable's source auxiliary variables that it links to

source
StockFlow.SyntaxModule

Alternative 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
source
StockFlow.Syntax.infer_linksMethod
infer_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

source
StockFlow.Syntax.@feetMacro
feet(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
source
StockFlow.Syntax.@footMacro
foot(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
source
StockFlow.Syntax.@stock_and_flowMacro
stock_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.

source