Skip to content

Commit 00a66c0

Browse files
authored
Make type restriction enumeration, cleanup stuff (#14962)
1 parent 4a96d9b commit 00a66c0

File tree

8 files changed

+85
-113
lines changed

8 files changed

+85
-113
lines changed

OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public
549549
(new_residuals, dae_type) := slicedDAEModeComponent(comp.strict.iteration_vars, comp.strict.residual_eqns, variables, uniqueIndex, slice_set);
550550
then (new_residuals, dae_type);
551551

552-
else ({}, if StrongComponent.isDiscrete(comp) then DAEType.REMOVED else DAEType.INNER);
552+
else ({}, if isDiscrete(comp) then DAEType.REMOVED else DAEType.INNER);
553553
end match;
554554

555555
if dae_type == DAEType.RESIDUAL then
@@ -907,8 +907,8 @@ public
907907
output list<Pointer<Variable>> residuals;
908908
algorithm
909909
residuals := match comp
910-
case ALGEBRAIC_LOOP() then Tearing.getResidualVars(comp.strict);
911-
else {};
910+
case ALGEBRAIC_LOOP() then Tearing.getResidualVars(comp.strict);
911+
else {};
912912
end match;
913913
end getLoopResiduals;
914914

@@ -917,14 +917,14 @@ public
917917
output list<Pointer<Variable>> vars;
918918
algorithm
919919
vars := match comp
920-
case SINGLE_COMPONENT() then {comp.var};
921-
case MULTI_COMPONENT() then list(Slice.getT(v) for v in comp.vars);
922-
case SLICED_COMPONENT() then {Slice.getT(comp.var)};
923-
case RESIZABLE_COMPONENT()then {Slice.getT(comp.var)};
924-
case GENERIC_COMPONENT() then {Slice.getT(comp.var)};
925-
case ENTWINED_COMPONENT() then List.flatten(list(getVariables(slice) for slice in comp.entwined_slices));
926-
case ALGEBRAIC_LOOP() then Tearing.getVariables(comp.strict);
927-
case ALIAS() then getVariables(comp.original);
920+
case SINGLE_COMPONENT() then {comp.var};
921+
case MULTI_COMPONENT() then list(Slice.getT(v) for v in comp.vars);
922+
case SLICED_COMPONENT() then {Slice.getT(comp.var)};
923+
case RESIZABLE_COMPONENT() then {Slice.getT(comp.var)};
924+
case GENERIC_COMPONENT() then {Slice.getT(comp.var)};
925+
case ENTWINED_COMPONENT() then List.flatten(list(getVariables(slice) for slice in comp.entwined_slices));
926+
case ALGEBRAIC_LOOP() then Tearing.getVariables(comp.strict);
927+
case ALIAS() then getVariables(comp.original);
928928
else algorithm
929929
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
930930
then fail();
@@ -936,10 +936,10 @@ public
936936
output ComponentRef var_cref;
937937
algorithm
938938
var_cref := match comp
939-
case SLICED_COMPONENT() then comp.var_cref;
940-
case RESIZABLE_COMPONENT()then comp.var_cref;
941-
case GENERIC_COMPONENT() then comp.var_cref;
942-
case ALIAS() then getVarCref(comp.original);
939+
case SLICED_COMPONENT() then comp.var_cref;
940+
case RESIZABLE_COMPONENT() then comp.var_cref;
941+
case GENERIC_COMPONENT() then comp.var_cref;
942+
case ALIAS() then getVarCref(comp.original);
943943
else algorithm
944944
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
945945
then fail();
@@ -951,14 +951,14 @@ public
951951
output list<Pointer<Equation>> eqns;
952952
algorithm
953953
eqns := match comp
954-
case SINGLE_COMPONENT() then {comp.eqn};
955-
case MULTI_COMPONENT() then {Slice.getT(comp.eqn)};
956-
case SLICED_COMPONENT() then {Slice.getT(comp.eqn)};
957-
case RESIZABLE_COMPONENT()then {Slice.getT(comp.eqn)};
958-
case GENERIC_COMPONENT() then {Slice.getT(comp.eqn)};
959-
case ENTWINED_COMPONENT() then List.flatten(list(getEquations(slice) for slice in comp.entwined_slices));
960-
case ALGEBRAIC_LOOP() then Tearing.getResidualEqns(comp.strict); // + inner?
961-
case ALIAS() then getEquations(comp.original);
954+
case SINGLE_COMPONENT() then {comp.eqn};
955+
case MULTI_COMPONENT() then {Slice.getT(comp.eqn)};
956+
case SLICED_COMPONENT() then {Slice.getT(comp.eqn)};
957+
case RESIZABLE_COMPONENT() then {Slice.getT(comp.eqn)};
958+
case GENERIC_COMPONENT() then {Slice.getT(comp.eqn)};
959+
case ENTWINED_COMPONENT() then List.flatten(list(getEquations(slice) for slice in comp.entwined_slices));
960+
case ALGEBRAIC_LOOP() then Tearing.getResidualEqns(comp.strict); // + inner?
961+
case ALIAS() then getEquations(comp.original);
962962
else algorithm
963963
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
964964
then fail();
@@ -970,14 +970,14 @@ public
970970
output Solve.Status status;
971971
algorithm
972972
status := match comp
973-
case SINGLE_COMPONENT() then comp.status;
974-
case MULTI_COMPONENT() then comp.status;
975-
case SLICED_COMPONENT() then comp.status;
976-
case RESIZABLE_COMPONENT()then comp.status;
977-
case GENERIC_COMPONENT() then NBSolve.Status.EXPLICIT;
978-
case ENTWINED_COMPONENT() then NBSolve.Status.EXPLICIT;
979-
case ALGEBRAIC_LOOP() then comp.status;
980-
case ALIAS() then getSolveStatus(comp.original);
973+
case SINGLE_COMPONENT() then comp.status;
974+
case MULTI_COMPONENT() then comp.status;
975+
case SLICED_COMPONENT() then comp.status;
976+
case RESIZABLE_COMPONENT() then comp.status;
977+
case GENERIC_COMPONENT() then NBSolve.Status.EXPLICIT;
978+
case ENTWINED_COMPONENT() then NBSolve.Status.EXPLICIT;
979+
case ALGEBRAIC_LOOP() then comp.status;
980+
case ALIAS() then getSolveStatus(comp.original);
981981
else algorithm
982982
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
983983
then fail();
@@ -990,14 +990,14 @@ public
990990
output Boolean b;
991991
algorithm
992992
b := match comp
993-
case SINGLE_COMPONENT() then Equation.isDiscrete(comp.eqn);
994-
case MULTI_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
995-
case SLICED_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
996-
case RESIZABLE_COMPONENT()then Equation.isDiscrete(Slice.getT(comp.eqn));
997-
case ENTWINED_COMPONENT() then List.all(comp.entwined_slices, isDiscrete);
998-
case GENERIC_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
999-
case ALGEBRAIC_LOOP() then comp.mixed;
1000-
case ALIAS() then isDiscrete(comp.original);
993+
case SINGLE_COMPONENT() then Equation.isDiscrete(comp.eqn);
994+
case MULTI_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
995+
case SLICED_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
996+
case RESIZABLE_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
997+
case ENTWINED_COMPONENT() then List.all(comp.entwined_slices, isDiscrete);
998+
case GENERIC_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
999+
case ALGEBRAIC_LOOP() then comp.mixed;
1000+
case ALIAS() then isDiscrete(comp.original);
10011001
else algorithm
10021002
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
10031003
then fail();

OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ public
12201220
list<Pointer<Variable>> arg_children;
12211221
algorithm
12221222
subscripts := ComponentRef.subscriptsAllFlat(cref);
1223-
arg_children := BVariable.getRecordChildren(getVarPointer(cref, sourceInfo()));
1223+
arg_children := getRecordChildren(getVarPointer(cref, sourceInfo()));
12241224
children := list(ComponentRef.mergeSubscripts(subscripts, getVarName(child), true, true) for child in arg_children);
12251225
end getRecordChildrenCref;
12261226

@@ -1285,7 +1285,7 @@ public
12851285
Variable pre;
12861286
case qual as InstNode.VAR_NODE()
12871287
algorithm
1288-
var_ptr := BVariable.getVarPointer(cref, sourceInfo());
1288+
var_ptr := getVarPointer(cref, sourceInfo());
12891289
qual.name := PREVIOUS_STR;
12901290
pre_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
12911291
pre := fromCref(pre_cref, Variable.attributes(Pointer.access(var_ptr)));
@@ -1456,7 +1456,7 @@ public
14561456
case qual as InstNode.VAR_NODE()
14571457
algorithm
14581458
// get the variable pointer from the old cref to later on link back to it
1459-
old_var_ptr := BVariable.getVarPointer(cref, sourceInfo());
1459+
old_var_ptr := getVarPointer(cref, sourceInfo());
14601460
// prepend the start str
14611461
qual.name := START_STR;
14621462
start_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));
@@ -1621,7 +1621,7 @@ public
16211621
case qual as InstNode.VAR_NODE()
16221622
algorithm
16231623
// get the variable pointer from the old cref to later on link back to it
1624-
old_var_ptr := BVariable.getVarPointer(cref, sourceInfo());
1624+
old_var_ptr := getVarPointer(cref, sourceInfo());
16251625
// prepend the tmp str
16261626
qual.name := TEMPORARY_STR;
16271627
tmp_cref := ComponentRef.append(cref, ComponentRef.fromNode(qual, ComponentRef.scalarType(cref)));

OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,14 @@ public
265265

266266
function getResidualVars
267267
input Tearing tearing;
268-
output list<Pointer<Variable>> residuals;
269-
algorithm
270-
residuals := list(Equation.getResidualVar(Slice.getT(eqn)) for eqn in tearing.residual_eqns);
268+
output list<Pointer<Variable>> residuals = list(Equation.getResidualVar(Slice.getT(eqn)) for eqn in tearing.residual_eqns);
271269
end getResidualVars;
272270

273271
function getIterationVars
274272
input Tearing tearing;
275-
output list<Pointer<Variable>> iterationVars;
276-
algorithm
277-
iterationVars := list(Slice.getT(var) for var in tearing.iteration_vars);
273+
output list<Pointer<Variable>> iterationVars = list(Slice.getT(var) for var in tearing.iteration_vars);
278274
end getIterationVars;
279-
275+
280276
function getResidualEqns
281277
input Tearing tearing;
282278
output list<Pointer<Equation>> residuals = list(Slice.getT(eqn) for eqn in tearing.residual_eqns);

OMCompiler/Compiler/NFFrontEnd/NFDimension.mo

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,14 @@ public
270270
"Returns the sizes of the given dimension sizes."
271271
input list<Dimension> dims;
272272
input Boolean resize = false;
273-
output list<Integer> outSizes;
274-
algorithm
275-
outSizes := list(Dimension.size(d, resize) for d in dims);
273+
output list<Integer> outSizes = list(Dimension.size(d, resize) for d in dims);
276274
end sizes;
277275

