Simcenter Amesim Import MATLAB Simulink FMU to AMESim

2021-07-12T05:10:38.000-0400
Simcenter Amesim Automation Connect Simcenter System Simulation Client for Git Simcenter Amesim Teamcenter Share

Summary


Details


Summery
Most of the time users are writing some codes in MATLAB. Sometimes, users don’t want to reinvest time on the same work to migrate these codes to AMESim.  By FMU and with few tricks it is possible now in minimum possible time. You can convert these codes to the Simulink function. Later on, you can create an FMU that can be further imported into the AMESim.  If you are very good in c programming, then you can also write a code using AMESet, which can help you to produce a Statistics submodel. In this case, you need not to depends on Simulink.
This demo will help user to
  • How to create an FMU of the Simulink MATLAB function?
  • How to create FMU in Simulink?
  • How to import this created FMU in AMESim?
  • How to carry out co-simulation?
Pre-requisite: -
  • Simulink with FMU license
  • At least AMESim base with FMU add on license
  • In addition to these other libraries as per your modeling requirements.
Before moving ahead, please remember the limitation of MATLAB Simulink FMU: -
  • You can create Simulink FMU with a fixed-step solver. Until MATLAB version R2021a (When we are publishing this article), it does not support the variable step solver.  
  • Simulink FMU does not support the model exchange FMU until we are publishing this article.
Considering the above facts, ame2slcosim’ is nearly providing similar options to the AMESim. These options will provide you more flexibility with solver selection and a simple process to carry out co-simulation. Indeed, ‘sl2ame’ can provide even better options compare to this method as of now. For more details about these options, please visit the AMESim help file.
We will try to create a Simulink MATLAB function to find out the statistics of the input signal. Below is the simple code of this Simulation function block
function [mean,stdev, var] = stats(currentVal)
 
% Calculates a statistical mean and a standard
% deviation for the values in vals.
 
persistent vals
 
if isempty(vals)
    vals = [];
end
 
vals = [vals currentVal];
 
len = length(vals);
mean = avg(vals,len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
var = (sum(((vals-avg(vals,len)).^2))/len);
%plot(vals,'-+');
 
function mean = avg(array,size)
mean = sum(array)/size;
Step 01 – Create a Simulink model and test the model Upfront
  • You will have to test your model for the fixed-step solver only to avoid any error in the next steps.

Step 02 – Copy the function block in the other ‘Statistics.slx’ file
  • Connect to inport and outport submodel in Simulink. Make sure that, you have selected a fixed-step solver here.

Step 03 – Create FMU in Simulink by below commands.
  • First create a Simulink project.
p = slproject.create('Statistic_Project')
  • This should create a new project directory. Copy the “statistics.slx” file to this folder. Then, drag and drop this file into the project window.
  • Create an FMU with the below command
Simulink.fmuexport.ExportSimulinkProjectToFMU(p,'Statistics.slx','-fmuname','Statistics_FMU')
  • This should generate the “Statistics_FMU.fmu” file in the project directory.
Step 04 – Import FMU in to AMESim
  • Open the AMESim
  • Go to the Interfaces >>> FMU import assistant. Follow the instructions given on each window.
  • In the end, your FMU will be added to the “FMU imported block” library
  • You can use this FMU block to build the AMESim model. Build the below AMESim models.

Step 05 – Run the Co-simulation
  • Start a Dedicated Session from MATLAB - If the application that imports the FMU runs a single instance of the FMU, you can use MATLAB to start a session. Run the below command in MATLAB to start the session
>> shareMATLABForFMUCoSim
  • This dedicates the current MATLAB session available for requests from the AMESim to co-simulate an imported FMU. When an FMU has connected this session, the Simulink editor and Simulink project are loaded, and co-simulation starts automatically.
Now you can run the AMESim model. When you hit the run button in AMESim, it will automatically load the Simulink model, Run the model and close the simulation model. For more details, please see the video.
Conclusion
  • Ame2slcosim, sl2ame can provide better options compared to this option provided in this article.
  • Simulink FMU will be implemented with fixed time step, while ame2slcosim does not have such limitations
  • All process related to the FMU import and export is simple and more interactive in AMESim compare to Simulink.
CLICK HERE to download the demo model
If you are interested to know the other method of cosimulation with Simulink, then please visit below VIDEO articles.

CLICK HERE to know about “Cosimulation between Simulink & AMESim (AME2SLCosim) using MinGW-w64 (GCC 64 bit) compiler
CLICK HERE to know about “Model exchange (AME2SL) between MATLAB Simulink and AMESim with MinGW 64bit compiler
CLICK HERE to know about “Import MATLAB Simulink Model to AMESim (SL2AMECOSIM) using MinGW 64-bit compiler”.
CLICK HERE to know about “Export the AMESim model Blackbox to MATLAB Simulink using GNU MinGW GCC x64 bit compiler.
CLICK HERE to know about “FMU/FMI - Export a co-simulation FMU from the AMESim & Import it in the Simulink
CLICK HERE to know about “FMU/FMI - Export a Model Exchange FMU from the AMESim & import in the Simulink

If you still need support or clarification, then click here to raise the case on the support portal.

 

KB Article ID# KB000046494_EN_US

Contents

SummaryDetails

Associated Components

Amesim Automation Connect