| 
1 | 1 | using ModelingToolkit, Test  | 
2 | 2 | using ModelingToolkit: t_nounits as t  | 
 | 3 | +using StableRNGs  | 
3 | 4 | 
 
  | 
4 | 5 | k = ShiftIndex(t)  | 
5 |  | -@variables x(t) = 1  | 
6 |  | -@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)  | 
7 |  | -tspan = (0, 10)  | 
 | 6 | +rng = StableRNG(22525)   | 
8 | 7 | 
 
  | 
9 | 8 | # Shift(t, -1)(x(t)) - x_{t-1}(t)  | 
10 | 9 | # -3 - x(t) + x(t)*x_{t-1}  | 
11 |  | -f = ImplicitDiscreteFunction(sys)  | 
12 |  | -u_next = [3., 1.5]  | 
13 |  | -@test f(u_next, [2.,3.], [], t) ≈ [0., 0.]  | 
14 |  | -u_next = [0., 0.]  | 
15 |  | -@test f(u_next, [2.,3.], [], t) ≈ [3., -3.]  | 
16 |  | - | 
17 |  | -resid = rand(2)  | 
18 |  | -f(resid, u_next, [2.,3.], [], t)  | 
19 |  | -@test resid ≈ [3., -3.]  | 
20 |  | - | 
21 |  | -prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan)  | 
22 |  | -@test prob.u0 == [3., 1.]  | 
23 |  | -prob = ImplicitDiscreteProblem(sys, [], tspan)  | 
24 |  | -@test prob.u0 == [1., 1.]  | 
25 |  | -@variables x(t)  | 
26 |  | -@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)  | 
27 |  | -@test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan)  | 
 | 10 | +@testset "Correct ImplicitDiscreteFunction" begin  | 
 | 11 | +    @variables x(t) = 1  | 
 | 12 | +    @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)  | 
 | 13 | +    tspan = (0, 10)  | 
 | 14 | +    f = ImplicitDiscreteFunction(sys)  | 
 | 15 | +    u_next = [3., 1.5]  | 
 | 16 | +    @test f(u_next, [2.,3.], [], t) ≈ [0., 0.]  | 
 | 17 | +    u_next = [0., 0.]  | 
 | 18 | +    @test f(u_next, [2.,3.], [], t) ≈ [3., -3.]  | 
 | 19 | +      | 
 | 20 | +    resid = rand(2)  | 
 | 21 | +    f(resid, u_next, [2.,3.], [], t)  | 
 | 22 | +    @test resid ≈ [3., -3.]  | 
 | 23 | +      | 
 | 24 | +    prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan)  | 
 | 25 | +    @test prob.u0 == [3., 1.]  | 
 | 26 | +    prob = ImplicitDiscreteProblem(sys, [], tspan)  | 
 | 27 | +    @test prob.u0 == [1., 1.]  | 
 | 28 | +    @variables x(t)  | 
 | 29 | +    @mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)  | 
 | 30 | +    @test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan)  | 
 | 31 | +end  | 
28 | 32 | 
 
  | 
29 | 33 | # Test solvers  | 
30 | 34 | @testset "System with algebraic equations" begin  | 
31 | 35 |     @variables x(t) y(t)  | 
32 | 36 |     eqs = [x(k) ~ x(k-1) + x(k-2),   | 
33 | 37 |            x^2 ~ 1 - y^2]  | 
34 | 38 |     @mtkbuild sys = ImplicitDiscreteSystem(eqs, t)  | 
 | 39 | +    f = ImplicitDiscreteFunction(sys)  | 
 | 40 | + | 
 | 41 | +    function correct_f(u_next, u, p, t)   | 
 | 42 | +        [u[2] - u_next[1],  | 
 | 43 | +         u[1] + u[2] - u_next[2],  | 
 | 44 | +         1 - (u_next[1]+u_next[2])^2 - u_next[3]^2]  | 
 | 45 | +    end  | 
 | 46 | + | 
 | 47 | +    for _ in 1:10  | 
 | 48 | +        u_next = rand(rng, 3)  | 
 | 49 | +        u = rand(rng, 3)  | 
 | 50 | +        @test correct_f(u_next, u, [], 0.) ≈ f(u_next, u, [], 0.)  | 
 | 51 | +    end  | 
 | 52 | + | 
 | 53 | +    # Initialization is satisfied.  | 
 | 54 | +    prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan)  | 
 | 55 | +    @test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 ≈ 1  | 
35 | 56 | end  | 
36 | 57 | 
 
  | 
37 | 58 | @testset "System with algebraic equations, implicit difference equations, explicit difference equations" begin  | 
 | 
40 | 61 |            y(k) ~ x(k) + x(k-2)*y(k-1)]  | 
41 | 62 |     @mtkbuild sys = ImplicitDiscreteSystem(eqs, t)  | 
42 | 63 | end  | 
43 |  | - | 
 | 
0 commit comments