Skip to content

Commit 47bf829

Browse files
Fix BAOAB cache to work with scalar u
1 parent 08bff7e commit 47bf829

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/caches/dynamical_caches.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
struct BAOABConstantCache{uType,uEltypeNoUnits} <: StochasticDiffEqConstantCache
2+
mutable struct BAOABConstantCache{uType,uEltypeNoUnits} <: StochasticDiffEqConstantCache
33
k::uType
44
half::uEltypeNoUnits
55
c1::uEltypeNoUnits

src/perform_step/dynamical.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function initialize!(integrator, cache::BAOABConstantCache)
1717
u1 = integrator.uprev.x[2]
1818

1919
verify_f2(integrator.f.f2, du1, u1, p, t, integrator, cache)
20-
cache.k .= integrator.f.f1(du1,u1,p,t)
20+
cache.k = integrator.f.f1(du1,u1,p,t)
2121
end
2222

2323
function initialize!(integrator, cache::BAOABCache)
@@ -31,12 +31,12 @@ end
3131

3232
@muladd function perform_step!(integrator,cache::BAOABConstantCache)
3333
@unpack t,dt,sqdt,uprev,u,p,W,f = integrator
34-
@unpack k, half, c1, c2 = cache
34+
@unpack half, c1, c2 = cache
3535
du1 = uprev.x[1]
3636
u1 = uprev.x[2]
3737

3838
# B
39-
du2 = du1 + half*dt*k
39+
du2 = du1 + half*dt*cache.k
4040

4141
# A
4242
u2 = u1 + half*dt*du2
@@ -49,8 +49,8 @@ end
4949
u = u2 + half*dt*du3
5050

5151
# B
52-
k .= f.f1(du3,u,p,t+dt)
53-
du = du3 + half*dt*k
52+
cache.k = f.f1(du3,u,p,t+dt)
53+
du = du3 + half*dt*cache.k
5454

5555
integrator.u = ArrayPartition((du, u))
5656
end

test/sde/sde_dynamical.jl

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
using StochasticDiffEq, DiffEqNoiseProcess, Test, DiffEqDevTools, Random
22
Random.seed!(1)
33

4-
u0 = zeros(2)
5-
v0 = ones(2)
6-
γ = 1
7-
84
f1_harmonic(v,u,p,t) = -u
95
f2_harmonic(v,u,p,t) = v
106
g(u,p,t) = 1
7+
γ = 1
8+
9+
@testset "Scalar u" begin
10+
u0 = 0
11+
v0 = 1
12+
13+
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g)
14+
prob1 = DynamicalSDEProblem(ff_harmonic,g,v0,u0,(0.0,5.0))
15+
16+
dts = (1/2) .^ (8:-1:4)
17+
18+
# Can't use NoiseGrid as noise is not generated with the correct size in convergence.jl. We require noise with shape of v.
19+
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ),(1/2)^10;trajectories=Int(2e2),use_noise_grid=false)
20+
display(sim1.𝒪est)
21+
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
22+
end
23+
24+
@testset "Vector u" begin
25+
26+
u0 = zeros(2)
27+
v0 = ones(2)
1128

12-
f1_harmonic_iip(dv,v,u,p,t) = dv .= f1_harmonic(v,u,p,t)
13-
f2_harmonic_iip(du,v,u,p,t) = du .= f2_harmonic(v,u,p,t)
14-
g_iip(du,u,p,t) = du .= g(u,p,t)
29+
f1_harmonic_iip(dv,v,u,p,t) = dv .= f1_harmonic(v,u,p,t)
30+
f2_harmonic_iip(du,v,u,p,t) = du .= f2_harmonic(v,u,p,t)
31+
g_iip(du,u,p,t) = du .= g(u,p,t)
1532

16-
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g)
17-
prob1 = DynamicalSDEProblem(ff_harmonic,g,v0,u0,(0.0,5.0))
18-
sol1 = solve(prob1,BAOAB(gamma=γ);dt=1/10,save_noise=true)
33+
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g)
34+
prob1 = DynamicalSDEProblem(ff_harmonic,g,v0,u0,(0.0,5.0))
35+
sol1 = solve(prob1,BAOAB(gamma=γ);dt=1/10,save_noise=true)
1936

20-
prob2 = DynamicalSDEProblem(f1_harmonic_iip,f2_harmonic_iip,g_iip,v0,u0,(0.0,5.0); noise=NoiseWrapper(sol1.W))
21-
sol2 = solve(prob2,BAOAB(gamma=γ);dt=1/10)
37+
prob2 = DynamicalSDEProblem(f1_harmonic_iip,f2_harmonic_iip,g_iip,v0,u0,(0.0,5.0); noise=NoiseWrapper(sol1.W))
38+
sol2 = solve(prob2,BAOAB(gamma=γ);dt=1/10)
2239

23-
@test sol1[:] sol2[:]
40+
@test sol1[:] sol2[:]
2441

25-
dts = (1/2) .^ (8:-1:4)
42+
dts = (1/2) .^ (8:-1:4)
2643

27-
# Can't use NoiseGrid as noise is not generated with the correct size in convergence.jl. We require noise with shape of v.
28-
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ),(1/2)^10;trajectories=Int(1e2),use_noise_grid=false)
29-
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
44+
# Can't use NoiseGrid as noise is not generated with the correct size in convergence.jl. We require noise with shape of v.
45+
sim1 = analyticless_test_convergence(dts,prob1,BAOAB(gamma=γ),(1/2)^10;trajectories=Int(1e2),use_noise_grid=false)
46+
@test abs(sim1.𝒪est[:weak_final]-1) < 0.3
47+
end

0 commit comments

Comments
 (0)