Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Improve module wrapFunctionCalls
Browse files Browse the repository at this point in the history
- constant cse-variables are stored in globalKnownVars and only
  evaluated once before simulation instead of in each simulation step
- also traverse globalKnownVars for function calls before
  traversing the systems
- SimEqns for parameters are created from the globalKnownVars
- constant variables (eg. cse-variables) are solved before initialization in
  updateBoundParameters in *_08bnd.c
- primaryParameters and allPrimaryParameters are removed
- improved dumps and documentation
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Mar 21, 2017
1 parent 52cd6ad commit a919ef4
Show file tree
Hide file tree
Showing 31 changed files with 993 additions and 243 deletions.
5 changes: 3 additions & 2 deletions Compiler/BackEnd/BackendDAE.mo
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ uniontype Var "variables"
.DAE.VarDirection varDirection "input, output or bidirectional";
.DAE.VarParallelism varParallelism "parallelism of the variable. parglobal, parlocal or non-parallel";
Type varType "built-in type or enumeration";
Option< .DAE.Exp> bindExp "Binding expression e.g. for parameters";
Option<.DAE.Exp> bindExp "Binding expression e.g. for parameters";
Option<.DAE.Exp> tplExp "Variable is part of a tuple. Needed for the globalKnownVars and localKnownVars";
.DAE.InstDims arryDim "array dimensions of non-expanded var";
.DAE.ElementSource source "origin of variable";
Option< .DAE.VariableAttributes> values "values on built-in attributes";
Option<.DAE.VariableAttributes> values "values on built-in attributes";
Option<TearingSelect> tearingSelectOption "value for TearingSelect";
.DAE.Exp hideResult "expression from the hideResult annotation";
Option<SCode.Comment> comment "this contains the comment and annotation from Absyn";
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ algorithm
ts = BackendDAEUtil.setTearingSelectAttribute(comment);
hideResult = BackendDAEUtil.setHideResultAttribute(comment, b, name);
then
(BackendDAE.VAR(name, kind_1, dir, prl, tp, NONE(), dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false));
(BackendDAE.VAR(name, kind_1, dir, prl, tp, NONE(), NONE(), dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false));
end match;
end lowerDynamicVar;

Expand Down Expand Up @@ -862,7 +862,7 @@ algorithm
ts = NONE();
hideResult = BackendDAEUtil.setHideResultAttribute(comment, b, name);
then
(BackendDAE.VAR(name, kind_1, dir, prl, tp, bind, dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false), iInlineHT, eqLst);
(BackendDAE.VAR(name, kind_1, dir, prl, tp, bind, NONE(), dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false), iInlineHT, eqLst);

else
equation
Expand Down Expand Up @@ -1181,7 +1181,7 @@ algorithm
ts = NONE();
hideResult = DAE.BCONST(false);
then
BackendDAE.VAR(name, kind_1, dir, prl, tp, bind, dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false);
BackendDAE.VAR(name, kind_1, dir, prl, tp, bind, NONE(), dims, source, dae_var_attr, ts, hideResult, comment, ct, DAEUtil.toDAEInnerOuter(io), false);
end match;
end lowerExtObjVar;

