Skip to content

Commit

Permalink
- move zeroCrossingsEquations to SimCodeUtil.mo
Browse files Browse the repository at this point in the history
- move expandDerOperatorto BackendDAEOptimize.mo 


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17819 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Oct 22, 2013
1 parent 22bc5b3 commit b1033e0
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 304 deletions.
302 changes: 0 additions & 302 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -3244,241 +3244,6 @@ algorithm
end match;
end expInt;



public function expandDerOperator "
Expands der(expr) using Derive.differentiteExpTime.
This can not be done in Static, since we need all time-
dependent variables, which is only available in BackendDAE."
input BackendDAE.BackendDAE dae;
output BackendDAE.BackendDAE odae;
algorithm
odae := BackendDAEUtil.mapEqSystem(dae, expandDerOperatorWork);
end expandDerOperator;

protected function expandDerOperatorWork
"expands der(expr) using Derive.differentiteExpTime.
This can not be done in Static, since we need all time-
dependent variables, which is only available in BackendDAE."
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
algorithm
(osyst, oshared) := match (syst, shared)
local
Option<BackendDAE.IncidenceMatrix> m, mT;
BackendDAE.Variables vars, knvars, exobj, vars1, vars2, av;
BackendDAE.EquationArray eqns, remeqns, inieqns, eqns1, inieqns1;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
DAE.FunctionTree funcs;
BackendDAE.SymbolicJacobians symjacs;
Env.Cache cache;
Env.Env env;
BackendDAE.StateSets stateSets;
BackendDAE.ExtraInfo ei;
case (BackendDAE.EQSYSTEM(vars, eqns, m, mT, matching, stateSets), BackendDAE.SHARED(knvars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs,ei))
equation
(eqns1, (vars1, _)) = BackendEquation.traverseBackendDAEEqnsWithUpdate(eqns, traverserexpandDerEquation, (vars, shared));
(inieqns1, (vars2, _)) = BackendEquation.traverseBackendDAEEqnsWithUpdate(inieqns, traverserexpandDerEquation, (vars1, shared));
then
(BackendDAE.EQSYSTEM(vars2, eqns1, m, mT, matching, stateSets), BackendDAE.SHARED(knvars, exobj, av, inieqns1, remeqns, constrs, clsAttrs, cache, env, funcs, einfo, eoc, btp, symjacs,ei));
end match;
end expandDerOperatorWork;

protected function traverserexpandDerEquation
"Help function to e.g. traverserexpandDerEquation"
input tuple<BackendDAE.Equation, tuple<BackendDAE.Variables, BackendDAE.Shared>> tpl;
output tuple<BackendDAE.Equation, tuple<BackendDAE.Variables, BackendDAE.Shared>> outTpl;
protected
BackendDAE.Equation e, e1;
tuple<BackendDAE.Variables, DAE.FunctionTree> ext_arg, ext_art1;
BackendDAE.Variables vars;
DAE.FunctionTree funcs;
Boolean b;
list<DAE.SymbolicOperation> ops;
BackendDAE.Shared shared;
algorithm
(e, (vars, shared)) := tpl;
(e1, (vars, shared, ops)) := BackendEquation.traverseBackendDAEExpsEqn(e, traverserexpandDerExp, (vars, shared, {}));
e1 := List.foldr(ops, BackendEquation.addOperation, e1);
outTpl := ((e1, (vars, shared)));
end traverserexpandDerEquation;

protected function traverserexpandDerExp
"Help function to e.g. traverserexpandDerExp"
input tuple<DAE.Exp, tuple<BackendDAE.Variables, BackendDAE.Shared, list<DAE.SymbolicOperation>>> tpl;
output tuple<DAE.Exp, tuple<BackendDAE.Variables, BackendDAE.Shared, list<DAE.SymbolicOperation>>> outTpl;
protected
DAE.Exp e, e1;
tuple<BackendDAE.Variables, BackendDAE.Shared, Boolean> ext_arg;
BackendDAE.Variables vars;
list<DAE.SymbolicOperation> ops;
DAE.FunctionTree funcs;
Boolean b;
BackendDAE.Shared shared;
algorithm
(e, (vars, shared, ops)) := tpl;
ext_arg := (vars, shared, false);
((e1, ext_arg)) := Expression.traverseExp(e, expandDerExp, ext_arg);
(vars, shared, b) := ext_arg;
ops := List.consOnTrue(b, DAE.OP_DIFFERENTIATE(DAE.crefTime, e, e1), ops);
outTpl := (e1, (vars, shared, ops));
end traverserexpandDerExp;

protected function expandDerExp
"Help function to e.g. expandDerOperatorEqn"
input tuple<DAE.Exp, tuple<BackendDAE.Variables, BackendDAE.Shared, Boolean>> tpl;
output tuple<DAE.Exp, tuple<BackendDAE.Variables, BackendDAE.Shared, Boolean>> outTpl;
algorithm
outTpl := matchcontinue(tpl)
local
BackendDAE.Variables vars;
DAE.Exp e1, e2;
DAE.ComponentRef cr;
String str;
BackendDAE.Shared shared;
list<BackendDAE.Var> varlst;
BackendDAE.Var v;
Boolean b;
DAE.FunctionTree funcs;
case((DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={e1 as DAE.CREF(componentRef=cr)})}), (vars, _, _)))
equation
str = ComponentReference.crefStr(cr);
str = stringAppendList({"The model includes derivatives of order > 1 for: ", str, ". That is not supported. Real d", str, " = der(", str, ") *might* result in a solvable model"});
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
// case for arrays
case((e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr, ty = DAE.T_ARRAY(dims=_))}), (vars, shared as BackendDAE.SHARED(functionTree=funcs), b)))
equation
((e1, (_, true))) = BackendDAEUtil.extendArrExp((e1, (SOME(funcs), false)));
then Expression.traverseExp(e1, expandDerExp, (vars, shared, b));
// case for records
case((e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr, ty = DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_)))}), (vars, shared as BackendDAE.SHARED(functionTree=funcs), b)))
equation
((e1, (_, true))) = BackendDAEUtil.extendArrExp((e1, (SOME(funcs), false)));
then Expression.traverseExp(e1, expandDerExp, (vars, shared, b));
case((e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr)}), (vars, shared, _)))
equation
({v}, _) = BackendVariable.getVar(cr, vars);
(vars, e1) = updateStatesVar(vars, v, e1);
then ((e1, (vars, shared, true)));
case((e1 as DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=cr)}), (vars, shared, _)))
equation
(varlst, _) = BackendVariable.getVar(cr, vars);
vars = updateStatesVars(vars, varlst, false);
then ((e1, (vars, shared, true)));
case((DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={e1}), (vars, shared, _)))
equation
e2 = Derive.differentiateExpTime(e1, (vars, shared));
(e2, _) = ExpressionSimplify.simplify(e2);
((_, vars)) = Expression.traverseExp(e2, derCrefsExp, vars);
then ((e2, (vars, shared, true)));
case _ then tpl;
end matchcontinue;
end expandDerExp;

protected function derCrefsExp "
helper for statesExp
"
input tuple<DAE.Exp, BackendDAE.Variables > inExp;
output tuple<DAE.Exp, BackendDAE.Variables > outExp;
algorithm outExp := matchcontinue(inExp)
local
DAE.ComponentRef cr;
BackendDAE.Variables vars;
list<BackendDAE.Var> varlst;
BackendDAE.Var v;
DAE.Exp e;
case ((e as DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)}), vars))
equation
({v}, _) = BackendVariable.getVar(cr, vars);
(vars, e) = updateStatesVar(vars, v, e);
then
((e, vars));
case ((e as DAE.CALL(path = Absyn.IDENT(name = "der"), expLst = {DAE.CREF(componentRef = cr)}), vars))
equation
(varlst, _) = BackendVariable.getVar(cr, vars);
vars = updateStatesVars(vars, varlst, false);
then
((e, vars));
case _ then inExp;
end matchcontinue;
end derCrefsExp;

protected function updateStatesVar
"Help function to expandDerExp"
input BackendDAE.Variables inVars;
input BackendDAE.Var var;
input DAE.Exp iExp;
output BackendDAE.Variables outVars;
output DAE.Exp oExp;
algorithm
(outVars, oExp) := matchcontinue(inVars, var, iExp)
local
BackendDAE.Variables vars;
BackendDAE.Var var1;
case(_, _, _)
equation
true = BackendVariable.isVarDiscrete(var) "do not change discrete vars to states, because they have no derivative" ;
then (inVars, DAE.RCONST(0.0));
case(_, _, _)
equation
false = BackendVariable.isVarDiscrete(var) "do not change discrete vars to states, because they have no derivative" ;
false = BackendVariable.isStateVar(var);
var1 = BackendVariable.setVarKind(var, BackendDAE.STATE(1,NONE()));
vars = BackendVariable.addVar(var1, inVars);
then (vars, iExp);
case(_, _, _)
equation
/* Might be part of a different equation-system...
str = "BackendDAECreate.updateStatesVars failed for: " +& ComponentReference.printComponentRefStr(cr);
Error.addMessage(Error.INTERNAL_ERROR, {str});
*/
then (inVars, iExp);
end matchcontinue;
end updateStatesVar;

protected function updateStatesVars
"Help function to expandDerExp"
input BackendDAE.Variables inVars;
input list<BackendDAE.Var> inNewStates;
input Boolean noStateFound;
output BackendDAE.Variables outVars;
algorithm
outVars := matchcontinue(inVars, inNewStates, noStateFound)
local
BackendDAE.Var var;
list<BackendDAE.Var> newStates;
BackendDAE.Variables vars;
//DAE.ComponentRef cr;
//String str;

case(_, {}, true) then inVars;
case(_, var::newStates, _)
equation
false = BackendVariable.isVarDiscrete(var) "do not change discrete vars to states, because they have no derivative" ;
false = BackendVariable.isStateVar(var);
var = BackendVariable.setVarKind(var, BackendDAE.STATE(1,NONE()));
vars = BackendVariable.addVar(var, inVars);
vars = updateStatesVars(vars, newStates, true);
then vars;
case(_, _::newStates, _)
equation
/* Might be part of a different equation-system...
str = "BackendDAECreate.updateStatesVars failed for: " +& ComponentReference.printComponentRefStr(cr);
Error.addMessage(Error.INTERNAL_ERROR, {str});
*/
vars = updateStatesVars(inVars, newStates, noStateFound);
then vars;
end matchcontinue;
end updateStatesVars;

// =============================================================================
// section for zero crossings
//
Expand Down Expand Up @@ -4480,16 +4245,6 @@ algorithm
end matchcontinue;
end zerocrossingindex;

protected function zeroCrossingEquations
"Returns the list of equations (indices) from a ZeroCrossing"
input BackendDAE.ZeroCrossing zc;
output list<Integer> lst;
algorithm
lst := match (zc)
case(BackendDAE.ZERO_CROSSING(_, lst, _)) then lst;
end match;
end zeroCrossingEquations;

protected function mergeZeroCrossings
"Takes a list of zero crossings and if more than one have identical
function expressions they are merged into one zerocrossing.
Expand Down Expand Up @@ -4903,63 +4658,6 @@ algorithm
end match;
end makeZeroCrossings;

public function zeroCrossingsEquations
"Returns a list of all equations (by their index) that contain a zero crossing
Used e.g. to find out which discrete equations are not part of a zero crossing"
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
output list<Integer> eqns;
algorithm
eqns := match (syst, shared)
local
list<BackendDAE.ZeroCrossing> zcLst;
list<list<Integer>> zcEqns;
list<Integer> wcEqns;
BackendDAE.EquationArray eqnArr;
case (BackendDAE.EQSYSTEM(orderedEqs=eqnArr), BackendDAE.SHARED(eventInfo=BackendDAE.EVENT_INFO(zeroCrossingLst = zcLst)))
equation
zcEqns = List.map(zcLst, zeroCrossingEquations);
wcEqns = whenEquationsIndices(eqnArr);
eqns = List.unionList(listAppend(zcEqns, {wcEqns}));
then eqns;
end match;
end zeroCrossingsEquations;

protected function whenEquationsIndices "Returns all equation-indices that contain a when clause"
input BackendDAE.EquationArray eqns;
output list<Integer> res;
algorithm
res := match (eqns)
case _ equation
res=whenEquationsIndices2(1, BackendDAEUtil.equationArraySize(eqns), eqns);
then res;
end match;
end whenEquationsIndices;

protected function whenEquationsIndices2
"Help function"
input Integer i;
input Integer size;
input BackendDAE.EquationArray eqns;
output list<Integer> eqnLst;
algorithm
eqnLst := matchcontinue(i, size, eqns)
case(_, _, _)
equation
true = (i > size );
then {};
case(_, _, _)
equation
BackendDAE.WHEN_EQUATION(whenEquation = _) = BackendDAEUtil.equationNth(eqns, i-1);
eqnLst = whenEquationsIndices2(i+1, size, eqns);
then i::eqnLst;
case(_, _, _)
equation
eqnLst=whenEquationsIndices2(i+1, size, eqns);
then eqnLst;
end matchcontinue;
end whenEquationsIndices2;

protected function addOptimizationVarsEqns
"add objective function to DAE. Neeed for derivatives"
input BackendDAE.Variables inVars;
Expand Down

0 comments on commit b1033e0

Please sign in to comment.