Skip to content

Commit

Permalink
New backend nice sorting (#9347)
Browse files Browse the repository at this point in the history
[NB] implement new 2 step sorting algorithm and generic for loop solutions
  • Loading branch information
kabdelhak committed Sep 14, 2022
1 parent f8efead commit b9042d0
Show file tree
Hide file tree
Showing 61 changed files with 2,965 additions and 2,030 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
Expand Up @@ -387,6 +387,7 @@ set(OMC_MM_BACKEND_SOURCES
# "NSimCode";
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimCode.mo
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimCodeUtil.mo
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimGenericCall.mo
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimJacobian.mo
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimStrongComponent.mo
${CMAKE_CURRENT_SOURCE_DIR}/NSimCode/NSimVar.mo
Expand Down
241 changes: 77 additions & 164 deletions OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo

Large diffs are not rendered by default.

563 changes: 249 additions & 314 deletions OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo
Expand Up @@ -956,7 +956,7 @@ public
// create inst node with dummy variable pointer and create cref from it
node := InstNode.VAR_NODE(RESIDUAL_STR + "_" + name + "_" + intString(uniqueIndex), Pointer.create(DUMMY_VARIABLE));
// Type for residuals is always REAL() !
cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.SCOPE, ComponentRef.EMPTY());
cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
// create variable and set its kind to dae_residual (change name?)
var := fromCref(cref);
// update the variable to be a seed and pass the pointer to the original variable
Expand Down Expand Up @@ -1591,7 +1591,7 @@ public
end if;

// flatten potential records
for var in listReverse(scalar_vars) loop
for var in scalar_vars loop
if Type.isComplex(var.ty) then
flattened := true;
element_vars := Scalarize.scalarizeComplexVariable(var);
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo
Expand Up @@ -37,8 +37,8 @@ encapsulated uniontype NBackendDAE
public
import BVariable = NBVariable;
import BEquation = NBEquation;
import NBEquation.{Equation, EquationPointers, EqData, EquationAttributes, IfEquationBody, Iterator};
import NBVariable.{VariablePointers, VarData};
import NBEquation.{Equation, EquationPointer, EquationPointers, EqData, EquationAttributes, IfEquationBody, Iterator};
import NBVariable.{VariablePointer, VariablePointers, VarData};
import Events = NBEvents;
import NFFlatten.FunctionTree;
import Jacobian = NBJacobian;
Expand Down
71 changes: 35 additions & 36 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo
Expand Up @@ -615,6 +615,41 @@ public
end match;
end nonZeroCount;

function expandMatrix
input output array<list<Integer>> m;
input Integer shift;
algorithm
m := Array.expandToSize(arrayLength(m) + shift, m, {});
end expandMatrix;

function transposeScalar
input array<list<Integer>> m "original matrix";
input Integer size "size of the transposed matrix (does not have to be square!)";
output array<list<Integer>> mT "transposed matrix";
algorithm
mT := arrayCreate(size, {});
// loop over all elements and store them in reverse
for row in 1:arrayLength(m) loop
for idx in m[row] loop
try
if idx > 0 then
mT[idx] := row :: mT[idx];
else
mT[intAbs(idx)] := -row :: mT[intAbs(idx)];
end if;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for variable index " + intString(idx) + ".
The variables have to be dense (without empty spaces) for this to work!"});
end try;
end for;
end for;
// sort the transposed matrix
// bigger to lower such that negative entries are at the and
for row in 1:arrayLength(mT) loop
mT[row] := List.sort(mT[row], intLt);
end for;
end transposeScalar;

protected
function toStringSingle
input array<list<Integer>> m;
Expand Down Expand Up @@ -895,13 +930,6 @@ public
end match;
end fillMatrix;

function expandMatrix
input output array<list<Integer>> m;
input Integer shift;
algorithm
m := Array.expandToSize(arrayLength(m) + shift, m, {});
end expandMatrix;

function cleanMatrix
input array<list<Integer>> m;
input Option<Mapping> mapping_opt;
Expand Down Expand Up @@ -929,34 +957,6 @@ public
end match;
end cleanMatrix;

function transposeScalar
input array<list<Integer>> m "original matrix";
input Integer size "size of the transposed matrix (does not have to be square!)";
output array<list<Integer>> mT "transposed matrix";
algorithm
mT := arrayCreate(size, {});
// loop over all elements and store them in reverse
for row in 1:arrayLength(m) loop
for idx in m[row] loop
try
if idx > 0 then
mT[idx] := row :: mT[idx];
else
mT[intAbs(idx)] := -row :: mT[intAbs(idx)];
end if;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for variable index " + intString(idx) + ".
The variables have to be dense (without empty spaces) for this to work!"});
end try;
end for;
end for;
// sort the transposed matrix
// bigger to lower such that negative entries are at the and
for row in 1:arrayLength(mT) loop
mT[row] := List.sort(mT[row], intLt);
end for;
end transposeScalar;

function createArray
input VariablePointers vars;
input EquationPointers eqns;
Expand Down Expand Up @@ -1026,7 +1026,6 @@ public
arrayUpdate(m, eqn_scal_idx+(i-1), listAppend(m_part[i], m[eqn_scal_idx+(i-1)]));
end for;
end expandRows;

end Matrix;

annotation(__OpenModelica_Interface="backend");
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBDAEMode.mo
Expand Up @@ -105,7 +105,7 @@ protected
// convert all algebraic variables to algebraic states
// BVariable.VariablePointers.mapPtr(syst.unknowns, function BVariable.makeAlgStateVar());
// convert all residual equations to dae residuals
BEquation.EquationPointers.mapPtr(syst.equations, BEquation.Equation.createResidual);
BEquation.EquationPointers.mapPtr(syst.equations, function BEquation.Equation.createResidual(new = false));
syst.unknowns := BEquation.EquationPointers.getResiduals(syst.equations);
new_systems := syst :: new_systems;
end for;
Expand Down
Expand Up @@ -98,7 +98,7 @@ public
then bdae;

else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to create initial system!"});
then fail();
end match;

Expand All @@ -117,7 +117,7 @@ public
end if;
end if;
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to apply modules!"});
end try;
end main;

Expand Down
Expand Up @@ -408,7 +408,7 @@ protected
end collectPartitions;

function collectPartitionsEquation
input Pointer<Equation> eqn;
input output Pointer<Equation> eqn;
input System.SystemType systemType;
input UnorderedMap<ComponentRef, ClusterPointer> map;
protected
Expand Down

0 comments on commit b9042d0

Please sign in to comment.