ModelicaStandardLibrary icon indicating copy to clipboard operation
ModelicaStandardLibrary copied to clipboard

Strange results when exporting an FMU to simulate in an external tool (both with model exchange and co-simulation)

Open thomash303 opened this issue 8 months ago • 3 comments

This question is regarding the FMU, so I'm not sure if the MSL GitHub is the right place to be asking but I wasn't sure where else to check.

I have been successfully running simulations of a mechanical system that I have in OpenModelica, but once I export the same system as an FMU and run it in MATLAB I get different results.

This is the system I am trying to simulate; I am driving with a simple sine wave into a position source to dictate the motion of the body (the parameter values are not set for the particular application, and thus the results are a little exaggerated).

Image

The results for the position (y1), which is the same as the sine input and the velocity (y11) are shown below; both of which seem reasonable.

Image

However, for my application I want to drive my model from an external source, which in this case is the same sine wave, but in a model-exchange (or co-sim I tried both) in MATLAB. Here is the Modelica model:

Image

Here is a snapshot of the Simulink environment with that FMU configured with the same sine wave input.

Image

However, I get vastly different results. The position response (blue) is correct, but any dynamic response or velocity (yellow) all return as zero (shown below).

Image

I assume some input is going to the device if it does have the correct position response, but nothing else makes any sense. It is the same model in Modelica, so I am confused how I am getting different results. This happens even with the co-sim which leads me to believe it is something with my Modelica model or the FMI not as much from Simulink. I would greatly appreciate any support, thank you!

thomash303 avatar May 08 '25 20:05 thomash303

As a very quick response: Did you have a look at Modelica.Mechanics.Translational.Examples.GenerationOfFMUs?

beutlich avatar May 09 '25 16:05 beutlich

Without investigating your example in detail: Be aware you transfer Position s into the FMU. If the derivatives Velocity v = der(s) and Acceleartion a = der(v) are needed inside the FMU, those are not automatically transferred - you have to do that "manually". Have a look at "adaptors" and their documentation. Especially when using "CoSimulation" it is impossible for the FMU to calculate deviations. Keep in mind that only at the time instances of communication the FMU gets new information.

AHaumer avatar May 09 '25 16:05 AHaumer

Thank you @beutlich and @AHaumer for your prompt responses. I think your initial assumption is correct, I require the velocity and the acceleration in my model, which is currently not being done when this is being implemented through the FMI. This was certainly a complete oversight on my end, and I appreciate the clarification.

I will provide further details on my application, which might provide more insight into what I am actually trying to accomplish. I have a Modelica model that I want to create an FMU for, where I dictate the position 1D and then output the force response. In the OpenModelica environment, I have been using a 1D translational position source, which I excite with a sine wave, and which I have been able to successfully validate.

Image

However, it seems whenever I try to import an FMU with that configuration with a position source into MATLAB I get this error:

Image

I only switched to the FMI adapter as an alternative to the position source. On a trial-and-error basis, it does seem like the error only occurs when I include that position source. I assume there might be something wrong in my formulation, but again I am confused why it works in OpenModelica and not through the FMU (unlike the FMU adapter I believe I only need to specify the 1D reference position as output).

Thank you so much for any insight you might have,

Thomas

thomash303 avatar May 13 '25 14:05 thomash303