Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: always build initialization problem #3347

Merged
merged 122 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
77c4aed
fix: handle case when parameter object is `nothing` in `GetUpdatedMTK…
AayushSabharwal Jan 21, 2025
5c873f7
feat: update `u0map` and `pmap` in `build_operating_point`
AayushSabharwal Jan 21, 2025
ba21ac5
feat: add dummy initialization parameters instead of hard constraints
AayushSabharwal Jan 21, 2025
127a0c8
feat: always build initialization problem, handle dummy parameters
AayushSabharwal Jan 21, 2025
a7b73ee
refactor: allow disabling `gensym` in `similar_variable`
AayushSabharwal Jan 21, 2025
3d88375
feat: count shift equations as differential equations
AayushSabharwal Jan 21, 2025
d9a1872
test: provide missing parameters to `NonlinearProblem` test
AayushSabharwal Jan 21, 2025
420a497
feat: add `force_time_independent` keyword to `InitializationProblem`
AayushSabharwal Jan 21, 2025
3153677
feat: expose `force_time_independent` through `process_SciMLProblem`
AayushSabharwal Jan 21, 2025
131ac81
fix: remove delayed terms in initialization system of DDEs
AayushSabharwal Jan 21, 2025
c8a282d
fix: only add dummy parameters for unknowns of time-dependent systems
AayushSabharwal Jan 21, 2025
b4289e6
fix: change differential operator when building initialization for `D…
AayushSabharwal Jan 21, 2025
1dbe295
fix: handle recursive default edge case in initializesystem
AayushSabharwal Jan 21, 2025
3b1dbc1
fix: remove occurrences of dummy parameters when remaking initializeprob
AayushSabharwal Jan 21, 2025
46f2b82
fix: only set solvable parameter values to zero in limited cases
AayushSabharwal Jan 21, 2025
aa3e6db
fix: ensure initialization systems for `SteadyStateProblem` do not in…
AayushSabharwal Jan 21, 2025
7e647ae
test: disambiguate `observed` in initializesystem tests
AayushSabharwal Jan 21, 2025
d419247
test: remove unnecessary test
AayushSabharwal Jan 21, 2025
44c05cd
test: refactor `initialization_data === nothing` tests
AayushSabharwal Jan 21, 2025
fbcacfb
test: update initializeprob type promotion test
AayushSabharwal Jan 21, 2025
0d5adc5
fix: don't build initialization for `JumpProblem`
AayushSabharwal Jan 23, 2025
2618c64
fix: don't use observed equations for initialization of `DiscreteSystem`
AayushSabharwal Jan 23, 2025
ea8b8d6
fix: populate observed into `u0map` for time-independent systems
AayushSabharwal Jan 23, 2025
c4c9ba2
fix: remove incorrect fix to dummy derivatives in `u0map` added in #3337
AayushSabharwal Jan 24, 2025
3d60e04
fix: do not build initialization for `DiscreteProblem`
AayushSabharwal Jan 24, 2025
2e6d172
test: make `NonlinearSystem` initialization tests less temperamental
AayushSabharwal Jan 24, 2025
44a28af
test: mark initialization of `JumpSystem` as broken
AayushSabharwal Jan 24, 2025
cab4506
fix: fix `ReconstructInitializeprob` overriding guesses
AayushSabharwal Jan 24, 2025
8d11252
feat: propagate `algebraic_only` kwarg in `InitializationProblem`
AayushSabharwal Jan 20, 2025
33fe6cd
fix: filter `nothing` values in `op`/`u0map`/`pmap` in `process_SciML…
AayushSabharwal Jan 20, 2025
3e37d24
feat: propagate `algebraic_only`, `allow_incomplete` kwargs to initia…
AayushSabharwal Jan 20, 2025
bca43ba
fix: use `SciMLBase.get_initial_values` in `linearization_function`
AayushSabharwal Jan 20, 2025
f524be8
fix: do not forward keywords to `linearize` in `AnalysisPoint` linear…
AayushSabharwal Jan 20, 2025
77939a3
fix: handle `=> nothing` entries in `u0map` in `generate_initializesy…
AayushSabharwal Feb 4, 2025
7bdc1f6
fix: output variable in `PerturbOutput` should not have a default
AayushSabharwal Feb 4, 2025
a3351ff
fix: better handle `=> nothing` entries in `u0map`
AayushSabharwal Feb 4, 2025
54bc714
test: fix some issues with initialization of InverseControlledTank
AayushSabharwal Feb 4, 2025
d626783
test: add #3319 as a test
AayushSabharwal Feb 4, 2025
e2dc475
test: fix initialization of model in tests
AayushSabharwal Feb 4, 2025
5a75d65
refactor: remove `wrap_array_vars`, `wrap_mtkparameters`, `wrap_param…
AayushSabharwal Feb 5, 2025
2956f0b
feat: add `Initial` operator
AayushSabharwal Feb 5, 2025
3096d09
feat: add initialization parameters in `complete`
AayushSabharwal Feb 5, 2025
c916452
feat: optionally filter `Initial` parameters in `parameters`
AayushSabharwal Feb 5, 2025
665d1d3
feat: put `Initial` parameters at the end of tunables
AayushSabharwal Feb 5, 2025
aeffa95
feat: add single-argument `reorder_parameters`
AayushSabharwal Feb 5, 2025
a726293
fix: fix `zero_var` for array symbolics
AayushSabharwal Feb 5, 2025
2a7432b
fix: handle `Initial` parameters in `MTKParameters` constructor
AayushSabharwal Feb 5, 2025
d19134f
fix: handle `Initial` parameters in `tunable_parameters`
AayushSabharwal Feb 5, 2025
6531c87
fix: handle `Initial` parameters in code generation
AayushSabharwal Feb 5, 2025
f5cce1d
test: fix usage of incorrect `MTKParameters` object in `generate_cust…
AayushSabharwal Feb 5, 2025
c8f488d
test: refactor parameter dependency tests
AayushSabharwal Feb 5, 2025
1fc8c5f
refactor: rename `build_operating_point` to `build_operating_point!`
AayushSabharwal Feb 5, 2025
2ec306b
HACK: fix: add `maketerm` for `Initial` operator
AayushSabharwal Feb 7, 2025
ecaa585
fix: filter existing `Initial`s in `add_initialization_parameters`
AayushSabharwal Feb 7, 2025
5150659
fix: handle `Initial` parameters in `flatten`
AayushSabharwal Feb 7, 2025
50bfcdd
refactor: use problem building utils in `MTKParameters` constructor
AayushSabharwal Feb 7, 2025
5432f51
fix: fix scalarization of array defaults in `structural_simplify`
AayushSabharwal Feb 7, 2025
49a8ccf
test: fix tests taking into account new initialization parameters
AayushSabharwal Feb 7, 2025
be89d29
test: account for additional initialization parameter defaults
AayushSabharwal Feb 7, 2025
8ca484f
test: account for additional initialization parameters
AayushSabharwal Feb 7, 2025
ec3d3a8
refactor: don't create new parameters in `generate_initializesystem`
AayushSabharwal Feb 7, 2025
4cef878
fix: better handling of extra symbolics and splitting in `build_opera…
AayushSabharwal Feb 7, 2025
91f4bae
refactor: remove `substitute_extra_variables!`
AayushSabharwal Feb 7, 2025
7f0342d
fix: handle `Initial` parameters in `process_SciMLProblem`
AayushSabharwal Feb 7, 2025
8b9728a
fix: handle `Initial` parameters in `get_u0_p`
AayushSabharwal Feb 7, 2025
6273543
fix: fix infinite recursion in `full_equations`
AayushSabharwal Feb 7, 2025
7c457e5
fix: fix modelingtoolkitize
AayushSabharwal Feb 7, 2025
9d88d5f
fix: fix handling of array unknowns in `add_initialization_parameters`
AayushSabharwal Feb 7, 2025
14b28d4
fix: respect `validate` kward in `_remake_buffer`
AayushSabharwal Feb 7, 2025
7b5f29d
fix: handle constant initial conditions of derivatives of unknowns
AayushSabharwal Feb 7, 2025
8142b76
feat: implement `is_time_dependent` for `PDESystem`
AayushSabharwal Feb 10, 2025
2d9194d
fix: retain parameter dependencies in `build_operating_point!`
AayushSabharwal Feb 10, 2025
cecb15c
feat: export `Initial`
AayushSabharwal Feb 10, 2025
33e844b
refactor: make `symbol_to_symbolic` a global utility function
AayushSabharwal Feb 10, 2025
b42075a
feat: implement `SciMLBase.late_binding_update_u0_p` to update `Initi…
AayushSabharwal Feb 10, 2025
9d7e413
fix: provide history function to `reconstruct_fn` in `remake_initiali…
AayushSabharwal Feb 10, 2025
fe329d0
test: fix unsatisfiable initialization
AayushSabharwal Feb 10, 2025
f5cdf21
test: account for additional initialization parameters
AayushSabharwal Feb 10, 2025
525324a
fix: don't copy variables to `Initial` parameters in `ReconstructInit…
AayushSabharwal Feb 10, 2025
b0749b6
feat: add `Initial` parameters for real-valued parameter dependencies
AayushSabharwal Feb 11, 2025
35ef0ab
test: update tests to consider `Initial` parameters of pdeps
AayushSabharwal Feb 11, 2025
bfae1d2
refactor: move linearization-related code to new `linearization.jl` file
AayushSabharwal Feb 11, 2025
382ce9b
refactor: use new `LinearizationFunction` instead of closure in `line…
AayushSabharwal Feb 11, 2025
f0590ae
feat: add `LinearizationProblem`
AayushSabharwal Feb 11, 2025
0f5c008
feat: add `LinearizationProblem` constructor, `getindex` and `.ps` sy…
AayushSabharwal Feb 11, 2025
f9c328f
feat: export `LinearizationProblem`
AayushSabharwal Feb 11, 2025
494a7e7
docs: add docstrings for linearization
AayushSabharwal Feb 11, 2025
4eee373
fix: don't use `defaults_and_guesses` for operating point
AayushSabharwal Feb 11, 2025
ce0533f
fix: handle case when `u0 === nothing` in `linearization_function`
AayushSabharwal Feb 11, 2025
e16cf4d
fix: fix initialization of `linearize` testset
AayushSabharwal Feb 11, 2025
f34a4e0
test: minimally test `LinearizationProblem`
AayushSabharwal Feb 11, 2025
7793cb3
fix: handle `nothing` values in `InitializationProblem`
AayushSabharwal Feb 11, 2025
2a2b8b3
fix: handle `nothing` values in `linearize`
AayushSabharwal Feb 11, 2025
db2ae7b
test: fix initialization of `linearization_dd` testset
AayushSabharwal Feb 11, 2025
f0f1da9
fix: disable discovering defaults from metadata in `flatten(::ODESyst…
AayushSabharwal Feb 12, 2025
648956d
test: fix inversemodel testset
AayushSabharwal Feb 12, 2025
370343b
update pendcart test
baggepinnen Feb 12, 2025
9ed0fe2
test: fix initialization of some tests
AayushSabharwal Feb 12, 2025
b664e1c
fix: unhack observed in `process_SciMLProblem`
AayushSabharwal Feb 12, 2025
c73b0a2
test: test mutation of `Initial` parameters
AayushSabharwal Feb 12, 2025
4724540
test: test Issue#3342
AayushSabharwal Feb 12, 2025
ca1f389
test: test initialization for unsimplified systems
AayushSabharwal Feb 12, 2025
6f38a13
fix: check `hasname` for parameters in `propertynames`
AayushSabharwal Feb 12, 2025
158f3f8
build: bump SciMLBase compat
AayushSabharwal Feb 12, 2025
c12374f
docs: add documentation for `Initial` parameters
AayushSabharwal Feb 12, 2025
5d190f6
fix: better handle overrides for differentiated variables in `remake`
AayushSabharwal Feb 12, 2025
7baf788
docs: import `DynamicQuantities` in doc example requiring it
AayushSabharwal Feb 13, 2025
dfa4db8
docs: update remake doc page to account for extra tunables
AayushSabharwal Feb 13, 2025
c5b9fe0
fix: handle `state_priority` of wrapped variables
AayushSabharwal Feb 13, 2025
1e9d28f
test: add guess for FMI test
AayushSabharwal Feb 13, 2025
6bc28a8
fix: handle non-standard array indexes in `parameter_index`
AayushSabharwal Feb 13, 2025
3e5c436
fix: better handle scalarized array parameters with unscalarized defa…
AayushSabharwal Feb 13, 2025
85f9880
test: move `generate_custom_function` tests to `InterfaceII`
AayushSabharwal Feb 13, 2025
471e77c
test: rename `generate_custom_function` testset to `code_generation`
AayushSabharwal Feb 13, 2025
a50081d
fix: handle non-standard array indexes in codegen
AayushSabharwal Feb 13, 2025
4d9552c
test: test code generation for non-standard array indexes
AayushSabharwal Feb 13, 2025
c78e9f0
refactor: remove dead code
AayushSabharwal Feb 13, 2025
2748f7f
docs: add documentation for `Initial` operator
AayushSabharwal Feb 13, 2025
10250a2
docs: add PreallocationTools.jl to docs environment
AayushSabharwal Feb 13, 2025
a25aab4
test: increase tolerances of FMI CS test
AayushSabharwal Feb 14, 2025
7767fb5
fix: handle unsized array symbolics in `get_possibly_array_fallback_s…
AayushSabharwal Feb 14, 2025
0b72737
fix: search for required parameter dependencies in observed equations
AayushSabharwal Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ RecursiveArrayTools = "3.26"
Reexport = "0.2, 1"
RuntimeGeneratedFunctions = "0.5.9"
SCCNonlinearSolve = "1.0.0"
SciMLBase = "2.72.1"
SciMLBase = "2.73"
SciMLStructures = "1.0"
Serialization = "1"
Setfield = "0.7, 0.8, 1"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
Expand Down
1 change: 1 addition & 0 deletions docs/src/basics/Variable_metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ state_priority(important_dof)
Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions.

```@example metadata
using DynamicQuantities
@variables speed [unit = u"m/s"]
hasunit(speed)
```
Expand Down
55 changes: 19 additions & 36 deletions docs/src/examples/remake.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ parameters to optimize.

```@example Remake
using SymbolicIndexingInterface: parameter_values, state_values
using SciMLStructures: Tunable, replace, replace!
using SciMLStructures: Tunable, canonicalize, replace, replace!
using PreallocationTools

function loss(x, p)
odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables
ps = parameter_values(odeprob) # obtain the parameter object from the problem
ps = replace(Tunable(), ps, x) # create a copy with the values passed to the loss function
diffcache = p[5]
# get an appropriately typed preallocated buffer to store the `x` values in
buffer = get_tmp(diffcache, x)
# copy the current values to this buffer
copyto!(buffer, canonicalize(Tunable(), ps)[1])
# create a copy of the parameter object with the buffer
ps = replace(Tunable(), ps, buffer)
# set the updated values in the parameter object
setter = p[4]
setter(ps, x)
# remake the problem, passing in our new parameter object
newprob = remake(odeprob; p = ps)
timesteps = p[2]
Expand Down Expand Up @@ -81,49 +91,22 @@ We can perform the optimization as below:
```@example Remake
using Optimization
using OptimizationOptimJL
using SymbolicIndexingInterface

# manually create an OptimizationFunction to ensure usage of `ForwardDiff`, which will
# require changing the types of parameters from `Float64` to `ForwardDiff.Dual`
optfn = OptimizationFunction(loss, Optimization.AutoForwardDiff())
# function to set the parameters we are optimizing
setter = setp(odeprob, [α, β, γ, δ])
# `DiffCache` to avoid allocations
diffcache = DiffCache(canonicalize(Tunable(), parameter_values(odeprob))[1])
# parameter object is a tuple, to store differently typed objects together
optprob = OptimizationProblem(
optfn, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4))
optfn, rand(4), (odeprob, timesteps, data, setter, diffcache),
lb = 0.1zeros(4), ub = 3ones(4))
sol = solve(optprob, BFGS())
```

To identify which values correspond to which parameters, we can `replace!` them into the
`ODEProblem`:

```@example Remake
replace!(Tunable(), parameter_values(odeprob), sol.u)
odeprob.ps[[α, β, γ, δ]]
```

`replace!` operates in-place, so the values being replaced must be of the same type as those
stored in the parameter object, or convertible to that type. For demonstration purposes, we
can construct a loss function that uses `replace!`, and calculate gradients using
`AutoFiniteDiff` rather than `AutoForwardDiff`.

```@example Remake
function loss2(x, p)
odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables
newprob = remake(odeprob) # copy the problem with `remake`
# update the parameter values in-place
replace!(Tunable(), parameter_values(newprob), x)
timesteps = p[2]
sol = solve(newprob, AutoTsit5(Rosenbrock23()); saveat = timesteps)
truth = p[3]
data = Array(sol)
return sum((truth .- data) .^ 2) / length(truth)
end

# use finite-differencing to calculate derivatives
optfn2 = OptimizationFunction(loss2, Optimization.AutoFiniteDiff())
optprob2 = OptimizationProblem(
optfn2, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4))
sol = solve(optprob2, BFGS())
```

# Re-creating the problem

There are multiple ways to re-create a problem with new state/parameter values. We will go
Expand Down
70 changes: 70 additions & 0 deletions docs/src/tutorials/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,73 @@ long enough you will see that `λ = 0` is required for this equation, but since
problem constructor. Additionally, any warning about not being fully determined can
be suppressed via passing `warn_initialize_determined = false`.

## Constant constraints in initialization

Consider the pendulum system again:

```@repl init
equations(pend)
observed(pend)
```

Suppose we want to solve the same system with multiple different initial
y-velocities from a given position.

```@example init
prob = ODEProblem(
pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1, x => 1])
sol1 = solve(prob, Rodas5P())
```

```@example init
sol1[D(y), 1]
```

Repeatedly re-creating the `ODEProblem` with different values of `D(y)` and `x` or
repeatedly calling `remake` is slow. Instead, for any `variable => constant` constraint
in the `ODEProblem` initialization (whether provided to the `ODEProblem` constructor or
a default value) we can update the `constant` value. ModelingToolkit refers to these
values using the `Initial` operator. For example:

```@example init
prob.ps[[Initial(x), Initial(D(y))]]
```

To solve with a different starting y-velocity, we can simply do

```@example init
prob.ps[Initial(D(y))] = -0.1
sol2 = solve(prob, Rodas5P())
```

```@example init
sol2[D(y), 1]
```

Note that this _only_ applies for constant constraints for the current ODEProblem.
For example, `D(x)` does not have a constant constraint - it is solved for by
initialization. Thus, mutating `Initial(D(x))` does not have any effect:

```@repl init
sol2[D(x), 1]
prob.ps[Initial(D(x))] = 1.0
sol3 = solve(prob, Rodas5P())
sol3[D(x), 1]
```

To enforce this constraint, we would have to `remake` the problem (or construct a new one).

```@repl init
prob2 = remake(prob; u0 = [y => 0.0, D(x) => 0.0, x => nothing, D(y) => nothing]);
sol4 = solve(prob2, Rodas5P())
sol4[D(x), 1]
```

Note the need to provide `x => nothing, D(y) => nothing` to override the previously
provided initial conditions. Since `remake` is a partial update, the constraints provided
to it are merged with the ones already present in the problem. Existing constraints can be
removed by providing a value of `nothing`.

## Initialization of parameters

Parameters may also be treated as unknowns in the initialization system. Doing so works
Expand Down Expand Up @@ -231,6 +298,9 @@ constraints provided to it. The new values will be combined with the original
variable-value mapping provided to `ODEProblem` and used to construct the initialization
problem.

The variable on the left hand side of all parameter dependencies also has an `Initial`
variant, which is used if a constant constraint is provided for the variable.

### Parameter initialization by example

Consider the following system, where the sum of two unknowns is a constant parameter
Expand Down
9 changes: 6 additions & 3 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using Compat
using AbstractTrees
using DiffEqBase, SciMLBase, ForwardDiff
using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap, TimeDomain,
PeriodicClock, Clock, SolverStepClock, Continuous
PeriodicClock, Clock, SolverStepClock, Continuous, OverrideInit, NoInit
using Distributed
import JuliaFormatter
using MLStyle
Expand All @@ -56,6 +56,7 @@ using RecursiveArrayTools
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix
import BlockArrays: BlockArray, BlockedArray, Block, blocksize, blocksizes, blockpush!,
undef_blocks, blocks
using OffsetArrays: Origin
import CommonSolve
import EnumX

Expand Down Expand Up @@ -152,6 +153,7 @@ include("systems/imperative_affect.jl")
include("systems/callbacks.jl")
include("systems/codegen_utils.jl")
include("systems/problem_utils.jl")
include("linearization.jl")

include("systems/nonlinear/nonlinearsystem.jl")
include("systems/nonlinear/homotopy_continuation.jl")
Expand Down Expand Up @@ -258,7 +260,8 @@ export Term, Sym
export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope
export independent_variable, equations, controls, observed, full_equations
export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy
export structural_simplify, expand_connections, linearize, linearization_function
export structural_simplify, expand_connections, linearize, linearization_function,
LinearizationProblem

export calculate_jacobian, generate_jacobian, generate_function, generate_custom_function
export calculate_control_jacobian, generate_control_jacobian
Expand All @@ -278,7 +281,7 @@ export toexpr, get_variables
export simplify, substitute
export build_function
export modelingtoolkitize
export generate_initializesystem
export generate_initializesystem, Initial

export alg_equations, diff_equations, has_alg_equations, has_diff_equations
export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs
Expand Down
11 changes: 5 additions & 6 deletions src/inputoutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu
sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...)

