Skip to content

Commit 915dbf6

Browse files
Merge pull request #4438 from SciML/as/codegen-both
feat: [AI] add `Both` and use it as default `iip` specifier
2 parents fcab2ba + 130c4ce commit 915dbf6

13 files changed

Lines changed: 68 additions & 48 deletions

lib/ModelingToolkitBase/src/ModelingToolkitBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ const set_scalar_metadata = setmetadata
351351
@public renamespace, namespace_equations
352352
@public check_mutable_cache, store_to_mutable_cache!, should_invalidate_mutable_cache_entry
353353
@public convert_bindings_for_time_independent_system, get_w
354+
@public Both
354355

355356
for prop in [SYS_PROPS; [:continuous_events, :discrete_events]]
356357
getter = Symbol(:get_, prop)

lib/ModelingToolkitBase/src/problems/bvproblem.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
check_compatibility && check_compatible_system(BVProblem, sys)
1010
isnothing(callback) || error("BVP solvers do not support callbacks.")
1111

12+
_iip = resolve_iip(iip, op)
1213
dvs = unknowns(sys)
1314
op = to_varmap(op, dvs)
1415
# Systems without algebraic equations should use both fixed values + guesses
@@ -17,7 +18,7 @@
1718

1819
fode, u0,
1920
p = process_SciMLProblem(
20-
ODEFunction{iip, spec}, sys, _op; guesses,
21+
ODEFunction{_iip, spec}, sys, _op; guesses,
2122
t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse,
2223
checkbounds, time_dependent_init = false, expression, kwargs...
2324
)
@@ -39,7 +40,7 @@
3940
f_prototype = n_controls > 0 ? zeros(eltype(u0), length(dvs) - n_controls) : nothing
4041
bcresid_prototype = zeros(eltype(u0), length(u0_idxs) + length(constraints(sys)))
4142

42-
bvpfn = BVPFunction{iip}(fode, fbc; cost = fcost, f_prototype, bcresid_prototype)
43+
bvpfn = BVPFunction{_iip}(fode, fbc; cost = fcost, f_prototype, bcresid_prototype)
4344

4445
if (length(constraints(sys)) + length(op) > length(dvs))
4546
@warn "The BVProblem is overdetermined. The total number of conditions (# constraints + # fixed initial values given by op) exceeds the total number of states. The BVP solvers will default to doing a nonlinear least-squares optimization."
@@ -48,7 +49,7 @@
4849
kwargs = process_kwargs(sys; expression, tspan, kwargs...)
4950
args = (; bvpfn, u0, tspan, p)
5051

51-
return maybe_codegen_scimlproblem(expression, BVProblem{iip}, args; kwargs...)
52+
return maybe_codegen_scimlproblem(expression, BVProblem{_iip}, args; kwargs...)
5253
end
5354

5455
function check_compatible_system(T::Type{BVProblem}, sys::System)

lib/ModelingToolkitBase/src/problems/daeproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ end
7575
check_complete(sys, DAEProblem)
7676
check_compatibility && check_compatible_system(DAEProblem, sys)
7777

78+
_iip = resolve_iip(iip, op)
7879
f, du0,
7980
u0,
8081
p = process_SciMLProblem(
81-
DAEFunction{iip, spec}, sys, op;
82+
DAEFunction{_iip, spec}, sys, op;
8283
t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression,
8384
eval_module, check_compatibility, implicit_dae = true, expression, kwargs...
8485
)
@@ -95,5 +96,5 @@ end
9596
args = (; f, du0, u0, tspan, p)
9697
kwargs = (; differential_vars, kwargs...)
9798

98-
return maybe_codegen_scimlproblem(expression, DAEProblem{iip}, args; kwargs...)
99+
return maybe_codegen_scimlproblem(expression, DAEProblem{_iip}, args; kwargs...)
99100
end

lib/ModelingToolkitBase/src/problems/ddeproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ end
5252
check_complete(sys, DDEProblem)
5353
check_compatibility && check_compatible_system(DDEProblem, sys)
5454

55+
_iip = resolve_iip(iip, op)
5556
f, u0,
5657
p = process_SciMLProblem(
57-
DDEFunction{iip, spec}, sys, op;
58+
DDEFunction{_iip, spec}, sys, op;
5859
t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds,
5960
eval_expression, eval_module, check_compatibility, symbolic_u0 = true,
6061
expression, u0_constructor, kwargs...
@@ -80,7 +81,7 @@ end
8081
)
8182
args = (; f, u0, h, tspan, p)
8283

