Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
[DAEmode] added basic algorithms support
Browse files Browse the repository at this point in the history
 - added dae header to all files
 - added a funciton in CheckModel to verify if crefs are algorithm outputs

Belonging to [master]:
  - #2248
  - OpenModelica/OpenModelica-testsuite#865
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Mar 2, 2018
1 parent b54a3c9 commit f5c5629
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
20 changes: 20 additions & 0 deletions Compiler/BackEnd/DAEMode.mo
Expand Up @@ -52,6 +52,7 @@ import BackendDAEFunc;
import BackendDump;
import BackendEquation;
import BackendVariable;
import CheckModel;
import CommonSubExpression;
import ComponentReference;
import Config;
Expand Down Expand Up @@ -271,6 +272,10 @@ algorithm
DAE.FunctionTree funcsTree;
list<DAE.ComponentRef> crlst;

DAE.Algorithm alg;
DAE.ElementSource source;
DAE.Expand crefExpand;

constant Boolean debug = false;

case ({eq}, vars)
Expand Down Expand Up @@ -328,6 +333,21 @@ algorithm
then
(traverserArgs);

case ({eq as BackendDAE.ALGORITHM(alg=alg, source=source, expand=crefExpand)}, vars)
guard (not listEmpty(varIdxs) // not inside of EQNS_SYSTEM possible solveable
and not Util.boolOrList(list(BackendVariable.isStateVar(v) for v in vars)))
equation
// check that all vars
true = CheckModel.isCrefListAlgorithmOutput(list(v.varName for v in vars), alg, source, crefExpand);
new_eq = BackendEquation.setEquationAttributes(eq, BackendDAE.EQ_ATTR_DEFAULT_AUX);
traverserArgs.auxVars = listAppend(vars, traverserArgs.auxVars);
traverserArgs.auxEqns = listAppend({new_eq}, traverserArgs.auxEqns);
if debug then print("[DAEmode] Create solved algorithms. vars:\n" +
BackendDump.varListString(vars, "") + "eq:\n" +
BackendDump.equationListString({new_eq}, "") + "\n"); end if;
then
(traverserArgs);

case ({eq}, vars)
guard (Util.boolOrList(list(BackendVariable.isStateVar(v) for v in vars))
or listEmpty(varIdxs)) // inside of EQNS_SYSTEM
Expand Down
24 changes: 24 additions & 0 deletions Compiler/FrontEnd/CheckModel.mo
Expand Up @@ -329,6 +329,30 @@ algorithm
end matchcontinue;
end checkAndGetAlgorithmOutputs;

public function isCrefListAlgorithmOutput
"This function verfies if all crefs in crefList are outputs
of the passed algorithm"
input list<DAE.ComponentRef> crefList;
input DAE.Algorithm inAlgorithm;
input DAE.ElementSource inSource;
input DAE.Expand inCrefExpansionRule;
output Boolean outResult;
protected
HashSet.HashSet ht = HashSet.emptyHashSet();
list<DAE.ComponentRef> algOutCrefs;
algorithm
algOutCrefs := CheckModel.checkAndGetAlgorithmOutputs(inAlgorithm, inSource, inCrefExpansionRule);
ht := List.fold(algOutCrefs, BaseHashSet.add, ht);
try
for cr in crefList loop
BaseHashSet.get(cr, ht);
end for;
outResult := true;
else
outResult := false;
end try;
end isCrefListAlgorithmOutput;

protected 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 Down
15 changes: 3 additions & 12 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -6180,26 +6180,20 @@ algorithm
// normal call
case (BackendDAE.ALGORITHM(alg=alg, source = source, expand=crefExpand)::_, false) equation
solvedVars = List.map(vars, BackendVariable.varCref);
algOutVars = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crefExpand);
// The variables solved for musst all be part of the output variables of the algorithm.
List.map2AllValue(solvedVars, List.isMemberOnTrue, true, algOutVars, ComponentReference.crefEqualNoStringCompare);
true = CheckModel.isCrefListAlgorithmOutput(solvedVars, alg, source, crefExpand);
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
then ({SimCode.SES_ALGORITHM(iuniqueEqIndex, algStatements)}, iuniqueEqIndex+1);

// remove discrete Vars
case (BackendDAE.ALGORITHM(alg=alg, source=source, expand=crefExpand)::_, true) equation
solvedVars = List.map(vars, BackendVariable.varCref);
algOutVars = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crefExpand);
// The variables solved for musst all be part of the output variables of the algorithm.
List.map2AllValue(solvedVars, List.isMemberOnTrue, true, algOutVars, ComponentReference.crefEqualNoStringCompare);
true = CheckModel.isCrefListAlgorithmOutput(solvedVars, alg, source, crefExpand);
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
algStatements = BackendDAEUtil.removeDiscreteAssignments(algStatements, BackendVariable.listVar1(vars));
then ({SimCode.SES_ALGORITHM(iuniqueEqIndex, algStatements)}, iuniqueEqIndex+1);

// inverse Algorithm for single variable.
case (BackendDAE.ALGORITHM(alg=alg, source=source, expand=crefExpand)::_, false) equation
_ = List.map(vars, BackendVariable.varCref);
_ = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crefExpand);
// We need to solve an inverse problem of an algorithm section.
DAE.ALGORITHM_STMTS(algStatements) = BackendDAEUtil.collateAlgorithm(alg, NONE());
algStatements = solveAlgorithmInverse(algStatements, vars);
Expand Down Expand Up @@ -6242,10 +6236,7 @@ algorithm
// Error message, inverse algorithms cannot be solved for discrete variables
case (BackendDAE.ALGORITHM(alg=alg, source=source, expand=crefExpand)::_, _) equation
solvedVars = List.map(vars, BackendVariable.varCref);
algOutVars = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crefExpand);

// The variables solved for must all be part of the output variables of the algorithm.
failure(List.map2AllValue(solvedVars, List.isMemberOnTrue, true, algOutVars, ComponentReference.crefEqualNoStringCompare));
false = CheckModel.isCrefListAlgorithmOutput(solvedVars, alg, source, crefExpand);

crefsStr = ComponentReference.printComponentRefListStr(solvedVars);
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg, source)});
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -1178,8 +1178,10 @@ end simulationFile;
template simulationFileHeader(String fileNamePrefix)
"Generates header part of simulation file."
::=
let daeHeader = if intGt(Flags.getConfigEnum(Flags.DAE_MODE), 1) then '#include "<%fileNamePrefix%>_16dae.h"'
<<
#include "<%fileNamePrefix%>_model.h"
<%daeHeader%>
>>
end simulationFileHeader;

Expand Down

0 comments on commit f5c5629

Please sign in to comment.