Skip to content

Commit

Permalink
[NB] array updates adjacency and simcode (#10007)
Browse files Browse the repository at this point in the history
- better handling of scalarized array dependencies (jacobian)
 - updated to old DAE conversion for iterators
  • Loading branch information
kabdelhak committed Jan 5, 2023
1 parent e906c3d commit eef6304
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
26 changes: 24 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo
Expand Up @@ -492,6 +492,16 @@ public
list<ComponentRef> names;
list<Expression> ranges;

// sliced array equations - create all the single entries
case SINGLE_COMPONENT() guard(Equation.isArrayEquation(comp.eqn)) algorithm
dependencies := Equation.collectCrefs(Pointer.access(comp.eqn), function Slice.getDependentCrefCausalized(set = set));
scalarized_dependencies := Slice.getDependentCrefsPseudoArrayCausalized(BVariable.getVarName(comp.var), dependencies);
for tpl in scalarized_dependencies loop
(cref, dependencies) := tpl;
updateDependencyMap(cref, dependencies, map, jacType);
end for;
then ();

case SINGLE_COMPONENT() algorithm
dependencies := Equation.collectCrefs(Pointer.access(comp.eqn), function Slice.getDependentCrefCausalized(set = set));
updateDependencyMap(BVariable.getVarName(comp.var), dependencies, map, jacType);
Expand All @@ -515,7 +525,18 @@ public
end for;
then ();

// sliced regular equation. ToDo: what if slice is array?
// sliced array equations - create all the single entries
case SLICED_COMPONENT() guard(Equation.isArrayEquation(Slice.getT(comp.eqn))) algorithm
eqn as Equation.FOR_EQUATION(iter = iter) := Pointer.access(Slice.getT(comp.eqn));
dependencies := Equation.collectCrefs(eqn, function Slice.getDependentCrefCausalized(set = set));
scalarized_dependencies := Slice.getDependentCrefsPseudoArrayCausalized(comp.var_cref, dependencies, comp.eqn.indices);
for tpl in scalarized_dependencies loop
(cref, dependencies) := tpl;
updateDependencyMap(cref, dependencies, map, jacType);
end for;
then ();

// sliced regular equation.
case SLICED_COMPONENT() algorithm
eqn := Pointer.access(Slice.getT(comp.eqn));
dependencies := Equation.collectCrefs(eqn, function Slice.getDependentCrefCausalized(set = set));
Expand Down Expand Up @@ -818,7 +839,8 @@ protected
// update the current value (res/tmp) --> {independent vars}
UnorderedMap.add(cref, fixed_dependencies, map);
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to update " + ComponentRef.toString(cref)
+ " with dependencies " + List.toString(dependencies, ComponentRef.toString) + "."});
end try;
end updateDependencyMap;

Expand Down
29 changes: 28 additions & 1 deletion OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo
Expand Up @@ -468,9 +468,36 @@ public
else
tpl_lst := list((new_row_cref, {}) for new_row_cref in new_row_crefs);
end if;

end getDependentCrefsPseudoForCausalized;


function getDependentCrefsPseudoArrayCausalized
"[Adjacency.MatrixType.PSEUDO] Array equations.
Turns cref dependencies into index lists, used for adjacency."
input ComponentRef row_cref "cref representing the current row";
input list<ComponentRef> dependencies "dependent var crefs";
input list<Integer> slice = {} "optional slice, empty least means all";
output list<tuple<ComponentRef, list<ComponentRef>>> tpl_lst "cref -> dependencies for each scalar cref";
protected
list<ComponentRef> row_cref_scal, dep_scal;
list<list<ComponentRef>> dependencies_scal = {};
Boolean sliced = not listEmpty(slice);
algorithm
row_cref_scal := ComponentRef.scalarizeAll(row_cref);
if sliced then
row_cref_scal := List.keepPositions(row_cref_scal, slice);
end if;
for dep in listReverse(dependencies) loop
dep_scal := ComponentRef.scalarizeAll(dep);
if sliced then
dep_scal := List.keepPositions(dep_scal, slice);
end if;
dependencies_scal := dep_scal :: dependencies_scal;
end for;
dependencies_scal := List.transposeList(dependencies_scal);
tpl_lst := List.zip(row_cref_scal, dependencies_scal);
end getDependentCrefsPseudoArrayCausalized;

function locationToIndex
"reverse function to indexToLocation()
maps a frame location to a scalar index starting from first index (one based!)"
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -937,7 +937,7 @@ protected
algorithm
Statement.FOR(iterator = iterator, range = SOME(range), body = body, forType = for_type, source = source) := forStmt;
dbody := convertStatements(body);
Component.ITERATOR(ty = ty) := InstNode.component(iterator);
ty := InstNode.getType(iterator);

forDAE := match for_type
case Statement.ForType.NORMAL()
Expand Down
6 changes: 5 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -1003,11 +1003,15 @@ uniontype InstNode
function getType
input InstNode node;
output Type ty;
protected
Variable var;
algorithm
ty := match node
case CLASS_NODE() then Class.getType(Pointer.access(node.cls), node);
case COMPONENT_NODE() then Component.getType(Pointer.access(node.component));
case VAR_NODE() then Type.ANY();
case VAR_NODE() algorithm
var := Pointer.access(node.varPointer);
then var.ty;
end match;
end getType;

Expand Down
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/NSimCode/NSimVar.mo
Expand Up @@ -1007,6 +1007,14 @@ public
Pointer.update(indices_ptr, simCodeIndices);
then ();

case (Type.ENUMERATION(), VarType.PARAMETER)
algorithm
Pointer.update(int_lst, SimVar.create(var, simCodeIndices.uniqueIndex, simCodeIndices.integerParamIndex) :: Pointer.access(int_lst));
simCodeIndices.integerParamIndex := simCodeIndices.integerParamIndex + 1;
simCodeIndices.uniqueIndex := simCodeIndices.uniqueIndex + 1;
Pointer.update(indices_ptr, simCodeIndices);
then ();

case (Type.BOOLEAN(), VarType.PARAMETER)
algorithm
Pointer.update(bool_lst, SimVar.create(var, simCodeIndices.uniqueIndex, simCodeIndices.booleanParamIndex) :: Pointer.access(bool_lst));
Expand Down
4 changes: 3 additions & 1 deletion OMCompiler/Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -401,10 +401,12 @@ algorithm
try
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMCODE);
simCode := NSimCode.SimCode.create(bdae, className, fileNamePrefix, simSettingsOpt);
if Flags.isSet(Flags.DUMP_SIMCODE) then
print(NSimCode.SimCode.toString(simCode));
end if;
(fileDir, libs) := NSimCode.SimCode.getDirectoryAndLibs(simCode);
oldSimCode := NSimCode.SimCode.convert(simCode);
if Flags.isSet(Flags.DUMP_SIMCODE) then
print(NSimCode.SimCode.toString(simCode));
SimCodeUtil.dumpSimCodeDebug(oldSimCode);
end if;
timeSimCode := System.realtimeTock(ClockIndexes.RT_CLOCK_SIMCODE);
Expand Down

0 comments on commit eef6304

Please sign in to comment.