Skip to content

Commit

Permalink
Handle overdetermined systems gracefully when fully_determined = false
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa authored and ChrisRackauckas committed Feb 24, 2024
1 parent 21818fa commit b9e0a70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bipartite_graph.jl
Expand Up @@ -423,7 +423,7 @@ return `false` may not be matched.
"""
function maximal_matching(g::BipartiteGraph, srcfilter = vsrc -> true,
dstfilter = vdst -> true, ::Type{U} = Unassigned) where {U}
matching = Matching{U}(ndsts(g))
matching = Matching{U}(max(nsrcs(g), ndsts(g)))

Check warning on line 426 in src/bipartite_graph.jl

View check run for this annotation

Codecov / codecov/patch

src/bipartite_graph.jl#L426

Added line #L426 was not covered by tests
foreach(Iterators.filter(srcfilter, 𝑠vertices(g))) do vsrc
construct_augmenting_path!(matching, g, vsrc, dstfilter)
end
Expand Down
18 changes: 10 additions & 8 deletions src/systems/nonlinear/initializesystem.jl
Expand Up @@ -14,7 +14,7 @@ function initializesystem(sys::ODESystem; name = nameof(sys), guesses = Dict(),
u0 = Vector{Pair}(undef, 0)
defs = ModelingToolkit.defaults(sys)

Check warning on line 15 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L13-L15

Added lines #L13 - L15 were not covered by tests

full_states = [sts;getfield.((observed(sys)),:lhs)]
full_states = [sts; getfield.((observed(sys)), :lhs)]

Check warning on line 17 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L17

Added line #L17 was not covered by tests

# Refactor to ODESystem construction
# should be ModelingToolkit.guesses(sys)
Expand All @@ -28,26 +28,28 @@ function initializesystem(sys::ODESystem; name = nameof(sys), guesses = Dict(),
def = defs[st]

Check warning on line 28 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L26-L28

Added lines #L26 - L28 were not covered by tests

if def isa Equation
st keys(guesses) && error("Invalid setup: unknown $(st) has an initial condition equation with no guess.")
push!(eqs_ics,def)
push!(u0,st => guesses[st])
st keys(guesses) &&

Check warning on line 31 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L30-L31

Added lines #L30 - L31 were not covered by tests
error("Invalid setup: unknown $(st) has an initial condition equation with no guess.")
push!(eqs_ics, def)
push!(u0, st => guesses[st])

Check warning on line 34 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L33-L34

Added lines #L33 - L34 were not covered by tests
else
push!(eqs_ics,st ~ def)
push!(eqs_ics, st ~ def)
push!(u0, st => def)

Check warning on line 37 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L36-L37

Added lines #L36 - L37 were not covered by tests
end
elseif st keys(guesses)
push!(u0,st => guesses[st])
push!(u0, st => guesses[st])

Check warning on line 40 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L39-L40

Added lines #L39 - L40 were not covered by tests
else
error("Invalid setup: unknown $(st) has no default value or initial guess")

Check warning on line 42 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L42

Added line #L42 was not covered by tests
end
end

pars = parameters(sys)
nleqs = [eqs_ics; observed(sys)]

Check warning on line 47 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L47

Added line #L47 was not covered by tests

sys_nl = NonlinearSystem([eqs_ics; observed(sys)],
sys_nl = NonlinearSystem(nleqs,

Check warning on line 49 in src/systems/nonlinear/initializesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L49

Added line #L49 was not covered by tests
full_states,
pars;
defaults = merge(ModelingToolkit.defaults(sys),todict(u0)),
defaults = merge(ModelingToolkit.defaults(sys), todict(u0)),
name,
kwargs...)

Expand Down

0 comments on commit b9e0a70

Please sign in to comment.