Skip to content

Commit

Permalink
update tests and min reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Nov 17, 2016
1 parent 7c7e362 commit ee764f9
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 66 deletions.
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.5
Parameters 0.5.0
ChunkedArrays 0.1.0
DiffEqBase
RecursiveArrayTools
DiffEqBase 0.2.0
RecursiveArrayTools 0.0.2
5 changes: 4 additions & 1 deletion src/StochasticDiffEq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module StochasticDiffEq
using DiffEqBase, Parameters, ChunkedArrays, RecursiveArrayTools, Juno
import DiffEqBase: solve


abstract AbstractMonteCarloSimulation

macro def(name, definition)
quote
macro $name()
Expand All @@ -23,7 +26,7 @@ module StochasticDiffEq
SRA1Optimized, SRAVectorized, SRIVectorized

#Stochastic Utils
export monteCarloSim, construct_correlated_noisefunc, WHITE_NOISE, NoiseProcess
export monte_carlo_simulation

#General Functions
export solve
Expand Down
61 changes: 27 additions & 34 deletions src/stochastic_utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
`monteCarloSim(dt::Number,prob::SDEProblem,alg::AbstractSDEAlgorithm)`
`monte_carlo_simulation(dt::Number,prob::AbstractSDEProblem,alg::AbstractSDEAlgorithm)`
Performs a parallel Monte-Carlo simulation to solve the SDE problem with dt numMonte times.
Returns a vector of solution objects.
Expand All @@ -9,49 +9,42 @@ Returns a vector of solution objects.
* `numMonte` - Number of Monte-Carlo simulations to run. Default is 10000
* `save_timeseries` - Denotes whether save_timeseries should be turned on in each run. Default is false.
"""
function monteCarloSim(prob::AbstractSDEProblem,alg;numMonte=10000,save_timeseries=false,kwargs...)
function monte_carlo_simulation(prob::SDETestProblem,alg;numMonte=10000,save_timeseries=false,kwargs...)
elapsedTime = @elapsed solutions = pmap((i)->solve(prob,alg;save_timeseries=save_timeseries,kwargs...),1:numMonte)
if typeof(prob) <: SDEProblem
solutions = convert(Array{SDESolution},solutions)
elseif typeof(prob) <: SDETestProblem
solutions = convert(Array{SDETestSolution},solutions)
solutions = convert(Array{SDETestSolution},solutions)
N = size(solutions,1)
errors = Dict() #Should add type information
error_means = Dict()
error_medians= Dict()
for k in keys(solutions[1].errors)
errors[k] = reshape(Float64[sol.errors[k] for sol in solutions],size(solutions)...)
error_means[k] = mean(errors[k])
error_medians[k]=median(errors[k])
end
if typeof(prob) <: SDETestProblem
N = size(solutions,1)
errors = Dict() #Should add type information
error_means = Dict()
error_medians= Dict()
for k in keys(solutions[1].errors)
errors[k] = reshape(Float64[sol.errors[k] for sol in solutions],size(solutions)...)
error_means[k] = mean(errors[k])
error_medians[k]=median(errors[k])
end
end
return(MonteCarloSimulation(solutions,errors,error_means,error_medians,N,elapsedTime))
return(MonteCarloTestSimulation(solutions,errors,error_means,error_medians,elapsedTime))
end

type MonteCarloSimulation
function monte_carlo_simulation(prob::SDEProblem,alg;numMonte=10000,save_timeseries=false,kwargs...)
elapsedTime = @elapsed solutions = pmap((i)->solve(prob,alg;save_timeseries=save_timeseries,kwargs...),1:numMonte)
solutions = convert(Array{SDESolution},solutions)
return(MonteCarloSimulation(solutions,elapsedTime))
end

type MonteCarloTestSimulation
solutions#::Array{T}
errors
error_means
error_medians
N
elapsedTime
end

Base.length(sim::MonteCarloSimulation) = sim.N
Base.endof( sim::MonteCarloSimulation) = length(sim)
Base.getindex(sim::MonteCarloSimulation,i::Int) = sim.solutions[i]
Base.getindex(sim::MonteCarloSimulation,i::Int,I::Int...) = sim.solutions[i][I]

function print(io::IO, sim::MonteCarloSimulation)
println(io,"$(typeof(sim)) of length $(length(sim)).")
println(io,"\n-----------Errors-----------")
for (k,v) in sim.errors
println(io,"$k: $v")
end
type MonteCarloSimulation
solutions#::Array{T}
elapsedTime
end

function show(io::IO,sim::MonteCarloSimulation)
println(io,"$(typeof(sim)) of length $(length(sim)).")
end

Base.length(sim::AbstractMonteCarloSimulation) = length(sim.solutions)
Base.endof( sim::AbstractMonteCarloSimulation) = length(sim)
Base.getindex(sim::AbstractMonteCarloSimulation,i::Int) = sim.solutions[i]
Base.getindex(sim::AbstractMonteCarloSimulation,i::Int,I::Int...) = sim.solutions[i][I]
21 changes: 7 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ using Base.Test
const TEST_PLOT = false

#SDE
println("Linear SDE Tests")
@time @test include("sde/sde_linear_tests.jl")
println("Two-dimensional Linear SDE Tests")
@time @test include("sde/sde_twodimlinear_tests.jl")
println("Additive SDE Tests")
@time @test include("sde/sde_additive_tests.jl")
println("Rossler Order Tests")
@time @test include("sde/sde_rosslerorder_tests.jl")
println("SDE Convergence Tests")
@time @test include("sde/sde_convergence_tests.jl")
println("SDE Number Type Tests")
@time @test include("sde/sde_numbertype_tests.jl")
println("Oval2")
@time @test include("oval2_test.jl")
@time @testset "Linear SDE Tests" begin include("sde/sde_linear_tests.jl") end
@time @testset "Two-dimensional Linear SDE Tests" begin include("sde/sde_twodimlinear_tests.jl") end
@time @testset "Additive SDE Tests" begin include("sde/sde_additive_tests.jl") end
@time @testset "Rossler Order Tests" begin include("sde/sde_rosslerorder_tests.jl") end
@time @testset "SDE Convergence Tests" begin include("sde/sde_convergence_tests.jl") end
@time @testset "SDE Number Type Tests" begin include("sde/sde_numbertype_tests.jl") end
@time @testset "Oval2" begin include("oval2_test.jl") end

#Adaptive SDE
#=
Expand Down
4 changes: 2 additions & 2 deletions test/sde/sde_additive_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sol =solve(prob,SRA,dt=1/2^(3))
sol =solve(prob,SRA1Optimized,dt=1/2^(3))

#Now do the simulation 10000 times in parallel. Return an array
solArr = monteCarloSim(prob,SRIW1Optimized,dt=1//2^(3),numMonte=5)
solArr = monte_carlo_simulation(prob,SRIW1Optimized,dt=1//2^(3),numMonte=5)

#First index is the sime, so sol.timeseries[1,..] is the initial condition
#Last indices are the indexes of the variables. Since our initial condition
Expand All @@ -29,4 +29,4 @@ sim = test_convergence(dts,prob,SRA,numMonte=5)

sim2 = test_convergence(dts,prob,SRA1Optimized,numMonte=5)

abs(sim.𝒪est[:l2]-2) + abs(sim2.𝒪est[:l∞]-2) <.1 #High tolerance since low dts for testing!
@test abs(sim.𝒪est[:l2]-2) + abs(sim2.𝒪est[:l∞]-2) <.1 #High tolerance since low dts for testing!
8 changes: 3 additions & 5 deletions test/sde/sde_convergence_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ sim3 = test_convergence(dts,prob,SRI,numMonte=Int(1e1))
sim4 = test_convergence(dts,prob,SRIW1Optimized,numMonte=Int(1e1))
sim5 = test_convergence(dts,prob,SRIVectorized,numMonte=Int(1e1))

bool1 = abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) + abs(sim5.𝒪est[:final]-1.5) <.5 #High tolerance since low dts for testing!
@test abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) + abs(sim5.𝒪est[:final]-1.5) <.5 #High tolerance since low dts for testing!

prob = prob_sde_cubic
sim = test_convergence(dts,prob,EM,numMonte=Int(1e1))
sim2 = test_convergence(dts,prob,RKMil,numMonte=Int(1e1))
sim3 = test_convergence(dts,prob,SRI,numMonte=Int(1e1))
sim4 = test_convergence(dts,prob,SRIW1Optimized,numMonte=Int(1e1))
bool2 = abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) <.6 #High tolerance since low dts for testing!
@test abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) <.6 #High tolerance since low dts for testing!

## Convergence Testing
prob = prob_sde_additive
Expand All @@ -27,6 +27,4 @@ sim4 = test_convergence(dts,prob,SRA,numMonte=Int(1e1))
sim5 = test_convergence(dts,prob,SRA1Optimized,numMonte=Int(1e1))
sim6 = test_convergence(dts,prob,SRIW1Optimized,numMonte=Int(1e1))
sim7 = test_convergence(dts,prob,SRAVectorized,numMonte=Int(1e1))
bool3 = abs(sim.𝒪est[:l2]-1) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-2) + abs(sim4.𝒪est[:final]-2) + abs(sim5.𝒪est[:final]-2) + abs(sim6.𝒪est[:final]-2) + abs(sim7.𝒪est[:final]-2) <.4 #High tolerance since low dts for testing!

bool1 && bool2 && bool3
@test abs(sim.𝒪est[:l2]-1) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-2) + abs(sim4.𝒪est[:final]-2) + abs(sim5.𝒪est[:final]-2) + abs(sim6.𝒪est[:final]-2) + abs(sim7.𝒪est[:final]-2) <.4 #High tolerance since low dts for testing!
2 changes: 1 addition & 1 deletion test/sde/sde_linear_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ sim3 = test_convergence(dts,prob,SRI,numMonte=NUM_MONTE)

#TEST_PLOT && plot(plot(sim),plot(sim2),plot(sim3),layout=@layout([a b c]),size=(1200,600))

abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5)<.441 #High tolerance since low dts for testing!
@test abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5)<.441 #High tolerance since low dts for testing!
2 changes: 1 addition & 1 deletion test/sde/sde_numbertype_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ sol =solve(prob,SRI,dt=1//2^(4),abstol=1,reltol=0)
err1 = sol.errors[:final]
TEST_PLOT && plot(sol,plot_analytic=true,legend=false,title="tol = 1")

true #Always gives float on SDE due to RNG
@test true
6 changes: 2 additions & 4 deletions test/sde/sde_rosslerorder_tests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using StochasticDiffEq

SRIW1 = constructSRIW1()
bool1 = minimum(checkSRIOrder(SRIW1))
@test minimum(checkSRIOrder(SRIW1))

SRA1 = constructSRA1()
bool2 = minimum(checkSRAOrder(SRA1))

bool1 && bool2
@test minimum(checkSRAOrder(SRA1))
4 changes: 2 additions & 2 deletions test/sde/sde_twodimlinear_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sol = solve(prob,SRIW1Optimized,dt=1/2^(3),progressbar=true,progress_steps=1)


#Now do the simulation 5 times in parallel. Return an array
solArr = monteCarloSim(prob,SRIW1Optimized,dt=1//2^(3),numMonte=5)
solArr = monte_carlo_simulation(prob,SRIW1Optimized,dt=1//2^(3),numMonte=5)

TEST_PLOT && plot(sol,plot_analytic=true)

Expand All @@ -32,4 +32,4 @@ sim3 = test_convergence(dts,prob,SRI,numMonte=5)

sim4 = test_convergence(dts,prob,SRIW1Optimized,numMonte=5,save_timeseries=false)

abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) <.6 #High tolerance since low dts for testing!
@test abs(sim.𝒪est[:l2]-.5) + abs(sim2.𝒪est[:l∞]-1) + abs(sim3.𝒪est[:final]-1.5) + abs(sim4.𝒪est[:final]-1.5) <.6 #High tolerance since low dts for testing!

0 comments on commit ee764f9

Please sign in to comment.