-
Notifications
You must be signed in to change notification settings - Fork 41
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
when initial() with impure function? #3499
Comments
It is not desired, but in this specific case it isn't that problematic.
|
It thus seems that the solution for this issue is to specify that the if the function is impure it should only be called once, and that it is an error if the call is part of a system of equations. |
Forbidding impure functions in the initial equations is a hard restriction. Especially if you need to initialize external objects outside the constructor, e.g. when implementing an FMI interface. Would it be possible to specify that an external function might be called multiple times if it is called in when initial() and it has to cope with it? |
I would consider the following possibilities:
As noted in #3498 similar considerations apply to non-initial when. In both cases: I would prefer the "only once" interpretation, but I don't know if there are any issues with implementing it. (Clarified that "In both cases:") |
Do people agree with this idea? Both for loops with when-equations and impure functions call in when-initial. However, I noted something else that is unclear:
Consider the following simple model:
It deliberately uses |
SimulationX starts with i=3 and j=2. So a more detailed description seems to be necessary. For me the initialization consists of two steps:
At the second point the question is whether code inside |
Language group: oversight that we didn't consider all cases where impure functions can be parts of systems of equations. |
MapleSim returns i = 3 and j =3. |
After some thinking I have come to the following conclusion regarding how to specify the initialization (and a plan for implementing it ...). A. Solve initial equations (including In particular for when-equations it means that if we have:
then I'm aware that it is a bit weird since:
But I cannot see any other good solution. I rejected the following, since they will for e.g., Modelica.StateGraph lead to non-convergence:
The reason is that we often have However, I think we need to have a better explanation for this. At the start of step B we use A consequence for A consequence for In the rare case that the when-clause is active both during step A and start of step B it will seem as if there's only one update. This case is the part I'm the most unsure about, but I cannot see any good alternative - and it seems quite esoteric. To simplify things use As for the actual issue and the previous example:
Will start by printing "X" once, and setting |
For the record, System Modeler starts with |
This model gives
|
Note that it wasn't specifically about "true", but conditions that happen to evaluate to "true" initially. A common situation is when using Modelica.StateGraph (will see if I can construct some examples). |
Sure, the case of a |
After some additional thinking I think I have found a better solution (that I initially rejected). Step 1 - initialization. The thing that needs to be clarified is thus:
|
Even though I'm not sure we strictly speaking need to clarify this, I can't see that it would hurt doing it either.
Sounds good, but should probably be slightly generalized. I think we should also write the text more carefully to deal with the fact that linear systems typically only require a single evaluation.
I'd also like us to clarify this part:
to make it clear that we are talking about the equation systems of the integration problem here, as we did take care of the equation systems in initialization. |
E.g. the MSL sample Modelica.Blocks.Examples.Noise.ImpureGenerator uses Modelica.Blocks.Examples.Noise.Utilities.ImpureRandom which contains:
During initialization the when initial() equations are active, so the result may depend on the number of iterations needed to solve the initial system. Is this desired?
The text was updated successfully, but these errors were encountered: