Skip to content
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

Added "four-line" proposal for units of literals #3266

Open
wants to merge 24 commits into
base: MCP/0027
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7524b11
Initial commit of ReadMe.md for MCP-0027
henrikt-ma Oct 4, 2022
a762fc7
Added "four-line" proposal for units of literals
casella Oct 25, 2022
bdbea3b
Reformatted to use sentence-based line breaks. Added inline codes for…
casella Oct 26, 2022
eb116d7
Wrote out LHS and RHS
casella Oct 26, 2022
b676ad9
Outputs of transcendental functions should be dimensionless
casella Oct 26, 2022
11dc52d
Fixed type
casella Oct 27, 2022
9a1deda
Updated proposal
casella Oct 28, 2022
df6242b
Update chapters/lexicalstructure.tex
casella Nov 2, 2022
5d5f466
Update chapters/lexicalstructure.tex
casella Nov 2, 2022
398c013
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
7a9f340
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
77eaae7
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
8916f6e
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
2c817dd
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
942776f
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
b3e0dc1
Update chapters/lexicalstructure.tex
casella Nov 9, 2022
3370313
Update lexicalstructure.tex
casella Nov 10, 2022
7539f1b
Update lexicalstructure.tex
casella Nov 10, 2022
15bb554
Update chapters/lexicalstructure.tex
HansOlsson Nov 11, 2022
26bce83
Update chapters/lexicalstructure.tex
HansOlsson Nov 11, 2022
ba23447
Update chapters/lexicalstructure.tex
HansOlsson Nov 11, 2022
4a7c941
Update chapters/lexicalstructure.tex
HansOlsson Nov 11, 2022
0f5c46a
Update lexicalstructure.tex
casella Nov 15, 2022
e325f6f
Used LaTeX instead of rST for itemized list
casella Nov 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions RationaleMCP/0027/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Modelica Change Proposal MCP-0027<br/>Units of Literal Constants
Francesco Casella, Henrik Tidefelt

**(In Development)**

## Summary
The purpose of this MCP is to allow more unit errors to be detected by giving more expressions the unit `"1"` instead of having an undefined unit.
The problem with undefined unit is that it gets in the way of carrying out checking of units (which tools tend to deal with by not checking units at all where this happens).

## Revisions
| Date | Description |
| --- | --- |
| 2022-10-04 | Henrik Tidefelt. Filling this document with initial content. |

## Contributor License Agreement
All authors of this MCP or their organizations have signed the "Modelica Contributor License Agreement".

## Rationale
FIXME

## Backwards Compatibility
As current Modelica doesn't clearly reject some models with non-sensical combination of units, this MCP will break backwards compatibility by turning at least some of these invalid.

## Tool Implementation
None, so far.

### Experience with Prototype
N/A

## Required Patents
To the best of our knowledge, there are no patents that would conflict with the incorporation of this MCP.

## References
(None.)
18 changes: 18 additions & 0 deletions chapters/lexicalstructure.tex
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ \subsection{Strings}\label{strings}
\end{lstlisting}
\end{nonnormative}

\subsection{Units of Literal Constants}\label{units-literal-constants}

The following rules apply:
- The result of `x + L`, `L + x`, `x - L`, `L - x`, `x*L`, `L*x`, `x/L`, where `x` is an expression with non-empty \lstinline!unit! attribute string `<s>` and `L` is an expression containing only literal constants, shall have the same \lstinline!unit! attribute string of `x`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm off-base or too precise again, but I don't think expressions even have a unit attribute string -- only Real does (4.8.1).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write "real expression". Note that the literal can also be an integer literal, but the assumption is that both x and x+L (etc) are "real expressions".
Alternatively, we could view it as expressions having a unit - in general - but that may require more work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall have the same \lstinline!unit! attribute string as x.

Copy link
Collaborator

@qlambert-pro qlambert-pro Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write "real expression".

What about arrays?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write "real expression".

What about arrays?

That's messier - let's focus on their scalar elements first.

Usually, arrays are homogenous - i.e., all elements have the same unit - but sometimes they are not (especially when applying filtering to an array).

Note that you can write Real x[2](unit={"m","m/s"}), so you can create non-homogenous arrays - but it's not common.
(I believe we added support for that in Dymola in 2013.)

HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
- The result of `L/x` shall the the \lstinline!unit! attribute string `1/(<s>)`.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
- The result of \lstinline!x^L! shall have the \lstinline!unit! attribute string of \lstinline!product(fill(x, L))! if L is a positive Integer, or the unit of \lstinline!1/product(fill(x, L))! if L is a negative Integer
- If either side of a relational operator is a literal constant, then it is assumed to have the same \lstinline!unit! attribute string of the other side, if that is well-defined.

The inputs of the elementary mathematical functions defined in \cref{built-in-mathematical-functions-and-external-built-in-functions} such as \lstinline!sin()!, \lstinline!cos()!, \lstinline!exp()!, etc., should have a dimensionless unit (e.g., \lstinline!"1"! or \lstinline!"rad"!) if the unit string of the input is non-empty. Function \lstinline!atan2(y,x)! should be treated as \lstinline!atan(x/y)! for dimensional consistency checking purposes.
In that case, their outputs are also implicitly assumed to have a dimensionless unit.

\begin{nonnormative}
Rationale: by default, literal Real and Integer constants do not have a defined, non-empty \lstinline!unit! string attribute; hence they act as "unit wildcards", preventing dimensional consistency checking of equations that contain them. The rules regarding multiplication and division prevent this effect, allowing, e.g., to determine that `v = sqrt(2*g*h)` is dimensionally consistent, while `v = sqrt(2*g)` is dimensionally inconsistent, and thus most likely wrong. The rules regarding addition and subtraction instead allow to perform some basic unit inference in expressions containing mixed literal constants and variables (when there are no ambiguities in doing so), again expanding the scope for dimensional consistency checking. For example, they allow to determine that `tau = L/(abs(v) + 1e-9)` is dimensionally consistent, while `tau = 1/(abs(v) + 1e-9)` is not.]_
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved


The rules involving elementary mathematical functions extend the scope of this concept to also allow determining that equations such as \lstinline!i = i0*exp(v/i0)! or \lstinline!i = v0*exp(i/i0)! are dimensionally inconsistent.
\end{nonnormative}

\section{Operator Symbols}\label{operator-symbols}

The predefined operator symbols are formally defined on page \pageref{lexical-conventions} and
Expand Down