Skip to content

Commit 84076da

Browse files
Convert LinearFunction to the SciMLFunctionOptions pattern
LinearFunction was the one *Function constructor never converted in the earlier SciMLFunctionOptions rollout — it read cachesyms/compiler_options out of a raw kwargs dict via get(kwargs, :key, default) instead of going through the options struct. Give it the same treatment as the other 12: a kwargs-based wrapper that builds SciMLFunctionOptions and delegates, plus a public (sys, opts) method holding the real logic. cachesyms and structural_hint aren't SciMLFunctionOptions fields (cachesyms is SCCNonlinearFunction-specific plumbing, not a codegen option) and stay explicit keywords on both methods, mirroring how steady_state etc. stay explicit on the other *Function constructors. This is a prerequisite for converting LinearProblem itself to accept SciMLProblemOptions, since LinearProblem calls LinearFunction as its `constructor` argument to process_SciMLProblem. Validated against linearproblem.jl (73 assertions, all passing). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent b1e1c4e commit 84076da

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

lib/ModelingToolkitBase/src/problems/linearproblem.jl

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,37 @@ end
2020
Moshi.Derive.@derive StructuralHint[Show]
2121

2222
function LinearFunction{iip}(
23-
sys::System; expression = Val{false}, check_compatibility = true,
23+
sys::System; u0 = nothing, p = nothing, t = nothing,
24+
expression = Val{false}, check_compatibility = true,
2425
sparse = false, eval_expression = false, eval_module = @__MODULE__,
25-
checkbounds = false,
26-
structural_hint::StructuralHint.Type = StructuralHint.NoHint(), kwargs...
26+
checkbounds = false, optimize = nothing,
27+
compiler_options::CompilerOptions = CompilerOptions(),
28+
structural_hint::StructuralHint.Type = StructuralHint.NoHint(),
29+
cachesyms = (), kwargs...
2730
) where {iip}
31+
opts = SciMLFunctionOptions(;
32+
u0, p, t, sparse, expression, check_compatibility, eval_expression, eval_module,
33+
compiler_options, checkbounds, optimize, kwargs...,
34+
)
35+
return LinearFunction{iip}(sys, opts; structural_hint, cachesyms)
36+
end
37+
38+
"""
39+
LinearFunction{iip}(sys::System, opts::SciMLFunctionOptions; kwargs...)
40+
41+
Public entry point that builds a `LinearFunction` directly from a pre-assembled
42+
[`SciMLFunctionOptions`](@ref), bypassing the `kwargs...` wrapper above.
43+
"""
44+
function LinearFunction{iip}(
45+
sys::System, opts::SciMLFunctionOptions{E};
46+
structural_hint::StructuralHint.Type = StructuralHint.NoHint(),
47+
cachesyms = ()
48+
) where {iip, E}
2849
check_complete(sys, LinearProblem)
29-
check_compatibility && check_compatible_system(LinearProblem, sys)
50+
opts.check_compatibility && check_compatible_system(LinearProblem, sys)
51+
52+
(; sparse) = opts
53+
codegen_opts = opts.codegen
3054

3155
A, b = calculate_A_b(sys; sparse)
3256
A = Moshi.Match.@match structural_hint begin
@@ -37,22 +61,11 @@ function LinearFunction{iip}(
3761
BandedMatrix{SymbolicT, Matrix{SymbolicT}}(A, (lower_band_size, upper_band_size))
3862
end
3963
end
40-
# `cachesyms` is a structural kwarg of the update generators (not a codegen option);
41-
# in the SCC path it arrives via `kwargs`, so extract it rather than funnelling it
42-
# into the codegen options where it would be dropped.
43-
cachesyms = get(kwargs, :cachesyms, ())
44-
codegen_opts = GeneratedFunctionOptions(;
45-
expression, wrap_gfw = Val{true}, eval_expression, eval_module,
46-
compiler_options = get(kwargs, :compiler_options, CompilerOptions()),
47-
codegen_function_options = Symbolics.CodegenFunctionOptions(; checkbounds, kwargs...)
48-
)
4964
update_A = generate_update_A(sys, A, codegen_opts; cachesyms)
5065
update_b = generate_update_b(sys, b, codegen_opts; cachesyms)
51-
observedfun = ObservedFunctionCache(
52-
sys; steady_state = false, expression, eval_expression, eval_module, checkbounds
53-
)
66+
observedfun = ObservedFunctionCache(sys, codegen_opts; steady_state = false)
5467

55-
if expression == Val{true}
68+
if E
5669
symbolic_interface = quote
5770
update_A = $update_A
5871
update_b = $update_b

0 commit comments

Comments
 (0)