Expand Down Expand Up @@ -1845,7 +1845,7 @@ algorithm
outVars := BackendDAE.VAR (
varName = cr, varKind = BackendDAE.VARIABLE(),
varDirection = DAE.BIDIR(), varParallelism = DAE.NON_PARALLEL(),
varType = DAE.T_CLOCK_DEFAULT, bindExp = NONE(),
varType = DAE.T_CLOCK_DEFAULT, bindExp = NONE(), tplExp = NONE(),
arryDim = {}, source = DAE.emptyElementSource,
values = NONE(), tearingSelectOption = SOME(BackendDAE.DEFAULT()), hideResult = DAE.BCONST(false),
comment = NONE(), connectorType = DAE.NON_CONNECTOR(),
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5543,7 +5543,7 @@ protected
algorithm
(BackendDAE.DAE(eqs, shared), _) := BackendDAEUtil.mapEqSystemAndFold(inDAE, addTimeAsState1, 0);
orderedVars := BackendVariable.emptyVars();
var := BackendDAE.VAR(DAE.crefTimeState, BackendDAE.STATE(1, NONE()), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true);
var := BackendDAE.VAR(DAE.crefTimeState, BackendDAE.STATE(1, NONE()), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true);
var := BackendVariable.setVarFixed(var, true);
var := BackendVariable.setVarStartValue(var, DAE.CREF(DAE.crefTime, DAE.T_REAL_DEFAULT));
orderedVars := BackendVariable.addVar(var, orderedVars);
Expand Down
70 changes: 62 additions & 8 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ protected
algorithm
name := Expression.reductionIterName(iter);
cr := ComponentReference.makeCrefIdent(name,DAE.T_INTEGER_DEFAULT,{});
backendVar := BackendDAE.VAR(cr, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_INTEGER_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
backendVar := BackendDAE.VAR(cr, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_INTEGER_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
end makeIterVariable;

protected function checkEquationSize"author: Frenkel TUD 2010-12
Expand Down Expand Up @@ -6364,26 +6364,27 @@ algorithm
Boolean unreplaceable;
String name;
Option<BackendDAE.Var> v;
Option<DAE.Exp> tplExp;

case NONE()
then (NONE(), inTypeA);

case SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, SOME(e1), instdims, source, attr, ts, hideResult, comment, ct, io, unreplaceable)) equation
case SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, SOME(e1), tplExp, instdims, source, attr, ts, hideResult, comment, ct, io, unreplaceable)) equation
(e1_, ext_arg_1) = func(e1, inTypeA);
(attr_, ext_arg_2) = traverseBackendDAEVarAttr(attr, func, ext_arg_1);
if referenceEq(e1,e1_) and referenceEq(attr,attr_) then
v = inVar;
else
v = SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, SOME(e1_), instdims, source, attr_, ts, hideResult, comment, ct, io, unreplaceable));
v = SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, SOME(e1_), tplExp, instdims, source, attr_, ts, hideResult, comment, ct, io, unreplaceable));
end if;
then (v, ext_arg_2);

case SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, NONE(), instdims, source, attr, ts, hideResult, comment, ct, io, unreplaceable)) equation
case SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, NONE(), tplExp, instdims, source, attr, ts, hideResult, comment, ct, io, unreplaceable)) equation
(attr_, ext_arg_2) = traverseBackendDAEVarAttr(attr, func, inTypeA);
if referenceEq(attr,attr_) then
v = inVar;
else
v = SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, NONE(), instdims, source, attr_, ts, hideResult, comment, ct, io, unreplaceable));
v = SOME(BackendDAE.VAR(cref, varKind, varDirection, varParallelism, varType, NONE(), tplExp, instdims, source, attr_, ts, hideResult, comment, ct, io, unreplaceable));
end if;
then (v, ext_arg_2);

Expand Down Expand Up @@ -6793,8 +6794,6 @@ public function getSolvedSystem "Run the equation system pipeline."
output Boolean outUseHomotopy "true if homotopy(...) is used during initialization";
output Option<BackendDAE.BackendDAE> outInitDAE_lambda0;
output list<BackendDAE.Equation> outRemovedInitialEquationLst;
output list<BackendDAE.Var> outPrimaryParameters "already sorted";
output list<BackendDAE.Var> outAllPrimaryParameters "already sorted";
protected
BackendDAE.BackendDAE dae, simDAE;
list<tuple<BackendDAEFunc.optimizationModule, String>> preOptModules;
Expand Down Expand Up @@ -6851,7 +6850,7 @@ algorithm
end if;

// generate system for initialization
(outInitDAE, outUseHomotopy, outInitDAE_lambda0, outRemovedInitialEquationLst, outPrimaryParameters, outAllPrimaryParameters, globalKnownVars) := Initialization.solveInitialSystem(dae);
(outInitDAE, outUseHomotopy, outInitDAE_lambda0, outRemovedInitialEquationLst, globalKnownVars) := Initialization.solveInitialSystem(dae);

// use function tree from initDAE further for simDAE
simDAE := BackendDAEUtil.setFunctionTree(dae, BackendDAEUtil.getFunctions(outInitDAE.shared));
Expand All @@ -6868,6 +6867,9 @@ algorithm
// post-optimization phase
outSimDAE := postOptimizeDAE(simDAE, postOptModules, matchingAlgorithm, daeHandler);

// sort the globalKnownVars
outSimDAE := sortGlobalKnownVarsInDAE(outSimDAE);

if Flags.isSet(Flags.DUMP_INDX_DAE) then
BackendDump.dumpBackendDAE(outSimDAE, "dumpindxdae");
if Flags.isSet(Flags.ADDITIONAL_GRAPHVIZ_DUMP) then
Expand Down Expand Up @@ -7242,6 +7244,58 @@ algorithm
//fcall2(Flags.DUMP_EQNINORDER, BackendDump.dumpEqnsSolved, outDAE, "system for jacobians");
end getSolvedSystemforJacobians;

protected function sortGlobalKnownVarsInDAE "
author: ptaeuber
This function adds the external objects to globalKnownVars and sorts the globalKnownVars"
input output BackendDAE.BackendDAE backendDAE;
protected
BackendDAE.Variables globalKnownVars, globalKnownVars_sorted;
BackendDAE.EquationArray parameterEqns;
BackendDAE.EqSystem paramSystem;
BackendDAE.IncidenceMatrix m;
BackendDAE.Var var;
array<Integer> ass1, ass2;
list<list<Integer>> comps;
list<Integer> flatComps;
algorithm
globalKnownVars := backendDAE.shared.globalKnownVars;
globalKnownVars := BackendVariable.addVariables(backendDAE.shared.externalObjects, globalKnownVars);
parameterEqns := BackendEquation.emptyEqns();
parameterEqns := BackendVariable.traverseBackendDAEVars(globalKnownVars, createParameterEquations, parameterEqns);

paramSystem := BackendDAEUtil.createEqSystem(globalKnownVars, parameterEqns);
(m, _) := BackendDAEUtil.incidenceMatrix(paramSystem, BackendDAE.NORMAL(), NONE());
(ass1, ass2) := Matching.PerfectMatching(m);
comps := Sorting.Tarjan(m, ass1);
flatComps := list(Initialization.flattenParamComp(comp, globalKnownVars) for comp in comps);

globalKnownVars_sorted := BackendVariable.emptyVars();
for i in flatComps loop
var := BackendVariable.getVarAt(globalKnownVars, i);
globalKnownVars_sorted := BackendVariable.addVar(var, globalKnownVars_sorted);
end for;

backendDAE := setDAEGlobalKnownVars(backendDAE, globalKnownVars_sorted);
end sortGlobalKnownVarsInDAE;

