Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel RLC Circuit #147

Closed
jebej opened this issue Jan 28, 2023 · 2 comments
Closed

Parallel RLC Circuit #147

jebej opened this issue Jan 28, 2023 · 2 comments

Comments

@jebej
Copy link

jebej commented Jan 28, 2023

I can't seem to get a parallel RLC circuit to work. The series circuit works fine, but the paralell version doesn't work properly. I must be doing something wrong, but I haven't been able to figure it out... It would be a good example to add to tests/docs if we can get it to work.

using ModelingToolkit, OrdinaryDiffEq, PyPlot
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant, Sine

R = 0.1
L = 1.0
C = 1.0
V = 1.0
@variables t
@named resistor = Resistor(R=R)
@named inductor = Inductor(L=L)
@named capacitor = Capacitor(C=C)
@named source = Voltage()
@named voltagefun = Constant(k=0)
@named ground = Ground()

# build the circuit by connecting components
series = false
if series
    rlc_eqs = [
        connect(voltagefun.output, source.V)
        connect(source.p, resistor.p)
        connect(resistor.n, inductor.p)
        connect(inductor.n, capacitor.p)
        connect(capacitor.n, source.n, ground.g)
        ]
else # parallel
    rlc_eqs = [
        connect(voltagefun.output, source.V)
        connect(source.p, resistor.p)
        connect(resistor.p, inductor.p, capacitor.p)
        connect(resistor.n, inductor.n, capacitor.n)
        connect(resistor.n, source.n, ground.g)
        ]
end
@named rlc_model = ODESystem(rlc_eqs, t, systems=[resistor, inductor, capacitor, voltagefun, source, ground])

sys = structural_simplify(rlc_model)

u0 = [inductor.i => 2]
prob = ODEProblem(sys, u0, (0, 100.0))
sol = solve(prob, Tsit5())

plot(sol.t, [sol[source.v] sol[inductor.i] sol[capacitor.i]])
@ven-k
Copy link
Member

ven-k commented Feb 21, 2023

It worked for me. Could you retry with the latest MTKStdLib, MTK and OrdinaryDiffEq (; although I can't think of anything specific to this that changed). If the error persists please provide the versions and error stacktrace; it would be easier to reproduce/debug.

@jebej
Copy link
Author

jebej commented Feb 22, 2023

The example above works? I didn't mean that it produces an error, but it didn't oscillate. That's because of the source voltage being maintained at 0, a misunderstanding on my part. The following shows the oscillations I expected:

R = 1/0.1
L = 1.0
C = 1.0
V = 1.0
@variables t
@named resistor = Resistor(R=R)
@named inductor = Inductor(L=L)
@named capacitor = Capacitor(C=C)
@named ground = Ground()

# build the circuit by connecting components
rlc_eqs = [
        connect(resistor.p, inductor.p, capacitor.p)
        connect(resistor.n, inductor.n, capacitor.n)
        connect(resistor.n, ground.g)
        ]

@named rlc_model = ODESystem(rlc_eqs, t, systems=[resistor, inductor, capacitor, ground])

sys = structural_simplify(rlc_model)

u0 = [inductor.i => 2.0]
prob = ODEProblem(sys, u0, (0, 50.0))
sol = solve(prob, Tsit5())

plot(sol.t, [sol[capacitor.v] sol[inductor.i]])

@jebej jebej closed this as completed Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants