# Ideal diode switching with multiple voltage sources in ModelingToolkit , solver gets stuck at t=0

**URL:** <https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [May 12, 2026, 8:24am UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086 "2026-05-12T08:24:44Z")\
**Posts on this page:** 9\
**Page:** 3

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [May 15, 2026, 12:14pm UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/41 "2026-05-15T12:14:46Z")

</div>

You are absolutely right, and that is a fair point. What we have learned throughout this process about numerical simulation has already been very valuable in itself (things we would never have had to think about using commercial tools). And from a research group perspective, the right attitude is exactly that: contributing to making the tools better rather than just being end users. That is precisely why we intend to keep working on this and contribute back to the ecosystem wherever we can.

---

<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:** [May 15, 2026, 6:07pm UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/42 "2026-05-15T18:07:00Z")

</div>

Some progress: I simplified the coupled system, and now all works fine:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d30b1557b2a2707b7efa8bf7a4a3c48fa577a56.png)

This is with a two-phase rectifier. I still have the warning:

```julia
┌ Warning: Did not converge after `maxiters = 100` substitutions. Either there \
│ is a cycle in the rules or `maxiters` needs to be higher.
└ @ Symbolics ~/.julia/packages/Symbolics/oZEAe/src/variable.jl:451

```

But this warning does not affect the result. I also created a flattened version of the same system (without components). With that version there is no warning.

I currently use this solver call:

```julia
sol = solve(prob, Rodas5P(), abstol = 1e-9, reltol = 1e-12, dense = false, saveat = 0.0001, maxiters = 10000000)

```

Another solver might work better, but this already works.

**Update:**  
The original 3-phase example now works, too:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/51d3e40ec135b5117d5a284f38de44174cf26cda.png)

I had to switch to the RadauIIA5 solver:

```julia-auto
sol = solve(prob, RadauIIA5(), abstol = 1e-8, reltol = 1e-8, saveat = 0.0001)

```

