Skip to content

Commit 038c30a

Browse files
Run Runic on initialization diagnostics (#4727)
Apply Runic formatting to the initialization Jacobian diagnostic files and related cache helpers so the repository format check passes again. Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
1 parent 17de025 commit 038c30a

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

lib/ModelingToolkitBase/src/systems/codegen.jl

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

11981198
function should_invalidate_mutable_cache_entry(::Type{NamespaceMap}, patch::NamedTuple)
11991199
return haskey(patch, :name) || haskey(patch, :observed) || haskey(patch, :unknowns) ||
1200-
haskey(patch, :ps) || haskey(patch, :systems)
1200+
haskey(patch, :ps) || haskey(patch, :systems)
12011201
end
12021202

12031203
function _build_namespace_map(sys)

lib/ModelingToolkitBase/src/systems/index_cache.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,11 @@ function should_invalidate_mutable_cache_entry(::Type{ReorderedDefaultParameters
597597
end
598598

599599
function _copy_reordered(v::ReorderedParametersT)
600-
return ReorderedParametersT(map(v) do inner
601-
inner isa Vector{SymbolicT} ? copy(inner) : map(copy, inner)
602-
end)
600+
return ReorderedParametersT(
601+
map(v) do inner
602+
inner isa Vector{SymbolicT} ? copy(inner) : map(copy, inner)
603+
end
604+
)
603605
end
604606

605607
function reorder_parameters(sys::AbstractSystem; kwargs...)

src/initialization.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ unknowns); a `redundancy` of `0` means full row rank (no redundant equations).
106106
rank deficient at a particular operating point, and vice versa.
107107
"""
108108
function analyze_initialization_jacobian(
109-
prob; rtol = 1e-8, atol = 0.0, threshold = 1e-3, verbose = true)
110-
empty_result = (; jacobian = nothing, singular_values = Float64[], rank = 0,
109+
prob; rtol = 1.0e-8, atol = 0.0, threshold = 1.0e-3, verbose = true
110+
)
111+
empty_result = (;
112+
jacobian = nothing, singular_values = Float64[], rank = 0,
111113
nullity = 0, redundancy = 0, underdetermined_unknowns = Pair[],
112-
redundant_equations = Pair[])
114+
redundant_equations = Pair[],
115+
)
113116
iprob = _initialization_problem(prob)
114117
if iprob === nothing
115118
verbose &&
@@ -148,24 +151,28 @@ function analyze_initialization_jacobian(
148151
# Participation = diagonal of the null-space projector (squared row norm over an
149152
# orthonormal null-space basis), invariant to the arbitrary basis within the null space.
150153
col_w = nullity == 0 ? zeros(ncols) :
151-
vec(sum(abs2, @view(fact.V[:, col_isnull]); dims = 2))
154+
vec(sum(abs2, @view(fact.V[:, col_isnull]); dims = 2))
152155
row_w = redundancy == 0 ? zeros(nrows) :
153-
vec(sum(abs2, @view(fact.U[:, row_isnull]); dims = 2))
156+
vec(sum(abs2, @view(fact.U[:, row_isnull]); dims = 2))
154157
syms = variable_symbols(iprob)
155158
eqs = _initialization_equations(iprob)
156159
underdetermined_unknowns = sort(
157160
[syms[i] => col_w[i] for i in 1:ncols if col_w[i] > threshold];
158-
by = last, rev = true)
161+
by = last, rev = true
162+
)
159163
redundant_equations = sort(
160164
[(eqs === nothing ? i : eqs[i]) => row_w[i] for i in 1:nrows if row_w[i] > threshold];
161-
by = last, rev = true)
165+
by = last, rev = true
166+
)
162167
if verbose
163168
println("Initialization Jacobian rank analysis")
164169
println(" residual Jacobian: $(nrows)×$(ncols), rank ≈ $rank, nullity ≈ $nullity, redundancy ≈ $redundancy")
165170
if !isempty(S)
166171
k = min(5, length(S))
167-
println(" smallest $k singular value(s): ",
168-
round.(S[(end - k + 1):end], sigdigits = 3))
172+
println(
173+
" smallest $k singular value(s): ",
174+
round.(S[(end - k + 1):end], sigdigits = 3)
175+
)
169176
end
170177
if nullity == 0
171178
println(" Full column rank at u0 — no underdetermined unknowns.")
@@ -184,8 +191,10 @@ function analyze_initialization_jacobian(
184191
end
185192
end
186193
end
187-
return (; jacobian = J, singular_values = S, rank, nullity, redundancy,
188-
underdetermined_unknowns, redundant_equations)
194+
return (;
195+
jacobian = J, singular_values = S, rank, nullity, redundancy,
196+
underdetermined_unknowns, redundant_equations,
197+
)
189198
end
190199

191200
# Symbolic equations of an initialization problem, in residual order, or `nothing` if not

test/initialization_jacobian_analysis.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
88
@variables x(t) y(t)
99
@named sys = System([D(x) ~ y, 0 ~ x^2 + y^2 - 1], t)
1010
sys = mtkcompile(sys)
11-
prob = ODEProblem(sys, [], (0.0, 1.0); guesses = [x => 0.6, y => 0.8],
12-
warn_initialize_determined = false)
11+
prob = ODEProblem(
12+
sys, [], (0.0, 1.0); guesses = [x => 0.6, y => 0.8],
13+
warn_initialize_determined = false
14+
)
1315
res = analyze_initialization_jacobian(prob; verbose = false)
1416
@test res.nullity 1
1517
@test !isempty(res.underdetermined_unknowns)
1618
@test res.jacobian !== nothing
17-
@test all(0 .≤ last.(res.underdetermined_unknowns) .≤ 1 + 1e-8)
19+
@test all(0 .≤ last.(res.underdetermined_unknowns) .≤ 1 + 1.0e-8)
1820
@test res.rank + res.nullity == size(res.jacobian, 2)
1921

2022
# Overdetermined initialization with a redundant equation: two consistent initial
2123
# conditions on a single unknown. The analysis must report a redundant equation.
2224
@variables z(t)
2325
@named sys2 = System([D(z) ~ -z], t; initialization_eqs = [log(exp(z)) ~ 1.0, z^2 ~ 1.0])
2426
sys2 = mtkcompile(sys2)
25-
prob2 = ODEProblem(sys2, [], (0.0, 1.0); guesses = [z => 0.9],
26-
warn_initialize_determined = false)
27+
prob2 = ODEProblem(
28+
sys2, [], (0.0, 1.0); guesses = [z => 0.9],
29+
warn_initialize_determined = false
30+
)
2731
res2 = analyze_initialization_jacobian(prob2; verbose = false)
2832
@test res2.redundancy 1
2933
@test !isempty(res2.redundant_equations)
30-
@test all(0 .≤ last.(res2.redundant_equations) .≤ 1 + 1e-8)
34+
@test all(0 .≤ last.(res2.redundant_equations) .≤ 1 + 1.0e-8)
3135

3236
# Determined initialization: an algebraic unknown uniquely fixed by a pinned state has a
3337
# full-rank initialization Jacobian — no underdetermined unknowns and no redundant

0 commit comments

Comments
 (0)