%% Export Simulink Model to Standalone FMU
% This example shows how to export Simulink component to standalone
% Co-Simulation FMU 2.0 with Simulink Compiler(R). For a detailed
% explanation of the model, see:
% 
% * <docid:simulink_ref#example-sldemo_fuelsys Modeling a Fault-Tolerant Fuel Control System>
%
% In this example, the air-fuel ratio control system is composed of three
% Simulink models: 
%
% * Fuel Rate Control Component: |fmudemo_export_fuelsys_controller|,
% * Engine Gas Dynamics Component: |fmudemo_export_fuelsys_plant|, and 
% * top-level model |fmudemo_export_fuelsys_top|.
%
%
% Once the controller and plant components are exported to FMU format, they
% can be integrated using the top-level model. The generated FMUs can also
% be imported into other simulation tools that support FMI. For a list of
% Tools that support FMI, see: <https://fmi-standard.org/tools/>.

% Copyright 2020 The MathWorks, Inc.

%% Export Fuel Rate Control Component to FMU
% Open the |fmudemo_export_fuelsys_controller| example model.
open_system('fmudemo_export_fuelsys_controller');
%%
% From *Simulation* tab, click drop-down button for *Save*. In *Export
% Model To* section, click *Standalone FMU...*. In FMU Export dialog,
% configure wrapper model and icon settings, and specify save location for
% generated FMU.
%%
% <<../fmudemo_export_dialog.png>>
%%
% Click *Create* to export to FMU. The
% |fmudemo_export_fuelsys_controller.fmu| file can be found at specified
% save location.
%
restoreOC = disableAutoSave;
evalc('exportToFMU2CS(''fmudemo_export_fuelsys_controller'', ''CreateModelAfterGeneratingFMU'', ''off'', ''AddIcon'', ''snapshot'', ''SaveDirectory'', pwd)');
restoreOC.delete;
close_system('fmudemo_export_fuelsys_controller', 0);

%% Export Engine Gas Dynamics Component to FMU
% Open the |fmudemo_export_fuelsys_plant| example model.
open_system('fmudemo_export_fuelsys_plant');
%%
% FMU can also be exported using command-line. In MATLAB command line
% window, use |exportToFMU2CS| command:

%%
%
%   % Export model to Standalone Co-Simulation FMU 2.0
%   exportToFMU2CS('fmudemo_export_fuelsys_plant', 'CreateModelAfterGeneratingFMU', 'off', 'AddIcon', 'snapshot', 'SaveDirectory', pwd);
%
evalc('exportToFMU2CS(''fmudemo_export_fuelsys_plant'', ''CreateModelAfterGeneratingFMU'', ''off'', ''AddIcon'', ''snapshot'', ''SaveDirectory'', pwd)');
%%
close_system('fmudemo_export_fuelsys_plant', 0);

%%
% You can use optional arguments *CreateModelAfterGeneratingFMU*,
% *AddIcon*, and *SaveDirectory* to configure FMU export settings. For more
% information, call |help ExportToFMU2CS|.

%% Integrate FMU components in Simulink
% Once both FMUs are successfully exported, you may use the top model
% |fmudemo_export_fuelsys_top| to fully integrate the system for testing.
%
clear Atm PumpCon RampRateKiZ SpeedVect max_press min_press p0 st_range ...
    PressEst RampRateKiX SonicFlow ThrotEst hys max_speed min_speed ...
    restoreOC zero_thresh PressVect RampRateKiY SpeedEst ThrotVect ...
    max_ego max_throt min_throt s;
load_system('fmudemo_export_fuelsys_top');
set_param('fmudemo_export_fuelsys_top', 'SimulationCommand', 'Update');
set_param('fmudemo_export_fuelsys_top', 'StopTime', '16');
sim('fmudemo_export_fuelsys_top');

%%
open_system('fmudemo_export_fuelsys_top');

%%
%
close_system('fmudemo_export_fuelsys_top', 0);
clear ans sldemo_fuelsys_output;

function restoreOC = disableAutoSave()
restoreval = get_param(0,'AutoSaveOptions');
newval = restoreval;
newval.SaveOnModelUpdate = false;
set_param(0,'AutoSaveOptions',newval);
restoreOC = onCleanup(@()set_param(0,'AutoSaveOptions',restoreval));
end
