Skip to content

Commit

Permalink
- get Modelica.StateGraph.Examples.ControlledTanks.mos working
Browse files Browse the repository at this point in the history
- move expandAlgorithmsbyInitStmts to SimCodeDump
- improve Error Message "Inverse Algorithm needs to be solved for ..."

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13896 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 14, 2012
1 parent a9a81ab commit 4658793
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 55 deletions.
127 changes: 96 additions & 31 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected import ExpressionDump;
protected import Flags;
protected import Global;
protected import HashSet;
protected import HashTable2;
protected import IndexReduction;
protected import Inline;
protected import List;
Expand Down Expand Up @@ -474,20 +475,20 @@ algorithm
outTpl := matchcontinue(inTpl)
local
DAE.Algorithm alg;
DAE.Algorithm algExpanded;
list<DAE.Statement> statements;
BackendDAE.Equation eqn;
BackendDAE.Variables vars;
Integer size;
list<DAE.Exp> outputs;
DAE.ElementSource source;
list<DAE.ComponentRef> crlst;
case((eqn as BackendDAE.ALGORITHM(size=size,alg=alg,source=source),vars))
case((eqn as BackendDAE.ALGORITHM(size=size,alg=alg as DAE.ALGORITHM_STMTS(statements),source=source),vars))
equation
crlst = CheckModel.algorithmOutputs(alg);
outputs = List.map(crlst,Expression.crefExp);
algExpanded = expandAlgorithmStmts(alg,outputs,vars);
statements = expandAlgorithmStmts(statements,outputs,vars);
then
((BackendDAE.ALGORITHM(size,algExpanded,source), vars));
((BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS(statements),source), vars));
else
then inTpl;
end matchcontinue;
Expand All @@ -500,47 +501,34 @@ protected function expandAlgorithmStmts
- A non-discrete variable is initialized with its start value (i.e. the value of the start-attribute).
- A discrete variable v is initialized with pre(v).
Helper function to expandAlgorithmsbyInitStmts1."
input DAE.Algorithm inAlg;
input list<DAE.Statement> inAlg;
input list<DAE.Exp> inOutputs;
input BackendDAE.Variables inVars;
output DAE.Algorithm outAlg;
output list<DAE.Statement> outAlg;
algorithm
outAlg := matchcontinue(inAlg, inOutputs, inVars)
outAlg := match(inAlg, inOutputs, inVars)
local
DAE.Algorithm alg;
DAE.Exp out,initExp;
list<DAE.Exp> rest;
DAE.ComponentRef cref;
Var var;
DAE.Statement stmt;
DAE.Type type_;
list<DAE.Statement> statements,statements1;
case(alg,{},_) then (alg);
case(alg,out::rest,_)
equation
cref = Expression.expCref(out);
type_ = Expression.typeof(out);
type_ = Expression.arrayEltType(type_);
(var::_,_) = BackendVariable.getVar(cref, inVars);
true = BackendVariable.isVarDiscrete(var);
initExp = Expression.makeBuiltinCall("pre", {out}, type_);
stmt = Algorithm.makeAssignment(DAE.CREF(cref,type_), DAE.PROP(type_,DAE.C_VAR()), initExp, DAE.PROP(type_,DAE.C_VAR()), DAE.dummyAttrVar, SCode.NON_INITIAL(), DAE.emptyElementSource);
(DAE.ALGORITHM_STMTS(statements)) = expandAlgorithmStmts(alg,rest,inVars);
statements1 = listAppend({stmt},statements);
then (DAE.ALGORITHM_STMTS(statements1));
case(alg,out::rest,_)
Boolean b;
case(statements,{},_) then statements;
case(statements,out::rest,_)
equation
cref = Expression.expCref(out);
type_ = Expression.typeof(out);
type_ = Expression.arrayEltType(type_);
(var::_,_) = BackendVariable.getVar(cref, inVars);
false = BackendVariable.isVarDiscrete(var);
initExp = Expression.makeBuiltinCall("$_start", {out}, type_);
b = BackendVariable.isVarDiscrete(var);
initExp = Expression.makeBuiltinCall(Util.if_(b,"pre","$_start"), {out}, type_);
stmt = Algorithm.makeAssignment(DAE.CREF(cref,type_), DAE.PROP(type_,DAE.C_VAR()), initExp, DAE.PROP(type_,DAE.C_VAR()), DAE.dummyAttrVar, SCode.NON_INITIAL(), DAE.emptyElementSource);
(DAE.ALGORITHM_STMTS(statements)) = expandAlgorithmStmts(alg,rest,inVars);
statements1 = listAppend({stmt},statements);
then (DAE.ALGORITHM_STMTS(statements1));
end matchcontinue;
then
expandAlgorithmStmts(stmt::statements,rest,inVars);
end match;
end expandAlgorithmStmts;


