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

Code generation contains Differential when equations contain variable time parameter #3480

Open
bradcarman opened this issue Mar 20, 2025 · 7 comments · May be fixed by #3493
Open

Code generation contains Differential when equations contain variable time parameter #3480

bradcarman opened this issue Mar 20, 2025 · 7 comments · May be fixed by #3493
Assignees
Labels
bug Something isn't working

Comments

@bradcarman
Copy link
Contributor

Take the following

@component function FilteredInput(; name, x0=0, T=0.1)
    params = @parameters begin
      k(t) = x0
      T = T
    end
    vars = @variables begin
      x(t) = k
      dx(t) = 0
      ddx(t)
    end
    systems = []
    eqs = [
      D(x) ~ dx
      D(dx) ~ ddx
      dx ~ (k - x)/T
    ]
    return ODESystem(eqs, t, vars, params; systems, name)
end

@mtkbuild sys = FilteredInput()
ModelingToolkit.observed(sys)

gives...

julia> ModelingToolkit.observed(sys)
3-element Vector{Equation}:
 dx(t) ~ (k(t) - x(t)) / T
 dxˍt(t) ~ (-dx(t) + Differential(t)(k(t))) / T
 ddx(t) ~ dxˍt(t)

Note the Differential(t)(k(t))) which cannot be computed.

Manifest:

[961ee093] ModelingToolkit v9.66.0
@bradcarman bradcarman added the bug Something isn't working label Mar 20, 2025
@baggepinnen
Copy link
Contributor

baggepinnen commented Mar 21, 2025

One problem here is that if k is a function of time, then the named variable ddx will depend on the time derivative of k, D(k), and there is no way for MTK to know what the derivative of this time-dependent parameter is. The problem goes away if you don't name ddx, and instead write the model as

julia> @component function FilteredInput(; name, x0=0, T=0.1)
           params = @parameters begin
             k(t) = x0
             T = T
           end
           vars = @variables begin
             x(t) = k
             dx(t) = 0
           end
           systems = []
           eqs = [
             D(x) ~ dx
             D(dx) ~ (k - x)/T
           ]
           return ODESystem(eqs, t, vars, params; systems, name)
       end

julia> @mtkbuild sys = FilteredInput()

julia> ModelingToolkit.observed(sys)
Equation[]

since then MTK has no reason to make ddx available as an observed, and the detivative of k never appears in any expression, it's only being integrated.

Furthermore, if you intend for k(t) to be piecewise constant, the derivative of k is infinite or ill defined at the time points when k changes.

@AayushSabharwal
Copy link
Member

Making parameter derivatives zero should handle this?

@ChrisRackauckas
Copy link
Member

We do need to make sure that when a parameter is a function, say a datainterpolation, that we still handle that case correctly.

@baggepinnen
Copy link
Contributor

Making parameter derivatives zero should handle this?

"handle"... if the derivative is zero then there's no need to have the parameter depend on time. If the parameter truly does vary with time it's wrong to just assume that the derivative is zero.

@ChrisRackauckas
Copy link
Member

Well the issue here is that discrete variables and parameter functions need to do this separately.

@baggepinnen
Copy link
Contributor

baggepinnen commented Mar 24, 2025

In both cases is it wrong to assume that the derivative is zero. A discrete-time variable that is piecewise constant has an ill-defined derivative (in the non-distribution sense at least) at the time point when it changes value, if the model is such that it depends on this derivative, the model is probably wrong. If the parameter is a function of time and MTK knows the derivative of this function it is fine, otherwise erroring is the only reasonable option.

@hersle
Copy link
Contributor

hersle commented Mar 25, 2025

See also #2823 (from #2823 (comment) and below). A good solution to this issue would probably also solve that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants