Skip to content

Commit

Permalink
Make use of singleton uniontypes in the back end
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Jul 4, 2015
1 parent 129db8b commit 1488c5e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 293 deletions.
23 changes: 8 additions & 15 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3924,16 +3924,15 @@ end updateStatesVars;
// =============================================================================

public function addedScaledVars
" added var_norm = var/nominal,
where var is state.
"
"added var_norm = var/nominal, where var is state."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := if Flags.isSet(Flags.ADD_SCALED_VARS) or Flags.isSet(Flags.ADD_SCALED_VARS_INPUT) then
addedScaledVarsWork(inDAE)
else
inDAE;
if Flags.isSet(Flags.ADD_SCALED_VARS) or Flags.isSet(Flags.ADD_SCALED_VARS_INPUT) then
outDAE := addedScaledVarsWork(inDAE);
else
outDAE := inDAE;
end if;
end addedScaledVars;

protected function addedScaledVarsWork
Expand All @@ -3942,24 +3941,18 @@ protected function addedScaledVarsWork
protected
list<BackendDAE.EqSystem> systlst;
list<BackendDAE.EqSystem> osystlst = {};

BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;

BackendDAE.Variables vars;
BackendDAE.Variables knvars;
list<BackendDAE.Var> kvarlst, lst_states, lst_inputs, lst_normv, lst_new_var = {};
list<BackendDAE.Var> kvarlst, lst_states, lst_inputs;
BackendDAE.Var tmpv;
DAE.ComponentRef cref;
DAE.Exp norm, y_norm, y, lhs;
BackendDAE.Equation eqn;
BackendDAE.Shared oshared;
BackendDAE.EqSystem syst;

algorithm
BackendDAE.DAE(systlst, oshared) := inDAE;
BackendDAE.SHARED(knownVars=knvars) := oshared;
kvarlst := BackendVariable.varList(knvars);
kvarlst := BackendVariable.varList(oshared.knownVars);
lst_inputs := List.select(kvarlst, BackendVariable.isVarOnTopLevelAndInputNoDerInput);
// states
if Flags.isSet(Flags.ADD_SCALED_VARS) then
Expand Down
77 changes: 28 additions & 49 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -1257,11 +1257,8 @@ public function equationsAddDAE "author: Frenkel TUD 2011-05"
input list<BackendDAE.Equation> inEquations;
input BackendDAE.EqSystem inEqSystem;
output BackendDAE.EqSystem outEqSystem;
protected
BackendDAE.EquationArray eqs;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=eqs) := inEqSystem;
outEqSystem := BackendDAEUtil.setEqSystEqs(inEqSystem, List.fold(inEquations, addEquation, eqs));
outEqSystem := BackendDAEUtil.setEqSystEqs(inEqSystem, List.fold(inEquations, addEquation, inEqSystem.orderedEqs));
outEqSystem := BackendDAEUtil.setEqSystMatching(outEqSystem, BackendDAE.NO_MATCHING());
end equationsAddDAE;

Expand All @@ -1272,24 +1269,19 @@ public function requationsAddDAE "author: Frenkel TUD 2012-10
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
algorithm
outShared := match (inEquations, inShared)
local
BackendDAE.EquationArray eqs;
case ({}, _)
then inShared;
case (_, BackendDAE.SHARED(removedEqs=eqs)) equation
then BackendDAEUtil.setSharedRemovedEqns(inShared, List.fold(inEquations, addEquation, eqs));
outShared := match inEquations
case {} then inShared;
else then BackendDAEUtil.setSharedRemovedEqns(inShared, List.fold(inEquations, addEquation, inShared.removedEqs));
end match;
end requationsAddDAE;

public function removeRemovedEqs "remove removedEqs"
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
protected
BackendDAE.EquationArray removedEqs;
BackendDAE.EquationArray removedEqs = inShared.removedEqs;
Integer N;
algorithm
BackendDAE.SHARED(removedEqs=removedEqs) := inShared;
N := removedEqs.numberOfElement;
for i in 1:N loop
removedEqs := equationRemove(i, removedEqs);
Expand Down Expand Up @@ -1410,25 +1402,23 @@ public function equationDelete "author: Frenkel TUD 2010-12
input list<Integer> inIndices;
output BackendDAE.EquationArray outEquationArray;
algorithm
outEquationArray := matchcontinue (inEquationArray, inIndices)
outEquationArray := matchcontinue inIndices
local
list<BackendDAE.Equation> eqnlst;
Integer numberOfElement, arrSize;
array<Option<BackendDAE.Equation>> equOptArr;

case (_, {})
case {}
then inEquationArray;

case (BackendDAE.EQUATION_ARRAY(arrSize=arrSize, equOptArr=equOptArr), _) equation
equOptArr = List.fold1r(inIndices, arrayUpdate, NONE(), equOptArr);
eqnlst = equationDelete1(arrSize, equOptArr, {});
case _ equation
equOptArr = List.fold1r(inIndices, arrayUpdate, NONE(), inEquationArray.equOptArr);
eqnlst = equationDelete1(inEquationArray.arrSize, equOptArr, {});
then listEquation(eqnlst);

else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("- BackendDAE.equationDelete failed\n");
then fail();
else equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("- BackendDAE.equationDelete failed\n");
then fail();
end matchcontinue;
end equationDelete;

Expand Down Expand Up @@ -1746,14 +1736,11 @@ algorithm
end equationInfo;

public function markedEquationSource
input BackendDAE.EqSystem syst;
input BackendDAE.EqSystem inEqSystem;
input Integer inPos "one-based indexing";
output DAE.ElementSource outSource;
protected
BackendDAE.EquationArray eqns;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=eqns) := syst;
outSource := equationSource(equationNth1(eqns, inPos));
outSource := equationSource(equationNth1(inEqSystem.orderedEqs, inPos));
end markedEquationSource;

public function equationSource "
Expand All @@ -1770,9 +1757,9 @@ algorithm
case BackendDAE.ALGORITHM(source=source) then source;
case BackendDAE.COMPLEX_EQUATION(source=source) then source;
case BackendDAE.IF_EQUATION(source=source) then source;
else
equation Error.addInternalError("BackendEquation.equationSource failed!", sourceInfo());
then fail();
else equation
Error.addInternalError("BackendEquation.equationSource failed!", sourceInfo());
then fail();
end match;
end equationSource;

Expand Down Expand Up @@ -1978,7 +1965,7 @@ algorithm
case BackendDAE.FOR_EQUATION(attr=attr) then attr;

else equation
Error.addInternalError("./Compiler/BackEnd/BackendEquation.mo: function getEquationAttributes failed", sourceInfo());
Error.addInternalError("function getEquationAttributes failed", sourceInfo());
then fail();
end match;
end getEquationAttributes;
Expand Down Expand Up @@ -2028,7 +2015,7 @@ algorithm
then BackendDAE.IF_EQUATION(conditions, eqnstrue, eqnsfalse, source, inAttr);

else equation
Error.addInternalError("./Compiler/BackEnd/BackendEquation.mo: function setEquationAttributes failed", sourceInfo());
Error.addInternalError("function setEquationAttributes failed", sourceInfo());
then fail();
end match;
end setEquationAttributes;
Expand Down Expand Up @@ -2063,7 +2050,7 @@ algorithm
then BackendDAE.ARRAY_EQUATION(dimSize, lhs, rhs, source, attr);

else equation
Error.addInternalError("./Compiler/BackEnd/BackendEquation.mo: function setEquationLHS failed", sourceInfo());
Error.addInternalError("function setEquationLHS failed", sourceInfo());
then fail();
end match;
end setEquationLHS;
Expand Down Expand Up @@ -2104,7 +2091,7 @@ algorithm
then BackendDAE.RESIDUAL_EQUATION(rhs, source, attr);

else equation
Error.addInternalError("./Compiler/BackEnd/BackendEquation.mo: function setEquationRHS failed", sourceInfo());
Error.addInternalError("function setEquationRHS failed", sourceInfo());
then fail();
end match;
end setEquationRHS;
Expand Down Expand Up @@ -2392,7 +2379,7 @@ algorithm
*/
else equation
BackendDump.dumpBackendDAEEqnList({eqn}, "function BackendEquation.solveEquation failed w.r.t " + ExpressionDump.printExpStr(crefExp), true);
Error.addInternalError("./Compiler/BackEnd/BackendEquation.mo: function solveEquation failed", sourceInfo());
Error.addInternalError("function solveEquation failed", sourceInfo());
then fail();
end matchcontinue;
end solveEquation;
Expand All @@ -2409,17 +2396,13 @@ end generateRESIDUAL_EQUATION;
public function getEqnsFromEqSystem "
Extracts the orderedEqs attribute from an equation system."
input BackendDAE.EqSystem inEqSystem;
output BackendDAE.EquationArray outOrderedEqs;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=outOrderedEqs) := inEqSystem;
output BackendDAE.EquationArray outOrderedEqs = inEqSystem.orderedEqs;
end getEqnsFromEqSystem;

public function getInitialEqnsFromShared "
Extracts the initial equations from a shared object."
input BackendDAE.Shared inShared;
output BackendDAE.EquationArray outInitialEqs;
algorithm
BackendDAE.SHARED(initialEqs=outInitialEqs) := inShared;
output BackendDAE.EquationArray outInitialEqs = inShared.initialEqs;
end getInitialEqnsFromShared;

public function aliasEquation "author Frenkel TUD 2011-04
Expand Down Expand Up @@ -2961,13 +2944,9 @@ end markDifferentiated;

protected function markDifferentiated2
input BackendDAE.EquationAttributes inAttr;
output BackendDAE.EquationAttributes outAttr;
protected
BackendDAE.EquationKind kind;
BackendDAE.LoopInfo loopInfo;
output BackendDAE.EquationAttributes outAttr = inAttr;
algorithm
BackendDAE.EQUATION_ATTRIBUTES(kind=kind, loopInfo=loopInfo) := inAttr;
outAttr := BackendDAE.EQUATION_ATTRIBUTES(true, kind, loopInfo);
outAttr.differentiated := true;
end markDifferentiated2;

public function isDifferentiated
Expand Down

0 comments on commit 1488c5e

Please sign in to comment.