Skip to content
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

Add SciML.build_solution #28

Merged
merged 9 commits into from
Feb 3, 2021
10 changes: 9 additions & 1 deletion src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ function SciMLBase.solve(prob::NonlinearProblem,
kwargs...)
solver = init(prob, alg, args...; kwargs...)
sol = solve!(solver)
return sol
if typeof(sol) <: NewtonSolution
Copy link
Member

Choose a reason for hiding this comment

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

Why is this branching needed? It makes the solution type unstable and break non-Newton solvers. We can just assume that sol.u exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are two types of solver, Newton solver and Bracketing methods, which outputs sol.left and sol.right. That's why I wanted to ask how to pass bracketing methods to build_solution?

resid = zero(prob.u0)
YingboMa marked this conversation as resolved.
Show resolved Hide resolved
if isinplace(prob)
prob.f(resid,sol.u,prob.p)
else
resid = prob.f(sol.u,prob.p)
end
return SciMLBase.build_solution(prob, alg, sol.u, resid;retcode=:Success)
end
end

function SciMLBase.init(prob::NonlinearProblem{uType, iip}, alg::AbstractBracketingAlgorithm, args...;
Expand Down
15 changes: 0 additions & 15 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@ function num_types_in_tuple(sig::UnionAll)
length(Base.unwrap_unionall(sig).parameters)
end

function numargs(f)
typ = Tuple{Any, Val{:analytic}, Vararg}
typ2 = Tuple{Any, Type{Val{:analytic}}, Vararg} # This one is required for overloaded types
typ3 = Tuple{Any, Val{:jac}, Vararg}
typ4 = Tuple{Any, Type{Val{:jac}}, Vararg} # This one is required for overloaded types
typ5 = Tuple{Any, Val{:tgrad}, Vararg}
typ6 = Tuple{Any, Type{Val{:tgrad}}, Vararg} # This one is required for overloaded types
numparam = maximum([(m.sig<:typ || m.sig<:typ2 || m.sig<:typ3 || m.sig<:typ4 || m.sig<:typ5 || m.sig<:typ6) ? 0 : num_types_in_tuple(m.sig) for m in methods(f)])
return (numparam-1) #-1 in v0.5 since it adds f as the first parameter
end

function isinplace(f,inplace_param_number)
numargs(f)>=inplace_param_number
end

### Default Linsolve

# Try to be as smart as possible
Expand Down