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? |
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.