﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
6045	The NF rejects supposedly legal code involving algorithms and discrete variables	Francesco Casella	Karim Adbdelhak	"In #5836, checks were introduced to make sure that discrete variables can only be matched to left-hand-sides of when-equations.

IN [https://github.com/OpenModelica/OpenModelica/commit/5aec587efc 5aec587] additional sanity checks for discrete variables were introduced in the NF.

Consider the [https://libraries.openmodelica.org/branches/newInst/PowerGrids/PowerGrids.html PowerGrids newInst report]. After the recent fixes to the library, that previously contained incorrect {{{discrete}}} prefixes, many models are now running OK. 

However, there are seven remaining failures, see e.g. [https://libraries.openmodelica.org/branches/newInst/PowerGrids/files/PowerGrids_PowerGrids.Examples.Tutorial.GridOperation.Controlled.ControlledGridWithoutPSS.err this one], that have all the same root cause, which can all be traced to the code of the [https://github.com/PowerGrids/PowerGrids/blob/b9b950475c5840c9f7aa76ef67e82e0250d24506/PowerGrids/Electrical/Buses/BusFault.mo#L17 Bus Fault model]:
{{{
  discrete Types.ComplexAdmittance Y(
    re(start = 0, fixed = true), 
    im(start = 0, fixed = true)) ""Shunt admittance"";
...
algorithm
   when pre(fault) then
     Y := 1/Complex(R, X);
   end when;

   when not pre(fault) then
     Y := Complex(0); 
   end when;
}}}

The Specification, [https://specification.modelica.org/v3.4/Ch11.html#when-statements Sect. 11.2.7] defines when-statements:
  The algorithmic statements within a when-statement are activated when the scalar or any one of the elements of the vector-expression becomes true.

In this case, I understand the restrictions of [https://specification.modelica.org/v3.4/Ch8.html#restrictions-on-equations-within-when-equations Section 8.3.5.2] do not apply, because in this case we do not have when-equations, but rather an algorithm with when-statements.

Unfortunately, the NF aborts immediately after completing NFScalarize, complaining that
{{{
Error: Following variable is discrete, but does not appear on the
LHS of a when-statement: NTHV.Y.im.
Error: Following variable is discrete, but does not appear on the
LHS of a when-statement: NTHV.Y.re.
}}}

I'd say these messages are incorrect, because indeed NTHV.Y appears on the left-hand-side of all the assignments inside the algorithm, even though not directly, being it a complex record. This model (that runs fine in Dymola) is thus legal and should be accepted for further optimizations. 

A couple of weeks ago the entire PowerGrids library worked flawlessly, so I guess this is due to some recent commit, that introduced a regression. It would be good if we can fix that in time for the final 1.16.0 release.

@perost, @Karim, would you mind having a look?"	defect	closed	blocker	1.16.0	New Instantiation		fixed		Karim Adbdelhak adrien.guironnet@… Per Östlund Mahder Alemseged Gebremedhin
