Skip to content

Commit

Permalink
changed macro to function...not fully tested
Browse files Browse the repository at this point in the history
  • Loading branch information
mongibellili committed May 14, 2024
1 parent e0b6ed0 commit 048e9d6
Show file tree
Hide file tree
Showing 19 changed files with 599 additions and 99 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Interface/indexMacro.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


macro NLodeProblem(odeExprs)
function NLodeProblem(odeExprs)
Base.remove_linenums!(odeExprs)
if VERBOSE println("starting prob parsing...") end
probHelper=arrangeProb(odeExprs)# replace symbols and params , extract info about size,symbols,initconds
Expand Down
2 changes: 1 addition & 1 deletion src/QuantizedSystemSolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Base: sqrt, exp, log, sin, cos, sincos, tan,
export plotAbsoluteError#,stackplotAbsoluteError,plot_save_AbsoluteError,stackplot_save_AbsoluteError,saveAbsoluteError,stacksaveAbsoluteError
export getError,getPlot,getPlot!#,plotCumulativeSquaredRelativeError,plotMSE,getIntervalError,plotElapsed

export @NLodeProblem,@saveNLodeProblem,solve,save_prob_to_model,QSS_Solve_from_model,solInterpolated
export NLodeProblem, @NLodeProblem,@saveNLodeProblem,solve,save_prob_to_model,QSS_Solve_from_model,solInterpolated
export Sol,getErrorByRodas,getAllErrorsByRefs,getAverageErrorByRefs

