From d8210f59bd90a7957a8409cdda588ab5534b0868 Mon Sep 17 00:00:00 2001 From: mforets Date: Thu, 2 Apr 2020 13:51:57 -0300 Subject: [PATCH] add tests --- src/Algorithms/TMJets/post.jl | 6 ++- src/Flowpipes/fields.jl | 16 +++++++ .../init_DifferentialEquations.jl | 16 ------- test/algorithms/TMJets.jl | 27 ++++++++++++ test/models/linear/exponential1D.jl | 7 ++++ test/models/nonlinear/vanderpol.jl | 42 +++++++++++++++++++ test/runtests.jl | 9 +++- 7 files changed, 105 insertions(+), 18 deletions(-) create mode 100644 test/algorithms/TMJets.jl create mode 100644 test/models/linear/exponential1D.jl create mode 100644 test/models/nonlinear/vanderpol.jl diff --git a/src/Algorithms/TMJets/post.jl b/src/Algorithms/TMJets/post.jl index 8b8e92e92..3515c22da 100644 --- a/src/Algorithms/TMJets/post.jl +++ b/src/Algorithms/TMJets/post.jl @@ -7,7 +7,11 @@ function post(alg::TMJets{N}, ivp::IVP{<:AbstractContinuousSystem}, tspan; kwarg T = tend(tspan) # vector field - f! = VectorField(ivp) + if islinear(ivp) || isaffine(ivp) # TODO: refactor with inplace_field! + f! = inplace_field!(ivp) + else + f! = VectorField(ivp) + end n = statedim(ivp) # initial set diff --git a/src/Flowpipes/fields.jl b/src/Flowpipes/fields.jl index 598460e50..99f400c2a 100644 --- a/src/Flowpipes/fields.jl +++ b/src/Flowpipes/fields.jl @@ -41,3 +41,19 @@ end function vector_field(sys::AbstractSystem, args...) return evaluate(VectorField(sys), args...) end + +function outofplace_field(ivp::InitialValueProblem) + # function closure over the inital-value problem + f = function f_outofplace(x, p, t) + VectorField(ivp)(x) + end + return f +end + +function inplace_field!(ivp::InitialValueProblem) + # function closure over the inital-value problem + f! = function f_inplace!(dx, x, p, t) + dx .= VectorField(ivp)(x) + end + return f_inplace! +end diff --git a/src/Initialization/init_DifferentialEquations.jl b/src/Initialization/init_DifferentialEquations.jl index 37cfafb26..3304a7ded 100644 --- a/src/Initialization/init_DifferentialEquations.jl +++ b/src/Initialization/init_DifferentialEquations.jl @@ -1,21 +1,5 @@ using .DifferentialEquations -function outofplace_field(ivp::InitialValueProblem) - # function closure over the inital-value problem - f = function f_outofplace(x, p, t) - VectorField(ivp)(x) - end - return f -end - -function inplace_field!(ivp::InitialValueProblem) - # function closure over the inital-value problem - f! = function f_inplace!(dx, x, p, t) - dx .= VectorField(ivp)(x) - end - return f_inplace! -end - # extend the solve API for initial-value problems function DifferentialEquations.solve(ivp::InitialValueProblem, args...; kwargs...) tspan = _get_tspan(args...; kwargs...) diff --git a/test/algorithms/TMJets.jl b/test/algorithms/TMJets.jl new file mode 100644 index 000000000..1d3f207c0 --- /dev/null +++ b/test/algorithms/TMJets.jl @@ -0,0 +1,27 @@ +@testset "TMJets with a linear IVP" begin + prob, tspan = exponential_1d() + sol = solve(prob, tspan=tspan, TMJets()) + @test sol.alg isa TMJets +end + +@testset "TMJets interface" begin + prob, tspan = vanderpol() + + # default algorithm for nonlinear systems + sol = solve(prob, tspan=tspan) + @test sol.alg isa TMJets + + # pass the algorithm explicitly + sol = solve(prob, tspan=tspan, TMJets()) + @test sol.alg isa TMJets + + # TODO: try different options +end + +#= +alg = TMJets(abs_tol=1e-10, orderT=10, orderQ=2) + +# reach mode +sol = solve(P, T=7.0, alg) +@test set(sol[1]) isa Hyperrectangle # check default set representation +=# diff --git a/test/models/linear/exponential1D.jl b/test/models/linear/exponential1D.jl new file mode 100644 index 000000000..f65d9d4c8 --- /dev/null +++ b/test/models/linear/exponential1D.jl @@ -0,0 +1,7 @@ +function exponential_1d() + s = @system(x' = -x) + x0 = Interval(0.4, 0.5) + prob = InitialValueProblem(s, x0) + tspan = (0.0, 1.0) + return prob, tspan +end diff --git a/test/models/nonlinear/vanderpol.jl b/test/models/nonlinear/vanderpol.jl new file mode 100644 index 000000000..5131a9844 --- /dev/null +++ b/test/models/nonlinear/vanderpol.jl @@ -0,0 +1,42 @@ +# ======================================= +# Van der Pol oscillator +# #include("models/lotka_volterra.jl") +# https://en.wikipedia.org/wiki/Van_der_Pol_oscillator +# Number of state variables: 2 +# ======================================= + +@taylorize function vanderPol!(dx, x, params, t) + local μ = 1.0 + dx[1] = x[2] + dx[2] = (μ * x[2]) * (1 - x[1]^2) - x[1] + return dx +end + +function vanderpol() + X0 = Hyperrectangle(low=[1.25, 2.35], high=[1.55, 2.45]) + prob = @ivp(x' = vanderPol!(x), dim: 2, x(0) ∈ X0) + tspan = (0.0, 5.0) + + return prob, tspan +end + +#= +OLD + +# check mode +property = (t, x) -> x[2] <= 2.75 +sol = solve(P, T=7.0, property=property, alg) + +# test set representation option +sol = solve(P, T=7.0, setrep=:Hyperrectangle, alg) +@test set(sol[1]) isa Hyperrectangle + +sol = solve(P, T=7.0, setrep=:IntervalBox, alg) +@test set(sol[1]) isa IntervalBox + +sol = solve(P, T=7.0, setrep=:Zonotope, alg) +@test set(sol[1]) isa Zonotope + +sol = solve(P, T=7.0, setrep=:TaylorModel, alg) +@test set(sol[1]) isa TaylorModel +=# diff --git a/test/runtests.jl b/test/runtests.jl index cb2f1a529..5d8d954e6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,13 @@ include("solve.jl") include("reachsets.jl") #include("traces.jl") +# load test models +include("models/linear/exponential1D.jl") +include("models/nonlinear/vanderpol.jl") + +# check algorithms +#include("algorithms/INT.jl") +#include("algorithms/BOX.jl") #include("algorithms/GLGM06.jl") #include("algorithms/BFFPSV18.jl") -#include("algorithms/TMJets.jl") +include("algorithms/TMJets.jl")