diff --git a/src/problems/optimization_problems.jl b/src/problems/optimization_problems.jl index 6d6033ea9..cd3d4436b 100644 --- a/src/problems/optimization_problems.jl +++ b/src/problems/optimization_problems.jl @@ -131,8 +131,7 @@ function OptimizationProblem( OptimizationProblem{isinplace(f)}(f, args...; kwargs...) end function OptimizationProblem(f, args...; kwargs...) - isinplace(f, 2, has_two_dispatches = false) - OptimizationProblem{true}(OptimizationFunction{true}(f), args...; kwargs...) + OptimizationProblem(OptimizationFunction(f), args...; kwargs...) end function OptimizationFunction( diff --git a/src/scimlfunctions.jl b/src/scimlfunctions.jl index 1b61bd491..9704ccbce 100644 --- a/src/scimlfunctions.jl +++ b/src/scimlfunctions.jl @@ -948,7 +948,7 @@ dt: the time step ```julia ImplicitDiscreteFunction{iip,specialize}(f; - analytic = __has_analytic(f) ? f.analytic : nothing, + analytic = __has_analytic(f) ? f.analytic : nothing, resid_prototype = __has_resid_prototype(f) ? f.resid_prototype : nothing) ``` @@ -2107,7 +2107,7 @@ A representation of a ODE function `f` with inputs, defined by: ```math \frac{dx}{dt} = f(x, u, p, t) ``` -where `x` are the states of the system and `u` are the inputs (which may represent +where `x` are the states of the system and `u` are the inputs (which may represent different things in different contexts, such as control variables in optimal control). Includes all of its related functions, such as the Jacobian of `f`, its gradient @@ -2134,7 +2134,7 @@ ODEInputFunction{iip, specialize}(f; sys = __has_sys(f) ? f.sys : nothing) ``` -`f` should be given as `f(x_out,x,u,p,t)` or `out = f(x,u,p,t)`. +`f` should be given as `f(x_out,x,u,p,t)` or `out = f(x,u,p,t)`. See the section on `iip` for more details on in-place vs out-of-place handling. - `mass_matrix`: the mass matrix `M` represented in the BVP function. Can be used @@ -4199,7 +4199,10 @@ IntervalNonlinearFunction(f::IntervalNonlinearFunction; kwargs...) = f struct NoAD <: AbstractADType end (f::OptimizationFunction)(args...) = f.f(args...) -OptimizationFunction(args...; kwargs...) = OptimizationFunction{true}(args...; kwargs...) +function OptimizationFunction(f, args...; kwargs...) + isinplace(f, 2, outofplace_param_number=2) + OptimizationFunction{true}(f, args...; kwargs...) +end function OptimizationFunction{iip}(f, adtype::AbstractADType = NoAD(); grad = nothing, fg = nothing, hess = nothing, hv = nothing, fgh = nothing, @@ -4251,8 +4254,9 @@ end (f::MultiObjectiveOptimizationFunction)(args...) = f.f(args...) # Convenience constructor -function MultiObjectiveOptimizationFunction(args...; kwargs...) - MultiObjectiveOptimizationFunction{true}(args...; kwargs...) +function MultiObjectiveOptimizationFunction(f, args...; kwargs...) + isinplace(f, 3) + MultiObjectiveOptimizationFunction{true}(f, args...; kwargs...) end # Constructor with keyword arguments @@ -4339,7 +4343,8 @@ function BVPFunction{iip, specialize, twopoint}(f, bc; if iip_f jac = update_coefficients! #(J,u,p,t) else - jac = (u, p, t) -> update_coefficients!(deepcopy(jac_prototype), u, p, t) + jac_prototype_copy = deepcopy(jac_prototype) + jac = (u, p, t) -> update_coefficients!(jac_prototype_copy, u, p, t) end end @@ -4347,7 +4352,8 @@ function BVPFunction{iip, specialize, twopoint}(f, bc; if iip_bc bcjac = update_coefficients! #(J,u,p,t) else - bcjac = (u, p, t) -> update_coefficients!(deepcopy(bcjac_prototype), u, p, t) + bcjac_prototype_copy = deepcopy(bcjac_prototype) + bcjac = (u, p, t) -> update_coefficients!(bcjac_prototype_copy, u, p, t) end end @@ -4512,7 +4518,8 @@ function DynamicalBVPFunction{iip, specialize, twopoint}(f, bc; if iip_f jac = update_coefficients! #(J,u,p,t) else - jac = (u, p, t) -> update_coefficients!(deepcopy(jac_prototype), u, p, t) + jac_prototype_copy = deepcopy(jac_prototype) + jac = (u, p, t) -> update_coefficients!(jac_prototype_copy, u, p, t) end end @@ -4520,7 +4527,8 @@ function DynamicalBVPFunction{iip, specialize, twopoint}(f, bc; if iip_bc bcjac = update_coefficients! #(J,u,p,t) else - bcjac = (u, p, t) -> update_coefficients!(deepcopy(bcjac_prototype), u, p, t) + bcjac_prototype_copy = deepcopy(jac_prototype) + bcjac = (u, p, t) -> update_coefficients!(bcjac_prototype_copy, u, p, t) end end diff --git a/test/aqua.jl b/test/aqua.jl index d3bdc5240..0989ee391 100644 --- a/test/aqua.jl +++ b/test/aqua.jl @@ -29,7 +29,7 @@ end # for method_ambiguity in ambs # @show method_ambiguity # end - @warn "Number of method ambiguities: $(length(ambs))" + !isempty(ambs) &&@warn "Number of method ambiguities: $(length(ambs))" @test length(ambs) ≤ 8 end diff --git a/test/function_building_error_messages.jl b/test/function_building_error_messages.jl index ee7ee4af7..931283ebe 100644 --- a/test/function_building_error_messages.jl +++ b/test/function_building_error_messages.jl @@ -54,8 +54,8 @@ ofboth(u, p, t) = u ofboth(du, u, p, t) = du .= u ODEFunction(ofboth) -ODEFunction{true}(ofboth) -ODEFunction{false}(ofboth) +@inferred ODEFunction{true}(ofboth) +@inferred ODEFunction{false}(ofboth) jac(u, t) = [1.0] @test_throws SciMLBase.TooFewArgumentsError ODEFunction(fiip, jac = jac) @@ -428,8 +428,8 @@ nfboth(u, p) = u nfboth(du, u, p) = du .= u NonlinearFunction(nfboth) -NonlinearFunction{true}(nfboth) -NonlinearFunction{false}(nfboth) +@inferred NonlinearFunction{true}(nfboth) +@inferred NonlinearFunction{false}(nfboth) njac(u) = [1.0] @test_throws SciMLBase.TooFewArgumentsError NonlinearFunction(nfiip, jac = njac) @@ -520,8 +520,8 @@ bcfboth(u, p, t) = u bcfboth(du, u, p, t) = du .= u BVPFunction(bfboth, bcfboth) -BVPFunction{true}(bfboth, bcfboth) -BVPFunction{false}(bfboth, bcfboth) +@inferred BVPFunction{true}(bfboth, bcfboth) +@inferred BVPFunction{false}(bfboth, bcfboth) bjac(u, t) = [1.0] bcjac(u, t) = [1.0] @@ -663,8 +663,8 @@ dbcfboth(du, u, p, t) = u dbcfboth(res, du, u, p, t) = res .= du .- u DynamicalBVPFunction(dbfboth, dbcfboth) -DynamicalBVPFunction{true}(dbfboth, dbcfboth) -DynamicalBVPFunction{false}(dbfboth, dbcfboth) +@inferred DynamicalBVPFunction{true}(dbfboth, dbcfboth) +@inferred DynamicalBVPFunction{false}(dbfboth, dbcfboth) dbjac(du, u, t) = [1.0] dbcjac(du, u, t) = [1.0]