Open
Description
Greetings,
I tried to simulate the electronic buck converter using the following code:
using DifferentialEquations
using Plots
function odeDiffEquPackage()
function f(du, u, p, t)
C = 1e-4; L = 1e-4; R = 10;U = 24.0; T = 1e-4; DC = 0.5; ROn = 1e-5;ROff = 1e5;
rd=p[1];rs=p[2];
il=u[1] ;uc=u[2]
id=(il*rs-U)/(rd+rs) # diode's current
du[1] =(-id*rd-uc)/L # inductor's current
du[2]=(il-uc/R)/C # capacitor's voltage
end
function condition1( u, t, integrator)
(t-p[3])
end
function condition2( u, t, integrator)
(t-p[4]-0.5*1e-4)
end
function condition3( u, t, integrator)
(p[5]*((u[1]*p[2]-24.0)/(p[1]+p[2]))+(1.0-p[5])*((u[1]*p[2]-24.0)*p[1]/(p[1]+p[2])))
end
function affect1!(integrator)
p[4]=p[3]
p[3]=p[3]+1e-4
p[2]=1e-5
end
function affect2!(integrator)
p[2]=1e5
end
function affect3!(integrator)
p[1]=1e-5
p[5]=1.0
end
function affect33!(integrator)
p[1]=1e5
p[5]=0.0
end
cb1 = ContinuousCallback(condition1, affect1!,nothing; )
cb2 = ContinuousCallback(condition2, affect2!,nothing; )
cb3 = ContinuousCallback(condition3, affect3!,affect33!; )
cbs = CallbackSet(cb1, cb2,cb3)
u0 = [0.0, 0.0]
tspan = (0.0, 0.001)
p = [1e5,1e-5,1e-4,0.0,0.0,0.0]
prob = ODEProblem(f, u0, tspan, p)
sol = solve(prob, Tsit5(), callback = cbs, reltol=1e-3,abstol=1e-4#= dt = 1e-3, adaptive = false =#)
p1=plot!(sol);
savefig(p1, "Tsit5()_34_buck_ft0005_")
end
odeDiffEquPackage()
However, after simulating the system via 3 other tools:
- ltspice: https://www.analog.com/en/design-center/design-tools-and-calculators/ltspice-simulator.html
- c solver https://github.com/CIFASIS/qss-solver
- julia qssolver https://github.com/mongibellili/QS_Solver using the same system
using qss
function test()
odeprob = @NLodeProblem begin
name=(buck,)
C = 1e-4; L = 1e-4; R = 10.0;U = 24.0; T = 1e-4; DC = 0.5; ROn = 1e-5;ROff = 1e5;
discrete = [1e5,1e-5,1e-4,0.0,0.0]
u = [0.0,0.0]
rd=discrete[1];rs=discrete[2];nextT=discrete[3];lastT=discrete[4];diodeon=discrete[5]
il=u[1] ;uc=u[2]
id=(il*rs-U)/(rd+rs) # diode's current
du[1] =(-id*rd-uc)/L
du[2]=(il-uc/R)/C
if t-nextT>0.0
lastT=nextT
nextT=nextT+T
rs=ROn
end
if t-lastT-DC*T>0.0
rs=ROff
end
if diodeon*(id)+(1.0-diodeon)*(id*rd)>0
rd=ROn
diodeon=1.0
else
rd=ROff
diodeon=0.0
end
end
tspan = (0.0, 0.0002)
sol= solve(odeprob,nmliqss2(),tspan,abstol=1e-4,reltol=1e-3)
save_Sol(sol)
end
test()
I received the following results:
c solver
ltspice
QS_Solver.jl
Am I misusing callbacks?
thank you.
Metadata
Metadata
Assignees
Labels
No labels