Skip to content

Commit 2297905

Browse files
Merge pull request #559 from Lightup1/baoab-dt
fix dt scaling when scale_noise==false
2 parents b6d2ec1 + 24026eb commit 2297905

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/caches/dynamical_caches.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020
function alg_cache(alg::BAOAB,prob,u,ΔW,ΔZ,p,rate_prototype,noise_rate_prototype,jump_rate_prototype,::Type{uEltypeNoUnits},::Type{uBottomEltypeNoUnits},::Type{tTypeNoUnits},uprev,f,t,dt,::Type{Val{false}}) where {uEltypeNoUnits,uBottomEltypeNoUnits,tTypeNoUnits}
2121
k = zero(rate_prototype.x[1])
2222
c1 = exp(-alg.gamma*dt)
23-
c2 = sqrt(1 - alg.scale_noise*c1^2) # if scale_noise == false, c2 = 1
23+
c2 = alg.scale_noise ? sqrt((1 - c1^2)/abs(dt)) : 1 # if scale_noise == false, c2 = 1
2424
BAOABConstantCache(k, uEltypeNoUnits(1//2), uEltypeNoUnits(c1), uEltypeNoUnits(c2))
2525
end
2626

@@ -34,7 +34,7 @@ function alg_cache(alg::BAOAB,prob,u,ΔW,ΔZ,p,rate_prototype,noise_rate_prototy
3434

3535
half = uEltypeNoUnits(1//2)
3636
c1 = exp(-alg.gamma*dt)
37-
c2 = sqrt(1 - alg.scale_noise*c1^2) # if scale_noise == false, c2 = 1
37+
c2 = alg.scale_noise ? sqrt((1 - c1^2)/abs(dt)) : 1 # if scale_noise == false, c2 = 1
3838

3939
tmp = zero(u)
4040

src/perform_step/dynamical.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ end
4242
u2 = u1 + half*dt*du2
4343

4444
# O
45-
noise = integrator.g(u2,p,t+dt*half).*W.dW / sqdt
45+
noise = integrator.g(u2,p,t+dt*half).*W.dW
4646
du3 = c1*du2 + c2*noise
4747

4848
# A
@@ -69,7 +69,7 @@ end
6969

7070
# O
7171
integrator.g(gtmp,utmp,p,t+dt*half)
72-
@.. noise = gtmp*W.dW / sqdt
72+
@.. noise = gtmp*W.dW
7373
@.. dutmp = c1*dutmp + c2*noise
7474

7575
# A

test/sde/sde_dynamical.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,43 @@ end
4545
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ),(1/2)^10;trajectories=Int(1e2),use_noise_grid=false)
4646
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
4747
end
48+
49+
@testset "Scalar u, scale_noise=false" begin
50+
u0 = 0
51+
v0 = 1
52+
53+
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g)
54+
prob1 = DynamicalSDEProblem(ff_harmonic,v0,u0,(0.0,5.0))
55+
56+
dts = (1/2) .^ (8:-1:4)
57+
58+
# Can't use NoiseGrid as noise is not generated with the correct size in convergence.jl. We require noise with shape of v.
59+
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ,scale_noise=false),(1/2)^10;trajectories=Int(2e2),use_noise_grid=false)
60+
display(sim1.𝒪est)
61+
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
62+
end
63+
64+
@testset "Vector u, scale_noise=false" begin
65+
66+
u0 = zeros(2)
67+
v0 = ones(2)
68+
69+
f1_harmonic_iip(dv,v,u,p,t) = dv .= f1_harmonic(v,u,p,t)
70+
f2_harmonic_iip(du,v,u,p,t) = du .= f2_harmonic(v,u,p,t)
71+
g_iip(du,u,p,t) = du .= g(u,p,t)
72+
73+
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g)
74+
prob1 = DynamicalSDEProblem(ff_harmonic,v0,u0,(0.0,5.0))
75+
sol1 = solve(prob1,BAOAB(gamma=γ,scale_noise=false);dt=1/10,save_noise=true)
76+
77+
prob2 = DynamicalSDEProblem(f1_harmonic_iip,f2_harmonic_iip,g_iip,v0,u0,(0.0,5.0); noise=NoiseWrapper(sol1.W))
78+
sol2 = solve(prob2,BAOAB(gamma=γ,scale_noise=false);dt=1/10)
79+
80+
@test sol1[:] sol2[:]
81+
82+
dts = (1/2) .^ (8:-1:4)
83+
84+
# Can't use NoiseGrid as noise is not generated with the correct size in convergence.jl. We require noise with shape of v.
85+
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ,scale_noise=false),(1/2)^10;trajectories=Int(1e2),use_noise_grid=false)
86+
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
87+
end

0 commit comments

Comments
 (0)