Skip to content

Commit

Permalink
- update generated FMI import model
Browse files Browse the repository at this point in the history
   (works now with new CSimulationRuntime also on linux)
 - adjust FMI export for new CSimulationRuntime



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10630 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 2, 2011
1 parent 1f6ccae commit 9bbb249
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 95 deletions.
62 changes: 0 additions & 62 deletions Compiler/susan_codegen/SimCode/CodegenFMU.tpl
Expand Up @@ -394,7 +394,6 @@ case SIMCODE(__) then
#endif
void setStartValues(ModelInstance *comp);
fmiStatus getEventIndicator(ModelInstance* comp, fmiReal eventIndicators[]);
void eventUpdate(ModelInstance* comp, fmiEventInfo* eventInfo);
fmiReal getReal(ModelInstance* comp, const fmiValueReference vr);
fmiStatus setReal(ModelInstance* comp, const fmiValueReference vr, const fmiReal value);
Expand All @@ -411,7 +410,6 @@ case SIMCODE(__) then
#include "fmu_model_interface.c"
<%setStartValues(modelInfo)%>
<%getEventIndicatorFunction(simCode)%>
<%eventUpdateFunction(simCode)%>
<%getRealFunction(modelInfo)%>
<%setRealFunction(modelInfo)%>
Expand Down Expand Up @@ -595,66 +593,6 @@ case SIMCODE(__) then
>>
end eventUpdateFunction;

template getEventIndicatorFunction(SimCode simCode)
"Generates get event indicator function for c file."
::=
match simCode
case SIMCODE(__) then
let &varDecls = buffer "" /*BUFD*/
let zeroCrossingsCode = zeroCrossingsTpl2_fmu(zeroCrossings, &varDecls /*BUFD*/)
<<
// Used to get event indicators
fmiStatus getEventIndicator(ModelInstance* comp, fmiReal eventIndicators[]) {
int res = function_onlyZeroCrossings(comp->fmuData, eventIndicators, &(comp->fmuData->localData[0]->timeValue));
if (res == 0) return fmiOK;
return fmiError;
}

>>
end getEventIndicatorFunction;


template zeroCrossingsTpl2_fmu(list<ZeroCrossing> zeroCrossings, Text &varDecls /*BUFP*/)
"Generates code for zero crossings."
::=

(zeroCrossings |> ZERO_CROSSING(__) hasindex i0 =>
zeroCrossingTpl2_fmu(i0, relation_, &varDecls /*BUFD*/)
;separator="\n")
end zeroCrossingsTpl2_fmu;

template zeroCrossingTpl2_fmu(Integer index1, Exp relation, Text &varDecls /*BUFP*/)
"Generates code for a zero crossing."
::=
match relation
case RELATION(__) then
let &preExp = buffer "" /*BUFD*/
let e1 = daeExp(exp1, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let op = zeroCrossingOpFunc_fmu(operator)
let e2 = daeExp(exp2, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<%preExp%>
FMIZEROCROSSING(<%index1%>, <%op%>(<%e1%>_, <%e2%>));
>>
case CALL(path=IDENT(name="sample"), expLst={start, interval}) then
<< >>
else
<<
// UNKNOWN ZERO CROSSING for <%index1%>
>>
end zeroCrossingTpl2_fmu;

template zeroCrossingOpFunc_fmu(Operator op)
"Generates zero crossing function name for operator."
::=
match op
case LESS(__) then "FmiLess"
case GREATER(__) then "FmiGreater"
case LESSEQ(__) then "FmiLessEq"
case GREATEREQ(__) then "FmiGreaterEq"

end zeroCrossingOpFunc_fmu;

template getRealFunction(ModelInfo modelInfo)
"Generates getReal function for c file."
::=
Expand Down
8 changes: 7 additions & 1 deletion FMI/import/source/Makefile.in
Expand Up @@ -22,12 +22,18 @@ all: all-before $(BIN) all-after $(LIB)

$(LIB): fmuWrapper.o
$(AR) $@ $<
cp $@ ../../../build/lib/omc2/libfmuWrapper.a

fmuModelica.h: fmuModelica.tmp
sed 's/"/\\"/g' $< | sed 's/^/"/' | sed 's/$$/\\n"/' > $@.tmp
mv $@.tmp $@

moGenerator.o: fmuModelica.h
fmuModelica.unix.h: fmuModelica.unix.tmp
sed 's/"/\\"/g' $< | sed 's/^/"/' | sed 's/$$/\\n"/' > $@.tmp
mv $@.tmp $@


moGenerator.o: fmuModelica.h fmuModelica.unix.h

xmlparser.o: xmlparser.c xmlparser.h stack.h

Expand Down
285 changes: 285 additions & 0 deletions FMI/import/source/fmuModelica.unix.tmp
@@ -0,0 +1,285 @@
class fmuModelInst
extends ExternalObject;
function constructor
input fmuFunctions in_fmufun;
input String in_instName;
input String guid;
input fmuCallbackFuns functions;
input Boolean logFlag;
output fmuModelInst inst;
external "C" inst = fmiInstantiate(in_fmufun,in_instName,guid,functions,logFlag) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
input fmuModelInst in_inst;
external "C" fmiFreeDummy(in_inst) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuModelInst;

class fmuLoadDll "load dll from FMU"
extends ExternalObject;
function constructor
input fmuFunctions in_fmufun;
input String in_dllPath;
output fmuLoadDll dummy;
external "C" dummy = loadFMUDll(in_fmufun,in_dllPath) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
external "C" freeFMUDll() annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuLoadDll;

class fmuFunctions "List of interface functions defined in FMI 1.0"
extends ExternalObject;
function constructor
input String mid;
input String dllPath;
output fmuFunctions fmufun;
external "C" fmufun = instantiateFMIFun(mid,dllPath) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
input fmuFunctions fmufun;
external "C" fmiFreeDummy(fmufun) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuFunctions;

class fmuCallbackFuns "Construct for callbacks defined in FMU"
extends ExternalObject;
function constructor
output fmuCallbackFuns functions;
external "C" functions = fmiCallbackFuns() annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
input fmuCallbackFuns functions;
external "C" fmiFreeDummy(functions) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuCallbackFuns;

function fmuSetTime
input fmuFunctions fmufun;
input fmuModelInst inst;
input Real in_time;
external "C" fmiSetT(fmufun,inst,in_time) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetTime;

function fmuSetContStates "set values of continuous states"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nx;
input Real x[nx];
external "C" fmiSetContStates(fmufun,in_inst,x,nx) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetContStates;

function fmuSetRealVR "set values of real variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nr;
input Integer in_vr[nr];
input Real in_rv[nr];
external "C" fmiSetRealVR(fmufun,in_inst,in_vr,in_rv,nr) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetRealVR;

function fmuSetIntegerVR "set values of integer variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer ni;
input Integer in_vr[ni];
input Integer in_iv[ni];
external "C" fmiSetIntegerVR(fmufun,in_inst,in_vr,in_iv,ni) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetIntegerVR;

function fmuSetStringVR "set values of string variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer ns;
input Integer in_vr[ns];
input String in_sv[ns];
external "C" fmiSetStringVR(fmufun,in_inst,in_vr,in_sv,ns) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetStringVR;

function fmuSetBooleanVR "set values of boolean variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nb;
input Integer in_vr[nb];
input Boolean in_bv[nb];
external "C" fmiSetBooleanVR(fmufun,in_inst,in_vr,in_bv,nb) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuSetBooleanVR;

function fmuGetContStates "get values of continuous states"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nx;
output Real in_x[nx];
external "C" fmiGetContStates(fmufun,in_inst,in_x,nx) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetContStates;

function fmuGetRealVR "get values of real variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nr;
input Integer in_vr[nr];
output Real rv[nr];
external "C" fmiGetRealVR(fmufun,in_inst,in_vr,rv,nr) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetRealVR;

function fmuGetIntegerVR "get values of integer variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer ni;
input Integer in_vr[ni];
output Integer iv[ni];
external "C" fmiGetIntegerVR(fmufun,in_inst,in_vr,iv,ni) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetIntegerVR;

function fmuGetStringVR "get values of string variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer ns;
input Integer in_vr[ns];
output String sv[ns];
external "C" fmiGetStringVR(fmufun,in_inst,in_vr,sv,ns) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetStringVR;

function fmuGetBooleanVR "get values of boolean variables via value references"
input fmuFunctions fmufun;
input fmuModelInst in_inst;
input Integer nb;
input Integer in_vr[nb];
output Boolean bv[nb];
external "C" fmiGetBooleanVR(fmufun,in_inst,in_vr,bv,nb) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetBooleanVR;

function fmuGetDer "function to calculate the derivatives"
input fmuFunctions fmufun;
input fmuModelInst inst;
input Integer nx;
input Real x[nx];
output Real der_x_out[nx];
external "C" fmiGetDer(fmufun,inst,der_x_out,nx,x) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetDer;

class fmuEventInfo
extends ExternalObject;
function constructor
output fmuEventInfo evtInfo;
external "C" evtInfo = fmiEvtInfo() annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
input fmuEventInfo evtInfo;
external "C" freefmiEvtInfo(evtInfo) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuEventInfo;

class fmuBoolean
extends ExternalObject;
function constructor
input Integer default;
output fmuBoolean fmuBool;
external "C" fmuBool = fmuBooleanInst(default) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end constructor;
function destructor
input fmuBoolean in_bool;
external "C" freefmuBooleanInst(in_bool) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end destructor;
end fmuBoolean;

function fmuInit
input fmuFunctions fmufun;
input fmuModelInst inst;
input Boolean tolControl;
input Real relTol;
input fmuEventInfo evtInfo;
external "C" fmiInit(fmufun,inst,tolControl,relTol,evtInfo) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuInit;

function fmuCompIntStep
input fmuFunctions fmufun;
input fmuModelInst inst;
input fmuBoolean stepEvt;
external "C" fmiCompIntStep(fmufun,inst,stepEvt) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuCompIntStep;

function fmuGetTimeEvt
input fmuEventInfo in_evtInfo;
input Real in_time;
input Real in_pretime;
input fmuBoolean in_timeEvt;
output Real out_nextTime;
external "C" fmiGetTimeEvent(in_evtInfo,in_time,in_pretime,in_timeEvt,out_nextTime) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetTimeEvt;

function fmuGetEventInd
input fmuFunctions fmufun;
input fmuModelInst inst;
input Integer nz;
output Real z[nz];
external "C" fmiGetEventInd(fmufun,inst,z,nz) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuGetEventInd;

function fmuStateEvtCheck
input fmuBoolean stateEvt;
input Integer nz;
input Real z[nz];
input Real prez[nz];
external "C" fmuStateEventCheck(stateEvt,nz,z,prez) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuStateEvtCheck;

function fmuEvtUpdate
input fmuFunctions in_fmufun;
input fmuModelInst in_inst;
input fmuEventInfo in_evtInfo;
input fmuBoolean timeEvt;
input fmuBoolean stepEvt;
input fmuBoolean stateEvt;
input fmuBoolean interMediateRes;
external "C" fmuEventUpdate(in_fmufun,in_inst,in_evtInfo,timeEvt,stepEvt,stateEvt,interMediateRes) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end fmuEvtUpdate;

function fmuFreeAll
input fmuFunctions in_fmufun;
input fmuModelInst in_inst;
input fmuCallbackFuns functions;
output fmuBoolean dummy;
external "C" fmuFreeAll(in_fmufun,in_inst,functions,dummy) annotation(Include = "#include <fmuWrapper.h>", Library = "fmuWrapper -ldl");
end fmuFreeAll;

function PrintState
input fmuModelInst in_fmu;
input Real in_time;
external "C" PrintModelStates(in_fmu,in_time) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end PrintState;

function printVariable
input Real var[:];
input Integer n;
input String varName;
external "C" printVariables(var,n,varName) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end printVariable;

function printIntVariable
input Integer var[:];
input Integer n;
input String varName;
external "C" printIntVariables(var,n,varName) annotation(Include="#include <fmuWrapper.h>",Library = "fmuWrapper -ldl");
end printIntVariable;

function Bool2Int
input Integer dim;
input Boolean in_bv[dim];
output Integer out_iv[dim];
algorithm
for i in 1:dim loop
out_iv[i]:=if in_bv[i] then 1 else 0;
end for;
end Bool2Int;

function Int2Bool
input Integer dim;
input Integer in_iv[dim];
output Boolean out_bv[dim];
algorithm
for i in 1:dim loop
out_bv[i]:=if in_iv[i] == 1 then true else false;
end for;
end Int2Bool;

0 comments on commit 9bbb249

Please sign in to comment.