Skip to content

Commit

Permalink
[janitor] Cleanup (#10954)
Browse files Browse the repository at this point in the history
  • Loading branch information
phannebohm committed Aug 31, 2023
1 parent 773177f commit c2604cd
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 93 deletions.
48 changes: 13 additions & 35 deletions OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5350,23 +5350,22 @@ protected function replaceDerCallWork
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared = shared;
protected
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Equation> eqnsList;
algorithm
osyst := match inSyst
local
BackendDAE.EqSystem syst;
BackendDAE.Variables localKnowns;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
case syst as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns)
algorithm
(eqns, vars) :=
BackendEquation.traverseEquationArray_WithUpdate(eqns, traverserreplaceDerCall, vars);
(localKnowns, vars) := BackendVariable.traverseBackendDAEVars(vars,
moveStatesVariables, (oshared.localKnownVars, vars));
oshared.localKnownVars := localKnowns;
syst.orderedEqs := eqns; syst.orderedVars := vars;
syst.orderedEqs := eqns;
syst.orderedVars := vars;
then syst;
end match;
end replaceDerCallWork;
Expand All @@ -5379,7 +5378,6 @@ protected function traverserreplaceDerCall "
output BackendDAE.Variables outVars = inVars;
protected
BackendDAE.Equation e;
BackendDAE.Variables vars;
list<DAE.SymbolicOperation> ops;
algorithm
(e, ops) := BackendEquation.traverseExpsOfEquation(inEq, traverserreplaceDerCallExp, {});
Expand All @@ -5394,20 +5392,12 @@ protected function traverserreplaceDerCallExp "
output list<DAE.SymbolicOperation> outTpl;
protected
DAE.Exp e, e1;
tuple<BackendDAE.Variables, Boolean> ext_arg;
BackendDAE.Variables vars;
list<DAE.SymbolicOperation> ops;
DAE.FunctionTree funcs;
Boolean b, addVars;
BackendDAE.Shared shared;
list<BackendDAE.Equation> eqnLst;
Boolean b;
algorithm
e := inExp;
ops := tpl;
(e1, b) := Expression.traverseExpBottomUp(e, replaceDerCall, false);
ops := List.consOnTrue(b, DAE.SUBSTITUTION({e1}, e), ops);
outTpl := List.consOnTrue(b, DAE.SUBSTITUTION({e1}, e), tpl);
outExp := e1;
outTpl := ops;
end traverserreplaceDerCallExp;

protected function replaceDerCall "
Expand All @@ -5419,28 +5409,16 @@ protected function replaceDerCall "
algorithm
(outExp,tpl) := matchcontinue (inExp, itpl)
local
BackendDAE.Variables vars;
DAE.ComponentRef cr,cref;
DAE.Type ty;
String str;
BackendDAE.Shared shared;
list<BackendDAE.Var> varlst;
BackendDAE.Var v, v1;
Boolean b, addVar;
DAE.FunctionTree funcs;
list<BackendDAE.Equation> eqnLst;
Integer numVars;
list<DAE.Exp> expLst;
String str;

case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr, ty=ty)}), _) equation
cref = ComponentReference.crefPrefixDer(cr);
outExp = DAE.CREF(cref,ty);
then (outExp, true);

case (DAE.CALL(path=Absyn.IDENT(name="der")), _) equation
str = "BackendDAEOptimize.replaceDerCall failed for: " + ExpressionDump.printExpStr(inExp) + "\n";
Error.addMessage(Error.INTERNAL_ERROR, {str});
Error.addMessage(Error.INTERNAL_ERROR, {getInstanceName() + " failed for: " + ExpressionDump.printExpStr(inExp) + "\n"});
then fail();

