Skip to content

Commit 92c1cb9

Browse files
committed
fix: fix generate_W
1 parent 47c1418 commit 92c1cb9

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export structural_simplify, expand_connections, linearize, linearization_functio
303303
LinearizationProblem
304304
export solve
305305

306-
export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function
306+
export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function, generate_W
307307
export calculate_control_jacobian, generate_control_jacobian
308308
export calculate_tgrad, generate_tgrad
309309
export calculate_gradient, generate_gradient

src/structural_transformation/symbolics_tearing.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ function generate_derivative_variables!(
378378
# For variable x, make dummy derivative x_t if the
379379
# derivative is in the system
380380
for v in 1:length(var_to_diff)
381+
@show graph
381382
dv = var_to_diff[v]
382383
dv isa Int || continue
383384
solved = var_eq_matching[dv] isa Int
@@ -403,6 +404,10 @@ function generate_derivative_variables!(
403404
v_t = add_dd_variable!(structure, fullvars, x_t, dv)
404405
# Add `D(x) - x_t ~ 0` to the graph
405406
dummy_eq = add_dd_equation!(structure, neweqs, 0 ~ dx - x_t, dv, v_t)
407+
# Update graph to say, all the equations featuring D(x) also feature x_t
408+
for e in 𝑑neighbors(graph, dv)
409+
add_edge!(graph, e, v_t)
410+
end
406411

407412
# Update matching
408413
push!(var_eq_matching, unassigned)

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,20 @@ function generate_W(sys::AbstractODESystem, γ = 1., dvs = unknowns(sys),
143143
simplify = false, sparse = false, kwargs...)
144144
@variables ˍ₋gamma
145145
M = calculate_massmatrix(sys; simplify)
146+
sparse && (M = SparseArrays.sparse(M))
146147
J = calculate_jacobian(sys; simplify, sparse, dvs)
147148
W = ˍ₋gamma*M + J
149+
@show W
148150

149151
p = reorder_parameters(sys, ps)
152+
@show length(p)
150153
return build_function_wrapper(sys, W,
151154
dvs,
152155
p...,
153156
ˍ₋gamma,
154157
get_iv(sys);
158+
wrap_code = sparse ? assert_jac_length_header(sys) : (identity, identity),
159+
p_end = 1 + length(p),
155160
kwargs...)
156161
end
157162

test/jacobiansparsity.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true)
8686
@testset "W matrix sparsity" begin
8787
t = ModelingToolkit.t_nounits
8888
@parameters g
89-
@variables x(t) y(t) [state_priority = 10] λ(t)
89+
@variables x(t) y(t) λ(t)
9090
eqs = [D(D(x)) ~ λ * x
9191
D(D(y)) ~ λ * y - g
9292
x^2 + y^2 ~ 1]
@@ -105,4 +105,10 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true)
105105
p = prob.p
106106
t = 0.0
107107
@test_throws AssertionError jac!(similar(jac_prototype, Float64), u, p, t)
108+
109+
W, W! = generate_W(pend; expression = Val{false}, sparse = true)
110+
γ = .1
111+
M = sparse(calculate_massmatrix(pend))
112+
@test_throws AssertionError W!(similar(jac_prototype, Float64), u, p, γ, t)
113+
@test W!(similar(W_prototype, Float64), u, p, γ, t) == 0.1 * M + jac!(similar(W_prototype, Float64), u, p, t)
108114
end

0 commit comments

Comments
 (0)