close all;
clear all;

dir1 = "Sin_Variar";
parametro = ["Phi r","Phi r real","R","T ref","Vdc","L", "L real","fc corrientes"];
Multiplicador = ["05","15"];

for param = parametro
    for mult = Multiplicador

        dir2 = strcat(param,'/',param,'_',mult);
        ley1 = "Sin Variar";
        ley2 = strcat(param,"*",mult);

        dir = [dir2,dir1];

        figure('Color','w');
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/Torque.mat'));
            plot(Speed.Data*60/2/pi,Torque.Data);
            hold on
        end
        legend(ley2,ley1,'Location', 'best')
        xlabel('Speed [rpm]');
        ylabel('Torque [N-m]');
        title('Torque and Speed');
        saveas(gcf,strcat('Imagenes/',dir2,'/Torque and Speed.png'))

        figure('Color','w');
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/Torque.mat'));
            plot(Speed.Data*60/2/pi,Torque.Data.*Speed.Data/1000);
            hold on
        end
        legend(ley2,ley1,'Location', 'best')
        xlabel('Speed [rpm]');
        ylabel('Power [kW]');
        title('Power and Speed');
        saveas(gcf,strcat('Imagenes/',dir2,'/Power and Speed.png'))

        figure('Color','w');
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/vdq.mat'));
            plot(Speed.Data*60/2/pi,vdq.Data);
            hold on
        end
        legend(strcat("Vd ",ley2), strcat("Vq ",ley2), strcat("Vd ",ley1),strcat("Vq ",ley1),'Location', 'best');
        xlabel('Speed [rpm]');
        ylabel('Voltage [V]');
        title('Vdq and Speed');
        saveas(gcf,strcat('Imagenes/',dir2,'/Vdc and Speed.png'))

        figure('Color','w');
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/idq.mat'));
            plot(Speed.Data*60/2/pi,idq.Data);
            hold on
        end
        legend(strcat("id ",ley2), strcat("iq ",ley2), strcat("id ",ley1),strcat("iq ",ley1),'Location', 'best');
        xlabel('Speed [rpm]');
        ylabel('Current [A]');
        title('idq and Speed');
        saveas(gcf,strcat('Imagenes/',dir2,'/idq and Speed.png'))

        figure('Color','w');
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/idq_plus_FW.mat'));
            plot(Speed.Data*60/2/pi,idq_plus_FW.Data);
            hold on
        end
        legend(strcat("id ",ley2), strcat("iq ",ley2), strcat("id ",ley1),strcat("iq ",ley1),'Location', 'best');
        xlabel('Speed [rpm]');
        ylabel('Current [A]');
        title('idq plus FW and Speed');
        saveas(gcf,strcat('Imagenes/',dir2,'/idq plus FW and Speed.png'))

        temp = 1;
        ef = [0,0];
        for i = dir
            load (strcat('data/',i,'/Speed.mat'));
            load (strcat('data/',i,'/Torque.mat'));
            load (strcat('data/',i,'/Idc.mat'));
            
            if i == "Vdc/Vdc_05"
                Vdc = 475 *0.5;
                P_ele = rms(Vdc.*Idc.Data);
                P_mec = rms(Torque.Data.*Speed.Data);
                ef(temp) = rms(P_mec/P_ele);

            elseif i == "Vdc/Vdc_15"
                Vdc = 475 * 1.5;
                P_ele = rms(Vdc.*Idc.Data);
                P_mec = rms(Torque.Data.*Speed.Data);
                ef(temp) = rms(P_mec/P_ele);
            else
                Vdc = 475;
                P_ele = rms(Vdc.*Idc.Data);
                P_mec = rms(Torque.Data.*Speed.Data);
                ef(temp) = rms(P_mec/P_ele);
            end
            
            temp = temp+1;
        end
        x = {
            strcat(convertStringsToChars(ley1),' ===> ',num2str(ef(2)))
            strcat(convertStringsToChars(ley2),' ===> ',num2str(ef(1)))
            };
        writelines(x,strcat('Imagenes/',dir2,'/Eficiencias.txt'))
    end
end