-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathnonlinearproblem.jl
More file actions
186 lines (163 loc) · 5.76 KB
/
Copy pathnonlinearproblem.jl
File metadata and controls
186 lines (163 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
@fallback_iip_specialize function SciMLBase.NonlinearFunction{iip, spec}(
sys::System; u0 = nothing, p = nothing, jac = false,
eval_expression = false, eval_module = @__MODULE__, sparse = false,
checkbounds = false, sparsity = false, analytic = nothing,
simplify = false, cse = true, initialization_data = nothing,
resid_prototype = nothing, check_compatibility = true, expression = Val{false},
kwargs...
) where {iip, spec}
check_complete(sys, NonlinearFunction)
check_compatibility && check_compatible_system(NonlinearFunction, sys)
f = generate_rhs(
sys; expression, wrap_gfw = Val{true},
eval_expression, eval_module, checkbounds = checkbounds, cse,
kwargs...
)
if spec === SciMLBase.FunctionWrapperSpecialize && iip
if u0 === nothing || p === nothing
error("u0, and p must be specified for FunctionWrapperSpecialize on NonlinearFunction.")
end
if expression == Val{true}
f = :($(SciMLBase.wrapfun_iip)($f, ($u0, $u0, $p)))
else
f = SciMLBase.wrapfun_iip(f, (u0, u0, p))
end
end
if jac
_jac = generate_jacobian(
sys; expression,
wrap_gfw = Val{true}, simplify, sparse, cse, eval_expression, eval_module,
checkbounds, kwargs...
)
else
_jac = nothing
end
observedfun = ObservedFunctionCache(
sys; steady_state = false, expression, eval_expression, eval_module, checkbounds,
cse
)
if sparse
jac_prototype = similar(calculate_jacobian(sys; sparse), eltype(u0))
else
jac_prototype = nothing
end
kwargs = (;
sys = sys,
jac = _jac,
observed = observedfun,
analytic = analytic,
jac_prototype,
resid_prototype,
initialization_data,
)
args = (; f)
return maybe_codegen_scimlfn(expression, NonlinearFunction{iip, spec}, args; kwargs...)
end
function _get_nonlinear_bounds_arrays(unknowns_list, u0)
if !any(hasbounds, unknowns_list)
return nothing, nothing
end
T = u0 === nothing ? Float64 : eltype(u0)
if !(T <: Number)
T = Float64
end
N = length(unknowns_list)
lb = Vector{T}(undef, N)
ub = Vector{T}(undef, N)
for (i, v) in enumerate(unknowns_list)
b = getbounds(v)
if b !== nothing && length(b) >= 2
lb[i] = b[1] !== nothing ? T(b[1]) : typemin(T)
ub[i] = b[2] !== nothing ? T(b[2]) : typemax(T)
else
lb[i] = typemin(T)
ub[i] = typemax(T)
end
end
return lb, ub
end
@fallback_iip_specialize function SciMLBase.NonlinearProblem{iip, spec}(
sys::System, op; expression = Val{false},
check_length = true, check_compatibility = true, kwargs...
) where {iip, spec}
check_complete(sys, NonlinearProblem)
if is_time_dependent(sys)
sys = NonlinearSystem(sys)
end
check_compatibility && check_compatible_system(NonlinearProblem, sys)
_iip = resolve_iip(iip, op)
f, u0,
p = process_SciMLProblem(
NonlinearFunction{_iip, spec}, sys, op;
check_length, check_compatibility, expression, kwargs...
)
kwargs = process_kwargs(sys; kwargs...)
ptype = getmetadata(sys, ProblemTypeCtx, StandardNonlinearProblem())
if !haskey(kwargs, :lb) && !haskey(kwargs, :ub)
lb, ub = _get_nonlinear_bounds_arrays(unknowns(sys), u0)
if lb !== nothing && ub !== nothing
kwargs = merge(kwargs, (lb = lb, ub = ub))
end
end
if expression == Val{false}
prob = SciMLBase.NonlinearProblem(f, u0, p; kwargs...)
if !(ptype isa StandardNonlinearProblem)
prob = SciMLBase.remake(prob; problem_type = ptype)
end
return prob
else
args = ptype isa StandardNonlinearProblem ? (; f, u0, p) : (; f, u0, p, ptype)
return maybe_codegen_scimlproblem(expression, NonlinearProblem{_iip}, args; kwargs...)
end
end
@fallback_iip_specialize function SciMLBase.NonlinearLeastSquaresProblem{iip, spec}(
sys::System, op; check_length = false,
check_compatibility = true, expression = Val{false}, kwargs...
) where {iip, spec}
check_complete(sys, NonlinearLeastSquaresProblem)
check_compatibility && check_compatible_system(NonlinearLeastSquaresProblem, sys)
_iip = resolve_iip(iip, op)
f, u0,
p = process_SciMLProblem(
NonlinearFunction{_iip}, sys, op;
check_length, expression, kwargs...
)
kwargs = process_kwargs(sys; kwargs...)
if !haskey(kwargs, :lb) && !haskey(kwargs, :ub)
lb, ub = _get_nonlinear_bounds_arrays(unknowns(sys), u0)
if lb !== nothing && ub !== nothing
kwargs = merge(kwargs, (lb = lb, ub = ub))
end
end
if expression == Val{false}
return SciMLBase.NonlinearLeastSquaresProblem(f, u0, p; kwargs...)
else
args = (; f, u0, p)
return maybe_codegen_scimlproblem(
expression, NonlinearLeastSquaresProblem{_iip}, args; kwargs...
)
end
end
function check_compatible_system(
T::Union{
Type{NonlinearFunction}, Type{NonlinearProblem},
Type{NonlinearLeastSquaresProblem},
}, sys::System
)
check_time_independent(sys, T)
check_not_dde(sys)
check_no_cost(sys, T)
check_no_constraints(sys, T)
check_no_jumps(sys, T)
return check_no_noise(sys, T)
end
function calculate_resid_prototype(N, u0, p)
u0ElType = u0 === nothing ? Float64 : eltype(u0)
if SciMLStructures.isscimlstructure(p)
u0ElType = promote_type(
eltype(SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1]),
u0ElType
)
end
return zeros(u0ElType, N)
end