278276
function sizesProduct
279277
"Returns the product of the given dimension sizes."
280278
input list<Dimension> dims;
281279
input Boolean resize = false;
282-
output Integer outSize = 1;
283-
algorithm
284-
for dim in dims loop
285-
outSize := outSize * Dimension.size(dim, resize);
286-
end for;
280+
output Integer outSize = product(Dimension.size(d, resize) for d in dims);
287281
end sizesProduct;
288282

289283
function isEqual

OMCompiler/Compiler/NFFrontEnd/NFOperator.mo

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,22 @@ public
135135
end match;
136136
end invert;
137137

138+
type TypeRestriction = enumeration(SCALAR, VECTOR, MATRIX, ARRAY, OTHER);
139+
138140
function typeRestriction
139141
input Type ty;
140-
output Integer i;
142+
output TypeRestriction restriction;
141143
algorithm
142144
if Type.isScalar(ty) then
143-
i := 0;
145+
restriction := TypeRestriction.SCALAR;
144146
elseif Type.isVector(ty) then
145-
i := 1;
147+
restriction := TypeRestriction.VECTOR;
146148
elseif Type.isMatrix(ty) then
147-
i := 2;
149+
restriction := TypeRestriction.MATRIX;
148150
elseif Type.isArray(ty) then
149-
i := 3;
151+
restriction := TypeRestriction.ARRAY;
150152
else
151-
i := 4;
153+
restriction := TypeRestriction.OTHER;
152154
end if;
153155
end typeRestriction;
154156

