Code covered by the BSD License  

Highlights from
Pxy Generator for Binary Ideal Solutions - w/Database

image thumbnail

Pxy Generator for Binary Ideal Solutions - w/Database

by

 

31 Jul 2012 (Updated )

The Pxy generator plots the Pxy diagram for a binary ideal solution.

Pxy_GUI(varargin)
function varargout = Pxy_GUI(varargin)
% Pxy_GUI MATLAB code for Pxy_GUI.fig
% Owned by David H Hagan 
% v1.1 -> Original release November 1,2012
% This GUI allows the user to choose two compounds from the listbox and
% populate the fields.  After temperature and Pressure units are input,
% the user can plot the Pxy Diagram.  This entire program assumes the
% Raoult's law is valid, so choose the compound pairs accordingly.
%
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @Pxy_GUI_OpeningFcn, ...
                       'gui_OutputFcn',  @Pxy_GUI_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);
    if nargin && ischar(varargin{1})
        gui_State.gui_Callback = str2func(varargin{1});
    end

    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
        gui_mainfcn(gui_State, varargin{:});
    end
    % End initialization code - DO NOT EDIT

end

% --- Executes just before Pxy_GUI is made visible.
function Pxy_GUI_OpeningFcn(hObject, eventdata, handles, varargin)

        load_listbox(hObject,handles);      %load the data into the listbox
    %set(handles.P,'String','0');
    %set(handles.z,'String','0');
        
    handles.output = hObject;
    guidata(hObject, handles);

end

% --- Outputs from this function are returned to the command line.
function varargout = Pxy_GUI_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;
end

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
   
    index_selected = get(hObject,'Value');
    list = get(hObject,'String');
    item = char(list(index_selected));
        conn = database('Txy_data','','');
        setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
        query = ['SELECT ID,Name,Tb,CAS_ID FROM Txy_data WHERE Name = ''',item,''''];
        result = fetch(conn,query);
        close(conn);

        
        
    Tb1 = result.Tb;
    name1 = result.Name;
    cas1 = result.CAS_ID;
    
    handles.cas1 = cas1;
    handles.Tb1 = Tb1;
    handles.Name1 = name1;

    handles.output = hObject;
    guidata(hObject, handles);         % Update handles structure
end

function load_listbox(hObject,handles) 
       
        conn = database('Txy_data','','');
        setdbprefs('datareturnformat','structure');
        query = 'SELECT ALL ID,Name FROM Txy_data WHERE CAS_ID > 0';  
        result = fetch(conn,query);
        close(conn);
        

        %The following creates a structure containing the names and ID's
        %of everything in the database
        
        data = struct([]);
        j = 1;
        for i=1:length(result.ID)
            data(i).id =   result.ID(i);
            data(i).name = result.Name(i);        %this is a cell
            names(i) = data(i).name;
        end
       
        handles.compounds = names;
        set(handles.listbox1,'String',handles.compounds,'Value',1); %sets the listbox text to names
        set(handles.listbox2,'String',handles.compounds,'Value',1); %sets the listbox text to names
        
   
        handles.output = hObject;
        guidata(hObject, handles);         % Update handles structure
        
end

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end

% --- Executes on button press in plot.
function plot_Callback(hObject, eventdata, handles)
    
    if (isempty(handles.T))
        errordlg('T not set. ');
        return
    end
    
    try
        switch handles.Tunit
            case 'C'
                T = handles.T;
            case 'K'
                T = handles.T - 273.15;
                handles.T = T;
        end
    catch
        errordlg('Please select a temperature unit. ');
        return
    end
    
    if (isempty(handles.Punit))
        errordlg('Please select a pressure unit. ');
        return
    end
    

    
     try
         if (strcmp(handles.cas1,handles.cas2) == 1) 
             errordlg('Compound 1 and 2 are the same.  Switch one of them or else!');
         else
             % Find the low boiler
             if (handles.Tb1 > handles.Tb2)
                 lowBoiler = char(handles.Name2);
                 low_ID = handles.cas2;
                 high_ID = handles.cas1;
                 set(handles.name1,'String',char(handles.Name2));
                 set(handles.name2,'String',char(handles.Name1));
             elseif (handles.Tb2 > handles.Tb1)
                 lowBoiler = char(handles.Name1);                
                 low_ID = handles.cas1;
                 high_ID = handles.cas2;
                 set(handles.name1,'String',char(handles.Name1));
                 set(handles.name2,'String',char(handles.Name2));
             end    
             handles.lowBoiler = lowBoiler;
             handles.low_ID = low_ID;
             handles.high_ID = high_ID;
         end
     catch
         errordlg('One of the compounds has not been selected.  Pleas select both compounds.');
         return
     end
        
     
     [handles.p,handles.dew,handles.bubble] = plot_low_boiler(hObject, eventdata, handles);

     
     handles.output = hObject;
     guidata(hObject, handles); 
    
end

function [p,dew,bubble] = plot_low_boiler(hObject,eventdata,handles)

        conn = database('Txy_data','','');
        setdbprefs('datareturnformat','structure');
        query = ['SELECT A,B,C,Tmin,Tmax from Txy_data where CAS_ID = ' num2str(handles.low_ID)];
        query2 = ['SELECT A,B,C,Tmin,Tmax from Txy_data where CAS_ID = ' num2str(handles.high_ID)];
        result = fetch(conn,query);
        result2 = fetch(conn,query2);
        close(conn);
        
        A_low       = result.A;
        B_low       = result.B;
        C_low       = result.C;
        Tmin_low    = result.Tmin;
        Tmax_low    = result.Tmax;
        
        A_high      = result2.A;
        B_high      = result2.B;
        C_high      = result2.C;

        VP_low      = antoine(A_low,B_low,C_low,handles.T);
        VP_high     = antoine(A_high,B_high,C_high,handles.T);
        
        Pmin = min(VP_low,VP_high);
        Pmax = max(VP_low,VP_high);
        j = 1;
        for P = Pmin:.01:Pmax
            k_low(j) = VP_low / P;
            x_low(j) = (P - VP_high) / (VP_low - VP_high);
            y_low(j) = k_low(j) * x_low(j);
            p(j) = P;
            j = j + 1;
        end

     switch handles.Punit
        case 'mmHg'
            p  = p;
        case 'MPa'
            p = (p ./ 760) * 1.01325;
            Pmin = (Pmin ./ 760) * 1.01325;
            Pmax = (Pmax ./ 760) * 1.01325;
        case 'atm'
            p = p ./ 760;
            Pmin = (Pmin ./ 760);
            Pmax = (Pmax ./ 760);
        case 'bar'
            p = (p ./ 760) * 1.01325;
            Pmin = (Pmin ./ 760) * 1.01325;
            Pmax = (Pmax ./ 760) * 1.01325;
     end
    
     p = p;
     dew = y_low;
     bubble = x_low;
     

     
    hold on
    title('P-x-y Diagram');
    xlabel(['x-y (mol frac.)  ',handles.lowBoiler]);
    ylabel(['P (',handles.Punit,')']);
    plot(y_low,p,'r');
    plot(x_low,p,'b');
    ylim([Pmin Pmax]);
    legend('Dew Point Curve','Bubble Point Curve','Location','Best');
    hold off
    
    
end

%-- This is the function that defines the Antoine equation
function [VP] = antoine(A,B,C,T)
        VP = 10^(A - B/(T + C));
end

% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
    cla
  
end




% --- Executes on selection change in text3.
function listbox2_Callback(hObject, eventdata, handles)

    index_selected = get(hObject,'Value');
    list = get(hObject,'String');
    item = char(list(index_selected));
       
        conn = database('Txy_data','','');
        setdbprefs('datareturnformat','structure'); 
        query = ['SELECT ID,Name,Tb,CAS_ID FROM Txy_data WHERE Name = ''',item,''''];
        result = fetch(conn,query);
        close(conn);
        
    Tb2 = result.Tb;
    name2 = result.Name;
    cas2 = result.CAS_ID;
    
    handles.Tb2 = Tb2;
    handles.Name2 = name2;
    handles.cas2 = cas2;
    
    handles.output = hObject;
    guidata(hObject, handles);         % Update handles structure
    
end

function listbox2_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end
% --- Executes during object creation, after setting all properties.
function text3_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end



function temp_Callback(hObject, eventdata, handles)
    T = str2num(get(hObject,'String'));

    handles.T = T;
    
    handles.output = hObject;
    guidata(hObject, handles);

end
% --- Executes during object creation, after setting all properties.
function temp_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end



function P_Callback(hObject, eventdata, handles)
    P       = str2num(get(hObject,'String'));
    
    handles.P_input = P;

    handles.output = hObject;
    guidata(hObject, handles);
end
% --- Executes during object creation, after setting all properties.
function P_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end

% --- Executes on button press in calcSplits.
function calcSplits_Callback(hObject, eventdata, handles)
    
    if (isempty(handles.P) || isempty(str2num(get(handles.P,'String'))))
        errordlg('The pressure is not set. ');
        return
    elseif (isempty(handles.z) || isempty(str2num(get(handles.z,'String'))))
        errordlg('The z value is not set. ');
        return
    end
    


    P = handles.P_input;
    z = handles.z_input;
    
    p = handles.p;
    dew = handles.dew;
    bubble = handles.bubble;

    Pmax = interp1(bubble,p,z);
    Pmin = interp1(dew,p,z);
    
    if (P > Pmax || P < Pmin)
        if (P > Pmax)
            errordlg('The P,z combination selected is all liquid!');
            return
        else
            errordlg('The P,z combination selected is all vapor!');
            return
        end
    end
    
    % Calculate the Quality
    A = abs(interp1(p,bubble,P) - z);
    B = abs(interp1(p,dew,P) - z);
    Q = A / (A + B);
    
    % Calculate the splits
    L1 = interp1(p,bubble,P);
    L2 = 1 - L1;
    V1 = interp1(p,dew,P);
    V2 = 1 - V1;
    
    % Set the handles
    set(handles.quality,'String',num2str(Q));
    set(handles.L1,'String',num2str(L1));
    set(handles.L2,'String',num2str(L2));
    set(handles.V1,'String',num2str(V1));
    set(handles.V2,'String',num2str(V2));
    
    x_point = [z];
    y_point = [P];
    x_vap_tie = [L1,z];
    y_vap_tie = [P,P];
    x_liq_tie = [z,V1];
    y_liq_tie = [P,P];
    
    x_liq = [L1,L1];
    y_liq = [0 P];
    x_vap = [V1,V1];
    y_vap = [0 P];
    
    hold on
    plot(x_point,y_point,'m*');
    plot(x_vap_tie,y_vap_tie,'c');
    plot(x_liq_tie,y_liq_tie,'g');
    plot(x_liq,y_liq,'g--');
    plot(x_vap,y_vap,'c--');
    hold off
    
end

function quality_Callback(hObject, eventdata, handles)
end

% --- Executes during object creation, after setting all properties.
function quality_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function name1_Callback(hObject, eventdata, handles)
end
% --- Executes during object creation, after setting all properties.
function name1_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function V1_Callback(hObject, eventdata, handles)
end

% --- Executes during object creation, after setting all properties.
function V1_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function L1_Callback(hObject, eventdata, handles)
end
% --- Executes during object creation, after setting all properties.
function L1_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function name2_Callback(hObject, eventdata, handles)
end


% --- Executes during object creation, after setting all properties.
function name2_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function V2_Callback(hObject, eventdata, handles)
end

% --- Executes during object creation, after setting all properties.
function V2_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


function L2_Callback(hObject, eventdata, handles)
end

% --- Executes during object creation, after setting all properties.
function L2_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end

% --- Executes on button press in mmHg.
function mmHg_Callback(hObject, eventdata, handles)
    if (get(hObject,'Value') == 1)
        handles.Punit = 'mmHg';
        set(handles.bar,'Value',0);
        set(handles.MPa,'Value',0);
        set(handles.atm,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end
% --- Executes on button press in bar.
function bar_Callback(hObject, eventdata, handles)
    if (get(hObject,'Value') == 1)
        handles.Punit = 'bar';
        set(handles.mmHg,'Value',0);
        set(handles.MPa,'Value',0);
        set(handles.atm,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end
% --- Executes on button press in MPa.
function MPa_Callback(hObject, eventdata, handles)
    if (get(hObject,'Value') == 1)
        handles.Punit = 'MPa';
        set(handles.bar,'Value',0);
        set(handles.mmHg,'Value',0);
        set(handles.atm,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end

% --- Executes on button press in atm.
function atm_Callback(hObject, eventdata, handles)
    if (get(hObject,'Value') == 1)
        handles.Punit = 'atm';
        set(handles.bar,'Value',0);
        set(handles.MPa,'Value',0);
        set(handles.mmHg,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end



function z_Callback(hObject, eventdata, handles)
    if (isempty(get(hObject,'String')))
        errordlg('z is not set. ');
        return
    end
    
    z = str2num(get(hObject,'String'));
    if (z > 1 || z < 0)
        errordlg('The current z value is not possible!');
        return
    end
    handles.z_input = z;
    
    handles.output = hObject;
    guidata(hObject, handles);

end
% --- Executes during object creation, after setting all properties.
function z_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
end


% --- Executes on button press in kelvin.
function kelvin_Callback(hObject, eventdata, handles)
    if (get(hObject, 'Value') == 1)
        handles.Tunit = 'K';
        set(handles.celcius,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end

% --- Executes on button press in celcius.
function celcius_Callback(hObject, eventdata, handles)
    if (get(hObject, 'Value') == 1)
        handles.Tunit = 'C';
        set(handles.kelvin,'Value',0);
    end
    
    handles.output = hObject;
    guidata(hObject, handles);
end

Contact us