Skip to content

Commit cbceec4

Browse files
committed
Specify new independent variable as a dependent variable in the old system
1 parent af3ae69 commit cbceec4

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/systems/diffeqs/basic_transformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252

5353
function change_independent_variable(sys::AbstractODESystem, iv, eq = nothing; verbose = false, kwargs...)
5454
iv1 = get_iv(sys) # e.g. t
55-
iv2name = nameof(iv) # TODO: handle namespacing?
55+
iv2name = nameof(operation(unwrap(iv))) # TODO: handle namespacing?
5656
iv2, = @independent_variables $iv2name # e.g. a
5757
iv2func, = @variables $iv2name(iv1) # e.g. a(t)
5858
D1 = Differential(iv1)

test/basic_transformations.jl

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,29 @@ D = Differential(t)
2626
end
2727

2828
@testset "Change independent variable" begin
29-
@independent_variables t
3029
@variables x(t) y(t) z(t) s(t)
3130
eqs = [
3231
D(x) ~ y
3332
D(D(y)) ~ 2 * x * D(y)
3433
z ~ x + D(y)
3534
D(s) ~ 1 / (2*s)
3635
]
37-
@named sys1 = ODESystem(eqs, t)
38-
sys1 = complete(sys1)
36+
M1 = ODESystem(eqs, t; name = :M) |> complete
37+
M2 = ModelingToolkit.change_independent_variable(M1, M1.s)
3938

40-
@independent_variables s
41-
sys2 = ModelingToolkit.change_independent_variable(sys1, s)
42-
43-
sys1 = structural_simplify(sys1; allow_symbolic = true)
44-
sys2 = structural_simplify(sys2; allow_symbolic = true)
45-
prob1 = ODEProblem(sys1, [sys1.x => 1.0, sys1.y => 1.0, Differential(t)(sys1.y) => 0.0, sys1.s => 1.0], (1.0, 4.0))
46-
prob2 = ODEProblem(sys2, [sys2.x => 1.0, sys2.y => 1.0, Differential(s)(sys2.y) => 0.0], (1.0, 2.0))
39+
M1 = structural_simplify(M1; allow_symbolic = true)
40+
M2 = structural_simplify(M2; allow_symbolic = true)
41+
prob1 = ODEProblem(M1, [M1.x => 1.0, M1.y => 1.0, Differential(M1.t)(M1.y) => 0.0, M1.s => 1.0], (1.0, 4.0))
42+
prob2 = ODEProblem(M2, [M2.x => 1.0, M2.y => 1.0, Differential(M2.s)(M2.y) => 0.0], (1.0, 2.0))
4743
sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10)
4844
sol2 = solve(prob2, Tsit5(); reltol = 1e-10, abstol = 1e-10)
4945
ts = range(0.0, 1.0, length = 50)
5046
ss = .√(ts)
51-
@test all(isapprox.(sol1(ts, idxs=sys1.x), sol2(ss, idxs=sys2.x); atol = 1e-7)) &&
52-
all(isapprox.(sol1(ts, idxs=sys1.y), sol2(ss, idxs=sys2.y); atol = 1e-7))
47+
@test all(isapprox.(sol1(ts, idxs=M1.x), sol2(ss, idxs=M2.x); atol = 1e-7)) &&
48+
all(isapprox.(sol1(ts, idxs=M1.y), sol2(ss, idxs=M2.y); atol = 1e-7))
5349
end
5450

5551
@testset "Change independent variable (Friedmann equation)" begin
56-
@independent_variables t
5752
D = Differential(t)
5853
@variables a(t) ρr(t) ρm(t) ρΛ(t) ρ(t) P(t) ϕ(t)
5954
@parameters Ωr0 Ωm0 ΩΛ0
@@ -68,8 +63,7 @@ end
6863
@named M1 = ODESystem(eqs, t)
6964
M1 = complete(M1)
7065

71-
@independent_variables a
72-
M2 = ModelingToolkit.change_independent_variable(M1, a)
66+
M2 = ModelingToolkit.change_independent_variable(M1, M1.a)
7367
M2 = structural_simplify(M2; allow_symbolic = true)
7468
@test length(unknowns(M2)) == 2
7569
end

0 commit comments

Comments
 (0)