@@ -25,6 +25,15 @@ D = Differential(t)
25
25
@test sol[end , end ] ≈ 1.0742818931017244
26
26
end
27
27
28
+ @testset " Change independent variable (trivial)" begin
29
+ @variables x (t) y (t)
30
+ eqs1 = [D (D (x)) ~ D (x) + x, D (y) ~ 1 ]
31
+ M1 = ODESystem (eqs1, t; name = :M ) |> complete
32
+ M2 = ModelingToolkit. change_independent_variable (M1, M1. y)
33
+ eqs2 = substitute (equations (M2), M2. y => M1. t) # system should be equivalent when parametrized with y (since D(y) ~ 1), so substitute back ...
34
+ @test eqs1[1 ] == only (eqs2) # ... and check that the equations are unmodified
35
+ end
36
+
28
37
@testset " Change independent variable" begin
29
38
@variables x (t) y (t) z (t) s (t)
30
39
eqs = [
75
84
@testset " Change independent variable (simple)" begin
76
85
@variables x (t)
77
86
Mt = ODESystem ([D (x) ~ 2 * x], t; name = :M ) |> complete
78
- Mx = ModelingToolkit. change_independent_variable (Mt, Mt. x)
87
+ Mx = ModelingToolkit. change_independent_variable (Mt, Mt. x; dummies = true )
79
88
@test (@variables x x_t (x) x_tt (x); Set (equations (Mx)) == Set ([x_t ~ 2 x, x_tt ~ 4 x]))
80
89
end
81
90
82
91
@testset " Change independent variable (free fall)" begin
83
92
@variables x (t) y (t)
84
93
@parameters g v # gravitational acceleration and constant horizontal velocity
85
94
Mt = ODESystem ([D (D (y)) ~ - g, D (x) ~ v], t; name = :M ) |> complete # gives (x, y) as function of t, ...
86
- Mx = ModelingToolkit. change_independent_variable (Mt, Mt. x) # ... but we want y as a function of x
95
+ Mx = ModelingToolkit. change_independent_variable (Mt, Mt. x; dummies = false ) # ... but we want y as a function of x
87
96
Mx = structural_simplify (Mx; allow_symbolic = true )
88
97
Dx = Differential (Mx. x)
89
98
prob = ODEProblem (Mx, [Mx. y => 0.0 , Dx (Mx. y) => 1.0 ], (0.0 , 20.0 ), [g => 9.81 , v => 10.0 ]) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities
90
99
sol = solve (prob, Tsit5 (); reltol = 1e-5 )
91
- @test all (isapprox .(sol[Mx. y], sol[Mx. x - g* (Mx. x/ v)^ 2 / 2 ]; atol = 1e-10 )) # eliminate t from anal solution x(t) = v*t, y(t) = v*t - g*t^2/2
100
+ @test all (isapprox .(sol[Mx. y], sol[Mx. x - g* (Mx. x/ v)^ 2 / 2 ]; atol = 1e-10 )) # compare to analytical solution ( x(t) = v*t, y(t) = v*t - g*t^2/2)
92
101
end
0 commit comments