Skip to content

Commit

Permalink
Fix double sum (#223)
Browse files Browse the repository at this point in the history
* fix DoubleSum with Number

* reduce time test indexed_correlation
  • Loading branch information
ChristophHotter authored Oct 8, 2024
1 parent 6c711d4 commit 41fd0ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/index_average.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ end
function IndexedAverageDoubleSum(term::symbolics_terms,sum_index::Index,non_equal_indices)
if iscall(term)
op = operation(term)
args = arguments(term)
args = copy(arguments(term))
param = 1.0
if op === *
if args[1] isa Number #put numbers out in front
Expand Down
12 changes: 12 additions & 0 deletions test/test_double_sums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ a(k) = IndexedOperator(Destroy(h,:a),k)
Ssum1 = Σ(g_ik*a(k_ind)*σ(2,1,i_ind),i_ind)
Ssum2 = Σ(g_ik*a(k_ind)'*σ(1,2,i_ind),i_ind)

### issue 221 (DoubleSum)
@cnumbers c1 N1
i_ind2 = Index(h,:i,N1,ha)
j_ind2 = Index(h,:j,N1,ha)
@test isequal(simplify(Σ(-σ(2,2,i_ind),i_ind,j_ind)),simplify(Σ(-σ(2,2,i_ind),i_ind)*4))
@test isequal(simplify(Σ(3*σ(2,2,i_ind),i_ind,j_ind)),simplify(Σ(3*σ(2,2,i_ind),i_ind)*4))
@test isequal(simplify(Σ(c1*σ(2,2,i_ind),i_ind,j_ind)),simplify(Σ(c1*σ(2,2,i_ind),i_ind)*4))
@test isequal(simplify(Σ(-σ(2,2,i_ind2),i_ind2,j_ind2)),simplify(Σ( (1-N1)*σ(2,2,i_ind2) ,i_ind2)) - Σ( σ(2,2,i_ind2), i_ind2))
@test isequal(simplify(Σ(3*σ(2,2,i_ind2),i_ind2,j_ind2)), simplify(Σ( 3*(N1-1)*σ(2,2,i_ind2) ,i_ind2)) + 3*Σ( σ(2,2,i_ind2), i_ind2))
@test isequal(simplify(Σ(c1*σ(2,2,i_ind2),i_ind2,j_ind2)),simplify(c1*Σ( σ(2,2,i_ind2), i_ind2) + Σ( c1*(N1-1)*σ(2,2,i_ind2) ,i_ind2)) )
###

@test isequal(Σ(conj(g_ik)*a(k_ind)'*σ(1,2,i_ind),i_ind),Ssum1')

@test isequal(Σ(Σ(g_ik*(a(k_ind)*σ(2,1,i_ind) + a(k_ind)'*σ(1,2,i_ind)),i_ind),k_ind),
Expand Down
18 changes: 11 additions & 7 deletions test/test_indexed_correlation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ p0_c2 = correlation_p0(corr2_sc, sol.u[end],ps.=>p0)
u0_c2 = correlation_u0(corr2_sc, sol.u[end])
@named csys2 = ODESystem(corr2_sc) # 5 equations, 8 parameters

prob_c2 = ODEProblem(csys2,u0_c2,(0.0,5.0),p0_c2)
# prob_c2 = ODEProblem(csys2,u0_c2,(0.0,5.0),p0_c2)
prob_c2 = ODEProblem(csys2,u0_c2,(0.0,0.05),p0_c2)
sol_c2 = solve(prob_c2,Tsit5();saveat=0.001,maxiters=1e8);

@test sol_c2.retcode == SciMLBase.ReturnCode.Success
Expand All @@ -152,8 +153,8 @@ eqs_ev = evaluate(eqs_c_2;limits=(N=>3))
u0_ev = zeros(ComplexF64, length(eqs_ev))

@named sys_ev = ODESystem(eqs_ev)
prob_ev = ODEProblem(sys_ev,u0_ev,(0.0, 1.0/50Γ_), ps.=>p0);

# prob_ev = ODEProblem(sys_ev,u0_ev,(0.0, 1.0/50Γ_), ps.=>p0);
prob_ev = ODEProblem(sys_ev,u0_ev,(0.0, 0.1/50Γ_), ps.=>p0);
sol_ev = solve(prob_ev,Tsit5(),maxiters=1e7)
@test sol_ev.retcode == SciMLBase.ReturnCode.Success

Expand All @@ -164,7 +165,8 @@ p0_c3 = correlation_p0(corr3_ev, sol_ev.u[end],ps.=>p0)
u0_c3 = correlation_u0(corr3_ev, sol_ev.u[end])
@named csys3 = ODESystem(corr3_ev)

prob_c3 = ODEProblem(csys3,u0_c3,(0.0,5.0),p0_c3)
# prob_c3 = ODEProblem(csys3,u0_c3,(0.0,5.0),p0_c3)
prob_c3 = ODEProblem(csys3,u0_c3,(0.0,0.05),p0_c3)
sol_c3 = solve(prob_c3,Tsit5();saveat=0.001,maxiters=1e8);

@test sol_c3.retcode == SciMLBase.ReturnCode.Success
Expand All @@ -186,8 +188,9 @@ u0_c = correlation_u0(corr_nss_sc, sol.u[end])

@named csys = ODESystem(corr_nss_sc)

prob_c = ODEProblem(csys,u0_c,(0.0,10.0),p0_c);
sol_c = solve(prob_c,Tsit5();saveat=0.001,maxiters=1e8);
# prob_c4 = ODEProblem(csys,u0_c,(0.0,10.0),p0_c);
prob_c4 = ODEProblem(csys,u0_c,(0.0,0.05),p0_c);
sol_c = solve(prob_c4,Tsit5();saveat=0.001,maxiters=1e8);

@test sol_c.retcode == SciMLBase.ReturnCode.Success

Expand Down Expand Up @@ -351,7 +354,8 @@ new_sum = qc._new_operator(sum1,h_)
ν_ = 1000Γ_ #ν=1Hz
ps = [N, Δ, g(1), κ, Γ, R, ν]
p0 = [N_, Δ_, g_, κ_, Γ_, R_, ν_]
prob = ODEProblem(sys,u0,(0.0, 1.0/50Γ_), ps.=>p0)
# prob = ODEProblem(sys,u0,(0.0, 1.0/50Γ_), ps.=>p0)
prob = ODEProblem(sys,u0,(0.0, 0.1/50Γ_), ps.=>p0)
# Solve the numeric problem
sol = solve(prob,Tsit5(),maxiters=1e7);
b = Index(h,:b,N,hA)
Expand Down

0 comments on commit 41fd0ad

Please sign in to comment.