You can find the code at: [GitHub - ufechner7/MTKCircuits.jl · GitHub](https://github.com/ufechner7/MTKCircuits.jl)

**Update:**  
It still does not work for the original value of `aero = Constant(k = -97)`. It does work for k= -9.7, though.

---

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [May 15, 2026, 6:52pm UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/43 "2026-05-15T18:52:18Z")

</div>

Great results! The waveforms look really clean, especially the 3-phase case.

Did you manage to pinpoint what was actually causing the issue? Was it something related to the topology of the system (e.g. how the diode switching interacts with the connections), or does it come down entirely to solver choice and configuration? I’m asking because the fact that `RadauIIA5` works while other solvers get stuck suggests the problem might be more about stiffness handling during the switching events rather than a structural issue with the model itself, but curious if you dug deeper into it.

---

<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:** [May 15, 2026, 7:07pm UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/44 "2026-05-15T19:07:53Z")

</div>

> [@JaviJR](#):
>
> I’m asking because the fact that `RadauIIA5` works while other solvers get stuck suggests the problem might be more about stiffness handling during the switching events rather than a structural issue with the model itself, but curious if you dug deeper into it.

Yes, stiffness handling during switching events is the main problem. So the problem is not solved yet. It still fails if the generator torque and thus the voltage become too high.

**UPDATE:**

The simulation now also works with the original parameters.

For higher DC voltages (here 928 V), it is necessary to add a small (here 650 pF) capacitor in parallel to the diodes, and also a very small resistor (here 1 mOhm) in series.

Example:

```julia

include("examples/coupled_system2.jl)

```

The following solver parameters were used:

```julia
sol = solve(prob, RadauIIA5(κ = 0.005), abstol = 1e-7, reltol = 1e-8, saveat = 0.000025, maxiters = 5e6)

```

**Update II:**  
Some of the examples did not work on Windows. Now they do. We had to add initial guesses. Furthermore, there is now a `bin/install` script and a script to create a system image.

---

<div class="post-metadata">

**Author:** ![GeorgeGkountouras](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@GeorgeGkountouras](https://discourse.julialang.org/u/GeorgeGkountouras)\
**Post date:** [May 19, 2026, 6:16pm UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/45 "2026-05-19T18:16:52Z")

</div>

> [@langestefan](#):
>
> Just tagging @GeorgeGkountouras in case he hasn’t seen this yet

Thanks! I was travelling at the time, but I’m keeping tabs on the topic. Since it has been marked resolved, I’ll chime in with my experience with audio circuits.

Some general tips:

- **Successful simulation:** ramp sources from zero, use known starting solution from SPICE, tune tolerances, switch solvers
- **Sometimes helps:** serial/parallel R for diodes, clamping exponentials
- **Initialization:** I haven’t seen ramping fail for any audio circuit yet
- **Agents:** use them for mechanical workflows which are not quite search-and-replace, I used to do this manually for years 🥲
- **Switching:** use the DiffEq event system, use known good runs for `affect!()` state projections, change Newton’s method to Halley’s method as a get-out-of-jail-free card (it behaves better away from the root, which is where switching sends you)

> [@JaviJR](#):
>
> Honestly, we are not sure at this point whether this is a fundamental limitation of how MTK handles simultaneous switching events, or whether we are simply missing the right way to set things up. Tools like PSCAD or PSpice have dedicated teams and decades of development behind their switching solvers, so it is understandable that they handle this kind of circuit more robustly, but it is still disappointing to hit this wall in MTK, especially given how well everything else has worked for us so far.

I think PSpice/Simulink are less rigorous than the DAE-based MTK simulation. This can be an advantage if you are interested in a lax-but-close-enough result that always outputs something.

For non-pathological cases (e.g. floating ideal transformers would fail!), MTK can be made to work, but it’s going to annoy you with its rigor in plenty of large, practical circuits.

> [@JaviJR](#):
>
> At this point we are not sure whether this is a fundamental limitation of MTK, whether tools like Simulink or PSpice are doing something under the hood to handle this case, or whether there is simply a bug somewhere in our code that we have not spotted. Any ideas would be greatly appreciated.

The DAE-based equivalent is not Simulink but Simscape.

---

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [May 20, 2026, 6:02am UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/46 "2026-05-20T06:02:15Z")

</div>

Thank for the tips! They are really useful and give us a clearer picture of what to expect from MTK in this kind of application.

With all the contributions in this thread we have managed to get the diode-based circuits working reasonably well, especially rectifiers, although we are still exploring how well they scale to more complex topologies. On top of that we are starting to test IGBT-based systems, which add another layer of complexity since EMT simulations of power electronics typically involve carrier signals in the kHz range, we are curious to see how MTK handles that.

As @ufechner7 has set up a GitHub repository to collect these models, I plan to keep uploading more implementations there as we make progress, so that anyone facing similar problems has something to work from. The next few weeks will be quite busy with other commitments and some travel, but I will update the repo whenever I get a free moment.

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [May 20, 2026, 8:49am UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/47 "2026-05-20T08:49:48Z")

</div>

Nice! Making better circuit simulations in MTK will help a lot of applications. My work on solar PV will also benefit from this, since it is essentially another diode-based circuit.

---

<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:** [May 20, 2026, 10:48am UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/48 "2026-05-20T10:48:33Z")

</div>

> [@JaviJR](#):
>
> On top of that we are starting to test IGBT-based systems, which add another layer of complexity since EMT simulations of power electronics typically involve carrier signals in the kHz range, we are curious to see how MTK handles that.

If you want to simulate switching-mode power converters, you should use a linearized state-space representation as PLECS does. See this very old paper: [PLECS-piece-wise linear electrical circuit simulation for Simulink | IEEE Conference Publication | IEEE Xplore](https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=794588)

---

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [May 20, 2026, 10:59am UTC](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086/49 "2026-05-20T10:59:57Z")

</div>

Thanks for the reference! I will have a look at it. Once I have new results I will let you know.

[Previous page](https://discourse.julialang.org/t/ideal-diode-switching-with-multiple-voltage-sources-in-modelingtoolkit-solver-gets-stuck-at-t-0/137086.md?page=2)
