Dependency Graphs
Dependency Graph API
The dependency graph constructors return a BipartiteGraph from BipartiteGraphs.jl. The graph type and its primitive operations are documented and versioned by BipartiteGraphs; use that API when working with the returned graph.
Constructing Dependency Graphs
ModelingToolkitBase.equation_dependencies — Function
equation_dependencies(sys::AbstractSystem; variables = unknowns(sys))Given an AbstractSystem calculate for each equation the variables it depends on.
Notes:
- Variables that are not in
variablesare filtered out. get_variables!is used to determine the variables within a given equation.- returns a
Vector{Vector{Variable}}()mapping the index of an equation to thevariablesit depends on.
Example:
using ModelingToolkitBase
using ModelingToolkitBase: t_nounits as t
@parameters β γ κ η
@variables S(t) I(t) R(t)
rate₁ = β * S * I
rate₂ = γ * I + t
affect₁ = [S ~ S - 1, I ~ I + 1]
affect₂ = [I ~ I - 1, R ~ R + 1]
j₁ = ModelingToolkitBase.ConstantRateJump(rate₁, affect₁)
j₂ = ModelingToolkitBase.VariableRateJump(rate₂, affect₂)
# create a JumpSystem using these jumps
@named jumpsys = JumpSystem([j₁, j₂], t, [S, I, R], [β, γ])
# dependency of each jump rate function on unknown variables
equation_dependencies(jumpsys)
# dependency of each jump rate function on parameters
equation_dependencies(jumpsys, variables = parameters(jumpsys))ModelingToolkitBase.asgraph — Function
asgraph(eqdeps, vtois)Convert a collection of equation dependencies, for example as returned by equation_dependencies, to a BipartiteGraphs.BipartiteGraph.
Notes:
vtoisshould provide aDictlike mapping from eachVariabledependency ineqdepsto the integer idx of the variable to use in the graph.
Example: Continuing the example started in equation_dependencies
digr = asgraph(equation_dependencies(jumpsys),
Dict(s => i for (i, s) in enumerate(unknowns(jumpsys))))asgraph(sys::AbstractSystem; variables = unknowns(sys),
variablestoids = Dict(convert(Variable, v) => i for (i, v) in enumerate(variables)))Convert an AbstractSystem to a BipartiteGraphs.BipartiteGraph mapping the index of equations to the indices of variables they depend on.
Notes:
- Defaults for kwargs creating a mapping from
equations(sys)tounknowns(sys)they depend on. variablesshould provide the list of variables to use for generating the dependency graph.variablestoidsshould provideDictlike mapping from aVariableto itsIntindex withinvariables.
Example: Continuing the example started in equation_dependencies
digr = asgraph(jumpsys)ModelingToolkitBase.variable_dependencies — Function
variable_dependencies(sys::AbstractSystem; variables = unknowns(sys),
variablestoids = nothing)For each variable, determine the equations that modify it and return a BipartiteGraphs.BipartiteGraph.
Notes:
- Dependencies are returned as a
BipartiteGraphs.BipartiteGraphmapping variable indices to the indices of equations that modify them. variablesdenotes the list of variables to determine dependencies for.variablestoidsdenotes aDictmappingVariables to theirIntindex invariables.
Example: Continuing the example of equation_dependencies
variable_dependencies(jumpsys)ModelingToolkitBase.asdigraph — Function
asdigraph(g::BipartiteGraph, sys::AbstractSystem; variables = unknowns(sys),
equationsfirst = true)Convert a BipartiteGraphs.BipartiteGraph to a LightGraph.SimpleDiGraph.
Notes:
- The resulting
SimpleDiGraphunifies the two sets of vertices (equations and then unknowns in the case it comes fromasgraph), producing one ordered set of integer vertices (SimpleDiGraphdoes not support two distinct collections of vertices, so they must be merged). variablesgives the variables thatgare associated with (usually theunknownsof a system).equationsfirst(default istrue) gives whether theBipartiteGraphs.BipartiteGraphgives a mapping from equations to variables they depend on (true), as calculated byasgraph, or whether it gives a mapping from variables to the equations that modify them, as calculated byvariable_dependencies.
Example: Continuing the example in asgraph
dg = asdigraph(digr, jumpsys)ModelingToolkitBase.eqeq_dependencies — Function
eqeq_dependencies(eqdeps::BipartiteGraph{T},
vardeps::BipartiteGraph{T}) where {T <: Integer}Calculate a LightGraph.SimpleDiGraph that maps each equation to equations they depend on.
Notes:
- The
fadjlistof theSimpleDiGraphmaps from an equation to the equations that modify variables it depends on. - The
badjlistof theSimpleDiGraphmaps from an equation to equations that depend on variables it modifies.
Example: Continuing the example of equation_dependencies
eqeqdep = eqeq_dependencies(asgraph(jumpsys), variable_dependencies(jumpsys))ModelingToolkitBase.varvar_dependencies — Function
function varvar_dependencies(eqdeps::BipartiteGraph{T},
vardeps::BipartiteGraph{T}) where {T <: Integer}
eqeq_dependencies(vardeps, eqdeps)
endCalculate a LightGraph.SimpleDiGraph that maps each variable to variables they depend on.
Notes:
- The
fadjlistof theSimpleDiGraphmaps from a variable to the variables that depend on it. - The
badjlistof theSimpleDiGraphmaps from a variable to variables on which it depends.
Example: Continuing the example of equation_dependencies
varvardep = varvar_dependencies(asgraph(jumpsys), variable_dependencies(jumpsys))Variable-to-Equation Mapping
ModelingToolkit.map_variables_to_equations — Function
map_variables_to_equations(
sys::ModelingToolkitBase.AbstractSystem;
rename_dummy_derivatives
) -> Dict{Union{Num, SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{T} where T}, Equation}
Given a system that has been simplified via mtkcompile, return a Dict mapping variables of the system to equations that are used to solve for them. This includes observed variables.
Arguments
sys: a system returned byModelingToolkitBase.mtkcompileor another simplification path that records a tearing state.
Keyword Arguments
rename_dummy_derivatives: Whether to rename dummy derivative variable keys into theirDifferentialforms. For example, this would turn the keyyˍt(t)intoDifferential(t)(y(t)).
Returns
A Dict mapping unknown or observed symbolic variables to the equations used to solve for them.
Examples
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@variables x(t) = 1 y(t) = 0
eqs = [D(x) ~ -x,
y ~ x + 1]
@named sys = System(eqs, t)
simplified = mtkcompile(sys)
mapping = map_variables_to_equations(simplified)
mapping[y]