@@ -25,6 +25,15 @@ D = Differential(t)
2525 @test sol[end , end ] ≈ 1.0742818931017244
2626end
2727
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+
2837@testset " Change independent variable" begin
2938 @variables x (t) y (t) z (t) s (t)
3039 eqs = [
7584@testset " Change independent variable (simple)" begin
7685 @variables x (t)
7786 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 )
7988 @test (@variables x x_t (x) x_tt (x); Set (equations (Mx)) == Set ([x_t ~ 2 x, x_tt ~ 4 x]))
8089end
8190
8291@testset " Change independent variable (free fall)" begin
8392 @variables x (t) y (t)
8493 @parameters g v # gravitational acceleration and constant horizontal velocity
8594 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
8796 Mx = structural_simplify (Mx; allow_symbolic = true )
8897 Dx = Differential (Mx. x)
8998 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
9099 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)
92101end
0 commit comments