export Taylor0,mulT,mulTT,createT,addsub,negateT,subsub,subadd,subT,addT,muladdT,mulsub,divT # in case to save into a file, otherwise remove
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/rootfinders/SimUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ end
function minPosRoot(coeff::Taylor0, ::Val{2}) # credit goes to github.com/CIFASIS/qss-solver
mpr=-1
a=coeff[2];b=coeff[1];c=coeff[0]; # a is coeff 3 because in taylor representation 1 is var 2 is der 3 is derder
if a == 0 || (10000 * abs(a)) < abs(b)# coef3 is the coef of t^2
if a == 0 || (100000 * abs(a)) < abs(b)# coef3 is the coef of t^2
if b == 0
mpr = Inf
else
Expand Down
7 changes: 4 additions & 3 deletions src/dense/NL_integrators/NL_mLiQSS_Integrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function mLiQSS_integrate(Al::QSSAlgorithm{:nmliqss,O},CommonqssData::CommonQSS_
#u=liqssdata.u;#tu=liqssdata.tu
#***************************************************************
qaux=liqssdata.qaux;dxaux=liqssdata.dxaux#= olddx=liqssdata.olddx; ; olddxSpec=liqssdata.olddxSpec =#

d=[0.0]
numSteps = Vector{Int}(undef, T)
pp=pointer(Vector{NTuple{2,Float64}}(undef, 7))

Expand Down Expand Up @@ -57,7 +57,8 @@ end
#push!(savedVarsQ[i],q[i][0])
push!(savedTimes[i],0.0)
quantum[i] = relQ * abs(x[i].coeffs[1]) ;quantum[i]=quantum[i] < absQ ? absQ : quantum[i];quantum[i]=quantum[i] > maxErr ? maxErr : quantum[i]
updateQ(Val(O),i,x,q,quantum,exacteA,cacheA,dxaux,qaux,tx,tq,initTime,ft,nextStateTime)
updateQ(Val(O),i,x,q,quantum,exacteA,d,cacheA,dxaux,qaux,tx,tq,initTime,ft,nextStateTime)

end
for i = 1:T
clearCache(taylorOpsCache,Val(CS),Val(O));f(i,q,t,taylorOpsCache);
Expand Down Expand Up @@ -106,7 +107,7 @@ end
elapsedq = simt - tq[b] ;
if elapsedq>0 integrateState(Val(O-1),q[b],elapsedq);tq[b]=simt end
end
firstguess=updateQ(Val(O),index,x,q,quantum,exacteA,cacheA,dxaux,qaux,tx,tq,simt,ft,nextStateTime) ;tq[index] = simt
firstguess=updateQ(Val(O),index,x,q,quantum,exacteA,d,cacheA,dxaux,qaux,tx,tq,simt,ft,nextStateTime) ;tq[index] = simt
#----------------------------------------------------check dependecy cycles---------------------------------------------
for j in SD(index)
for b in (jac(j) ) # update Qb: to be used to calculate exacte Ajb
Expand Down
59 changes: 36 additions & 23 deletions src/dense/NL_integrators/NL_nmLiQSS_discreteIntegrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function integrate(Al::QSSAlgorithm{:nmliqss,O},CommonqssData::CommonQSS_data{Z}

qaux=liqssdata.qaux;dxaux=liqssdata.dxaux#= olddx=liqssdata.olddx; ; olddxSpec=liqssdata.olddxSpec =#

savedDers = Vector{Vector{Float64}}(undef, T)
#savedDers = Vector{Vector{Float64}}(undef, T)
# savedVarsQ = Vector{Vector{Float64}}(undef, T)

#= setprecision(BigFloat,80)
Expand Down Expand Up @@ -72,9 +72,9 @@ end

for i = 1:T
numSteps[i]=0
savedDers[i]=Vector{Float64}()
#savedDers[i]=Vector{Float64}()
push!(savedVars[i],x[i][0])
push!(savedDers[i],x[i][1])
# push!(savedDers[i],x[i][1])
push!(savedTimes[i],0.0)

quantum[i] = relQ * abs(x[i].coeffs[1]) ;quantum[i]=quantum[i] < absQ ? absQ : quantum[i];quantum[i]=quantum[i] > maxErr ? maxErr : quantum[i]
Expand Down Expand Up @@ -122,22 +122,30 @@ while simt< ft && totalSteps < 50000000

t[0]=simt

DEBUG_time=DEBUG && 0.0002<=simt<=0.00022
DEBUG_time=DEBUG && #= (29750 <=totalSteps<=29750) =# (0.0003557 <=simt<=0.0003559 )
##########################################state########################################
if stepType == :ST_STATE
statestep+=1

if DEBUG_time #&& (index!=13 && index!=15)
println("at simt=$simt x $index at begining of state step = $(x)")
println("-------------q begining of state step = $q")
@show totalSteps

end

xitemp=x[index][0]
numSteps[index]+=1;

elapsed = simt - tx[index];integrateState(Val(O),x[index],elapsed);tx[index] = simt ;
dirI=x[index][0]-xitemp
if abs(dirI)>3*quantum[index] x[index][0]= 2*quantum[index] *sign(dirI) end # this is a rare case where dxi gets changed a lot by an event
#= if abs(dirI)>3*quantum[index]
x[index][0]=xitemp+ 3*quantum[index] *sign(dirI)
println("at start of state step, x moved more than 2 deltas")
end # this is a rare case where dxi gets changed a lot by an event =#

quantum[index] = relQ * abs(x[index].coeffs[1]) ;quantum[index]=quantum[index] < absQ ? absQ : quantum[index];quantum[index]=quantum[index] > maxErr ? maxErr : quantum[index]

if abs(x[index].coeffs[2])>1e9 quantum[index]=10*quantum[index] end # i added this for the case a function is climbing (up/down) fast
#if abs(x[index].coeffs[2])>1e9 quantum[index]=10*quantum[index] end # i added this for the case a function is climbing (up/down) fast



Expand Down Expand Up @@ -257,18 +265,18 @@ while simt< ft && totalSteps < 50000000
computeNextEventTime(Val(O),j,taylorOpsCache[1],oldsignValue,simt, nextEventTime, quantum,absQ)
end#end for SZ

#= if DEBUG_time
println("at simt=$simt x end of state step = $x")
if DEBUG_time #&& (index!=13 && index!=15)
println("at simt=$simt x $index at end of state step = $(x)")
println("-------------q end of state step = $q")
end =#
@show totalSteps
end

if DEBUG_time && (index==3 || index==4)
#= if DEBUG_time #&& (index==3 || index==6)
println("========end state=======")
@show index,simt
@show x,q
@show nextStateTime,quantum
end
@show x[]
# @show nextStateTime,quantum
end =#

##################################input########################################
elseif stepType == :ST_INPUT # time of change has come to a state var that does not depend on anything...no one will give you a chance to change but yourself
Expand Down Expand Up @@ -320,8 +328,8 @@ else

if DEBUG_time
println("x at start of event simt=$simt index=$index")

# println("-------------q start of event = $q")
println("-------------x start of event = $x")
println("-------------q start of event = $q")
#@show index,d
end

Expand Down Expand Up @@ -397,7 +405,7 @@ else
firstguess=updateQ(Val(O),i,x,q,quantum,exactA,d,cacheA,dxaux,qaux,tx,tq,simt,ft,nextStateTime)
# computeNextTime(Val(O), i, simt, nextStateTime, x, quantum)
tx[i] = simt;tq[i] = simt
#Liqss_reComputeNextTime(Val(O), i, simt, nextStateTime, x, q, quantum)
Liqss_reComputeNextTime(Val(O), i, simt, nextStateTime, x, q, quantum)
#= if DEBUG_time
println("x end evcont simt=$simt x2=$(x[2])")
println("q end evcont simt=$simt q2=$(q[2])")
Expand All @@ -421,7 +429,10 @@ else
elapsedq = simt - tq[b];if elapsedq>0 integrateState(Val(O-1),q[b],elapsedq);tq[b]=simt;#= @show q[b] =# end
end
end

if DEBUG_time
println("q $j after intgratestate")
@show x[j],q[j]
end

clearCache(taylorOpsCache,Val(CS),Val(O));f(j,-1,-1,q,d,t,taylorOpsCache);computeDerivative(Val(O), x[j], taylorOpsCache[1])

Expand Down Expand Up @@ -450,10 +461,10 @@ else


if DEBUG_time
println("x at end of event simt=$simt x2=$(x)")
# println("x at end of event simt=$simt x=$(x)")
println("q at end of event simt=$simt q2=$(q)")
@show countEvents,totalSteps,statestep
@show nextStateTime,quantum
# @show nextStateTime,quantum
end
#= if 9.236846540089048e-5<=simt<9.237519926276279e-5
println("-------------end of event------------")
Expand All @@ -474,7 +485,7 @@ else

#= for i =1:T
push!(savedVars[i],x[i][0])
push!(savedDers[i],x[i][1])
# push!(savedDers[i],x[i][1])
push!(savedTimes[i],simt)
end =#
else
Expand All @@ -485,7 +496,9 @@ else
push!(savedVars[j],x[j][0])
#push!(savedDers[j],x[j][1])
push!(savedTimes[j],simt)

#= if simt==0.00035
@show j,x[j][0]
end =#
end
# end
end
Expand Down
3 changes: 2 additions & 1 deletion src/dense/Quantizers/LiQSS_quantizer1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ end =#
function Liqss_reComputeNextTime(::Val{1}, i::Int, simt::Float64, nextStateTime::Vector{Float64}, xv::Vector{Taylor0},qv::Vector{Taylor0}, quantum::Vector{Float64}#= ,a::Vector{Vector{Float64}} =#)
dt=0.0; q=qv[i][0];x=xv[i][0];x1=xv[i][1]
if abs(q-x) >= 2*quantum[i] # this happened when var i and j s turns are now...var i depends on j, j is asked here for next time...or if you want to increase quant*10 later it can be put back to normal and q & x are spread out by 10quan
nextStateTime[i] = simt+1e-15
nextStateTime[i] = simt+1e-12
@show simt,i
else
if x1 !=0.0 #&& abs(q-x)>quantum[i]/10
dt=(q-x)/x1
Expand Down
4 changes: 2 additions & 2 deletions src/dense/Quantizers/LiQSS_quantizer2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ function Liqss_reComputeNextTime(::Val{2}, i::Int, simt::Float64, nextStateTime:
β=0
if abs(q-x) >= 2*quani # this happened when var i and j s turns are now...var i depends on j, j is asked here for next time...or if you want to increase quant*10 later it can be put back to normal and q & x are spread out by 10quan
nextStateTime[i] = simt+1e-12
if simt==2.500745083181994e-5
#= if simt==2.500745083181994e-5
println("quantizer-recomputeNext: abs(q-x) >= 2*quani")
@show simt,i,x,q,abs(q-x),quani
end
end =#
#= if simt>3.486047550372409
@show djsksfs
end =#
Expand Down
2 changes: 1 addition & 1 deletion test/systemsTests/commonTests/globalTest_ord1tol-2-5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ function test(case,solvr)
end

case="order1_"
test(case,nmliqss1())
test(case,nmliqss2())
#test(case,nmliqss2())
2 changes: 1 addition & 1 deletion test/systemsTests/converters/boost.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function test()
end =#

end
tspan=(0.0,0.001)
tspan=(0.0,0.0025)
sol= solve(odeprob,nmliqss2(),abstol=1e-4,reltol=1e-3,tspan)

save_Sol(sol)
Expand Down
29 changes: 29 additions & 0 deletions test/systemsTests/converters/buck copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using QuantizedSystemSolver
function test()
odeprob = NLodeProblem(
quote
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.6)>0
rd=ROn;diodeon=1.0
else
rd=ROff;diodeon=0.0
end
end)
tspan = (0.0, 0.001)
sol= solve(odeprob,nmliqss2(),tspan,abstol=1e-4,reltol=1e-3)
save_Sol(sol)
end
test()
4 changes: 2 additions & 2 deletions test/systemsTests/converters/buck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ function test()
if t-lastT-DC*T>0.0
rs=ROff
end
if diodeon*(id)+(1.0-diodeon)*(id*rd)>0
if diodeon*(id)+(1.0-diodeon)*(id*rd-0.6)>0
rd=ROn;diodeon=1.0
else
rd=ROff;diodeon=0.0
end
end
tspan = (0.0, 0.001)
sol= solve(odeprob,nmliqss1(),tspan,abstol=1e-4,reltol=1e-3)
sol= solve(odeprob,nmliqss2(),tspan,abstol=1e-4,reltol=1e-3)
save_Sol(sol)
end
test()
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ function testRailGun()
odeprob = @NLodeProblem begin
name=(RG2Cap,)
#parameters
ROn = 1e-5;ROff = 1e5; Lpr = 4.2*1e-9 ; L1 = 0.6*1e-4 #L2 = 4.0e-6;L3 = 1.1*1e-6 #L23=5.1e-6
ROn = 1e-5;ROff = 1e0; Lpr = 4.2*1e-9 ; L1 = 0.6*1e-4 #L2 = 4.0e-6;L3 = 1.1*1e-6 #L23=5.1e-6
R1= 4.0e-3; R2 = 0.28*1e-3;R3 = 3.6*1e-3;C = 3.08*1e-3;m=0.12
γ = 50.0*1e6; w = 15.0*1e-3; μ = 4.0*3.14*1e-7;Rpr0 = 15.5*1e-6; FNmec = 680.0; α = 0.154; t0=1.0
γ = 50.0*1e6; w = 15.0*1e0; μ = 4.0*3.14*1e-7;Rpr0 = 1.5*1e-6; FNmec = 680.0; α = 0.154; t0=1.0;
#continuous and discrete vars
discrete = [1e5,1e-5,1.0,1e5,1e-5,1.0,1.0,0.0,1e-3];u = [0.0,80000.75,0.0,0.0,80000.75,0.0,0.0,0.0]
discrete = [1e5,1e-5,1.0,1e5,1e-5,1.0,1.0,0.0,1e-3];u = [0.0,10000.75,0.0,0.0,10000.75,0.0,0.0,0.0]
rd1=discrete[1];rs1=discrete[2];charge1=discrete[3]; rd2=discrete[4];rs2=discrete[5];charge2=discrete[6];start2=discrete[7]; manualIntg=discrete[8]; nextT=discrete[9]
is1=u[1] ;uc1=u[2]; il1=u[3] ;is2=u[4] ;uc2=u[5]; il2=u[6] ;x=u[7]; v=u[8]
#helper functions
Expand All @@ -28,20 +28,30 @@ function testRailGun()
du[8]=F/m
#events
if -(uc1)>0.0
#= rs1=ROff; =#charge1=0.0 # rs off not needed since charge=0
rs1=ROff;charge1=0.0 # rs off not needed since charge=0
rd1=ROn;
end
if -(uc2)>0.0
#= rs2=ROff; =#charge2=0.0
rs2=ROff;charge2=0.0
rd2=ROn;
end
#= if -(is1)>0.0
rs1=ROff;charge1=0.0 # rs off not needed since charge=0
rd1=ROn;
#= if -(il1)>0.0
end
if -(is2)>0.0
rs2=ROff;charge2=0.0
rd2=ROn;
if -(il2)>0.0
end =#
#= if rs1*((il1-is1))+(1.0-rs1)*((il1-is1)*rd1-0.6)>0
# rs off not needed since charge=0
rd1=ROn;rs1=1.0
else
rd1=ROff;rs1=0.0
end
if rs2*((il2-is2))+(1.0-rs2)*((il2-is2)*rd2-0.6)>0
# rs off not needed since charge=0
rd2=ROn;rs2=1.0
else
rd2=ROff;rs2=0.0
end =#
if t-nextT>0
manualIntg=manualIntg+v/sqrt(t[0])
Expand Down
Loading

0 comments on commit 048e9d6

Please sign in to comment.