Skip to content

Commit

Permalink
Merge pull request #3031 from SciML/labelledarrays
Browse files Browse the repository at this point in the history
Move labelledarrays to extension testing
  • Loading branch information
ChrisRackauckas authored Sep 9, 2024
2 parents 36bc96f + 7f4a488 commit 38392d1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
Ipopt_jll = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
Expand All @@ -155,4 +154,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "LabelledArrays"]
test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"]
1 change: 1 addition & 0 deletions test/extensions/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[deps]
BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
Expand Down
48 changes: 48 additions & 0 deletions test/labelledarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,51 @@ d = LVector(x = 1.0, y = 2.0, z = 3.0)
@test ff.jac(d, p, ForwardDiff.Dual(0.0, 1.0)) isa Array
@inferred ff.jac(d, p, ForwardDiff.Dual(0.0, 1.0))
@test eltype(ff.jac(d, p, ForwardDiff.Dual(0.0, 1.0))) <: ForwardDiff.Dual

## https://github.com/SciML/ModelingToolkit.jl/issues/1054
using LabelledArrays
using ModelingToolkit

# ODE model: simple SIR model with seasonally forced contact rate
function SIR!(du, u, p, t)

# Unknowns
(S, I, R) = u[1:3]
N = S + I + R

# params
β = p.β
η = p.η
φ = p.φ
ω = 1.0 / p.ω
μ = p.μ
σ = p.σ

# FOI
βeff = β * (1.0 + η * cos(2.0 * π * (t - φ) / 365.0))
λ = βeff * I / N

# change in unknowns
du[1] =* N - λ * S - μ * S + ω * R)
du[2] =* S - σ * I - μ * I)
du[3] =* I - μ * R - ω * R)
du[4] =* I) # cumulative incidence
end

# Solver settings
tmin = 0.0
tmax = 10.0 * 365.0
tspan = (tmin, tmax)

# Initiate ODE problem
theta_fix = [1.0 / (80 * 365)]
theta_est = [0.28, 0.07, 1.0 / 365.0, 1.0, 1.0 / 5.0]
p = @LArray [theta_est; theta_fix] (, , , , , )
u0 = @LArray [9998.0, 1.0, 1.0, 1.0] (:S, :I, :R, :C)

# Initiate ODE problem
problem = ODEProblem(SIR!, u0, tspan, p)
sys = complete(modelingtoolkitize(problem))

@test all(isequal.(parameters(sys), getproperty.(@variables(β, η, ω, φ, σ, μ), :val)))
@test all(isequal.(Symbol.(unknowns(sys)), Symbol.(@variables(S(t), I(t), R(t), C(t)))))
58 changes: 1 addition & 57 deletions test/modelingtoolkitize.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using OrdinaryDiffEq, ModelingToolkit, DataStructures, Test
using Optimization, RecursiveArrayTools, OptimizationOptimJL
using LabelledArrays, SymbolicIndexingInterface
using SymbolicIndexingInterface
using ModelingToolkit: t_nounits as t, D_nounits as D
using SciMLBase: parameterless_type

Expand Down Expand Up @@ -210,54 +210,6 @@ tspan = (0.0, 1.0)
prob = ODEProblem(k, x0, tspan)
sys = modelingtoolkitize(prob)

## https://github.com/SciML/ModelingToolkit.jl/issues/1054
using LabelledArrays
using ModelingToolkit

# ODE model: simple SIR model with seasonally forced contact rate
function SIR!(du, u, p, t)

# Unknowns
(S, I, R) = u[1:3]
N = S + I + R

# params
β = p.β
η = p.η
φ = p.φ
ω = 1.0 / p.ω
μ = p.μ
σ = p.σ

# FOI
βeff = β * (1.0 + η * cos(2.0 * π * (t - φ) / 365.0))
λ = βeff * I / N

# change in unknowns
du[1] =* N - λ * S - μ * S + ω * R)
du[2] =* S - σ * I - μ * I)
du[3] =* I - μ * R - ω * R)
du[4] =* I) # cumulative incidence
end

# Solver settings
tmin = 0.0
tmax = 10.0 * 365.0
tspan = (tmin, tmax)

# Initiate ODE problem
theta_fix = [1.0 / (80 * 365)]
theta_est = [0.28, 0.07, 1.0 / 365.0, 1.0, 1.0 / 5.0]
p = @LArray [theta_est; theta_fix] (, , , , , )
u0 = @LArray [9998.0, 1.0, 1.0, 1.0] (:S, :I, :R, :C)

# Initiate ODE problem
problem = ODEProblem(SIR!, u0, tspan, p)
sys = complete(modelingtoolkitize(problem))

@test all(isequal.(parameters(sys), getproperty.(@variables(β, η, ω, φ, σ, μ), :val)))
@test all(isequal.(Symbol.(unknowns(sys)), Symbol.(@variables(S(t), I(t), R(t), C(t)))))

# https://github.com/SciML/ModelingToolkit.jl/issues/1158

function ode_prob(du, u, p::NamedTuple, t)
Expand Down Expand Up @@ -361,10 +313,6 @@ sys = modelingtoolkitize(prob)
(1.0, [:p]),
(1.0, Dict(1 => :p)),
(Dict(:a => 2, :b => 4), Dict(:a => :p, :b => :q)),
(LVector(a = 1, b = 2), [:p, :q]),
(SLVector(a = 1, b = 2), [:p, :q]),
(LVector(a = 1, b = 2), Dict(1 => :p, 2 => :q)),
(SLVector(a = 1, b = 2), Dict(1 => :p, 2 => :q)),
((a = 1, b = 2), (a = :p, b = :q)),
((a = 1, b = 2), Dict(:a => :p, :b => :q))
]
Expand All @@ -390,10 +338,6 @@ sys = modelingtoolkitize(prob)
(1.0, [:p, :q]),
(1.0, Dict(1 => :p, 2 => :q)),
(Dict(:a => 2, :b => 4), Dict(:a => :p, :b => :q, :c => :r)),
(LVector(a = 1, b = 2), [:p, :q, :r]),
(SLVector(a = 1, b = 2), [:p, :q, :r]),
(LVector(a = 1, b = 2), Dict(1 => :p, 2 => :q, 3 => :r)),
(SLVector(a = 1, b = 2), Dict(1 => :p, 2 => :q, 3 => :r)),
((a = 1, b = 2), (a = :p, b = :q, c = :r)),
((a = 1, b = 2), Dict(:a => :p, :b => :q, :c => :r))
]
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ end
@safetestset "ODESystem Test" include("odesystem.jl")
@safetestset "Dynamic Quantities Test" include("dq_units.jl")
@safetestset "Unitful Quantities Test" include("units.jl")
@safetestset "LabelledArrays Test" include("labelledarrays.jl")
@safetestset "Mass Matrix Test" include("mass_matrix.jl")
@safetestset "InitializationSystem Test" include("initializationsystem.jl")
@safetestset "Guess Propagation" include("guess_propagation.jl")
Expand Down Expand Up @@ -110,5 +109,6 @@ end
activate_extensions_env()
@safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl")
@safetestset "Auto Differentiation Test" include("extensions/ad.jl")
@safetestset "LabelledArrays Test" include("labelledarrays.jl")
end
end

0 comments on commit 38392d1

Please sign in to comment.