@@ -158,25 +160,25 @@ public
158160
protected
159161
MathClassification mc = getMathClassification(operator);
160162
SizeClassification sc;
161-
list<tuple<Integer, Type>> lst;
162-
tuple<Integer, Type> min_, max_;
163+
list<tuple<TypeRestriction, Type>> lst;
164+
tuple<TypeRestriction, Type> min_, max_;
163165
Type ty;
164166
function tplLt
165-
input tuple<Integer, Type> tpl1;
166-
input tuple<Integer, Type> tpl2;
167+
input tuple<TypeRestriction, Type> tpl1;
168+
input tuple<TypeRestriction, Type> tpl2;
167169
output Boolean b = Util.tuple21(tpl1) < Util.tuple21(tpl2);
168170
end tplLt;
169171
algorithm
170172
lst := list((typeRestriction(t), t) for t in types);
171173
min_ := List.minElement(lst, tplLt);
172174
max_ := List.maxElement(lst, tplLt);
173175
(sc, ty) := match (min_, max_)
174-
case ((0, _), (0, ty)) then (SizeClassification.SCALAR, ty);
175-
case ((0, _), (_ ,ty)) then (SizeClassification.SCALAR_ARRAY, ty);
176-
case ((1, _), (1, ty)) then (SizeClassification.ELEMENT_WISE, ty);
177-
case ((1, _), (2, ty)) then (SizeClassification.VECTOR_MATRIX, ty);
178-
case ((2, _), (2, ty)) then (SizeClassification.ELEMENT_WISE, ty);
179-
case ((3, _), (3, ty)) then (SizeClassification.ELEMENT_WISE, ty);
176+
case ((TypeRestriction.SCALAR, _), (TypeRestriction.SCALAR, ty)) then (SizeClassification.SCALAR, ty);
177+
case ((TypeRestriction.SCALAR, _), (_ ,ty)) then (SizeClassification.SCALAR_ARRAY, ty);
178+
case ((TypeRestriction.VECTOR, _), (TypeRestriction.VECTOR, ty)) then (SizeClassification.ELEMENT_WISE, ty);
179+
case ((TypeRestriction.VECTOR, _), (TypeRestriction.MATRIX, ty)) then (SizeClassification.VECTOR_MATRIX, ty);
180+
case ((TypeRestriction.MATRIX, _), (TypeRestriction.MATRIX, ty)) then (SizeClassification.ELEMENT_WISE, ty);
181+
case ((TypeRestriction.ARRAY, _), (TypeRestriction.ARRAY, ty)) then (SizeClassification.ELEMENT_WISE, ty);
180182
else algorithm
181183
Error.assertion(false, getInstanceName() + " failed because the multary arguments have incompatible sizes: "
182184
+ List.toString(types, Type.toString), sourceInfo());
@@ -196,13 +198,13 @@ public
196198
algorithm
197199
(sc, ty) := match (typeRestriction(ty1), typeRestriction(ty2))
198200
local
199-
Integer i1, i2;
200-
case (0, 0) then (SizeClassification.SCALAR, ty1);
201-
case (0, i2) guard(i2>0) then (SizeClassification.SCALAR_ARRAY, ty2);
202-
case (i1, 0) guard(i1>0) then (SizeClassification.ARRAY_SCALAR, ty1);
203-
case (1, 2) then (SizeClassification.VECTOR_MATRIX, ty1);
204-
case (2, 1) then (SizeClassification.MATRIX_VECTOR, ty2);
205-
case (i1, i2) guard(i1==i2) then (getSizeClassification(operator), ty1);
201+
TypeRestriction r1, r2;
202+
case (TypeRestriction.SCALAR, TypeRestriction.SCALAR) then (SizeClassification.SCALAR, ty1);
203+
case (TypeRestriction.SCALAR, r2) guard(r2>TypeRestriction.SCALAR) then (SizeClassification.SCALAR_ARRAY, ty2);
204+
case (r1, TypeRestriction.SCALAR) guard(r1>TypeRestriction.SCALAR) then (SizeClassification.ARRAY_SCALAR, ty1);
205+
case (TypeRestriction.VECTOR, TypeRestriction.MATRIX) then (SizeClassification.VECTOR_MATRIX, ty1);
206+
case (TypeRestriction.MATRIX, TypeRestriction.VECTOR) then (SizeClassification.MATRIX_VECTOR, ty2);
207+
case (r1, r2) guard(r1 == r2) then (getSizeClassification(operator), ty1);
206208
else algorithm
207209
Error.assertion(false, getInstanceName() + " failed because the binary arguments have incompatible sizes: "
208210
+ Type.toString(ty1) + ", " + Type.toString(ty2), sourceInfo());
@@ -216,10 +218,10 @@ public
216218
output Boolean b;
217219
algorithm
218220
b := match operator.op
219-
case Op.AND then true;
220-
case Op.OR then true;
221-
case Op.NOT then true;
222-
else false;
221+
case Op.AND then true;
222+
case Op.OR then true;
223+
case Op.NOT then true;
224+
else false;
223225
end match;
224226
end isLogical;
225227

