From 89df2416156eafe8f2ba180363a1cbf4c28429d4 Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Sat, 29 Jul 2023 12:34:45 -0400 Subject: [PATCH 1/3] Make introspecting `dummy_derivative` easier --- .../bipartite_tearing/modia_tearing.jl | 4 ++-- .../partial_state_selection.jl | 15 ++++++++++----- .../symbolics_tearing.jl | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/structural_transformation/bipartite_tearing/modia_tearing.jl b/src/structural_transformation/bipartite_tearing/modia_tearing.jl index a15e7c3e3d..e8304752e8 100644 --- a/src/structural_transformation/bipartite_tearing/modia_tearing.jl +++ b/src/structural_transformation/bipartite_tearing/modia_tearing.jl @@ -82,6 +82,7 @@ function tear_graph_modia(structure::SystemStructure, isder::F = nothing, var_eq_matching = complete(var_eq_matching, max(length(var_eq_matching), maximum(x -> x isa Int ? x : 0, var_eq_matching))) + full_var_eq_matching = copy(var_eq_matching) var_sccs::Vector{Union{Vector{Int}, Int}} = find_var_sccs(graph, var_eq_matching) vargraph = DiCMOBiGraph{true}(graph) ict = IncrementalCycleTracker(vargraph; dir = :in) @@ -110,6 +111,5 @@ function tear_graph_modia(structure::SystemStructure, isder::F = nothing, empty!(ieqs) empty!(filtered_vars) end - - return var_eq_matching + return var_eq_matching, full_var_eq_matching end diff --git a/src/structural_transformation/partial_state_selection.jl b/src/structural_transformation/partial_state_selection.jl index 4669119b63..9338cd585b 100644 --- a/src/structural_transformation/partial_state_selection.jl +++ b/src/structural_transformation/partial_state_selection.jl @@ -168,15 +168,15 @@ function partial_state_selection_graph!(structure::SystemStructure, var_eq_match end function dummy_derivative_graph!(state::TransformationState, jac = nothing; - state_priority = nothing, kwargs...) + state_priority = nothing, log = Val(false), kwargs...) state.structure.solvable_graph === nothing && find_solvables!(state; kwargs...) complete!(state.structure) var_eq_matching = complete(pantelides!(state)) - dummy_derivative_graph!(state.structure, var_eq_matching, jac, state_priority) + dummy_derivative_graph!(state.structure, var_eq_matching, jac, state_priority, log) end function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac, - state_priority) + state_priority, ::Val{log} = Val(false)) where {log} @unpack eq_to_diff, var_to_diff, graph = structure diff_to_eq = invview(eq_to_diff) diff_to_var = invview(var_to_diff) @@ -338,7 +338,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja end end - var_eq_matching = tear_graph_modia(structure, isdiffed, + var_eq_matching, full_var_eq_matching = tear_graph_modia(structure, isdiffed, Union{Unassigned, SelectedState}; varfilter = can_eliminate) for v in eachindex(var_eq_matching) @@ -348,5 +348,10 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja var_eq_matching[v] = SelectedState() end - return var_eq_matching + if log + candidates = can_eliminate.(1:ndsts(graph)) + return var_eq_matching, full_var_eq_matching, candidates + else + return var_eq_matching + end end diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 7546ec2cda..63b9e3ab14 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -564,7 +564,7 @@ function tearing(state::TearingState; kwargs...) @unpack graph = state.structure algvars = BitSet(findall(v -> isalgvar(state.structure, v), 1:ndsts(graph))) aeqs = algeqs(state.structure) - var_eq_matching′ = tear_graph_modia(state.structure; + var_eq_matching′, = tear_graph_modia(state.structure; varfilter = var -> var in algvars, eqfilter = eq -> eq in aeqs) var_eq_matching = Matching{Union{Unassigned, SelectedState}}(var_eq_matching′) From 48c8e91f25fb3a4ae813360e5532c17366ad9d4a Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Mon, 31 Jul 2023 18:26:20 -0400 Subject: [PATCH 2/3] Update tests --- test/structural_transformation/tearing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index e816f6612f..a30238a645 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -169,7 +169,7 @@ prob.f(du, u, pr, tt) # test the initial guess is respected @named sys = ODESystem(eqs, t, defaults = Dict(z => Inf)) infprob = ODAEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) -@test_throws DomainError infprob.f(du, u, pr, tt) +@test_throws Any infprob.f(du, u, pr, tt) sol1 = solve(prob, Tsit5()) sol2 = solve(ODEProblem{false}((u, p, t) -> [-asin(u[1] - pr * t)], From fd581569f365ce310caa481203a578d4f6ae40ab Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Mon, 31 Jul 2023 18:27:51 -0400 Subject: [PATCH 3/3] Relax tolerance --- test/ccompile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ccompile.jl b/test/ccompile.jl index 43b80a5858..82850fe55c 100644 --- a/test/ccompile.jl +++ b/test/ccompile.jl @@ -14,4 +14,4 @@ p = rand(1) _t = rand() f(du, u, p, _t) f2(du2, u, p, _t) -@test du == du2 +@test du ≈ du2