Skip to content

Commit

Permalink
Merge pull request #3258 from modelica/MCP/0031+simplify-when
Browse files Browse the repository at this point in the history
Web-Meeting (Hans, Henrik, Martin, Oliver)
Simplify when-equations
  • Loading branch information
olivleno committed Nov 30, 2022
2 parents 9bd4424 + bfd1fc2 commit 5a996ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion RationaleMCP/0031/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ These are subtopics that are considered necessary to resolve for a first version
- [x] Add function `realParameterEqual` for use in automatically generated asserts on `Real` equality. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Breal-equality/RationaleMCP/0031/differences.md#connect-equations), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3175)
- [x] Figure out what to do with synchronous features. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsynchronous/RationaleMCP/0031/differences.md#clock-partitions), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3240)
- [x] Event handling semantics is preserved as in Modelica.
- [ ] Get rid of `when initial()` and `when`-equations inside `if` and `for`. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsimplify-when/RationaleMCP/0031/differences.md#when-equations), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3258)
- [x] Get rid of `when initial()` and `when`-equations inside `if` and `for`. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsimplify-when/RationaleMCP/0031/differences.md#when-equations), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3258)
- [ ] Source locations pointing back to the original Modelica code. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bsource-locations/RationaleMCP/0031/source-locations.md), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3295)
- [ ] Settle the name (currently _Flat Modelica_), considering that scalarization isn't mandatory. [Design](https://github.com/modelica/ModelicaSpecification/blob/MCP/0031%2Bname-of-the-game/RationaleMCP/0031/name-of-the-game.md), [PR with discussion](https://github.com/modelica/ModelicaSpecification/pull/3224)

Expand Down
70 changes: 70 additions & 0 deletions RationaleMCP/0031/differences.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,76 @@ end 'M';
```


## When-Equations

The `when`-equations in Flat Modelica are more restricted compared to full Modelica.
In summary:
- `when`-equations have no meaning at all for the initialization problem:
* No special treatment of `initial()` as a `when`-clause trigger expression.
* No implicit initial equations in the form `x = pre(x)` for a variable `x` assigned in the `when`-equation.
- It is not allowed to have `when`-equations inside `if`-equations and `for`-equations.

Here, the _special treatment_ of `when initial() then` refers to the special meaning of such a `when`-equation in the initialization problem, including the special meaning of `reinit` when activated by `initial()`.
Hence, the first `when`-clause triggered by `initial()` in full Modelica needs to be turned into `initial equation` form in Flat Modelica, with `reinit`-equations replaced by equality-equations.
This also means that in Flat Modelica, the triggering condition `initial()` will have the same effect as the triggering condition `true and initial()`, namely that they will never trigger the `when`-clause because the expression never undergoes a positive edge.

The implicit initial equations `x = pre(x)` in full Modelica (in case no `when`-clause is activated with `initial()`) need to be made explicit in Flat Modelica.

Regarding `when`-equations inside `if`-equations and `for`-equations, full Modelica only allows this where the `if`-equation conditions and `for`-equation ranges are parameter expressions.
Hence, it is only with a small loss of generality that it is being assumed that these conditions and ranges should be possible to evaluate during translation, allowing an `if`-equation to be reduced to one of its branches, or a `for`-equation to be unrolled.


## When-Statements

Unlike the `when`-equations, there are no restrictions on the `when`-statements in Flat Modelica relative to full Modelica.

Note that `reinit` is not allowed in a `when`-statement, so the notable thing about `when`-statements in Flat Modelica is that they may be triggerd by `initial()` just like in Full Modelica.

### Rationale

The reason for not further restricting the `when`-statements is that it it was considered too complicated to reduce `when initial()` in an algorithm to something more elementary. As an example, consider the following full Modelica `when`-statement with a clause triggered by `initial()`:
```
Real x;
Real y;
algorithm
x := 0.5;
x := x + time;
when {x^2 > 2.0, initial()} then
y := pre(x);
end when;
x := x + 0.5;
```

Note that putting the following `if`-statement in the algorithm would be illegal since the argument of `pre` needs to be discrete-time:
```
if initial() then
'y' := pre('x'); /* 'x' is continuous-time, since no longer inside when-clause. */
end if;
```

To get around this problem, one could try making separate variants of the algorithm depending on `initial()`:
```
Real 'x';
Real 'y';
initial algorithm
'x' := 0.5;
'x' := 'x' + time;
'y' := pre('x'); /* 'x' is discrete-time, since inside initial algorithm. */
'x' := 'x' + 0.5;
algorithm
if not initial() then
'x' := 0.5;
'x' := 'x' + time;
when 'x'^2 > 2.0 then
'y' := pre('x');
end when;
'x' := 'x' + 0.5;
end if;
```

However, this doesn't work either, as the initialization problem will have two algorithms assigning to `'x'` and `'y'`, even though one of the algorithm has a disabled body.


## Pure Modelica functions

In addition to full Modelica's classification into _pure_ and _impure_, Flat Modelica adds the concept of a `pure constant` function, informally characterized by the following properties:
Expand Down

0 comments on commit 5a996ba

Please sign in to comment.