Skip to content

Commit 8041fa6

Browse files
authored
Merge pull request #112 from JuliaDiffEq/myb/fix
Fix ode_order_lowering pass and add stricter tests
2 parents 6450e3b + e9016c0 commit 8041fa6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/systems/diffeqs/first_order_transform.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ function lower_varname(var::Variable, idv, order)
88
end
99

1010
function ode_order_lowering(sys::DiffEqSystem)
11-
eqs_lowered = ode_order_lowering(sys.eqs, sys.iv)
12-
DiffEqSystem(eqs_lowered, sys.iv, sys.dvs, sys.ps)
11+
eqs_lowered, vars_lowered = ode_order_lowering(sys.eqs, sys.iv)
12+
DiffEqSystem(eqs_lowered, sys.iv, vars_lowered, sys.ps)
1313
end
1414
function ode_order_lowering(eqs, iv)
1515
var_order = Dict{Variable,Int}()
1616
vars = Variable[]
17+
new_vars = Variable[]
1718
new_eqs = similar(eqs, DiffEq)
1819

1920
for (i, eq) enumerate(eqs)
@@ -23,6 +24,7 @@ function ode_order_lowering(eqs, iv)
2324
any(isequal(var), vars) || push!(vars, var)
2425
end
2526
var′ = lower_varname(eq.x, eq.t, eq.n - 1)
27+
push!(new_vars, var′)
2628
rhs′ = rename(eq.rhs)
2729
new_eqs[i] = DiffEq(var′, iv, 1, rhs′)
2830
end
@@ -31,13 +33,14 @@ function ode_order_lowering(eqs, iv)
3133
order = var_order[var]
3234
for o in (order-1):-1:1
3335
lvar = lower_varname(var, iv, o-1)
36+
push!(new_vars, lvar)
3437
rhs = lower_varname(var, iv, o)
3538
eq = DiffEq(lvar, iv, 1, rhs)
3639
push!(new_eqs, eq)
3740
end
3841
end
3942

40-
return new_eqs
43+
return new_eqs, new_vars
4144
end
4245

4346
function rename(O::Expression)

test/system_construction.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ lowered_eqs = [D(u_tt) ~ 2u_tt + u_t + x_t + 1
5959
D(u) ~ u_t
6060
D(x) ~ x_t]
6161
@test de1.eqs == convert.(ModelingToolkit.DiffEq, lowered_eqs)
62+
@test isequal(de1.dvs, [u_tt, x_t, u_t, u, x])
63+
@test isequal(de1.iv, t)
64+
du = zeros(5)
65+
ODEFunction(de1)(du, ones(5), nothing, 0.1)
66+
@test du == [5.0, 3.0, 1.0, 1.0, 1.0]
6267

6368
# Internal calculations
6469
a = y - x

0 commit comments

Comments
 (0)