Skip to content

Commit 88a0315

Browse files
committed
Code generation for the stand-alone solver of QSS methods
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8912 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 4a5b83f commit 88a0315

File tree

3 files changed

+140
-11
lines changed

3 files changed

+140
-11
lines changed

Compiler/BackEnd/BackendQSS.mo

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,8 +3581,6 @@ algorithm
35813581
end matchcontinue;
35823582
end createIncreasingList;
35833583

3584-
3585-
35863584
public function createListFromElement
35873585
input Type_a elem;
35883586
output list<Type_a> outList;
@@ -3598,6 +3596,80 @@ algorithm
35983596
end matchcontinue;
35993597
end createListFromElement;
36003598

3599+
public function getAllInputs
3600+
input QSSinfo qssInfo;
3601+
output list<Integer> vars_tuple;
3602+
algorithm
3603+
vars_tuple :=
3604+
match (qssInfo)
3605+
local
3606+
array<list<list<Integer>>> inVars "input connections for each DEVS block";
3607+
case QSSINFO(_,DEVS_STRUCT(_,_,_,inVars),_,_)
3608+
then convertArrayToFlatList(inVars,1,{});
3609+
end match;
3610+
end getAllInputs;
3611+
3612+
protected function listPair
3613+
input list<Integer> l;
3614+
input Integer i;
3615+
output list<Integer> o;
3616+
algorithm
3617+
o :=
3618+
match (l,i)
3619+
local list<Integer> rest;
3620+
Integer h;
3621+
case ((h::rest),i)
3622+
equation
3623+
rest = listPair(rest,i);
3624+
rest = listAppend({i,h},rest);
3625+
then rest;
3626+
case ({},_)
3627+
then {};
3628+
end match;
3629+
end listPair;
3630+
3631+
3632+
protected function convertArrayToFlatList
3633+
input array<list<list<Integer>>> vars;
3634+
input Integer index;
3635+
input list<Integer> res;
3636+
output list<Integer> vars_tuple;
3637+
algorithm
3638+
vars_tuple :=
3639+
matchcontinue (vars,index,res)
3640+
local list<list<Integer>> v;
3641+
list<Integer> vf;
3642+
case (_,_,_)
3643+
equation
3644+
true = (index <= arrayLength(vars));
3645+
v = vars[index];
3646+
vf = Util.listMap(Util.listFlatten(v),intAbs);
3647+
vf = Util.listFilter(vf,isPositive);
3648+
vf = Util.listMap1(vf,intSub,1);
3649+
vf = listPair(vf,index-1);
3650+
vf = listAppend(res,convertArrayToFlatList(vars,index+1,vf));
3651+
then vf;
3652+
case (_,_,_)
3653+
equation
3654+
false = (index <= arrayLength(vars));
3655+
then res;
3656+
end matchcontinue;
3657+
end convertArrayToFlatList;
3658+
3659+
3660+
public function getAllOutputs
3661+
input QSSinfo qssInfo;
3662+
output list<Integer> vars_tuple;
3663+
algorithm
3664+
vars_tuple :=
3665+
match (qssInfo)
3666+
local
3667+
array<list<list<Integer>>> outVars "output connections for each DEVS block";
3668+
case QSSINFO(_,DEVS_STRUCT(_,outVars,_,_),_,_)
3669+
then convertArrayToFlatList(outVars,1,{});
3670+
end match;
3671+
end getAllOutputs;
3672+
36013673
protected function replaceRowsArray
36023674
"function: getListofZeroCrossings
36033675
Takes as input the DAE and extracts the zero-crossings as well as the zero crosses that are
@@ -3612,7 +3684,6 @@ protected function replaceRowsArray
36123684
algorithm
36133685
incidenceMatOut := matchcontinue (incidenceMat, rowsInd, newRows)
36143686
local
3615-
36163687
Integer cur_ind;
36173688
list<Integer> rest_ind, cur_row;
36183689
list<list<Integer>> rest_rows;
@@ -3723,7 +3794,6 @@ algorithm
37233794
(temp);
37243795
end matchcontinue;
37253796
end findElementInList;
3726-
37273797
////////////////////////////////////////////////////////////////////////////////////////////////////
37283798
///// END OF PACKAGE
37293799
////////////////////////////////////////////////////////////////////////////////////////////////////

Compiler/susan_codegen/SimCode/SimCodeQSS.tpl

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
118118
startNonInteractiveSimulation(int, char**);
119119
int
120120
initRuntimeAndSimulation(int, char**);
121+
<%generateIncidenceMatrix(BackendQSS.generateConnections(qssInfo))%>
121122
extern int interactiveSimulation;
122-
char incidenceMatrix[0];
123-
char inputMatrix[0];
124-
char outputMatrix[0];
123+
<%generateInputVars(BackendQSS.getAllInputs(qssInfo))%>
124+
<%generateOutputVars(BackendQSS.getAllOutputs(qssInfo))%>
125125

126126
#ifndef _OMC_OMPD
127127
int
@@ -132,7 +132,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(varInfo=varInfo as VARINFO(__)))
132132
if (initRuntimeAndSimulation(argc, argv)) //initRuntimeAndSimulation returns 1 if an error occurs
133133
return 1;
134134
135-
if (interactiveSimuation) {
135+
if (interactiveSimulation) {
136136
//cout << "startInteractiveSimulation: " << version << endl;
137137
retVal = startInteractiveSimulation(argc, argv);
138138
} else {
@@ -542,6 +542,9 @@ template generateZCExp(DAE.Exp exp, Context context, Text &preExp /*BUFP*/, Text
542542
<<
543543
<% sub %>
544544
>>
545+
case e as CALL(path=IDENT(name="sample")) then
546+
<<
547+
>>
545548
else SimCodeC.error(sourceInfo(), 'Unhandled expression in SimCodeQSS.generateZCExp: <%ExpressionDump.printExpStr(exp)%>')
546549
end generateZCExp;
547550

@@ -756,7 +759,7 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
756759
let extraCflags = match sopt case SOME(s as SIMULATION_SETTINGS(__)) then
757760
'<%if s.measureTime then "-D_OMC_MEASURE_TIME "%> <%match s.method
758761
case "inline-euler" then "-D_OMC_INLINE_EULER"
759-
case "inline-rungekutta" then "-D_OMC_INLINE_RK"%>'
762+
case "inline-rungekutta" then "-D_OMC_INLINE_RK"%> -D_OMC_OMPD_LIB'
760763
<<
761764
# Makefile generated by OpenModelica
762765

@@ -768,15 +771,15 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
768771
EXEEXT=<%makefileParams.exeext%>
769772
DLLEXT=<%makefileParams.dllext%>
770773
CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>
771-
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -I"<%makefileParams.omhome%>/include/omc" <%makefileParams.cflags%> -D_OMC_QSS -g
774+
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -I"<%makefileParams.omhome%>/include/omc" <%makefileParams.cflags%> -D_OMC_QSS -g -D_OMC_OMPD
772775
LDFLAGS=-L"<%makefileParams.omhome%>/lib/omc" <%makefileParams.ldflags%>
773776
SENDDATALIBS=<%makefileParams.senddatalibs%>
774777
PERL=perl
775778

776779
.PHONY: <%fileNamePrefix%>
777780
<%fileNamePrefix%>: <%fileNamePrefix%>.conv.cpp <%fileNamePrefix%>_functions.cpp <%fileNamePrefix%>_functions.h <%fileNamePrefix%>_records.c
778781
<%\t%> $(CXX) -I. -o <%fileNamePrefix%>$(EXEEXT) <%fileNamePrefix%>.conv.cpp <%fileNamePrefix%>_functions.cpp <%dirExtra%> <%libsPos1%> <%libsPos2%> -lsim_ompd -linteractive $(CFLAGS) $(SENDDATALIBS) $(LDFLAGS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%> <%fileNamePrefix%>_records.c
779-
<%fileNamePrefix%>.conv.cpp: <%fileNamePrefix%>.cpp
782+
<%fileNamePrefix%>.conv.cpp: modelica_funcs.cpp
780783
<%\t%> $(PERL) <%makefileParams.omhome%>/share/omc/scripts/convert_lines.pl $< $@.tmp
781784
<%\t%> @mv $@.tmp $@
782785
>>
@@ -816,6 +819,46 @@ template simulationFunctionsFile(String filePrefix, list<Function> functions, li
816819
/* adpro: leave a newline at the end of file to get rid of warnings! */
817820
end simulationFunctionsFile;
818821

822+
template generateIncidenceMatrix(list<list<Integer>> conns)
823+
"Generate the incidence matrix for the stand alone solver"
824+
::=
825+
<<
826+
int incidenceRows = <% listLength(conns) %>;
827+
int incidenceMatrix[] = { <% conns |> c =>
828+
<<
829+
<% listNth(c,0) %>,<% listNth(c,2) %>
830+
>>
831+
;separator="," %> };
832+
>>
833+
end generateIncidenceMatrix;
834+
835+
template generateInputVars(list<Integer> vars_tuple)
836+
"Generate the input vars for the stand alone solver"
837+
::=
838+
<<
839+
int inputMatrix[] = { <% vars_tuple |> i =>
840+
<<
841+
<% i %>
842+
>>
843+
; separator="," %> };
844+
int inputRows = <% intDiv(listLength(vars_tuple),2) %>;
845+
>>
846+
end generateInputVars;
847+
848+
template generateOutputVars(list<Integer> vars_tuple)
849+
"Generate the output vars for the stand alone solver"
850+
::=
851+
<<
852+
int outputMatrix[] = { <% vars_tuple |> i =>
853+
<<
854+
<% i %>
855+
>>
856+
; separator="," %> };
857+
int outputRows = <% intDiv(listLength(vars_tuple),2) %>;
858+
>>
859+
end generateOutputVars;
860+
861+
819862

820863
end SimCodeQSS;
821864

Compiler/susan_codegen/SimCode/SimCodeTV.mo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ package builtin
2727
output Integer c;
2828
end intAdd;
2929

30+
31+
function intDiv
32+
input Integer a;
33+
input Integer b;
34+
output Integer c;
35+
end intDiv;
36+
3037
function arrayList
3138
replaceable type TypeVar subtypeof Any;
3239
input TypeVar[:] arr;
@@ -1997,6 +2004,15 @@ package BackendQSS
19972004
output list<BackendDAE.Var> states;
19982005
end getStates;
19992006

2007+
function getAllInputs
2008+
input QSSinfo qssInfo;
2009+
output list<Integer> vars_tuple;
2010+
end getAllInputs;
2011+
2012+
function getAllOutputs
2013+
input QSSinfo qssInfo;
2014+
output list<Integer> vars_tuple;
2015+
end getAllOutputs;
20002016

20012017
end BackendQSS;
20022018

0 commit comments

Comments
 (0)