Skip to content

Commit 46b4608

Browse files
fixup! test: test parameters as initialization unknowns
1 parent a3f26ec commit 46b4608

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

test/initializationsystem.jl

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ModelingToolkit, OrdinaryDiffEq, NonlinearSolve, Test
2+
using SymbolicIndexingInterface
23
using ModelingToolkit: t_nounits as t, D_nounits as D
34

45
@parameters g
@@ -493,27 +494,69 @@ end
493494
@test init(prob, Tsit5()).ps[sym] val
494495
@test solve(prob, Tsit5()).ps[sym] val
495496
end
497+
function test_initializesystem(sys, u0map, pmap, p, equation)
498+
isys = ModelingToolkit.generate_initializesystem(sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys))
499+
@test is_variable(isys, p)
500+
@test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys)
501+
end
496502
@variables x(t) y(t)
497-
@parameters p
498-
_p = ModelingToolkit.setdefault(p, missing)
499-
_p = ModelingToolkit.setguess(_p, 0.0)
500-
@mtkbuild sys = ODESystem([D(x) ~ x, _p ~ x + y], t)
501-
prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0))
502-
test_parameter(prob, _p, 2.0)
503+
@parameters p q
504+
u0map = Dict(x => 1.0, y => 1.0)
505+
pmap = Dict()
506+
pmap[q] = 1.0
507+
# `missing` default, equation from ODEProblem
508+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing])
509+
pmap[p] = 2q
510+
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
511+
test_parameter(prob, p, 2.0)
512+
# `missing` default, provided guess
503513
@mtkbuild sys = ODESystem(
504514
[D(x) ~ x, p ~ x + y], t; defaults = [p => missing], guesses = [p => 0.0])
505-
prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0))
515+
prob = ODEProblem(sys, u0map, (0.0, 1.0))
506516
test_parameter(prob, p, 2.0)
507-
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0])
508-
prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0), [p => missing])
517+
test_initializesystem(sys, u0map, pmap, p, 0 ~ p - x - y)
518+
519+
# `missing` to ODEProblem, equation from default
520+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q])
521+
pmap[p] = missing
522+
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
523+
test_parameter(prob, p, 2.0)
524+
test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p)
525+
test_parameter(prob2, p, 2.0)
526+
# `missing` to ODEProblem, provided guess
527+
@mtkbuild sys = ODESystem(
528+
[D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0])
529+
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
509530
test_parameter(prob, p, 2.0)
531+
test_initializesystem(sys, u0map, pmap, p, 0 ~ x + y - p)
510532

511-
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 1.0])
533+
# No `missing`, default and guess
534+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 0.0])
535+
delete!(pmap, p)
536+
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
537+
test_parameter(prob, p, 2.0)
538+
test_initializesystem(sys, u0map, pmap, p, 0 ~ 2q - p)
539+
540+
# Should not be solved for:
541+
542+
# ODEProblem value with guess, no `missing`
543+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; guesses = [p => 0.0])
544+
_pmap = merge(pmap, Dict(p => 3q))
545+
prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap)
546+
@test prob.ps[p] 3.0
547+
@test prob.f.initializeprob === nothing
548+
# Default overridden by ODEProblem, guess provided
549+
@mtkbuild sys = ODESystem([D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0])
550+
prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap)
551+
@test prob.ps[p] 3.0
552+
@test prob.f.initializeprob === nothing
553+
554+
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0])
512555
@test_throws ModelingToolkit.MissingParametersError ODEProblem(
513556
sys, [x => 1.0, y => 1.0], (0.0, 1.0))
514557
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t)
515-
@test_throws ["Invalid setup", "parameter p", "guess"] ODEProblem(
516-
sys, [x => 1.0, y => 1.0], (0.0, 1.0), [p => missing])
558+
@test_throws ["Invalid setup", "parameter p", "guess"] ModelingToolkit.generate_initializesystem(
559+
sys; u0map = Dict(x => 1.0, y => 1.0), pmap = Dict(p => missing), check_defguess = true)
517560

518561
@testset "Null system" begin
519562
@variables x(t) y(t) s(t)
@@ -526,14 +569,15 @@ end
526569
using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Fixed, Mass,
527570
Spring, Force,
528571
Damper
572+
using ModelingToolkitStandardLibrary.Mechanical: TranslationalModelica as TM
529573
using ModelingToolkitStandardLibrary.Blocks: Constant
530574

531-
@named mass = Mass(; m = 1.0, s = 1.0, v = 0.0, a = 0.0)
575+
@named mass = TM.Mass(; m = 1.0, s = 1.0, v = 0.0, a = 0.0)
532576
@named fixed = Fixed(; s0 = 0.0)
533-
@named spring = Spring(; c = 2.0)
577+
@named spring = Spring(; c = 2.0, s_rel0 = nothing)
534578
@named gravity = Force()
535579
@named constant = Constant(; k = 9.81)
536-
@named damper = Damper(; d = 0.1)
580+
@named damper = TM.Damper(; d = 0.1)
537581
@mtkbuild sys = ODESystem(
538582
[connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a),
539583
connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f),

0 commit comments

Comments
 (0)