-
Notifications
You must be signed in to change notification settings - Fork 42
Giving unit to literals #3668
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
Comments
I am strongly against the idea of using the types derived from
The use of Regarding Vectorization will not be a problem once we get to the operator forms in the future. In the meantime, there are still the two options of using a one-off constant array or to add the unit to each of the array elements in case the array is small. By the way, how many examples have we encountered so far where this is needed? |
Those are some of the reasons why it is a good idea, as it expresses the underlying intent which isn't tied to the unit. I hadn't realized that when I wrote it, but now it becomes much clearer. One reason is that quantities have understandable (and familiar) names, and that's why they are used in Modelica - and similarly the current work-arounds are normally called unitTime, unitCurrent, etc. I simply don't see a problem with someone using Enthalpy in a formula to indicate that it is enthalpy and not something else that happen to have the same unit (even if only the unit is used for checking) - but I do see the benefits in terms of understandability. More fundamentally the title of this issue says "Giving units to literals" - whereas it in the SI-standard is introduced as "Defining the unit of a quantity"; and the definition of base units are preceded by the corresponding quantities. That shift in perspective is important - one shouldn't think of starting with 1.6 in a formula and slap a unit on it, but start with a quantity - that has a numeric value and a unit.
Yes, tools can spend the effort of one simple way of finding them; that can be used both for declarations and for these cases; whereas we otherwise need one way of finding the type when declaring parameter with a certain quantity and another for finding units when defining the unit of a quantity inside equations. |
I agree in the sense that slapping units to Who really cares if the
|
I would say it is Work, but I view objecting to Energy there as a bit of nitpicking. The difference is that Work is the Energy transferred, which goes together with a time-period (and Enthalpy is quite different). However, I see two major drawbacks with 'J':
I view both as blockers on their own, and and none of this is currently needed. There are some literals in libraries that need to be converted to quantities with unit to have better unit-checking. |
It is not nice, but still so good that it has support both in SimulationX and System Modeler. Perhaps you would prefer
There is no ambiguity here:
To think about automatic conversions is making this a lot more complicated than it should be. |
Whereas we have support in Dymola for:
That will just bring chaos and disappointed users. In Modelica we have for decades used displayUnit to write And more fundamentally I don't see that as needed to solve the actual issue of improving unit-checking of equations, as equations should normally be in base-SI-units whereas non-base-unit normally only appear in the external interface (as parameters and for plots). |
The point is that reasoning about the kind of quantity is not possible in the way we can do it for units. The following looks stupid to me, but not possible to reject based on any general principle that I am aware of:
Hence, what you can see here is only the inapplicable information that the author had torque in mind, while you cannot see the important part, being the unit of |
You are mixing things up. The introduction of a syntax for attaching a unit to a literal is not replacing the old way of editing parameter values in a chosen display unit. This mechanism works great for setting parameter values where the parameter is properly declared with a type that comes with Literals with unit should be used deeper down in expressions and equations, where the unit is not immediately available from the context.
No, since tools will guide the users to using the decades old mechanism when editing these declaration equations. |
So, you are claiming that the syntax, that solves all of the actual problems without introducing any, would be bad because people might write bad code that we cannot currently detect? Added: And similarly we wouldn't detect an error for |
No. I'm staying consistent - since I care about the entire user experience. I don't want two different ways of writing 5 kJ with wildly different semantics (especially not to handle a corner case that we don't see a need for), and I don't want two different ways of introducing literals with unit - when one will do. I'm trying to solve actual problems - where MSL have a few equations with real values that are some quantity with unit where the unit is currently omitted (and similarly in other libraries); and I want an actual solution. This proposal shows that:
|
Sticking to SI base units doesn't remove the need for attaching units to literals:
|
No. I'm saying that it solves the wrong problem. It solves the problem of being able to express the kind of quantity for a sub-expression, but doesn't solve the problem of clearly attaching a certain unit to the literal. |
And the proposal solves that either as:
or as:
No new syntax, no new keywords even though in this case I would just write:
(Even if |
For me, the proposals are not a big contradiction. One could allow both spellings. For simple units, such as |
I actually find it very helpful that quoted identifiers are a rare thing in Modelica models, so that |
Can we actually be serious? The reasonable expectation of a user writing In Dymola that means that for The key for enabling that is that we know the underlying unit (in this case Joule) and the conversion, as that is consistent with the design that variables are (primarily) in base-units and display unit is for displaying (and entering) values, and unrecognized display units aren't an issue. (If we don't consider those constraints storing it as This is especially important for cases where the display unit itself is not enough: If we now introduce Additionally, when we consider giving values to quantities in expressions the expectation is also that we should consider it more carefully. One of the classic examples was Modelica.Mechanics.MultiBody.Examples.Loops.Utilities.GasForce2 with:
|
That depends. In some cases (like the parameter dialog) one can bypass the lexical analysis and thus don't care whether it is an IDENT or not; so |
Good question, The question is whether min/max are useful - |
Btw., as I learned in the good old East German school, °C was always an absolute unit, temperature differences were always expressed in K.
Therefore the quotes. Inside them you are free to define a new grammar dealing with units. It is even possible to determine the base unit from derived units and to handle non SI units then. |
BIPM (who defines the SI-standard https://www.bipm.org/en/publications/si-brochure ) disagrees and says: "A difference or interval of temperature may be expressed in kelvins or in degrees Celsius, the numerical value of the temperature difference being the same in either case.... I'm a bit unsure if there has been a relevant change (they dropped the °K for K in 1967/1968), or the school got it wrong, but anyway that's the current status. Added:
Coherence was one of the design goals of the SI-system, and that allows writing equations without conversion factors as long as you stay with the base and coherent derived units (like Newton and Joule). Thus my goal here is to unleash the power of the SI-system by using coherent units to have equations without conversion factors (as much as possible); but still allow using non-coherent units for input/output. Added: Historical note - based on my understanding: |
In addition to this new function(s) we (as previously discussed) also need a way to disable unit-checking in specific equations. Introducing this in Modelica gives something like https://github.com/HansOlsson/Modelica/tree/UnitTest (based master and cherry-picking modelica/ModelicaStandardLibrary#4377 modelica/ModelicaStandardLibrary#4591 modelica/ModelicaStandardLibrary#4398 ). That shows that this is a working proposal for libraries; in combination with extended unit-checking. The conclusions are:
Note that there are still some remaining issues in the library; such as modelica/ModelicaStandardLibrary#4241 but they require more domain knowledge to correct. (oh, and it's fairly trivial to implement - we already have a similar syntax for record constructors etc) |
In case some others missed the coherent part: The alternative proposal was that Modifying that so that BTW: I also re-read the original design for units when going through the tutorial document; I believe that is also relevant for others; see #3672 . |
For what it is worth I have to say that I think the short-hand form is fantastic as it is very explicit in what unit is used without affecting readability negatively. It actually enhances the readability as the unit is provided. An expression involving something like |
That may happen if it was needed (a lot), but in practice using this proposal looks like this: https://github.com/HansOlsson/Modelica/tree/UnitTest
In particular people should avoid For Or basically Modelica has so far worked by saying that in order to have a parameter with "value" Tools have then made sure that it is presented in a good way; giving the combined benefit of safety (in terms of units), understandability (for users), and low entry point for tools. And to me bad readability (when there's no tool support) is less of a problem than making it easy to have non-coherent units inside equations giving a real risk of errors; especially as you are not really required to use the parts that you view as having bad readability. |
I'm afraid I do not understand your coherence arguments. An equation involving a literal number with an explicit unit instead of just literal number (without an explicit unit) seems better in every possible way.
|
The coherence is a major part of the design of the SI-system. The coherence means that the equations are the same for the values as for the quantities - if you go back to previous systems (like imperial or the various cgs-systems (*)) the problem was that extra factors sort of randomly appeared. I remember one of my physics courses also had all equation in a non-coherent system - I hope to never see that again. The unit system in Modelica was designed based on that coherence; that's why values are in the unit, but there's a displayUnit for input/output. I agree that just having 5e3 for The idea with sticking to the SI-system as a coherent system is that people don't have to wonder about the unit; so when you see Or basically this proposal solves the problem in a few needed cases without introducing any new syntax, and without making it easy to make errors. Considering that tools have not fully supported unit-checking I don't think it is a good idea to introduce something where it is so easy to make errors, and then rely on tools to catch those errors. As previously stated: having And please look at the attached branch to see that this proposal actually works, in the rare cases it is helpful. *: cgs stands for centimeter, gram, seconds. But the problem with the cgs-systems are the units for electricity. |
It would not cause major problems. Don't worry about coherence – we have unit checking which will detect mistakes. Only worry about the clarity and simplicity of expressions where a unit needs to be attached to a literal. |
Please:
|
As part of unit-checking #3491 various ways of attaching units to Reals are proposed including withUnit(...) and 1 'm'. After additional internal discussion we have found a simpler proposal that voids most of the pitfalls.
The simple proposal is that Real (and any type derived from Real) can be called as follows:
Benefits:
parameter Real p(unit="m", displayUnit="mm")=0.001;
byparameter Real p=Real(0.001, unit="m", displayUnit="mm");
- with the same semantics, in particular we make it easy to avoid non-base-units in equations.Including types derived from Real would allow something like the following in the IdealDiode:
The text was updated successfully, but these errors were encountered: