Skip to content

Commit

Permalink
[NB] correctly create children of record with fromCref() (OpenModelic…
Browse files Browse the repository at this point in the history
…a#12570)

- get all children at once
 - small update to debugging output
 - update to binding equation generation. do not make equation for unbound records (children have to be bound)
  • Loading branch information
kabdelhak committed Jun 12, 2024
1 parent 2d6641b commit d94c8c9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 44 deletions.
7 changes: 1 addition & 6 deletions OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,6 @@ public
attr = attr
);
end match;

// create for-loop around it if there is an iterator
if not Iterator.isEmpty(iter) then
e := FOR_EQUATION(
Expand All @@ -967,9 +966,7 @@ public
end if;
end makeAssignmentEqn;


public

public
function makeAlgorithm
input list<Statement> stmts;
input Boolean init;
Expand Down Expand Up @@ -1363,7 +1360,6 @@ public
then eqn;
else eqn;
end match;

Pointer.update(eqn_ptr, eqn);
end createName;

Expand Down Expand Up @@ -1804,7 +1800,6 @@ public
else
eqnAttr := EquationAttributes.default(EquationKind.DISCRETE, initial_);
end if;

// simplify rhs and get potential iterators
(iter, rhs) := Iterator.extract(rhs);
rhs := SimplifyExp.simplifyDump(rhs, true, getInstanceName());
Expand Down
16 changes: 6 additions & 10 deletions OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public
input Binding binding = NFBinding.EMPTY_BINDING;
output Variable variable;
protected
InstNode node, class_node, child_node;
InstNode node, class_node;
array<InstNode> child_nodes;
ComponentRef child_cref;
Type ty;
Prefixes.Visibility vis;
Expand All @@ -160,16 +161,12 @@ public
ty := ComponentRef.getSubscriptedType(cref, true);
vis := InstNode.visibility(node);
info := InstNode.info(node);

// get the record children if the variable is a record (and not an external object)
if not Type.isExternalObject(ty) then
children := match (Type.arrayElementType(ty), Type.complexSize(ty))
case (Type.COMPLEX(cls = class_node), SOME(complexSize)) algorithm
for i in complexSize:-1:1 loop
child_node := Class.nthComponent(i, InstNode.getClass(class_node));
child_cref := ComponentRef.prefixCref(child_node, InstNode.getType(child_node), {}, cref);
children := fromCref(child_cref) :: children;
end for;
children := match Type.arrayElementType(ty)
case Type.COMPLEX(cls = class_node) algorithm
child_nodes := Class.getComponents(InstNode.getClass(class_node));
children := list(fromCref(ComponentRef.prefixCref(c, InstNode.getType(c), {}, cref)) for c in child_nodes);
then children;
else {};
end match;
Expand Down Expand Up @@ -1009,7 +1006,6 @@ public
var := fromCref(cref);
// update the variable to be a seed and pass the pointer to the original variable
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DAE_RESIDUAL_VAR(uniqueIndex));

// create the new variable pointer and safe it to the component reference
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
end makeResidualVar;
Expand Down
14 changes: 6 additions & 8 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBInitialization.mo
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,17 @@ public
list<Pointer<Equation>> parameter_eqs = {};
list<Pointer<Variable>> initial_param_vars = {};
Pointer<Variable> parent;
Boolean skip_record_element;
Boolean skip;
algorithm

for var in VariablePointers.toList(parameters) loop

// check if the variable is a record element with bound parent
skip_record_element := match BVariable.getParent(var)
// check if the variable is a record element with bound parent or a record without binding
skip := match BVariable.getParent(var)
case SOME(parent) then BVariable.isBound(parent);
else false;
else BVariable.isRecord(var) and not BVariable.isBound(var);
end match;

// parse records slightly different
if BVariable.isKnownRecord(var) then
if BVariable.isKnownRecord(var) and not skip then
// only consider non-evaluable parameter bindings
if not BVariable.hasEvaluableBinding(var) then
initial_param_vars := listAppend(BVariable.getRecordChildren(var), initial_param_vars);
Expand All @@ -341,7 +339,7 @@ public
end if;

