function Validation_2prod(filename,step_f,step_za,step_zb,step_zc,step_zd,vrng,Prngf,Prng,a)
%% Applying disturbances

serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;

%Feed flow
display('Feed flow disturbance')
Feed = HyCase.Flowsheet.Operations.Item('FIC-100').SPValue;
HyCase.Flowsheet.Operations.Item('FIC-100').SP.Value = Feed *(1+step_f);
t_dur = 1500;
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue
while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end
if a == 3
    xyDatasketch_3plots
else
    xyDatasketch
end

serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman');
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman');
title(['Feed disturbance: +',num2str(step_f*100),'%'],'fontsize',14,'fontname','Times New Roman')




figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['Feed disturbance: +',num2str(step_f*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_feedDisturb.fig')
saveas(figure(2),'xB_feedDisturb.fig')

% compositions
display('disturbance of xC1 in Feed')
serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Col = HyCase.Flowsheet.Operations.Item('T-100');
Compfrac = HyCase.Flowsheet.MaterialStreams.Item('F_in').componentMolarFractionValue;

compositionDisturbance(1, Compfrac, -step_za);
HyCase.Application.PlayScript([pwd,'\compChange.SCP'])
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end


if a == 3
    xyDatasketch_3plots
else
    xyDatasketch
end
  

serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['z_F_1 disturbance: -',num2str(step_za*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['z_F_1 disturbance: -',num2str(step_za*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_zf1Disturb.fig')
saveas(figure(2),'xB_zf1Disturb.fig')

%c2
display('disturbance of xC2 in Feed')
serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Compfrac = HyCase.Flowsheet.MaterialStreams.Item('F_in').componentMolarFractionValue;

compositionDisturbance(2, Compfrac, -step_zb);
HyCase.Application.PlayScript([pwd,'\compChange.SCP'])
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end

 
if a == 3
    xyDatasketch_3plots
else
    xyDatasketch 
end
  
serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['z_F_2 disturbance: -',num2str(step_zb*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['z_F_2 disturbance: -',num2str(step_zb*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_zf2Disturb.fig')
saveas(figure(2),'xB_zf2Disturb.fig')

%c3
display('disturbance of xC3 in Feed')
serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Compfrac = HyCase.Flowsheet.MaterialStreams.Item('F_in').componentMolarFractionValue;

compositionDisturbance(3, Compfrac, -step_zc);
HyCase.Application.PlayScript([pwd,'\compChange.SCP'])
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end
 
if a == 3
    xyDatasketch_3plots
else 
    xyDatasketch
end
   
serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['z_F_3 disturbance: -',num2str(step_zc*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['z_F_3 disturbance: -',num2str(step_zc*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_zf3Disturb.fig')
saveas(figure(2),'xB_zf3Disturb.fig')

%c4
display('disturbance of xC4 in Feed')
serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Compfrac = HyCase.Flowsheet.MaterialStreams.Item('F_in').componentMolarFractionValue;

compositionDisturbance(4, Compfrac, -step_zd);
HyCase.Application.PlayScript([pwd,'\compChange.SCP'])
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end

 
if a == 3
    xyDatasketch_3plots
else
    xyDatasketch
end
   
serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['z_F_4 disturbance: -',num2str(step_zd*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['z_F_4 disturbance: -',num2str(step_zd*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_zf4Disturb.fig')
saveas(figure(2),'xB_zf4Disturb.fig')

% feed quality
display('disturbance of Feed quality')

serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,'\twoproductcolumn_pref_exct_wilsonIdeal.HSC']);
HyCase.Activate;
temptoset = fsolve(@reachq,100,[],HyCase,1-vrng);
serv.ActiveDocument.Close

serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
HyCase.Flowsheet.MaterialStreams.Item('F').VapourFraction.Erase
HyCase.Flowsheet.MaterialStreams.Item('F').Temperature.Value = temptoset;
t_now = HyCase.Solver.Integrator.CurrentTimevalue;

HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end
 
if a == 3
    xyDatasketch_3plots
else
    xyDatasketch   
end

serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['q_F disturbance: -',num2str(vrng*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['q_F disturbance: -',num2str(vrng*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_qDisturb.fig')
saveas(figure(2),'xB_qDisturb.fig')

%% pressure disturbances
display('disturbance of Feed pressure')


serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Col = HyCase.Flowsheet.Operations.Item('T-100');
Pnom_Feed = HyCase.Flowsheet.MaterialStreams.Item('F').Pressure.value;
HyCase.Flowsheet.MaterialStreams.Item('F').Pressure.value = Pnom_Feed*(1+Prngf);

t_now = HyCase.Solver.Integrator.CurrentTimevalue;
HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end

 
if a == 3
    xyDatasketch_3plots
else
    xyDatasketch
end
   
serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['P_F_e_e_d disturbance: +',num2str(Prngf*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['P_F_e_e_d disturbance: +',num2str(Prngf*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_PFDisturb.fig')
saveas(figure(2),'xB_PFDisturb.fig')



%% Top pressure disturbance
display('disturbance of top pressure')


serv = actxserver('Hysys.Application.2006.5');
HyCase = serv.SimulationCases.Open([pwd,filename]);
HyCase.Activate;
Col = HyCase.Flowsheet.Operations.Item('T-100');
Pnom_top = Col.ColumnFlowsheet.Operations.Item('PIC-100').SPValue;
Col.ColumnFlowsheet.Operations.Item('PIC-100').SP.Value = Pnom_top*(1+Prng);
Col.ColumnFlowsheet.Pressures.Values(2) = Pnom_top*(1+Prng);
PressChange('PconChange.scp',serv,Col,Pnom_top*(1+Prng));    
t_now = HyCase.Solver.Integrator.CurrentTimevalue;
HyCase.Solver.Integrator.StopTime = t_now + t_dur*60;
HyCase.Solver.Integrator.Continue

while 1
    if HyCase.Solver.Integrator.isRunning == 0
        break
    end
end
 
if a == 3
    xyDatasketch_3plots
else 
    xyDatasketch
end
   
serv.ActiveDocument.Close
figure(1)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC3 in Distillate','fontsize',18,'fontname','Times New Roman')
title(['P_c_o_n disturbance: +',num2str(Prng*100),'%'],'fontsize',14,'fontname','Times New Roman')
figure(2)
xlabel('time(min)','fontsize',18,'fontname','Times New Roman')
ylabel('xC2 in Bot','fontsize',18,'fontname','Times New Roman')
title(['P_c_o_n disturbance: +',num2str(Prng*100),'%'],'fontsize',14,'fontname','Times New Roman')
saveas(figure(1),'yD_PConDisturb.fig')
saveas(figure(2),'xB_PConDisturb.fig')

end

