From 91fa47c202ef468320f0dab5ff4b7107bdc27473 Mon Sep 17 00:00:00 2001 From: Harrison Grodin Date: Sun, 3 Feb 2019 01:00:24 -0500 Subject: [PATCH] Introduce time-varying parameters --- src/variables.jl | 6 +++++- test/system_construction.jl | 36 ++++++++++-------------------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 7cefc3bad0..b5b7f6cc76 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -30,7 +30,11 @@ Base.:(==)(c::Constant, n::Number) = c.value == n Base.:(==)(n::Number, c::Constant) = c.value == n Base.:(==)(a::Constant, b::Constant) = a.value == b.value -Base.convert(::Type{Expr}, x::Variable) = x.name +function Base.convert(::Type{Expr}, x::Variable) + x.subtype === :Parameter || return x.name + isempty(x.dependents) && return x.name + return :($(x.name)($(convert.(Expr, x.dependents)...))) +end Base.convert(::Type{Expr}, c::Constant) = c.value Base.show(io::IO, x::Variable) = print(io, x.subtype, '(', x.name, ')') diff --git a/test/system_construction.jl b/test/system_construction.jl index ad1f56d1da..18821d970e 100644 --- a/test/system_construction.jl +++ b/test/system_construction.jl @@ -32,34 +32,18 @@ function test_vars_extraction(de, de2) end test_vars_extraction(de, de2) -# Time-varying parameters - -@test_broken begin - eqs = [D(x) ~ σ(t)*(y-x), +@testset "time-varying parameters" begin + @Param σ′(t) + eqs = [D(x) ~ σ′*(y-x), D(y) ~ x*(ρ-z)-y, D(z) ~ x*y - β*z] - de = DiffEqSystem(eqs,[t],[x,y,z],[σ,ρ,β]) - generate_function(de) - - #= - ```julia - :((du, u, p, t)->begin - x = u[1] - y = u[2] - z = u[3] - σ = p[1] - ρ = p[2] - β = p[3] - x_t = σ(t) * (y - x) - y_t = x * (ρ - z) - y - z_t = x * y - β * z - du[1] = x_t - du[2] = y_t - du[3] = z_t - end - end) - ``` - =# + de = DiffEqSystem(eqs,t,[x,y,z],[σ′,ρ,β]) + @test begin + f = eval(generate_function(de)) + du = [0.0,0.0,0.0] + f(du, [1.0,2.0,3.0], [x->x+7,2,3], 5.0) + du ≈ [12, -3, -7] + end end # Conversion to first-order ODEs #17