Skip to content

Commit c423abe

Browse files
committed
peel off Constant in mass matrix
1 parent f1cc897 commit c423abe

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

src/simplify.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function simplify_constants(expr)
4343
end
4444

4545
to_mtk(x) = x
46+
to_mtk(x::Number) = Constant(x)
4647
to_mtk(v::SymbolicUtils.Variable{T}) where {T} = Variable{T}(nameof(v))
4748
to_mtk(v::SymbolicUtils.Variable{FnType{X,Y}}) where {X,Y} = Variable{Y}(nameof(v))
4849
function to_mtk(expr::SymbolicUtils.Term)

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ function calculate_massmatrix(sys::AbstractODESystem, simplify=true)
114114
end
115115
end
116116
M = simplify ? simplify_constants.(M) : M
117+
# M should only contain concrete numbers
118+
M = map(x->x isa Constant ? x.value : x, M)
117119
M == I ? I : M
118120
end
119121

test/derivatives.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using Test
66
@variables x y z
77
@derivatives D'~t D2''~t Dx'~x
88

9+
test_equal(a, b) = @test isequal(simplify_constants(a), simplify_constants(b))
10+
911
@test @macroexpand(@derivatives D'~t D2''~t) == @macroexpand(@derivatives (D'~t), (D2''~t))
1012

1113
@test isequal(expand_derivatives(D(t)), 1)
@@ -38,15 +40,15 @@ eqs = [0 ~ σ*(y-x),
3840
0 ~ x*y - β*z]
3941
sys = NonlinearSystem(eqs, [x,y,z], [σ,ρ,β])
4042
jac = calculate_jacobian(sys)
41-
@test isequal(jac[1,1], -1σ)
42-
@test isequal(jac[1,2], σ)
43-
@test isequal(jac[1,3], 0)
44-
@test isequal(jac[2,1], -1z + ρ) # FIXME
45-
@test isequal(jac[2,2], -1)
46-
@test isequal(jac[2,3], -1x)
47-
@test isequal(jac[3,1], y)
48-
@test isequal(jac[3,2], x)
49-
@test isequal(jac[3,3], -1*β)
43+
test_equal(jac[1,1], -1σ)
44+
test_equal(jac[1,2], σ)
45+
test_equal(jac[1,3], 0)
46+
test_equal(jac[2,1], -1z + ρ) # FIXME
47+
test_equal(jac[2,2], -1)
48+
test_equal(jac[2,3], -1x)
49+
test_equal(jac[3,1], y)
50+
test_equal(jac[3,2], x)
51+
test_equal(jac[3,3], -1β)
5052

5153
# Variable dependence checking in differentiation
5254
@variables a(t) b(a)

test/mass_matrix.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ eqs = [D(y[1]) ~ -k[1]*y[1] + k[3]*y[2]*y[3],
1010

1111
sys = ODESystem(eqs,t,y,k)
1212
M = calculate_massmatrix(sys)
13-
M == [1 0 0
14-
0 1 0
15-
0 0 0]
13+
@test M == [1 0 0
14+
0 1 0
15+
0 0 0]
1616

1717
f = ODEFunction(sys)
1818
prob_mm = ODEProblem(f,[1.0,0.0,0.0],(0.0,1e5),(0.04,3e7,1e4))

test/simplify.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ d2 = Differential(x)(d1)
2828
d3 = Differential(x)(d2)
2929

3030
@test simplified_expr(expand_derivatives(d3)) == :(0)
31-
#@test simplify_constants(x^0) == 1
31+
@test simplified_expr(simplify_constants(x^0)) == :(1)

0 commit comments

Comments
 (0)