dGCALC

%% Information 

%dGCALC(GA,GB,xB,T,I0AB,I1AB,I2AB,I3AB,I4AB,I5AB) is for calculating delta Gibbs of binary single solution system.. 

% I0AB,I1AB,I2AB,I3AB,I4AB,I5AB are parameters of Redlich-Kister binary excess model (Redlich & Kister, 1948)


%Contact: x.chen@mpie.de

function[dG]= dGCALC(varargin)

narginchk(5,10)

R=8.314;

if nargin==5

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5};

switch xB

    case {0,1}

        dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB; %+0.00001 for preventing Inf cases 

    otherwise

        dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB;

end

end


if nargin==6

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5}+varargin{6}*(1-2*xB);%???????

    dIAB=varargin{6}*(-2);

switch xB

    case {0,1}

        dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB;% +xA*xB*dIAB=0 omit

    otherwise

       dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB+xA*xB*dIAB;

end

end


if nargin==7

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5}+varargin{6}*(1-2*xB)+varargin{7}*(1-2*xB)^2;

    dIAB=varargin{6}*(-2)+varargin{7}*(8*xB-4);%???????

switch xB

    case {0,1}

        dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB;

    otherwise

       dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB+xA*xB*dIAB;

end

end


if nargin==8

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5}+varargin{6}*(1-2*xB)+varargin{7}*(1-2*xB)^2+varargin{8}*(1-2*xB)^3;

    dIAB=varargin{6}*(-2)+varargin{7}*(8*xB-4)+varargin{8}*(-6*(2*xB-1)^2);

switch xB

    case {0,1}

        dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB;

    otherwise

       dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB+xA*xB*dIAB;

end

end


if nargin==9

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5}+varargin{6}*(1-2*xB)+varargin{7}*(1-2*xB)^2+varargin{8}*(1-2*xB)^3+varargin{9}*(1-2*xB)^4;

    dIAB=varargin{6}*(-2)+varargin{7}*(8*xB-4)+varargin{8}*(-6*(2*xB-1)^2)+varargin{9}*(8*(2*xB-1)^3);

switch xB

    case {0,1}

        dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB;

    otherwise

       dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB+xA*xB*dIAB;

end

end


if nargin==10

    GA=varargin{1};

    GB=varargin{2};

    xB=varargin{3};

    xA=1-xB;

    T=varargin{4};

    IAB=varargin{5}+varargin{6}*(1-2*xB)+varargin{7}*(1-2*xB)^2+varargin{8}*(1-2*xB)^3+varargin{9}*(1-2*xB)^4+varargin{10}*(1-2*xB)^5;

    dIAB=varargin{6}*(-2)+varargin{7}*(8*xB-4)+varargin{8}*(-6*(2*xB-1)^2)+varargin{9}*(8*(2*xB-1)^3)+varargin{10}*(-10*(2*xB - 1)^4);

switch xB

    case {0,1}

       dG=-GA+GB+R*T*(log(xB+0.00001)-log(xA+0.00001))+(1-2*xB)*IAB;

    otherwise

       dG=-GA+GB+R*T*(log(xB)-log(xA))+(1-2*xB)*IAB+xA*xB*dIAB;

end

end

end