Skip to content

Commit e5c40a6

Browse files
test: test simplification independent of metadata
1 parent e99fd80 commit e5c40a6

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/structural_transformation/utils.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using UnPack
66
using ModelingToolkit: t_nounits as t, D_nounits as D, default_toterm
77
using Symbolics: unwrap
88
using DataInterpolations
9+
using OrdinaryDiffEq, NonlinearSolve, StochasticDiffEq
910
const ST = StructuralTransformations
1011

1112
# Define some variables
@@ -386,3 +387,52 @@ end
386387
@test D(sys.k(t)) in vs
387388
end
388389
end
390+
391+
@testset "Don't rely on metadata" begin
392+
@testset "ODESystem" begin
393+
@variables x(t) p
394+
@parameters y(t) q
395+
@mtkbuild sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y],
396+
[p, q]; initialization_eqs = [p + q ~ 3],
397+
defaults = [p => missing], guesses = [p => 1.0, y => 1.0])
398+
@test length(equations(sys)) == 2
399+
@test length(parameters(sys)) == 2
400+
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [q => 2.0])
401+
integ = init(prob, Rodas5P(); abstol = 1e-10, reltol = 1e-8)
402+
@test integ.ps[p]1.0 atol=1e-6
403+
@test integ[y]0.0 atol=1e-5
404+
end
405+
406+
@testset "NonlinearSystem" begin
407+
@variables x p
408+
@parameters y q
409+
@mtkbuild sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y],
410+
[p, q]; initialization_eqs = [p ~ q + 1],
411+
guesses = [p => 1.0], defaults = [p => missing])
412+
@test length(equations(sys)) == length(unknowns(sys)) == 1
413+
@test length(observed(sys)) == 1
414+
@test observed(sys)[1].lhs in Set([x, y])
415+
@test length(parameters(sys)) == 2
416+
prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [q => 1.0])
417+
integ = init(prob, NewtonRaphson())
418+
@test prob.ps[p] 2.0
419+
end
420+
421+
@testset "SDESystem" begin
422+
@variables x(t) p a
423+
@parameters y(t) q b
424+
@brownian c
425+
@mtkbuild sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y],
426+
[p, q], [a, b, c]; initialization_eqs = [p + q ~ 4],
427+
guesses = [p => 1.0], defaults = [p => missing])
428+
@test length(equations(sys)) == 2
429+
@test issetequal(unknowns(sys), [x, y])
430+
@test issetequal(parameters(sys), [p, q])
431+
@test isempty(brownians(sys))
432+
neqs = ModelingToolkit.get_noise_eqs(sys)
433+
@test issetequal(sum.(eachrow(neqs)), [q, 1 + p])
434+
prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), [q => 1.0])
435+
integ = init(prob, ImplicitEM())
436+
@test integ.ps[p] 3.0
437+
end
438+
end

0 commit comments

Comments
 (0)