- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » FMI communication Dymola->OM
FMI communication Dymola->OM
FMI communication Dymola->OM
To test FMI Dymola-to-OM communication I've tried with the classic bouncing ball model (that, as a mo file, runs well in both Dymola and OM):
Code:
model Bouncing
parameter Real yStart = 1;
parameter Real vStart = 0;
parameter Real dampCoeff = 0.8;
Real y(start = yStart);
Real v(start = vStart);
Real g(start = -9.81);
equation
der(y) = v;
der(v) = g;
when y < 0 then
reinit(v, -dampCoeff * v);
end when;
when abs(v) < 0.001 and y < 0.0001 then
g = 0;
reinit(v, 0);
end when;
annotation(uses(Modelica(version = "3.2")),
experiment(StopTime=4),
__Dymola_experimentSetupOutput);
end Bouncing;
I've created the FMU using the following Dymola 2014 options:
Type-> model exchange
Version -> 1.0
This generated the FMU shown below(as read by OMEdit), that crashes in OM r16523.
Any suggestions?
THIS IS THE FMU CODE:
Code:
model Bouncing_me_FMU
constant String fmuFile = "Y:/Documents/Dymola/Bouncing.fmu";
constant String fmuWorkingDir = "C:/Users/ADMINI~1/AppData/Local/Temp/OpenModelica/OMEdit";
constant Integer fmiLogLevel = 3;
constant Boolean debugLogging = false;
fmi1ImportInstance fmi = fmi1ImportInstance(context, fmuWorkingDir);
fmi1ImportContext context = fmi1ImportContext(fmiLogLevel);
fmi1EventInfo eventInfo;
parameter Real yStart(start = 1.0, fixed = true);
parameter Real vStart(start = 0.0, fixed = true);
parameter Real dampCoeff(start = 0.8, fixed = true);
Real y;
Real der_y_;
Real v;
Real der_v_;
Real g(start = -9.81, fixed = true);
constant Integer numberOfContinuousStates = 2;
Real fmi_x[numberOfContinuousStates] "States";
Real fmi_x_new[numberOfContinuousStates] "New States";
constant Integer numberOfEventIndicators = 6;
Real fmi_z[numberOfEventIndicators] "Events Indicators";
Boolean fmi_z_positive[numberOfEventIndicators];
parameter Real flowInstantiate(fixed = false);
Real flowTime;
parameter Real flowParamsStart(fixed = false);
Real flowStatesInputs;
Boolean callEventUpdate;
constant Boolean intermediateResults = false;
Boolean newStatesAvailable;
Integer fmi_status;
Real triggerDSSEvent;
Real nextEventTime;
initial algorithm
flowInstantiate:=fmiFunctions.fmi1InstantiateModel(fmi, "Bouncing", debugLogging);
flowParamsStart:=0;
eventInfo:=fmiFunctions.fmi1Initialize(fmi, eventInfo);
fmi_x:=fmiFunctions.fmi1GetContinuousStates(fmi, numberOfContinuousStates, flowParamsStart);
equation
flowTime = fmiFunctions.fmi1SetTime(fmi, time);
flowStatesInputs = fmiFunctions.fmi1SetContinuousStates(fmi, fmi_x, flowParamsStart + flowTime);
der(fmi_x) = fmiFunctions.fmi1GetDerivatives(fmi, numberOfContinuousStates, flowStatesInputs);
fmi_z = fmiFunctions.fmi1GetEventIndicators(fmi, numberOfEventIndicators, flowStatesInputs);
for i in 1:size(fmi_z, 1) loop
fmi_z_positive[i] = if not terminal() then fmi_z[i] > 0 else pre(fmi_z_positive[i]);
end for;
callEventUpdate = fmiFunctions.fmi1CompletedIntegratorStep(fmi, flowStatesInputs);
triggerDSSEvent = noEvent(if callEventUpdate then flowStatesInputs + 1.0 else flowStatesInputs - 1.0);
nextEventTime = fmiFunctions.fmi1nextEventTime(fmi, eventInfo, flowStatesInputs);
{y,der_y_,v,der_v_,g} = fmiFunctions.fmi1GetReal(fmi, {33554432.0,587202560.0,33554433.0,587202561.0,637534208.0}, flowStatesInputs);
algorithm
when {(change(fmi_z_positive[6]) or change(fmi_z_positive[5]) or change(fmi_z_positive[4]) or change(fmi_z_positive[3]) or change(fmi_z_positive[2]) or change(fmi_z_positive[1])) and not initial(),triggerDSSEvent > flowStatesInputs,nextEventTime < time} then
newStatesAvailable:=fmiFunctions.fmi1EventUpdate(fmi, intermediateResults, eventInfo, flowStatesInputs);
if newStatesAvailable then
fmi_x_new:=fmiFunctions.fmi1GetContinuousStates(fmi, numberOfContinuousStates, flowStatesInputs);
reinit(fmi_x[2], fmi_x_new[2]);
reinit(fmi_x[1], fmi_x_new[1]);
else
end if;
end when;
when terminal() then
fmi_status:=fmiFunctions.fmi1Terminate(fmi);
end when;
protected
class fmi1ImportContext
extends ExternalObject;
function constructor
input Integer fmiLogLevel;
output fmi1ImportContext context;
external "C" context = fmi1ImportContext_OMC(fmiLogLevel) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end constructor;
function destructor
input fmi1ImportContext context;
external "C" fmi1ImportFreeContext_OMC(context) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end destructor;
end fmi1ImportContext;
class fmi1ImportInstance
extends ExternalObject;
function constructor
input fmi1ImportContext context;
input String tempPath;
output fmi1ImportInstance fmi;
external "C" fmi = fmi1ImportInstance_OMC(context,tempPath) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end constructor;
function destructor
input fmi1ImportInstance fmi;
external "C" fmi1ImportFreeInstance_OMC(fmi) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end destructor;
end fmi1ImportInstance;
class fmi1EventInfo
extends ExternalObject;
function constructor
end constructor;
function destructor
input fmi1EventInfo eventInfo;
external "C" fmi1FreeEventInfo_OMC(eventInfo) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end destructor;
end fmi1EventInfo;
package fmiFunctions
function fmi1InstantiateModel
input fmi1ImportInstance fmi;
input String instanceName;
input Boolean debugLogging;
output Real outFlowInstantiate;
external "C" outFlowInstantiate = fmi1InstantiateModel_OMC(fmi,instanceName,debugLogging) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1InstantiateModel;
function fmi1Initialize
input fmi1ImportInstance fmi;
input fmi1EventInfo inEventInfo;
output fmi1EventInfo outEventInfo;
external "C" outEventInfo = fmi1Initialize_OMC(fmi,inEventInfo) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1Initialize;
function fmi1SetTime
input fmi1ImportInstance fmi;
input Real inTime;
output Real status;
external "C" status = fmi1SetTime_OMC(fmi,inTime) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetTime;
function fmi1GetContinuousStates
input fmi1ImportInstance fmi;
input Integer numberOfContinuousStates;
input Real inFlowParams;
output Real fmi_x[numberOfContinuousStates];
external "C" fmi1GetContinuousStates_OMC(fmi,numberOfContinuousStates,inFlowParams,fmi_x) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetContinuousStates;
function fmi1SetContinuousStates
input fmi1ImportInstance fmi;
input Real fmi_x[:];
input Real inFlowParams;
output Real outFlowStates;
external "C" outFlowStates = fmi1SetContinuousStates_OMC(fmi,size(fmi_x, 1),inFlowParams,fmi_x) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetContinuousStates;
function fmi1GetDerivatives
input fmi1ImportInstance fmi;
input Integer numberOfContinuousStates;
input Real inFlowStates;
output Real fmi_x[numberOfContinuousStates];
external "C" fmi1GetDerivatives_OMC(fmi,numberOfContinuousStates,inFlowStates,fmi_x) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetDerivatives;
function fmi1GetEventIndicators
input fmi1ImportInstance fmi;
input Integer numberOfEventIndicators;
input Real inFlowStates;
output Real fmi_z[numberOfEventIndicators];
external "C" fmi1GetEventIndicators_OMC(fmi,numberOfEventIndicators,inFlowStates,fmi_z) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetEventIndicators;
function fmi1GetReal
input fmi1ImportInstance fmi;
input Real realValuesReferences[:];
input Real inFlowStatesInput;
output Real realValues[size(realValuesReferences, 1)];
external "C" fmi1GetReal_OMC(fmi,size(realValuesReferences, 1),realValuesReferences,inFlowStatesInput,realValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetReal;
function fmi1SetReal
input fmi1ImportInstance fmi;
input Real realValuesReferences[:];
input Real realValues[size(realValuesReferences, 1)];
output Real outFlowParams;
external "C" outFlowParams = fmi1SetReal_OMC(fmi,size(realValuesReferences, 1),realValuesReferences,realValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetReal;
function fmi1GetInteger
input fmi1ImportInstance fmi;
input Real integerValuesReferences[:];
input Real inFlowStatesInput;
output Integer integerValues[size(integerValuesReferences, 1)];
external "C" fmi1GetInteger_OMC(fmi,size(integerValuesReferences, 1),integerValuesReferences,inFlowStatesInput,integerValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetInteger;
function fmi1SetInteger
input fmi1ImportInstance fmi;
input Real integerValuesReferences[:];
input Integer integerValues[size(integerValuesReferences, 1)];
output Real outFlowParams;
external "C" outFlowParams = fmi1SetInteger_OMC(fmi,size(integerValuesReferences, 1),integerValuesReferences,integerValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetInteger;
function fmi1GetBoolean
input fmi1ImportInstance fmi;
input Real booleanValuesReferences[:];
input Real inFlowStatesInput;
output Boolean booleanValues[size(booleanValuesReferences, 1)];
external "C" fmi1GetBoolean_OMC(fmi,size(booleanValuesReferences, 1),booleanValuesReferences,inFlowStatesInput,booleanValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetBoolean;
function fmi1SetBoolean
input fmi1ImportInstance fmi;
input Real booleanValuesReferences[:];
input Boolean booleanValues[size(booleanValuesReferences, 1)];
output Real outFlowParams;
external "C" outFlowParams = fmi1SetBoolean_OMC(fmi,size(booleanValuesReferences, 1),booleanValuesReferences,booleanValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetBoolean;
function fmi1GetString
input fmi1ImportInstance fmi;
input Real stringValuesReferences[:];
input Real inFlowStatesInput;
output String stringValues[size(stringValuesReferences, 1)];
external "C" fmi1GetString_OMC(fmi,size(stringValuesReferences, 1),stringValuesReferences,inFlowStatesInput,stringValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetString;
function fmi1SetString
input fmi1ImportInstance fmi;
input Real stringValuesReferences[:];
input String stringValues[size(stringValuesReferences, 1)];
output Real outFlowParams;
external "C" outFlowParams = fmi1SetString_OMC(fmi,size(stringValuesReferences, 1),stringValuesReferences,stringValues) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetString;
function fmi1EventUpdate
input fmi1ImportInstance fmi;
input Boolean intermediateResults;
input fmi1EventInfo inEventInfo;
input Real inFlowStates;
output Boolean outNewStatesAvailable;
external "C" outNewStatesAvailable = fmi1EventUpdate_OMC(fmi,intermediateResults,inEventInfo,inFlowStates) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1EventUpdate;
function fmi1nextEventTime
input fmi1ImportInstance fmi;
input fmi1EventInfo inEventInfo;
input Real inFlowStates;
output Real outNewnextTime;
external "C" outNewnextTime = fmi1nextEventTime_OMC(fmi,inEventInfo,inFlowStates) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1nextEventTime;
function fmi1CompletedIntegratorStep
input fmi1ImportInstance fmi;
input Real inFlowStates;
output Boolean outCallEventUpdate;
external "C" outCallEventUpdate = fmi1CompletedIntegratorStep_OMC(fmi,inFlowStates) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1CompletedIntegratorStep;
function fmi1Terminate
input fmi1ImportInstance fmi;
output Integer status;
external "C" status = fmi1Terminate_OMC(fmi) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1Terminate;
end fmiFunctions;
package fmiStatus
constant Integer fmiOK = 0;
constant Integer fmiWarning = 1;
constant Integer fmiDiscard = 2;
constant Integer fmiError = 3;
constant Integer fmiFatal = 4;
constant Integer fmiPending = 5;
end fmiStatus;
annotation(experiment(StartTime = 0.0, StopTime = 4.0, Tolerance = 0.0001));
annotation(Icon(graphics = {Rectangle(extent = {{-100,100},{100,-100}}, lineColor = {0,0,0}, fillColor = {240,240,240}, fillPattern = FillPattern.Solid, lineThickness = 0.5),Text(extent = {{-100,40},{100,0}}, lineColor = {0,0,0}, textString = "%name"),Text(extent = {{-100,-50},{100,-90}}, lineColor = {0,0,0}, textString = "V1.0")}));
end Bouncing_me_FMU;
- ceraolo
- 147 Posts
Re: FMI communication Dymola->OM
I cannot run the generated code. I need the FMU for reproducing the crash and testing.
Adeel.
- adeas
- 454 Posts
Re: FMI communication Dymola->OM
Sorry for the late reply.
I did not succeed to upload the file. I will send it to you soon in a separate e-mail.
Massimo
- ceraolo
- 147 Posts
Re: FMI communication Dymola->OM
https://trac.openmodelica.org/OpenModelica/newticket is better for bug reports since the upload system works better
- sjoelund.se
- 1700 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » FMI communication Dymola->OM