Skip to content

Commit 58592a8

Browse files
Refactor basic problems into sets of problems
1 parent 8b20758 commit 58592a8

File tree

4 files changed

+250
-250
lines changed

4 files changed

+250
-250
lines changed

src/SciMLBase.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,9 @@ include("problems/discrete_problems.jl")
700700
include("problems/implicit_discrete_problems.jl")
701701
include("problems/steady_state_problems.jl")
702702
include("problems/analytical_problems.jl")
703-
include("problems/basic_problems.jl")
703+
include("problems/linear_problems.jl")
704+
include("problems/nonlinear_problems.jl")
705+
include("problems/integral_problems.jl")
704706
include("problems/ode_problems.jl")
705707
include("problems/rode_problems.jl")
706708
include("problems/sde_problems.jl")

src/problems/integral_problems.jl

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
@doc doc"""
2+
3+
Defines an integral problem.
4+
Documentation Page: https://docs.sciml.ai/Integrals/stable/
5+
6+
## Mathematical Specification of an Integral Problem
7+
8+
Integral problems are multi-dimensional integrals defined as:
9+
10+
```math
11+
\int_{lb}^{ub} f(u,p) du
12+
```
13+
14+
where `p` are parameters. `u` is a `Number` or `AbstractVector`
15+
whose geometry matches the space being integrated.
16+
This space is bounded by the lowerbound `lb` and upperbound `ub`,
17+
which are `Number`s or `AbstractVector`s with the same geometry as `u`.
18+
19+
## Problem Type
20+
21+
### Constructors
22+
23+
```
24+
IntegralProblem(f::AbstractIntegralFunction,domain,p=NullParameters(); kwargs...)
25+
IntegralProblem(f::AbstractIntegralFunction,lb,ub,p=NullParameters(); kwargs...)
26+
IntegralProblem(f,domain,p=NullParameters(); nout=nothing, batch=nothing, kwargs...)
27+
IntegralProblem(f,lb,ub,p=NullParameters(); nout=nothing, batch=nothing, kwargs...)
28+
```
29+
30+
- f: the integrand, callable function `y = f(u,p)` for out-of-place (default) or an
31+
`IntegralFunction` or `BatchIntegralFunction` for inplace and batching optimizations.
32+
- domain: an object representing an integration domain, i.e. the tuple `(lb, ub)`.
33+
- lb: DEPRECATED: Either a number or vector of lower bounds.
34+
- ub: DEPRECATED: Either a number or vector of upper bounds.
35+
- p: The parameters associated with the problem.
36+
- nout: DEPRECATED (see `IntegralFunction`): length of the vector output of the integrand
37+
(by default the integrand is assumed to be scalar)
38+
- batch: DEPRECATED (see `BatchIntegralFunction`): number of points the integrand can
39+
evaluate simultaneously (by default there is no batching)
40+
- kwargs: Keyword arguments copied to the solvers.
41+
42+
Additionally, we can supply iip like IntegralProblem{iip}(...) as true or false to declare at
43+
compile time whether the integrator function is in-place.
44+
45+
### Fields
46+
47+
The fields match the names of the constructor arguments.
48+
"""
49+
struct IntegralProblem{isinplace, P, F, T, K} <: AbstractIntegralProblem{isinplace}
50+
f::F
51+
domain::T
52+
p::P
53+
kwargs::K
54+
@add_kwonly function IntegralProblem{iip}(f::AbstractIntegralFunction{iip}, domain,
55+
p = NullParameters(); nout = nothing, batch = nothing,
56+
kwargs...) where {iip}
57+
warn_paramtype(p)
58+
new{iip, typeof(p), typeof(f), typeof(domain), typeof(kwargs)}(f,
59+
domain, p, kwargs)
60+
end
61+
end
62+
63+
function IntegralProblem(f::AbstractIntegralFunction,
64+
domain,
65+
p = NullParameters();
66+
kwargs...)
67+
IntegralProblem{isinplace(f)}(f, domain, p; kwargs...)
68+
end
69+
70+
@deprecate IntegralProblem{iip}(f::AbstractIntegralFunction,
71+
lb::Union{Number, AbstractVector{<:Number}},
72+
ub::Union{Number, AbstractVector{<:Number}},
73+
p = NullParameters(); kwargs...) where {iip} IntegralProblem{iip}(
74+
f, (lb, ub), p; kwargs...)
75+
76+
function IntegralProblem(f, args...; kwargs...)
77+
IntegralProblem{isinplace(f, 3)}(f, args...; kwargs...)
78+
end
79+
function IntegralProblem{iip}(
80+
f, args...; nout = nothing, batch = nothing, kwargs...) where {iip}
81+
if nout !== nothing || batch !== nothing
82+
@warn "`nout` and `batch` keywords are deprecated in favor of inplace `IntegralFunction`s or `BatchIntegralFunction`s. See the updated Integrals.jl documentation for details."
83+
end
84+
85+
g = if iip
86+
if batch === nothing
87+
output_prototype = nout === nothing ? Array{Float64, 0}(undef) :
88+
Vector{Float64}(undef, nout)
89+
IntegralFunction(f, output_prototype)
90+
else
91+
output_prototype = nout === nothing ? Float64[] :
92+
Matrix{Float64}(undef, nout, 0)
93+
BatchIntegralFunction(f, output_prototype, max_batch = batch)
94+
end
95+
else
96+
if batch === nothing
97+
IntegralFunction(f)
98+
else
99+
BatchIntegralFunction(f, max_batch = batch)
100+
end
101+
end
102+
IntegralProblem(g, args...; kwargs...)
103+
end
104+
105+
function Base.getproperty(prob::IntegralProblem, name::Symbol)
106+
if name === :lb
107+
domain = getfield(prob, :domain)
108+
lb, ub = domain
109+
return lb
110+
elseif name === :ub
111+
domain = getfield(prob, :domain)
112+
lb, ub = domain
113+
return ub
114+
elseif name === :ps
115+
return ParameterIndexingProxy(prob)
116+
end
117+
return Base.getfield(prob, name)
118+
end
119+
120+
struct QuadratureProblem end
121+
@deprecate QuadratureProblem(args...; kwargs...) IntegralProblem(args...; kwargs...)
122+
123+
@doc doc"""
124+
125+
Defines a integral problem over pre-sampled data.
126+
Documentation Page: https://docs.sciml.ai/Integrals/stable/
127+
128+
## Mathematical Specification of a data Integral Problem
129+
130+
Sampled integral problems are defined as:
131+
132+
```math
133+
\sum_i w_i y_i
134+
```
135+
where `y_i` are sampled values of the integrand, and `w_i` are weights
136+
assigned by a quadrature rule, which depend on sampling points `x`.
137+
138+
## Problem Type
139+
140+
### Constructors
141+
142+
```
143+
SampledIntegralProblem(y::AbstractArray, x::AbstractVector; dim=ndims(y), kwargs...)
144+
```
145+
- y: The sampled integrand, must be a subtype of `AbstractArray`.
146+
It is assumed that the values of `y` along dimension `dim`
147+
correspond to the integrand evaluated at sampling points `x`
148+
- x: Sampling points, must be a subtype of `AbstractVector`.
149+
- dim: Dimension along which to integrate. Defaults to the last dimension of `y`.
150+
- kwargs: Keyword arguments copied to the solvers.
151+
152+
### Fields
153+
154+
The fields match the names of the constructor arguments.
155+
"""
156+
struct SampledIntegralProblem{Y, X, K} <: AbstractIntegralProblem{false}
157+
y::Y
158+
x::X
159+
dim::Int
160+
kwargs::K
161+
@add_kwonly function SampledIntegralProblem(y::AbstractArray, x::AbstractVector;
162+
dim = ndims(y),
163+
kwargs...)
164+
@assert dim<=ndims(y) "The integration dimension `dim` is larger than the number of dimensions of the integrand `y`"
165+
@assert length(x)==size(y, dim) "The integrand `y` must have the same length as the sampling points `x` along the integrated dimension."
166+
@assert axes(x, 1)==axes(y, dim) "The integrand `y` must obey the same indexing as the sampling points `x` along the integrated dimension."
167+
new{typeof(y), typeof(x), typeof(kwargs)}(y, x, dim, kwargs)
168+
end
169+
end

src/problems/linear_problems.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
@doc doc"""
2+
3+
Defines a linear system problem.
4+
Documentation Page: https://docs.sciml.ai/LinearSolve/stable/basics/LinearProblem/
5+
6+
## Mathematical Specification of a Linear Problem
7+
8+
### Concrete LinearProblem
9+
10+
To define a `LinearProblem`, you simply need to give the `AbstractMatrix` ``A``
11+
and an `AbstractVector` ``b`` which defines the linear system:
12+
13+
```math
14+
Au = b
15+
```
16+
17+
### Matrix-Free LinearProblem
18+
19+
For matrix-free versions, the specification of the problem is given by an
20+
operator `A(u,p,t)` which computes `A*u`, or in-place as `A(du,u,p,t)`. These
21+
are specified via the `AbstractSciMLOperator` interface. For more details, see
22+
the [SciMLBase Documentation](https://docs.sciml.ai/SciMLBase/stable/).
23+
24+
Note that matrix-free versions of LinearProblem definitions are not compatible
25+
with all solvers. To check a solver for compatibility, use the function xxxxx.
26+
27+
## Problem Type
28+
29+
### Constructors
30+
31+
Optionally, an initial guess ``u₀`` can be supplied which is used for iterative
32+
methods.
33+
34+
```julia
35+
LinearProblem{isinplace}(A,x,p=NullParameters();u0=nothing,kwargs...)
36+
LinearProblem(f::AbstractSciMLOperator,u0,p=NullParameters();u0=nothing,kwargs...)
37+
```
38+
39+
`isinplace` optionally sets whether the function is in-place or not, i.e. whether
40+
the solvers are allowed to mutate. By default this is true for `AbstractMatrix`,
41+
and for `AbstractSciMLOperator`s it matches the choice of the operator definition.
42+
43+
Parameters are optional, and if not given, then a `NullParameters()` singleton
44+
will be used, which will throw nice errors if you try to index non-existent
45+
parameters. Any extra keyword arguments are passed on to the solvers.
46+
47+
### Fields
48+
49+
* `A`: The representation of the linear operator.
50+
* `b`: The right-hand side of the linear system.
51+
* `p`: The parameters for the problem. Defaults to `NullParameters`. Currently unused.
52+
* `u0`: The initial condition used by iterative solvers.
53+
* `kwargs`: The keyword arguments passed on to the solvers.
54+
"""
55+
struct LinearProblem{uType, isinplace, F, bType, P, K} <:
56+
AbstractLinearProblem{bType, isinplace}
57+
A::F
58+
b::bType
59+
u0::uType
60+
p::P
61+
kwargs::K
62+
@add_kwonly function LinearProblem{iip}(A, b, p = NullParameters(); u0 = nothing,
63+
kwargs...) where {iip}
64+
warn_paramtype(p)
65+
new{typeof(u0), iip, typeof(A), typeof(b), typeof(p), typeof(kwargs)}(A, b, u0, p,
66+
kwargs)
67+
end
68+
end
69+
70+
function LinearProblem(A, b, args...; kwargs...)
71+
if A isa AbstractArray
72+
LinearProblem{true}(A, b, args...; kwargs...)
73+
elseif A isa Number
74+
LinearProblem{false}(A, b, args...; kwargs...)
75+
else
76+
LinearProblem{isinplace(A, 4)}(A, b, args...; kwargs...)
77+
end
78+
end

0 commit comments

Comments
 (0)