Skip to content

Commit 0b2deda

Browse files
committed
Instantaneous test API
1 parent 2fd76d0 commit 0b2deda

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/ModelTesting.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ include("test/continuous/delta_sol.jl")
88
include("test/discrete/single_shooting.jl")
99
include("test/discrete/merge.jl")
1010
include("test/metric/metric.jl")
11+
include("test/instantaneous/instant.jl")
1112
include("problem_config/problem.jl")
1213
end

src/test/instantaneous/instant.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function test_instantaneous(
2+
sys::ModelingToolkit.AbstractSystem,
3+
ic,
4+
checks::Num;
5+
t = nothing) # todo: after porting to v9 use InitializationSystem to solve the checks system
6+
if !SymbolicIndexingInterface.is_observed(checks)
7+
@assert false "The given check values are not observed values of the given system"
8+
end
9+
sv = ModelingToolkit.varmap_to_vars(ic, states(sys); defaults=ModelingToolkit.defaults(sys))
10+
pv = ModelingToolkit.varmap_to_vars(ic, parameters(sys); defaults=ModelingToolkit.defaults(sys))
11+
12+
obsfun = SymbolicIndexingInterface.observed(sys, checks)
13+
if SymbolicIndexingInterface.istimedependent(sys)
14+
@assert !isnothing(t) "The kwarg t must be given (and be a non-nothing value) if the system is time dependent"
15+
return obsfun(sv, pv, t)
16+
elseif !SymbolicIndexingInterface.constant_structure(sys)
17+
@assert false "The system's structure must be constant to use test_instantaneous; to be fixed" # TODO
18+
else
19+
return obsfun(sv, pv)
20+
end
21+
end
22+
23+
function test_instantaneous(
24+
sys::ModelingToolkit.AbstractSystem,
25+
ic,
26+
checks::Array;
27+
t = nothing) # todo: after porting to v9 use InitializationSystem to solve the checks system
28+
if !all(SymbolicIndexingInterface.is_observed.((sys, ), checks))
29+
@assert false "The given check values are not observed values of the given system"
30+
end
31+
sv = ModelingToolkit.varmap_to_vars(ic, states(sys); defaults=ModelingToolkit.defaults(sys))
32+
pv = ModelingToolkit.varmap_to_vars(ic, parameters(sys); defaults=ModelingToolkit.defaults(sys))
33+
34+
obsfuns = SymbolicIndexingInterface.observed.((ODEFunction(sys),), checks)
35+
if SymbolicIndexingInterface.is_time_dependent(sys)
36+
@assert !isnothing(t) "The kwarg t must be given (and be a non-nothing value) if the system is time dependent"
37+
return map(fun -> fun(sv, pv, t), obsfuns)
38+
elseif !SymbolicIndexingInterface.constant_structure(sys)
39+
@assert false "The system's structure must be constant to use test_instantaneous; to be fixed" # TODO
40+
else
41+
return map(fun -> fun(sv, pv), obsfuns)
42+
end
43+
end
44+
45+
export test_instantaneous

test/block_modeling.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ using ModelingToolkitStandardLibrary.Blocks: Constant
6464
sys = structural_simplify(rc_model)
6565
prob1 = ODEProblem(sys, Pair[], (0, 10.0))
6666
sol1 = solve(prob1, Tsit5())
67+
68+
ref = DataFrame(:timestamp => sol1.t, Symbol(resistor.v) => sol1[resistor.v])
69+
measure, measured_sys = compare_data(resistor.v, ref)(sys)
70+
prob_measured = ODEProblem(measured_sys, Pair[], (0, 10.0))
71+
sol_measured = solve(prob_measured, Tsit5())
72+
6773
prob2 = ODEProblem(sys, Pair[capacitor.C => 0.9], (0, 10.0))
6874
sol2 = solve(prob2, Tsit5())
6975
prob3 = ODEProblem(sys, Pair[capacitor.C => 5.0], (0, 10.0))

test/instantaneous.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@testset "Instantaneous" begin
2+
@testset "RC Functional" begin
3+
R = 1.0
4+
C = 1.0
5+
V = 1.0
6+
@variables t
7+
@named resistor = Resistor(R = R)
8+
@named capacitor = Capacitor(C = C)
9+
@named source = Voltage()
10+
@named constant = Constant(k = V)
11+
@named ground = Ground()
12+
13+
@named sensor = PotentialSensor()
14+
@named key_parameter = Measurement(sensor)
15+
16+
rc_eqs = [connect(constant.output, source.V)
17+
connect(source.p, resistor.p)
18+
connect(resistor.n, capacitor.p)
19+
connect(capacitor.n, source.n, ground.g)
20+
connect(sensor.p, capacitor.p)]
21+
22+
@named rc_model = ODESystem(rc_eqs, t,
23+
systems = [resistor, capacitor, constant, source, ground, sensor, key_parameter])
24+
sys = structural_simplify(rc_model)
25+
@test test_instantaneous(sys, [], [resistor.v]; t = 0.0)[1] > 0
26+
@test test_instantaneous(sys, [], [resistor.i]; t = 0.0)[1] > 0
27+
@test test_instantaneous(sys, [constant.k => 0.0], [resistor.v]; t = 0.0)[1] == 0
28+
@test test_instantaneous(sys, [constant.k => 5.0], [resistor.v]; t = 0.0)[1] == 5
29+
end
30+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ using Test
77
@testset "ModelTesting.jl" begin
88
include("timeseries.jl")
99
include("block_modeling.jl")
10+
include("instantaneous.jl")
1011
end

0 commit comments

Comments
 (0)