# \[ANN\] Jusdl.jl v0.1.2 - Signal-Flow Graphs, Detecting and Breaking Algebraic Loops

**URL:** https://discourse.julialang.org/t/ann-jusdl-jl-v0-1-2-signal-flow-graphs-detecting-and-breaking-algebraic-loops/38773
**Category:** Package Announcements
**Created:** [May 4, 2020, 10:44pm UTC](https://discourse.julialang.org/t/ann-jusdl-jl-v0-1-2-signal-flow-graphs-detecting-and-breaking-algebraic-loops/38773 "2020-05-04T22:44:18Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [May 4, 2020, 10:44pm UTC](https://discourse.julialang.org/t/ann-jusdl-jl-v0-1-2-signal-flow-graphs-detecting-and-breaking-algebraic-loops/38773/1 "2020-05-04T22:44:18Z")

</div>

Hi all,

I am pleased to announce the changes in the new patch release v0.1.2 of [Jusdl](https://github.com/zekeriyasari/Jusdl.jl).

One big change is that a signal-flow graph(which simply a directed graph consisting of nodes and branches) is associated with the model to be simulated. As the model is modified by new adding nodes(i.e components) and new branches(i.e connections), the signal flow graph of the model is modified accordingly to keep track of the changes in the topology of the model.

Here is an example case study from [the docs of Jusdl](https://zekeriyasari.github.io/Jusdl.jl/dev/). Consider the following model block diagram

![model](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6899bda3e371d94085f750573192bc3d08ba716.png)

The construction of the model using Jusdl is as follows. We start with an empty model and add nodes connections to the model.

```julia
using Jusdl 

# Construct an empty model 
model = Model()
 
# Add nodes to the model 
addnode(model, SinewaveGenerator(frequency=2), label=:gen1)
addnode(model, Gain(gain=1), label=:gain1)
addnode(model, Adder((+,+)), label=:adder1)
addnode(model, SinewaveGenerator(frequency=3), label=:gen2)
addnode(model, Adder((+, +, -)), label=:adder2)
addnode(model, Gain(gain=1), label=:gain2)
addnode(model, Writer(), label=:writer)
addnode(model, Gain(gain=1), label=:gain3)

# Add branches to the model
addbranch(model, :gen1 => :gain1, 1 => 1)
addbranch(model, :gain1 => :adder1, 1 => 1)
addbranch(model, :adder1 => :adder2, 1 => 1)
addbranch(model, :gen2 => :adder1, 1 => 2)
addbranch(model, :gen2 => :adder2, 1 => 2)
addbranch(model, :adder2 => :gain2, 1 => 1)
addbranch(model, :gain2 => :writer, 1 => 1)
addbranch(model, :gain2 => :gain3, 1 => 1)
addbranch(model, :gain3 => :adder2, 1 => 3)

```

Note that when a `node` is added, it is given a `label` so that the `node` can be accessed by its `label`. And the syntax, `addbranch(model, src_node_label => dst_node_label, src_node_output_port_subindex => dst_node_inport_subindex` is used to connect the pins (indexed by `src_node_output_port_subindex`) of output port of a source node to the input pins(indexed by `dst_node_inport_subindex`) of the input port of a destination node.

The signal flow of the `model` can be plotted using `signalflow`, which uses the tools provided by [GraphPlot](https://github.com/JuliaGraphs/GraphPlot.jl).

```julia
signalflow(model)

```

 ![signalflow](https://global.discourse-cdn.com/julialang/original/3X/3/f/3ff20ef82e9e0abdd3697c69d173c0284f3bab0b.png)

Associating a signal-flow graph to the model makes it possible to carry out graph-theoretic analysis on the model topology(by using the tools provided by [LightGraphs](https://github.com/JuliaGraphs/LightGraphs.jl)). An example to such an analysis is the inspection and breaking algebraic loops. An algebraic loop is a closed-loop consisting of one or more components whose outputs are directly dependent on their inputs. If algebraic loops exist in a model, the simulation gets stuck because none of the components in the loop can generate output to break the loop. Such a problem can be broken by rearranging the model without algebraic loops, solving the feed-forward algebraic equation of the loop, or inserting a memory component with a certain initial condition anywhere in the loop. Jusdl provides all these loop-breaking solutions. During the inspection stage, in case they are detected, all the loops are broken. Otherwise, a report is printed to notify the user to insert memory components to break the loops.

Note that the model has an algebraic loop consisting of the components `adder2`, `gain2` and `gain3` and during the simulation Jusdl detects and breaks this loop without requiring any user intervention. To see this, let us simulate the model.

```julia
simulate(model)

```

Here are the log messages printed on the console during the simulation. The algebraic loop between the nodes `adder2`, `gain2` and `gain3`, (as nodes are added to the model, an index is assigned to access them, and node indices of `adder2`, `gain2` and `gain3` are 5, 6, and 8, respectively.), is detected and broken during the inspection stage of the simulation.

```julia
[ Info: 2020-05-04T21:39:00.768 Started simulation...
[ Info: 2020-05-04T21:39:00.768 Inspecting model...
┌ Info: The model has algrebraic loops:[[5, 6, 8]]
└ Trying to break these loops...
[Info: Loop [5, 6, 8] is broken
[ Info: 2020-05-04T21:39:00.964 Done.
[ Info: 2020-05-04T21:39:00.964 Initializing the model...
[ Info: 2020-05-04T21:39:01.143 Done...
[ Info: 2020-05-04T21:39:01.144 Running the simulation...
[ Info: 2020-05-04T21:39:01.364 Done...
[ Info: 2020-05-04T21:39:01.364 Terminating the simulation...
[ Info: 2020-05-04T21:39:01.367 Done.

```

When the simulation is completed, the simulation data saved in `writer` file can be read back and any offline data analysis can be performerd. Through its `Plugins` interface, `Jusdl` also provides online(while the model is being simulated) data analysis.

```julia
t, x = read(getnode(model, :writer).component)
plot(t, x)

```

![plot](https://global.discourse-cdn.com/julialang/original/3X/7/1/7146c19f97b82d36c9469725e7a3d3d926979496.png)

The [docs](https://zekeriyasari.github.io/Jusdl.jl/dev/) provides further information and short tutorials about the capabilities of Jusdl.

Jusdl is still in its development phase and any form of contribution is welcome. Please feel free to open an [issue](https://github.com/zekeriyasari/Jusdl.jl/issues) for bug reports, feature requests, new ideas and suggestions etc., or to send a pull request for any bug fixes.
