@@ -20,8 +20,134 @@ function warn_overdetermined(sys, u0map)
2020 end
2121end
2222
23+ """
24+ Generate the control function f(x, u, p, t) from the ODESystem.
25+ Input variables are automatically inferred but can be manually specified.
26+ """
27+ function SciMLBase. ControlFunction {iip, specialize} (sys:: ODESystem ,
28+ dvs = unknowns (sys),
29+ ps = parameters (sys), u0 = nothing ,
30+ inputs = unbound_inputs (sys),
31+ disturbance_inputs = disturbances (sys);
32+ version = nothing , tgrad = false ,
33+ jac = false , controljac = false ,
34+ p = nothing , t = nothing ,
35+ eval_expression = false ,
36+ sparse = false , simplify = false ,
37+ eval_module = @__MODULE__ ,
38+ steady_state = false ,
39+ checkbounds = false ,
40+ sparsity = false ,
41+ analytic = nothing ,
42+ split_idxs = nothing ,
43+ initialization_data = nothing ,
44+ cse = true ,
45+ kwargs... ) where {iip, specialize}
46+
47+ (f), _, _ = generate_control_function (sys, inputs, disturbance_inputs; eval_expression = true , eval_module, cse, kwargs... )
48+
49+ if tgrad
50+ tgrad_gen = generate_tgrad (sys, dvs, ps;
51+ simplify = simplify,
52+ expression = Val{true },
53+ expression_module = eval_module, cse,
54+ checkbounds = checkbounds, kwargs... )
55+ tgrad_oop, tgrad_iip = eval_or_rgf .(tgrad_gen; eval_expression, eval_module)
56+ _tgrad = GeneratedFunctionWrapper {(2, 3, is_split(sys))} (tgrad_oop, tgrad_iip)
57+ else
58+ _tgrad = nothing
59+ end
60+
61+ if jac
62+ jac_gen = generate_jacobian (sys, dvs, ps;
63+ simplify = simplify, sparse = sparse,
64+ expression = Val{true },
65+ expression_module = eval_module, cse,
66+ checkbounds = checkbounds, kwargs... )
67+ jac_oop, jac_iip = eval_or_rgf .(jac_gen; eval_expression, eval_module)
68+
69+ _jac = GeneratedFunctionWrapper {(2, 3, is_split(sys))} (jac_oop, jac_iip)
70+ else
71+ _jac = nothing
72+ end
73+
74+ if controljac
75+ cjac_gen = generate_control_jacobian (sys, dvs, ps;
76+ simplify = simplify, sparse = sparse,
77+ expression = Val{true },
78+ expression_module = eval_module, cse,
79+ checkbounds = checkbounds, kwargs... )
80+ cjac_oop, cjac_iip = eval_or_rgf .(cjac_gen; eval_expression, eval_module)
81+
82+ _cjac = GeneratedFunctionWrapper {(2, 3, is_split(sys))} (cjac_oop, cjac_iip)
83+ else
84+ _cjac = nothing
85+ end
86+
87+ M = calculate_massmatrix (sys)
88+ _M = if sparse && ! (u0 === nothing || M === I)
89+ SparseArrays. sparse (M)
90+ elseif u0 === nothing || M === I
91+ M
92+ else
93+ ArrayInterface. restructure (u0 .* u0' , M)
94+ end
95+
96+ observedfun = ObservedFunctionCache (
97+ sys; steady_state, eval_expression, eval_module, checkbounds, cse)
98+
99+ if sparse
100+ uElType = u0 === nothing ? Float64 : eltype (u0)
101+ W_prototype = similar (W_sparsity (sys), uElType)
102+ controljac_prototype = similar (calculate_control_jacobian (sys), uElType)
103+ else
104+ W_prototype = nothing
105+ controljac_prototype = nothing
106+ end
107+
108+ ControlFunction {iip, specialize} (f;
109+ sys = sys,
110+ jac = _jac === nothing ? nothing : _jac,
111+ controljac = _cjac === nothing ? nothing : _cjac,
112+ tgrad = _tgrad === nothing ? nothing : _tgrad,
113+ mass_matrix = _M,
114+ jac_prototype = W_prototype,
115+ controljac_prototype = controljac_prototype,
116+ observed = observedfun,
117+ sparsity = sparsity ? W_sparsity (sys) : nothing ,
118+ analytic = analytic,
119+ initialization_data)
120+ end
121+
122+ function SciMLBase. ControlFunction (sys:: AbstractODESystem , args... ; kwargs... )
123+ ControlFunction {true} (sys, args... ; kwargs... )
124+ end
125+
126+ function SciMLBase. ControlFunction {true} (sys:: AbstractODESystem , args... ;
127+ kwargs... )
128+ ControlFunction {true, SciMLBase.AutoSpecialize} (sys, args... ; kwargs... )
129+ end
130+
131+ function SciMLBase. ControlFunction {false} (sys:: AbstractODESystem , args... ;
132+ kwargs... )
133+ ControlFunction {false, SciMLBase.FullSpecialize} (sys, args... ; kwargs... )
134+ end
135+
23136"""
24137IntegralNorm. When applied to an expression.
25138"""
26139struct IntegralNorm end
27140
141+ """
142+ $(SIGNATURES)
143+
144+ Define one or more inputs.
145+
146+ See also [`@independent_variables`](@ref), [`@variables`](@ref) and [`@constants`](@ref).
147+ """
148+ macro inputs (xs... )
149+ Symbolics. _parse_vars (:inputs ,
150+ Real,
151+ xs,
152+ toparam) |> esc
153+ end
0 commit comments