Skip to content

Commit 6667457

Browse files
test: add tests for HomotopyContinuation.jl interface
1 parent 05e89ab commit 6667457

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

test/extensions/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
33
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
44
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
55
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
6+
HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327"
67
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
78
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
89
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
910
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
1011
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
1112
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
13+
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1214
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using ModelingToolkit, NonlinearSolve, SymbolicIndexingInterface
2+
using LinearAlgebra
3+
using Test
4+
import HomotopyContinuation
5+
6+
@testset "No parameters" begin
7+
@variables x y z
8+
eqs = [0 ~ x^2 + y^2 + 2x * y
9+
0 ~ x^2 + 4x + 4
10+
0 ~ y * z + 4x^2]
11+
@mtkbuild sys = NonlinearSystem(eqs)
12+
prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], [])
13+
@test prob[x] == prob[y] == prob[z] == 1.0
14+
@test prob[x + y] == 2.0
15+
sol = solve(prob; threading = false)
16+
@test SciMLBase.successful_retcode(sol)
17+
@test norm(sol.resid)0.0 atol=1e-10
18+
end
19+
20+
struct Wrapper
21+
x::Matrix{Float64}
22+
end
23+
24+
@testset "Parameters" begin
25+
wrapper(w::Wrapper) = det(w.x)
26+
@register_symbolic wrapper(w::Wrapper)
27+
28+
@variables x y z
29+
@parameters p q::Int r::Wrapper
30+
31+
eqs = [0 ~ x^2 + y^2 + p * x * y
32+
0 ~ x^2 + 4x + q
33+
0 ~ y * z + 4x^2 + wrapper(r)]
34+
35+
@mtkbuild sys = NonlinearSystem(eqs)
36+
prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0],
37+
[p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])])
38+
@test prob.ps[p] == 2.0
39+
@test prob.ps[q] == 4
40+
@test prob.ps[r].x == [1.0 1.0; 0.0 0.0]
41+
@test prob.ps[p * q] == 8.0
42+
sol = solve(prob; threading = false)
43+
@test SciMLBase.successful_retcode(sol)
44+
@test norm(sol.resid)0.0 atol=1e-10
45+
end
46+
47+
@testset "Array variables" begin
48+
@variables x[1:3]
49+
@parameters p[1:3]
50+
_x = collect(x)
51+
eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p))
52+
@mtkbuild sys = NonlinearSystem(eqs)
53+
prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3])
54+
@test prob[x] == ones(3)
55+
@test prob[p + x] == [2, 3, 4]
56+
prob[x] = 2ones(3)
57+
@test prob[x] == 2ones(3)
58+
prob.ps[p] = [2, 3, 4]
59+
@test prob.ps[p] == [2, 3, 4]
60+
sol = @test_nowarn solve(prob; threading = false)
61+
@test sol.retcode == ReturnCode.ConvergenceFailure
62+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ end
110110
if GROUP == "All" || GROUP == "Extensions"
111111
activate_extensions_env()
112112
@safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl")
113+
@safetestset "HomotopyContinuation Extension Test" include("extensions/homotopy_continuation.jl")
113114
@safetestset "Auto Differentiation Test" include("extensions/ad.jl")
114115
@safetestset "LabelledArrays Test" include("labelledarrays.jl")
115116
end

0 commit comments

Comments
 (0)