else (inExp, itpl);
Expand All @@ -5453,11 +5431,11 @@ protected function moveStatesVariables
output BackendDAE.Var outVar = inVar;
output tuple<BackendDAE.Variables, BackendDAE.Variables> outTpl = inTpl;
algorithm
_ := match(inVar)
local
DAE.ComponentRef cref;
BackendDAE.Var newVar;
BackendDAE.Variables localKnowns, newVars;
() := match(inVar)
local
DAE.ComponentRef cref;
BackendDAE.Var newVar;
BackendDAE.Variables localKnowns, newVars;
case(BackendDAE.VAR(varKind = BackendDAE.STATE(), varName=cref)) algorithm
(localKnowns, newVars) := inTpl;
// remove the state from variables
Expand All @@ -5472,7 +5450,7 @@ algorithm
newVars := BackendVariable.addVar(newVar, newVars);
outTpl := (localKnowns, newVars);
then ();
else then();
else ();
end match;
end moveStatesVariables;

Expand Down
9 changes: 3 additions & 6 deletions OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -5468,22 +5468,19 @@ algorithm
local
DAE.ComponentRef cr;
DAE.Type ty;
String str;
BackendDAE.Var v;
list<DAE.Exp> expLst;
case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr, ty=ty)}))
equation
v = BackendVariable.createAliasDerVar(cr);
cr = BackendVariable.varCref(v);
outExp = DAE.CREF(cr,ty);
then (outExp);
then (outExp);
case (DAE.CALL(path=Absyn.IDENT(name="der")))
equation
str = "BackendDAEUtil.replaceDerCall failed for: " + ExpressionDump.printExpStr(inExp) + "\n";
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
Error.addMessage(Error.INTERNAL_ERROR, {getInstanceName() + " failed for: " + ExpressionDump.printExpStr(inExp) + "\n"});
then fail();
else (inExp);
end matchcontinue;
Expand Down
12 changes: 5 additions & 7 deletions OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -3444,18 +3444,16 @@ algorithm
end setVarKindForStates;

protected function findDerVarCrefs "traverses all the sub expressions and searches for der(var)"
input DAE.Exp inExp;
input output DAE.Exp exp;
input list<DAE.ComponentRef> inCrefs;
output DAE.Exp outExp;
output list<DAE.ComponentRef> outCrefs;
algorithm
(outExp,outCrefs) := match(inExp,inCrefs)
outCrefs := match exp
local
DAE.ComponentRef cr;
tuple<DAE.Exp,list<DAE.ComponentRef>> tpl;
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),_)
then (inExp,cr::inCrefs);
else (inExp,inCrefs);
case DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)})
then cr :: inCrefs;
else inCrefs;
end match;
end findDerVarCrefs;

Expand Down
46 changes: 28 additions & 18 deletions OMCompiler/Compiler/FrontEnd/Expression.mo
Expand Up @@ -917,7 +917,8 @@ public function prependSubscriptExp
algorithm
outExp := match(exp,subscr)
local
Type t; ComponentRef cr,cr1,cr2;
Type t;
ComponentRef cr, cr1, cr2;
list<DAE.Subscript> subs;
DAE.Exp e;

Expand Down Expand Up @@ -7718,43 +7719,39 @@ public function isPositiveOrZero
input DAE.Exp inExp;
output Boolean outBoolean;
algorithm


outBoolean := match (inExp)
local
Boolean b,b1,b2,b3;
DAE.Exp e1,e2;
Integer i;
Real r;
/* abs(e) */
case DAE.CALL(path = Absyn.IDENT("abs")) then true;
// exp(x)
case DAE.CALL(path = Absyn.IDENT("exp")) then true;
// cosh(x)
case DAE.CALL(path = Absyn.IDENT("cosh")) then true;
/* literals */

/* literals */
case DAE.ICONST(i) then i >= 0;
case DAE.RCONST(r) then r >= 0.0;
/* e1 + e2 */

/* e1 + e2 */
case DAE.BINARY(e1,DAE.ADD(),e2)
then isPositiveOrZero(e1) and isPositiveOrZero(e2);
/* e1 - e2 */
/* e1 - e2 */
case DAE.BINARY(e1,DAE.SUB(),e2)
then isPositiveOrZero(e1) and isNegativeOrZero(e2);
/* e1 * e2 , -e1 * -e2, e ^ 2.0 */

/* e1 * e2 , -e1 * -e2, e ^ 2.0 */
case DAE.BINARY(e1,DAE.MUL(),e2)
equation
b1 = (isPositiveOrZero(e1) and isPositiveOrZero(e2));
b2 = (isNegativeOrZero(e1) and isNegativeOrZero(e2));
b3 = expEqual(e1,e2);
then b1 or b2 or b3;
/* e1 / e2, -e1 / -e2 */
/* e1 / e2, -e1 / -e2 */
case DAE.BINARY(e1,DAE.DIV(),e2)
equation
b1 = (isPositiveOrZero(e1) and isPositiveOrZero(e2));
b2 = (isNegativeOrZero(e1) and isNegativeOrZero(e2));
then b1 or b2;
/* Integer power we can say something good about */

/* Integer power we can say something good about */
case DAE.BINARY(e1,DAE.POW(),DAE.RCONST(r))
equation
i = realInt(r);
Expand All @@ -7764,10 +7761,24 @@ algorithm
b = b2 or b3;
then b1 and b;
case DAE.BINARY(_,DAE.POW(),e2) then isEven(e2);
// -(x)

/* -(x) */
case DAE.UNARY(DAE.UMINUS(), e1) then isNegativeOrZero(e1);
else isZero(inExp);

/* builtin calls */
case DAE.CALL(path = Absyn.IDENT("abs")) then true;
case DAE.CALL(path = Absyn.IDENT("cosh")) then true;
case DAE.CALL(path = Absyn.IDENT("exp")) then true;
case DAE.CALL(path = Absyn.IDENT("sign"), expLst = {e1}) then isPositiveOrZero(e1);
case DAE.CALL(path = Absyn.IDENT("sinh"), expLst = {e1}) then isPositiveOrZero(e1);
case DAE.CALL(path = Absyn.IDENT("tanh"), expLst = {e1}) then isPositiveOrZero(e1);
case DAE.CALL(path = Absyn.IDENT("ceil"), expLst = {e1}) then isPositiveOrZero(e1);
case DAE.CALL(path = Absyn.IDENT("floor"), expLst = {e1}) then isPositiveOrZero(e1);
case DAE.CALL(path = Absyn.IDENT("integer"), expLst = {e1}) then isPositiveOrZero(e1);

// TODO div, mod, rem, ...

else isZero(inExp);
end match;
end isPositiveOrZero;

Expand All @@ -7787,7 +7798,6 @@ algorithm
case DAE.BINARY(_,DAE.POW(),e2) guard(isOdd(e2)) then isNegativeOrZero(e2);

else isZero(inExp);

end match;
end isNegativeOrZero;

Expand Down
6 changes: 1 addition & 5 deletions OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo
Expand Up @@ -351,11 +351,7 @@ public

function size
input Iterator iter;
output Integer size = 1;
algorithm
for i in sizes(iter) loop
size := size * i;
end for;
output Integer size = product(i for i in sizes(iter));
end size;

function createLocationReplacements
Expand Down
7 changes: 3 additions & 4 deletions OMCompiler/Compiler/SimCode/SimCodeMain.mo
@@ -1,7 +1,7 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
Expand Down Expand Up @@ -108,7 +108,6 @@ import Util;
import SerializeTaskSystemInfo;

public
constant Boolean debug=true;
uniontype TranslateModelKind
record NORMAL
end NORMAL;
Expand Down Expand Up @@ -310,7 +309,7 @@ algorithm
else
setGlobalRoot(Global.stackoverFlowIndex, NONE());
ErrorExt.rollbackNumCheckpoints(ErrorExt.getNumCheckpoints()-numCheckpoints);
Error.addInternalError("Stack overflow in "+getInstanceName()+"...\n"+stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
Error.addInternalError("Stack overflow in " + getInstanceName() + "...\n" + stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
/* Do not fail or we can loop too much */
StackOverflow.clearStacktraceMessages();
end try annotation(__OpenModelica_stackOverflowCheckpoint=true);
Expand Down Expand Up @@ -428,7 +427,7 @@ algorithm
else
setGlobalRoot(Global.stackoverFlowIndex, NONE());
ErrorExt.rollbackNumCheckpoints(ErrorExt.getNumCheckpoints()-numCheckpoints);
Error.addInternalError("Stack overflow in "+getInstanceName()+"...\n"+stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
Error.addInternalError("Stack overflow in " + getInstanceName() + "...\n" + stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
/* Do not fail or we can loop too much */
StackOverflow.clearStacktraceMessages();
fail();
Expand Down
7 changes: 0 additions & 7 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -15419,13 +15419,6 @@ algorithm
end match;
end getNumContinuousEquationsSingleEq;

public function sortCrefBasedOnSimCodeIndex
input output list<DAE.ComponentRef> crs;
input SimCode.SimCode simCode;
algorithm
crs := List.sort(crs, function crefSimCodeIndexGreaterThan(simCode=simCode));
end sortCrefBasedOnSimCodeIndex;

protected function crefSimCodeIndexGreaterThan
input DAE.ComponentRef cr1, cr2;
input SimCode.SimCode simCode;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -330,7 +330,7 @@ template functionInitSynchronous(list<ClockedPartition> clockedPartitions, Strin
void <%symbolName(modelNamePrefix,"function_initSynchronous")%>(DATA *data, threadData_t *threadData)
{
TRACE_PUSH
assertStreamPrint(threadData, data->modelData->nBaseClocks==<%listLength(clockedPartitions)%>, "Number of base clocks doens't match numer of clocks that are initialized! Code generation error!");
assertStreamPrint(threadData, data->modelData->nBaseClocks==<%listLength(clockedPartitions)%>, "Number of base clocks doesn't match number of clocks that are initialized! Code generation error!");
data->simulationInfo->baseClocks = calloc(<%listLength(clockedPartitions)%>, sizeof(BASECLOCK_DATA));

<%body%>
Expand Down
6 changes: 0 additions & 6 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -1383,12 +1383,6 @@ package SimCodeUtil
output list<SimCode.SimEqSystem> eqs;
end sortSimpleAssignmentBasedOnLhs;

function sortCrefBasedOnSimCodeIndex
input list<DAE.ComponentRef> inCrefs;
input SimCode.SimCode simCode;
output list<DAE.ComponentRef> crs;
end sortCrefBasedOnSimCodeIndex;

function getNumContinuousEquations
input list<SimCode.SimEqSystem> eqs;
input Integer numStates;
Expand Down
3 changes: 0 additions & 3 deletions OMCompiler/Compiler/Util/Util.mo
Expand Up @@ -602,7 +602,6 @@ algorithm
outValue := match(inValue)
local
TI value;
TO res;

case SOME(value) then inFunc(value);
else inDefaultValue;
Expand All @@ -628,7 +627,6 @@ algorithm
outValue := match(inValue)
local
TI value;
TO res;

case SOME(value) then inFunc(value, inArg);
else inDefaultValue;
Expand Down Expand Up @@ -656,7 +654,6 @@ algorithm
outValue := match(inValue)
local
TI value;
TO res;

case SOME(value) then inFunc(value, inArg1, inArg2);
else inDefaultValue;
Expand Down
Expand Up @@ -583,7 +583,7 @@ int cvode_solver_initial(DATA *data, threadData_t *threadData, SOLVER_INFO *solv
flag = CVodeSetErrHandlerFn(cvodeData->cvode_mem, cvodeErrorHandlerFunction, cvodeData);
checkReturnFlag_SUNDIALS(flag, SUNDIALS_CV_FLAG, "CVodeSetErrHandlerFn");

/* Set linear solver useb by CVODE */
/* Set linear solver used by CVODE */
cvodeData->y_linSol = N_VNew_Serial(cvodeData->N);
switch (cvodeData->config.jacobianMethod)
{
Expand Down

0 comments on commit c2604cd

Please sign in to comment.