Skip to content

Commit

Permalink
the evaluation of zerocrossings take now only needed equations into …
Browse files Browse the repository at this point in the history
…account

 - less equations are needed to evaluate function_ZeroCrossings, not the whole rhs)
 - Modelica.Fluid.Examples.Tanks.EmptyTanks.mos works again



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19937 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Apr 2, 2014
1 parent b7e5c39 commit 299f738
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 93 deletions.
107 changes: 107 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2110,6 +2110,54 @@ algorithm
outIntegerArray := markStateEquationsWork(eqns,{},m,ass1,arr);
end markStateEquations;

public function markZeroCrossingEquations "function: markStateEquations
This function goes through all equations and marks the ones that
calculates a state, or is needed in order to calculate a state,
with a non-zero value in the array passed as argument.
This is done by traversing the directed graph of nodes where
a node is an equation/solved variable and following the edges in the
backward direction.
inputs: (daeLow: BackendDAE,
marks: int array,
incidenceMatrix: IncidenceMatrix,
incidenceMatrixT: IncidenceMatrixT,
assignments1: int vector,
assignments2: int vector)
outputs: marks: int array"
input BackendDAE.EqSystem syst;
input list<BackendDAE.ZeroCrossing> inZeroCross;
input array<Integer> arr;
input array<Integer> ass1;
output array<Integer> outIntegerArray;
protected
list<Integer> varindx_lst,eqns;
BackendDAE.IncidenceMatrix m;
BackendDAE.Variables v,v2;
list<BackendDAE.Var> varlst;
algorithm
BackendDAE.EQSYSTEM(orderedVars = v,m=SOME(m)) := syst;
(_, (_, varlst)) := traverseZeroCrossingExps(inZeroCross, varsCollector, (v,{}), {});
v2 := BackendVariable.listVar(varlst);
varindx_lst := BackendVariable.getVarIndexFromVariables(v2, v);
eqns := List.map1r(varindx_lst,arrayGet,ass1);
eqns := List.select(eqns, Util.intPositive);
outIntegerArray := markStateEquationsWork(eqns,{},m,ass1,arr);
end markZeroCrossingEquations;

protected function varsCollector
input tuple<DAE.Exp, tuple<BackendDAE.Variables, list<BackendDAE.Var>>> inTpl;
output tuple<DAE.Exp, tuple<BackendDAE.Variables, list<BackendDAE.Var>>> outTpl;
protected
DAE.Exp inExp;
BackendDAE.Variables vars;
list<BackendDAE.Var> varsLst, varLst2;
algorithm
(inExp, (vars, varsLst)) := inTpl;
varLst2 := BackendEquation.expressionVars(inExp, vars);
varsLst := listAppend(varsLst, varLst2);
outTpl := (inExp, (vars, varsLst));
end varsCollector;

protected function markStateEquationsWork
"Helper function to mark_state_equation
Does the job by looking at variable indexes and incidencematrices.
Expand Down Expand Up @@ -8018,6 +8066,65 @@ algorithm
end match;
end traverseAlgorithmExpsWithUpdate;

public function traverseWhenClauseExps
replaceable type Type_a subtypeof Any;
input list<BackendDAE.WhenClause> iWhenClauses;
input FuncExpType func;
input Type_a inTypeA;
input list<BackendDAE.WhenClause> iAcc;
output list<BackendDAE.WhenClause> oWhenClauses;
output Type_a outTypeA;
partial function FuncExpType
input tuple<DAE.Exp, Type_a> inTpl;
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
algorithm
(oWhenClauses,outTypeA) := match(iWhenClauses,func,inTypeA,iAcc)
local
list<BackendDAE.WhenClause> whenClause;
DAE.Exp condition;
list<BackendDAE.WhenOperator> reinitStmtLst;
Option<Integer> elseClause;
Type_a arg;
case({},_,_,_) then (listReverse(iAcc),inTypeA);
case(BackendDAE.WHEN_CLAUSE(condition,reinitStmtLst,elseClause)::whenClause,_,_,_)
equation
((condition,arg)) = Expression.traverseExp(condition,func,inTypeA);
(whenClause,arg) = traverseWhenClauseExps(whenClause,func,arg,BackendDAE.WHEN_CLAUSE(condition,reinitStmtLst,elseClause)::iAcc);
then
(whenClause,arg);
end match;
end traverseWhenClauseExps;

public function traverseZeroCrossingExps
replaceable type Type_a subtypeof Any;
input list<BackendDAE.ZeroCrossing> iZeroCrossing;
input FuncExpType func;
input Type_a inTypeA;
input list<BackendDAE.ZeroCrossing> iAcc;
output list<BackendDAE.ZeroCrossing> oZeroCrossing;
output Type_a outTypeA;
partial function FuncExpType
input tuple<DAE.Exp, Type_a> inTpl;
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
algorithm
(oZeroCrossing,outTypeA) := match(iZeroCrossing,func,inTypeA,iAcc)
local
list<BackendDAE.ZeroCrossing> zeroCrossing;
DAE.Exp relation_;
list<Integer> occurEquLst,occurWhenLst;
Type_a arg;
case({},_,_,_) then (listReverse(iAcc),inTypeA);
case(BackendDAE.ZERO_CROSSING(relation_,occurEquLst,occurWhenLst)::zeroCrossing,_,_,_)
equation
((relation_,arg)) = Expression.traverseExp(relation_,func,inTypeA);
(zeroCrossing,arg) = traverseZeroCrossingExps(zeroCrossing,func,arg,BackendDAE.ZERO_CROSSING(relation_,occurEquLst,occurWhenLst)::iAcc);
then
(zeroCrossing,arg);
end match;
end traverseZeroCrossingExps;

/*
protected function traverseBackendDAEExpsWrapper "author: Frenkel TUD
Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/BackendQSS.mo
Expand Up @@ -996,6 +996,7 @@ algorithm
list<String> externalFunctionIncludes;
list<list<SimCode.SimEqSystem>> algebraicEquations,odeEquations;
list<SimCode.SimEqSystem> allEquations,residualEquations,startValueEquations,nominalValueEquations,minValueEquations,maxValueEquations,parameterEquations,inlineEquations,removedEquations,algorithmAndEquationAsserts,jacobianEquations;
list<SimCode.SimEqSystem> equationsForZeroCrossings;
list<SimCode.StateSet> stateSets;
Boolean useSymbolicInitialization, useHomotopy;
list<SimCode.SimEqSystem> initialEquations;
Expand All @@ -1017,7 +1018,7 @@ algorithm

case (SimCode.SIMCODE(modelInfo,literals,recordDecls,externalFunctionIncludes,allEquations,odeEquations,
algebraicEquations,residualEquations,useSymbolicInitialization,useHomotopy,initialEquations,startValueEquations,nominalValueEquations,minValueEquations,maxValueEquations,
parameterEquations,inlineEquations,removedEquations,algorithmAndEquationAsserts,jacobianEquations,stateSets,constraints,classAttributes,zeroCrossings,relations,
parameterEquations,inlineEquations,removedEquations,algorithmAndEquationAsserts,equationsForZeroCrossings,jacobianEquations,stateSets,constraints,classAttributes,zeroCrossings,relations,
timeEvents,whenClauses,discreteModelVars,extObjInfo,makefileParams,
delayedExps,jacobianMatrixes,simulationSettingsOpt,fileNamePrefix,crefToSimVarHT,hpcOmSchedule),_)
equation
Expand All @@ -1026,7 +1027,7 @@ algorithm
then SimCode.SIMCODE(modelInfo, literals, recordDecls, externalFunctionIncludes,
allEquations, {eqs}, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy,
initialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations, parameterEquations, inlineEquations,
removedEquations, algorithmAndEquationAsserts, jacobianEquations, stateSets, constraints, classAttributes,
removedEquations, algorithmAndEquationAsserts, equationsForZeroCrossings, jacobianEquations, stateSets, constraints, classAttributes,
zeroCrossings, relations, timeEvents, whenClauses, discreteModelVars, extObjInfo,
makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT,hpcOmSchedule);

Expand Down
42 changes: 42 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1161,6 +1161,20 @@ algorithm
end match;
end isDummyDerVar;

public function isStateDerVar
"function: isStateDerVar
Returns true for der(state) variables, false otherwise."
input BackendDAE.Var inVar;
output Boolean outBoolean;
algorithm
outBoolean:=
match (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.STATE_DER())) then true;
else
then false;
end match;
end isStateDerVar;

public function isStateorStateDerVar
"Returns true for state and der(state) variables, false otherwise."
input BackendDAE.Var inVar;
Expand Down Expand Up @@ -4072,6 +4086,34 @@ algorithm
outTpl := (v,v_lst);
end traversingisStateVarFinder;

public function getAllStateDerVarIndexFromVariables
input BackendDAE.Variables inVariables;
output list<BackendDAE.Var> v_lst;
output list<Integer> i_lst;
algorithm
((v_lst,i_lst,_)) := traverseBackendDAEVars(inVariables,traversingisStateDerVarIndexFinder,({},{},1));
end getAllStateDerVarIndexFromVariables;

protected function traversingisStateDerVarIndexFinder
"author: Frenkel TUD 2010-11"
input tuple<BackendDAE.Var, tuple<list<BackendDAE.Var>,list<Integer>,Integer>> inTpl;
output tuple<BackendDAE.Var, tuple<list<BackendDAE.Var>,list<Integer>,Integer>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
BackendDAE.Var v;
list<BackendDAE.Var> v_lst;
list<Integer> i_lst;
Integer i;
case ((v,(v_lst,i_lst,i)))
equation
true = isStateDerVar(v);
then ((v,(v::v_lst,i::i_lst,i+1)));
case ((v,(v_lst,i_lst,i))) then ((v,(v_lst,i_lst,i+1)));
end matchcontinue;
end traversingisStateDerVarIndexFinder;

public function getAllStateVarIndexFromVariables
input BackendDAE.Variables inVariables;
output list<BackendDAE.Var> v_lst;
Expand Down
5 changes: 3 additions & 2 deletions Compiler/BackEnd/HpcOmSimCode.mo
Expand Up @@ -108,6 +108,7 @@ algorithm
list<SimCode.SimEqSystem> removedEquations;
list<SimCode.SimEqSystem> algorithmAndEquationAsserts;
list<SimCode.SimEqSystem> jacobianEquations;
list<SimCode.SimEqSystem> zeroCrossingsEquations;
//list<DAE.Statement> algorithmAndEquationAsserts;
list<DAE.Constraint> constraints;
list<DAE.ClassAttributes> classAttributes;
Expand Down Expand Up @@ -265,14 +266,14 @@ algorithm

Debug.execStat("hpcom dump schedule TaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, _) = simCode;

checkOdeSystemSize(taskGraphOde,odeEquations);
Debug.execStat("hpcom check ODE system size", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);

simCode = SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, SOME(taskScheduleSimCode));


Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -96,6 +96,7 @@ uniontype SimCode
list<SimEqSystem> inlineEquations;
list<SimEqSystem> removedEquations;
list<SimEqSystem> algorithmAndEquationAsserts;
list<SimEqSystem> equationsForZeroCrossings;
list<SimEqSystem> jacobianEquations;
//list<DAE.Statement> algorithmAndEquationAsserts;
list<StateSet> stateSets;
Expand Down

0 comments on commit 299f738

Please sign in to comment.