-
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
MCP-0027 Units of Literal Constants #2127
Comments
Comment by fcasella on 14 Dec 2016 09:49 UTC Conclusions
Make sure it works with
|
Comment by fcasella on 19 May 2017 14:12 UTC |
Comment by stefanv on 20 May 2017 07:43 UTC
After writing the above, I'm not sure this even needs to be built-in, because I think the above might already be valid Modelica. Then, an expression like the one above could be written as |
Comment by hansolsson on 22 May 2017 10:22 UTC
I agree that this seems like a better solution and might not need anything built-in. However, it might be that it is clearer to write A somewhat related issue is to construct units for blocks (#921) - think of building this expression with block-diagrams with units. Previously I thought that using unit-names directly for that was more problematic than using the SIunits-types; but if units are more intuitive for equations it might be that they are more intuitive there as well and it is more a matter of making that even more convenient. |
Comment by stefanv on 22 May 2017 14:30 UTC Also, making use of |
Comment by cbuerger on 22 May 2017 15:12 UTC My rational is, that an expression-based approach requires special checks that are in their nature syntactical but must be realised and specifified on a semantic level. Consider the following examples; are they valid and if not what are the exact restrictions?:
|
Comment by fcasella on 22 May 2017 16:04 UTC I guess what is currently in the MCP i.e., adding constants with units to Modelica.SIUnits and using them in expressions, implicitly addresses these concerns, using syntax and semantics which are already defined in the specification as of today. When defining the constants, we should try to follow authoritative recommendations such as this, or better ones if you know them. That is outside the scope of the MCP. |
Comment by stefanv on 22 May 2017 17:07 UTC
No special checks are required at all, beyond any unit checking already done by the code. For example, my proposed function |
Comment by stefanv on 22 May 2017 17:10 UTC
And as I have pointed out, what I suggest also does, and doesn't require the introduction of any new symbols (except for |
Comment by anonymous on 23 May 2017 11:38 UTC
Given that Remember, that we do not enforce units everywhere, but just check that if units are given the equation system is well-typed (to which end we automatically derive units). That is why I am cautious with introducing a built-in function that just returns a unit; we have no such thing as an value-less expression with just a unit in the language so far. Using a two argument built-in function that combines value and unit makes it more explicit and less fragile to unexpected unit derivations and value computation combinations. |
Comment by cbuerger on 23 May 2017 11:40 UTC |
Comment by stefanv on 23 May 2017 12:13 UTC
The semantics of
and also identical to writing
And with my proposal, we still won't.
I disagree. It is semantically equivalent, but harder to read. I'm not sure what you mean by "unexpected unit derivations". Doing unit arithmetic is a well understood problem. In the 1970s, there even existed a slide rule that could do this (i.e., it worked purely with units, not numbers). |
Comment by fcasella on 23 May 2017 12:34 UTC
According to the text of the MCP, the constant 2 will have unit "1" and Unit("ohm") will have unit ohm, so a unit checker should report that this expression is dimensionally inconsistent. Whether this is a warning or an error is a tool issue, and may also depend on the settings. |
Comment by hansolsson on 23 May 2017 13:11 UTC
That recommendation is a good guide, and not primarily about defining constants - but about how to use units in general - and I agree that we should follow it if possible. Note in particular that In particular recommendation 12 says: It seems that if we have something like |
Comment by fcasella on 23 May 2017 13:41 UTC
Yeah, that's what I meant :)
I think this is no big deal. In mathematical notation you would write v = sqrt(2gh) with no explicit multiplications, but in programming languages you always have to put the {*} sign explicitly. As I see it, 2 kg means 2 times a kilogram, and I don't see a problem if we have to make the "times" explicit. Anyway, let me remind everyone that in its current form, this MCP is not really about how you write literal constants with units. To the contrary, it only specifies when a literal constant is actually a nondimensional number, so as to avoid bogus unit checking when such constants are involved in expressions and may end up being used as "unit slack variables". I would recommend that we don't mix this issue up with how we can write 4 kg in a Modelica expression, which is also probably bad modelling style, as I'd define a constant with the proper unit to store that numerical value, and then use that instead of the literal constant, rather than hard-wire it in an expression. The suggestion about defining and using unit constants is in the non-normative part and I can actually remove it if it causes controversy. That won't change the proposal significantly. |
Comment by hansolsson on 23 May 2017 14:48 UTC
The guides and recommendation make it clear that it is multiplication, but that the normal multiplication sign is not used. Recommendation 15 says (NIST guide 7.2): Recommendation 5 says: Note that they use "x" for multiplication of values, e.g. 1 Ci = 3.7 x 1010 Bq - i.e. not the same symbol used when multiplying units (see NIST guide 10.5.4 - https://www.nist.gov/pml/nist-guide-si-chapter-10-more-printing-and-using-symbols-and-numbers-scientific-and-technical#1054). Note that they also make a minor difference between how "2" is written as part of "2 kg" and as a general number (10.5.1). The main part here is that they don't have "kg" free-floating in expressions, it is always "2 kg" or similarly. BTW - division is allowed on the other hand so both of the following are ok: However, the proposal will not unit-check the one with division as I understand it. |
Comment by hansolsson on 24 Oct 2017 12:20 UTC E.g. if foo takes an argument of type Temperature then foo(293.15) is ok; we might view the call of the function as "assigning" to the input - but I believe it deserves its own item. Assuming that they have unit "1" works in some cases, e.g. |
I think you may have read too much into the presence of unit inference in #3257. In that proposal, unit inference is only allowed where there's one and only one obvious unit candidate for the inference. Hence, I don't see that any of the concerns you raise regarding unit inference would apply to my proposal. Further, #3257 doesn't involve any "dimensional inference"; the very limited unit inference is considered sufficient. |
When this happens, isn't the solution to modify some of the equations by multiplication of a 1 with unit |
Another paper and some relevant tables (same author as the previous material on angles): I will say it feels more natural having the denominator of rad for torque. It is a little bothersome to me that torque has units of work. Rather in this framework it's the more intuitive work per unit rotation (or angle). In the same sense that for a linear force it's work per unit distance. Sort of an interesting topic, but not clear if the juice is worth the squeeze here. |
The problem is that it is basically cheating - as soon as you allow formulas to have unitRad in them you have the lost the benefit of unit-checking - and you get things like "centrifugal force" having unit="N.rad" as if it were different from other forces. |
A minor note on the centrifugal force result as pictured in the table is that the 2022 paper actually specifically calls out and corrects that error. The table is primarily derived from a rule of thumb, but that rule of thumb apparently doesn't apply to that particular case. Another comment I would give is that the proposed/implied changes from the discussed literature seem to have two different flavors:
|
To me treating "rad" as sort of "1" avoids adding those odd factors, and still helps. You will be unable to "deduce units" for unit-conversion - but Modelica doesn't have automatic unit-conversions in models. More importantly I don't see that it will necessarily reduce the number of problems so significantly that it is worth the effort - as the rad-factors and similar unit-factors can also be applied incorrectly; as seen in modelica/ModelicaStandardLibrary#4040 At least this can wait until we have the other unit-checking things in place. |
Going back to Francesco's proposal I see that it importantly states expressions only containing numbers are fine, and not only expressions that consists of just one number. That's important and Dymola's (not officially released (*) ) current unrelaxed unit-checking doesn't allow that (it opts for the second alternative) and thus e.g.,
I'm not saying that it is the best way of writing such parameter bindings, but I can understand that we allow it - while still detecting the problem in *: Obviously, we plan on improving it before releasing it. |
It was also my impression some time ago that such strict rules were desired, as it would allow every inferred unit to be attached to a particular literal in a formula. However, the discussion in #2127 made it clear to me that it was generally considered too restrictive, and #3257 has been designed with the more relaxed approach in mind. I'm not sure what is the desired result here, to me the
Edit: I suppose a unit-correct way of expressing the declaration equation would require the use of an auxiliary constant to which the missing unit can be attached, for example:
|
Ah, sorry - I was tired and saw it as 11 not l1; so probably Dymola did the right thing. |
See PR #3266 with my concrete proposal for amending the Specification text. |
From the various comments here and in the rejected #3266, I had compiled this list of examples/test cases for desired unit checking behaviour involving literals: https://gist.github.com/bilderbuchi/317bc1a2e039f477462a61f5826260ea#file-mcp-0027-test-examples-md |
Thanks @bilderbuchi, it is definitely good for me. |
One comment regarding the ones with |
I absolutely agree with that. But I understand this may also be a subject of debate. I once added |
I am confused: Aren't |
Yes, and no. One of the underlying issues with the unit-system is that I know there are people that claim that they can differentiate between them, but those tricks normally makes everything more confusing. |
D'accord and I would be really interested in any reason to not let |
As a side note, I've been participating to Modelica Design Meetings for 20 years, and I sometimes thought our discussions had something in common with the scholarly debates of medieval theologians about how many angels would fit on a pin. Though I was impressed by the progress we made over the years, contrary to the motto that "a language should be designed by a person, not by a committee". At least, Modelica came out way better than Ada 😃 . I guess the discussion on units is particularly fit for this scenario 😅 |
@gwr69 on this topic please also check my comment above, specifically on the difference between unit checking and dimensional consistency checking. My feeling is that dimensional consistency is what we should try to check, and from that point of view "rad" or "1" make no difference, because they are both dimensionless. Proper unit checking and unit inference are a lot more demanding, and I have a strong feeling that they require some understanding of the equations, as the examples in my comment above demonstrate. Maybe we should eventually have our models checked by LLMs? 😅 |
Issues that we need to consider and decide on (the list may be incomplete):
|
To discuss:
|
|
Reported by fcasella on 11 Dec 2016 19:03 UTC
I have added a proposal for the clarification of the units of literal constants, which is currently unspecified in Modelica 3.3r1. I hope there's still time to have it in Modelica 3.4, the proposal is very simple and the evaluation process should be straightforward.
Documents can be accessed directly here: Overview and Specification Changes.
Migrated-From: https://trac.modelica.org/Modelica/ticket/2127
The text was updated successfully, but these errors were encountered: