Avoid that Constants.pi gets the unit "rad"#4774
Avoid that Constants.pi gets the unit "rad"#4774henrikt-ma wants to merge 1 commit intomodelica:masterfrom
Conversation
The change for 'pi' avoids that it gets the unit "rad", and 'e' is updated to keep the two definitions consistent.
|
By the way, I am intentionally avoiding the question of what units to have on the interface of |
|
In my understand, "rad" is a pseudo unit as good as "1", just declaring "This is an angle, not a dimensionless ratio.". |
HansOlsson
left a comment
There was a problem hiding this comment.
I would argue that pi logically should have unit "rad", e.g., when you call sin(2*pi*f*time) it makes sense that f is in Hz and time in seconds, so that pi ensures that the argument has unit "rad".
Obviously there are cases where radians for pi are a bit confusing.
That's why it is important BIPM states that radians exist (for clarity), but it is expressed as having unit "1" in base units.
Additionally, for a user seeing these lines it doesn't seem clear that asin instead of Modelica.Math.asin has such an interpretation. I would even say that the built-in asin should behave the same as Modelica.Math.asin in this respect, so in that sense it seems like a non-change.
Yes, I'd phrase it as The point of this PR has to do with the sort of unit inconsistency you see here: If we want to use Applied to
Add to that the desire to easily use To make all this work nicely, we need When speaking about angles, I personally prefer saying:
not this:
Then over to @HansOlsson's argument:
Well, as long as |
Exactly. However, the exact handling of "rad" is a bit complicated, and it seems more discussion is needed. I see two ways of handling "rad" in unit-checks:
I don't see that this PR gives any benefit with either alternative. Additionally, it relies on the yet-to-be-specified semantics of the built-in function The problem I see with having Basically I see three possible units for pi:
and my preference is clearly for the first one, with the second one as the second best. |
In the future, they might be made the same simply by removing all units from the interface of One thing I find necessary that the language function supports is to take part in empty unit expressions, meaning that |
If we think about the other uses of |
|
Maybe some of the disagreement could be resolved by also having a constant for one revolution: Then, |
|
I'm firmly in the camp that @henrikt-ma I'm also aligned with your last comment or at least the intent of your comment regarding how we could connect pi and angles. And regarding this PR specifically: I don't have a problem wth it and would gladly approve if others see value, but I see it as a half measure. Instead, I think the unit for pi should be set back to 1. |
You can - but it could also indicate that something is missing, and to me unit checking is intended to catch such issues (you might still do it - but then with some extra work arounds to indicate that you know what you are doing). Consider an actual use case , Using Similarly for Modelica.Electrical.Analog.Examples.CauerLowPassAnalog that didn't/doesn't have |
Yes, and it is more than 2 years since #4191 was merged with that hope, and it's a pity that we haven't progressed further. Updated: missed negation. |
Let's try the obvious: You can have any power of However, the effect of setting unit |
Since then, I think we have developed a better idea for how units of constants should behave. The idea is that a constant with empty unit shall behave just like a numeric literal; there should be just as little "wild-card effect" when using the factor |
|
Just my 5 cent: This is a misleading formulation - I would avoid that. This is really a bad example, frequency is missing. it should read as: |
Your input is appreciated.
That won't work out of the box since regardless of what of the unit alternatives we pick for Anyway, I can see that So, That looks strange to me, but maybe it somehow makes sense to you? |
|
ok, I'll bite. So, looking at these recent examples, I want to rewind slightly to an upstream equation here that is connected to the equation that has been discussed. So for inductors we have But now, for sinusoidal current, we can derive the relationship for inductive reactive: Looking at the units, and if we take units for angle seriously, we have But wait, this apparently isn't dimensionally consistent anymore (Assuming we care about the angle units). What happened? Where did this Following the logic in this article: https://iopscience.iop.org/article/10.1088/1681-7575/ac7bc2 So we can see that we are dimensionally correct again (regardless of angle choice). To conclude, you can see that it's not Now, of course, every equation with angle units has a different story and way angle units are balanced out, but this is the story for this one. |
As noted above it is more that There's clearly one degree of freedom here, i.e,, as long as the product of L and C stays the same it doesn't matter for the frequency. Whether that freedom is just used for setting L, C, or the ratio between them just depends. If we use the square root of the ratio as the degree of freedom the formula is (unless I made some mistake): Nice symmetry - I would say. And as far as I understand we have understood that radians are useful in formulas (and pi sort of has that unit), but technically is the same as unit "1"; so we want to allow |
Thus, the relevant question is: is allowing that a good thing? Consider the following model: (Should likely rewrite in some more advanced way, and have a better unit for x and v.) After changing it to 1.0, one could skip the time-scale in both the equations and in the computation of Note that I deliberately introduced it as |
|
I'll repeat here what I already posted somewhere else in similar form, but can't find right now. Apologies for any duplicate content and for the long post. 1. Definition of piAccording to Wikipedia and to any mathematics textbook I know of, Unfortunately, this is far from settling the matter, see below. 2 The unit EntscheidungsproblemI may be mistaken, but I am afraid that with units in Modelica 3.8 we are repeating David Hilbert's mistake. Hilbert (one of the greatest mathematicians of all time, mind you) had a grand plan to completely formalize mathematics. In particular, he argued that the Entscheidungsproblem could be solved mechanically, by an algorithm. Until Alonso Church and Alan Turing proved this was not possible. I'm of course not even remotely as smart as Church or Turing, but I'd like to try doing the same in this context 😅 My fundamental argument is that, when considering physical equations, context matters. Unfortunately, context is not completely formalized: a Modelica compiler does not understand what equations actually mean and where they come from. This is a big deal. 2.1 An example problem, which formalizes requirements on dimensional consistency checkingConsider the following Modelica code. It contains some basic equations from mechanics, which are correct and thus also dimensionally consistent, as well as some equations which are not dimensionally consistent, so they are for sure wrong and ought to be rejected by a unit-conscious compiler. Some equations are redundant, but that's not an issue here, nor is the variability of the variables (constants vs. parameters vs. time-varying variables). The only thing we care about in this model is the dimensional consistency of the equations, not their solvability. model Examples
constant Real pi = 3.141592653589793;
Real D(unit = "m") "Diameter of the wheel";
Real p(unit = "m") "Perimeter of the wheel";
Real r(unit = "m") "Radius of the wheel";
Real phi(unit = "rad") "Rotation angle of the wheel";
Real phi_t(unit = "rad") "Target rotation angle of the wheel";
Real w(unit = "rad/s") "Angular velocity of the wheel";
Real T(unit = "s") "Period of rotation of the wheel";
Real f(unit = "Hz") "Frequency of rotation of the wheel";
Real L(unit = "m") "Distance travelled by the wheel";
Real v(unit = "m/s") "Translational velocity of the wheel";
Real J(unit = "kg.m2") "Moment of inertia of the wheel";
Real M(unit = "kg") "Mass of the wheel";
Real tau(unit = "N.m") "Torque applied to the wheel";
Real E(unit = "J") "Mechanical energy of the wheel";
Real P(unit = "W") "Power applied to the wheel";
Real pi;
equation
// Dimensionally consistent equations, should be accepted
der(phi) = omega; // (1)
der(phi) = 2*pi*f ; // (2)
der(L) = pi*D*f; // (3)
J*der(w) = tau; // (4)
der(E) = P; // (5)
p = pi*D; // (6)
p = 2*pi*R; // (7)
D = 2*r; // (8)
f = 1/T; // (9)
L = phi*r; // (10)
E = J*omega^2/2 + M*v^2/2; // (11)
P = omega*tau; // (12)
phi_t = pi; // (13)
// Dimensionally inconsistent equations, should be rejected
der(phi) = 2*pi*f^2; // (14)
p = pi*D^2; // (15)
p = 2*pi*R^2; // (16)
D = 2*r^2; // (17)
f = 1/T^2; // (18)
L = phi*r^2; // (19)
der(E) = omega; // (20)
end Examples;Whatever decision we take on the unit of I assume we can all agree about this requirement. If not, then I would suggest to discuss this matter separatly and settle it down first, because that's a pre-requisite for any further discussion. 2.2 What is the unit of
|
Good. One could add
Agreed, so the unit-checking has to sort of accept that "rad" and "1" can be mixed, but still try to differentiate between them when possible. |
|
I totally agree with @casella looking at his chapter 2.3, tht's what I meant. |
|
The problem of removing "rad" that I see for the usage in drives: |
No! This is what I made very clear above: Our design work for unit consistency has converged to a good design long time ago:
Yes, there are remaining topics in Modelica unit consistency which are still being debated, but I have a very clear picture of the handling of constants not being one of them. Regarding we have too much legacy to be able to reject so therefore (13) will also be accepted when I don't see what this has to do with the unit of However I just want to point out that, as @mestinso explained in #4774 (comment), this sort of relation may actually be missing a hidden factor with unit, and because of the widespread use of sloppy relations like this we end up being forced to make the radian an alias for the unit 1. Also remember that when comparing to other literature on units, there may be no distinction between a numeric value and a "value of quantity" with unit 1. In Modelica this distinction is important, since we have a long tradition relying on the so-called empty unit expressions being accepted where the unit 1 would be wrong. For example, Hence, adequate support for also working without units is important in Modelica, and forcing the user to just throw units away is not adequate support. Failure to stay in the unitless world should be rejected in the same way as unit inconsistency is rejected. If the MSL provides a I just can't see the big point of rejecting and forcing the user to use a literal instead: |
|
It seems there are two separate discussion points here:
In dymola i see the following: Why don't we have the same logic for pi? Give it unit="1" and the same way out if we want to set the length equal to pi? |
So far so good.
Here Dymola is not aligned with where the standardization is converging to; there must not be a "wild-card" effect when including a numeric literal as a factor. With its current behavior it I guess it is only rejecting (15), (18) and (19) on @casella's list. Maybe Dymola will be more aligned in its next version? By the way, with the unitful literals from modelica/ModelicaSpecification#3688 it will be easy to resolve the inconsistencies like so: Once we have unitful literals, it could be argued that this is bad style and should be rejected: just because there is a cleaner way to do it: However, since the variant with an empty unit expression (often a literal) is still today the only way of doing it in Modelica, the legacy is enormous and it is too late to get it "right". However, as long as the unit is immediately available from context, it isn't really a problem to allow an empty unit expression to implicitly be associated with the unit from the context. (It is the use of empty unit expressions where the unit is not immediately available from context that is the real problem, and here I don't see an enormous legacy that would prevent us from gradually phasing it out from Modelica.) This the reason why it is so important for the definition of unit consistency in Modelica that there are ways to work both in the physical domain of values of quantity (expressions with unit), and in the mathematical domain of real numbers (expressions with empty unit). Also, there must be a clear separation of the two so that there is no question whether an expression has empty unit allowing it to take on a unit from context or an expression with unit that must be used consistently. In Modelica, giving
It would be thanks to actually having unit I don't see a strong analogy between π and c. While π appears in a variety of purely mathematical relations on real numbers, c is used in physical relations involving values of quantity. π is not only the ratio between the circumference of a circle of radius 4m and its diameter, it is also the ratio between the real unit circle's circumference and radius. To support both uses in Modelica, it should behave as a numeric literal. |
|
@henrikt-ma To clarify, I understand that today pi doesn't have units on it, so the behavior is not comparable to the speed of light behavior. What I mean to say or am trying to imply is that I'd like the behavior to be analogous. So yes, I'd like to see this future state after we have the unitful literals features come online: And I'd like both of these to give a unit warning: ...and until that new unitful literals feature comes online, I'm ok with this being a sort of workaround: |
Just to clarify - that is with default settings (and after import If you set Note that setting that flag will trigger lots of diagnostics from MSL, and I would have hoped we could focus on actually improving them to help users - and to find potential errors. (There are some PRs to improve the situation even for the 3.6 version of the Modelica Language; like #4591 #4398 #4051 there are some marked requires37 and then for additional proposals we have something like https://github.com/HansOlsson/Modelica/tree/UnitTest2 - their might be more needed for pi - I haven't investigated that in detail) Obviously, there can be different opinions on whether The constant issue (in particular for package constants) is that they should only infer unit from their declaration not from their use, since something like "constant Real eps=2...e-16;" and then have Obviously some constants like
That would be good (or using "rad" as indicated before), but it requires actually working on improving MSL to be better at handling units. Simply put - pi should have unit "rad" or "1"; at least in the future. Now we need to work on improving MSL. |
(And in some cases in "rev/min".) Note that the SI-standard has another solution for this: "Hz" instead of "1/s".
So, if you have "rad/s" and "Hz" it is clear which is which, but when we infer units we often get "1/s" and don't know if it is one, the other, or something completely different (and don't use becquerel just because it is "1/s"). (We might have some over-use of "rad" in Dymola.) That's why we have: |
This is where we need the discussion to be focused, and we need two more variations on the theme: Ultimately, I think we must accept that the unit of Edit: I added one more variation where |
Agreed. I think the following reference lays it out the best: https://iopscience.iop.org/article/10.1088/1681-7575/ac7bc2/pdf The following screenshots demonstrate the basic idea for unit handling on trig functions.
Unitwise, the trig functions in modelica are the standard "rad" variety. So And pi itself is dimensionless/unit="1" |
Yes, they make it very clear that π is not an angle by itself, and I love the way they stress the importance of "complete functions". However, they do not pay much attention to "analytical angle" (introduction to section 4) and dimensionless functions (section 4.3.4), while the Modelica ecosystem comes with a legacy that require a much stronger recognition of these concepts.
The point is that in Modelica, we need to make a real distinction between dimensionless (empty unit) and Besides the weirdness of allowing an expression with empty unit to take on a unit given by the context, computations with empty units also have an important role to play for empirical relations that relate values of quantities when expressed in some particular choice of units, and if we are serious about supporting cyber-physical systems (section 1.1 in the specification) I think it has a value in itself to properly support computations on real numbers without any presence of units. It is in this context that I would like to have a constant for π which may be used in all those truly dimensionless computations where there should be no presence of units whatsoever. By the way, it is also nice to see the very clear notation they use for the transition between quantities and "coefficients" in section 2, which we are also able to express using the experimental operators in System Modeler. Take the angle |
|
Regarding the built-in trigonometric functions in Modelica, one could actually gain some unit safety by only supporting "analytical angle", corresponding to empty unit. This would avoid the need for somewhat complicated overloaded behavior, while functions such as (The use of The degree variant of With some intelligence in unit inference for function calls, one could even make a generic variant: This would actually be the safest variant of all, as |
Agreed.
Dimensionless for sure, but I think we need to consider "1" or "rad" this a bit more, and to the most pragmatic solution (after fixing unit issues in MSL like merging #4780 - and more) is to make That is consistent with all of the uses of But, that is not urgent as we first need to correct the units of MSL in general; and I still find it sad that we spend so much time on this - and so little time on actually fixing the real unit issus in MSL. |
I opened this because of a unit error in a library caused by |
At least with this way of understanding π in the paper – as something to which a unit can be attached – we get that π in the paper would correspond to an empty unit 3.14… in Modelica. So far so good. But to then say that the MSL should have a If we would only get those unitful literals in the language, and if we additionally get implicit unit conversion in some places where it is safe, then there will be no more need to write |


The current definition of
Modelica.Constants.pigives it the unit"rad", but based on the ongoing discussions about standardizing a definition of unit consistency in the Modelica specification, I find it clear thatpishould have "empty" unit, making the constant behave just like a numeric literal.For example, the following is expected to be allowed if
pihas empty unit, but not if it has unit"rad":With the unit
"rad", the workaround would be to insert a conversion factor,but I have a clear impression that our community wouldn't like to have to do it this way (even though it would be much more convenient once the factor can be given in-line as
1's/rad').In this PR, the change for
piavoids that it gets the unit"rad", andeis updated to keep the two definitions consistent.