Skip to content

Commit a0910cb

Browse files
Merge pull request #892 from AayushSabharwal/as/fix-constructorof
fix: fix `constructorof` for `NonlinearLeastSquaresProblem`
2 parents b0dc015 + 1b385ec commit a0910cb

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/problems/nonlinear_problems.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function ConstructionBase.constructorof(::Type{P}) where {P <: NonlinearLeastSqu
340340
else
341341
iip = isinplace(f, 4)
342342
end
343-
return NonlinearProblem{iip}(f, u0, p; kw...)
343+
return NonlinearLeastSquaresProblem{iip}(f, u0, p; kw...)
344344
end
345345
end
346346

test/problem_building_test.jl

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Test, SciMLBase
1+
using Test, SciMLBase, SymbolicIndexingInterface, Accessors
22

33
function simplependulum!(du, u, p, t)
44
θ = u[1]
@@ -12,3 +12,63 @@ function bc!(residual, u, p, t)
1212
end
1313
prob_bvp = BVProblem(simplependulum!, bc!, [pi / 2, pi / 2], (0, 1.0))
1414
@test prob_bvp.tspan === (0.0, 1.0)
15+
16+
@testset "`constructorof` tests" begin
17+
probs = []
18+
19+
function lorenz!(du, u, p, t)
20+
du[1] = p[1] * (u[2] - u[1])
21+
du[2] = u[1] * (p[2] - u[3]) - u[2]
22+
du[3] = u[1] * u[2] - p[3] * u[3]
23+
end
24+
u0 = [1.0; 2.0; 3.0]
25+
du0 = similar(u0)
26+
p = [10.0, 20.0, 30.0]
27+
tspan = (0.0, 100.0)
28+
lorenz!(du0, u0, p, tspan[1])
29+
sys = SymbolCache([:x, :y, :z], [:a, :b, :c], :t)
30+
fn = ODEFunction(lorenz!; sys)
31+
push!(probs, ODEProblem(fn, u0, tspan, p))
32+
33+
function daelorenz!(resid, du, u, p, t)
34+
lorenz!(resid, u, p, t)
35+
resid .-= du
36+
end
37+
fn = DAEFunction(daelorenz!; sys)
38+
push!(probs, DAEProblem(fn, du0, u0, tspan, p))
39+
40+
function ddelorenz!(du, u, h, p, t)
41+
du[1] = p[1] * (u[2] - u[1])
42+
du[2] = u[1] * (p[2] - u[3]) - u[2]
43+
du[3] = u[1] * u[2] - p[3] * u[3]
44+
end
45+
46+
function history(p, t)
47+
return u0 .- t
48+
end
49+
50+
fn = DDEFunction(ddelorenz!; sys)
51+
push!(probs, DDEProblem(fn, u0, history, tspan, p))
52+
53+
function noise!(du, u, p, t)
54+
du .= 0.1u
55+
end
56+
fn = SDEFunction(lorenz!, noise!; sys)
57+
push!(probs, SDEProblem(fn, u0, tspan, p))
58+
59+
fn = SDDEFunction(ddelorenz!, noise!; sys)
60+
push!(probs, SDDEProblem(fn, noise!, u0, history, tspan, p))
61+
62+
function nllorenz!(du, u, p)
63+
lorenz!(du, u, p, 0.0)
64+
end
65+
66+
fn = NonlinearFunction(nllorenz!; sys)
67+
push!(probs, NonlinearProblem(fn, u0, p))
68+
push!(probs, NonlinearLeastSquaresProblem(fn, u0, p))
69+
70+
@testset "$(SciMLBase.parameterless_type(typeof(prob)))" for prob in probs
71+
newprob = @reset prob.u0 = u0 .+ 1
72+
@test typeof(newprob) == typeof(prob)
73+
end
74+
end

0 commit comments

Comments
 (0)