# Modeling piecewise linear diodes (discontinuous behaviors) with Dyad?

**URL:** <https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit, dyad\
**Created:** [June 20, 2025, 12:08pm UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058 "2025-06-20T12:08:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [June 20, 2025, 12:08pm UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/1 "2025-06-20T12:08:47Z")

</div>

Hello,

In the light of [Dyad announcement](https://discourse.julialang.org/t/ann-dyad-a-new-language-to-make-hardware-engineering-as-fast-as-software/129996), and perhaps as a continuation of the [Ideal Diode and Ideal Switch instability problem](https://discourse.julialang.org/t/ideal-diode-and-ideal-switch-instability-problem/107769) or [Diode model not working as expected with ModelingToolkit](https://discourse.julialang.org/t/diode-model-not-working-as-expected-with-modelingtoolkit/95125) discussions, I’d like to know how Dyad (or I guess just MTK down the road) deals with the following case which isn’t well managed by the present OpenModelica Compiler (OMC): when a particular parameter value makes the problem singular because it changes the order of the ODE.

Here is below the Modelica code (taken from this [exercise](http://xn--ole-9la.net/courses/modelica/11-discrete-eq.html#state-event-diode-rectifier)), which I’m assuming can be translated quite directly to Dyad (if the `if`/`then`/`else` piecewise function definition syntax is supported, which I didn’t check). The diode is modeled almost like the Modelica Standart Library diode, a resistance which value depends in the sign of the current.

From the way OMC works (first model compilation for “any” parameter value, then simulation as separate stage for a given parameter value), this models compiles fine and run fine _as long as_ Ron\>0. However, the simulations makes a runtime “division by zero” error for Ron=0.

In particular, I’ve seen that Dyad has the `structural parameter` and `final parameter` [syntax](https://help.juliahub.com/dyad/dev/manual/syntax.html#Syntax), while Modelica doesn’t have `structural`. Would `structural` be the way to model the case Ron=0?

To make things clearer, perhaps I should split my questions into two:

1. forgetting about the piecewise linear aspect (no more if/else definition, instead replacing the diode by a constant resistor: vd=Ron\*i), how does the Dyad toolchain handles the case Ron=0?
2. about the piecewise linear aspect, is the `vd = if i>0 then Ron*i else Roff*i` syntax supported?
  - and if yes, is there a zero-crossing event generation (it seems in was not the case in [Feb 2023](https://discourse.julialang.org/t/diode-model-not-working-as-expected-with-modelingtoolkit/95125/4))?

 ![diode_rectifier](https://global.discourse-cdn.com/julialang/original/3X/0/1/018fb9bc8acfe201c08693e5732d37c01f79b062.png)

```Modelica
model DiodeRectifier
  import Modelica.SIunits.{Voltage, Current, Resistance, Conductance, Capacitance, Frequency};
  import Modelica.Constants.pi;
  
  Voltage u, vd, vr, vc;
  Current i, ir, ic;
  parameter Resistance R=1;
  parameter Resistance Ron=1e-2 "R closed diode";
  parameter Resistance Roff=1e3 "R open diode";
  parameter Capacitance C=1;
  parameter Frequency f=3;
  
equation
  /*KCL, KVL*/
  vr = vc "R//C";
  u = vd+vc "mesh";
  i = ir+ic "node";
  
  /*Components*/
  u = sin(2*pi*f*time);
  vr = R*ir;
  C*der(vc) = ic;
  /*Diode: long syntax version*/
  /*if i>0 then
    vd = Ron*i;
  else
    vd = Roff*i;
  end if;*/
  /*Diode: compact syntax alternative*/
  vd = if i>0 then Ron*i else Roff*i;

end DiodeRectifier;

```

---

<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:** [June 20, 2025, 1:10pm UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/2 "2025-06-20T13:10:44Z")

</div>

Yeah I have a solution coming here, it’s actually not symbolic but instead to build solvers capable of handling the switching behavior. So it’s an MIT project for DifferentialEquations.jl, but something that will be targeted through Dyad. Give it about a year though since it’s a bit more researchy, but we have a concrete plan on the exact algorithm.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [June 20, 2025, 1:29pm UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/3 "2025-06-20T13:29:15Z")

</div>

Great news! Let’s see in a year then.  
Only, I keep in mind that back in 2001, Michael Tiller wrote in his [first Modelica book](https://link.springer.com/book/10.1007/978-1-4615-1561-6) that modeling discrete events such as mechanical backlash was complicated but was about to get solved in the near future 😉

---

<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:** [June 20, 2025, 3:35pm UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/4 "2025-06-20T15:35:59Z")

</div>

The modelica community kept trying to do symbolic things while keeping DASSL. The fundamental issue is DASSL.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [June 23, 2025, 9:17am UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/5 "2025-06-23T09:17:44Z")

</div>

OK, so there is ongoing work on a switched system solver then. Good to know.

Some months ago, a nearby researcher @benoit_caillaud pointed me to the work of one of his colleague: the nonsmooth ODE solver [Siconos](https://nonsmooth.gricad-pages.univ-grenoble-alpes.fr/siconos/). Very nice contact mechanics simulation videos. I’ve never tried it though. From what I got, the downside is that it is a first order ODE solver.

> **[Vincent Acary INRIA](https://tripop.inrialpes.fr/people/acary/software.html)**

---

<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:** [June 23, 2025, 9:45am UTC](https://discourse.julialang.org/t/modeling-piecewise-linear-diodes-discontinuous-behaviors-with-dyad/130058/6 "2025-06-23T09:45:47Z")

</div>

We have something similar to that going, but that’s a different project. Yeah that drops to first order and makes some other concessions. It’s a bit heavy handed to go all the way to non-smooth, but that is a fun idea. The BVP/NonlinearSolve work is building towards something of that sort though.
