-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BAOAB algorithm #397
Merged
Merged
Add BAOAB algorithm #397
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5928190
Add BAOAB algorithm
jamesgardner1421 277c393
Clean up and add to runtests
jamesgardner1421 3d0e1b2
Add convergence tests
jamesgardner1421 f4c28a9
Fix mistake in O step
jamesgardner1421 9f0b29c
Modify form of equation
jamesgardner1421 28446aa
Fix order and testing
jamesgardner1421 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
struct BAOABConstantCache{uType,uEltypeNoUnits} <: StochasticDiffEqConstantCache | ||
k::uType | ||
half::uEltypeNoUnits | ||
c1::uEltypeNoUnits | ||
c2::uEltypeNoUnits | ||
end | ||
@cache struct BAOABCache{uType,uEltypeNoUnits,rateNoiseType} <: StochasticDiffEqMutableCache | ||
utmp::uType | ||
dutmp::uType | ||
k::uType | ||
gtmp::uType | ||
noise::rateNoiseType | ||
half::uEltypeNoUnits | ||
c1::uEltypeNoUnits | ||
c2::uEltypeNoUnits | ||
end | ||
|
||
function alg_cache(alg::BAOAB,prob,u,ΔW,ΔZ,p,rate_prototype,noise_rate_prototype,jump_rate_prototype,uEltypeNoUnits,uBottomEltypeNoUnits,tTypeNoUnits,uprev,f,t,dt,::Type{Val{false}}) | ||
k = zero(rate_prototype.x[1]) | ||
c1 = exp(-alg.gamma*dt) | ||
c2 = sqrt(1 - c1^2) | ||
BAOABConstantCache(k, uEltypeNoUnits(1//2), uEltypeNoUnits(c1), uEltypeNoUnits(c2)) | ||
end | ||
|
||
function alg_cache(alg::BAOAB,prob,u,ΔW,ΔZ,p,rate_prototype,noise_rate_prototype,jump_rate_prototype,uEltypeNoUnits,uBottomEltypeNoUnits,tTypeNoUnits,uprev,f,t,dt,::Type{Val{true}}) | ||
dutmp = zero(u.x[1]) | ||
utmp = zero(u.x[2]) | ||
k = zero(rate_prototype.x[1]) | ||
|
||
gtmp = zero(rate_prototype.x[1]) | ||
noise = zero(rate_prototype.x[1]) | ||
|
||
half = uEltypeNoUnits(1//2) | ||
c1 = exp(-alg.gamma*dt) | ||
c2 = sqrt(1 - c1^2) | ||
|
||
BAOABCache(utmp, dutmp, k, gtmp, noise, half, uEltypeNoUnits(c1), uEltypeNoUnits(c2)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
function verify_f2(f, p, q, pa, t, integrator, ::BAOABConstantCache) | ||
res = f(p, q, pa, t) | ||
res != p && throwex(integrator) | ||
end | ||
function verify_f2(f, res, p, q, pa, t, integrator, ::BAOABCache) | ||
f(res, p, q, pa, t) | ||
res != p && throwex(integrator) | ||
end | ||
function throwex(integrator) | ||
algn = typeof(integrator.alg) | ||
throw(ArgumentError("Algorithm $algn is not applicable if f2(p, q, t) != p")) | ||
end | ||
|
||
function initialize!(integrator, cache::BAOABConstantCache) | ||
@unpack t,dt,uprev,u,p,W = integrator | ||
du1 = integrator.uprev.x[1] | ||
u1 = integrator.uprev.x[2] | ||
|
||
verify_f2(integrator.f.f2, du1, u1, p, t, integrator, cache) | ||
cache.k .= integrator.f.f1(du1,u1,p,t) | ||
end | ||
|
||
function initialize!(integrator, cache::BAOABCache) | ||
@unpack t,dt,uprev,u,p,W = integrator | ||
du1 = integrator.uprev.x[1] | ||
u1 = integrator.uprev.x[2] | ||
|
||
verify_f2(integrator.f.f2, cache.k, du1, u1, p, t, integrator, cache) | ||
integrator.f.f1(cache.k,du1,u1,p,t) | ||
end | ||
|
||
@muladd function perform_step!(integrator,cache::BAOABConstantCache,f=integrator.f) | ||
@unpack t,dt,uprev,u,p,W = integrator | ||
@unpack k, half, c1, c2 = cache | ||
du1 = uprev.x[1] | ||
u1 = uprev.x[2] | ||
|
||
# B | ||
du2 = du1 + half*dt*k | ||
|
||
# A | ||
u2 = u1 + half*dt*du2 | ||
|
||
# O | ||
noise = integrator.g(u2,p,t+dt*half).*W.dW | ||
du3 = c1*du2 + c2*noise | ||
|
||
# A | ||
u = u2 + half*dt*du3 | ||
|
||
# B | ||
k .= f.f1(du3,u,p,t+dt) | ||
du = du3 + half*dt*k | ||
|
||
integrator.u = ArrayPartition((du, u)) | ||
end | ||
|
||
@muladd function perform_step!(integrator,cache::BAOABCache,f=integrator.f) | ||
@unpack t,dt,uprev,u,p,W = integrator | ||
@unpack utmp, dutmp, k, gtmp, noise, half, c1, c2 = cache | ||
du1 = uprev.x[1] | ||
u1 = uprev.x[2] | ||
|
||
# B | ||
@.. dutmp = du1 + half*dt*k | ||
|
||
# A | ||
@.. utmp = u1 + half*dt*dutmp | ||
|
||
# O | ||
integrator.g(gtmp,utmp,p,t+dt*half) | ||
@.. noise = gtmp*W.dW | ||
@.. dutmp = c1*dutmp + c2*noise | ||
|
||
# A | ||
@.. u.x[2] = utmp + half*dt*dutmp | ||
|
||
# B | ||
f.f1(k,dutmp,u.x[2],p,t+dt) | ||
@.. u.x[1] = dutmp + half*dt*k | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
jamesgardner1421 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using StochasticDiffEq, DiffEqNoiseProcess, Test, DiffEqDevTools | ||
jamesgardner1421 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using Plots | ||
using RecursiveArrayTools | ||
|
||
u0 = zeros(2) | ||
v0 = ones(2) | ||
γ = 1 | ||
f1_harmonic(v,u,p,t) = -u | ||
f2_harmonic(v,u,p,t) = v | ||
g(u,p,t) = 0.2 | ||
|
||
ff_harmonic = DynamicalSDEFunction(f1_harmonic,f2_harmonic,g) | ||
prob = DynamicalSDEProblem(ff_harmonic,g,v0,u0,(0.0,5.0)) | ||
|
||
sol1 = solve(prob,BAOAB(gamma=γ);dt=1/10,save_noise=true) | ||
|
||
f1_harmonic_iip(dv,v,u,p,t) = dv .= f1_harmonic(v,u,p,t) | ||
f2_harmonic_iip(du,v,u,p,t) = du .= f2_harmonic(v,u,p,t) | ||
g_iip(du,u,p,t) = du .= g(u,p,t) | ||
|
||
prob = DynamicalSDEProblem(f1_harmonic_iip,f2_harmonic_iip,g_iip,v0,u0,(0.0,5.0); noise=NoiseWrapper(sol1.W)) | ||
|
||
sol2 = solve(prob,BAOAB(gamma=γ);dt=1/10) | ||
@test sol1[:] ≈ sol2[:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should get convergence tests |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would be good to look into