83-
return maybe_codegen_scimlproblem(expression, DDEProblem{iip}, args; kwargs...)
84+
return maybe_codegen_scimlproblem(expression, DDEProblem{_iip}, args; kwargs...)
8485
end
8586

8687
function check_compatible_system(T::Union{Type{DDEFunction}, Type{DDEProblem}}, sys::System)

lib/ModelingToolkitBase/src/problems/discreteproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ end
4848
check_complete(sys, DiscreteProblem)
4949
check_compatibility && check_compatible_system(DiscreteProblem, sys)
5050

51+
_iip = resolve_iip(iip, op)
5152
dvs = unknowns(sys)
5253
op = to_varmap(op, dvs)
5354
add_toterms!(op; replace = true)
5455
f, u0,
5556
p = process_SciMLProblem(
56-
DiscreteFunction{iip, spec}, sys, op;
57+
DiscreteFunction{_iip, spec}, sys, op;
5758
t = tspan !== nothing ? tspan[1] : tspan, check_compatibility, expression,
5859
kwargs...
5960
)
@@ -67,7 +68,7 @@ end
6768
kwargs = process_kwargs(sys; kwargs...)
6869
args = (; f, u0, tspan, p)
6970

70-
return maybe_codegen_scimlproblem(expression, DiscreteProblem{iip}, args; kwargs...)
71+
return maybe_codegen_scimlproblem(expression, DiscreteProblem{_iip}, args; kwargs...)
7172
end
7273

7374
function check_compatible_system(

lib/ModelingToolkitBase/src/problems/implicitdiscreteproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ end
5555
check_complete(sys, ImplicitDiscreteProblem)
5656
check_compatibility && check_compatible_system(ImplicitDiscreteProblem, sys)
5757

58+
_iip = resolve_iip(iip, op)
5859
dvs = unknowns(sys)
5960
op = to_varmap(op, dvs)
6061
add_toterms!(op; replace = true)
6162
f, u0,
6263
p = process_SciMLProblem(
63-
ImplicitDiscreteFunction{iip, spec}, sys, op;
64+
ImplicitDiscreteFunction{_iip, spec}, sys, op;
6465
t = tspan !== nothing ? tspan[1] : tspan, check_compatibility,
6566
expression, kwargs...
6667
)
6768

6869
kwargs = process_kwargs(sys; kwargs...)
6970
args = (; f, u0, tspan, p)
7071
return maybe_codegen_scimlproblem(
71-
expression, ImplicitDiscreteProblem{iip}, args; kwargs...
72+
expression, ImplicitDiscreteProblem{_iip}, args; kwargs...
7273
)
7374
end
7475

lib/ModelingToolkitBase/src/problems/initializationproblem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ All other keyword arguments are forwarded to the wrapped problem constructor.
3939
if !iscomplete(sys)
4040
error("A completed system is required. Call `complete` or `mtkcompile` on the system before creating an `ODEProblem`")
4141
end
42+
_iip = resolve_iip(iip, op)
4243
if !fast_path
4344
op = build_operating_point(sys, op)
4445
fast_path = true
@@ -122,7 +123,7 @@ All other keyword arguments are forwarded to the wrapped problem constructor.
122123
sys, isys; warn_initialize_determined,
123124
kwargs...
124125
)
125-
TProb{iip}(isys, op; kwargs..., build_initializeprob = false, is_initializeprob = true)
126+
TProb{_iip}(isys, op; kwargs..., build_initializeprob = false, is_initializeprob = true)
126127
end
127128

128129
function overdetermined_initialization_message(neqs::Integer, nunknown::Integer, extra::AbstractString)

lib/ModelingToolkitBase/src/problems/nonlinearproblem.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,18 @@ end
7171
end
7272
check_compatibility && check_compatible_system(NonlinearProblem, sys)
7373

74+
_iip = resolve_iip(iip, op)
7475
f, u0,
7576
p = process_SciMLProblem(
76-
NonlinearFunction{iip, spec}, sys, op;
77+
NonlinearFunction{_iip, spec}, sys, op;
7778
check_length, check_compatibility, expression, kwargs...
7879
)
7980

8081
kwargs = process_kwargs(sys; kwargs...)
8182
ptype = getmetadata(sys, ProblemTypeCtx, StandardNonlinearProblem())
8283
args = (; f, u0, p, ptype)
8384

