Hello, everyone!
I have some trouble with simple electric circuit: voltage source and custom RC.
If R isn’t equal to zero everything is good, code works.
using ModelingToolkit, DifferentialEquations
import ModelingToolkitStandardLibrary.Electrical, ModelingToolkitStandardLibrary.Blocks
import ModelingToolkit: t_nounits as t, D_nounits as D
@mtkmodel myCapacitor begin
    @parameters begin
        r  = 0.1
        C = 1e-3
    end
    @variables begin
        v_C(t)
    end
    @extend v, i = oneport = Electrical.OnePort(; i, v)
    @equations begin
        v ~ i*r + v_C
        i ~ C*D(v_C)
    end
end
@mtkmodel mtkModel begin
    @components begin
        cap = myCapacitor(r=0.5, C=1e-3)
        source = Electrical.Voltage()
        voltage_signal = Blocks.Sine(frequency=5.0)
        ground = Electrical.Ground()
    end
    @equations begin
        connect(source.p, cap.p)
        connect(source.n, cap.n, ground.g)
        connect(voltage_signal.output, source.V)
    end
end
@mtkbuild model = mtkModel()
problem = ODEProblem(model, [model.cap.v_C => 0.0], (0, 2.0))
solution = solve(problem, Tsit5(), dt = 1e-3, adaptive=false)
but if R is equal to zero I have a warning.
Warning: Instability detected. Aborting
I understand that the problem is now incorrect and voltage cannot be a state variable anymore. I fixed it by replacing the
v ~ i*r + v_C
equation with the
D(v) ~ D(v_C)
equation.
But it only works correctly if r=0 because I don’t use “r”.
How can I combine two equations in one?
I tried if/else construction but it doesn’t work.
Thanks in advance for any feedback!