Skip to content

Commit

Permalink
Implemented function checkForDiscreteVarChanges.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4624 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Rickard Lindberg committed Dec 3, 2009
1 parent da1f466 commit fe9751f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
52 changes: 51 additions & 1 deletion Compiler/SimCode.mo
Expand Up @@ -277,6 +277,7 @@ uniontype SimCode
list<list<DAE.ComponentRef>> zeroCrossingsNeedSave;
list<HelpVarInfo> helpVarInfo;
list<SimWhenClause> whenClauses;
list<DAE.ComponentRef> discreteModelVars;
end SIMCODE;
end SimCode;

Expand Down Expand Up @@ -1166,6 +1167,7 @@ algorithm
list<DAELow.ZeroCrossing> zeroCrossings;
list<list<DAE.ComponentRef>> zeroCrossingsNeedSave;
list<SimWhenClause> whenClauses;
list<DAE.ComponentRef> discreteModelVars;
case (dae,dlow,ass1,ass2,m,mt,comps,class_,filename,funcfilename,fileDir)
equation
print("in mine:"); print(filename); print("\n");
Expand Down Expand Up @@ -1206,12 +1208,14 @@ algorithm
zeroCrossings = createZeroCrossings(dlow2);
zeroCrossingsNeedSave = createZeroCrossingsNeedSave(zeroCrossings, dae, dlow2, ass1, ass2, comps);
whenClauses = createSimWhenClauses(dlow2);
discreteModelVars = extractDiscreteModelVars(dlow2, mt);
print("creating SIMCODE"); print("\n");
simCode = SIMCODE(modelInfo, {}, stateEquations,
nonStateContEquations, nonStateDiscEquations,
residualEquations, initialEquations,
parameterEquations, zeroCrossings,
zeroCrossingsNeedSave, helpVarInfo, whenClauses);
zeroCrossingsNeedSave, helpVarInfo, whenClauses,
discreteModelVars);
// Generate with template
print("writing template to disk"); print("\n");
_ = Tpl.tplString(SimCodeC.translateModel, simCode);
Expand All @@ -1225,6 +1229,52 @@ algorithm
end matchcontinue;
end generateSimulationCodeC;

public function extractDiscreteModelVars
input DAELow.DAELow dlow;
input DAELow.IncidenceMatrixT mT;
output list<DAE.ComponentRef> discreteModelVars;
algorithm
discreteModelVars :=
matchcontinue (dlow, mT)
local
DAELow.Variables v;
list<DAELow.Var> vLst;
list<DAE.ComponentRef> vLst2;
case (DAELow.DAELOW(orderedVars=v), mT)
equation
vLst = DAELow.varList(v);
// select all discrete vars.
vLst = Util.listSelect(vLst, DAELow.isVarDiscrete);
// remove those vars that are solved in when equations
vLst = Util.listSelect2(vLst, dlow, mT, varNotSolvedInWhen);
// replace var with cref
vLst2 = Util.listMap(vLst, DAELow.varCref);
then vLst2;
end matchcontinue;
end extractDiscreteModelVars;

public function varNotSolvedInWhen
input DAELow.Var var;
input DAELow.DAELow dlow;
input DAELow.IncidenceMatrixT mT;
output Boolean include;
algorithm
include :=
matchcontinue(var, dlow, mT)
local
Exp.ComponentRef cr;
Integer varIndx;
list<Integer> eqns;
case(var as DAELow.VAR(varName=cr, index=varIndx), dlow, mT)
equation
eqns = mT[varIndx + 1];
true = crefNotInWhenEquation(cr, dlow, eqns);
then true;
case(_,_,_)
then false;
end matchcontinue;
end varNotSolvedInWhen;

public function createSimWhenClauses
input DAELow.DAELow dlow;
output list<SimWhenClause> simWhenClauses;
Expand Down
11 changes: 9 additions & 2 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -80,7 +80,7 @@ case SIMCODE(modelInfo = MODELINFO) then

<boundParametersFunction(parameterEquations)>

<eventCheckingCode()>
<eventCheckingCode(helpVarInfo, discreteModelVars)>
>>

globalData(ModelInfo modelInfo) ::=
Expand Down Expand Up @@ -756,11 +756,18 @@ int bound_parameters()
}
>>

eventCheckingCode() ::=
eventCheckingCode(list<HelpVarInfo> helpVarInfo,
list<ComponentRef> discreteModelVars) ::=
<<
int checkForDiscreteVarChanges()
{
int needToIterate = 0;
<helpVarInfo of (id1, exp, id2):
'if (edge(localData-\>helpVars[<id1>])) AddEvent(<id2> + localData-\>nZeroCrossing);' "\n">
<discreteModelVars of var:
'if (change(<cref(var)>)) { needToIterate = 1; }' "\n">
for (long i = 0; i \< localData-\>nHelpVars; i++) {
if (change(localData-\>helpVars[i])) {
Expand Down
1 change: 1 addition & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -53,6 +53,7 @@ package SimCode
list<list<DAE.ComponentRef>> zeroCrossingsNeedSave;
list<HelpVarInfo> helpVarInfo;
list<SimWhenClause> whenClauses;
list<DAE.ComponentRef> discreteModelVars;
end SIMCODE;
end SimCode;

Expand Down

0 comments on commit fe9751f

Please sign in to comment.