%% Inline Numeric Values of Block Parameters
% This example shows how to optimize the generated code by inlining the numeric
% values of block parameters. _Block parameters_ include the *Gain* parameter
% of a Gain block and the table data and breakpoint sets of an n-D Lookup
% Table block.  
% 
% This optimization determines whether numeric block parameters occupy global memory in the generated code. The optimization can:
% 
% * Improve execution speed. 
% * Reduce RAM and ROM consumption. 
% 

%   Copyright 2015 The MathWorks, Inc.

%% Explore Example Model
% Open the example model |rtwdemo_paraminline| and configure it to show the generated names of blocks.
load_system('rtwdemo_paraminline')
set_param('rtwdemo_paraminline','HideAutomaticNames','off')
open_system('rtwdemo_paraminline')
%%
% The model contains blocks that have these numeric parameters:
%
% * The *Gain* parameters of the Gain blocks
% * The *Constant value* parameters of the Constant blocks
% * The table data and breakpoint sets of the n-D Lookup Table blocks
%
% The output of the block G2, and the outputs of blocks upstream of G2,
% change only if you tune the
% values of the block parameters during simulation or during code
% execution. When you update the model diagram, these blocks and signal lines appear magenta
% in color.
%
% Several blocks use |Simulink.Parameter| objects in the base workspace to
% set the values of their parameters. The parameter objects all use the
% storage class |Auto|, which means that you can configure the generated code to 
% inline the parameter values.
%% Generate Code Without Optimization
% Create a temporary folder for the
% build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();
%%
% Disable the optimization by setting *Configuration Parameters >
% Default parameter behavior* to |Tunable|.
set_param('rtwdemo_paraminline','DefaultParameterBehavior','Tunable')
%%
% Generate code from the model.
rtwbuild('rtwdemo_paraminline')
%%
% In the code generation report, view the source file |rtwdemo_paraminline_data.c|. 
% The code defines a global structure
% that contains the block parameter values. Each block parameter in the model,
% such as a lookup table array, breakpoint set, or gain, appears as a field of
% the structure.
cfile = fullfile(...
    cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline_data.c');
rtwdemodbtype(cfile,'/* Block parameters (default storage) */', '};', 1, 1);
%%
% You can tune the structure fields during code execution because they occupy global memory. 
% However, at each step of the generated algorithm, the code
% must calculate the output of each block,
% including the outputs of the block G2 and the upstream blocks.
% View the algorithm in the model |step| function in the file
% |rtwdemo_paraminline.c|.
cfile = fullfile(cgDir,'rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(...
    cfile,'/* Model step function */','/* Model initialize function */',1,0);
%% Generate Code with Optimization
% Set *Default parameter behavior* to |Inlined|.
set_param('rtwdemo_paraminline','DefaultParameterBehavior','Inlined')
%%
% Generate code from the model.
rtwbuild('rtwdemo_paraminline')
%%
% In the code generation report, view the algorithm in the
% file |rtwdemo_paraminline.c|.
rtwdemodbtype(...
    cfile,'/* Model step function */','/* Model initialize function */',1,0);
%%
% The code does not allocate memory for block parameters or for parameter objects
% that use the storage class |Auto|. Instead, the code
% generator uses the parameter values from the model, and from the parameter objects, to calculate and inline
% the constant output of the block G2, |150.0|. The
% generator also inlines the value of the *Gain*
% parameter of the Gain block G1, |2.0|.
%
% With the optimization, the generated code leaves out computationally expensive algorithmic code for
% blocks such as the lookup tables. The optimized code calculates the output of a
% block only if the output can change during execution. For this model, only the
% outputs of the Inport block In1, the Gain block G1, and the Sum block can
% change.
%%
% Close the model and the code generation report.
bdclose('rtwdemo_paraminline')
rtwdemoclean;
cd(currentDir)
%% Preserve Block Parameter Tunability
% When you set *Default parameter behavior* to |Inlined|, you can preserve
% block parameter tunability by creating |Simulink.Parameter| objects for
% individual parameters. You can configure each object to appear in the
% code as a tunable field of the global parameter structure or as an
% individual global variable. You can change parameter
% values during code execution and interface the generated code with
% your own handwritten code. For more information, see
% <docid:rtw_ug.bsmzxo9 Create Tunable Calibration Parameter in the Generated Code>.
%% Inline Invariant Signals
% You can select the *Inline invariant signals* code generation option
% (which also places constant values in the generated code) only when you
% set *Default parameter behavior* to |Inlined|. See
% <docid:rtw_ug.f1145247 Inline Invariant Signals>.