// all other variables that are not records and not record elements to be skipped
elseif not (BVariable.isRecord(var) or skip_record_element) then
elseif not (BVariable.isRecord(var) or skip) then
// only consider non-evaluable parameter bindings
if not BVariable.hasEvaluableBinding(var) then
// add variable to initial unknowns
Expand Down
32 changes: 25 additions & 7 deletions OMCompiler/Compiler/NBackEnd/Util/NBAdjacency.mo
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,38 @@ public
function toString
input Matrix adj;
input output String str = "";
protected
function complexSizeStr
input Option<Integer> sz;
output String str;
algorithm
str := match sz
local
Integer i;
case SOME(i) then intString(i);
else "0";
end match;
end complexSizeStr;
algorithm
str := StringUtil.headline_2(str + "AdjacencyMatrix") + "\n";
str := match adj
local
array<String> names, types;
Integer length1, length2;
list<Type> types;
array<String> names, types_str, complex_sizes;
Integer length0, length1, length2;

case FULL() algorithm
types := listArray(list(dimsString(Type.arrayDims(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
types := list(ComponentRef.getSubscriptedType(name) for name in adj.equation_names);
complex_sizes := listArray(list(complexSizeStr(Type.complexSize(ty)) for ty in types));
types_str := listArray(list(dimsString(Type.arrayDims(ty)) for ty in types));
names := listArray(list(ComponentRef.toString(name) for name in adj.equation_names));
length1 := max(stringLength(ty) for ty in types) + 1;
length0 := max(stringLength(sz) for sz in complex_sizes);
length1 := max(stringLength(ty) for ty in types_str) + 1;
length2 := max(stringLength(name) for name in names) + 3;
for i in 1:arrayLength(names) loop
str := str + arrayGet(types, i) + " " + StringUtil.repeat(".", length1 - stringLength(arrayGet(types, i))) + " "
str := str
+ arrayGet(complex_sizes, i) + " " + StringUtil.repeat(" ", length0 - stringLength(arrayGet(complex_sizes, i))) + " | "
+ arrayGet(types_str, i) + " " + StringUtil.repeat(".", length1 - stringLength(arrayGet(types_str, i))) + " "
+ arrayGet(names, i) + " " + StringUtil.repeat(".", length2 - stringLength(arrayGet(names, i)))
+ " " + List.toString(UnorderedSet.toList(adj.occurences[i]), function fullString(dep_map = adj.dependencies[i],
sol_map = adj.solvabilities[i], rep_set = adj.repetitions[i])) + "\n";
Expand Down Expand Up @@ -907,7 +925,7 @@ public

case FULL() algorithm
str := StringUtil.headline_2(str + " Solvability Adjacency Matrix") + "\n";
types := listArray(list(dimsString(Type.arrayDims(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
types := listArray(list(intString(Type.sizeOf(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
names := listArray(list(ComponentRef.toString(name) for name in adj.equation_names));
for i in arrayLength(names):-1:1 loop
(XX, II, NM, NP, LV, LP, LC, QQ) := Solvability.categorize(UnorderedSet.toList(adj.occurences[i]), adj.solvabilities[i]);
Expand Down Expand Up @@ -968,7 +986,7 @@ public

case FULL() algorithm
str := StringUtil.headline_2(str + " Dependency Adjacency Matrix") + "\n";
types := listArray(list(dimsString(Type.arrayDims(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
types := listArray(list(intString(Type.sizeOf(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
names := listArray(list(ComponentRef.toString(name) for name in adj.equation_names));
for i in arrayLength(names):-1:1 loop
(F, R, E, A, S, K) := Dependency.categorize(UnorderedSet.toList(adj.occurences[i]), adj.dependencies[i], adj.repetitions[i]);
Expand Down
5 changes: 5 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFClass.mo
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
component := ClassTree.nthComponent(index, classTree(cls));
end nthComponent;

function getComponents
input Class cls;
output array<InstNode> comps = ClassTree.getComponents(classTree(cls));
end getComponents;

function lookupAttributeBinding
input String name;
input Class cls;
Expand Down
18 changes: 13 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4647,6 +4647,9 @@ public
case Type.BOOLEAN() then BOOLEAN(false);
case Type.ARRAY() then fillType(ty, makeZero(Type.arrayElementType(ty)));
case Type.COMPLEX() then makeOperatorRecordZero(ty.cls);
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for: " + Type.toString(ty)});
then fail();
end match;
end makeZero;

Expand All @@ -4657,11 +4660,16 @@ public
InstNode op_node;
Function.Function fn;
algorithm
op_node := Class.lookupElement("'0'", InstNode.getClass(recordNode));
Function.Function.instFunctionNode(op_node, NFInstContext.NO_CONTEXT, InstNode.info(InstNode.parent(op_node)));
{fn} := Function.Function.typeNodeCache(op_node);
zeroExp := CALL(Call.makeTypedCall(fn, {}, Variability.CONSTANT, Purity.PURE));
zeroExp := Ceval.evalExp(zeroExp);
try
op_node := Class.lookupElement("'0'", InstNode.getClass(recordNode));
Function.Function.instFunctionNode(op_node, NFInstContext.NO_CONTEXT, InstNode.info(InstNode.parent(op_node)));
{fn} := Function.Function.typeNodeCache(op_node);
zeroExp := CALL(Call.makeTypedCall(fn, {}, Variability.CONSTANT, Purity.PURE));
zeroExp := Ceval.evalExp(zeroExp);
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for: " + InstNode.toString(recordNode)});
fail();
end try;
end makeOperatorRecordZero;

function makeOne
Expand Down
18 changes: 10 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public
output Variable variable;
protected
list<ComponentRef> crefs;
InstNode node, child_node;
InstNode node, class_node;
Component comp;
Type ty;
Binding binding;
Expand All @@ -89,8 +89,8 @@ public
Option<SCode.Comment> cmt;
SourceInfo info;
BackendExtension.BackendInfo binfo = NFBackendExtension.DUMMY_BACKEND_INFO;
array<InstNode> child_nodes;
list<Variable> children = {};
Option<Integer> complexSize;
algorithm
node := ComponentRef.node(cref);
comp := InstNode.component(node);
Expand All @@ -110,12 +110,14 @@ public
end if;

// get the record children if the variable is a record
complexSize := Type.complexSize(ty);
if Util.isSome(complexSize) then
for i in Util.getOption(complexSize):-1:1 loop
child_node := Class.nthComponent(i, InstNode.getClass(node));
children := fromCref(ComponentRef.fromNode(child_node, InstNode.getType(child_node))) :: children;
end for;
if not Type.isExternalObject(ty) then
children := match Type.arrayElementType(ty)
case Type.COMPLEX(cls = class_node) algorithm
child_nodes := Class.getComponents(InstNode.getClass(class_node));
children := list(fromCref(ComponentRef.prefixCref(c, InstNode.getType(c), {}, cref)) for c in child_nodes);
then children;
else {};
end match;
end if;

variable := VARIABLE(cref, ty, binding, vis, attr, {}, children, cmt, info, binfo);
Expand Down
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/Util/Util.mo
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ algorithm
end for;
end boolAndList;

public function optionToString<T>
input Option<T> ot;
input FuncType f;
output String str;
partial function FuncType
input T t;
output String str;
end FuncType;
protected
T t;
algorithm
str := match ot
case SOME(t) then "SOME(" + f(t) + ")";
else "NONE()";
end match;
end optionToString;

public function applyOption<TI, TO>
"Takes an option value and a function over the value. It returns in another
option value, resulting from the application of the function on the value.
Expand Down

0 comments on commit d94c8c9

Please sign in to comment.