CATIA-Systems / FMIKit-Simulink

Import and export Functional Mock-up Units with Simulink
Other
155 stars 50 forks source link

rtwsfcnfmi FMU containing an S-Function packed in MATLAB 2022b crashes MATLAB #475

Open benplay2 opened 2 months ago

benplay2 commented 2 months ago

Problem

FMUs packaged with rtwsfcnfmi.tlc from FMIKit 3.1 in MATLAB 2022b crash MATLAB when they are executed.

image

After a little debugging, I discovered that the error occurs in fmi2functions.c fmi2SetReal(): image

Replication

I created a test function to demonstrate the error state. Please see the zip file. Here's a snippet of the code:

% Perform a simple test to demonstrate an error state of FMIKit which
% causes MATLAB to crash.
% 
% From my testing with FMIKit v3.1, usage of the FMU created with 2021b
% works properly, but the one created using 2022b crashes MATLAB.

load_system(model)

cs = getActiveConfigSet(gcs);
switchTarget(cs, 'rtwsfcnfmi.tlc', []);

set_param(model, 'GenerateReport', 'off');
set_param(model, 'FMIType', 'CoSimulation');

set_param(model, 'SolverType', 'Fixed-step');

set_param(model, 'LoadBinaryMEX', loadSfunctionBinary); %Even if no s-Function in "model", this causes a crash when simulated (2022b)

slbuild(model);

close_system(model, 0);

%Now, open a new simulink model and create an FMU block of this and try to
%simulate. I used the built-in Simulink FMU block

fmikitErrorExamples.zip

Workaround

Using MATLAB 2021b to create the FMU seems to resolve the problem. Was there an S-Function API change between the versions?

System details:

t-sommer commented 2 months ago

Thanks for sharing the example, @benplay2. There seems to be an internal API change in 2022b.