protected function createParameterEquations
input output BackendDAE.Var var;
input output BackendDAE.EquationArray parameterEqns;
protected
DAE.Exp lhs, rhs;
BackendDAE.Equation eqn;
algorithm
lhs := BackendVariable.varExp(var);
try
rhs := BackendVariable.varBindExpStartValue(var);
else
rhs := DAE.RCONST(0.0);
end try;
eqn := BackendDAE.EQUATION(lhs, rhs, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
parameterEqns := BackendEquation.addEquation(eqn, parameterEqns);
end createParameterEquations;


/*************************************************
* index reduction method Selection
************************************************/
Expand Down
3 changes: 2 additions & 1 deletion Compiler/BackEnd/BackendDump.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,8 @@ algorithm
unreplaceableStr := if inVar.unreplaceable then " unreplaceable" else "";
dimensions := ExpressionDump.dimensionsString(inVar.arryDim);
dimensions := if dimensions <> "" then " [" + dimensions + "]" else "";
outStr := DAEDump.dumpDirectionStr(inVar.varDirection) + ComponentReference.printComponentRefStr(inVar.varName) + ":"
outStr := DAEDump.dumpDirectionStr(inVar.varDirection) + ComponentReference.printComponentRefStr(inVar.varName)
+ (if isSome(inVar.tplExp) then " in " + ExpressionDump.printExpStr(Util.getOption(inVar.tplExp)) else "") + ":"
+ kindString(inVar.varKind) + "(" + connectorTypeString(inVar.connectorType) + attributesString(inVar.values)
+ ") " + optExpressionString(inVar.bindExp, "") + DAEDump.dumpCommentAnnotationStr(inVar.comment)
+ stringDelimitList(paths_lst, ", ") + " type: " + DAEDump.daeTypeStr(inVar.varType) + dimensions + unreplaceableStr;
Expand Down
12 changes: 6 additions & 6 deletions Compiler/BackEnd/BackendEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2301,7 +2301,7 @@ algorithm

case (_, DAE.RELATION(e1, DAE.LESS(_), e2, _, _)) equation
lhs = ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
rhs = Expression.expSub(e1,e2);
(rhs, _) = ExpressionSimplify.simplify1(rhs);
expNull = DAE.RCONST(0.0);
Expand All @@ -2311,7 +2311,7 @@ algorithm

case (_, DAE.RELATION(e1, DAE.LESSEQ(_), e2, _, _)) equation
lhs = ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
rhs = Expression.expSub(e1,e2);
(rhs, _) = ExpressionSimplify.simplify1(rhs);
expNull = DAE.RCONST(0.0);
Expand All @@ -2321,7 +2321,7 @@ algorithm

case (_, DAE.RELATION(e1, DAE.GREATER(_), e2, _, _)) equation
lhs = ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
rhs = Expression.expSub(e2,e1);
(rhs, _) = ExpressionSimplify.simplify1(rhs);
expNull = DAE.RCONST(0.0);
Expand All @@ -2331,7 +2331,7 @@ algorithm

case (_, DAE.RELATION(e1, DAE.GREATEREQ(_), e2, _, _)) equation
lhs = ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
rhs = Expression.expSub(e2,e1);
(rhs, _) = ExpressionSimplify.simplify(rhs);
expNull = DAE.RCONST(0.0);
Expand All @@ -2341,7 +2341,7 @@ algorithm

case (_, DAE.RELATION(e1, DAE.EQUAL(_), e2, _, _)) equation
lhs = ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar = BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
rhs = Expression.expSub(e2,e1);
(rhs, _) = ExpressionSimplify.simplify(rhs);
expNull = DAE.RCONST(0.0);
Expand All @@ -2356,7 +2356,7 @@ algorithm
end try;

lhs := ComponentReference.makeCrefIdent(conCrefName, DAE.T_REAL_DEFAULT, {});
dummyVar := BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar := BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.BCONST(false), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
dummyVar := BackendVariable.mergeAliasVars(dummyVar, v, false, knvars);
eqn := BackendDAE.SOLVED_EQUATION(lhs, e1, Source, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);

Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/BackendInline.mo
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,15 @@ algorithm
DAE.ConnectorType ct;
DAE.ElementSource source;
Option<DAE.Exp> bind;
Option<DAE.Exp> tplExp;
Boolean b1,b2;
DAE.VarInnerOuter io;
Boolean unreplaceable;

case BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,arrayDim,source,values,ts,hideResult,comment,ct,io,unreplaceable) equation
case BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,tplExp,arrayDim,source,values,ts,hideResult,comment,ct,io,unreplaceable) equation
(bind,source,b1) = Inline.inlineExpOpt(bind,inElementList,source);
(values1,source,b2) = Inline.inlineStartAttribute(values,source,inElementList);
then (BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,arrayDim,source,values1,ts,hideResult,comment,ct,io,unreplaceable), b1 or b2);
then (BackendDAE.VAR(varName,varKind,varDirection,varParallelism,varType,bind,tplExp,arrayDim,source,values1,ts,hideResult,comment,ct,io,unreplaceable), b1 or b2);

else (inVar, false);
end match;
Expand Down

0 comments on commit a919ef4

Please sign in to comment.