% Code to plot simulation results from sscfluids_refrigeration
%% Plot Description:
%
% This plot shows the power consumed by the compressor and the cooling load
% of the refrigeration system, which is the rate of heat transfer in the
% evaporator. The coefficient of performance is the ratio of the cooling
% load and the power consumed.

% Copyright 2019 The MathWorks, Inc.

% Generate simulation results if they don't exist
if ~exist('simlog_sscfluids_refrigeration', 'var')
    sim('sscfluids_refrigeration')
end

% Reuse figure if it exists, else create new figure
if ~exist('h2_sscfluids_refrigeration', 'var') || ...
        ~isgraphics(h2_sscfluids_refrigeration, 'figure')
    h2_sscfluids_refrigeration = figure('Name', 'sscfluids_refrigeration');
end
figure(h2_sscfluids_refrigeration)
clf(h2_sscfluids_refrigeration)

plotPower(simlog_sscfluids_refrigeration)



% Plot power and coefficient of performance
function plotPower(simlog)

% Get simulation results
t = simlog.Compressor.Compressor.power.series.time;
W_comp = simlog.Compressor.Compressor.power.series.values('W');
W_load = simlog.Evaporator.Q.series.values('W');
COP = -W_load./W_comp;
COP(abs(W_comp) < 40) = 0;

% Plot results
handles(1) = subplot(2, 1, 1);
plot(t, W_comp, 'LineWidth', 1)
hold on
plot(t, -W_load, 'LineWidth', 1)
hold off
grid on
legend('Compressor', 'Cooling Load', 'Location', 'best')
ylabel('Power (W)')

handles(2) = subplot(2, 1, 2);
plot(t, COP, 'LineWidth', 1)
grid on
ylim([0 5])
ylabel('Coefficient of Performance')
xlabel('Time (s)')

linkaxes(handles, 'x')

end