Skip to content

Commit

Permalink
Nb update function alias (#11451)
Browse files Browse the repository at this point in the history
* [NB] update function alias

  - create tuple auxilliaries as multiple variables instead of one tuple variable
  - update Equation.makeAssignment
  - tuple assignments are considered record assignments structurally

* [NB] update EquationPointers.toString

 - correctly skip empty equations inside so that it doesnt have to be compressed to be printed

* [NB] solve tuple equations

* [NB] fix makeAssignment for loop size
  • Loading branch information
kabdelhak committed Oct 25, 2023
1 parent 346282c commit a4bf847
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 69 deletions.
82 changes: 56 additions & 26 deletions OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo
Expand Up @@ -680,11 +680,14 @@ public
input output String str = "";
protected
String s = "(" + intString(Equation.size(Pointer.create(eq))) + ")";
String tupl_recd_str;
algorithm
str := match eq
case SCALAR_EQUATION() then str + "[SCAL] " + s + " " + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + EquationAttributes.toString(eq.attr, " ");
case ARRAY_EQUATION() then str + "[ARRY] " + s + " " + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + EquationAttributes.toString(eq.attr, " ");
case RECORD_EQUATION() then str + "[RECD] " + s + " " + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + EquationAttributes.toString(eq.attr, " ");
case RECORD_EQUATION() algorithm
tupl_recd_str := if Type.isTuple(eq.ty) then "[TUPL] " else "[RECD] ";
then str + tupl_recd_str + s + " " + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + EquationAttributes.toString(eq.attr, " ");
case ALGORITHM() then str + "[ALGO] " + s + EquationAttributes.toString(eq.attr, " ") + "\n" + Algorithm.toString(eq.alg, str + "[----] ");
case IF_EQUATION() then str + IfEquationBody.toString(eq.body, str + "[----] ", "[-IF-] " + s);
case FOR_EQUATION() then str + forEquationToString(eq.iter, eq.body, "", str + "[----] ", "[FOR-] " + s + EquationAttributes.toString(eq.attr, " "));
Expand Down Expand Up @@ -822,7 +825,7 @@ public
end getSolvedVar;

function makeAssignment
input ComponentRef lhs;
input Expression lhs;
input Expression rhs;
input Pointer<Integer> idx;
input String str;
Expand All @@ -831,38 +834,56 @@ public
output Pointer<Equation> eq;
protected
Equation e;
Type ty = ComponentRef.getSubscriptedType(lhs, true);
Type ty = Expression.typeOf(lhs);
algorithm
if Iterator.isEmpty(iter) then
if Type.isArray(ty) then
eq := Pointer.create(ARRAY_EQUATION(
// match type and create equation accordingly
e := match ty
case Type.ARRAY() then ARRAY_EQUATION(
ty = ty,
lhs = Expression.fromCref(lhs),
lhs = lhs,
rhs = rhs,
source = DAE.emptyElementSource,
attr = attr,
recordSize = NONE()
));
else
eq := Pointer.create(SCALAR_EQUATION(
);
case Type.TUPLE() then RECORD_EQUATION(
ty = ty,
lhs = lhs,
rhs = rhs,
source = DAE.emptyElementSource,
attr = attr,
recordSize = Type.sizeOf(ty)
);
case Type.COMPLEX() then RECORD_EQUATION(
ty = ty,
lhs = lhs,
rhs = rhs,
source = DAE.emptyElementSource,
attr = attr,
recordSize = Type.sizeOf(ty)
);
else SCALAR_EQUATION(
ty = ty,
lhs = Expression.fromCref(lhs),
lhs = lhs,
rhs = rhs,
source = DAE.emptyElementSource,
attr = attr
));
end if;
else
);
end match;

// create for-loop around it if there is an iterator
if not Iterator.isEmpty(iter) then
e := FOR_EQUATION(
size = ComponentRef.size(lhs),
size = Type.sizeOf(ty) * Iterator.size(iter),
iter = iter,
body = {SCALAR_EQUATION(ty, Expression.fromCref(lhs), rhs, DAE.emptyElementSource, attr)}, // this can also be an array?
body = {e},
source = DAE.emptyElementSource,
attr = attr
);
// inline if it has size 1
eq := Pointer.create(Inline.inlineForEquation(e));
e := Inline.inlineForEquation(e);
end if;
eq := Pointer.create(e);
Equation.createName(eq, idx, str);
end makeAssignment;

Expand Down Expand Up @@ -2788,16 +2809,19 @@ public
input output String str = "";
input Boolean printEmpty = true;
protected
Integer numberOfElements = EquationPointers.size(equations);
Integer length = 10;
Integer luI = lastUsedIndex(equations);
Integer length = 10, current_index = 1;
String index;
algorithm
if printEmpty or numberOfElements > 0 then
str := StringUtil.headline_4(str + " Equations (" + intString(numberOfElements) + "/" + intString(scalarSize(equations)) + ")");
for i in 1:numberOfElements loop
index := "(" + intString(i) + ")";
index := index + StringUtil.repeat(" ", length - stringLength(index));
str := str + Equation.toString(Pointer.access(ExpandableArray.get(i, equations.eqArr)), index) + "\n";
if printEmpty or luI > 0 then
str := StringUtil.headline_4(str + " Equations (" + intString(EquationPointers.size(equations)) + "/" + intString(scalarSize(equations)) + ")");
for i in 1:luI loop
if ExpandableArray.occupied(i, equations.eqArr) then
index := "(" + intString(current_index) + ")";
index := index + StringUtil.repeat(" ", length - stringLength(index));
str := str + Equation.toString(Pointer.access(ExpandableArray.get(i, equations.eqArr)), index) + "\n";
current_index := current_index + 1;
end if;
end for;
str := str + "\n";
else
Expand Down Expand Up @@ -2845,6 +2869,12 @@ public
end for;
end scalarSize;

function lastUsedIndex
"returns the last used index != size!"
input EquationPointers equations;
output Integer sz = ExpandableArray.getLastUsedIndex(equations.eqArr);
end lastUsedIndex;

function toList
"Creates a EquationPointer list from EquationPointers."
input EquationPointers equations;
Expand Down Expand Up @@ -3349,7 +3379,7 @@ public

case EQ_DATA_EMPTY() then "Empty equation Data!\n";

else getInstanceName() + " failed!\n";
else getInstanceName() + " failed!\n";
end match;
end toString;

Expand Down
10 changes: 6 additions & 4 deletions OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo
Expand Up @@ -223,6 +223,7 @@ public
Integer multi_algorithm;
Integer multi_when;
Integer multi_if;
Integer multi_tpl;
Integer generic_for;
Integer entwined_for;
Integer loop_lin;
Expand All @@ -248,10 +249,11 @@ public

case StrongComponent.MULTI_COMPONENT() algorithm
_ := match Pointer.access(comp.eqn)
case Equation.ALGORITHM() algorithm collector.multi_algorithm := collector.multi_algorithm + 1; Pointer.update(collector_ptr, collector); then ();
case Equation.WHEN_EQUATION() algorithm collector.multi_when := collector.multi_when + 1; Pointer.update(collector_ptr, collector); then ();
case Equation.IF_EQUATION() algorithm collector.multi_if := collector.multi_if + 1; Pointer.update(collector_ptr, collector); then ();
else algorithm Error.addCompilerWarning("Cannot classify strong component:\n" + toString(comp) + "\n"); then ();
case Equation.ALGORITHM() algorithm collector.multi_algorithm := collector.multi_algorithm + 1; Pointer.update(collector_ptr, collector); then ();
case Equation.WHEN_EQUATION() algorithm collector.multi_when := collector.multi_when + 1; Pointer.update(collector_ptr, collector); then ();
case Equation.IF_EQUATION() algorithm collector.multi_if := collector.multi_if + 1; Pointer.update(collector_ptr, collector); then ();
case Equation.RECORD_EQUATION() algorithm collector.multi_tpl := collector.multi_tpl + 1; Pointer.update(collector_ptr, collector); then ();
else algorithm Error.addCompilerWarning("Cannot classify strong component:\n" + toString(comp) + "\n"); then ();
end match;
then ();

Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo
Expand Up @@ -1352,7 +1352,7 @@ public
input String phase;
input list<list<System>> systems;
protected
CountCollector c = CountCollector.COUNT_COLLECTOR(0,0,0,0,0,0,0,0,0,0);
CountCollector c = CountCollector.COUNT_COLLECTOR(0,0,0,0,0,0,0,0,0,0,0);
Pointer<CountCollector> collector_ptr = Pointer.create(c);
String single_sc, multi_sc, for_sc, alg_sc;
algorithm
Expand All @@ -1363,7 +1363,7 @@ public
end for;
c := Pointer.access(collector_ptr);
single_sc := intString(c.single_scalar + c.single_array + c.single_record) + " (scalar:" + intString(c.single_scalar) + ", array:" + intString(c.single_array) + ", record:" + intString(c.single_record) + ")";
multi_sc := intString(c.multi_algorithm + c.multi_when + c.multi_if) + " (algorithm:" + intString(c.multi_algorithm) + ", when:" + intString(c.multi_when) + ", if:" + intString(c.multi_if) + ")";
multi_sc := intString(c.multi_algorithm + c.multi_when + c.multi_if) + " (algorithm:" + intString(c.multi_algorithm) + ", when:" + intString(c.multi_when) + ", if:" + intString(c.multi_if) + ", tuple:" + intString(c.multi_tpl) + ")";
for_sc := intString(c.generic_for + c.entwined_for) + " (generic: " + intString(c.generic_for) + ", entwined:" + intString(c.entwined_for) + ")";
alg_sc := intString(c.loop_lin + c.loop_nlin) + " (linear: " + intString(c.loop_lin) + ", nonlinear:" + intString(c.loop_nlin) + ")";
Expand Down
Expand Up @@ -192,7 +192,7 @@ public

// make the new start equation
kind := if BVariable.isContinuous(state) then EquationKind.CONTINUOUS else EquationKind.DISCRETE;
start_eq := Equation.makeAssignment(name, start_exp, idx, NBEquation.START_STR, Iterator.EMPTY(), EquationAttributes.default(kind, true));
start_eq := Equation.makeAssignment(Expression.fromCref(name), start_exp, idx, NBEquation.START_STR, Iterator.EMPTY(), EquationAttributes.default(kind, true));
Pointer.update(ptr_start_eqs, start_eq :: Pointer.access(ptr_start_eqs));
then ();

Expand Down Expand Up @@ -326,7 +326,7 @@ public

// make the new start equation
kind := if BVariable.isContinuous(var_ptr) then EquationKind.CONTINUOUS else EquationKind.DISCRETE;
start_eq := Equation.makeAssignment(name, start_exp, idx, NBEquation.START_STR, Iterator.fromFrames(frames), EquationAttributes.default(kind, true));
start_eq := Equation.makeAssignment(Expression.fromCref(name), start_exp, idx, NBEquation.START_STR, Iterator.fromFrames(frames), EquationAttributes.default(kind, true));
if not listEmpty(state.indices) then
// empty list indicates full array, slice otherwise
(start_eq, _, _) := Equation.slice(start_eq, state.indices, NONE(), FunctionTreeImpl.EMPTY());
Expand All @@ -348,7 +348,7 @@ public
pre := BVariable.getPrePost(var_ptr);
if Util.isSome(pre) then
kind := if BVariable.isContinuous(var_ptr) then EquationKind.CONTINUOUS else EquationKind.DISCRETE;
pre_eq := Equation.makeAssignment(BVariable.getVarName(var_ptr), Expression.fromCref(BVariable.getVarName(Util.getOption(pre))), idx, NBEquation.PRE_STR, Iterator.EMPTY(), EquationAttributes.default(kind, true));
pre_eq := Equation.makeAssignment(Expression.fromCref(BVariable.getVarName(var_ptr)), Expression.fromCref(BVariable.getVarName(Util.getOption(pre))), idx, NBEquation.PRE_STR, Iterator.EMPTY(), EquationAttributes.default(kind, true));
Pointer.update(ptr_pre_eqs, pre_eq :: Pointer.access(ptr_pre_eqs));
end if;
end if;
Expand Down Expand Up @@ -386,7 +386,7 @@ public
name := ComponentRef.mergeSubscripts(subscripts, name, true, true);

kind := if BVariable.isContinuous(var_ptr) then EquationKind.CONTINUOUS else EquationKind.DISCRETE;
pre_eq := Equation.makeAssignment(name, Expression.fromCref(pre_name), idx, NBEquation.PRE_STR, Iterator.fromFrames(frames), EquationAttributes.default(kind, true));
pre_eq := Equation.makeAssignment(Expression.fromCref(name), Expression.fromCref(pre_name), idx, NBEquation.PRE_STR, Iterator.fromFrames(frames), EquationAttributes.default(kind, true));

if not listEmpty(var_slice.indices) then
// empty list indicates full array, slice otherwise
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBEvents.mo
Expand Up @@ -186,7 +186,7 @@ public
(iter, range) := Equation.Iterator.getFrames(iterator);
// lower the subscripts (containing iterators)
lhs_cref := ComponentRef.mapSubscripts(BVariable.getVarName(aux_var), function Subscript.mapExp(func = function BackendDAE.lowerComponentReferenceExp(variables = variables)));
aux_eqn := Equation.makeAssignment(lhs_cref, rhs, idx, context, Iterator.fromFrames(List.zip(iter, range)), EquationAttributes.default(EquationKind.DISCRETE, false));
aux_eqn := Equation.makeAssignment(Expression.fromCref(lhs_cref), rhs, idx, context, Iterator.fromFrames(List.zip(iter, range)), EquationAttributes.default(EquationKind.DISCRETE, false));
auxiliary_vars := aux_var :: auxiliary_vars;
auxiliary_eqns := aux_eqn :: auxiliary_eqns;
end if;
Expand Down

0 comments on commit a4bf847

Please sign in to comment.