-
Notifications
You must be signed in to change notification settings - Fork 44
Unitful literals #3688
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
base: master
Are you sure you want to change the base?
Unitful literals #3688
Conversation
The ease of use and readability provided by this proposal is important for the acceptance of this as a correct way to address the unit error in a situation like this:
This PR makes no changes to unit checking; the following should be rejected as inconsistent:
|
The latter isn't even close to beginning to meaningfully adress the actual problems this would introduce in practice. |
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.
To avoid merging by mistake:
As repeatedly indicated this makes it easy to introduce non-coherent units, which requires a detailed analysis.
It is hard to think of a programming language where units and unit checking matter more than in Modelica. It is just silly that the language is lacking this basic construct of constructing unitful literals. Applications are everywhere:
Example of the day:
Due to the lack of clarity and the wildcard effect, the error above is easy to make, and the model developer who knows that the model only works for acute angles might fail to check that the assertion actually works. If the developer would have used a unitful literal instead, a unit inconsistency would have revealed the problem:
When corrected, the attached unit adds clarity and removes any uncertainty related to the wildcard effect:
If the modeler decides to use |
Let's not pretend that we can't perform a decent amount of unit checking, and let's not assume that the feature would be massively misused to exploit any gaps we still have in the unit checking semantics. The alternative, using numeric literals without unit and relying on the wildcard effect is so much worse than the tiny risk of introducing a unitful literal with non-coherent unit in a place where it wouldn't be caught by unit checking. |
The original design was to have equations in coherent units; and have non-coherent units in the user interface at the boundary. Apart from that the previously raised issues with temperatures, and rotations are still unresolved. |
I think we need external input to make progress here. @casella, @AHaumer, @GallLeo, @hubertus65, would you be interested in joining us for a session about the pros and cons of being able to express unitful literals in expressions and equations? |
I don't see it as meaningful to involve more people when there are still no answers regarding how possibly non-coherent units in equations would actually work, especially for temperatures and rotations. |
It's also misleading to tell others that the issue is 'unitful literals' when the problem is that it's suddenly easy to add possibly non-coherent units like I have still not seen anything meaningful addressing those issues, or how it would work in practice. Just to be clear: the proposal isn't to allow |
After a long discussion at the MAP-Lib meeting in Lucern (see the minutes, it is agreed that this is not controversial and orthogonal to all other proposals/issues regarding units and unit checking, so we should go ahead with that. The only thing that needs to be checked is if this somehow conflicts with existing syntax in SimulationX that has different meaning. @gkurzbach, @TorstenBlochwitz can you please comment on that? |
I have to contradict my previous statement, sorry. I found another potential issue after writing #3744. In Chapter 19 of the latest MLS draft on master, it is stated that:
I think that, for consistency, we should then restrict unitful literals to only use SI-units (including the listed 7 non-SI units that are allowed in the SI system), as we do with unit attributes. @henrikt-ma do you agree? Also, should we add the three example cases listed in the minutes of our meeting as non-normative text for further clarification? |
-Yes, the same sort of recommendation makes sense for unitful literals. I'll add it.- Edit: The current recommendation already applies to unitful literals, since these don't go under |
Personally I would have preferred to not include a bigger example like that for a "simple" construct like this, but considering the wild discussions i Rotkreutz I realize that it might be unrealistic to think that the average specification reader would be annoyed by such a "trivial" example. I'll add the example, and then we can take the discussion from there. |
When making the extended example I realized that it is difficult to tell the story without making any assumptions about how unit consistency is defined in Modelica. For me, this is a stronger reason than the triviality to not include the example. @casella, what do you think about the extended example? |
I like the proposal (saw it first at the conference in your talk) and the extended example (in my book, more illustrative examples are typically better than fewer)! |
@casella are the minutes going to be published or summarized so that non-MA members can learn what has been decided? At least the unit-related parts would be interesting, to learn where MCP0027-related efforts stand now. |
Based on the discussion in Lucern it seems this is the preferred syntax, and that the preferred semantics is that it is automatically converted to suitable units (normally base units). The current proposal intends to have a more restrictive semantics, that may possibly work. Background We might delay conversions (and/or restrict them - e.g., to parameter bindings with unit) provided that:
I have some doubts whether completely delaying it will actually work as tools will naturally attempt to support conversions, in some way. (Those ways should then converge.) In order to simplify the discussion I find it useful to only consider unitful literals in parameter bindings as other cases can be reduced to that, e.g. the example code
I'm not saying that we should restrict the unitful literals to these cases, merely that this is useful for understanding the general issues as any issue for unitful literals in expressions can be reproduced using only unitful literals in parameter bindings. It also shows that automatically converting unitful literals in general isn't more complicated than only converting them in parameter bindings. Specifically considering parameter bindings show a number of issues related to the following areas:
( Regarding conversion: if models only had used SI-base-units one could have automatically added a conversion to SI-base-units. However, since that is not the case one needs to check whether that is the case first, and that check seems as complicated as inferring the actual unit. ) Proposed semantics
The key part is that goal isn't to handle everything, but instead that many problematic constructs should generate errors (or warnings for the last case). This is important, as users (and tools) can currently ignore/disable unit checking and inference (or at least ignore the diagnostics), but once we start considering unit conversion one cannot unit checking - as unit conversion and ignored unit checking will give different results for the same model (and unit conversion for models with unit errors will be a mess.) The implication is that:
would be legal, whereas
would currently both be in-valid (they require conversion), and
would not be valid as we couldn't infer the unit for x (so it could be MMixing2 or MValid). Additionally, this implies:
Or in summary: we can safely extend both models and the language. ( An interesting consequence is that some ambiguous unitful literals could be disambiguated by the inferred unit (so "T" could mean Tesla or Tonne depending on the unit; and similarly for the different pounds), but I don't think that's a good idea. ) |
Let's not allow anything involving conversion to creep into the scope of this PR. It is crucial that this PR is only about the syntax, so that we don't need to make any assumptions about the rules for unit checking which are still under development. Then, once we have rules for unit checking, it will be a much better time to talk about implicitly converting these literals to units that don't lead to unit inconsistency. If we want some sort of conversion before we have a definition of unit consistency, I think the conversion needs to be explicit, like so:
Here, tools can figure out what (While |
As suggested by Hans. Co-authored-by: Hans Olsson <HansOlsson@users.noreply.github.com>
I didn't add the semantics of unit conversion, I only made the semantic restriction that you are only able to use the construct if you can infer the unit (and the unit of the unitful literal is compatible with that) fully clear. As far as I understand SimulationX users can write Making it an error for the time being also works, my main concern is that it must be clear; and I'm very worried that trying to clarify the text (including error cases) is met with push-back. If we don't specify any semantics some could follow the normal idea that units can be ignored (or disable unit checking) and think that it means the same as The current proposal sort of suggests that the model should be invalid due to unit inconsistency, which requires that the unit is inferred. The specification shouldn't merely suggest semantics for the value of parameters (that so significantly impact the result), and thus I proposed to explicitly add it to the proposal - and in particular clarify that if the unit cannot be inferred it is an error; so that tools cannot say "we don't infer units so it's obviously valid". (Even if the conversion-check is the hard part, and the actual conversion is the easy part.) That unclarity for what a model means (is the value of (If we didn't allow Importantly, it is easy to determine the unit in this case (it's part of the type for the variable) and it would be silly to ignore it, but as I understand the intended goal is to allow them in places where the unit inference is less straightforward - with the same problems for the unit and which value to use or whether it is invalid. Therefore, if we want to just add the syntax without problems we could say that it is currently only allowed in simple bindings for variables with unit and it is then automatically converted. Alternatively, we could state that they are only allowed in those cases and it is in an error if the units would require a conversion - but as previously stated the actual conversion is the easy part. As far as I understand that possibility:
|
Using a unitful literal makes the intention clear: | ||
\begin{lstlisting}[language=modelica] | ||
y = x + 2'cm'; | ||
\end{lstlisting} |
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.
\end{lstlisting} | |
\end{lstlisting} | |
Tools must infer the unit information of the unitful literal, including absolueValue when applicable, from the rest of the model, and verify that the given unit is compatible. | |
It is an error if the needed unit information cannot be inferred. | |
Diagnostics may be given for consistent units that shouldn't be mixed (like \lstinline!"N.m"! and \lstinline!"J"!). | |
Special care is needed to handle rotational frequencies. |
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.
To make it clearer that it is just adding lines to clarify, and that the following lines are unchanged.
As far as I recall from previous discussion it does cause a conflict, and I would be surprised if SimulationX spent time to introduce the syntax so that users could write |
Yes, this notation does not lead to any conflict and we use it mainly for conversation. |
To be concrete consider:
Either we agree that Ok is ok while Bad, and Ambiguous should be errors, and NeedConversion is also an error (unless we add conversions) - and then I see no harm in clarifying that. Or we are adding something without knowing what it means. |
I'm happy to see that |
Because we have no unit checking in SimulationX, the numbers with units in quotes are always converted to the SI base unit, so 1'cm' is equal to 0.01'm' but the unit is dropped then, so 0.01 remains. If there would be unit checking, the unit could be kept and checked for consistency with the inferred unit. Further, if the units are not the same, but both have the same SI base unit, the result could be converted to the inferred unit. So, if the inferred unit would be 'mm', 0.01'm' would be converted to 10 'mm' and the unit then could be dropped, and 10 remains. |
Ok, one solution that is very easy to implement - and allows the use of Do you also assume that temperatures are absolute temperatures so that
Yes, that would be the expected result with unit checking and non-base units (in addition to detecting |
Yes, we do. Instead, we introduced new unit symbols for temperature differences in addition to |
This is the design for attaching units to literals that was discussed at the most language design meeting in Linköping held in March this year. The proposal is an alternative to #3668, with emphasis on simple design, ease of use, and human readability.
This design was originally implemented in SimulationX, and later given two parallel implementations in System Modeler - one for testing it in regular Modelica expressions, and one for (undocumented) use in vendor-specific annotations.