% Code to plot simulation results from sh_cyl_vdpc_pump_fixed_step
%% Plot Description:
%
% The plots below show the behavior of the abstract models and the detailed
% model.  The results are very similar, indicating that the abstract models
% can be used without a significant loss of accuracy.

% Copyright 2015 The MathWorks, Inc.

% Generate simulation results
temp_variant = {'Detailed','Abstract'};

for temp_ivar=1:length(temp_variant)
    % Configure model variants    
    sh_cyl_vdpc_pump_fixed_step_setabstractdetailed('Cylinder',temp_variant{temp_ivar});
    sh_cyl_vdpc_pump_fixed_step_setabstractdetailed('Valve and Power Unit',temp_variant{temp_ivar});
    
    % Run simulation
    sim('sh_cyl_vdpc_pump_fixed_step');
    
    % Save simulation results
    simlog_t.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Mass.v.series.time;
    simlog_cylx.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Load_Spring.x.series.values('m');
    if (simlog_sh_cyl_vdpc_pump_fixed_step.Cylinder.hasChild('Detailed'));
        simlog_cylpA.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Cylinder.Detailed.Cylinder.A.p.series.values('Pa');
        simlog_cylpB.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Cylinder.Detailed.Cylinder.B.p.series.values('Pa');
    else
        simlog_cylpA.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Cylinder.Abstract.Cylinder.A.p.series.values('Pa');
        simlog_cylpB.(temp_variant{temp_ivar}) = simlog_sh_cyl_vdpc_pump_fixed_step.Cylinder.Abstract.Cylinder.B.p.series.values('Pa');
    end
end

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

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
for temp_ivar=1:length(temp_variant)
    plot(simlog_t.(temp_variant{temp_ivar}), simlog_cylpA.(temp_variant{temp_ivar})-simlog_cylpB.(temp_variant{temp_ivar}), 'LineWidth', 1);
    hold on
end
hold off
grid on
title('Pressure Difference in Cylinder Chambers (pA-pB)')
ylabel('pA-pB (Pa)')
legend(temp_variant,'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
for temp_ivar=1:length(temp_variant)
    plot(simlog_t.(temp_variant{temp_ivar}), simlog_cylx.(temp_variant{temp_ivar}), 'LineWidth', 1);
    hold on
end
hold off
grid on
title('Piston Position')
ylabel('Position (m)')
xlabel('Time (s)')
legend(temp_variant,'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylpA simlog_cylpB simlog_cylx simlog_handles

clear temp_solvertype temp_isol temp_variant temp_ivar