% PROFILEPLOT3D.M %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Plot composition and temperature profiles with time axis

indmat();

N = size(xout,1);
% N = 500;

x1 = zeros(N,36); x2 = x1;
x3 = zeros(N,24); x4 = x3; x5 = x3; x6 = x3; x7 = x3;

for i = 1:N
    x1(i,:) = xout(i,im_x1);
    x2(i,:) = xout(i,im_x2);
    x3(i,:) = xout(i,im_x3);
    x4(i,:) = xout(i,im_x4);
    x5(i,:) = xout(i,im_x5);
    x6(i,:) = xout(i,im_x6);
    x7(i,:) = xout(i,im_x7);
end

main_col_D = zeros(40,N);
main_col_S1 = main_col_D;
main_col_S2 = main_col_D;
main_col_B = main_col_D;
prefrac_D = zeros(24,N);
prefrac_S1 = prefrac_D;
prefrac_S2 = prefrac_S1;

main_col_temp = zeros(40,N);
prefrac_temp = main_col_temp;


for i = 1:N
    main_col_D(:,i) = [
        x3(i,1:coldata.Nt(3))'
        x4(i,1:coldata.Nt(4))'
        x7(i,1:coldata.Nt(7))'
        x5(i,1:coldata.Nt(5))'
        x6(i,1:coldata.Nt(6))'
        ];
    main_col_S1(:,i) = [
        x3(i,coldata.Nt(3) + 1:2 * coldata.Nt(3))'
        x4(i,coldata.Nt(4) + 1:2 * coldata.Nt(4))'
        x7(i,coldata.Nt(7) + 1:2 * coldata.Nt(7))'
        x5(i,coldata.Nt(5) + 1:2 * coldata.Nt(5))'
        x6(i,coldata.Nt(6) + 1:2 * coldata.Nt(6))'
        ];
    main_col_S2(:,i) = [
        x3(i,2 * coldata.Nt(3) + 1:3 * coldata.Nt(3))'
        x4(i,2 * coldata.Nt(4) + 1:3 * coldata.Nt(4))'
        x7(i,2 * coldata.Nt(7) + 1:3 * coldata.Nt(7))'
        x5(i,2 * coldata.Nt(5) + 1:3 * coldata.Nt(5))'
        x6(i,2 * coldata.Nt(6) + 1:3 * coldata.Nt(6))'
    ];
    prefrac_D(:,i) = [
        x1(i,1:coldata.Nt(1))'
        x2(i,1:coldata.Nt(2))'
        ];
    prefrac_S1(:,i) = [
        x1(i,coldata.Nt(1) + 1:2 * coldata.Nt(1))'
        x2(i,coldata.Nt(2) + 1:2 * coldata.Nt(2))'
        ];
    prefrac_S2(:,i) = [
        x1(i,2 * coldata.Nt(1) + 1:3 * coldata.Nt(1))'
        x2(i,2 * coldata.Nt(2) + 1:3 * coldata.Nt(2))'
    ];
    main_col_temp(:,i) = [
        xout(i,im_T3)'
        xout(i,im_T4)'
        xout(i,im_T7)'
        xout(i,im_T5)'
        xout(i,im_T6)'
        ];
    prefrac_temp(:,i) = [
        xout(i,im_T3)'
        xout(i,im_T1)'
        xout(i,im_T2)'
        xout(i,im_T6)'
        ];
    
end

figure();
mesh(tout,1:40,main_col_D);
hold on;
mesh(tout,1:40,main_col_S1);
mesh(tout,1:40,main_col_S2);
mesh(tout,1:40,1 - main_col_D - main_col_S1 - main_col_S2);
xlabel('Time [min]');
ylabel('Stage');
zlabel('Composition');
title('Main column');

figure();
mesh(tout,1:24,prefrac_D);
hold on;
mesh(tout,1:24,prefrac_S1);
mesh(tout,1:24,prefrac_S2);
mesh(tout,1:24,1 - prefrac_D - prefrac_S1 - prefrac_S2);
xlabel('Time [min]');
ylabel('Stage');
zlabel('Composition');
title('Prefractionator');

figure();
subplot(1,2,1);
mesh(tout,1:40,main_col_temp);
xlabel('Time [min]');
ylabel('Stage');
zlabel('Temperature [K]');
title('Temperature main column');
subplot(1,2,2);
mesh(tout,1:40,prefrac_temp);
xlabel('Time [min]');
ylabel('Stage');
zlabel('Temperature [K]');
title('Temperature prefractionator');