-
-
Notifications
You must be signed in to change notification settings - Fork 217
Code generation contains Differential
when equations contain variable time parameter
#3480
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
Comments
One problem here is that if 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 Furthermore, if you intend for |
Making parameter derivatives zero should handle this? |
We do need to make sure that when a parameter is a function, say a datainterpolation, that we still handle that case correctly. |
"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. |
Well the issue here is that discrete variables and parameter functions need to do this separately. |
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. |
See also #2823 (from #2823 (comment) and below). A good solution to this issue would probably also solve that issue. |
We do need to handle parameter derivatives in the explicit case at least.
Isn't that the point of it being discrete-time, though? The callback handles the instantaneous change in value, and the assumption of the derivative being zero holds at all other time points. Is there a case where it would give an incorrect result? At the very least, I think we can allow explicitly specifying parameter derivatives and erroring when they're required but not provided. |
It hides a likely model error, MTK is not supposed to have to differentiate such a discrete-time parameter. Take the model in the OP as an example, Change the domain from mechanical to electrical or any other and you get the same problem, MTK should error if you try to impulsively disturb a variable in any other way than to explicitly change the variable in a callback. Note, it's |
Interesting. So time-dependent parameter derivatives should error unless the user explicitly provides a derivative for them? |
If the derivative is of a parameter that changes discretely, then yes, that's my opinion at least. In the FMU case, the output is probably a continuous variable with a well-defined time derivative, and it's our problem that we call that a parameter 😅 The modelica crowd has a number of recent papers on how to do the right thing with Dirac impulses, but I don't think any tool implements this research yet. |
Well yes and no. CoSimulation FMUs by nature are meant to be handled via callbacks, and their outputs/states are discrete variables. Discrete variables are stored in the parameter struct. If a model uses the output of such an FMU in an equation that MTK differentiates, then this is still valid. The FMU allows obtaining the output derivative, we just don't have a way to hook into it yet. The question then arises what to do if the FMU doesn't support calculating a high enough order derivative. Error? |
This is missing context. It's like saying the derivative of the PDE equation cannot be directly calculated because there's a boundary condition which the derivative equation may not satisfy. The extra context is the domain. The definition of the
There are mathematically reasonable equations to be considered for this though. I mean, you can have a pendulum where the length
You can change L only in callbacks and that's precisely the case where you need
Yes, Cosimulation FMUs are clocked, so they have discrete inputs and outputs. https://www.claytex.com/tech-blog/fmi-basics-co-simulation-vs-model-exchange/ makes this very clear with a nice image. A nice way to do this would be to add metadeta for dummy derivative substitutes, which when not supplied is 0. Then when going through the index reduction process, you check if this is supplied, if so you put that expression in (which may just be a different output of an FMU for example), and if not it's 0. Even then the derivative substitution to zero is not incorrect, as within the mathematical with a finite dt clocked FMU it's correct that at any place where
Yes, when that comes up that's definitely an error. |
Take the following
gives...
Note the
Differential(t)(k(t)))
which cannot be computed.Manifest:
The text was updated successfully, but these errors were encountered: