Skip to content

Commit

Permalink
* Implementing discontinuities
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8700 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
fbergero committed Apr 19, 2011
1 parent d6e7781 commit b312514
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
26 changes: 25 additions & 1 deletion Compiler/BackEnd/BackendQSS.mo
Expand Up @@ -192,9 +192,13 @@ algorithm

dumpDEVSstructs(DEVS_structure);

//conns = generateConnections(QSSINFO(stateEq_blt, DEVS_structure,eqs,varlst));
//print("CONNECTIONS\n");
//printListOfLists(conns);
conns = generateConnections(QSSINFO(stateEq_blt, DEVS_structure,eqs,varlst));
print("CONNECTIONS");
printListOfLists(conns);
//print("\n");

then
QSSINFO(stateEq_blt, DEVS_structure,eqs,varlst);
Expand Down Expand Up @@ -2404,6 +2408,20 @@ algorithm
end match;
end generateEqFromBlt;

protected function isPositive
input Integer i;
algorithm
_ :=
match i
local
case _
equation
true = i > 0;
then ();
case _
then fail();
end match;
end isPositive;

public function getInputs
input DevsStruct st;
Expand All @@ -2418,6 +2436,7 @@ algorithm
case (DEVS_STRUCT(inVars=inVars),_)
equation
vars = Util.listMap(Util.listFlatten(inVars[index]),intAbs);
vars = Util.listFilter(vars,isPositive);
then vars;
end match;
end getInputs;
Expand Down Expand Up @@ -2556,6 +2575,11 @@ algorithm
print("- BackendQSS.generateConnections2\n");
then
fail();
case (_,_,_,_)
equation
print("Fail in generateConnections2\n");
then
fail();
end match;
end generateConnections2;

Expand Down Expand Up @@ -2595,7 +2619,7 @@ algorithm
curBlock_conns_temp = getDEVSblock_conns(blockIndex, rest_out_edges, rest_out_names, inVars, loopIndex+1, curBlock_conns_temp);
then
(curBlock_conns_temp);

case (_,_,_,_,_,_)
equation
print("- BackendQSS.getDEVSblock_conns failed\n");
Expand Down
64 changes: 50 additions & 14 deletions Compiler/susan_codegen/SimCode/SimCodeQSS.tpl
Expand Up @@ -146,6 +146,8 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
EXIT(retVal);
}
#endif

#define condition_rettype bool
void init_ompd();
void clean_ompd();

Expand All @@ -157,7 +159,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
//for QSS solver
double rel_accuracy = 1e-5;
double abs_accuracy = 1e-5;
char* method = "QSS3";
char* method = (char*)"QSS3";

double state_values(int state)
{
Expand Down Expand Up @@ -194,6 +196,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
init=true;
init_ompd();
}
return 0;
}

void clean_runtime()
Expand Down Expand Up @@ -274,7 +277,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
{
<%generateIntegrators(varInfo.numStateVars)%>
<%generateStaticBlocks(qssInfo,varInfo.numStateVars)%>
<%generateZeroCrossingFunctions(zeroCrossings,qssInfo)%>
<%generateZeroCrossingFunctions(zeroCrossings,qssInfo,varInfo.numStateVars)%>
<%generateCrossingDetector(zeroCrossings,qssInfo)%>
<%generateWhenBlocks(whenClauses,helpVarInfo)%>
Simulator
Expand All @@ -290,7 +293,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
}
IC
{
<%connections%>
<% connections %>
}
}

