Skip to content

Commit 82198b7

Browse files
refactor: move HomtopyContinuation-related code to its own file
1 parent ab5747f commit 82198b7

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
@@ -150,6 +150,7 @@ include("systems/callbacks.jl")
150150
include("systems/problem_utils.jl")
151151

152152
include("systems/nonlinear/nonlinearsystem.jl")
153+
include("systems/nonlinear/homotopy_continuation.jl")
153154
include("systems/diffeqs/odesystem.jl")
154155
include("systems/diffeqs/sdesystem.jl")
155156
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
@@ -866,72 +866,3 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem)
866866
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&
867867
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
868868
end
869-
870-
"""
871-
$(TYPEDEF)
872-
873-
A type of Nonlinear problem which specializes on polynomial systems and uses
874-
HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to
875-
create and solve.
876-
"""
877-
struct HomotopyContinuationProblem{uType, H, D, O, SS, U} <:
878-
SciMLBase.AbstractNonlinearProblem{uType, true}
879-
"""
880-
The initial values of states in the system. If there are multiple real roots of
881-
the system, the one closest to this point is returned.
882-
"""
883-
u0::uType
884-
"""
885-
A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the
886-
parameter object.
887-
"""
888-
homotopy_continuation_system::H
889-
"""
890-
A function with signature `(u, p) -> resid`. In case of rational functions, this
891-
is used to rule out roots of the system which would cause the denominator to be
892-
zero.
893-
"""
894-
denominator::D
895-
"""
896-
The `NonlinearSystem` used to create this problem. Used for symbolic indexing.
897-
"""
898-
sys::NonlinearSystem
899-
"""
900-
A function which generates and returns observed expressions for the given system.
901-
"""
902-
obsfn::O
903-
"""
904-
The HomotopyContinuation.jl solver and start system, obtained through
905-
`HomotopyContinuation.solver_startsystems`.
906-
"""
907-
solver_and_starts::SS
908-
"""
909-
A function which takes a solution of the transformed system, and returns a vector
910-
of solutions for the original system. This is utilized when converting systems
911-
to polynomials.
912-
"""
913-
unpack_solution::U
914-
end
915-
916-
function HomotopyContinuationProblem(::AbstractSystem, _u0, _p; kwargs...)
917-
error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.")
918-
end
919-
920-
SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys
921-
SymbolicIndexingInterface.state_values(p::HomotopyContinuationProblem) = p.u0
922-
function SymbolicIndexingInterface.set_state!(p::HomotopyContinuationProblem, args...)
923-
set_state!(p.u0, args...)
924-
end
925-
function SymbolicIndexingInterface.parameter_values(p::HomotopyContinuationProblem)
926-
parameter_values(p.homotopy_continuation_system)
927-
end
928-
function SymbolicIndexingInterface.set_parameter!(p::HomotopyContinuationProblem, args...)
929-
set_parameter!(parameter_values(p), args...)
930-
end
931-
function SymbolicIndexingInterface.observed(p::HomotopyContinuationProblem, sym)
932-
if p.obsfn !== nothing
933-
return p.obsfn(sym)
934-
else
935-
return SymbolicIndexingInterface.observed(p.sys, sym)
936-
end
937-
end

0 commit comments

Comments
 (0)