StockFlow
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
— MethodCausalLoop(ns,es)
Create causal loop diagram from collection of nodes and collection of edges.
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.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.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.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 CLD. Nodes: stocks, flows, sum variables, parameters, nonflow dynamic variables Edges: morphisms in stock flow
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_loops
— MethodCycles 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.
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.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.inflows
— Methodreturn inflows of stock index s
StockFlow.inflowsAll
— Methodreturn all inflows
StockFlow.instock
— Methodreturn stocks of flow index f flow in
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.ne
— Methodreturn count of edges of CLD
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.nn
— Methodreturn count of nodes of CLD
StockFlow.nname
— Methodreturn node's name with index n
StockFlow.nnames
— Methodreturn node names of CLD
StockFlow.no
— Methodoutflow count
StockFlow.np
— Methodparameter count
StockFlow.ns
— Methodstock count
StockFlow.nsv
— Methodsum variable count
StockFlow.nvb
— Methoddynamic variable count
StockFlow.outflows
— Methodreturn outflows of stock index s
StockFlow.outflowsAll
— Methodreturn all outflows
StockFlow.outstock
— Methodreturn stocks that flow index f flow out from
StockFlow.pname
— Methodreturn the auxiliary variables name with index of v
StockFlow.pnames
— Methodreturn parameter names
StockFlow.rebuildStratifiedModelByFlattenSymbols
— MethodReturn a new stock flow with flattened names, operators and positions from the old
StockFlow.sedge
— Methodreturn edge's name with target number t
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.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 edge's name with edge number e
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.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.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.@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