Skip to content

Commit d5e9430

Browse files
docs: add documentation for HomotopyContinuation interface
1 parent a1d0756 commit d5e9430

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

ext/MTKHomotopyContinuationExt.jl

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ using ModelingToolkit
44
using ModelingToolkit.SciMLBase
55
using ModelingToolkit.Symbolics: unwrap
66
using ModelingToolkit.SymbolicIndexingInterface
7+
using ModelingToolkit.DocStringExtensions
78
using HomotopyContinuation
89
using ModelingToolkit: iscomplete, parameters, has_index_cache, get_index_cache, get_u0,
910
get_u0_p, check_eqs_u0, CommonSolve
1011

1112
const MTK = ModelingToolkit
1213

1314
function contains_variable(x, wrt)
14-
any(isequal(x), wrt) && return true
15-
iscall(x) || return false
16-
return any(y -> contains_variable(y, wrt), arguments(x))
15+
any(y -> occursin(y, x), wrt)
1716
end
1817

18+
"""
19+
$(TYPEDSIGNATURES)
20+
21+
Check if `x` is polynomial with respect to the variables in `wrt`.
22+
"""
1923
function is_polynomial(x, wrt)
2024
x = unwrap(x)
2125
symbolic_type(x) == NotSymbolic() && return true
@@ -33,6 +37,11 @@ function is_polynomial(x, wrt)
3337
return false
3438
end
3539

40+
"""
41+
$(TYPEDSIGNATURES)
42+
43+
Convert `expr` from a symbolics expression to one that uses `HomotopyContinuation.ModelKit`.
44+
"""
3645
function symbolics_to_hc(expr)
3746
if iscall(expr)
3847
if operation(expr) == getindex
@@ -48,11 +57,32 @@ function symbolics_to_hc(expr)
4857
end
4958
end
5059

60+
"""
61+
$(TYPEDEF)
62+
63+
A subtype of `HomotopyContinuation.AbstractSystem` used to solve `HomotopyContinuationProblem`s.
64+
"""
5165
struct MTKHomotopySystem{F, P, J, V} <: HomotopyContinuation.AbstractSystem
66+
"""
67+
The generated function for the residual of the polynomial system. In-place.
68+
"""
5269
f::F
70+
"""
71+
The parameter object.
72+
"""
5373
p::P
74+
"""
75+
The generated function for the jacobian of the polynomial system. In-place.
76+
"""
5477
jac::J
78+
"""
79+
The `HomotopyContinuation.ModelKit.Variable` representation of the unknowns of
80+
the system.
81+
"""
5582
vars::V
83+
"""
84+
The number of polynomials in the system. Must also be equal to `length(vars)`.
85+
"""
5686
nexprs::Int
5787
end
5888

@@ -112,7 +142,14 @@ function MTK.HomotopyContinuationProblem(
112142
return MTK.HomotopyContinuationProblem(u0, mtkhsys, sys, obsfn)
113143
end
114144

115-
function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem; kwargs...)
145+
"""
146+
$(TYPEDSIGNATURES)
147+
148+
Solve a `HomotopyContinuationProblem`. Ignores the algorithm passed to it, and always
149+
uses `HomotopyContinuation.jl`. All keyword arguments are forwarded to
150+
`HomotopyContinuation.solve`.
151+
"""
152+
function CommonSolve.solve(prob::MTK.HomotopyContinuationProblem, alg = nothing; kwargs...)
116153
sol = HomotopyContinuation.solve(prob.homotopy_continuation_system; kwargs...)
117154
realsols = HomotopyContinuation.results(sol; only_real = true)
118155
if isempty(realsols)

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,36 @@ function Base.:(==)(sys1::NonlinearSystem, sys2::NonlinearSystem)
566566
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
567567
end
568568

569+
"""
570+
$(TYPEDEF)
571+
572+
A type of Nonlinear problem which specializes on polynomial systems and uses
573+
HomotopyContinuation.jl to solve the system. Requires importing HomotopyContinuation.jl to
574+
create and solve.
575+
"""
569576
struct HomotopyContinuationProblem{uType, H, O} <: SciMLBase.AbstractNonlinearProblem{uType, true}
577+
"""
578+
The initial values of states in the system. If there are multiple real roots of
579+
the system, the one closest to this point is returned.
580+
"""
570581
u0::uType
582+
"""
583+
A subtype of `HomotopyContinuation.AbstractSystem` to solve. Also contains the
584+
parameter object.
585+
"""
571586
homotopy_continuation_system::H
587+
"""
588+
The `NonlinearSystem` used to create this problem. Used for symbolic indexing.
589+
"""
572590
sys::NonlinearSystem
591+
"""
592+
A function which generates and returns observed expressions for the given system.
593+
"""
573594
obsfn::O
574595
end
575596

576-
function HomotopyContinuationProblem(args...; kwargs...)
577-
error("Requires HomotopyContinuationExt")
597+
function HomotopyContinuationProblem(::NonlinearSystem, _u0, _p; kwargs...)
598+
error("HomotopyContinuation.jl is required to create and solve `HomotopyContinuationProblem`s. Please run `Pkg.add(\"HomotopyContinuation\")` to continue.")
578599
end
579600

580601
SymbolicIndexingInterface.symbolic_container(p::HomotopyContinuationProblem) = p.sys

0 commit comments

Comments
 (0)