Expand Down Expand Up @@ -8197,9 +8185,10 @@ algorithm
sode1 := BackendDAECreate.findZeroCrossings(optsode);
Debug.execStat("findZeroCrossings",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
_ := traverseBackendDAEExpsNoCopyWithUpdate(sode1,ExpressionSimplify.simplifyTraverseHelper,0) "simplify all expressions";
sode2 := calculateValues(sode1);
Debug.execStat("calculateValue",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
outSODE := expandAlgorithmsbyInitStmts(sode2);
outSODE := calculateValues(sode1);
// moved to SimCodeUtil because of initial system
//Debug.execStat("calculateValue",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
//outSODE := expandAlgorithmsbyInitStmts(sode2);
Debug.execStat("expandAlgorithmsbyInitStmts",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.fcall(Flags.DUMP_INDX_DAE, print, "dumpindxdae:\n");
Debug.fcall(Flags.DUMP_INDX_DAE, BackendDump.dump, outSODE);
Expand Down Expand Up @@ -9329,6 +9318,7 @@ algorithm
array<DAE.ClassAttributes> classAttrs;
HashSet.HashSet usedPreVars;
list<DAE.ComponentRef> inaktiveWhen;
Boolean b;

case(BackendDAE.DAE(systs, BackendDAE.SHARED(knownVars=knvars,
initialEqs=inieqns,
Expand All @@ -9337,6 +9327,10 @@ algorithm
cache=cache,
env=env,
functionTree=functionTree))) equation
// remove multiple equal definted pre vars
((eqnslst,_,b)) = BackendEquation.traverseBackendDAEEqns(inieqns,removeMultipleEqualPre,({},HashTable2.emptyHashTable(),false));
inieqns = Debug.bcallret1(b,listEquation,eqnslst,inieqns);

// collect used pre vars
usedPreVars = HashSet.emptyHashSet();
usedPreVars = traverseBackendDAEExpsEqns(inieqns, collectPreCrefExp, usedPreVars);
Expand Down Expand Up @@ -9698,6 +9692,52 @@ algorithm
end matchcontinue;
end addStartValueEquations1;

protected function removeMultipleEqualPre
input tuple<BackendDAE.Equation, tuple<list<BackendDAE.Equation>,HashTable2.HashTable,Boolean>> inTpl;
output tuple<BackendDAE.Equation, tuple<list<BackendDAE.Equation>,HashTable2.HashTable,Boolean>> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
BackendDAE.Equation eqn;
list<BackendDAE.Equation> eqns;
HashTable2.HashTable multiplepre;
Boolean b;
DAE.ComponentRef cr;
DAE.Exp e,e1;
case ((eqn as BackendDAE.EQUATION(exp=DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {DAE.CREF(componentRef = cr)}),scalar=e),(eqns,multiplepre,b)))
equation
true = Expression.isConst(e);
e1 = BaseHashTable.get(cr,multiplepre);
true = Expression.expEqual(e,e1);
then
((eqn,(eqns,multiplepre,true)));
case ((eqn as BackendDAE.EQUATION(exp=e,scalar=DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {DAE.CREF(componentRef = cr)})),(eqns,multiplepre,b)))
equation
true = Expression.isConst(e);
e1 = BaseHashTable.get(cr,multiplepre);
true = Expression.expEqual(e,e1);
then
((eqn,(eqns,multiplepre,true)));
case ((eqn as BackendDAE.EQUATION(exp=DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {DAE.CREF(componentRef = cr)}),scalar=e),(eqns,multiplepre,b)))
equation
true = Expression.isConst(e);
failure( _ = BaseHashTable.get(cr,multiplepre));
multiplepre = BaseHashTable.add((cr,e),multiplepre);
then
((eqn,(eqn::eqns,multiplepre,b)));
case ((eqn as BackendDAE.EQUATION(exp=e,scalar=DAE.CALL(path = Absyn.IDENT(name = "pre"), expLst = {DAE.CREF(componentRef = cr)})),(eqns,multiplepre,b)))
equation
true = Expression.isConst(e);
failure( _ = BaseHashTable.get(cr,multiplepre));
multiplepre = BaseHashTable.add((cr,e),multiplepre);
then
((eqn,(eqn::eqns,multiplepre,b)));
case ((eqn,(eqns,multiplepre,b)))
then
((eqn,(eqn::eqns,multiplepre,b)));
end matchcontinue;
end removeMultipleEqualPre;

