Skip to content

Commit b8c9839

Browse files
committed
Improve change of independent variable tests
1 parent 75fe0f0 commit b8c9839

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

test/basic_transformations.jl

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,43 @@ end
5050

5151
@testset "Change independent variable (Friedmann equation)" begin
5252
D = Differential(t)
53-
@variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t)
53+
@variables a(t) (t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t)
5454
@parameters Ωr0 Ωm0 ΩΛ0
5555
eqs = [
5656
ρr ~ 3/(8*Num(π)) * Ωr0 / a^4
5757
ρm ~ 3/(8*Num(π)) * Ωm0 / a^3
5858
ρΛ ~ 3/(8*Num(π)) * ΩΛ0
5959
ρ ~ ρr + ρm + ρΛ
60-
D(a) ~ (8*Num(π)/3*ρ*a^4)
60+
~ (8*Num(π)/3*ρ*a^4)
61+
D(a) ~
6162
D(D(ϕ)) ~ -3*D(a)/a*D(ϕ)
6263
]
63-
@named M1 = ODESystem(eqs, t)
64-
M1 = complete(M1)
64+
M1 = ODESystem(eqs, t; name = :M) |> complete
6565

66-
M2 = ModelingToolkit.change_independent_variable(M1, M1.a)
66+
# Apply in two steps, where derivatives are defined at each step: first t -> a, then a -> b
67+
M2 = ModelingToolkit.change_independent_variable(M1, M1.a) |> complete #, D(b) ~ D(a)/a; verbose = true)
68+
@variables b(M2.a)
69+
M3 = ModelingToolkit.change_independent_variable(M2, b, Differential(M2.a)(b) ~ exp(-b))
6770
M2 = structural_simplify(M2; allow_symbolic = true)
71+
M3 = structural_simplify(M3; allow_symbolic = true)
6872
@test length(unknowns(M2)) == 2
6973
end
74+
75+
@testset "Change independent variable (simple)" begin
76+
@variables x(t)
77+
Mt = ODESystem([D(x) ~ 2*x], t; name = :M) |> complete
78+
Mx = ModelingToolkit.change_independent_variable(Mt, Mt.x)
79+
@test (@variables x x_t(x) x_tt(x); Set(equations(Mx)) == Set([x_t ~ 2x, x_tt ~ 4x]))
80+
end
81+
82+
@testset "Change independent variable (free fall)" begin
83+
@variables x(t) y(t)
84+
@parameters g v # gravitational acceleration and constant horizontal velocity
85+
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
87+
Mx = structural_simplify(Mx; allow_symbolic = true)
88+
Dx = Differential(Mx.x)
89+
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+
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
92+
end

0 commit comments

Comments
 (0)