# ModelingToolkit's structural\_simplify fails on LI-cutset simplification

**URL:** <https://discourse.julialang.org/t/modelingtoolkits-structural-simplify-fails-on-li-cutset-simplification/57852>\
**Category:** Modelling & Simulations\
**Created:** [March 24, 2021, 9:19am UTC](https://discourse.julialang.org/t/modelingtoolkits-structural-simplify-fails-on-li-cutset-simplification/57852 "2021-03-24T09:19:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 24, 2021, 9:19am UTC](https://discourse.julialang.org/t/modelingtoolkits-structural-simplify-fails-on-li-cutset-simplification/57852/1 "2021-03-24T09:19:22Z")

</div>

I modified the [example](https://mtk.sciml.ai/stable/tutorials/acausal_components/) on acausal component modelling from [ModelingToolkit](https://github.com/SciML/ModelingToolkit.jl) and I get internal tearing error. In my modification I have a series connected V+R+L+L circuit.

The series connection of inductors/current sources or capacitor/voltage sources is a known problem when solving electrical circuits (see e.g. Chen et al. “A practical regularization technique for modified nodal analysis in large-scale time-domain circuit simulation.” _IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems_ 31.7 (2012): 1031-1040).

Is there any way I can help the `structural_simplify` in simplifying the system?

```julia
using ModelingToolkit, DifferentialEquations

function Pin(;name)
    @variables v(t) i(t)
    ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
end
  
function Ground(;name)
    @named g = Pin()
    eqs = [g.v ~ 0]
    ODESystem(eqs, t, [], [], systems=[g], name=name)
end

function Resistor(;name, R = 1.0)
    val = R
    @named p = Pin()
    @named n = Pin()
    @variables v(t)
    @parameters R
    eqs = [
           v ~ p.v - n.v
           0 ~ p.i + n.i
           v ~ p.i * R
          ]
    ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
end

function Inductor(; name, L = 1.0)
    val = L
    @named p = Pin()
    @named n = Pin()
    @variables v(t) i(t)
    @parameters L
    D = Differential(t)
    eqs = [
           v ~ p.v - n.v
           0 ~ p.i + n.i
           i ~ p.i
           D(i) ~ v / L
          ]
    ODESystem(eqs, t, [v, i], [L], systems=[p, n], defaults=Dict(L => val), name=name)
end

function ConstantVoltage(;name, V = 1.0)
    val = V
    @named p = Pin()
    @named n = Pin()
    @parameters V
    eqs = [
           V ~ p.v - n.v
           0 ~ p.i + n.i
          ]
    ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
end

function connect_pins(ps...)
    eqs = [
           0 ~ sum(p->p.i, ps) # KCL
          ]
    # KVL
    for i in 1:length(ps)-1
        push!(eqs, ps[i].v ~ ps[i+1].v)
    end

    return eqs
end

@parameters t

@named V1 = ConstantVoltage(V=10.0)
@named R1 = Resistor(R=1.0)
@named L1 = Inductor(L=8.0e-9)
@named L2 = Inductor(L=2.0e-9)
@named GND = Ground()

ckt = [
       connect_pins(V1.p, R1.p)
       connect_pins(R1.n, L1.p)
       connect_pins(L1.n, L2.p)
       connect_pins(V1.n, L2.n, GND.g)
      ]

@named ckt_model = ODESystem(ckt, t, systems=[V1, R1, L1, L2, GND])

sys = structural_simplify(ckt_model)

```

I get this error message when running this code:

```nohighlight
ERROR: 

... Internal error in Tearing.jl: vs[1] = 0.

```

Run with Julia 1.5.4 and `Project.toml`:

```nohighlight
[deps]
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"

```

---

<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:** [March 24, 2021, 5:09pm UTC](https://discourse.julialang.org/t/modelingtoolkits-structural-simplify-fails-on-li-cutset-simplification/57852/2 "2021-03-24T17:09:45Z")

</div>

Can you post this as an issue instead?

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 24, 2021, 8:42pm UTC](https://discourse.julialang.org/t/modelingtoolkits-structural-simplify-fails-on-li-cutset-simplification/57852/3 "2021-03-24T20:42:42Z")

</div>

Done: [structural\_simplify error #931](https://github.com/SciML/ModelingToolkit.jl/issues/931)
