Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergero committed Oct 13, 2015
2 parents e88115a + 1eff681 commit 9648600
Show file tree
Hide file tree
Showing 44 changed files with 660 additions and 521 deletions.
2 changes: 1 addition & 1 deletion 3rdParty
9 changes: 6 additions & 3 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -243,6 +243,9 @@ uniontype VarKind "variable kind"
record STATE_DER end STATE_DER;
record DUMMY_DER end DUMMY_DER;
record DUMMY_STATE end DUMMY_STATE;
record CLOCKED_STATE
.DAE.ComponentRef previousName "the name of the previous variable";
end CLOCKED_STATE;
record DISCRETE end DISCRETE;
record PARAM end PARAM;
record CONST end CONST;
Expand Down Expand Up @@ -725,13 +728,13 @@ uniontype DifferentiateInputData
Option<Variables> dependenentVars; // Dependent variables
Option<Variables> knownVars; // known variables (e.g. parameter, constants, ...)
Option<Variables> allVars; // all variables
Option<list< Var>> controlVars; // variables to save control vars of for algorithm
Option<list< .DAE.ComponentRef>> diffCrefs; // all crefs to differentiate, needed for generic gradient
list< Var> controlVars; // variables to save control vars of for algorithm
list< .DAE.ComponentRef> diffCrefs; // all crefs to differentiate, needed for generic gradient
Option<String> matrixName; // name to create temporary vars, needed for generic gradient
end DIFFINPUTDATA;
end DifferentiateInputData;

public constant DifferentiateInputData noInputData = DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
public constant DifferentiateInputData emptyInputData = DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),{},{},NONE());

public
type DifferentiateInputArguments = tuple< .DAE.ComponentRef, DifferentiateInputData, DifferentiationType, .DAE.FunctionTree>;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -917,7 +917,7 @@ algorithm

else
algorithm
false := BackendVariable.topLevelInput(inComponentRef, inVarDirection, inConnectorType);
false := DAEUtil.topLevelInput(inComponentRef, inVarDirection, inConnectorType);
then
match (inVarKind, inType)
case (DAE.VARIABLE(), DAE.T_BOOL()) then BackendDAE.DISCRETE();
Expand All @@ -944,7 +944,7 @@ algorithm
case (DAE.CONST(), _, _, _) then BackendDAE.CONST();
case (DAE.VARIABLE(), _, _, _)
equation
true = BackendVariable.topLevelInput(inComponentRef, inVarDirection, inConnectorType);
true = DAEUtil.topLevelInput(inComponentRef, inVarDirection, inConnectorType);
then
BackendDAE.VARIABLE();
// adrpo: topLevelInput might fail!
Expand Down
17 changes: 11 additions & 6 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4200,10 +4200,15 @@ protected
DAE.CallAttributes cattr;
DAE.ComponentRef cr;
BackendDAE.Var tmpvar;

String tmpVarPrefix;
algorithm

shared := inDAE.shared;
tmpVarPrefix := match shared
case BackendDAE.SHARED(backendDAEType=BackendDAE.SIMULATION()) then "$OMC$CF$sim";
case BackendDAE.SHARED(backendDAEType=BackendDAE.INITIALSYSTEM()) then "$OMC$CF$init";
else fail();
end match;

for syst in inDAE.eqs loop
BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns) := syst;
BackendDAE.EQUATION_ARRAY(numberOfElement = n) := eqns;
Expand Down Expand Up @@ -4238,7 +4243,7 @@ algorithm
DAE.CREF(componentRef = cr) := e1;
if Expression.expHasCrefNoPreOrStart(right, cr) then
update := true;
cr := ComponentReference.makeCrefIdent("$OMC$CF$" + intString(idx), Expression.typeof(e1) , {});
cr := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx), Expression.typeof(e1) , {});
idx := idx + 1;
e := Expression.crefExp(cr);
tmpvar := BackendVariable.makeVar(cr);
Expand All @@ -4252,7 +4257,7 @@ algorithm
end if;
elseif Expression.isUnaryCref(e1) then
update := true;
cr := ComponentReference.makeCrefIdent("$OMC$CF$" + intString(idx), Expression.typeof(e1) , {});
cr := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx), Expression.typeof(e1) , {});
idx := idx + 1;
e := Expression.crefExp(cr);
tmpvar := BackendVariable.makeVar(cr);
Expand All @@ -4265,7 +4270,7 @@ algorithm
update := true;
DAE.ARRAY(array=arrayLst, scalar=sc) := e1;
m := listLength(arrayLst);
cr := ComponentReference.makeCrefIdent("$OMC$CF$" + intString(idx), Expression.typeof(e1) , {});
cr := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx), Expression.typeof(e1) , {});
idx := idx + 1;
e := Expression.crefExp(cr);
tmpvar := BackendVariable.makeVar(cr);
Expand All @@ -4279,7 +4284,7 @@ algorithm
eqn1 := BackendDAE.EQUATION(e2, e3, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
//print(BackendDump.equationString(eqn1) + "--new--\n");
eqns := BackendEquation.addEquation(eqn1, eqns);
cr := ComponentReference.makeCrefIdent("$OMC$CF$" + intString(idx-1), Expression.typeof(e1) , {DAE.INDEX(DAE.ICONST(j))});
cr := ComponentReference.makeCrefIdent(tmpVarPrefix + intString(idx-1), Expression.typeof(e1) , {DAE.INDEX(DAE.ICONST(j))});
j := j + 1;
tmpvar.varName := cr;
vars := BackendVariable.addVar(tmpvar, vars);
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -2309,6 +2309,7 @@ algorithm
case BackendDAE.STATE_DER() then "STATE_DER";
case BackendDAE.DUMMY_DER() then "DUMMY_DER";
case BackendDAE.DUMMY_STATE() then "DUMMY_STATE";
case BackendDAE.CLOCKED_STATE() then "CLOCKED_STATE";
case BackendDAE.DISCRETE() then "DISCRETE";
case BackendDAE.PARAM() then "PARAM";
case BackendDAE.CONST() then "CONST";
Expand Down
64 changes: 17 additions & 47 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -818,6 +818,9 @@ algorithm
list<BackendDAE.VarKind> kind_lst;

/* Real non discrete variable */
case (BackendDAE.VAR(varKind = BackendDAE.CLOCKED_STATE(_)))
then true;

case (BackendDAE.VAR(varKind = kind, varType = DAE.T_REAL(_,_))) equation
kind_lst = {BackendDAE.VARIABLE(), BackendDAE.DUMMY_DER(), BackendDAE.DUMMY_STATE(), BackendDAE.OPT_INPUT_WITH_DER(), BackendDAE.OPT_INPUT_DER()};
then listMember(kind, kind_lst) or isOptLoopInput(kind);
Expand Down Expand Up @@ -1692,51 +1695,22 @@ algorithm
DAE.VarDirection dir;
DAE.ConnectorType ct;
case (BackendDAE.VAR(varName = cr,varDirection = dir,connectorType = ct))
then topLevelOutput(cr, dir, ct);
then DAEUtil.topLevelOutput(cr, dir, ct);
end match;
end isVarOnTopLevelAndOutput;

public function isVarOnTopLevelAndInput "and has the DAE.VarDirection = INPUT
The check for top-model is done by splitting the name at '.' and checking if
the list-length is 1."
input BackendDAE.Var inVar;
output Boolean outBoolean = topLevelInput(inVar.varName, inVar.varDirection, inVar.connectorType);
output Boolean outBoolean = DAEUtil.topLevelInput(inVar.varName, inVar.varDirection, inVar.connectorType);
end isVarOnTopLevelAndInput;

public function isVarOnTopLevelAndInputNoDerInput
input BackendDAE.Var inVar;
output Boolean outBoolean = isVarOnTopLevelAndInput(inVar) and not isRealOptimizeDerInput(inVar);
end isVarOnTopLevelAndInputNoDerInput;

public function topLevelInput "author: PA
Succeeds if variable is input declared at the top level of the model,
or if it is an input in a connector instance at top level."
input DAE.ComponentRef inComponentRef;
input DAE.VarDirection inVarDirection;
input DAE.ConnectorType inConnectorType;
output Boolean outTopLevelInput;
algorithm
outTopLevelInput := match (inComponentRef,inVarDirection,inConnectorType)
case (DAE.CREF_IDENT(), DAE.INPUT(), _) then true;
case (DAE.CREF_QUAL(componentRef = DAE.CREF_IDENT()), DAE.INPUT(), DAE.FLOW()) then true;
case (DAE.CREF_QUAL(componentRef = DAE.CREF_IDENT()), DAE.INPUT(), DAE.POTENTIAL()) then true;
else false;
end match;
end topLevelInput;

protected function topLevelOutput
input DAE.ComponentRef inComponentRef;
input DAE.VarDirection inVarDirection;
input DAE.ConnectorType inConnectorType;
output Boolean outTopLevelOutput;
algorithm
outTopLevelOutput := match(inComponentRef, inVarDirection, inConnectorType)
case (DAE.CREF_IDENT(), DAE.OUTPUT(), _) then true;
case (DAE.CREF_QUAL(componentRef = DAE.CREF_IDENT()), DAE.OUTPUT(), DAE.FLOW()) then true;
case (DAE.CREF_QUAL(componentRef = DAE.CREF_IDENT()), DAE.OUTPUT(), DAE.POTENTIAL()) then true;
else false;
end match;
end topLevelOutput;


public function isFinalVar "Returns true if the variable is final."
Expand Down Expand Up @@ -2231,26 +2205,22 @@ public function isTopLevelInputOrOutput "author: LP
vars: Variables, /* BackendDAE.Variables */
knownVars: BackendDAE.Variables /* Known BackendDAE.Variables */)
outputs: bool"
input DAE.ComponentRef inComponentRef1;
input BackendDAE.Variables inVariables2;
input BackendDAE.Variables inVariables3;
input DAE.ComponentRef inComponentRef;
input BackendDAE.Variables inVars;
input BackendDAE.Variables inKnVars;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inComponentRef1,inVariables2,inVariables3)
outBoolean := matchcontinue inComponentRef
local
DAE.ComponentRef cr;
BackendDAE.Variables vars,knvars;
case (cr,vars,_)
equation
((BackendDAE.VAR(varName = DAE.CREF_IDENT(), varDirection = DAE.OUTPUT()) :: _),_) = getVar(cr, vars);
then
true;
case (cr,_,knvars)
equation
((BackendDAE.VAR(varDirection = DAE.INPUT()) :: _),_) = getVar(cr, knvars) "input variables stored in known variables are input on top level";
then
true;
case (_,_,_) then false;
BackendDAE.Var v;
case _
equation (v::_, _) = getVar(inComponentRef, inVars);
then isVarOnTopLevelAndOutput(v);
case _
equation (v::_, _) = getVar(inComponentRef, inKnVars);
then isVarOnTopLevelAndInput(v);
else false;
end matchcontinue;
end isTopLevelInputOrOutput;

Expand Down
24 changes: 11 additions & 13 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -101,7 +101,7 @@ algorithm
try
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getknvars(inShared);
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), SOME({}), NONE(), NONE());
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), {}, {}, NONE());
(outEquation, funcs) := differentiateEquation(inEquation, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
else
Expand All @@ -128,7 +128,7 @@ algorithm
try
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getknvars(inShared);
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), SOME({}), NONE(), NONE());
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), SOME(inVariables), {}, {}, NONE());
(dexp, funcs) := differentiateExp(inExp, DAE.crefTime, diffData, BackendDAE.DIFFERENTIATION_TIME(), funcs, defaultMaxIter, {});
(outExp, _) := ExpressionSimplify.simplify(dexp);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
Expand All @@ -154,7 +154,6 @@ public function differentiateExpSolve
protected
list<DAE.Exp> fac = Expression.factors(inExp);
DAE.Exp dexp;
BackendDAE.DifferentiateInputData diffData;
DAE.FunctionTree fun;
algorithm
({}, _) := List.split1OnTrue(fac, Expression.expHasCrefInIf, inCref); // check if differentiateExpSolve is allowed
Expand All @@ -167,8 +166,7 @@ algorithm
else DAE.emptyFuncTree;
end match;

diffData := BackendDAE.DIFFINPUTDATA(NONE(), NONE(), NONE(), NONE(), SOME({}), NONE(), NONE());
(dexp, _) := differentiateExp(inExp, inCref, diffData, BackendDAE.SIMPLE_DIFFERENTIATION(), fun, defaultMaxIter, {});
(dexp, _) := differentiateExp(inExp, inCref, BackendDAE.emptyInputData, BackendDAE.SIMPLE_DIFFERENTIATION(), fun, defaultMaxIter, {});
(outExp, _) := ExpressionSimplify.simplify(dexp);
else
if Flags.isSet(Flags.FAILTRACE) then
Expand Down Expand Up @@ -196,7 +194,7 @@ algorithm
try
funcs := BackendDAEUtil.getFunctions(inShared);
knvars := BackendDAEUtil.getknvars(inShared);
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), NONE(), SOME({}), NONE(), NONE());
diffData := BackendDAE.DIFFINPUTDATA(NONE(), SOME(inVariables), SOME(knvars), NONE(), {}, {}, NONE());
(dexp, funcs) := differentiateExp(inExp, inCref, diffData, BackendDAE.DIFF_FULL_JACOBIAN(), funcs, defaultMaxIter, {});
(outExp,_) := ExpressionSimplify.simplify(dexp);
outShared := BackendDAEUtil.setSharedFunctionTree(inShared, funcs);
Expand Down Expand Up @@ -2107,7 +2105,7 @@ algorithm
(expl1,_) = List.splitOnBoolList(expl, blst);
(dexpl, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter, inExpStack=expStack), inDiffwrtCref, inInputData, inDiffType, inFunctionTree);
funcname = Util.modelicaStringToCStr(Absyn.pathString(path), false);
diffFuncData = BackendDAE.DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),SOME(funcname));
diffFuncData = BackendDAE.DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),{},{},SOME(funcname));
(dexplZero, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter, inExpStack=expStack), DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), diffFuncData, BackendDAE.GENERIC_GRADIENT(), functions);
//dexpl = listAppend(expl, dexpl);
//print("Start creation of partial Der\n");
Expand Down Expand Up @@ -2175,7 +2173,7 @@ algorithm
expBoolLst = List.filterOnTrue(expBoolLst, Util.tuple22);
expl1 = List.map(expBoolLst, Util.tuple21);
(dexpl, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter, inExpStack=expStack), inDiffwrtCref, inInputData, inDiffType, functions);
(dexplZero, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter, inExpStack=expStack), DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), inInputData, BackendDAE.GENERIC_GRADIENT(), functions);
(dexplZero, functions) = List.map3Fold(expl1, function differentiateExp(maxIter=maxIter, inExpStack=expStack), DAE.CREF_IDENT("$",DAE.T_REAL_DEFAULT,{}), BackendDAE.emptyInputData, BackendDAE.GENERIC_GRADIENT(), functions);
//dexpl = listAppend(expl, dexpl);
//print("Start creation of partial Der\n");
//print("Diffed ExpList: \n");
Expand Down Expand Up @@ -2421,7 +2419,7 @@ algorithm

path = DAEUtil.functionName(func);
funcname = Util.modelicaStringToCStr(Absyn.pathString(path), false);
diffFuncData = BackendDAE.DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),SOME(funcname));
diffFuncData = BackendDAE.DIFFINPUTDATA(NONE(),NONE(),NONE(),NONE(),{},{},SOME(funcname));

(inputVarsDer, functions, inputVarsNoDer, blst) = differentiateElementVars(inputVars, inDiffwrtCref, diffFuncData, BackendDAE.DIFFERENTIATION_FUNCTION(), inFunctionTree, {}, {}, {}, maxIter, expStack);
(outputVarsDer, functions, outputVarsNoDer, _) = differentiateElementVars(outputVars, inDiffwrtCref, diffFuncData, BackendDAE.DIFFERENTIATION_FUNCTION(), functions, {}, {}, {}, maxIter, expStack);
Expand Down Expand Up @@ -2796,8 +2794,8 @@ algorithm
local
Option<BackendDAE.Variables> indepVars, knownVars, allVars;
BackendDAE.Variables depVars;
Option<list< BackendDAE.Var>> algVars;
Option<list< .DAE.ComponentRef>> diffCrefs;
list<BackendDAE.Var> algVars;
list< .DAE.ComponentRef> diffCrefs;
Option<String> diffname;

case ({}, _)
Expand All @@ -2824,8 +2822,8 @@ algorithm
local
Option<BackendDAE.Variables> depVars, knownVars, indepVars;
BackendDAE.Variables allVars;
Option<list< BackendDAE.Var>> algVars;
Option<list< .DAE.ComponentRef>> diffCrefs;
list<BackendDAE.Var> algVars;
list< .DAE.ComponentRef> diffCrefs;
Option<String> diffname;

case ({}, _)
Expand Down

0 comments on commit 9648600

Please sign in to comment.