protected function handleinaktiveWhen
input DAE.ComponentRef inaktiveWhen;
input BackendDAE.Variables vars;
Expand Down Expand Up @@ -9911,6 +9951,7 @@ algorithm
Integer size;
Boolean b;
BackendDAE.EquationArray eqns,reeqns;
list<DAE.Statement> statementLst;
// active when equation during initialization
case (BackendDAE.WHEN_EQUATION(whenEquation=BackendDAE.WHEN_EQ(condition=condition, left=left, right=right),size=size, source=source),_,_,_)
equation
Expand Down Expand Up @@ -9938,13 +9979,37 @@ algorithm
reeqns = Debug.bcallret2(not b, BackendEquation.equationAdd, eqn, iReeqns, iReeqns);
then
(eqns,reeqns,left::iInaktiveWhen);

// inactive when equation during initialization
case (BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS(statementLst=statementLst),size=size, source=source),_,_,_)
equation
statementLst = List.select(statementLst,isStmtWhenAktive);
b = intGt(listLength(statementLst),0);
eqns = Debug.bcallret2(b, BackendEquation.equationAdd, BackendDAE.ALGORITHM(size,DAE.ALGORITHM_STMTS(statementLst),source), iEqns, iEqns);
then
(eqns,iReeqns,iInaktiveWhen);

else
equation
eqns = BackendEquation.equationAdd(inEqn, iEqns);
then (eqns,iReeqns,iInaktiveWhen);
end matchcontinue;
end generateInitialWhenEqn;

protected function isStmtWhenAktive
input DAE.Statement statement;
output Boolean b;
algorithm
b := match(statement)
local
DAE.Exp e;
case DAE.STMT_WHEN(exp=e)
then Expression.containsInitialCall(e, false);
else
then true;
end match;
end isStmtWhenAktive;

protected function collectInitialEqns
input tuple<BackendDAE.Equation, tuple<BackendDAE.EquationArray,BackendDAE.EquationArray,list<DAE.ComponentRef>>> inTpl;
output tuple<BackendDAE.Equation, tuple<BackendDAE.EquationArray,BackendDAE.EquationArray,list<DAE.ComponentRef>>> outTpl;
Expand Down
17 changes: 14 additions & 3 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,14 @@ algorithm
(residuals, initialEquations, numberOfInitialEquations, numberOfInitialAlgorithms, uniqueEqIndex, tempvars, useSymbolicInitialization) = createInitialResiduals(dlow2, uniqueEqIndex, {}, helpVarInfo);
(jacG, uniqueEqIndex) = createInitialMatrices(dlow2, uniqueEqIndex);

// expandAlgorithmsbyInitStmts
dlow2 = BackendDAEUtil.expandAlgorithmsbyInitStmts(dlow2);
BackendDAE.DAE(systs, shared as BackendDAE.SHARED(removedEqs=removedEqs,
constraints = constrsarr,
classAttrs = clsattrsarra,
functionTree = functionTree,
symjacs = symJacs)) = dlow2;

// Add model info
modelInfo = createModelInfo(class_, dlow2, functions, {}, n_h, numberOfInitialEquations, numberOfInitialAlgorithms, fileDir,ifcpp);