dvs = unknowns(sys)
ps = parameters(sys)
ps = parameters(sys; initial_parameters = true)
ps = setdiff(ps, inputs)
if disturbance_inputs !== nothing
# remove from inputs since we do not want them as actual inputs to the dynamics
Expand All @@ -234,16 +234,14 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu
[eq.rhs for eq in eqs]

# TODO: add an optional check on the ordering of observed equations
u = map(x -> time_varying_as_func(value(x), sys), dvs)
p = map(x -> time_varying_as_func(value(x), sys), ps)
p = reorder_parameters(sys, p)
p = reorder_parameters(sys, ps)
t = get_iv(sys)

# pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys)
if disturbance_argument
args = (u, inputs, p..., t, disturbance_inputs)
args = (dvs, inputs, p..., t, disturbance_inputs)
else
args = (u, inputs, p..., t)
args = (dvs, inputs, p..., t)
end
if implicit_dae
ddvs = map(Differential(get_iv(sys)), dvs)
Expand All @@ -252,6 +250,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu
f = build_function_wrapper(sys, rhss, args...; p_start = 3 + implicit_dae,
p_end = length(p) + 2 + implicit_dae)
f = eval_or_rgf.(f; eval_expression, eval_module)
ps = setdiff(parameters(sys), inputs, disturbance_inputs)
(; f, dvs, ps, io_sys = sys)
end

Expand Down
Loading
Loading