@@ -987,16 +989,15 @@ public
987989
input MathClassification mcl2;
988990
output Boolean b;
989991
algorithm
990-
b := (Util.intCompare(Integer(mcl1), Integer(mcl2)) == 0)
991-
or (isDashClassification(mcl1) and isDashClassification(mcl2));
992+
b := mcl1 == mcl2 or (isDashClassification(mcl1) and isDashClassification(mcl2));
992993
end isCombineableMath;
993994

994995
function isCombineableSize
995996
input SizeClassification scl1;
996997
input SizeClassification scl2;
997998
output Boolean b;
998999
algorithm
999-
b := (Util.intCompare(Integer(scl1), Integer(scl2)) == 0);
1000+
b := scl1 == scl2;
10001001
end isCombineableSize;
10011002

10021003
function toDebugString

OMCompiler/Compiler/NSimCode/NSimCodeUtil.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public
9292
input list<SimVar> simVars;
9393
input output UnorderedMap<ComponentRef, SimVar> simcode_map;
9494
algorithm
95+
// TODO write function UnorderedMap.addList to avoid rehashes
9596
for var in simVars loop
9697
UnorderedMap.add(SimVar.getName(var), var, simcode_map);
9798
end for;

OMCompiler/Compiler/NSimCode/NSimJacobian.mo

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public
8484
Integer numColors "number of colors";
8585
list<SimGenericCall> generic_loop_calls "Generic for-loop and array calls";
8686
Option<UnorderedMap<ComponentRef, SimVar>> jac_map "hash table for cref -> simVar";
87-
Boolean isAdjoint "indicates if this is an adjoint jacobian";
87+
Boolean isAdjoint "indicates if this is an adjoint jacobian";
8888
end SIM_JAC;
8989

9090
function toString
@@ -449,15 +449,9 @@ public
449449
dep_indices := List.map(dependencies, function UnorderedMap.getOrFail(map = idx_map));
450450
simPattern := (UnorderedMap.getOrFail(cref, idx_map), List.sort(dep_indices, intGt)) :: simPattern;
451451
end for;
452-
simPattern := List.sort(simPattern, sparsityTplSortGt);
452+
simPattern := List.sort(simPattern, Util.compareTupleIntGt);
453453
end createSparsityPattern;
454454

455-
function sparsityTplSortGt
456-
input tuple<Integer, list<Integer>> col1 "or row1";
457-
input tuple<Integer, list<Integer>> col2 "or row2";
458-
output Boolean b = Util.tuple21(col1) > Util.tuple21(col2);
459-
end sparsityTplSortGt;
460-
461455
function createSparsityColoring
462456
input Jacobian.SparsityColoring coloring;
463457
input UnorderedMap<ComponentRef, Integer> idx_map;

0 commit comments

Comments
 (0)