Seth on Simulink

August 11th, 2009

How Many Blocks are in that Model?

My colleague Sam and I wanted to find an example model with about 10,000 blocks.  I suggested the HL-20 model from the Aerospace Blockset, asbhl20.mdl.  Sam decided to measure the total number of blocks by running sldiagnostics:

>> [t,s] = sldiagnostics('asbhl20','CountBlocks');
s(1)
ans =
    isMask: 0
      type: 'asbhl20 Total blocks'
     count: 5700

Sam quickly sent me a message saying that the HL-20 model only had 5700 blocks.  What gives?

What is SLDIAGNOSTICS?

First, let us discuss a little history of sldiagnostics.  Over the years, Simulink developers have worked with many customers to learn about their modeling styles and assist in debugging problems.  Unfortunately some customers are unable to share their models because they might contain proprietary information.  The solution these developers created were a series of scripts that would quickly gather information about a model without any model algorithm information. These collected scripts then became the sldiagnostics function.  Because the output of these scripts was generally devoid of any proprietary data, even customers who could not share their models were able to provide the diagnostic information instead.

Most of the checks done by sldiagnostics help you gather an understanding of the model as a whole.  This is a great help in learning about models rather than having to browse through every system one-by-one. 

CountBlocks

The diagnostic information Sam gathered was the output of CountBlocks.  Here is a part of the text report for the HL-20 model.

Finished counting blocks in 'asbhl20'.
Found 5700 blocks.
 
         asbhl20 Total blocks :  5700
                          Abs :    16
                   ActionPort :    11
                    Assertion :     2
                         Bias :     6
                   BusCreator :    46
                  BusSelector :   126
            <snip>
                       Inport :  1175
                 InportShadow :    49
                   Integrator :    43
            Interpolation_n-D :     1
                        Logic :     6
                       Lookup :     6
                   Lookup_n-D :     6
                         Math :    59
                       Memory :     4
                        Merge :     4
                       MinMax :     4
               ModelReference :     9
              MultiPortSwitch :     1
                          Mux :   141
                      Outport :   669
            <snip>

HL-20 Includes Model Reference

The HL-20 model includes 9 ModelReference blocks.  (I introduced some of the basics of model reference a previous post.)  You can see the hierarchy of the model in the Dependency Viewer instance view.

The model reference hierarchy for the ASBHL20 model from the Aerospace Blockset.

When I think about a model, I imagine the net collected functionality represented by it.  I think the number of blocks used to create that model is a simple measure for how much functionality/algorithm there is.  Model reference blocks contain the functionality of the entire model that they reference, however, sldiagnostics counts ever Model Reference block as only one block.

The measure I am really looking for is the subsystem equivalent implementation of all the blocks in the model.  To get this for the HL-20 model, you actually need to count the blocks inside each model referenced and multiply by the number of instances.  I wrote a MATLAB function to do this:

function totalBlocks = mdlrefCountBlocks(mdl)
% mdlrefCountBlocks Count the subsystem equivalent number of blocks
% in a model reference hierarchy.
 
% Copyright 2009 The MathWorks, Inc
 
%% Open the model
open_system(mdl)
 
%% Get instance information
[mDep,mInst] = find_mdlrefs(mdl);
% Open dependent models
for i = 1:length(mDep)
    load_system(mDep{i})
end
 
%% Count the number of instances of each dependency
mCount = zeros(size(mDep));
mCount(end) = 1; % Last element is the top model, only one instance
for i = 1:length(mInst)
    mod = get_param(mInst{i},'ModelName');
    mCount = mCount + strcmp(mod,mDep);
end
%%
for i = 1:length(mDep)
    disp([num2str(mCount(i)) ' instances of ' mDep{i}])
end
disp(' ')
 
%% Loop over dependencies, get number of blocks
s = cell(size(mDep));
for i = 1:length(mDep)
    [t,s{i}] = sldiagnostics(mDep{i},'CountBlocks');
    disp([mDep{i} ' has ' num2str(s{i}(1).count) ' blocks'])
end
 
%% Multiply number of blocks, times model count, add to total
totalBlocks = 0;
for i = 1:length(mDep)
    totalBlocks = totalBlocks + s{i}(1).count * mCount(i);
end
 
disp(' ')
disp(['Total blocks: ' num2str(totalBlocks)])

Total Blocks

Here are the results of running this function:

>> total = mdlrefCountBlocks('asbhl20');
3 instances of asbhl20_FCSApp
3 instances of asbhl20_FDIRApp
3 instances of asbhl20_GuidanceApp
1 instances of asbhl20
 
asbhl20_FCSApp has 115 blocks
asbhl20_FDIRApp has 1248 blocks
asbhl20_GuidanceApp has 388 blocks
asbhl20 has 5700 blocks
 
Total blocks: 10953

Now it’s your turn

In the end, we can see the HL-20 model has almost 11K blocks.  Do you use model reference? How many total blocks are in the hierarchy of your largest model?  Use the script above to gather this data and post it in the comments here.

8 Responses to “How Many Blocks are in that Model?”

  1. Jim replied on :

    Seth,
    There are two related items I’d like to be able to generate from a script - the model’s cyclomatic complexity and the block sort order. Is there any way to return these to a workspace variable using a script?

  2. wei replied on :

    @Seth, Interestingly this functions counts blocks inside of library Simulink subsystems, but not objects inside of library Stateflow charts. Is there way to work around this?

  3. Gen Sasaki replied on :

    Hi Seth,
    It may be of interest to our users that someone in MathWorks Consulting has uploaded a script which does something similar as the above, except it goes inside nested model reference blocks. (http://www.mathworks.com/matlabcentral/fileexchange/20568) Your script above may be able to do the same if find_mdlrefs(mdl,true) is used.

  4. Gen Sasaki replied on :

    Whoops, I wrote too soon. The second parameter of find_mdlrefs defaults to true, so your code will also find nested model references.

  5. Seth replied on :

    @Gen - Thanks for the link to the file Gen!

  6. Rob Aberg replied on :

    @Wei, Stateflow metrics for a model are obtained via the ‘CountSF’ option of sldiagnostics.

  7. wei replied on :

    @Rob,
    1. The difference I said was about counting library linked Simulink blocks vs. Stateflow objects (states, junction, etc.) with ‘CountBlocks’ or ‘CountSF’. sldiagnostics counts number of linked charts, nothing further.

    2. sldiagnostics doesn’t work on block diagram library model, including Stateflow or not. A simple enhancement to add in future release.

  8. Uba osy replied on :

    Hi,

    in the introductory example for fuzzy logic toolbox it was noted that using non fuzzy means, you could “Specify that service accounts for 80% of the overall tipping grade and the food makes up the other 20%”. Can you do this using the fuzzy logic toolbox of matlab. Can you give weights to diffrenet inputs.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • Mohamamd: Hi Suth, I try to simulate a load tap-changing transformer in simulink but its control part has to be...
  • Han Geerligs: Hello Guy, thanks for the clarificaton and link. However in the documentation I am missing the...
  • Guy: @Han, you probably already know, but I think it is good to share with everyone. To zoom in use the key...
  • Han Geerligs: Hi Seth, Once again I’d like to point out that my biggest accelerator is using mouse and keyboard...
  • XaL: Hi, thanks for the tips. As someone wrote in http://blogs.mathwor ks.com/seth/2009/03/ 13/new-%C2%A0rele...
  • Uba osy: Hi, in the introductory example for fuzzy logic toolbox it was noted that using non fuzzy means, you could...
  • Prashant: How can I have same example but instead AC(1 to 10V 50 or 60Hz) and DC(0.5 to 10 V) then adding AC+DC but...
  • adrian chavarro: Great tool, for educational and sicentific, simulation. I would like to know where can i place a...
  • Ashish Sadanandan: @wei, I was talking about the case where the compiler would perform the ‘model_Xdim...
  • wei: @Ashish, I agree with your observation on compiler optimization but fail to see why Han’s code would be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.