Skip to content

Commit

Permalink
Add SecondOrderBVProblem
Browse files Browse the repository at this point in the history
Signed-off-by: ErikQQY <[email protected]>
  • Loading branch information
ErikQQY committed Apr 14, 2024
1 parent 7fbd1cf commit f4994cf
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/SciMLBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export SDDEProblem
export PDEProblem
export IncrementingODEProblem

export BVProblem, TwoPointBVProblem
export BVProblem, TwoPointBVProblem, SecondOrderBVProblem

export remake

Expand Down
111 changes: 111 additions & 0 deletions src/problems/bvp_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,114 @@ function TwoPointBVProblem(f::AbstractODEFunction, bc, initialGuess, tspan::Abst
u0 = [initialGuess(i) for i in tspan]
return TwoPointBVProblem(f, bc, u0, (tspan[1], tspan[end]), p; kwargs...)
end

"""
$(TYPEDEF)
"""
struct StandardSecondOrderBVProblem end

@doc doc"""
Defines a second order BVP problem.
Documentation Page: [https://docs.sciml.ai/DiffEqDocs/stable/types/bvp_types/](https://docs.sciml.ai/DiffEqDocs/stable/types/bvp_types/)
## Mathematical Specification of a second order BVP Problem
To define a BVP Problem, you simply need to give the function ``f`` and the initial
condition ``u_0`` which define an ODE:
```math
\frac{ddu}{dt} = f(du,u,p,t)
```
along with an implicit function `bc` which defines the residual equation, where
```math
bc(du,u,p,t) = 0
```
is the manifold on which the solution must live.
## Problem Type
### Constructors
```julia
SecondOrderBVProblem{isinplace}(f, bc, u0, tspan, p=NullParameters(); kwargs...)
```
or if we have an initial guess function `initialGuess(p, t)` for the given BVP,
we can pass the initial guess to the problem constructors:
```julia
SecondOrderBVProblem{isinplace}(f, bc, initialGuess, tspan, p=NullParameters(); kwargs...)
```
For any BVP problem type, `bc` must be inplace if `f` is inplace. Otherwise it must be
out-of-place.
If the bvp is a StandardSecondOrderBVProblem (also known as a Multi-Point BV Problem) it must define
either of the following functions
```julia
bc!(residual, du, u, p, t)
residual = bc(du, u, p, t)
```
where `residual` computed from the current `u`. `u` is an array of solution values
where `u[i]` is at time `t[i]`, while `p` are the parameters. For a `TwoPointBVProblem`,
`t = tspan`. For the more general `BVProblem`, `u` can be all of the internal
time points, and for shooting type methods `u=sol` the ODE solution.
Note that all features of the `ODESolution` are present in this form.
In both cases, the size of the residual matches the size of the initial condition.
Parameters are optional, and if not given, then a `NullParameters()` singleton
will be used which will throw nice errors if you try to index non-existent
parameters. Any extra keyword arguments are passed on to the solvers. For example,
if you set a `callback` in the problem, then that `callback` will be added in
every solve call.
### Fields
* `f`: The function for the ODE.
* `bc`: The boundary condition function.
* `u0`: The initial condition. Either the initial condition for the ODE as an
initial value problem, or a `Vector` of values for ``u(t_i)`` for collocation
methods.
* `tspan`: The timespan for the problem.
* `p`: The parameters for the problem. Defaults to `NullParameters`
* `kwargs`: The keyword arguments passed onto the solves.
"""
struct SecondOrderBVProblem{uType, tType, isinplace, nlls, P, F, PT, K} <:
AbstractBVProblem{uType, tType, isinplace, nlls}
f::F
u0::uType
tspan::tType
p::P
problem_type::PT
kwargs::K

@add_kwonly function SecondOrderBVProblem{iip}(f::AbstractBVPFunction{iip}, u0, tspan,

Check warning on line 323 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L323

Added line #L323 was not covered by tests
p = NullParameters(); problem_type = StandardSecondOrderBVProblem(), nlls = nothing,
kwargs...) where {iip}
_u0 = prepare_initial_state(u0)
_tspan = promote_tspan(tspan)
warn_paramtype(p)

Check warning on line 328 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L326-L328

Added lines #L326 - L328 were not covered by tests

return new{typeof(_u0), typeof(_tspan), iip, typeof(nlls), typeof(p), typeof(f),

Check warning on line 330 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L330

Added line #L330 was not covered by tests
typeof(problem_type), typeof(kwargs)}(f, _u0, _tspan, p, problem_type, kwargs)
end

function SecondOrderBVProblem{iip}(f, bc, u0, tspan, p = NullParameters(); kwargs...) where {iip}
SecondOrderBVProblem(BVPFunction{iip}(f, bc), u0, tspan, p; kwargs...)

Check warning on line 335 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L334-L335

Added lines #L334 - L335 were not covered by tests
end
end

function SecondOrderBVProblem(f, bc, u0, tspan, p = NullParameters(); kwargs...)
iip = isinplace(f, 5)
return SecondOrderBVProblem{iip}(BVPFunction{iip}(f, bc), u0, tspan, p; kwargs...)

Check warning on line 341 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L339-L341

Added lines #L339 - L341 were not covered by tests
end

function SecondOrderBVProblem(f::AbstractBVPFunction, u0, tspan, p = NullParameters(); kwargs...)
return SecondOrderBVProblem{isinplace(f)}(f, u0, tspan, p; kwargs...)

Check warning on line 345 in src/problems/bvp_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/bvp_problems.jl#L344-L345

Added lines #L344 - L345 were not covered by tests
end
3 changes: 2 additions & 1 deletion src/scimlfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,8 @@ function BVPFunction{iip, specialize, twopoint}(f, bc;
end

bciip = if !twopoint
isinplace(bc, 4, "bc", iip)
# bc in SecondOrderBVProblem should have 5 arguments
isinplace(f, 5) ? isinplace(bc, 5, "bc", iip) : isinplace(bc, 4, "bc", iip)

Check warning on line 3755 in src/scimlfunctions.jl

View check run for this annotation

Codecov / codecov/patch

src/scimlfunctions.jl#L3755

Added line #L3755 was not covered by tests
else
@assert length(bc) == 2
bc = Tuple(bc)
Expand Down

0 comments on commit f4994cf

Please sign in to comment.