-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add variability change for these functions. #3610
base: master
Are you sure you want to change the base?
Conversation
So, an Integer output will be discrete-time, but a Real output still non-discrete time (assuming the inputs are non-discrete time).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would only have a chance of working if GenerateEvents = true
was applied recursively also to all function calls in the function body. We need to avoid loopholes like this:
model DiscreteValuedFunctions
function f
input Real u;
output Integer y = integer(u + 0.5); /* Non-discrete-time Integer output. */
annotation(Inline = false); // Avoid SystemModeler issue.
end f;
function g
input Real u;
output Integer y = if u < 2 then 0 else f(u);
annotation(GenerateEvents = true);
end g;
Real a = g(time); // Fine, result with discontinuity at time 2.0
//Integer b = g(time); // Bad: b is assigned a non-discrete-time solution
annotation(experiment(StopTime = 5.0, Interval = 0.1));
end DiscreteValuedFunctions;
Edit: Improved example.
A good point, and it's possible that such a recursive solution would work, but I would rather propose to rethink this in another way that gives a similar result in this case. A function with The problem with applying it recursively is that it must be done with care.
Naively generating events would imply that
Totally agree. |
Co-authored-by: Henrik Tidefelt <henrikt@wolfram.com>
I have now added some text for that. We could add an example showing how this requires that some called functions also use the annotation. |
\item | ||
The functions \lstinline!pre!, \lstinline!edge!, and \lstinline!change! result in discrete-time expressions. | ||
\item | ||
Expressions in functions behave as though they were discrete-time expressions. | ||
Expressions in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}), behave as though they were discrete-time expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need more specification of variability inside functions that do have GenerateEvents = true
? We don't want all expressions inside such function bodies to get non-discrete-time variability, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a start…
Expressions in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}), behave as though they were discrete-time expressions. | |
The variability of expressions inside functions depends upon the \lstinline!GenerateEvents! annotation (\Cref{modelica:GenerateEvents}). | |
With \lstinline!GenerateEvents = true!, expression variability is determined according to the same rules as outside of functions. | |
Otherwise, all expressions have at most discrete-time variability (some expressions, such as function inputs and literals have lower variability). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was the intent of the text I wrote (ignoring the misplaced trailing quotes).
The overall structure of that secition is that it specifies when expressions have lower variability. Stating that functions having GenerateEvents=true use the same rule as non-function is thus not needed, and similarly there is no need to say "at most discrete-time" as the less variable cases should already be handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, makes sense. The formulation is a bit unusual, though, shouldn't we simply say:
Expressions in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}), behave as though they were discrete-time expressions. | |
Expressions in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}). |
This item would then look better before or after Expressions in a clocked discrete-time partition.
I also think there is a missing \cref{discrete-time-expressions}
link to this section from this item in functions.tex, and the item would need to be updated according to whatever semantics we come up with here:
Components of a function will inside the function behave as though they had discrete-time variability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "behave as though they were discrete-time expressions." have been present for quite some time on some of the entries. I don't want to change them as part of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And added the other reference.
I think we'll have to rethink the variabilities of function inputs when It would be great if setting
It would seem unfortunate if the following would be a variability error inside the function:
So the question is whether it would be possible to utilize the expression variability of arguments passed to the function, instead of only using the worst-case variability for the type of the input. |
I actually believe there is a natural and elegant solution to the problem with
That is, declared variability of function inputs. It would primarily be useful for functions with Some details:
|
I see the point, and agree to an extent - and will return to that. However, it doesn't need discrete-time variability for I can agree that it is inefficient to generate an event for a literal (or parameter) input, but I don't see that it is an error. It is also a bit confusing that the outputs from functions have two variabilities:
But that isn't a new problem, and I don't see that it generates problems. In practice the usual implementation of GenerateEvents=true is to simply inline the function (in contrast to the complicated inlining for other functions that need to handle events), and after that the expression variability of the arguments will be used to avoid generating events. |
Sorry, my example was bad. What I meant was rather something like this:
Again, the elegant solution to this would be the following:
|
To me the function
I have a bit of problem seeing the use-case for this; having a function generating events and then declaring that the inputs are discrete mean that function will not generate events. And remember that the simple implementation of GenerateEvents is to inline it, so the case of a function having multiple real inputs - some discrete and some non-discrete - seems quite esoteric. |
The point would be to have rules for variability checking that allow for separate checking of the function body and the expressions where the function is called. This is something we (System Modeler) rely heavily upon when Separate variability analysis of the function body also makes the function a much better abstraction, and allows errors to be reported in a simpler way than when the error is a joint effect of the body of the function and the arguments passed. |
I agree that such a separation is a good thing, but I don't see why it is necessary to declare some inputs as discrete for that. |
…omplicated, with link.
Based on discussion in modelica/ModelicaStandardLibrary#4495
I created it as a PR in order to have something concrete to discuss.
It is a draft PR since there are a number of issues to resolve (in addition to the obvious issues of an annotation having such an impact):
The reason it is a separate item is to make it easier to discuss.
I have slightly changed my mind on these items and will update the PR