Skip to content

Commit

Permalink
- a more general implementation for when-clauses in algorithm-section…
Browse files Browse the repository at this point in the history
…s for the initialization

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14166 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Nov 30, 2012
1 parent 581283e commit c0dbed9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 42 deletions.
98 changes: 76 additions & 22 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -9126,7 +9126,7 @@ algorithm
{}));

// some debug prints
Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, print, "initial system\n");
Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, print, "\n##################\n# initial system #\n##################\n\n");
Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dump, initdae);

// now let's solve the system!
Expand Down Expand Up @@ -9181,7 +9181,8 @@ algorithm

// simplify system
(isyst,Util.SUCCESS()) = pastoptimiseDAE(isyst, pastOptModules, matchingAlgorithm, daeHandler);
Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, print, "Solved Initial System:\n");

Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, print, "\n#########################\n# solved initial system #\n#########################\n\n");
Debug.fcall(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dump, isyst);
then isyst;

Expand Down Expand Up @@ -9762,23 +9763,6 @@ algorithm
eqn = generateInactiveWhenEquationForInitialization(left, source);
then eqn;

// active when equation during initialization
case BackendDAE.ALGORITHM(alg=(alg as DAE.ALGORITHM_STMTS(statementLst={DAE.STMT_WHEN(exp=condition, statementLst=algStmts, source=source)}))) equation
true = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmOutputs(alg);
size = listLength(crefLst);
eqn = BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(algStmts), source);
then eqn;

// inactive when equation during initialization
case BackendDAE.ALGORITHM(alg=(alg as DAE.ALGORITHM_STMTS(statementLst={DAE.STMT_WHEN(exp=condition, source=source)}))) equation
false = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmOutputs(alg);
size = listLength(crefLst);
algStmts = List.map1(crefLst, generateInactiveWhenAlgStatementForInitialization, source);
eqn = BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(algStmts), source);
then eqn;

case eqn equation
errorMessage = "./Compiler/BackEnd/BackendDAEUtil.mo: function generateInitialWhenEqn failed for:\n" +& BackendDump.equationStr(eqn);
Error.addMessage(Error.INTERNAL_ERROR, {errorMessage});
Expand All @@ -9790,14 +9774,84 @@ algorithm
end matchcontinue;
end generateInitialWhenEqn;

protected function generateInitialWhenAlg "public function generateInitialWhenAlg
author: lochel
This function generates out of a given when-algorithm, a algorithm for the initialization-problem."
input BackendDAE.Equation inEqn;
output BackendDAE.Equation outEqn;
protected
Integer size;
.DAE.Algorithm alg;
.DAE.ElementSource source;
list< .DAE.Statement> stmts;
algorithm
BackendDAE.ALGORITHM(size=size, alg=alg, source=source) := inEqn;
DAE.ALGORITHM_STMTS(statementLst=stmts) := alg;
stmts := generateInitialWhenAlg1(stmts);
alg := DAE.ALGORITHM_STMTS(stmts);
outEqn := BackendDAE.ALGORITHM(size, alg, source);
end generateInitialWhenAlg;

protected function generateInitialWhenAlg1 "public function generateInitialWhenAlg1
author: lochel
This function generates out of a given when-algorithm, a algorithm for the initialization-problem."
input list< .DAE.Statement> inStmts;
output list< .DAE.Statement> outStmts;
algorithm
outStmts := matchcontinue(inStmts)
local
.DAE.Exp condition "The when-condition" ;
.DAE.ComponentRef left "Left hand side of equation" ;
.DAE.Exp right "Right hand side of equation" ;
BackendDAE.Equation eqn;
.DAE.Type identType;
String errorMessage;
list< .DAE.ComponentRef> crefLst;
.DAE.Statement stmt;
list< .DAE.Statement> stmts, rest;
Integer size "size of equation" ;
.DAE.Algorithm alg;
.DAE.ElementSource source "origin of when-stmt";
.DAE.ElementSource algSource "origin of algorithm";

case {} then {};

// active when equation during initialization
case DAE.STMT_WHEN(exp=condition, statementLst=stmts)::rest equation
true = Expression.containsInitialCall(condition, false);
rest = generateInitialWhenAlg1(rest);
stmts = listAppend(stmts, rest);
then stmts;

// inactive when equation during initialization
case DAE.STMT_WHEN(exp=condition, statementLst=stmts, source=source)::rest equation
false = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmStatementListOutputs(stmts);
stmts = List.map1(crefLst, generateInactiveWhenAlgStatementForInitialization, source);
rest = generateInitialWhenAlg1(rest);
stmts = listAppend(stmts, rest);
then stmts;

// no when equation
case stmt::rest equation
// false = isWhenStmt(stmt);
rest = generateInitialWhenAlg1(rest);
then stmt::rest;

else equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAEUtil.mo: function generateInitialWhenAlg1 failed"});
then fail();
end matchcontinue;
end generateInitialWhenAlg1;

protected function collectInitialEqns
input tuple<BackendDAE.Equation, tuple<BackendDAE.EquationArray,BackendDAE.EquationArray>> inTpl;
output tuple<BackendDAE.Equation, tuple<BackendDAE.EquationArray,BackendDAE.EquationArray>> outTpl;
protected
BackendDAE.Equation eqn, eqn1;
BackendDAE.EquationArray eqns, reeqns;
Integer size;
Boolean b, isWhen;
Boolean b, isAlg, isWhen;
algorithm
(eqn, (eqns, reeqns)) := inTpl;

Expand All @@ -9809,8 +9863,8 @@ algorithm
eqn1 := Debug.bcallret1(isWhen, generateInitialWhenEqn, eqn1, eqn1);

// traverse when algorithms
isWhen := BackendEquation.isWhenAlgorithm(eqn);
eqn1 := Debug.bcallret1(isWhen, generateInitialWhenEqn, eqn1, eqn1);
isAlg := BackendEquation.isAlgorithm(eqn);
eqn1 := Debug.bcallret1(isAlg, generateInitialWhenAlg, eqn1, eqn1);

// add it, if size is zero (terminate,assert,noretcall) move to removed equations
size := BackendEquation.equationSize(eqn1);
Expand Down
12 changes: 0 additions & 12 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -2341,18 +2341,6 @@ algorithm
end match;
end isWhenEquation;

public function isWhenAlgorithm "public function isWhenAlgorithm
author: lochel
This function checks whether a given BackendDAE.Equation is a when-algorithm or not."
input BackendDAE.Equation inEqn;
output Boolean b;
algorithm
b := matchcontinue(inEqn)
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS(statementLst={DAE.STMT_WHEN(source=_)})) then true;
else then false;
end matchcontinue;
end isWhenAlgorithm;

public function isArrayEquation
input BackendDAE.Equation inEqn;
output Boolean b;
Expand Down
27 changes: 19 additions & 8 deletions Compiler/FrontEnd/CheckModel.mo
Expand Up @@ -350,8 +350,7 @@ algorithm
end match;
end topLevelInput;

public function algorithmOutputs
"function: algorithmOutputs
public function algorithmOutputs "function: algorithmOutputs
This function finds the the outputs of an algorithm.
An input is all values that are reffered on the right hand side of any
statement in the algorithm and an output is a variables belonging to the
Expand All @@ -360,17 +359,29 @@ public function algorithmOutputs
input DAE.Algorithm inAlgorithm;
output list<DAE.ComponentRef> outCrefLst;
protected
list<DAE.Statement> ss;
list<DAE.Statement> stmts;
algorithm
DAE.ALGORITHM_STMTS(statementLst=stmts) := inAlgorithm;
outCrefLst := algorithmStatementListOutputs(stmts);
end algorithmOutputs;

public function algorithmStatementListOutputs "function: algorithmStatementListOutputs
This function finds the the outputs of an algorithm.
An input is all values that are reffered on the right hand side of any
statement in the algorithm and an output is a variables belonging to the
variables that are assigned a value in the algorithm. If a variable is an
input and an output it will be treated as an output."
input list<DAE.Statement> inStmts;
output list<DAE.ComponentRef> outCrefLst;
protected
HashSet.HashSet hs;
algorithm
DAE.ALGORITHM_STMTS(statementLst = ss) := inAlgorithm;
hs := HashSet.emptyHashSet();
hs := List.fold(ss,statementOutputs,hs);
hs := List.fold(inStmts, statementOutputs, hs);
outCrefLst := BaseHashSet.hashSetList(hs);
end algorithmOutputs;
end algorithmStatementListOutputs;

protected function statementOutputs
"function: statementOutputs
protected function statementOutputs "function: statementOutputs
Helper relation to algorithmOutputs"
input DAE.Statement inStatement;
input HashSet.HashSet iht;
Expand Down

0 comments on commit c0dbed9

Please sign in to comment.