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