SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 204 forks source link

`build_explicit_observed_function` no longer handles inputs correctly #2794

Closed baggepinnen closed 3 months ago

baggepinnen commented 3 months ago

When sys has been processed by io_preprocessing and inputs are passed to build_explicit_observed_function, these inputs are sorted out of the parameter vector by the code

    if inputs !== nothing
        ps = setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list
    end

However, the following code does not account for this

reorder_parameters(get_index_cache(sys), ps)

so the generated code still expects a parameter vector that is the length of length(ps) + length(inputs) rather than just length(ps). This causes the generated code to be incorrect, accessing parameter values at the wrong indices.

I tried

reorder_parameters(get_index_cache(sys), ps, drop_missing=true)

which makes the parameter array in the generated code of the correct length, but the order is still wrong. @AayushSabharwal I don't understand what reorder_parameters is supposed to do, can you have a look?

It appears possible for us to work around the issue by using split=false to structural_simplify

CC @shobhitvoleti

AayushSabharwal commented 3 months ago

Is structural_simplify called with the inputs specified, or are these inputs only specified to build_explicit_observed_function?

baggepinnen commented 3 months ago

Yeah, we call

(f_oop, f_ip), dvs, psym, io_sys = ModelingToolkit.generate_control_function(car_model, inputs, split=false)
f_obs = ModelingToolkit.build_explicit_observed_function(io_sys, outputs; inputs = inputs)

where generate_control_function internally calls structural_simplify with inputs specified