-
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
Unit-aware conversion of unit #3300
base: master
Are you sure you want to change the base?
Conversation
@bilderbuchi, I couldn't add you a a reviewer, but this might be of interest to you as well. |
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.
I don't see this as a good idea as it seems a way towards mixing units in models. That is not our previous intention, and it will be really problematic for units with offsets like Fahrenheit.
What are the problems you anticipate w.r.t. offset units? |
In particular knowing when they apply - as the difference between delta and non-delta units isn't part of the unit in Modelica, and isn't really part of the SI system either.
I would say this is different as changes in the unit-system will have to be analyzed in greater detail than the introduction of a new one so that we don't break existing things. E.g., it seems pint has limitations on adding temperatures (due to the possibility of delta), but adding two values and dividing by two is a common operation for computing an average and it works for temperatures as well. Taking the conversion operator without considering that seems dangerous. |
I agree, hence my comment
I suspect a design discussion would be good to have, to settle the design before a PR is made.
Maybe the way FMI does it with a
I'm not sure what the "really" qualifier implies for you, but the International Temperature Scale of 1990 (ITS-90), available from BIPM, at least makes explicit reference to temperature differences: "A difference in temperature may be expressed in kelvin or degrees Celsius." If all you have available are unit strings, I'm not sure a flexible treatment is possible. I think this will boil down to a question of types, not units, in the end (or leveraging
AIUI, the limitations are there to avoid ambiguities, whose resolution might be clear to a practitioner from context (or not), but are not clear on a programming language level. For your example, how to resolve the addition of two Celsius temperatures, or dividing a Celsius temperature by 2 is clear in the context of a "mean", but how should the computer know? |
In any case, this is a gnarly problem to tackle (this was also not an easy process in the Pint project, from what I saw), and I think that personally I'll concentrate the little time I have on getting #3266 finished. |
I believe that is historically based on how it is done in Modelica; so it's not part of the unit-string - but a separate annotation. That actually makes sense based on how it is currently used: The implied conversion will depend on whether the value is relative or absolute and even though you can easily change displayUnit you cannot easily change if it is relative in those dialogs. That separation means that it just works!
True, I should have been clearer that even if the concept exists there isn't a standard way to specify it. |
I agree with @HansOlsson
I totally agree with @HansOlsson. @henrikt-ma you write
I think that to the contrary the role of these functions is precisely the opposite, i.e., to make the unit conversion explicit, typically in the context of signal processing. E.g. you get Celsius readings from a data file, which is not Modelica so it doesn't have use SI, and you use to_K to convert it to SI units in a block diagram. Not when writing physical equations, you should not use those functions in there. Your proposal instead hides some unit conversion magic behind the scene. To be honest, I find it a tad too dangerous. I'm fine at having units for documentation and dimensional consistency checking. Adding more semantics to them, that actually changes the results of the models by introducing automatic unit conversions behind the scene, doesn't seem wise to me. |
Modelica has the A key safety measure that tools must obviously take when implementing a unit conversion operator such as |
I wouldn't be surprised if there is Modelica code out there that is performing additions that are invalid if applied to quantities in units with offset. Similar to other unit errors that we detect as our unit checkers improve, I think that such errors should be fixed instead of preventing development of the specification. For the particular problem of computing a mean, it could be interesting to consider a built-in
|
This sounds like a matter of modeling practice that the specification shouldn't interfere with, but I didn't say you have to hide the conversions. The top comment shows how to construct a block that applies the conversion:
This PR is not about automatically applying unit conversions. This is about explicit conversion that relies on the sanity of the unit handling systems which we already use both for unit checking and for handling display units. Before considering automatic conversions we'd need to build even more trust in these systems, and this I believe we can only do put putting them to work. |
The only units with offsets I am aware of are degC (degree Celsius), barg (bar-gauge, i.e. relative to atmospheric pressure) and psig (ditto in US customary units). These are not SI units, so they shouldn't be used in writing Modelica code in the first place. Using these units for computations can lead to very fishy results and subtle bugs, so we should discourage their use, rather than encourage it by trying to support it with sophisticated automatic conversion features. In the 21st century, people should just use SI units. At least for engineering use, of course I'm fine with km/h speed limits on the roads or using liters for milk cartons in the supermarket 😄 I understand the consensus of the Modelica community is that the only tolerated use of of such non-SI unites is for pre- and post-processing of input and output data, e.g. in displayUnits. I fully agree with this and I don't think there is any need to make changes. |
I can't say I feel that consensus in my surroundings. It's a fact that some external data exists in other units, and to not have a unit system for handling that safely is a language design failure in my opinion, not a problem with the external data. |
Surely you mean "SI base units", here? AFAIK, degrees Celsius were not removed from the list of SI derived units? |
It's complicated. The SI standard includes degree Celsius, but primarily as an alternative for temperature differences (when it is exactly the same as kelvin); so just because you see degree Celsius doesn't mean you can use it for the intended purpose. For a temperature value it is stated that it isn't coherent, but I'm not sure how legal that is according to BIPM (as the section is about coherent units). |
I was in fact referring to the consensus about 10 years ago, when these decisions were taken. Hard to say now, since we've not been meeting for a long time in person 😭
This is one of those things that should be discussed in a larger assembly, not just in a small MAP-LANG working group.
This is a different requirement than being able to handle unit conversion automatically anywhere. I would say that what you need for this are blocks/functions that perform unit conversion from/to custom units to/from SI units. Maybe this is best handled using libraries that provide such block functions, and include them in the MSL, rather than embedding fancy unit handling in the language, which has a much broader (and possibly controversial) impact. I don't see any problem with a conversion block that is totally generic, and makes the conversion based on the unit attributes of inputs and outputs. Or maybe, even more simply, on unit strings declared for inputs and outputs as String parameters. |
@henrikt-ma, BTW, some years ago we had a discussion in the MA about trying to split the MLS into a core part and some "libraries" or "packages" specified more formally by some kind of code. For example, the stream connector specification is quite tricky, it would have been nice if we could have handled that with some kind of code or meta-code. As we did with the Clocked semantics, which is formally specified in terms of Modelia code. This was to avoid bloating the specification (which is already over 300 pages) in favour of code that can be executed automatically, rather than interpreted by tool developers, and managed in a more straightforward and flexible way than textual plain English specification. I guess we should try to go in this direction for the purpose you stated above. |
Yes. This is essential to be able to write physical equations without the need of any conversion factors, which are always tricky. The idea is to push conversion factors to pre- and post-processing as much as we can, and do the core modelling in SI base units only, so that one need not bother with them in the first place in that context. Which AFAIK is exactly what is done in every sensible industrial engineering context, including US-based ones. |
In a setting where custom user symbols are present this functionality needs tool-specific implementation, and I don't want to end up with another |
What I mean is, the unit conversion rules will be programmed in some Modelica blocks/functions that should be included in the MSL. Not just the interface, like ModelicaServices. Customizations could be handled by inheritance. We could do this entirely using Modelica, there is no need to make it a language feature. |
I'm afraid you are mistaking context lacking proper tool support for unit handling with sensible contexts. That context might be the Matlab community, or the Simulink community ten years ago. One could also say that in a context without proper tool support, it is a sensible decision by the community to try to mitigate the shortcomings of the tools. To be clear, I don't expect Matlab's solution based on the Symbolic Math Toolbox to be integrated well enough in the language to count as proper tool support: https://se.mathworks.com/help/symbolic/units-of-measurement.html Simulink, on the other hand (based on what I find by a quick search), may not have had proper unit support ten years ago, but appears to have it now: https://se.mathworks.com/help/simulink/ug/units-in-simulink.html In the Wolfram world, we are proud to deliver strong support for unit handling. To us, even adding quantities expressed in different units is a perfectly fine thing to do, thanks to the safety of the underlying unit system:
Similarly, the famous
Neither Maple has any problem mixing units, see https://www.maplesoft.com/support/help/maple/view.aspx?path=units Given the current limitations of Modelica, I can see that it is a sensible design decision for the MSL to agree on which base units to use, but this mustn't be mistaken for a wise decision about the development of the Modelica language. It seems like a very odd idea to defend that Modelica should be the only remaining technology where it isn't even possible to get unit conversion by explicitly asking for it. |
Such a solution will obviously not be able to take advantage of the powerful unit handling systems we already have in tools, and that the tools will always need to have regardless of which functions and blocks for conversion that the MSL provides. A unit system can automatically handle endless combinations of conversions that would be madness to try to cover with error-prone hand-coded conversion functions. Both madness to maintain, and madness for the users that need to search for the name of the block that converts from |
@henrikt-ma thanks for providing this context information, I've not been using Simulink for ages, since I'm fine using Modelica for my job 😄. I based my statement on what a former PhD colleague of mine working in the R&D of a large US industrial conglomerate told me, about 10 years ago. I should probably ask him the same question, it could be that the availability of symbolic tools with strong unit handling made using a hodgepodge of units in large engineering projects a desirable thing. I personally doubt it, but I could be mistaken. I guess I should ask him again. Regarding what Simulink does, I agree that having such a block available could be useful for the purpose of input/output data handling. I'll comment on that separately. Regarding what symbolic manipulation tools like Mathematica or Maple do, I take note that you can do stuff like model Scary
Real L1(unit = "m") = 1;
Real L2(unit = "mm") = 2;
Real L3(unit = "mm");
Real L4;
equation
L3 = L1 + L2;
L4 = L1 + L2;
end Scary; where L3 gets a value of 1002 and L4 gets a value of ? What I mean is, for 25 years the semantics of L3 = L1 + L2 in Modelica has been very clear: the numerical value of L3 is bound to be the sum of the numerical values of L1 and L2. Units were there for the purpose of documentation, display, and to allow dimensional consistency checks, in which case L1 + L2 would give an error because you can't sum two numbers with different units. What you are apparently proposing (also in other places, e.g. 2127 and links therein) is to change the semantics of the language, giving unit information first-class citizenship. I would be very much in favour of that if we were designing a new language. The problem I see is that if we do that, 25+ years of legacy Modelica code, that were written without this first-class citizenship, may become invalid, or broke, in the worst possible way, i.e., because of unexpected (and originally unintended, when the code was written) automatic conversions. I find this perspective very scary indeed. The bottom line is that we should have a debate about this issue in the larger MA context, involving people that are responsible for actual industrial usage, and hear what they say. This is not something that should be resolved in a conversation between us two 😅.
I agree, as long as the unit conversion is explicit, not as in the |
I could agree to this, provided that the MLS defines the content of those unit strings much more explicitly than it does now, particularly regarding non-SI units. With the contents of the current Section 19, the likelyhood that two different tools end up doing different things seems to me pretty high. |
Actually not a big deal, if you ask me. All I require is that A more elementary example of this mechanism would be something like this:
or the slightly more advanced:
In these examples, it seems rather fundamental that the unit handling system keeps track of the units of
To summarize the above, what scares me is not that the unit system figures out the units of variables that haven't been annotated with a Maybe it didn't go without saying, so I clarified the proposed text to make it clear that However, what is essential to the safety is that the tool understands the crucial difference between expressions with empty unit and expressions with undefined unit. Compare binding equations: Expressions with empty unit can safely be used to initialize a parameter with a concrete
As it would probably be too restrictive and backfire if a tool would reject any model where not all expression units are well-defined, the tool is more or less forced to at most warn about the problem with the binding equation of If expressed with
|
Yes, one day we'll need to talk about the radian, and that "the compatible FMI standard" has chosen to treat it as a base unit: https://fmi-standard.org/docs/3.0/#_physical_units Apart from that, I would expect tools to generally implement this part in consistent ways. I believe that the much bigger problem with unit checking differences between tools has to do with the amount of unit inference and propagation being performed, the sophistication of function call unit checking, unit handling in arrays, etc, all of which are problems outside the scope of this PR. |
In the ongoing intense work on unit handling driven by #3255, the need for a unit-aware conversion between units has come up on several occasions. Applications include:
Modelica.Units.Conversions.to_degF
(explained below).Functions like
Modelica.Units.Conversions.to_degF
would become unnecessary except as documented and annotated wrappers around the built-in operator. It is more interesting to see how a unit conversion block could be defined:Compare this to the need to introduce a specific block that calls the
to_degF
function, where the function's implementation is a complete mess with regards to units:(Changing the function so that the result is computed via unit
"1"
would be possible, but it would end up being a very complicated way of expressing something that the unit system should already know how to do.)