Expand Down Expand Up @@ -3760,7 +3768,8 @@ algorithm
// We need to solve an inverse problem of an algorithm section.
false = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
message = stringAppendList({"Inverse Algorithm needs to be solved for in ",algStr,". This has not been implemented yet.\n"});
message = ComponentReference.printComponentRefStr(BackendVariable.varCref(v));
message = stringAppendList({"Inverse Algorithm needs to be solved for ",message," in ",algStr,". This has not been implemented yet.\n"});
Error.addMessage(Error.INTERNAL_ERROR,{message});
then fail();
// Algorithm for single variable.
Expand All @@ -3787,7 +3796,8 @@ algorithm
// We need to solve an inverse problem of an algorithm section.
false = ComponentReference.crefEqualNoStringCompare(BackendVariable.varCref(v),varOutput);
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
message = stringAppendList({"Inverse Algorithm needs to be solved for in ",algStr,". This has not been implemented yet.\n"});
message = ComponentReference.printComponentRefStr(BackendVariable.varCref(v));
message = stringAppendList({"Inverse Algorithm needs to be solved for ",message," in ",algStr,". This has not been implemented yet.\n"});
Error.addMessage(Error.INTERNAL_ERROR,{message});
then fail();
end matchcontinue;
Expand Down Expand Up @@ -6186,7 +6196,8 @@ algorithm
// The variables solved for musst all be part of the output variables of the algorithm.
failure(List.map2AllValue(solvedVars,List.isMemberOnTrue,true,algOutVars,ComponentReference.crefEqualNoStringCompare));
algStr = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
message = stringAppendList({"Inverse Algorithm needs to be solved for in ",algStr,". This has not been implemented yet.\n"});
message = ComponentReference.printComponentRefListStr(solvedVars);
message = stringAppendList({"Inverse Algorithm needs to be solved for ",message," in ",algStr,". This has not been implemented yet.\n"});
Error.addMessage(Error.INTERNAL_ERROR,{message});
then
fail();
Expand Down
42 changes: 21 additions & 21 deletions Compiler/Template/CodegenAdevs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//
// - Code after a case should be indented with 2 spaces if not written on the
// same line

package CodegenAdevs
import interface SimCodeTV;

Expand Down Expand Up @@ -346,19 +346,19 @@ template makeExtraResiduals(list<SimEqSystem> allEquations, String name)
<<
static int residualFunc<%index%>(N_Vector y, N_Vector f, void*)
{
double* yd = NV_DATA_S(y);
double* fd = NV_DATA_S(f);
active_model->residualFunc<%index%>_cpp(yd,fd);
return 0;
double* yd = NV_DATA_S(y);
double* fd = NV_DATA_S(f);
active_model->residualFunc<%index%>_cpp(yd,fd);
return 0;
}

void <%name%>::residualFunc<%index%>_cpp(double* y, double* res)
{
<%crefs |> name hasindex i0 =>
<<
<%cref(name)%> = y[<%i0%>];
>>
;separator="\n"%>
<%crefs |> name hasindex i0 =>
<<
<%cref(name)%> = y[<%i0%>];
>>
;separator="\n"%>
<%varDecls%>
<%algs%>
<%prebody%>
Expand All @@ -374,21 +374,21 @@ template makeExtraResiduals(list<SimEqSystem> allEquations, String name)
void* kmem = KINCreate();
assert(kmem != NULL);
flag = KINInit(kmem, residualFunc<%index%>, y);
assert(flag == KIN_SUCCESS);
assert(flag == KIN_SUCCESS);
flag = KINDense(kmem, NEQ);
assert(flag == KIN_SUCCESS);
N_VConst_Serial(1.0,scale);
<%crefs |> name hasindex i0 =>
<<
assert(flag == KIN_SUCCESS);
N_VConst_Serial(1.0,scale);
<%crefs |> name hasindex i0 =>
<<
NV_Ith_S(y,<%i0%>) = <%cref(name)%>;
>>
;separator="\n"%>
>>
;separator="\n"%>
flag = KINSol(kmem,y,KIN_LINESEARCH,scale,scale);
<%crefs |> name hasindex i0 =>
<<
<%crefs |> name hasindex i0 =>
<<
<%cref(name)%> = NV_Ith_S(y,<%i0%>);
>>
;separator="\n"%>
>>
;separator="\n"%>
N_VDestroy_Serial(y);
N_VDestroy_Serial(scale);
KINFree(&kmem);
Expand Down

0 comments on commit 4658793

Please sign in to comment.