|
| 1 | +import SciMLBase: @add_kwonly, AbstractODEProblem, AbstractODEFunction, |
| 2 | + FunctionWrapperSpecialize, StandardODEProblem, prepare_initial_state, promote_tspan, |
| 3 | + warn_paramtype |
| 4 | + |
| 5 | +struct ImmutableODEProblem{uType, tType, isinplace, P, F, K, PT} <: |
| 6 | + AbstractODEProblem{uType, tType, isinplace} |
| 7 | + """The ODE is `du = f(u,p,t)` for out-of-place and f(du,u,p,t) for in-place.""" |
| 8 | + f::F |
| 9 | + """The initial condition is `u(tspan[1]) = u0`.""" |
| 10 | + u0::uType |
| 11 | + """The solution `u(t)` will be computed for `tspan[1] ≤ t ≤ tspan[2]`.""" |
| 12 | + tspan::tType |
| 13 | + """Constant parameters to be supplied as the second argument of `f`.""" |
| 14 | + p::P |
| 15 | + """A callback to be applied to every solver which uses the problem.""" |
| 16 | + kwargs::K |
| 17 | + """An internal argument for storing traits about the solving process.""" |
| 18 | + problem_type::PT |
| 19 | + @add_kwonly function ImmutableODEProblem{iip}(f::AbstractODEFunction{iip}, |
| 20 | + u0, tspan, p = NullParameters(), |
| 21 | + problem_type = StandardODEProblem(); |
| 22 | + kwargs...) where {iip} |
| 23 | + _u0 = prepare_initial_state(u0) |
| 24 | + _tspan = promote_tspan(tspan) |
| 25 | + warn_paramtype(p) |
| 26 | + new{typeof(_u0), typeof(_tspan), |
| 27 | + isinplace(f), typeof(p), typeof(f), |
| 28 | + typeof(kwargs), |
| 29 | + typeof(problem_type)}(f, |
| 30 | + _u0, |
| 31 | + _tspan, |
| 32 | + p, |
| 33 | + kwargs, |
| 34 | + problem_type) |
| 35 | + end |
| 36 | + |
| 37 | + """ |
| 38 | + ImmutableODEProblem{isinplace}(f,u0,tspan,p=NullParameters(),callback=CallbackSet()) |
| 39 | +
|
| 40 | + Define an ODE problem with the specified function. |
| 41 | + `isinplace` optionally sets whether the function is inplace or not. |
| 42 | + This is determined automatically, but not inferred. |
| 43 | + """ |
| 44 | + function ImmutableODEProblem{iip}(f, |
| 45 | + u0, |
| 46 | + tspan, |
| 47 | + p = NullParameters(); |
| 48 | + kwargs...) where {iip} |
| 49 | + _u0 = prepare_initial_state(u0) |
| 50 | + _tspan = promote_tspan(tspan) |
| 51 | + _f = ODEFunction{iip, DEFAULT_SPECIALIZATION}(f) |
| 52 | + ImmutableODEProblem(_f, _u0, _tspan, p; kwargs...) |
| 53 | + end |
| 54 | + |
| 55 | + @add_kwonly function ImmutableODEProblem{iip, recompile}(f, u0, tspan, |
| 56 | + p = NullParameters(); |
| 57 | + kwargs...) where {iip, recompile} |
| 58 | + ImmutableODEProblem{iip}(ODEFunction{iip, recompile}(f), u0, tspan, p; kwargs...) |
| 59 | + end |
| 60 | + |
| 61 | + function ImmutableODEProblem{iip, FunctionWrapperSpecialize}(f, u0, tspan, |
| 62 | + p = NullParameters(); |
| 63 | + kwargs...) where {iip} |
| 64 | + _u0 = prepare_initial_state(u0) |
| 65 | + _tspan = promote_tspan(tspan) |
| 66 | + if !(f isa FunctionWrappersWrappers.FunctionWrappersWrapper) |
| 67 | + if iip |
| 68 | + ff = ODEFunction{iip, FunctionWrapperSpecialize}(wrapfun_iip(f, |
| 69 | + (_u0, _u0, p, |
| 70 | + _tspan[1]))) |
| 71 | + else |
| 72 | + ff = ODEFunction{iip, FunctionWrapperSpecialize}(wrapfun_oop(f, |
| 73 | + (_u0, p, |
| 74 | + _tspan[1]))) |
| 75 | + end |
| 76 | + end |
| 77 | + ImmutableODEProblem{iip}(ff, _u0, _tspan, p; kwargs...) |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +""" |
| 82 | + ImmutableODEProblem(f::ODEFunction,u0,tspan,p=NullParameters(),callback=CallbackSet()) |
| 83 | +
|
| 84 | +Define an ODE problem from an [`ODEFunction`](@ref). |
| 85 | +""" |
| 86 | +function ImmutableODEProblem(f::AbstractODEFunction, u0, tspan, args...; kwargs...) |
| 87 | + ImmutableODEProblem{isinplace(f)}(f, u0, tspan, args...; kwargs...) |
| 88 | +end |
| 89 | + |
| 90 | +function ImmutableODEProblem(f, u0, tspan, p = NullParameters(); kwargs...) |
| 91 | + iip = isinplace(f, 4) |
| 92 | + _u0 = prepare_initial_state(u0) |
| 93 | + _tspan = promote_tspan(tspan) |
| 94 | + _f = ODEFunction{iip, DEFAULT_SPECIALIZATION}(f) |
| 95 | + ImmutableODEProblem(_f, _u0, _tspan, p; kwargs...) |
| 96 | +end |
| 97 | + |
| 98 | +function Base.convert(::Type{ImmutableODEProblem}, prob::T) where {T <: ODEProblem} |
| 99 | + ImmutableODEProblem(prob.f, |
| 100 | + prob.u0, |
| 101 | + prob.tspan, |
| 102 | + prob.p, |
| 103 | + prob.problem_type; |
| 104 | + prob.kwargs...) |
| 105 | +end |
0 commit comments