% Post-simulation visualization of logs using MAT files containing
% parameters used for simulation run and the logs of simulation

% Copyright 2019 The MathWorks, Inc.

parametersFile = 'simParameters.mat';
simulationLogFile = 'simulationLogs.mat';
if ~isfile(parametersFile) % Check presence of simulation parameters file
    error('Unable to load file %s ', parametersFile);
end

if ~isfile(simulationLogFile) % Check presence of simulation log file
    error('Unable to load file %s ', simulationLogFile);
end
% Read simulation parameters
simParametersInfo = load(parametersFile);
simParameters = simParametersInfo.simParameters;

% Read simulation logs
simulationLogsInfo = load(simulationLogFile);
schedulingLogs = simulationLogsInfo.timeStepLogs; % MAC scheduling logs
rlcLogs = simulationLogsInfo.rlcLogs; % RLC statistics logs

% Flag to enable or disable CQI visualization
simParameters.CQIVisualization = true;
% Flag to enable or disable visualization of RB assignment. If enabled,
% then for slot based scheduling it shows RB allocation to the UEs for
% different slots of the frame. For symbol based scheduling, it shows
% RB allocation to the UEs over different symbols of the slot.
simParameters.RBVisualization = true;

% Set this flag to true for replay of simulation logs.
% Set this flag to false, to analyze the details of a particular frame or
% a particular slot of a frame. In the 'Resource Grid Allocation' window,
% input the frame number and slot number to visualize the scheduling
% assignment of the particular slot, if scheduling type is symbol-based.
% For slot based scheduling, enter the frame number to visualize the
% scheduling assignment for the entire frame. The frame number entered here
% controls the frame number for 'Channel Quality Visualization' figure
% too.
isLogReplay = false;

visualizationFlag = 2; % Both DL and UL visualization
simLogger = hNRSchedulingLogger(simParameters, visualizationFlag, isLogReplay); % Create object for MAC scheduling visualization and logging

% Construct information for RLC logger
lchInfo = repmat(struct('LCID',[],'EntityDir',[]), [simParameters.NumUEs 1]);
for idx=1:simParameters.NumUEs
    lchInfo(idx).LCID = simParameters.LCHConfig.LCID(idx, :);
    lchInfo(idx).EntityDir = simParameters.RLCConfig.EntityDir(idx, :);
end
simRLCLogger = hNRRLCLogger(simParameters, lchInfo); % Create an object for RLC visualization and logging
simRLCLogger.RLCStatsLog = rlcLogs(2:end, :); % First row has titles, so omitting it
simLogger.SchedulingLog{1} = schedulingLogs(2:end, :); % First row has titles, so omitting it
numSlotsFrame = (10 * simParameters.SCS)/ 15; % Number of slots in a 10 ms frame
simDuration = simParameters.NumFramesSim * numSlotsFrame; % In terms of number of slots

for slotNum = 1:simDuration % For each slot of simulation log
    if isLogReplay
        if simParameters.SchedulingType 
            % Symbol based scheduling
            plotPostSimRBGrids(simLogger, slotNum); % Plot RB-assignment grid and RB-CQI grid
        else
            % Slot based scheduling
            if ~mod(slotNum, numSlotsFrame) % Last slot of the frame
                plotPostSimRBGrids(simLogger, slotNum); % Plot RB-assignment grid and RB-CQI grid
            end
        end
    end
    if ~mod(slotNum, simParameters.MetricsStepSize)
        plotMetrics(simLogger); % Plot scheduler performance metrics
        plotMetrics(simRLCLogger); % Plot RLC throughput metrics
    end
end