Skip to content

feat: pass resid_prototype as u to null oop NonlinearLeastSquaresProblem #1149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ function build_null_solution(
resid = isnothing(prob.f.resid_prototype) ? Float64[] : copy(prob.f.resid_prototype)
prob.f(resid, prob.u0, prob.p)
else
resid = prob.f(prob.u0, prob.p)
resid = prob.f(prob.f.resid_prototype, prob.p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only do this if u0 === nothing? Why is this needed though?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_null_solution is only called when prob.u0 === nothing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because otherwise the OOP form doesn't know what sort of array to create

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, makes sense in its own way.

end

if success
Expand Down
10 changes: 10 additions & 0 deletions test/downstream/null_de.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ModelingToolkit, OrdinaryDiffEq, SteadyStateDiffEq, Test
using StaticArrays
using ModelingToolkit: t_nounits as t, D_nounits as D
using ForwardDiff

Expand Down Expand Up @@ -77,3 +78,12 @@ sol = solve(unsatprob) # Success
@test_nowarn ForwardDiff.derivative(x_at_1_num, 1.0)
@test_nowarn ForwardDiff.derivative(x_at_1_anal, 1.0)
end

@testset "Null OOP NonlinearLeastSquaresProblem" begin
fn = NonlinearFunction{false}(; resid_prototype = SA[1.0]) do u, p
return zero(u)
end
prob = NonlinearLeastSquaresProblem(fn, nothing)
sol = solve(prob)
@test sol.resid isa SVector{1, Float64}
end
Loading