Skip to content

Commit a079451

Browse files
refactor: move HomtopyContinuation-related code to its own file
1 parent 8e48f65 commit a079451

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ include("systems/callbacks.jl")
149149
include("systems/problem_utils.jl")
150150

151151
include("systems/nonlinear/nonlinearsystem.jl")
152+
include("systems/nonlinear/homotopy_continuation.jl")
152153
include("systems/diffeqs/odesystem.jl")
153154
include("systems/diffeqs/sdesystem.jl")
154155
include("systems/diffeqs/abstractodesystem.jl")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
$(TYPEDEF)
3+
4+
A type of Nonlinear problem which specializes on polynomial systems and uses
5+
HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to
6+
create and solve.
7+
"""
8+
struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <:
9+
SciMLBase.AbstractNonlinearProblem{uType, true}
10+
"""
11+
The initial values of states in the system. If there are multiple real roots of
12+
the system, the one closest to this point is returned.
13+
"""
14+
u0::uType
15+
"""
16+
A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the
17+
parameter object.
18+
"""
19+
homotopy_continuation_system::H
20+
"""
21+
A function with signature `(u, p) -> resid`. In case of rational functions, this
22+
is used to rule out roots of the system which would cause the denominator to be
23+
zero.
24+
"""
25+
denominator::D
26+
"""
27+
The `NonlinearSystem` used to create this problem. Used for symbolic indexing.
28+
"""
29+
sys::NonlinearSystem
30+
"""
31+
A function which generates and returns observed expressions for the given system.
32+
"""
33+
obsfn::O
34+
"""
35+
The HomotopyContinuation.jl solver and start system, obtained through
36+
`HomotopyContinuation.solver_startsystems`.
37+
"""
38+
solver_and_starts::SS
39+
"""
40+
A function which takes a solution of the transformed system, and returns a vector
41+
of solutions for the original system. This is utilized when converting systems
42+
to polynomials.
43+
"""
44+
unpack_solution::U
45+
end
46+
47+
function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...)
48+
error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.")
49+
end
50+
51+
SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys
52+
SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0
53+
function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...)
54+
set_state!(p.u0, args...)
55+
end
56+
function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem)
57+
parameter_values(p.homotopy_continuation_system)
58+
end
59+
function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...)
60+
set_parameter!(parameter_values(p), args...)
61+
end
62+
function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym)
63+
if p.obsfn !== nothing
64+
return p.obsfn(sym)
65+
else
66+
return SymbolicIndexingInterface.observed(p.sys, sym)
67+
end
68+
end

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -682,72 +682,3 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem)
682682
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&
683683
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
684684
end
685-
686-
"""
687-
$(TYPEDEF)
688-
689-
A type of Nonlinear problem which specializes on polynomial systems and uses
690-
HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to
691-
create and solve.
692-
"""
693-
struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <:
694-
SciMLBase.AbstractNonlinearProblem{uType, true}
695-
"""
696-
The initial values of states in the system. If there are multiple real roots of
697-
the system, the one closest to this point is returned.
698-
"""
699-
u0::uType
700-
"""
701-
A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the
702-
parameter object.
703-
"""
704-
homotopy_continuation_system::H
705-
"""
706-
A function with signature `(u, p) -> resid`. In case of rational functions, this
707-
is used to rule out roots of the system which would cause the denominator to be
708-
zero.
709-
"""
710-
denominator::D
711-
"""
712-
The `NonlinearSystem` used to create this problem. Used for symbolic indexing.
713-
"""
714-
sys::NonlinearSystem
715-
"""
716-
A function which generates and returns observed expressions for the given system.
717-
"""
718-
obsfn::O
719-
"""
720-
The HomotopyContinuation.jl solver and start system, obtained through
721-
`HomotopyContinuation.solver_startsystems`.
722-
"""
723-
solver_and_starts::SS
724-
"""
725-
A function which takes a solution of the transformed system, and returns a vector
726-
of solutions for the original system. This is utilized when converting systems
727-
to polynomials.
728-
"""
729-
unpack_solution::U
730-
end
731-
732-
function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...)
733-
error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.")
734-
end
735-
736-
SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys
737-
SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0
738-
function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...)
739-
set_state!(p.u0, args...)
740-
end
741-
function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem)
742-
parameter_values(p.homotopy_continuation_system)
743-
end
744-
function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...)
745-
set_parameter!(parameter_values(p), args...)
746-
end
747-
function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym)
748-
if p.obsfn !== nothing
749-
return p.obsfn(sym)
750-
else
751-
return SymbolicIndexingInterface.observed(p.sys, sym)
752-
end
753-
end

0 commit comments

Comments
 (0)