Expand Down Expand Up @@ -356,7 +359,7 @@ template functionQssWhen(list<SimWhenClause> whenClauses, list<HelpVarInfo> help
{
<% whenCases %>
}
restore_memory_state(mem_state);
return false;
}
>>
end functionQssWhen;
Expand All @@ -379,9 +382,11 @@ template functionQssWhen2(list<SimWhenClause> whenClauses, list<HelpVarInfo> hel
if (<% cond %>) {
<% equations %>
<%saves%>
restore_memory_state(mem_state);
return true;
} else {
<%saves%>
restore_memory_state(mem_state);
return false;
}
break;
Expand Down Expand Up @@ -440,8 +445,8 @@ template functionQssStaticBlocks(list<SimEqSystem> derivativEquations,list<ZeroC
case BackendQSS.QSSINFO(__) then
let &varDecls = buffer "" /*BUFD*/
let numStatic = listLength(eqs)
let zeroCross = generateZeroCrossingsEq(listLength(eqs),zeroCrossings,varDecls)
let staticFun = generateStaticFunc(derivativEquations,zeroCrossings,varDecls,DEVSstructure,eqs,outVarLst,nStates)
let zeroCross = generateZeroCrossingsEq(listLength(eqs),zeroCrossings,varDecls,DEVSstructure,outVarLst,nStates)

<<
int staticBlocks = <% numStatic %>;
Expand Down Expand Up @@ -479,7 +484,7 @@ end generateOutputs;
template generateInputs(BackendQSS.DevsStruct devsst, Integer index, list<BackendDAE.Var> varLst, Integer nStates)
"Generate inputs for static blocks"
::=
(BackendQSS.getInputs(devsst,intAdd(index,intAdd(1,nStates))) |> i hasindex i0 =>
(BackendQSS.getInputs(devsst,index) |> i hasindex i0 =>
<<
// Input <% i0 %> is var <% i %>
<% SimCodeC.cref(BackendVariable.varCref(listNth(varLst,intAdd(i,-1)))) %> = in[<% i0 %>];
Expand All @@ -495,9 +500,10 @@ template generateStaticFunc(list<SimEqSystem> odeEq,list<ZeroCrossing> zeroCross
<<

case <% i0 %>:
{
// Read inputs from in[]
#ifdef _OMC_OMPD
<% generateInputs(devsst,i0,varLst,nStates) %>
<% generateInputs(devsst,intAdd(intAdd(i0,nStates),1),varLst,nStates) %>
#endif
// Evalute the static function
Expand All @@ -508,17 +514,44 @@ template generateStaticFunc(list<SimEqSystem> odeEq,list<ZeroCrossing> zeroCross
<% generateOutputs(devsst,i0,varLst,nStates) %>
#endif
break;
}
>>
;separator="\n")
end generateStaticFunc;

template generateRelation(DAE.Operator op, String e1, String e2)
::=
match op
case LESS(__) then '<% e1 %> - <% e2 %>'
case LESSEQ(__) then '<% e1 %> - <% e2 %>'
case GREATER(__) then '<% e2 %> - <% e1 %>'
case GREATEREQ(__) then '<% e2 %> - <% e1 %>'
case EQUAL(__) then '<% e1 %> - <% e2 %>'
case NEQUAL(__) then '<% e2 %> - <% e1 %>'
end generateRelation;

template generateZCExp(DAE.Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
"Generate the ZC exp by substracting both arguments
author: fbergero"
::=
match exp
case e as RELATION(__) then
let e1 = SimCodeC.daeExp(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let e2 = SimCodeC.daeExp(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let sub = generateRelation(operator,e1,e2)
<<
<% sub %>
>>
else SimCodeC.error(sourceInfo(), 'Unhandled expression in SimCodeQSS.generateZCExp: <%ExpressionDump.printExpStr(exp)%>')
end generateZCExp;

template generateZeroCrossingsEq(Integer offset,list<ZeroCrossing> zeroCrossings,Text &varDecls /*BUFP*/)
template generateZeroCrossingsEq(Integer offset,list<ZeroCrossing> zeroCrossings,Text &varDecls /*BUFP*/,
BackendQSS.DevsStruct devsst,list<BackendDAE.Var> varLst,Integer nStates)
"Generate the cases for the zero crossings"
::=
(zeroCrossings |> ZERO_CROSSING(__) hasindex i0 =>
let &preExp = buffer "" /*BUFD*/
let zcExp = SimCodeC.daeExp(relation_, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let zcExp = generateZCExp(relation_, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
match relation_
case CALL(path=IDENT(name="sample")) then
<<
Expand All @@ -531,14 +564,17 @@ template generateZeroCrossingsEq(Integer offset,list<ZeroCrossing> zeroCrossings
<<

case <%intAdd(i0,offset)%>:
{
#ifdef _OMC_OMPD
// Read inputs from in[]
<% generateInputs(devsst,intAdd(offset,intAdd(i0,intAdd(1,nStates))),varLst,nStates) %>
#endif
// Evalute the ZeroCrossing function
<%preExp%>
// Write outputs to out[]
out[0] = <% zcExp %>;
break;
}
>>
;separator="\n")
end generateZeroCrossingsEq;
Expand All @@ -549,7 +585,7 @@ template functionQssUpdateDiscrete(list<SimEqSystem> allEquationsPlusWhen,list<Z
let &varDecls = buffer "" /*BUFD*/
let eqs = (allEquationsPlusWhen |> eq => generateDiscUpdate(BackendQSS.replaceZC(eq,zeroCrossings), zeroCrossings, &varDecls); separator="\n")
<<
void functionQssUpdateDiscrete(double t)
void function_updateDepend(double t, int index)
{
state mem_state;
time = t;
Expand Down Expand Up @@ -622,7 +658,7 @@ template generateStaticBlocks(QSSinfo qssInfo, Integer nStates)
;separator="\n")
end generateStaticBlocks;

template generateZeroCrossingFunctions(list<ZeroCrossing> zeroCrossings,QSSinfo qssInfo)
template generateZeroCrossingFunctions(list<ZeroCrossing> zeroCrossings,QSSinfo qssInfo,Integer nStates)
"Function to generate the crossing functions atomics for the DEVS structure"
::=
match qssInfo
Expand All @@ -631,10 +667,10 @@ template generateZeroCrossingFunctions(list<ZeroCrossing> zeroCrossings,QSSinfo
let numStatic = listLength(eqs)
let &preExp = buffer "" /*BUFD*/
(zeroCrossings |> ZERO_CROSSING(relation_ = DAE.RELATION()) hasindex i0 =>
<<Simulator
<<Simulator // Block # <% intAdd(intAdd(listLength(eqs),nStates),i0) %>
{
Path = modelica/modelica_qss_static.h // Crossing function <%i0%> for <% SimCodeC.daeExp(relation_, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/) %>
Parameters = 2.0, 1.0, <% intAdd(i0,listLength(eqs))%>.0 // Inputs, Outputs, Index
Parameters = <% BackendQSS.numInputs(qssInfo,intAdd(i0,intAdd(listLength(eqs),intAdd(nStates,1)))) %>.0, 1.0, <% intAdd(i0,listLength(eqs))%>.0 // Inputs, Outputs, Index
}
>>
;separator="\n")
Expand All @@ -651,7 +687,7 @@ template generateCrossingDetector(list<ZeroCrossing> zeroCrossings,QSSinfo qssIn
(zeroCrossings |> ZERO_CROSSING(relation_ = DAE.RELATION()) hasindex i0 =>
<<Simulator
{
Path = modelica/modelica_qss_crossdetect.h // Crossing detector <%i0%> for <% SimCodeC.daeExp(relation_, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/) %>
Path = modelica/modelica_qss_cross_detect.h // Crossing detector <%i0%> for <% SimCodeC.daeExp(relation_, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/) %>
Parameters = <% i0 %>.0
}
>>
Expand Down
8 changes: 1 addition & 7 deletions c_runtime/solver_qss/solver_qss.cpp
Expand Up @@ -285,13 +285,7 @@ main_qss(int argc, char**argv)
if (initRuntimeAndSimulation(argc, argv)) //initRuntimeAndSimulation returns 1 if an error occurs
return 1;

if (interactiveSimuation) {
//cout << "startInteractiveSimulation: " << version << endl;
retVal = startInteractiveSimulation(argc, argv);
} else {
//cout << "startNonInteractiveSimulation: " << version << endl;
retVal = startNonInteractiveSimulation(argc, argv);
}
retVal = startNonInteractiveSimulation(argc, argv);

deInitializeDataStruc(globalData);
free(globalData);
Expand Down

0 comments on commit b312514

Please sign in to comment.