Skip to content

Instantly share code, notes, and snippets.

@ashwani-rathee
Created December 6, 2023 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwani-rathee/f6d3e14a5f52f00e371387b1fc3caf00 to your computer and use it in GitHub Desktop.
Save ashwani-rathee/f6d3e14a5f52f00e371387b1fc3caf00 to your computer and use it in GitHub Desktop.
RC Circuits
using Revise
using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks: RealInput, Step, Sine, Cosine
using ModelingToolkitStandardLibrary.Electrical
using OrdinaryDiffEq
using Plots
@parameters t
@component function ControlledSwitch(; name)
@named n = Pin()
@named p = Pin()
@named u = RealInput()
eqs = [ 0 ~ ifelse(u.u > 0.05, n.v - p.v, n.i)
0 ~ ifelse(u.u > 0.05, n.i + p.i, p.i)
]
ODESystem(eqs, t, [], []; name, systems=[p, n, u])
end
function plot_controlled_switch()
@named vcc = Voltage()
@named resistor = Resistor(R=1)
@named resistor2 = Resistor(R=0.0001)
@named resistor3 = Resistor(R=0.0001)
@named cap = Capacitor(C=4)
@named ground = Ground()
@named switch1 = ControlledSwitch()
@named control1 = Cosine(;frequency = 0.01)
@named switch2 = ControlledSwitch()
@named control2 = Cosine(;frequency = 0.01, phase = π)
ps = @parameters supply_voltage=12.0
eqs = [
connect(vcc.p, resistor2.p)
connect(resistor2.n, switch1.p)
connect(switch1.n, resistor.p)
connect(switch2.n, resistor.p)
connect(resistor.n, cap.p)
connect(switch2.p, resistor3.p)
connect(vcc.n, resistor3.n, cap.n, ground.g)
vcc.V.u ~ supply_voltage
connect(switch1.u, control1.output)
connect(switch2.u, control2.output)
]
@named switch_sys = ODESystem(eqs, t, [], ps;
systems = [vcc, resistor2, resistor, resistor3, switch1, switch2, cap, ground, control1, control2])
sys = structural_simplify(switch_sys)
prob = ODAEProblem(sys, [], (0, 200.0))
sol = solve(prob, Tsit5())
plot(sol, idxs = [cap.v, resistor.i, control1.output.u>0.05, control2.output.u > 0.05],
title = "RC charging and Discharging!",
labels = ["Capacitor Voltage(V)" "Resistor Current(A)" "Switch 1 state" "Switch 2 state"])
end
plot_controlled_switch()
@ashwani-rathee
Copy link
Author

image_360

@ashwani-rathee
Copy link
Author

image_480

@ashwani-rathee
Copy link
Author

left one is switch 1 and upper top one is switch 2

@ashwani-rathee
Copy link
Author

Low pass filter:

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

R = 1.0
C = 1.0
V = 1.0
@variables t
@named resistor = Resistor(R = R)
@named capacitor = Capacitor(C = C, v = 0.0)
@named source = Voltage()
@named waveform = Sine(;frequency=0.5)
@named ground = Ground()
@named volt_sense = VoltageSensor()

rc_eqs = [connect(waveform.output, source.V)
    connect(source.p, resistor.p)
    connect(resistor.n, capacitor.p, volt_sense.p)
    connect(volt_sense.n, capacitor.n, source.n, ground.g)]

@named rc_model = ODESystem(rc_eqs, t,
    systems = [resistor, capacitor, volt_sense, waveform, source, ground])
sys = structural_simplify(rc_model)
prob = ODAEProblem(sys, Pair[], (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol, idxs = [source.v, volt_sense.v],
    title = "RC Low Pass Filter",
    labels = ["vin" "vout"])
savefig("plot.png");

f = 1
w = 2 * π * f 
C = 1
R = 1
xc = 1/(w * C)
vout = (abs(xc)/(abs(xc) + R)) * vin

@ashwani-rathee
Copy link
Author

image_480

@ashwani-rathee
Copy link
Author

High pass filter:

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

R = 1
C = 5.0
@variables t
@named resistor = Resistor(R = R)
@named capacitor = Capacitor(C = C, v = 4.0)
@named source = Voltage()
@named waveform = Sine(;frequency=0.1)
@named ground = Ground()
@named volt_sense = VoltageSensor()

rc_eqs = [connect(waveform.output, source.V)
    connect(source.p, capacitor.p)
    connect(capacitor.n, resistor.p, volt_sense.p)
    connect(volt_sense.n, resistor.n, source.n, ground.g)
    ]

root_eqs = [t ~ 5]  
affect = [waveform.frequency ~ 2] # the effect is that the velocity changes sign

@named rc_model = ODESystem(rc_eqs, t,
    systems = [resistor, capacitor, volt_sense, waveform, source, ground], continuous_events = root_eqs => affect)
sys = structural_simplify(rc_model)
prob = ODAEProblem(sys, Pair[], (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol, idxs = [source.v, volt_sense.v],
    title = "RC High Pass Filter",
    labels = ["vin" "vout"])
savefig("plot.png");

@ashwani-rathee
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment