You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice to have the following work on any AbstractSystem. You supply a network given by a LightGraphs AbstractGraph, you provide a prototype component, and you have a syntax for unidirectional edges. Here's an example. Let g be the graph and suppose we've already defined a component c. Then we might want to construct:
that clones c into c_i many versions, one for each node, and for every edge makes that connection equation. For more complex examples, we could have
ODESystem([(connection1,g1),(connection2,g2)],c)
or something like that so that way you can have different graphs specifying different edges and different equations associated with those edge graphs. The constraint would be that the nodes would have to be the same between all of the graphs, since you're just changing the associated edges.
I think this could cover neuron simulations, building RDME reaction networks, graph-based epidemiological models, and power systems models. Thoughts @isaacsas@asinghvi17 ?
Maybe I should clarify a bit. It's for network dynamics where you have for example Lotka-Volterra on a network, so you have the Lotka-Volterra equations for x_i and y_i, and then you have terms in the differential equations that connect ODE sets i and j.
Let me rerun some simulations - as I recall, I kind of dropped this effort because (a) the DAE solve was quite a bit slower than the handwritten ODE solve, and there were some differences between the DAE and ODE solutions. If the DAE solution is accurate, then this approach makes sense to me; otherwise, I was going to try and build up the graph "by hand", by modifying the equations before creating a system.
The DAE should be as accurate: if not there's an error in the model form. But indeed, DAEs are as fast as stiff ODE solves (in mass matrix form since it uses the same methods), but if it's a non-stiff ODE then you need #340 in order to convert back down to an ODE for explicit methods.
Also, I think we should change the required arguments of connection to the following:
connection(
sys_from,
sys_to,
edge_weight, # can be any arbitrary metadata, even a function
)::Vector{Equation}
This should take care of weighted connections, which are particularly necessary in neuron simulation. It occurs to me that we could have two constructors - a high level one which constructs the graph with systems for you, and a lower level one where you provide the system for each vertex. (We can't carry this information around in the graph, so we may as well carry it separately in a vector indexed by vertex number). This would allow anisotropic graphs, to selectively introduce defects into systems.
How would we unify the graphs? As far as I'm aware, there's no mechanism for graph fusion in LightGraphs. I imagine that a MetaDiGraph with connection functions or some user dispatch mechanisms on edges would be the best way forward for this.
Wouldn't that just be a degenerate graph, then? I'm not sure if we need to know which nodes are the same, since presumably if you pass two graphs with two coupling mechanisms, you want all of the coupling specified...unless I'm misinterpreting the purpose of this.
Well I want to say "this is node 10", and in graph 1, node 10 has an edge to node 11 so add these equations, in graph 2 it does not so don't add these equations.
Oh, didn't know there was a global index. I thought all indexing was node local because of the edgelists being vector of vector. Okay, then I think we're good.
If we want to tune the strength of a connection after defining the dynamics it's much more convenient to use the DEFunction parameter for that. I think that approach would work here, too?
Not sure what you're mentioning here.
We also allow an array of edge relationships (e.g. most edges are copper lines, some are more sophisticated controllable transformers). We are looking at whether multilayer modelling will allow us to keep things homogeneous per layer.
That's what I plan to do with different graphs: give to graphs each doing something slightly different, instead of 1 graph with different edge types. I think the two are equivalent though.
How does MTK make use of the parallelism?
Many ways. Threading, distributed, and now we're starting to write schedulers to split the graph more effectively. I plan to train a neural network cost model so we auto-split in a way that minimizes the overhead of over-subscribing one thread. We'd cut components down at compile time so that if they are unbalanced we can rebalance the threads. You can find a bunch of other issues open on these topics, and my JuliaCon talk is actually going to be about that part of MTK.
I am really amazed at the performance of the system builder for large equations. I remember having real issues with the very early on when I protoyped some networked systems in MTK (maybe a year ago?). Are you looking into how this scales for very large systems (also in memory use)?
We testing quite frequently on million equation systems. There are funny things we hit in the Julia compiler, but we've been working around them or getting them fixed (the most curious being that splitting + matters a lot when you have 100 terms in a line 😆 ). MTK is also going to be the test bed for SymbolicUtils performance (JuliaSymbolics/SymbolicUtils.jl#61, JuliaSymbolics/SymbolicUtils.jl#62), so file an issue if you hit anything.
If we want to tune the strength of a connection after defining the dynamics it's much more convenient to use the DEFunction parameter for that. I think that approach would work here, too?
Not sure what you're mentioning here.
The edge weights shouldn't be hardcoded in the graph structure, but rather expressed as system parameters, i.e. the p in the f(dx, x, t, p) syntax. That's especially useful when the behaviour of an edge/connection depends on more than one parameter or when you wan't to perform an optimization on them.
Offering both approaches through using MTK parameters as edge weights would be trivial to do. Edit: Ah, I see what you're saying now. There are multiple ways to achieve this - you could not provide weights, and do this optimization in the coupling function.
Offering both approaches through using MTK parameters as edge weights would be trivial to do. Edit: Ah, I see what you're saying now. There are multiple ways to achieve this - you could not provide weights, and do this optimization in the coupling function.
Let's make this concrete for a power grid case, this is something we are looking to do: To parametrize the system you draw current demand at each of the 10.000 vertices from a random distribution. You also have different node types, so every node has a different number of parameters (but they all include the power infeed P). Now you want to simulate what happens when 10 out of 40.000 edges fail, you want to do that by randomly selecting 10 edges and setting their weight to 0. You want to run this experiment 10.000 times.
So to do that, it would be amazing if I could just manipulate parameters like this:
I believe if you build the NetworkSystem to play nice with ArrayPartitions this should be possible. The question is, if you have inhomogeneous vertices where parameters only occur at some nodes but not at others, what do you do then?
(In ND.jl we actually often have callable structs for the vertices that carry around their parameters, and then manipulate parameters directly inside the rhs, less than ideal user experience).
Edit:
For further fun: 200 of the nodes have control capabilities parametrized by a set of control parameters, and we want to tune these control capabilities by optimizing them. So we need a wrapper that takes in an array and then distributes the values to these 200 special nodes.
That should be easy enough (with a couple of caveats). Currently, parameters as arrays don't work so well in MTK (as far as I know) but you should be able to achieve this kind of result with the prototype we have up now.
Currently, parameters as arrays don't work so well in MTK (as far as I know)
I assume you mean ArrayPartition? Yeah, the generated code assumes arrays. MTK wants to flatten the memory layout so it can control it for auto-parallelism. We need better tools to allow the transformation, since in the end it's better off being declarative here. The reason is:
(In ND.jl we actually often have callable structs for the vertices that carry around their parameters, and then manipulate parameters directly inside the rhs, less than ideal user experience).
Exactly this. In an imperative language space, you mix abstraction with memory layout and thus performance. The abstraction of "what things go together" might not necessarily jive with the best way to split computations for parallelism and DAE index reduction. So what we're trying to do here is just internally have everything by index, associate it by name, take values from you and have you cede control of memory layout so that we can choose our own (which can be a requirement for parallelism). There are ways around it, like making Variable{Matrix}, but those just clump in a way that should be sane.
So I think the way to make NetworkSystem play nice with ArrayPartition is to do that outside MTK and have it trace to something for the NetworkSystem, which would lower to code that has all of the right naming and referencing, but is not tied to the memory layout there. That would fix performance on more than a few projects I have found a way into, where the abstraction didn't match how it should be computed.
let's say you make a bunch of circuit things. It would be really nice to be able to specify a graph of connections for that? So I think we're ready for a high level API on it.
Reading this thread I've noticed that there are two different ways to think about network systems, I'm not sure whether everyone is on the same page here:
Each node is represented by a system, each edge represents a identity relation between variables of the connected nodes.
Each node as well as each edge is represented by a system. In this case the edges become somewhat statefull. This is how (we) would model PowerSystems, diffusion networks, ...
I'll refer to the second one in this post. Obviously the version 2 can be transformed into version 1 by substituting each "statefull" edge with a new node between the the connected ondes.
I am from the NetworkDynamics.jl group aswell. Currently we're working on a MTK-Network interface. We have an small working example for this here.
This example works with our own wrappers around ODESystems but works quite similar to the acausal modeling.
There need to be three main components:
systems for the edges,
systems for the nodes,
some kind of "network layer definition": which edge-variables are accessible by the nodes and which node-variables are accessible by the edges.
The last one is quite important because it specifies which kind of connections between the different subsystems need to be defined. It should be possible to have heterogeneous node-systems and edge-systems as long as every subsystem fulfills those definition. For example for power grids it does not matter how your node dynamics work as long as it calculates a voltage. Vice versa it does not matter how the edge dynamic works as long as it calculates a current based on the voltage of the connected nodes.
One important point is the edge aggregation: each node might have a different number of connected edges. There are two ways around this:
introduce some kind of aggregator function/system, which takes all of the surrounding edge variables and sums them up for example (i.e. the node will only see the sum of all current on the connected edges)
Instead of providing a system for the nodes one has to provide some kind of system-generator. Based on the number of incoming edges the system will have different variables lets say @variables edges[1:N] which can be connected.
Another major point is the direction of the edges. This topic actually causes a lot of headache. I'd say the most basic and most important way of modeling is by using directed graphs. Each edge system will take two variables: source_vertex and destination_vertex and calculates the edge_state based on those. Each node should only consider the incoming edges as connected edges.
In the real world those edge functions often have some kind of symmetry/antisymmetry. It might be useful to implement higher level interfaces on top of those purely directed graphs for undirected graphs with symmetry properties. But i think it is important to get the basic interface right first.
introduce some kind of aggregator function/system, which takes all of the surrounding edge variables and sums them up for example (i.e. the node will only see the sum of all current on the connected edges)
In the real world those edge functions often have some kind of symmetry/antisymmetry. It might be useful to implement higher level interfaces on top of those purely directed graphs for undirected graphs with symmetry properties. But i think it is important to get the basic interface right first.
Activity
mforets commentedon May 6, 2020
you may be interested in https://github.com/blegat/HybridSystems.jl which implements hybrid automata.
ChrisRackauckas commentedon May 6, 2020
I'm not sure that's related at all. I plan to add
RegimeSwitchingSystems, but that's a fairly different problem from network dynamics.mforets commentedon May 6, 2020
oops, sorry.. i thought this was about an api for hybrid systems.
ChrisRackauckas commentedon May 6, 2020
Maybe I should clarify a bit. It's for network dynamics where you have for example Lotka-Volterra on a network, so you have the Lotka-Volterra equations for
x_iandy_i, and then you have terms in the differential equations that connect ODE sets i and j.asinghvi17 commentedon May 6, 2020
Let me rerun some simulations - as I recall, I kind of dropped this effort because (a) the DAE solve was quite a bit slower than the handwritten ODE solve, and there were some differences between the DAE and ODE solutions. If the DAE solution is accurate, then this approach makes sense to me; otherwise, I was going to try and build up the graph "by hand", by modifying the equations before creating a system.
ChrisRackauckas commentedon May 6, 2020
The DAE should be as accurate: if not there's an error in the model form. But indeed, DAEs are as fast as stiff ODE solves (in mass matrix form since it uses the same methods), but if it's a non-stiff ODE then you need #340 in order to convert back down to an ODE for explicit methods.
asinghvi17 commentedon May 6, 2020
Also, I think we should change the required arguments of
connectionto the following:This should take care of weighted connections, which are particularly necessary in neuron simulation. It occurs to me that we could have two constructors - a high level one which constructs the graph with systems for you, and a lower level one where you provide the system for each vertex. (We can't carry this information around in the graph, so we may as well carry it separately in a vector indexed by vertex number). This would allow anisotropic graphs, to selectively introduce defects into systems.
asinghvi17 commentedon May 6, 2020
How would we unify the graphs? As far as I'm aware, there's no mechanism for graph fusion in LightGraphs. I imagine that a MetaDiGraph with connection functions or some user dispatch mechanisms on edges would be the best way forward for this.
ChrisRackauckas commentedon May 6, 2020
I don't think we'd unify graphs. But we'd somehow need a canonical numbering system for the nodes.
asinghvi17 commentedon May 6, 2020
As I understand it, LightGraphs handles the node numbering. For example, the constructor for a directed, weighted graph is:
whose vertices are then
1:12. As I understand it, this is the canonical system for all LightGraphs graphs.ChrisRackauckas commentedon May 6, 2020
And if we have a different graph but with the same number of nodes and different edges, how can we node which nodes are the same?
asinghvi17 commentedon May 6, 2020
Wouldn't that just be a degenerate graph, then? I'm not sure if we need to know which nodes are the same, since presumably if you pass two graphs with two coupling mechanisms, you want all of the coupling specified...unless I'm misinterpreting the purpose of this.
ChrisRackauckas commentedon May 6, 2020
Well I want to say "this is node 10", and in graph 1, node 10 has an edge to node 11 so add these equations, in graph 2 it does not so don't add these equations.
asinghvi17 commentedon May 6, 2020
That's just the vertex number, no? Here's some example code which might clarify this.
looks like this:

ChrisRackauckas commentedon May 6, 2020
Oh, didn't know there was a global index. I thought all indexing was node local because of the edgelists being vector of vector. Okay, then I think we're good.
9 remaining items
ChrisRackauckas commentedon May 15, 2020
Not sure what you're mentioning here.
That's what I plan to do with different graphs: give to graphs each doing something slightly different, instead of 1 graph with different edge types. I think the two are equivalent though.
Many ways. Threading, distributed, and now we're starting to write schedulers to split the graph more effectively. I plan to train a neural network cost model so we auto-split in a way that minimizes the overhead of over-subscribing one thread. We'd cut components down at compile time so that if they are unbalanced we can rebalance the threads. You can find a bunch of other issues open on these topics, and my JuliaCon talk is actually going to be about that part of MTK.
We testing quite frequently on million equation systems. There are funny things we hit in the Julia compiler, but we've been working around them or getting them fixed (the most curious being that splitting
+matters a lot when you have 100 terms in a line 😆 ). MTK is also going to be the test bed for SymbolicUtils performance (JuliaSymbolics/SymbolicUtils.jl#61, JuliaSymbolics/SymbolicUtils.jl#62), so file an issue if you hit anything.lindnemi commentedon May 18, 2020
The edge weights shouldn't be hardcoded in the graph structure, but rather expressed as system parameters, i.e. the
pin thef(dx, x, t, p)syntax. That's especially useful when the behaviour of an edge/connection depends on more than one parameter or when you wan't to perform an optimization on them.asinghvi17 commentedon May 18, 2020
Offering both approaches through using MTK parameters as edge weights would be trivial to do. Edit: Ah, I see what you're saying now. There are multiple ways to achieve this - you could not provide weights, and do this optimization in the coupling function.
FHell commentedon May 19, 2020
Let's make this concrete for a power grid case, this is something we are looking to do: To parametrize the system you draw current demand at each of the 10.000 vertices from a random distribution. You also have different node types, so every node has a different number of parameters (but they all include the power infeed P). Now you want to simulate what happens when 10 out of 40.000 edges fail, you want to do that by randomly selecting 10 edges and setting their weight to 0. You want to run this experiment 10.000 times.
So to do that, it would be amazing if I could just manipulate parameters like this:
Is this what you had in mind as well?
I believe if you build the NetworkSystem to play nice with ArrayPartitions this should be possible. The question is, if you have inhomogeneous vertices where parameters only occur at some nodes but not at others, what do you do then?
(In ND.jl we actually often have callable structs for the vertices that carry around their parameters, and then manipulate parameters directly inside the rhs, less than ideal user experience).
Edit:
For further fun: 200 of the nodes have control capabilities parametrized by a set of control parameters, and we want to tune these control capabilities by optimizing them. So we need a wrapper that takes in an array and then distributes the values to these 200 special nodes.
asinghvi17 commentedon May 19, 2020
That should be easy enough (with a couple of caveats). Currently, parameters as arrays don't work so well in MTK (as far as I know) but you should be able to achieve this kind of result with the prototype we have up now.
ChrisRackauckas commentedon May 19, 2020
I assume you mean ArrayPartition? Yeah, the generated code assumes arrays. MTK wants to flatten the memory layout so it can control it for auto-parallelism. We need better tools to allow the transformation, since in the end it's better off being declarative here. The reason is:
Exactly this. In an imperative language space, you mix abstraction with memory layout and thus performance. The abstraction of "what things go together" might not necessarily jive with the best way to split computations for parallelism and DAE index reduction. So what we're trying to do here is just internally have everything by index, associate it by name, take values from you and have you cede control of memory layout so that we can choose our own (which can be a requirement for parallelism). There are ways around it, like making
Variable{Matrix}, but those just clump in a way that should be sane.So I think the way to make NetworkSystem play nice with ArrayPartition is to do that outside MTK and have it trace to something for the NetworkSystem, which would lower to code that has all of the right naming and referencing, but is not tied to the memory layout there. That would fix performance on more than a few projects I have found a way into, where the abstraction didn't match how it should be computed.
ChrisRackauckas commentedon Mar 11, 2021
The design of this should now start to get reconsidered given the acausal modeling. For example, with:
https://mtk.sciml.ai/dev/tutorials/acausal_components/
let's say you make a bunch of circuit things. It would be really nice to be able to specify a graph of connections for that? So I think we're ready for a high level API on it.
hexaeder commentedon Mar 29, 2021
Reading this thread I've noticed that there are two different ways to think about network systems, I'm not sure whether everyone is on the same page here:
I'll refer to the second one in this post. Obviously the version 2 can be transformed into version 1 by substituting each "statefull" edge with a new node between the the connected ondes.
I am from the NetworkDynamics.jl group aswell. Currently we're working on a MTK-Network interface. We have an small working example for this here.
This example works with our own wrappers around ODESystems but works quite similar to the acausal modeling.
There need to be three main components:
The last one is quite important because it specifies which kind of connections between the different subsystems need to be defined. It should be possible to have heterogeneous node-systems and edge-systems as long as every subsystem fulfills those definition. For example for power grids it does not matter how your node dynamics work as long as it calculates a voltage. Vice versa it does not matter how the edge dynamic works as long as it calculates a current based on the voltage of the connected nodes.
One important point is the edge aggregation: each node might have a different number of connected edges. There are two ways around this:
@variables edges[1:N]which can be connected.Another major point is the direction of the edges. This topic actually causes a lot of headache. I'd say the most basic and most important way of modeling is by using directed graphs. Each edge system will take two variables:
source_vertexanddestination_vertexand calculates theedge_statebased on those. Each node should only consider the incoming edges as connected edges.In the real world those edge functions often have some kind of symmetry/antisymmetry. It might be useful to implement higher level interfaces on top of those purely directed graphs for undirected graphs with symmetry properties. But i think it is important to get the basic interface right first.
ChrisRackauckas commentedon Mar 29, 2021
That's what #814 is for.
ChrisRackauckas commentedon Mar 29, 2021
Yes, definitely agreed.
ChrisRackauckas commentedon Feb 23, 2024
It seems there are downstream extensions which do this kind of thing now.
Add `multiconnect`: graph-based connections over node arrays