84-
return maybe_codegen_scimlproblem(expression, NonlinearProblem{iip}, args; kwargs...)
85+
return maybe_codegen_scimlproblem(expression, NonlinearProblem{_iip}, args; kwargs...)
8586
end
8687

8788
@fallback_iip_specialize function SciMLBase.NonlinearLeastSquaresProblem{iip, spec}(
@@ -91,17 +92,18 @@ end
9192
check_complete(sys, NonlinearLeastSquaresProblem)
9293
check_compatibility && check_compatible_system(NonlinearLeastSquaresProblem, sys)
9394

95+
_iip = resolve_iip(iip, op)
9496
f, u0,
9597
p = process_SciMLProblem(
96-
NonlinearFunction{iip}, sys, op;
98+
NonlinearFunction{_iip}, sys, op;
9799
check_length, expression, kwargs...
98100
)
99101

100102
kwargs = process_kwargs(sys; kwargs...)
101103
args = (; f, u0, p)
102104

103105
return maybe_codegen_scimlproblem(
104-
expression, NonlinearLeastSquaresProblem{iip}, args; kwargs...
106+
expression, NonlinearLeastSquaresProblem{_iip}, args; kwargs...
105107
)
106108
end
107109

lib/ModelingToolkitBase/src/problems/odeproblem.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ Base.@nospecializeinfer @fallback_iip_specialize function SciMLBase.ODEProblem{i
107107
check_complete(sys, ODEProblem)
108108
check_compatibility && check_compatible_system(ODEProblem, sys)
109109

110+
_iip = resolve_iip(iip, op)
110111
f, u0,
111112
p = process_SciMLProblem(
112-
ODEFunction{iip, spec}, sys, op;
113+
ODEFunction{_iip, spec}, sys, op;
113114
t = tspan !== nothing ? tspan[1] : tspan, check_length, eval_expression,
114115
eval_module, expression, check_compatibility, kwargs...
115116
)
@@ -120,7 +121,7 @@ Base.@nospecializeinfer @fallback_iip_specialize function SciMLBase.ODEProblem{i
120121

121122
ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem())
122123
args = (; f, u0, tspan, p, ptype)
123-
maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...)
124+
maybe_codegen_scimlproblem(expression, ODEProblem{_iip}, args; kwargs...)
124125
end
125126

126127
@fallback_iip_specialize function DiffEqBase.SteadyStateProblem{iip, spec}(
@@ -130,17 +131,18 @@ end
130131
check_complete(sys, SteadyStateProblem)
131132
check_compatibility && check_compatible_system(SteadyStateProblem, sys)
132133

134+
_iip = resolve_iip(iip, op)
133135
f, u0,
134136
p = process_SciMLProblem(
135-
ODEFunction{iip}, sys, op;
137+
ODEFunction{_iip}, sys, op;
136138
steady_state = true, check_length, check_compatibility, expression,
137139
is_steadystateprob = true, kwargs...
138140
)
139141

140142
kwargs = process_kwargs(sys; expression, tspan = (0, Inf), kwargs...)
141143
args = (; f, u0, p)
142144

143-
maybe_codegen_scimlproblem(expression, SteadyStateProblem{iip}, args; kwargs...)
145+
maybe_codegen_scimlproblem(expression, SteadyStateProblem{_iip}, args; kwargs...)
144146
end
145147

146148
function check_compatible_system(

lib/ModelingToolkitBase/src/problems/sddeproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ end
5656
check_complete(sys, SDDEProblem)
5757
check_compatibility && check_compatible_system(SDDEProblem, sys)
5858

59+
_iip = resolve_iip(iip, op)
5960
f, u0,
6061
p = process_SciMLProblem(
61-
SDDEFunction{iip, spec}, sys, op;
62+
SDDEFunction{_iip, spec}, sys, op;
6263
t = tspan !== nothing ? tspan[1] : tspan, check_length, cse, checkbounds,
6364
eval_expression, eval_module, check_compatibility, sparse, symbolic_u0 = true,
6465
expression, u0_constructor, kwargs...
@@ -90,7 +91,7 @@ end
9091
args = (; f, g, u0, h, tspan, p)
9192
kwargs = (; noise, noise_rate_prototype, kwargs...)
9293

93-
return maybe_codegen_scimlproblem(expression, SDDEProblem{iip}, args; kwargs...)
94+
return maybe_codegen_scimlproblem(expression, SDDEProblem{_iip}, args; kwargs...)
9495
end
9596

9697
function check_compatible_system(

0 commit comments

Comments
 (0)