# Convergence Issues with Controllable Switch and Square Wave using MTK

**URL:** https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [July 31, 2025, 11:07pm UTC](https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264 "2025-07-31T23:07:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![junmorenodi99](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/junmorenodi99/32/217862_2.png) [@junmorenodi99](https://discourse.julialang.org/u/junmorenodi99)
#### Post date: [July 31, 2025, 11:07pm UTC](https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264/1 "2025-07-31T23:07:46Z")

</div>

Hi everyone,

I am new to the Julia community and I’m interested in the applications of the `ModelingToolkit` package.

In particular, I’m trying to simulate a _switching circuit_ using an _External Control Signal_ and a _Controllable Switch_. The Switch is designed as a non-ideal resistor where the resistance value varies from high to low in response to the Control Signal. I tried a simple test circuit as below with two approaches :

```julia-auto

       +V  
        |  
       ┌┴┐ Control (Square Wave)
       │S│<─────●  
       └┬┘            
        |            
       [R] ← Load resistor R  
        |            
       GND           

```

1. Using a sinusoidal wave as Control Signal. In this case the model works. Here’s the plot of the Voltage on the load and the resistance of the Cotrollable “Switch”:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/1/71abfba76423597c7511625e6ff5030a7879b679.png)

1. Using a square wave as Control Signal. In this case the model has convergence problems. It don’t show any “error” but two warnings:

```julia-auto
┌ Warning: Rosenbrock methods on equations without differential states do not bound the error on interpolations.

```

and

```julia-auto
┌ Warning: At t=0.0, dt was forced below floating point epsilon 5.0e-324, and step error estimate = NaN. Aborting. There is either an error in your model specification or the true solution is unstable (or the true solution can not be represented in the precision of Float64).

```

and the resulting plot is empty :

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

So I’m having some questions as I’m stuck in the implementation

- Which solver and settings (tolerances, max step size) work best for fast switching circuits in ModelingToolkit.jl?
- Are there any references or examples of switching models using MTK?
- Can ModelingToolkit.jl reliably handle non-linear, hybrid systems like this?

Here’s the code of the Circuit model:

```julia-auto
@mtkmodel R_Test begin
    @parameters begin
        RLoad_param = 10, [description = "Output Load (Ohm)"]
        Vin_param = 200, [description = "Vin Souce (Vdc)"]
        f = 100, [description = "Switch input Frequency (Hz)"]
    end

    @components begin
        switch_input = squareWave(frequency = f, amplitude = 1, offset = 0, dutyCycle = 0.3)
        #switch_input = Sine(frequency = f, amplitude = 0.99, offset = 1.0)
        voltage_input = Constant(k = Vin_param)
        switch = IdealSwitch(Ron = 1e-3, Roff = 1)
        source = Voltage()
        RLoad = Resistor(R = RLoad_param)
        ground = Ground()
    end

    @equations begin 
      connect(voltage_input.output, source.V)
      connect(switch_input.output, switch.conduction)
      connect(source.p, switch.p)
      connect(switch.n,RLoad.p)
      connect(source.n, RLoad.n, ground.g) 
    end
end

```

The Switch is defined as

```julia-auto
@mtkmodel IdealSwitch begin
  @extend v, i = oneport = OnePort()  
  
  @parameters begin 
    Ron = 1e-3, [description = "On resistance magnitud"]
    Roff = 1e6, [description = "Off resistance magnitud"]
  end

  @components begin
    conduction = RealInput() 
  end

  @variables begin
    q(t), [description = "Control Signal "]
    R(t), [description = "Resistance "]
  end
  
  @equations begin
    q ~ conduction.u
    R ~ Ron + q * Roff
    v ~ i * R
  end
end 

```

The square wave is defined as

```julia-auto
@component function squareWave(;name, frequency, amplitude = 1, offset = 0, dutyCycle = 0.5)
    @named output = RealOutput()
    pars = @parameters amplitude=amplitude frequency=frequency dutyCycle=dutyCycle
    equation = amplitude * mod1(t, 1/frequency) < dutyCycle/frequency
    eqs = output.u ~ equation
    compose(System(eqs, t, [], pars; name = name), [output])
end 

```

and the solver configuration is

```julia-auto
@mtkcompile sys = R_Test()
prob = ODEProblem(sys, Pair[], (0.0, 100e-3); guesses = [sys.switch.i => 5.0])
sol = solve(prob, Rodas5();
            reltol=1e-8,
            abstol=1e-10,
            dt=1e-7,        
            dtmax=1e-5,     
            saveat=1e-5)

```

Any pointers or references to prior work are greatly appreciated!  
Thanks!

---

<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: [July 31, 2025, 11:51pm UTC](https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264/2 "2025-07-31T23:51:13Z")

</div>

> [@junmorenodi99](#):
>
> - Are there any references or examples of switching models using MTK?
> - Can [ModelingToolkit.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/ModelingToolkit) reliably handle non-linear, hybrid systems like this?

> **[Model building reference · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/API/model_building/#ModelingToolkit.IfLifting)**
>
> Documentation for ModelingToolkit.jl.

It’s still a bit experimental but you can try the iflifting to see if the event handling fixes it. Right now it’s opt-in.

---

<div class="post-metadata">

### Author: ![junmorenodi99](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/junmorenodi99/32/217862_2.png) [@junmorenodi99](https://discourse.julialang.org/u/junmorenodi99)
#### Post date: [August 2, 2025, 2:41pm UTC](https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264/3 "2025-08-02T14:41:26Z")

</div>

Thanks for your help @ChrisRackauckas. I’ve integrated the `iflifting` call into my code but now I’m seeing this error for both control signals:

```julia-auto
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [1]

```

I suspect the problem is how I added `iflifting`, rather than the feature itself… Here’s the change I made:

```julia-auto
#@mtkcompile sys = R_Test()
@named sys_dec = R_Test()
sys = structural_simplify(sys_dec,additional_passes = [IfLifting])

```

Any ideas on how I can debug this? Thanks

---

<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 3, 2025, 1:13am UTC](https://discourse.julialang.org/t/convergence-issues-with-controllable-switch-and-square-wave-using-mtk/131264/4 "2025-08-03T01:13:36Z")

</div>

Open an issue, easier to track it there.
