@@ -4,101 +4,101 @@ using Test, LinearAlgebra
4
4
5
5
# Change of variables: z = log(x)
6
6
# (this implies that x = exp(z) is automatically non-negative)
7
- # @independent_variables t
8
- # @variables z(t)[1:2, 1:2]
9
- # D = Differential(t)
10
- # eqs = [D(D(z)) ~ ones(2, 2)]
11
- # @mtkcompile sys = System(eqs, t)
12
- # @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0))
7
+ @independent_variables t
8
+ @variables z (t)[1 : 2 , 1 : 2 ]
9
+ D = Differential (t)
10
+ eqs = [D (D (z)) ~ ones (2 , 2 )]
11
+ @mtkcompile sys = System (eqs, t)
12
+ @test_nowarn ODEProblem (sys, [z => zeros (2 , 2 ), D (z) => ones (2 , 2 )], (0.0 , 10.0 ))
13
13
14
- # @parameters α
15
- # @variables x(t)
16
- # D = Differential(t)
17
- # eqs = [D(x) ~ α*x]
14
+ @parameters α
15
+ @variables x (t)
16
+ D = Differential (t)
17
+ eqs = [D (x) ~ α* x]
18
18
19
- # tspan = (0., 1.)
20
- # def = [x => 1.0, α => -0.5]
19
+ tspan = (0. , 1. )
20
+ def = [x => 1.0 , α => - 0.5 ]
21
21
22
- # @mtkcompile sys = System(eqs, t;defaults=def)
23
- # prob = ODEProblem(sys, [], tspan)
24
- # sol = solve(prob, Tsit5())
22
+ @mtkcompile sys = System (eqs, t;defaults= def)
23
+ prob = ODEProblem (sys, [], tspan)
24
+ sol = solve (prob, Tsit5 ())
25
25
26
- # @variables z(t)
27
- # forward_subs = [log(x) => z]
28
- # backward_subs = [x => exp(z)]
29
- # new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
30
- # @test equations(new_sys)[1] == (D(z) ~ α)
26
+ @variables z (t)
27
+ forward_subs = [log (x) => z]
28
+ backward_subs = [x => exp (z)]
29
+ new_sys = changeofvariables (sys, t, forward_subs, backward_subs)
30
+ @test equations (new_sys)[1 ] == (D (z) ~ α)
31
31
32
- # new_prob = ODEProblem(new_sys, [], tspan)
33
- # new_sol = solve(new_prob, Tsit5())
32
+ new_prob = ODEProblem (new_sys, [], tspan)
33
+ new_sol = solve (new_prob, Tsit5 ())
34
34
35
- # @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4)
35
+ @test isapprox (new_sol[x][end ], sol[x][end ], atol= 1e-4 )
36
36
37
37
38
38
39
- # # Riccati equation
40
- # @parameters α
41
- # @variables x(t)
42
- # D = Differential(t)
43
- # eqs = [D(x) ~ t^2 + α - x^2]
44
- # def = [x=>1., α => 1.]
45
- # @mtkcompile sys = System(eqs, t; defaults=def)
39
+ # Riccati equation
40
+ @parameters α
41
+ @variables x (t)
42
+ D = Differential (t)
43
+ eqs = [D (x) ~ t^ 2 + α - x^ 2 ]
44
+ def = [x=> 1. , α => 1. ]
45
+ @mtkcompile sys = System (eqs, t; defaults= def)
46
46
47
- # @variables z(t)
48
- # forward_subs = [t + α/(x+t) => z ]
49
- # backward_subs = [ x => α/(z-t) - t]
47
+ @variables z (t)
48
+ forward_subs = [t + α/ (x+ t) => z ]
49
+ backward_subs = [ x => α/ (z- t) - t]
50
50
51
- # new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.)
52
- # # output should be equivalent to
53
- # # t^2 + α - z^2 + 2 (but this simplification is not found automatically)
51
+ new_sys = changeofvariables (sys, t, forward_subs, backward_subs; simplify= true , t0= 0. )
52
+ # output should be equivalent to
53
+ # t^2 + α - z^2 + 2 (but this simplification is not found automatically)
54
54
55
- # tspan = (0., 1.)
56
- # prob = ODEProblem(sys,[],tspan)
57
- # new_prob = ODEProblem(new_sys,[],tspan)
55
+ tspan = (0. , 1. )
56
+ prob = ODEProblem (sys,[],tspan)
57
+ new_prob = ODEProblem (new_sys,[],tspan)
58
58
59
- # sol = solve(prob, Tsit5())
60
- # new_sol = solve(new_prob, Tsit5())
59
+ sol = solve (prob, Tsit5 ())
60
+ new_sol = solve (new_prob, Tsit5 ())
61
61
62
- # @test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4)
62
+ @test isapprox (sol[x][end ], new_sol[x][end ], rtol= 1e-4 )
63
63
64
64
65
- # # Linear transformation to diagonal system
66
- # @independent_variables t
67
- # @variables x(t)[1:3]
68
- # x = reshape(x, 3, 1)
69
- # D = Differential(t)
70
- # A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.]
71
- # right = A*x
72
- # eqs = vec(D.(x) .~ right)
65
+ # Linear transformation to diagonal system
66
+ @independent_variables t
67
+ @variables x (t)[1 : 3 ]
68
+ x = reshape (x, 3 , 1 )
69
+ D = Differential (t)
70
+ A = [0. - 1. 0. ; - 0.5 0.5 0. ; 0. 0. - 1. ]
71
+ right = A* x
72
+ eqs = vec (D .(x) .~ right)
73
73
74
- # tspan = (0., 10.)
75
- # u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0]
74
+ tspan = (0. , 10. )
75
+ u0 = [x[1 ] => 1.0 , x[2 ] => 2.0 , x[3 ] => - 1.0 ]
76
76
77
- # @mtkcompile sys = System(eqs, t; defaults=u0)
78
- # prob = ODEProblem(sys,[],tspan)
79
- # sol = solve(prob, Tsit5())
77
+ @mtkcompile sys = System (eqs, t; defaults= u0)
78
+ prob = ODEProblem (sys,[],tspan)
79
+ sol = solve (prob, Tsit5 ())
80
80
81
- # T = eigen(A).vectors
82
- # T_inv = inv(T)
81
+ T = eigen (A). vectors
82
+ T_inv = inv (T)
83
83
84
- # @variables z(t)[1:3]
85
- # z = reshape(z, 3, 1)
86
- # forward_subs = vec(T_inv*x .=> z)
87
- # backward_subs = vec(x .=> T*z)
84
+ @variables z (t)[1 : 3 ]
85
+ z = reshape (z, 3 , 1 )
86
+ forward_subs = vec (T_inv* x .=> z)
87
+ backward_subs = vec (x .=> T* z)
88
88
89
- # new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true)
89
+ new_sys = changeofvariables (sys, t, forward_subs, backward_subs; simplify= true )
90
90
91
- # new_prob = ODEProblem(new_sys, [], tspan)
92
- # new_sol = solve(new_prob, Tsit5())
91
+ new_prob = ODEProblem (new_sys, [], tspan)
92
+ new_sol = solve (new_prob, Tsit5 ())
93
93
94
- # # test RHS
95
- # new_rhs = [eq.rhs for eq in equations(new_sys)]
96
- # new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z))
97
- # A = diagm(eigen(A).values)
98
- # A = sortslices(A, dims=1)
99
- # new_A = sortslices(new_A, dims=1)
100
- # @test isapprox(A, new_A, rtol = 1e-10)
101
- # @test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4)
94
+ # test RHS
95
+ new_rhs = [eq. rhs for eq in equations (new_sys)]
96
+ new_A = Symbolics. value .(Symbolics. jacobian (new_rhs, z))
97
+ A = diagm (eigen (A). values)
98
+ A = sortslices (A, dims= 1 )
99
+ new_A = sortslices (new_A, dims= 1 )
100
+ @test isapprox (A, new_A, rtol = 1e-10 )
101
+ @test isapprox ( new_sol[x[1 ],end ], sol[x[1 ],end ], rtol= 1e-4 )
102
102
103
103
# Change of variables for sde
104
104
noise_eqs = ModelingToolkit. get_noise_eqs
@@ -115,7 +115,7 @@ def = [x=>0., μ => 2., σ=>1.]
115
115
@mtkcompile sys = System (eqs, t; defaults= def)
116
116
forward_subs = [log (x) => y]
117
117
backward_subs = [x => exp (y)]
118
- new_sys = change_of_variable_SDE (sys, t, forward_subs, backward_subs)
118
+ new_sys = changeofvariables (sys, t, forward_subs, backward_subs)
119
119
@test equations (new_sys)[1 ] == (D (y) ~ μ - 1 / 2 * σ^ 2 )
120
120
@test noise_eqs (new_sys)[1 ] === value (σ)
121
121
@@ -130,7 +130,7 @@ def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.]
130
130
@mtkcompile sys = System (eqs, t; defaults= def)
131
131
forward_subs = [log (x) => z, y^ 2 => w, log (u) => v]
132
132
backward_subs = [x => exp (z), y => w^ .5 , u => exp (v)]
133
- new_sys = change_of_variable_SDE (sys, t, forward_subs, backward_subs)
133
+ new_sys = changeofvariables (sys, t, forward_subs, backward_subs)
134
134
@test equations (new_sys)[1 ] == (D (z) ~ μ - 1 / 2 * σ^ 2 )
135
135
@test equations (new_sys)[2 ] == (D (w) ~ α^ 2 )
136
136
@test equations (new_sys)[3 ] == (D (v) ~ μ - 1 / 2 * (α^ 2 + σ^ 2 ))
0 commit comments