# Causal.jl vs ModelingToolkit.jl for causal, discrete-time, and Simulink-like systems

**URL:** https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881
**Category:** Modelling & Simulations
**Tags:** package, modelingtoolkit
**Created:** [June 16, 2022, 2:26pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881 "2022-06-16T14:26:41Z")
**Posts on this page:** 17
**Page:** 2

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 12, 2022, 10:26am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/21 "2022-08-12T10:26:13Z")

</div>

> [@tapyu](#):
>
> Exactly, that is my point, and that is why I used the RC circuit as an example. In LTSpice you only need to set the values of the passive components and, when needed, declare the initial values of state variables (capacitor voltage and inductor current). So, LTSpice works without you having to worry about declaring the mathematical function that governs that system.

That sounds a lot like ModelingToolkit?

```julia
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant

R = 1.0
C = 1.0
V = 1.0
@variables t
@named resistor = Resistor(R=R)
@named capacitor = Capacitor(C=C)
@named source = Voltage()
@named constant = Constant(k=V)
@named ground = Ground()

rc_eqs = [
        connect(constant.output, source.V)
        connect(source.p, resistor.p)
        connect(resistor.n, capacitor.p)
        connect(capacitor.n, source.n, ground.g)
        ]

@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, constant, source, ground])
sys = structural_simplify(rc_model)
prob = ODAEProblem(sys, Pair[], (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol, vars = [capacitor.v, resistor.i],
     title = "RC Circuit Demonstration",
     labels = ["Capacitor Voltage" "Resistor Current"])
savefig("plot.png")

```

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

That’s the first tutorial at:

> **[GitHub - SciML/ModelingToolkitStandardLibrary.jl: A standard library of...](https://github.com/SciML/ModelingToolkitStandardLibrary.jl)**
>
> A standard library of components to model the world and beyond - GitHub - SciML/ModelingToolkitStandardLibrary.jl: A standard library of components to model the world and beyond

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 12, 2022, 12:06pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/22 "2022-08-12T12:06:18Z")

</div>

> [@dawbarton](#):
>
> So work is happening to make life simpler for end users but MTK is still quite young and MATLAB/Simulink still has a strong hold over many engineers (and my colleagues unfortunately).

Agreed, though there have already been some nice wins. If you haven’t seen the Instron talk at JuliaCon, check it out.

[![](https://global.discourse-cdn.com/julialang/original/3X/6/e/6ef62f1ee06b7fbe0b4272b71c9c38f594b4f1a2.jpeg "Modeling a Crash Simulation System With ModelingToolkit.jl | Bradley Carman | JuliaCon 2022") ](https://www.youtube.com/watch?v=ChwKqrH8OQU)

It shows some MTK wins, but also some things we’re working on and next directions.

> [@dawbarton](#):
>
> As for causal versus acausal, I’m not sure it’s completely clear cut.

Causal is just a subset of acausal modeling. Causal modeling is acausal modeling where all of the components only have input and output ports, no free ports. If you made ModelingToolkit throw an error on any open component that wasn’t labelled input, output, or internal, then it would be a causal modeling system. Indeed having inputs and outputs labelled makes it easier to throw nice error messages (“Input not connected”, “Two inputs connected”, etc.), but all of those nice debugging messages can (and are) thrown from acausal modeling systems as well (indeed, the purpose for input/output designations in acausal modeling systems is simply for GUIs and error messages, they serve no other purpose).

> [@dawbarton](#):
>
> For many application areas, acausal works very well, however, when you are building models that represent electronic systems the flow based approach of causal can be much easier (particularly dealing with things like different clock rates for different components).

It’s hard to find systems which are truly “easier” to think causally. Take for example your electronic systems example. What direction is the flow? In general, the flow goes in the direction of the lower voltage. That can be dependent on parameters and even can change in time. The direction of flow is not clear cut, which is why circuit systems usually want a way to just “connect” components. This is acausal, and then SPICE system use something called modal node analysis (MNA) to find a causal arrow. But a generalized form of MNA is Pantelides algorithm for index reduction because connecting electrical circuits generates Index-2 DAEs, and then finding a causal flow is a subset of what `structural_simplify` does with index reduction and tearing.

Note that Julia Computing is using this as a way to build a differentiable SPICE called Cedar.

[https://cedar-eda.com/](https://cedar-eda.com/)

It is of course built on ModelingToolkit and uses its algorithms to transform circuit components into ODEs to simulate.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 12, 2022, 12:46pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/23 "2022-08-12T12:46:44Z")

</div>

> [@ChrisRackauckas](#):
>
> It’s hard to find systems which are truly “easier” to think causally.

All control systems are inherently causal, the same goes for signal-processing systems. For many engineers, such systems are what you are mostly modeling, and then the causal approach may very well feel more intuitive.

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [August 12, 2022, 1:23pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/24 "2022-08-12T13:23:37Z")

</div>

> [@ChrisRackauckas](#):
>
> Agreed, though there have already been some nice wins. If you haven’t seen the Instron talk at JuliaCon, check it out.

Nice! I hadn’t spotted that one, thanks for pointing it out.

> [@ChrisRackauckas](#):
>
> Note that Julia Computing is using this as a way to build a differentiable SPICE called Cedar.

I’ve been keeping my eye out for this as there are some projects I’m involved with that it could be useful for around the design/optimisation of power converters. I’m guessing that it is commercial only - are there any aspects that might be made open to researchers?

---

<div class="post-metadata">

### Author: ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)
#### Post date: [August 12, 2022, 2:39pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/25 "2022-08-12T14:39:47Z")

</div>

Nice! It would be amazing having something like that in Julia!

---

<div class="post-metadata">

### Author: ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)
#### Post date: [August 12, 2022, 3:35pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/26 "2022-08-12T15:35:12Z")

</div>

> [@ChrisRackauckas](#):
>
> It’s hard to find systems which are truly “easier” to think causally.

I tend to disagree. From a modelling perspective, acausal modelling may be the way to go since it is a strict generalization of causal systems, in addition to solving it faster due to the transformations that reduce the index of the DAE.

However, engineering systems (continuous and discrete control systems, communication systems, signal processing, etc…) are inherently causal as we often build more complex systems by joining isolated-but-simple blocks, which can nicely be represented as block diagrams that relates inputs and output, as it is done in Simulink. Since [“causal modelling is also known as explicit modelling, block-oriented modelling”](https://arxiv.org/pdf/1909.00484.pdf), it is the natural way of modelling more complex systems in engineering. A challenging engineering problem is far cry from an RC model and looks like something like this

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/d/7ddb4ffa85bbea90d1357e5e51d9439f03aa02d0.png)  
I could implement such a system in Simulink. But in Julia, I do not even know if it is possible.

To sum things up, _causal vs. acausal modeling is a pointless discussion for the end user. It does not matter how the algorithm solves it under the hood as long as (s)he can understand how is the input-output relationship of each block diagram_. Furthermore, a GUI such as [pysimCoder](https://github.com/robertobucher/pysimCoder) would be even better.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 12, 2022, 3:43pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/27 "2022-08-12T15:43:15Z")

</div>

> [@tapyu](#):
>
> I could implement such a system in Simulink. But in Julia, I do not even know if it is possible.

Why not write exactly the same thing using the blocks library?

[https://mtkstdlib.sciml.ai/stable/API/blocks/](https://mtkstdlib.sciml.ai/stable/API/blocks/)

This is just connecting basic blocks and other blocks with only inputs and outputs? I agree that it can have an extra level of complexity to know that acausal exists in the system, but using the causal subset with causal blocks gives a causal model.

---

<div class="post-metadata">

### Author: ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)
#### Post date: [August 12, 2022, 3:59pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/28 "2022-08-12T15:59:11Z")

</div>

I’ll take a look, thanks for the reference.

---

<div class="post-metadata">

### Author: ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)
#### Post date: [August 12, 2022, 5:55pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/29 "2022-08-12T17:55:14Z")

</div>

> [@ChrisRackauckas](#):
>
> This is just connecting basic blocks and other blocks with only inputs and outputs?

@ChrisRackauckas Not really, a system like that works with different sampling rate, is it possible to define different sampling rate?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 12, 2022, 6:04pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/30 "2022-08-12T18:04:24Z")

</div>

> [@tapyu](#):
>
> @ChrisRackauckas Not really, a system like that works with different sampling rate, is it possible to define different sampling rate?

You use the discrete update operator.

> <https://github.com/SciML/ModelingToolkit.jl/blob/1392a7cab5eff1853b79197a24ae931b085007dd/test/odesystem.jl#L409-L420>

And with multiple clocks:

> <https://github.com/SciML/ModelingToolkit.jl/blob/master/test/discretesystem.jl#L87-L116>

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [August 12, 2022, 8:52pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/31 "2022-08-12T20:52:17Z")

</div>

What would be a (mature enough) platform for desktop GUI development in Julia? In particular, do you think that the [QML.jl](https://github.com/barche/QML.jl) package for Qt5 development (that you discuss elsewhere here at Discourse) is usable for making something like Julia version of pysimCoder? I am myself not in GUI development, just asking if the time is ripe for encouraging students for such adventures.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 13, 2022, 9:57am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/32 "2022-08-13T09:57:16Z")

</div>

Well, QML.jl could definitly be used, and I like it, mainly because I have years of QT development experiance. But on the other hand there are not enough maintainers for this package, and it usually takes some time before it works with new Julia versions. In the moment it only works with Julia 1.7, but not with 1.8rc4.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 13, 2022, 10:20am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/33 "2022-08-13T10:20:54Z")

</div>

> [@ufechner7](#):
>
> I like it, mainly because I have years of QT development experiance. But on the other hand there are not enough maintainers for this package

Sounds like you’re a good candidate for a new maintainer 😊

Jokes aside, people with both experience and motivation often make good maintainers of open-source software.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [August 13, 2022, 11:22am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/34 "2022-08-13T11:22:02Z")

</div>

> [@baggepinnen](#):
>
> Sounds like you’re a good candidate for a new maintainer 😊

Well, this year I will still be busy with improving the quality of my own packages, in particular [KiteControllers.jl](https://github.com/aenarete/KiteControllers.jl) If you look at this package you can see that I did a lot of control design with Simulink and later ported it to Julia…

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 4, 2022, 9:27am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/35 "2022-12-04T09:27:11Z")

</div>

There has been some improvements lately to MTK and MTKstdlib that makes life easier for control engineers, see, e.g., this tutorial [building and analyzing the closed-loop properties of a speed-controlled DC motor](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/tutorials/dc_motor_pi/) using MTK stdlib. This should hopefully show that, given a block diagram, building the corresponding control system in MTK using standard-library components like PID controllers etc. is rather straightforward.

See also this tutorial that is [building and auto tuning a cascade PI + P servo-control system](https://help.juliahub.com/juliasimcontrol/dev/tuning_objectives/#Automatic-tuning-of-structured-controllers) and this tutorial [with some general illustrations on how to use MTK for control-purposes](https://help.juliahub.com/juliasimcontrol/dev/examples/mtk_control/).

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [January 2, 2023, 5:35pm UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/36 "2023-01-02T17:35:19Z")

</div>

> [https://mtkstdlib.sciml.ai/stable/API/blocks/](https://mtkstdlib.sciml.ai/stable/API/blocks/)

Updated link for future readers: [Basic Blocks · ModelingToolkitStandardLibrary.jl](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/blocks/)

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [August 18, 2023, 8:41am UTC](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881/37 "2023-08-18T08:41:27Z")

</div>

Here is a relevant and recent youtube video from @ChrisRackauckas:  
[https://youtu.be/ZYkojUozeC4](https://youtu.be/ZYkojUozeC4)

[Previous page](https://discourse.julialang.org/t/causal-jl-vs-modelingtoolkit-jl-for-causal-discrete-time-and-simulink-like-systems/82881.md?page=1)
