Skip to content

Commit

Permalink
[NF] Fix DAE conversion of Operator.SUB_ARRAY_SCALAR.
Browse files Browse the repository at this point in the history
- Convert array .- scalar to array +- (scalar) when converting to DAE,
  since the DAE doesn't have the corresponding subtraction operator.
  • Loading branch information
perost committed Aug 18, 2020
1 parent 3631e42 commit 20e4ce4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -1841,7 +1841,7 @@ public
local
Type ty;
DAE.Operator daeOp;
Boolean swap;
Boolean swap, negate;
DAE.Exp dae1, dae2;
list<String> names;
Function.Function fn;
Expand Down Expand Up @@ -1890,9 +1890,9 @@ public

case BINARY()
algorithm
(daeOp, swap) := Operator.toDAE(exp.operator);
(daeOp, swap, negate) := Operator.toDAE(exp.operator);
dae1 := toDAE(exp.exp1);
dae2 := toDAE(exp.exp2);
dae2 := toDAE(if negate then Expression.negate(exp.exp2) else exp.exp2);
then
DAE.BINARY(if swap then dae2 else dae1, daeOp, if swap then dae1 else dae2);

Expand Down
6 changes: 4 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFOperator.mo
Expand Up @@ -140,7 +140,8 @@ public
function toDAE
input Operator op;
output DAE.Operator daeOp;
output Boolean swapArguments=false "The DAE structure only has array*scalar, not scalar*array, etc";
output Boolean swapArguments = false "The DAE structure only has array*scalar, not scalar*array, etc";
output Boolean negate = false "The second argument should be negated.";
protected
DAE.Type ty;
algorithm
Expand All @@ -154,7 +155,8 @@ public
case Op.ADD_SCALAR_ARRAY algorithm swapArguments := true; then DAE.ADD_ARRAY_SCALAR(ty);
case Op.ADD_ARRAY_SCALAR then DAE.ADD_ARRAY_SCALAR(ty);
case Op.SUB_SCALAR_ARRAY then DAE.SUB_SCALAR_ARRAY(ty);
case Op.SUB_ARRAY_SCALAR algorithm Error.addInternalError(getInstanceName() + ": Don't know how to handle " + String(op.op), sourceInfo()); then DAE.SUB(ty);
// array .- scalar is handled as array .+ (-scalar)
case Op.SUB_ARRAY_SCALAR algorithm negate := true; then DAE.ADD_ARRAY_SCALAR(ty);
case Op.MUL_SCALAR_ARRAY algorithm swapArguments := true; then DAE.MUL_ARRAY_SCALAR(ty);
case Op.MUL_ARRAY_SCALAR then DAE.MUL_ARRAY_SCALAR(ty);
case Op.MUL_VECTOR_MATRIX then DAE.MUL_MATRIX_PRODUCT(ty);
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -683,6 +683,7 @@ OperationPowEW1.mo \
OperationRelational1.mo \
OperationSub1.mo \
OperationSubEW1.mo \
OperationSubEW2.mo \
OperationUnary1.mo \
OperationVectorMatrixProduct1.mo \
OperationVectorProduct1.mo \
Expand Down
40 changes: 40 additions & 0 deletions testsuite/flattening/modelica/scodeinst/OperationSubEW2.mo
@@ -0,0 +1,40 @@
// name: OperationSubEW2
// keywords:
// status: correct
// cflags: -d=newInst
//

function f
input Real x;
input Real y[:];
output Real z[:];
algorithm
z := x .- y;
z := y .- x;
end f;

model OperationSubEW2
Real r1;
Real r2[2];
equation
f(r1, r2);
end OperationSubEW2;

// Result:
// function f
// input Real x;
// input Real[:] y;
// output Real[:] z;
// algorithm
// z := x .- y;
// z := y .+ (-x);
// end f;
//
// class OperationSubEW2
// Real r1;
// Real r2[1];
// Real r2[2];
// equation
// f(r1, r2);
// end OperationSubEW2;
// endResult

0 comments on commit 20e4ce4

Please sign in to comment.