From 91c7b5a3085fb543d51eadf91e72b8f65996e96a Mon Sep 17 00:00:00 2001 From: oameye Date: Mon, 25 Mar 2024 09:51:00 +0100 Subject: [PATCH] add StochSystem unit tests --- test/stochsystem.jl | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/stochsystem.jl b/test/stochsystem.jl index 2eb86db0..731df1fb 100644 --- a/test/stochsystem.jl +++ b/test/stochsystem.jl @@ -1,4 +1,5 @@ using CriticalTransitions, StaticArrays +CT = CriticalTransitions function meier_stein(u, p, t) # out-of-place x, y = u @@ -13,3 +14,83 @@ sys = StochSystem(meier_stein, [], zeros(2)) tspan = (0.0, 100.0) prob = ODEProblem{false}(meier_stein, SA[0.0, 0.0], tspan, ()) kpo = CoupledODEs(prob) # DynamicalSystems method + +using Test + +# Test StochSystem constructors +@testset "StochSystem constructors" begin + f(x,p,t) = x^2 + sys1 = StochSystem(f, [1], [0]) + @test sys1.f(2, sys1.pf, 0) == 4 + @test sys1.u == [0] + @test sys1.σ == 0.0 + @test sys1.g == idfunc + @test sys1.pg == nothing + @test sys1.Σ == I(1) + @test sys1.process == "WhiteGauss" + + sys2 = StochSystem(f, [1], [0], 0.5) + @test sys2.f(2, sys2.pf, 0) == 4 + @test sys2.u == [0] + @test sys2.σ == 0.5 + @test sys2.g == idfunc + @test sys2.pg == nothing + @test sys2.Σ == I(1) + @test sys2.process == "WhiteGauss" + + Σ = [true false; false true] + sys3 = StochSystem(f, [1], [0], 0.5, Σ) + @test sys3.f(2, sys3.pf, 0) == 4 + @test sys3.u == [0] + @test sys3.σ == 0.5 + @test sys3.g == idfunc + @test sys3.pg == nothing + @test sys3.Σ == Σ + @test sys3.process == "WhiteGauss" +end + +# Test drift function +@testset "drift" begin + f(x,p,t) = x.^2 + sys = StochSystem(f, [1], [0]) + @test drift(sys, [2]) == [4] +end + +# Test σg function +@testset "σg" begin + f(x,p,t) = x^2 + sys = StochSystem(f, [1], [0], 0.5) + @test CT.σg(sys)(2, sys.pf, 0) == 0.5 .* CT.idfunc(2, sys.pf, 0) +end + +# Test p function +@testset "p" begin + f(x,p,t) = x^2 + sys = StochSystem(f, [1], [0], 0.5) + @test CT.p(sys) == [[1], nothing] +end + +# Test CoupledODEs function +@testset "CoupledODEs" begin + f(x,p,t) = x.^2 + sys = StochSystem(f, [1.0], [0.0], 0.5) + cds = CoupledODEs(sys) + @test cds.integ.f([2], sys.pf, 0) == [4] + @test cds.integ.u == [0] + @test cds.p0 == sys.pf +end + + +# Test StochSystem conversion from CoupledODEs +@testset "StochSystem from CoupledODEs" begin + f(x,p,t) = x.^2 + cds = CoupledODEs(f, [0.0], [0.0]) + sys = StochSystem(cds, 0.5, idfunc, nothing, I(1), "WhiteGauss") + @test sys.f([2], sys.pf, 0) == [4] + @test sys.u == [0] + @test sys.σ == 0.5 + @test sys.g == idfunc + @test sys.pg == nothing + @test sys.Σ == I(1) + @test sys.process == "WhiteGauss" +end