Stream Logging Data to Disk

When you log simulation data, you can either store the data in a workspace variable, or stream the data to a temporary file on disk and have the workspace variable point to that temporary file. In either case, you interact with the logged simulation data through the simulation log variable.

Saving data to the workspace consumes memory. Streaming logged data to disk significantly increases the data logging capacity, because you are no longer limited by the system memory.

To enable streaming data to disk for all models, on the MATLAB® Toolstrip, click Preferences. In the left pane of the Preferences dialog box, select Simscape, then select the Stream data to temporary disk directory check box.

When this preference is turned on, the simulation data, in the form of a simlog object generated during simulation, is stored in an HDF5 file in a temporary folder under your user name. The workspace variable of type simscape.logging.Node, named as specified by the Workspace variable name configuration parameter, gets created, but instead of storing all the simulation data it references the simlog object in the temporary file. The temporary file persists as long as there is a logging variable name in the workspace that references it.

You view and analyze logged simulation data by accessing the simulation log variable, exactly the same way as if the simulation data was stored inside it. All the interaction between the workspace variable and the stored object happens behind the scenes. Therefore, you can use the Simscape™ Results Explorer, as well as all the methods associated with the simscape.logging.Node and simscape.logging.Series classes to query, plot, and analyze logged simulation data.

The following limitations apply when streaming data to disk:

  • The Limit data points and Data history (last N steps) configuration parameters are ignored. However, you can use the Decimation parameter to limit the number of logged data points. For more information, see Data Logging Options.

  • When you pause model simulation and step back and then forward, all the time points are logged on disk. This is different from storing the data directly in the workspace variable, where the log data is rolled back in this case.

Streaming to Disk and parfor Loop

When you simulate a model inside the parfor loop, the temporary HDF5 file is generated within the address space of the worker threads. To access the simulation data outside the parfor loop, export the data and then import the exported file outside the parfor loop.

parfor i=1:2 
   model = 'ssc_dcmotor' 
   load_system(model); 
   set_param(model, 'SimulationMode', 'normal'); 
   set_param(model, 'SimscapeLogType', 'all', 'SimscapeLogName', 'simlog'); 
   simOut = sim(model, 'ReturnWorkspaceOutputs', 'on'); 

   % save to a different file by appending the index 
   file = ['fileName_' num2str(i) '.h5']; 
   simscape.logging.export(simOut.get('simlog'), file); 
end

% import the exported files 
var = simscape.logging.import('fileName_1.h5');
...
 

Streaming to Disk with parsim

Simulating a model with the parsim command provides additional functionality, compared to using the parfor loop. The following example shows how you can use the parsim command when streaming logged simulation data to disk.

model = 'ssc_dcmotor';
% Create array of inputs to run multiple simulations
num = 10;
in(1:num) = Simulink.SimulationInput(model);

% Specify any directory where the H5 files will be created for every run
currDir = '/tmp'; % any directory

for i = 1:num
    % This will only work with local pools
    in(i).PostSimFcn = @(x) locHandleSimscapeLTF(model, x, currDir, i); 
end
out = parsim(in);

for idx = 1:numel(out)
    simlog = simscape.logging.import(out(idx).SimscapeFileName);
    sscexplore(name);
end

function newOut = locHandleSimscapeLTF(model, out, dirName, runId)
    % All the logged variables along with simlog should be part of 'newOut' object 
    loggedVars = out.who;
    newOut = struct;
    for i = 1 : numel(loggedVars)
        loggedData = out.(loggedVars{i});
        if isa(loggedData, 'simscape.logging.Node')
		    % Specify any file name with .h5 extension
            filename = [model '_simlog_file_' num2str(runId) '.h5']; 
            simscapeFileName = fullfile(dirName, filename);
			
			% Export simlog to H5 file
            simscape.logging.export(loggedData, simscapeFileName);
            newOut.SimscapeFileName = simscapeFileName;
        else
            newOut.(loggedVars{i}) = out.(loggedVars{i});
        end
    end
end
 

Related Topics