- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » FMU Co-simulation : Inputs/Outputs of...
FMU Co-simulation : Inputs/Outputs of FMU Block
FMU Co-simulation : Inputs/Outputs of FMU Block
Hello,
I have a problem with fmu co-simulation. The problem that there isn’t any call of fmiSetReal function from OpenModelica so that all of the input variables remain constant during simulation.
As I read from OpenModelica forum that (https://www.openmodelica.org/forum/defa … bles#p4313 ) currently OpenModelica doesn’t fully supports fmu co-simulation import.
However in this forum, Addel gives some solutions for fixing OpenModelica : he says that I can add SetReal function in the code. (https://www.openmodelica.org/forum/defa … bles#p4317 ).
I done this but I get another error : [:0:0-0:0] Error: Too many equations, over-determined system. The model has 4 equation(s) and 3 variable(s).
I would like to know if I need to add something special in order to fmiSetReal function will be called by OpenModelica or do you have another solutions for fmiSetReal function
Best regards,
Dimitri
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
model e_battery_cs_st_FMU
constant String fmuLocation = "C:/Users/dimitri.tratkanov/Documents/FMU_SDK_MODIF/fmusdk_mna_ver22.06/fmu10/fmu/cs/e_battery.fmu";
constant String fmuWorkingDir = "C:/Users/DIMITR~1.TRA/AppData/Local/Temp/OpenModelica/OMEdit";
parameter Integer logLevel = 3 "log level used during the loading of FMU" annotation(Dialog(tab = "FMI", group = "Enable logging"));
parameter Boolean debugLogging = false "enables the FMU simulation logging" annotation(Dialog(tab = "FMI", group = "Enable logging"));
constant String mimeType = "";
constant Real timeout = 0.0;
constant Boolean visible = false;
constant Boolean interactive = false;
parameter Real StartTime = 0.0 "start time used to initialize the slave" annotation(Dialog(tab = "FMI", group = "Step time"));
parameter Real StopTime = 1.0 "stop time used to initialize the slave" annotation(Dialog(tab = "FMI", group = "Step time"));
parameter Real communicationStepSize = (StopTime - StartTime) / 500 "step size used by fmiDoStep" annotation(Dialog(tab = "FMI", group = "Step time"));
constant Boolean stopTimeDefined = false;
FMI1CoSimulation fmi1cs = FMI1CoSimulation(logLevel, fmuWorkingDir, "e_battery", debugLogging, fmuLocation, mimeType, timeout, visible, interactive, StartTime, stopTimeDefined, StopTime);
parameter Real n_cellule(start = 2.0) "number of parallel cells";
parameter Real m_cellule(start = 3.0) "number of series cells";
parameter Real nbr_warburg(start = 4.0) "number of Warburg cells";
parameter Real k_1(start = 0.9) "parameter k1 of model";
parameter Real k_2(start = 0.8) "parameter k2 of model";
Modelica.Blocks.Interfaces.RealInput I_req(start = 5.0) "Request of current" annotation(Placement(transformation(extent = {{-120,60},{-100,80}})));
Modelica.Blocks.Interfaces.RealOutput soc(start = 0.0) "State Of Charge" annotation(Placement(transformation(extent = {{100,60},{120,80}})));
Modelica.Blocks.Interfaces.RealOutput voltage(start = 0.0) "Number of I reference node" annotation(Placement(transformation(extent = {{100,35},{120,55}})));
parameter Real E(start = 3.2) "E";
Real flowControl;
class FMI1CoSimulation
extends ExternalObject;
function constructor
input Integer fmiLogLevel;
input String workingDirectory;
input String instanceName;
input Boolean debugLogging;
input String fmuLocation;
input String mimeType;
input Real timeOut;
input Boolean visible;
input Boolean interactive;
input Real tStart;
input Boolean stopTimeDefined;
input Real tStop;
output FMI1CoSimulation fmi1cs;
external "C" fmi1cs = FMI1CoSimulationConstructor_OMC(fmiLogLevel,workingDirectory,instanceName,debugLogging,fmuLocation,mimeType,timeOut,visible,interactive,tStart,stopTimeDefined,tStop) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end constructor;
function destructor
input FMI1CoSimulation fmi1cs;
external "C" FMI1CoSimulationDestructor_OMC(fmi1cs) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end destructor;
end FMI1CoSimulation;
package fmi1Functions
function fmi1DoStep
input FMI1CoSimulation fmi1cs;
input Real currentCommunicationPoint;
input Real communicationStepSize;
input Boolean newStep;
output Real outFlowControl;
external "C" outFlowControl = fmi1DoStep_OMC(fmi1cs,currentCommunicationPoint,communicationStepSize,newStep) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1DoStep;
function fmi1GetReal
input FMI1CoSimulation fmi1cs;
input Real realValuesReferences[:];
input Real inFlowStatesInput;
output Real realValues[size(realValuesReferences, 1)];
external "C" fmi1GetReal_OMC(fmi1cs,size(realValuesReferences, 1),realValuesReferences,inFlowStatesInput,realValues,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetReal;
function fmi1SetReal
input FMI1CoSimulation fmi1cs;
input Real realValuesReferences[:];
input Real realValues[size(realValuesReferences, 1)];
output Real out_Values[size(realValuesReferences, 1)];
external "C" fmi1SetReal_OMC(fmi1cs,size(realValuesReferences, 1),realValuesReferences,realValues,out_Values,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetReal;
function fmi1GetInteger
input FMI1CoSimulation fmi1cs;
input Real integerValuesReferences[:];
input Real inFlowStatesInput;
output Integer integerValues[size(integerValuesReferences, 1)];
external "C" fmi1GetInteger_OMC(fmi1cs,size(integerValuesReferences, 1),integerValuesReferences,inFlowStatesInput,integerValues,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetInteger;
function fmi1SetInteger
input FMI1CoSimulation fmi1cs;
input Real integerValuesReferences[:];
input Integer integerValues[size(integerValuesReferences, 1)];
output Real out_Values[size(integerValuesReferences, 1)];
external "C" fmi1SetInteger_OMC(fmi1cs,size(integerValuesReferences, 1),integerValuesReferences,integerValues,out_Values,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetInteger;
function fmi1GetBoolean
input FMI1CoSimulation fmi1cs;
input Real booleanValuesReferences[:];
input Real inFlowStatesInput;
output Boolean booleanValues[size(booleanValuesReferences, 1)];
external "C" fmi1GetBoolean_OMC(fmi1cs,size(booleanValuesReferences, 1),booleanValuesReferences,inFlowStatesInput,booleanValues,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetBoolean;
function fmi1SetBoolean
input FMI1CoSimulation fmi1cs;
input Real booleanValuesReferences[:];
input Boolean booleanValues[size(booleanValuesReferences, 1)];
output Real out_Values[size(booleanValuesReferences, 1)];
external "C" fmi1SetBoolean_OMC(fmi1cs,size(booleanValuesReferences, 1),booleanValuesReferences,booleanValues,out_Values,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetBoolean;
function fmi1GetString
input FMI1CoSimulation fmi1cs;
input Real stringValuesReferences[:];
input Real inFlowStatesInput;
output String stringValues[size(stringValuesReferences, 1)];
external "C" fmi1GetString_OMC(fmi1cs,size(stringValuesReferences, 1),stringValuesReferences,inFlowStatesInput,stringValues,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1GetString;
function fmi1SetString
input FMI1CoSimulation fmi1cs;
input Real stringValuesReferences[:];
input String stringValues[size(stringValuesReferences, 1)];
output Real out_Values[size(stringValuesReferences, 1)];
external "C" fmi1SetString_OMC(fmi1cs,size(stringValuesReferences, 1),stringValuesReferences,stringValues,out_Values,2) annotation(Library = {"OpenModelicaFMIRuntimeC","fmilib","shlwapi"});
end fmi1SetString;
end fmi1Functions;
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;
equation
{I_req} = fmi1Functions.fmi1SetReal(fmi1cs, {5.0}, {I_req});
flowControl = fmi1Functions.fmi1DoStep(fmi1cs, time, communicationStepSize, true);
{voltage} = fmi1Functions.fmi1GetReal(fmi1cs, {6.0}, flowControl);
{soc} = fmi1Functions.fmi1GetReal(fmi1cs, {7.0}, flowControl);
end e_battery_cs_st_FMU;
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
Which version of OpenModelica are you using?
The suggestion i proposed in (https://www.openmodelica.org/forum/defa … les#p4313) was implemented a long back.
I can see that your generated code is still old.
Adeel.
- adeas
- 454 Posts
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
By latest version you mean what? I need to know the revision.
I would also like to see the generated code from the latest version.
Adeel.
- adeas
- 454 Posts
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
This is the code of OpenModelica 1.9.2 (r25117). In this case I haven't any error but still the function fmiSetReal do not change the input values.
Code:
model e_battery_cs_st_FMU
constant String fmuLocation = "file://C:/Users/DIMITR~1.TRA/AppData/Local/Temp/OpenModelica/OMEdit/resources";
constant String fmuWorkingDir = "C:/Users/DIMITR~1.TRA/AppData/Local/Temp/OpenModelica/OMEdit";
parameter Integer logLevel = 3 "log level used during the loading of FMU" annotation(Dialog(tab = "FMI", group = "Enable logging"));
parameter Boolean debugLogging = false "enables the FMU simulation logging" annotation(Dialog(tab = "FMI", group = "Enable logging"));
constant String mimeType = "";
constant Real timeout = 0.0;
constant Boolean visible = false;
constant Boolean interactive = false;
parameter Real startTime = 0.0 "start time used to initialize the slave" annotation(Dialog(tab = "FMI", group = "Step time"));
parameter Real stopTime = 1.0 "stop time used to initialize the slave" annotation(Dialog(tab = "FMI", group = "Step time"));
parameter Real numberOfSteps = 500 annotation(Dialog(tab = "FMI", group = "Step time"));
parameter Real communicationStepSize = (stopTime - startTime) / numberOfSteps "step size used by fmiDoStep" annotation(Dialog(tab = "FMI", group = "Step time"));
constant Boolean stopTimeDefined = true;
parameter Real n_cellule(start = 2.0, fixed = false) "number of parallel cells";
parameter Real m_cellule(start = 3.0, fixed = false) "number of series cells";
parameter Real nbr_warburg(start = 4.0, fixed = false) "number of Warburg cells";
parameter Real k_1(start = 0.9, fixed = false) "parameter k1 of model";
parameter Real k_2(start = 0.8, fixed = false) "parameter k2 of model";
Modelica.Blocks.Interfaces.RealInput I_req(start = 5.0, fixed = false) "Request of current" annotation(Placement(transformation(extent = {{-120, 60}, {-100, 80}})));
Modelica.Blocks.Interfaces.RealOutput soc(start = 0.0, fixed = false) "State Of Charge" annotation(Placement(transformation(extent = {{100, 60}, {120, 80}})));
Modelica.Blocks.Interfaces.RealOutput voltage(start = 0.0, fixed = false) "Number of I reference node" annotation(Placement(transformation(extent = {{100, 35}, {120, 55}})));
parameter Real E(start = 3.2, fixed = false) "E";
protected
FMI1CoSimulation fmi1cs = FMI1CoSimulation(logLevel, fmuWorkingDir, "e_battery", debugLogging, fmuLocation, mimeType, timeout, visible, interactive, startTime, stopTimeDefined, stopTime);
parameter Real flowInitialized(fixed = false);
Real flowStep;
Real fmi_input_I_req;
initial equation
flowInitialized = fmi1Functions.fmi1InitializeSlave(fmi1cs, 1);
equation
{soc, voltage} = fmi1Functions.fmi1GetReal(fmi1cs, {6.0, 7.0}, flowInitialized);
{fmi_input_I_req} = fmi1Functions.fmi1SetReal(fmi1cs, {5.0}, {I_req});
flowStep = fmi1Functions.fmi1DoStep(fmi1cs, time, communicationStepSize, true, flowInitialized);
protected
class FMI1CoSimulation
extends ExternalObject;
function constructor
input Integer fmiLogLevel;
input String workingDirectory;
input String instanceName;
input Boolean debugLogging;
input String fmuLocation;
input String mimeType;
input Real timeOut;
input Boolean visible;
input Boolean interactive;
input Real tStart;
input Boolean stopTimeDefined;
input Real tStop;
output FMI1CoSimulation fmi1cs;
external "C" fmi1cs = FMI1CoSimulationConstructor_OMC(fmiLogLevel, workingDirectory, instanceName, debugLogging, fmuLocation, mimeType, timeOut, visible, interactive, tStart, stopTimeDefined, tStop) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end constructor;
function destructor
input FMI1CoSimulation fmi1cs;
external "C" FMI1CoSimulationDestructor_OMC(fmi1cs) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end destructor;
end FMI1CoSimulation;
package fmi1Functions
function fmi1InitializeSlave
input FMI1CoSimulation fmi1cs;
input Real preInitialized;
output Real postInitialized = preInitialized;
external "C" fmi1InitializeSlave_OMC(fmi1cs) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1InitializeSlave;
function fmi1DoStep
input FMI1CoSimulation fmi1cs;
input Real currentCommunicationPoint;
input Real communicationStepSize;
input Boolean newStep;
input Real preInitialized;
output Real postInitialized = preInitialized;
external "C" fmi1DoStep_OMC(fmi1cs, currentCommunicationPoint, communicationStepSize, newStep) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1DoStep;
function fmi1GetReal
input FMI1CoSimulation fmi1cs;
input Real realValuesReferences[:];
input Real inFlowStatesInput;
output Real realValues[size(realValuesReferences, 1)];
external "C" fmi1GetReal_OMC(fmi1cs, size(realValuesReferences, 1), realValuesReferences, inFlowStatesInput, realValues, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1GetReal;
function fmi1SetReal
input FMI1CoSimulation fmi1cs;
input Real realValuesReferences[:];
input Real realValues[size(realValuesReferences, 1)];
output Real out_Values[size(realValuesReferences, 1)];
external "C" fmi1SetReal_OMC(fmi1cs, size(realValuesReferences, 1), realValuesReferences, realValues, out_Values, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1SetReal;
function fmi1GetInteger
input FMI1CoSimulation fmi1cs;
input Real integerValuesReferences[:];
input Real inFlowStatesInput;
output Integer integerValues[size(integerValuesReferences, 1)];
external "C" fmi1GetInteger_OMC(fmi1cs, size(integerValuesReferences, 1), integerValuesReferences, inFlowStatesInput, integerValues, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1GetInteger;
function fmi1SetInteger
input FMI1CoSimulation fmi1cs;
input Real integerValuesReferences[:];
input Integer integerValues[size(integerValuesReferences, 1)];
output Real out_Values[size(integerValuesReferences, 1)];
external "C" fmi1SetInteger_OMC(fmi1cs, size(integerValuesReferences, 1), integerValuesReferences, integerValues, out_Values, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1SetInteger;
function fmi1GetBoolean
input FMI1CoSimulation fmi1cs;
input Real booleanValuesReferences[:];
input Real inFlowStatesInput;
output Boolean booleanValues[size(booleanValuesReferences, 1)];
external "C" fmi1GetBoolean_OMC(fmi1cs, size(booleanValuesReferences, 1), booleanValuesReferences, inFlowStatesInput, booleanValues, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1GetBoolean;
function fmi1SetBoolean
input FMI1CoSimulation fmi1cs;
input Real booleanValuesReferences[:];
input Boolean booleanValues[size(booleanValuesReferences, 1)];
output Boolean out_Values[size(booleanValuesReferences, 1)];
external "C" fmi1SetBoolean_OMC(fmi1cs, size(booleanValuesReferences, 1), booleanValuesReferences, booleanValues, out_Values, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1SetBoolean;
function fmi1GetString
input FMI1CoSimulation fmi1cs;
input Real stringValuesReferences[:];
input Real inFlowStatesInput;
output String stringValues[size(stringValuesReferences, 1)];
external "C" fmi1GetString_OMC(fmi1cs, size(stringValuesReferences, 1), stringValuesReferences, inFlowStatesInput, stringValues, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1GetString;
function fmi1SetString
input FMI1CoSimulation fmi1cs;
input Real stringValuesReferences[:];
input String stringValues[size(stringValuesReferences, 1)];
output String out_Values[size(stringValuesReferences, 1)];
external "C" fmi1SetString_OMC(fmi1cs, size(stringValuesReferences, 1), stringValuesReferences, stringValues, out_Values, 2) annotation(Library = {"OpenModelicaFMIRuntimeC", "fmilib"});
end fmi1SetString;
end fmi1Functions;
annotation(experiment(StartTime = 0.0, StopTime = 1.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 e_battery_cs_st_FMU;
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
Does this problem still persist?
I wrote a FMI 1.0 CS fmu in Golang which works fine with e.g. fmpy, but
when imported into OpenModelica (1.16, 1.18-nightly) "fmiSetReal" is never called (as observed above/before)
fmiDoStep and fmiGetReal and all other ".so" functions are called as expected.
Could the problem have started with the transition to C++ for the simulator?
I found that there is an empty function body for ::SetReal here:
https://github.com/OpenModelica/OpenMod … .cpp#L1245
If there is a chance that only this little code is missing, I could try and provide a fix,
only if it is not, I would rather not spend the time to setup the build environment.
My FMUs also support FMI 2.0 CS and run well in OMSimulator, but working with SSP-Models seems a lot more uncomfortable,
especially because every bloc has to be exported to fmu, and there is no nice element browser...
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
Hey thomaskurz,
this thread is already 5 years old and somewhat off-topic. You should probably start a new one.
But while we are here:
We will drop support for FMI 1.0 for OpenModelica because:
a) It never worked as intended in OpenModelica
and
b) It is out of date and not supported by that many tools. FMI 2.0 is the current standard supported by more tools and FMI 3.0 is already available in an alpha release.
Also, as you noticed, the FMI import changed in OMEdit. It is not longer possible to import FMUs as Modelica models. They were no legal model and the new Frontend doesn't work with them. We want to add some way so users can link FMUs with Modelica models, but at the moment you have to export your other Modelica models as FMUs and link those up in a composite model (SSP).
So my advise:
Targeting FMI 2.0 (or even 3.0?) could be a better plan for your go FMU. If your model works as a 2.0 CS FMUs with OMSimulator you should stick with that, we will keep supporting FMI 2.0 for a while.
- AnHeuermann
- 52 Posts
Re: FMU Co-simulation : Inputs/Outputs of FMU Block
Hi Andreas, thanks for the quick and helpful response and guidance.
For OMSimulator users it would certainly be nice if they could drag Modelica models from the tree browser and directly drop them as new subsystem in the composite model view without the manual export-to-fmu-then-load-from-filesystem steps, but as there is a feasible way, this might be kept low priority.
Therefore as you recommended I will keep using OMSimulator and the SSP editor and look forward to the bright future :-)
Best Regards, Thomas
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » FMU Co-simulation : Inputs/Outputs of...