You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using ModelingToolkit, DifferentialEquations
@parameters t
# a point in the circuit with a given voltage and current@connectorfunctionPin(; name)
sts =@variablesv(t)=1.0i(t)=1.0ODESystem(Equation[], t, sts, []; name)
end# ground pin, where voltage is 0functionGround(; name)
@named g =Pin()
eqs = [g.v ~0.]
compose(ODESystem(eqs, t, [], []; name), g)
end# abstraction for all 2-pin componentsfunctionOnePort(; name)
# two pins@named p =Pin()
@named n =Pin()
# component has a voltage across it, and current through
sts =@variablesv(t)=1.0i(t)=1.0
eqs = [
v ~ p.v - n.v # KVL0.~ p.i + n.i # KCL
i ~ p.i # Current through component is current through +ve pin
]
compose(ODESystem(eqs, t, sts, []; name), p, n)
endfunctionResistor(; name, R =1.0)
# 2 pin component@named oneport =OnePort()
@unpack v, i = oneport
ps =@parameters R=R
eqs = [
v ~ i * R
]
extend(ODESystem(eqs, t, [], ps; name), oneport)
endfunctionCapacitor(; name, C =1.0)
@named oneport =OnePort()
@unpack v, i = oneport
ps =@parameters C=C
D =Differential(t)
eqs = [
D(v) ~ i / C
]
extend(ODESystem(eqs, t, [], ps; name), oneport)
endfunctionInductor(; name, L =1.0)
@named oneport =OnePort()
@unpack v, i = oneport
ps =@parameters L=L
D =Differential(t)
eqs = [
D(i) ~ v / L
]
extend(ODESystem(eqs, t, [], ps; name), oneport)
endfunctionDCVoltageSource(; name, V =1.0)
@named oneport =OnePort()
@unpack v = oneport
ps =@parameters V=V
eqs = [
V ~ v
]
extend(ODESystem(eqs, t, [], ps; name), oneport)
end# V = Vm sin(ωt+ϕ)functionACVoltageSource(; name, Vm =1.0, ω =2π, ϕ =0)
@named oneport =OnePort()
@unpack v, i = oneport
ps =@parameters Vm=Vm ω=ω ϕ=ϕ
eqs = [
v ~ Vm *sin(ω * t + ϕ)
]
extend(ODESystem(eqs, t, [], ps; name), oneport)
endfunction ModelingToolkit.connect(::Type{Pin}, ps...)
eqs = [
0.~sum(p->p.i, ps) # KCL
]
# KVLfor i in1:length(ps)-1push!(eqs, ps[i].v ~ ps[i+1].v)
endreturn eqs
end@named acsrc =ACVoltageSource(Vm =10, ω =20π)
@named res =Resistor(R =5.)
@named cap =Capacitor(C =5e-6)
@named gnd =Ground()
eqs = [
connect(acsrc.p, res.p)
connect(res.n, cap.p)
connect(cap.n, gnd.g, acsrc.n)
]
@named acmodel =compose(ODESystem(eqs, t), [res, cap, acsrc, gnd])
sys =structural_simplify(acmodel)
u0 = [
cap.v =>0.0,
res.i =>0.0,
]
prob =ODEProblem(sys, u0, (0., 50.))
sol =solve(prob, Rosenbrock23())
plot(sol) initially gave:
Which doesn't look right. However, I restarted my REPL and got this:
Which makes sense. I'm not entirely sure what went wrong initially.
There were some other intermediate plots with other forms of weird output with other solvers, but since I was working in a REPL, I didn't save them
The text was updated successfully, but these errors were encountered:
@ChrisRackauckas
I tried to extend the https://mtk.sciml.ai/stable/tutorials/acausal_components/ example, and tried playing around with the above circuit. Following is my code:
plot(sol)
initially gave:Which doesn't look right. However, I restarted my REPL and got this:

Which makes sense. I'm not entirely sure what went wrong initially.
There were some other intermediate plots with other forms of weird output with other solvers, but since I was working in a REPL, I didn't save them
The text was updated successfully, but these errors were encountered: