Skip to content

Commit 539af20

Browse files
Add SCCNonlinearProblem Try 1
1 parent ca77954 commit 539af20

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

src/SciMLBase.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,10 @@ export isinplace
791791

792792
export solve, solve!, init, discretize, symbolic_discretize
793793

794-
export LinearProblem, NonlinearProblem, IntervalNonlinearProblem,
795-
IntegralProblem, SampledIntegralProblem, OptimizationProblem,
796-
NonlinearLeastSquaresProblem
794+
export LinearProblem, IntervalNonlinearProblem,
795+
IntegralProblem, SampledIntegralProblem, OptimizationProblem
796+
797+
export NonlinearProblem, SCCNonlinearProblem, NonlinearLeastSquaresProblem
797798

798799
export DiscreteProblem, ImplicitDiscreteProblem
799800
export SteadyStateProblem, SteadyStateSolution

src/problems/nonlinear_problems.jl

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,95 @@ end
321321
function NonlinearLeastSquaresProblem(f, u0, p = NullParameters(); kwargs...)
322322
return NonlinearLeastSquaresProblem(NonlinearFunction(f), u0, p; kwargs...)
323323
end
324+
325+
@doc doc"""
326+
327+
Defines a nonlinear system problem.
328+
Documentation Page: [https://docs.sciml.ai/NonlinearSolve/stable/basics/nonlinear_problem/](https://docs.sciml.ai/NonlinearSolve/stable/basics/nonlinear_problem/)
329+
330+
## Mathematical Specification of a Nonlinear Problem
331+
332+
To define a Nonlinear Problem, you simply need to give the function ``f``
333+
which defines the nonlinear system:
334+
335+
```math
336+
f(u,p) = 0
337+
```
338+
339+
and an initial guess ``u₀`` of where `f(u, p) = 0`. `f` should be specified as `f(u, p)`
340+
(or in-place as `f(du, u, p)`), and `u₀` should be an AbstractArray (or number)
341+
whose geometry matches the desired geometry of `u`. Note that we are not limited
342+
to numbers or vectors for `u₀`; one is allowed to provide `u₀` as arbitrary
343+
matrices / higher-dimension tensors as well.
344+
345+
## Problem Type
346+
347+
### Constructors
348+
349+
```julia
350+
NonlinearProblem(f::NonlinearFunction, u0, p = NullParameters(); kwargs...)
351+
NonlinearProblem{isinplace}(f, u0, p = NullParameters(); kwargs...)
352+
```
353+
354+
`isinplace` optionally sets whether the function is in-place or not. This is
355+
determined automatically, but not inferred.
356+
357+
Parameters are optional, and if not given, then a `NullParameters()` singleton
358+
will be used, which will throw nice errors if you try to index non-existent
359+
parameters. Any extra keyword arguments are passed on to the solvers. For example,
360+
if you set a `callback` in the problem, then that `callback` will be added in
361+
every solve call.
362+
363+
For specifying Jacobians and mass matrices, see the
364+
[NonlinearFunctions](@ref nonlinearfunctions) page.
365+
366+
### Fields
367+
368+
* `f`: The function in the problem.
369+
* `u0`: The initial guess for the root.
370+
* `p`: The parameters for the problem. Defaults to `NullParameters`.
371+
* `kwargs`: The keyword arguments passed on to the solvers.
372+
"""
373+
mutable struct SCCNonlinearProblem{uType, isinplace, P, F, K, PT} <:
374+
AbstractNonlinearProblem{uType, isinplace}
375+
f::F
376+
u0::uType
377+
p::P
378+
problem_type::PT
379+
kwargs::K
380+
@add_kwonly function SCCNonlinearProblem{iip}(f::Union{AbstractVector,Tuple},
381+
u0::Union{AbstractVector,Tuple},
382+
p = NullParameters(),
383+
problem_type = StandardNonlinearProblem();
384+
kwargs...) where {iip}
385+
if !(eltype(f) <: AbstractNonlinearFunction)
386+
f = NonlinearFunction{iip}.(f)
387+
end
388+
389+
eltype(u0) <: AbstractVector || error("!(eltype(u0) <: AbstractVector) detected. SCC states must be vector.")
390+
length(f) != length(u0) && error("Number of SCCs undefined. length(f) = $(length(f)) != $(length(u0)) == length(u0)")
391+
392+
if haskey(kwargs, :p)
393+
error("`p` specified as a keyword argument `p = $(kwargs[:p])` to `NonlinearProblem`. This is not supported.")
394+
end
395+
warn_paramtype(p)
396+
new{typeof(u0), iip, typeof(p), typeof(f),
397+
typeof(kwargs), typeof(problem_type)}(f,
398+
u0,
399+
p,
400+
problem_type,
401+
kwargs)
402+
end
403+
end
404+
405+
"""
406+
$(SIGNATURES)
407+
"""
408+
function SCCNonlinearProblem(f::Union{AbstractVector,Tuple}, u0::Union{AbstractVector,Tuple}, p = NullParameters(); kwargs...)
409+
if !(eltype(f) <: AbstractNonlinearFunction)
410+
f = NonlinearFunction.(f)
411+
end
412+
all(isinplace,f) || all(!inplace,f) || error("All SCC Functions must match in-placeness")
413+
iip = isinplace(f[1])
414+
SCCNonlinearProblem{iip}(f, u0, p; kwargs...)
415+
end

0 commit comments

Comments
 (0)