// ======================================================
// Thermodynamics package in java
// water_ammonia_mix class to calculate properties of water 
// and ammonia mixture
// user interface (JApplet)
// Dr. Turhan Coban
// TUBITAK Ulusal Metroloji Enstitüs
// email : Turhan.Coban@ume.tubitak.gov.tr
// File Name   : water_ammonia_mix.java
// requires    : amonia.java, steam1.java
// =====================================================
// Reference : Thermophysical Properties of {NH3+H2O} Solutions for
// the Industrial Design of Absorbtion Refrigeration
// M. Conde Engineering 2004
import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class water_ammonia_mix extends Mathd
{
steam st;
ammonia amn;
ref_CS3 rf;

double Tcc=647.226;//K
double roc=317.763;//kg/m3
double etac=55.071E-06;//Pa.s

public water_ammonia_mix()
{ st=new steam();
  amn=new ammonia();
}
// Nomenclature used
// T temperature
// p pressure
// x molar fraction of NH3 in the liquid phase
// y molar fraction of NH3 in the vapor phase
// xx mass fraction of NH3 in liquid phase
// yy mass fraction of NH3 in vapor phase 
// h specific enthalpy
// Cp specific heat
// D mass diffisuvity
// ro density
// v  specific volume
// k thermal conductivity
// eta dynamic viscosity
// surface tension

public double mole_to_mass(double x)
{
// converts mole fraction to mass fraction
double Mh20=18.015268;
double Mnh3=17.03026;
double mass=(-x*Mnh3)/(x*Mh20-Mh20-x*Mnh3);
return mass;	
}

public double mass_to_mole(double xx)
{
// converts mass fraction to mole fraction	
double M_nh3=17.03026;
double M_h20=18.015268;
double mole=((xx/(1-xx))*M_h20)/(((xx/(1-xx))*M_h20)+M_nh3);
return mole; 
}

double T_l(double p, double x)
{
//equilibrium temperature of liquid mixture
// p pressure kPa
// x kg NH3/kg mix
// p1 pressure bar
double p1=p/100.0;
double T0=100.0;//K
double P0=20.0;//bar
double a[]={3.223020000000E+00,
-3.842060000000E-01,
4.609650000000E-02,
-3.789450000000E-03,
1.356100000000E-04,
4.877550000000E-01,
-1.201080000000E-01,
1.061540000000E-02,
-5.335890000000E-04,
7.850410000000E+00,
-1.159410000000E+01,
-5.231500000000E-02,
4.895960000000E+00,
4.210590000000E-02};
int m[]={0,0,0,0,0,1,1,1,2,4,5,5,6,13};
int n[]={0,1,2,3,4,0,1,2,3,0,0,1,0,1};
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((1-x),m[i])*Math.pow(Math.log(P0/p1),n[i]);}
tt*=T0;
return tt;
}

public double T_lroot(double Tinit,double p, double x)
{
//equilibrium temperature of liquid mixture
// p pressure kPa
// x kg NH3/kg mix
// p1 pressure bar
double p1=p/100.0;
double T0=100.0;//K
double P0=20.0;//bar
double a[]={3.223020000000E+00,
-3.842060000000E-01,
4.609650000000E-02,
-3.789450000000E-03,
1.356100000000E-04,
4.877550000000E-01,
-1.201080000000E-01,
1.061540000000E-02,
-5.335890000000E-04,
7.850410000000E+00,
-1.159410000000E+01,
-5.231500000000E-02,
4.895960000000E+00,
4.210590000000E-02};
int m[]={0,0,0,0,0,1,1,1,2,4,5,5,6,13};
int n[]={0,1,2,3,4,0,1,2,3,0,0,1,0,1};
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((1-x),m[i])*Math.pow(Math.log(P0/p1),n[i]);}
tt*=T0;
return tt-Tinit;
}

public double T_groot(double Tinit,double p, double y)
{
double p1=p/100.0;
double T0=100.0;//K
double P0=20.0;//bar
double a[]={3.240040E+00,
-3.959200E-01,
4.356240E-02,
-2.189430E-03,
-1.435260E+00,
1.052560E+00,
-7.192810E-02,
1.223620E+01,
-2.243680E+00,
-2.017800E+01,
1.108340E+00,
1.453990E+01,
6.443120E-01,
-2.212460E+00,
-7.562660E-01,
-1.355290E+00,
1.835410E-01
};
int m[]={0,0,0,0,1,1,1,2,2,3,3,4,4,5,5,6,7};
int n[]={0,1,2,3,0,1,2,0,1,0,1,0,2,0,2,0,2};
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((1-y),(m[i]/4.0))*Math.pow(Math.log(P0/p1),n[i]);}
tt*=T0;
return tt-Tinit;	
}

public double rootTgx(double Tinit,double p)
{//newton_secand_bisection

double tol=0.1;
double maxiter=300;	
double h=0.01;
double dxx=0.01;
double bb=1.0;
double aa=0.0;
double[] xx=rootsearchv(Tinit,p,aa,bb,dxx);
double x1=xx[0];
double x2=xx[1];
double f1=T_groot(Tinit,p,x1);
double f2=T_groot(Tinit,p,x2);

if(f1*f2>0)
{System.out.println("There are no roots in the region defined");}

double root=0;;
if(f1==0)
{root=x1;

}	

if(f2==0)	
{root=x2;

}
double a;
double b;
if(f1<0)
{a=x1;
 b=x2;
}
else
{
a=x1;b=x2;	
}
double r=0.5*(x1+x2);
double dxold=Math.abs(x2-x1);
double dx=dxold;
double fr=T_groot(Tinit,p,r);
double dfr=((-1*T_groot(Tinit,p,r+2*h))+(8*T_groot(Tinit,p,r+h))-(8*T_groot(Tinit,p,r-h))+T_groot(Tinit,p,r-2*h))/(12*h);
double temp;
for(int j=1;j<maxiter;j++)
{
if(((((r-b)*dfr-fr)*((r-a)*dfr-fr))>0.0)||(Math.abs(2*fr)>Math.abs(dxold*dfr)))
{
dxold=dx;	
dx=0.5*(b-a);
r=a+dx;
    if(a==r)
     {root=r;}
else
{
dxold=dx;	
dx=fr/dfr;
temp=r;
r-=dx;	
    if(temp==r)
    {root=r;}	
}
}	

if(Math.abs(dx)<tol)
{root=r;} 
fr=T_groot(Tinit,p,r);
dfr=((-1*T_groot(Tinit,p,r+2*h))+(8*T_groot(Tinit,p,r+h))-(8*T_groot(Tinit,p,r-h))+T_groot(Tinit,p,r-2*h))/(12*h);
if(fr<0.0)
{
a=r;	
}	
else
{
b=r;
}
}
return root;	
}
public double[] rootsearchl(double Tinit,double p,double a,double b,double dx)
{
double x1=a;double x2=a+dx;	
double f1=T_lroot(Tinit,p,x1);
double f2=T_lroot(Tinit,p,x2);	

double[] x=new double[2];
x[0]=x1;
x[1]=x2;
while(f1*f2>0)
{
	if(x[0]>=b)
	{x[0]=0;x[1]=0;}
x[0]=x[1];
f1=f2;
x[1]=x[0]+dx;
f2=T_lroot(Tinit,p,x[1]);	
}	
return x;	
}
public double[] rootsearchv(double Tinit,double p,double a,double b,double dx)
{
double x1=a;double x2=a+dx;	
double f1=T_groot(Tinit,p,x1);
double f2=T_groot(Tinit,p,x2);	

double[] x=new double[2];
x[0]=x1;
x[1]=x2;
while(f1*f2>0)
{
	if(x[0]>=b)
	{x[0]=0;x[1]=0;}
x[0]=x[1];
f1=f2;
x[1]=x[0]+dx;
f2=T_groot(Tinit,p,x[1]);	
}	
return x;	
}
double T_g(double p, double y)
{
//equilibrium temperature of gas mixture
// p pressure kPa
// y kg NH3/kg mix
// p1 pressure bar
double p1=p/100.0;
double T0=100.0;//K
double P0=20.0;//bar
double a[]={3.240040E+00,
-3.959200E-01,
4.356240E-02,
-2.189430E-03,
-1.435260E+00,
1.052560E+00,
-7.192810E-02,
1.223620E+01,
-2.243680E+00,
-2.017800E+01,
1.108340E+00,
1.453990E+01,
6.443120E-01,
-2.212460E+00,
-7.562660E-01,
-1.355290E+00,
1.835410E-01
};
int m[]={0,0,0,0,1,1,1,2,2,3,3,4,4,5,5,6,7};
int n[]={0,1,2,3,0,1,2,0,1,0,1,0,2,0,2,0,2};
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((1-y),(m[i]/4.0))*Math.pow(Math.log(P0/p1),n[i]);}
tt*=T0;
return tt;
}
public double rootTLx(double Tinit,double p)
{//newton_secand_bisection
double tol=0.1;
double maxiter=300;	
double h=0.01;
double dxx=0.01;
double bb=1.0;
double aa=0.0;
double[] xx=rootsearchl(Tinit,p,aa,bb,dxx);
double x1=xx[0];
double x2=xx[1];
double f1=T_lroot(Tinit,p,x1);
double f2=T_lroot(Tinit,p,x2);

if(f1*f2>0)
{System.out.println("Tanımlanan aralıkta kök bulunmuyor");}

double root=0;;
if(f1==0)
{root=x1;

}	

if(f2==0)	
{root=x2;

}
double a;
double b;
if(f1<0)
{a=x1;
 b=x2;
}
else
{
a=x1;b=x2;	
}
double r=0.5*(x1+x2);
double dxold=Math.abs(x2-x1);
double dx=dxold;
double fr=T_lroot(Tinit,p,r);
double dfr=((-1*T_lroot(Tinit,p,r+2*h))+(8*T_lroot(Tinit,p,r+h))-(8*T_lroot(Tinit,p,r-h))+T_lroot(Tinit,p,r-2*h))/(12*h);
double temp;
for(int j=1;j<maxiter;j++)
{
if(((((r-b)*dfr-fr)*((r-a)*dfr-fr))>0.0)||(Math.abs(2*fr)>Math.abs(dxold*dfr)))
{
dxold=dx;	
dx=0.5*(b-a);
r=a+dx;
    if(a==r)
     {root=r;}
else
{
dxold=dx;	
dx=fr/dfr;
temp=r;
r-=dx;	
    if(temp==r)
    {root=r;}	
}
}	

if(Math.abs(dx)<tol)
{root=r;} 
fr=T_lroot(Tinit,p,r);
dfr=((-1*T_lroot(Tinit,p,r+2*h))+(8*T_lroot(Tinit,p,r+h))-(8*T_lroot(Tinit,p,r-h))+T_lroot(Tinit,p,r-2*h))/(12*h);
if(fr<0.0)
{
a=r;	
}	
else
{
b=r;
}
}
return root;	
}
double y_Px(double p, double x)
{
// y molar fraction of NH3 in the vapor phase
// P pressure kPa
// y kg NH3/kg mix
// P1 pressure bar
double P=p/100.0;
double T0=100.0;//K
double P0=20.0;//bar
double a[]={1.980220170E+01,
-1.180926690E+01,
2.774799800E+01,
-2.886342770E+01,
-5.916166080E+01,
5.780913050E+02,
-6.217367430E+00,
-3.421984020E+03,
1.194031270E+04,
-2.454137770E+04,
2.915918650E+04,
-1.847822900E+04,
2.348194340E+01,
4.803106170E+03};
int m[]={0,0,0,0,1,2,2,3,4,5,6,7,7,8};
int n[]={0,1,6,7,0,1,2,2,3,4,5,6,7,7};
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((P/P0),m[i])*Math.pow(x,(n[i]/3.0));}
tt=1-Math.exp(Math.log(1-x)*tt);
return tt;
}

public double hlv(double Tsol,double p,double x)
{
double xl=rootTLx(Tsol,p);
double yg=y_Px(p,xl);
double yx=rootTgx(Tsol,p);
double hgg=hg(p,x);
double hll=hlnew(Tsol,p,x);
return hll*(1-x)+hgg*(x);//hll*(yx-x)+hgg*(x-xl);
}

public double slv(double Tsol,double p,double x)
{
double Tl=T_l(p,x);
double Tg=T_g(p,x);		
double sg1=sg(Tg,p,x);	
double sl1=sl(p,x);

return (1-x)*sl1+x*sg1;	
}

public double Cplv(double Tsol,double p,double x)
{
double Tl=T_l(p,x);
double Tg=T_g(p,x);
double cpv=sol_Cpv(Tg,x);	
double cpl=Cp_sol(Tl,x);
double cpmix=((Tsol-Tl)*(cpv-cpl)/(Tg-Tl))+cpl;	
return cpmix;	
}
public double klv(double Tsol,double p,double x)
{ // equation is ot valid in very high temperature due to interpolation limits
double Tl=T_l(p,x);
double Tg=T_g(p,x);	
double kv1=kv_sol(Tg,x);
double kl1=k_sol(Tl,x);
double kmix=((Tsol-Tl)*(kv1-kl1)/(Tg-Tl))+kl1;		
return kmix;	
}
public double ro_lv(double Tsol,double p,double x)
{ // equation is ot valid in very high temperature due to interpolation limits
double Tl=T_l(p,x);
double Tg=T_g(p,x);		
double roliq=ro_sol(Tl,x);
double rovap=rov_sol(Tg,x);	
double romix=((Tsol-Tl)*(rovap-roliq)/(Tg-Tl))+roliq;		
return rovap;	
}
public double k_lv(double Tsol,double p,double x)
{
double Tl=T_l(p,x);
double Tg=T_g(p,x);	
double kl=k_sol(Tsol,x);
double kv=kv_sol(Tsol,x);
double kmix=((Tsol-Tl)*(kv-kl)/(Tg-Tl))+kl;	
return kmix;		
}
public  double P(double Tsoll,double x)
{
// return P in kPa;
double Tsol=Tsoll-273.15; 
double xx=mole_to_mass(x);	
double A9[]={+ 1.2328,-9.9394,+2.6586E01,-1.3512E01};
double B9[]={+ 1.8947E-02,- 1.9512E-01,+ 7.1830E-01,- 3.8253E-01};
double C9[]={- 7.5905E-04,+ 2.5043E-03,+ 3.8511E-03,-3.5429E-03};
double D9[]={+ 5.5294E-06,+ 3.1725E-06,- 1.4522E-05,+ 2.7155E-05};

double C=0;
for(int i=0;i<A9.length;i++)
{C+=(A9[i]+B9[i]*Tsol+C9[i]*Tsol*Tsol+D9[i]*Tsol*Tsol*Tsol)*Math.pow(xx,i);}	
return C*100;	
}







public double h_g_t_p(double Ti,double pi,double y)
{
pi=pi/100;
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;
double[] A_nh3={3.971423E-02,-1.790557E-05,-1.308905E-02,3.752836E-03};	
double[] A_h20={2.748796E-02,-1.016665E-05,-4.452025E-03,8.389246E-04};
double[] B_nh3={1.634519E01,-6.508119,1.448937};
double[] B_h20={1.214557E01,-1.898065,2.911966E-01};
double[] C_nh3={-1.049377E-02,-8.288224,-6.647257E02,-3.045352E03};
double[] C_h20={2.136131E-02,-3.169291E01,-4.634611E04,0.0};
double[] D_nh3={3.673647,9.989629E-02,3.617622E-02};
double[] D_h20={4.019170,-5.175550E-02,1.951939E-02};
double hg_nh3=26.468879;
double hg_h20=60.965058;
double E1=-41.733398;
double E2=0.02414;
double E3=6.702285;
double E4=-0.011475;
double E5=63.608967;
double E6=-62.490768;
double E7=1.761064;
double E8=0.008626;
double E9=0.387983;
double E10=0.004772;
double E11=-4.648107;
double E12=0.836376;
double E13=-3.553627;
double E14=0.000904;
double E15=24.361723;
double E16=-20.736547;
//double h_h20=((-R/18)*TB)*(1-y)*(hg_h20+(D_h20[0]*(T-T0_h20))+(0.5*D_h20[1]*(T*T-T0_h20*T0_h20))+(0.333*D_h20[2]*(T*T*T-T0_h20*T0_h20*T0_h20))+(C_h20[0]*(p-P0_h20))+(4*C_h20[1]*((p/(T*T*T))-(P0_h20/(T0_h20*T0_h20*T0_h20))))+(12*C_h20[2]*((p/Math.pow(T,11))-(P0_h20/Math.pow(T0_h20,11))))+4*C_h20[3]*((Math.pow(p,3)/Math.pow(T,11))-(Math.pow(P0_h20,3)/Math.pow(T0_h20,11))));
//double h_nh3=((-R/17)*TB)*(y)*(hg_nh3+(D_nh3[0]*(T-T0_nh3))+(0.5*D_nh3[1]*(T*T-T0_nh3*T0_nh3))+(0.333*D_nh3[2]*(T*T*T-T0_nh3*T0_nh3*T0_nh3))+(C_nh3[0]*(p-P0_nh3))+(4*C_nh3[1]*((p/(T*T*T))-(P0_nh3/(T0_nh3*T0_nh3*T0_nh3))))+(12*C_nh3[2]*((p/Math.pow(T,11))-(P0_nh3/Math.pow(T0_nh3,11))))+4*C_nh3[3]*((Math.pow(p,3)/Math.pow(T,11))-(Math.pow(P0_nh3,3)/Math.pow(T0_nh3,11))));
double hgg_h20=(-R/18)*TB*(-hg_h20+(D_h20[0]*T0_h20)+(0.5*D_h20[1]*(T*T+T0_h20*T0_h20))+(0.666*D_h20[2]*T*T*T)+(0.333*D_h20[2]*T0_h20*T0_h20*T0_h20)-(D_h20[0]*T)-(D_h20[1]*T*T)-(0.5*D_h20[2]*(T*T+T0_h20*T0_h20))-(C_h20[0]*(p-P0_h20))+(C_h20[1]*((-4*p/T/T/T)+(4*P0_h20/T0_h20/T0_h20/T0_h20)))+(C_h20[2]*((-12*p/(Math.pow(T,11)))+(12*P0_h20/(Math.pow(T0_h20,11)))))+(0.333*C_h20[3]*((-12*p*p*p/Math.pow(T,11))+(12*P0_h20*P0_h20*P0_h20/(Math.pow(T0_h20,11))))));
double hgg_nh3=(-R/17)*TB*(-hg_nh3+(D_nh3[0]*T0_nh3)+(0.5*D_nh3[1]*(T*T+T0_nh3*T0_nh3))+(0.666*D_nh3[2]*T*T*T)+(0.333*D_nh3[2]*T0_nh3*T0_nh3*T0_nh3)-(D_nh3[0]*T)-(D_nh3[1]*T*T)-(0.5*D_nh3[2]*(T*T+T0_nh3*T0_nh3))-(C_nh3[0]*(p-P0_nh3))+(C_nh3[1]*((-4*p/T/T/T)+(4*P0_nh3/T0_nh3/T0_nh3/T0_nh3)))+(C_nh3[2]*((-12*p/(Math.pow(T,11)))+(12*P0_nh3/(Math.pow(T0_nh3,11)))))+(0.333*C_nh3[3]*((-12*p*p*p/Math.pow(T,11))+(12*P0_nh3*P0_nh3*P0_nh3/(Math.pow(T0_nh3,11))))));

return y*hgg_nh3+(1-y)*hgg_h20;
}

public double hg(double p,double x)
{
double Tsol=T_g(p,x);
double T0=324;//K
double h0=1000;//kj/kg
double[] a={1.288270000,1.252470000E-01,-2.087480000,2.176960000,2.356870000,-8.869870000,1.026350000E+01,-2.374400000,-6.701550000,1.645080000E+01,-9.368490000,8.422540000,-8.588070000,-2.770490000,-9.612480000E-01,9.880090000E-01,3.084820000e-1};
double[] m={0,1,2,3,0,1,2,3,0,1,2,0,1,0,4,2,1}; 	
double[] n={0,0,0,0,2,2,2,2,3,3,3,4,4,5,6,7,10}; 	

double l=a.length;
double tt=0;
for(int i=0;i<a.length;i++)
{tt+=a[i]*Math.pow((1-Tsol/T0),m[i])*Math.pow(1-x,n[i]/4);}
tt*=h0;
return tt;
} 

public double hl(double p,double x)
{
double Tsol=T_l(p,x);
Tsol-=273.15;
double[] a={-1.8056E01,-7.2789E02,-1.2275E02,+1.6910E03,-8.1873E02};
double[] b={+5.3693,-1.2381E01,+4.1312E01,-5.0338E01,+ 2.0859E01};
double[] c={-2.0134E-02,+2.2495E-01,- 7.4557E-01,+ 9.9641E-01,- 4.6552E-01};
double[] d={+9.8404E-05,-1.1315E-03,+ 4.1910E-03,-6.1301E-03,+ 3.1072E-03};
double t=0;
for(int i=0;i<a.length;i++)
{t+=(a[i]+(b[i]*Tsol)+(c[i]*Tsol*Tsol)+(d[i]*Tsol*Tsol*Tsol))*Math.pow(x,i);}
return t;
}

public double sl(double p,double x)
{
double T=T_l(p,x);
T-=273.15;
double eps=mole_to_mass(x);
double[][] a=new double[4][6];
a[0][0]=+2.1713E-02;
a[0][1]=+1.5910E-02;
a[0][2]=-5.0981E-05;
a[0][3]=+3.2942E-07;
a[0][4]=-1.0652E-09;
a[0][5]=0.0;
a[1][0]=+7.8243E-01;
a[1][1]=-6.6703E-03;
a[1][2]=+2.0955E-04;
a[1][3]=-2.5047E-06;
a[1][4]=+1.7901E-08;
a[1][5]=-4.6583E-11;
a[2][0]=+1.2675E-01;
a[2][1]=+2.4487E-02;
a[2][2]=-3.9966E-04;
a[2][3]=+3.5886E-06;
a[2][4]=-2.3771E-08;
a[2][5]=+5.5948E-11;
a[3][0]=+5.7267E-01;
a[3][1]=-1.6800E-02;
a[3][2]=+2.1541E-04;	
a[3][3]=-1.5282E-06;
a[3][4]=+1.0844E-08;
a[3][5]=-2.3703E-11;
double sumin;
double sumout=0;

for(int i=0;i<=3;i++)
{    sumin=0;
     for(int j=0;j<=5;j++)
       {sumin+=a[i][j]*Math.pow(T,j);}
     sumout+=sumin*Math.pow(eps,i);
}
return sumout;	
}

public double hlnew(double Ti,double pi,double x)
{
pi=pi/100;
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;
double[] A_nh3={3.971423E-02,-1.790557E-05,-1.308905E-02,3.752836E-03};	
double[] A_h20={2.748796E-02,-1.016665E-05,-4.452025E-03,8.389246E-04};
double[] B_nh3={1.634519E01,-6.508119,1.448937};
double[] B_h20={1.214557E01,-1.898065,2.911966E-01};
double E1=-4.626129E01;
double E2=2.060225E-02;
double E3=7.292369;
double E4=-1.032613E-02;
double E5=8.074824E01;
double E6=-8.461214E01;
double E7=2.452882E01;
double E8=9.598767E-03;
double E9=-1.475383;
double E10=-5.038107E-03;
double E11=-9.640398E01;
double E12=1.226973E02;
double E13=-7.582637;
double E14=6.012445E-04;
double E15=5.487018E01;
double E16=-7.667596E01;
double h_nh3=4.878573;
double h_h20=21.821141;	
double hlwat=(R/18)*TB*(1-x)*(h_h20+(B_h20[0]*(T-T0_h20))+((0.5*B_h20[1]*(T*T-T0_h20*T0_h20)))+(0.333*B_h20[2]*(T*T*T-T0_h20*T0_h20*T0_h20))+((A_h20[0]-A_h20[3]*T*T)*(p-P0_h20))+(0.5*A_h20[1]*(p*p-P0_h20*P0_h20)));
double hlnh3=(R/17)*TB*(x)*(h_nh3+(B_nh3[0]*(T-T0_nh3))+((0.5*B_nh3[1]*(T*T-T0_nh3*T0_nh3)))+(0.333*B_nh3[2]*(T*T*T-T0_nh3*T0_nh3*T0_nh3))+((A_nh3[0]-A_nh3[3]*T*T)*(p-P0_nh3))+(0.5*A_nh3[1]*(p*p-P0_nh3*P0_nh3)));
double hmix=(R/(17*x+18*(1-x)))*TB*x*(1-x)*(((E1+(E2*p)+(2*E5/T)+(3*E6/T/T)))+((2*x-1)*(E7+(E8*p)+(2*E11/T)+(3*E12/T/T)))+(((2*x-1)*(2*x-1))*(E13+(E14*p)+(2*E15/T)+(3*E16/T/T))));
return hlwat+hlnh3+hmix;
}
public double T_crit_sol(double x) 
// Critical Temperature of solution in Kelvin
// x molar fraction of NH3 in the liquid phase
{ double[] a={647.14,-199.822371,109.035522,-239.626217,88.689691}; 
double toptc=0;
for(int i=0;i<a.length;i++)
{toptc+=a[i]*Math.pow(x,i);}
return toptc; // in Kelvin
}

public double P_crit_sol(double x)
// Critical Pressure of solution in Kelvin
// x molar fraction of NH3 in the liquid phase
{double[] b={220.64,-37.923795,36.424739,-41.851597,-63.805617};
double toppc=0;
for(int i=0;i<b.length;i++)
{toppc+=b[i]*Math.pow(x,i);}
return toppc*100;
}

public double CpNH3(double Tsol)
{
// Specific thermal capacities of the ammonia
// Tsol : solution temperature in KELVİN	
double NH3_Acp=3.875648;
double NH3_Bcp=0.242125;	
double Tcnh3=405;
double ronh3=1-(Tsol/Tcnh3);
double Cpnh3=NH3_Acp+NH3_Bcp*(1/ronh3);	
return Cpnh3;	
}
public double CpH20(double Tsol)
{
// Specific thermal capacities of the water
// Tsol : solution temperature in KELVİN
double H20_Acp=3.665785;
double H20_Bcp=0.236312;
double Tch20=647.14;// in Kelvin
double roh20=1-(Tsol/Tch20);	
return 	H20_Acp+H20_Bcp*(1/roh20);	
}
public double Cp_sol(double Tsol,double x)
{
// Solution thermal capacity
// x molar fraction of NH3 in the liquid phase
// Tsol : solution temperature in KELVİN
double Cpsol=x*CpNH3(Tsol)+(1-x)*CpH20(Tsol);
return Cpsol;	
}
public double k_water(double Tsol)
{
// Calculating thermal conductivity of water 
// Tsol in KELVIN
double Tsoll=Tsol-273;
double Tcritwater=647.26;
double Tdm=Tsol/Tcritwater;
double[] L0={0.0102811,0.0299621,0.0156146,-0.00422464,0.0,0.0};
double kapppa0=0;
for(int i=0;i<(L0.length-2);i++)
{kapppa0+=L0[i]*Math.pow(Tdm,i);}
double value1=Math.sqrt(Tdm)*kapppa0;
ref_CS3 refc31=new ref_CS3();

double ro_ll=refc31.k_l(Tsoll);
double ro_crit=317.7;//kg/m3
double rodm=ro_ll/ro_crit;
double[] L1={-0.397070,0.400302,1.060000,-0.171587,2.392190,0.0};
double value2=L1[0]+L1[1]*rodm+L1[2]*Math.exp(L1[3]*(rodm+L1[4])*(rodm+L1[4]));
double[] L2={0.0701309,0.0118520,0.00169937,-1.0200,0.0,0.0};
double[] L3={0.642857,-4.11717,-6.17937,0.00308976,0.0822994,10.0932};
double dt=Math.abs(Tdm-1)+L3[3];
double V0=0;
double V1=2+(L3[4]/Math.pow(dt,0.6));

if(dt>=1){V0=1/dt;}
else{V0=L3[5]/Math.pow(dt,0.6);}
double val1=(L2[0]/(Math.pow(Tdm,10))+L2[1])*Math.pow(rodm,1.8)*Math.exp(L3[0]*(1-Math.pow(rodm,2.8)));
double val2=L2[2]*V0*Math.pow(rodm,V1)*Math.exp((V1/(1+V1))*(1-Math.pow(rodm,1+V1)));
double val3=L2[3]*Math.exp(L3[1]*Math.pow(Tdm,1.5)+(L3[2]/(Math.pow(rodm,5))));
double value3=val1+val2+val3;
double topval=value1*value2*value3;
return ro_ll;
}
public double k_nh3(double Tsol)
{
// Calculating thermal conductivity of NH3;
// Tsol in KELVIN 
//double[] A={8.902275E02,-0.69235,-2.401E-03,0.0};
//double topl=0;
//for(int i=0;i<A.length;i++)
//{topl+=A[i]*Math.pow(Tsol,i);}
ref_CS3 refcs3=new ref_CS3("R717");
double topl=refcs3.k_l(Tsol-273.15);
return topl;
}
public double k_sol(double Tsol,double x)
{
// Calculating thermal conductivity of solution
// x molar fraction of NH3 in the liquid phase
return x*k_nh3(Tsol)+(1-x)*k_water(Tsol);	
}
public double vis_water(double Tsol)
// Calculating viscosity of water
// Tsol in KELVİN
{
double Tsolt=Tsol-273;
double[] H={1.000,0.978197,0.579829,-0.202354};
double Tf=Tsol/Tcc;
ref_CS3 recs33=new ref_CS3();
double royt=recs33.rol(Tsolt);	
double rof=royt/roc;
double top1=0;
for(int i=0;i<H.length;i++)
{top1+=H[i]*Math.pow(Tf,-i);}
double topll=Math.sqrt(Tf)*(1/top1);
double eta0=topll;
double[][] G=new double[6][7];
G[0][0]=0.5132047;
G[0][1]=0.2151778;
G[0][2]=-0.2818107;
G[0][3]=0.1778064;
G[0][4]=-0.0417661;
G[0][5]=0.0;
G[0][6]=0.0;
G[1][0]=0.3205656;
G[1][1]=0.7317883;
G[1][2]=-1.070786;
G[1][3]=0.4605040;
G[1][4]=0.0;
G[1][5]=-0.01578386;
G[1][6]=0.0;
G[2][0]=0.0;
G[2][1]=1.241044;
G[2][2]=-1.263184;
G[2][3]=0.2340379;
G[2][4]=0.0;
G[2][5]=0.0;
G[2][6]=0.0;
G[3][0]=0.0;
G[3][1]=1.476783;
G[3][2]=0.0;
G[3][3]=-0.4924179;
G[3][4]=0.1600435;
G[3][5]=0.0;
G[3][6]=-0.003629481;
G[4][0]=-0.7782567;
G[4][1]=0.0;
G[4][2]=0.0;
G[4][3]=0.0;
G[4][4]=0.0;
G[4][5]=0.0;
G[4][6]=0.0;
G[5][0]=0.1885447;
G[5][1]=0.0;
G[5][2]=0.0;
G[5][3]=0.0;
G[5][4]=0.0;
G[5][5]=0.0;
G[5][6]=0.0;
double sumout=0;
 double sumin=3;
   for(int i=0;i<G.length;i++){
        sumin=0;
	   for(int j=0;j<G[0].length;j++)
    {sumin+=G[i][j]*Math.pow((rof-1),j)*Math.pow(((1/Tf)-1),i);}
  sumout+=sumin; }
double dummy=sumout*rof;
double eta1=Math.exp(dummy);
double eta2=1;
return eta0*eta1*etac;
}

public double nh3_vis(double Tsol)
{
double[] An={4.99318220,-0.61122364,0.0,0.18535124,-0.11160946};
double[] Cn={-0.17999496E01,0.46692621E02,-0.53460794E03,0.33604074E04,-0.13019164E05,0.33414230E05,-0.58711743E05,0.71426686E05,-0.59834012E05,0.33652741E05,-0.12027350E05,0.24348205E04,-0.20807957E03};	

double[][] dn=new double[5][5];
dn[0][0]=0.0;
dn[0][1]=0.0;
dn[0][2]=0.0;
dn[0][3]=0.0;
dn[0][4]=0.0;

dn[1][0]=0.0;
dn[1][1]=0.0;
dn[1][2]=0.0;
dn[1][3]=0.0;
dn[1][4]=0.0;

dn[2][0]=0.0;
dn[2][1]=0.0;
dn[2][2]=2.19664285E-01;
dn[2][3]=0.0;
dn[2][4]=-0.83651107E-01;

dn[3][0]=0.17366936E-02;
dn[3][1]=-0.64250359E-02;
dn[3][2]=0.0;
dn[3][3]=0.0;
dn[3][4]=0.0;

dn[4][0]=0.0;
dn[4][1]=0.0;
dn[4][2]=1.67668649E-04;
dn[4][3]=-1.49710093E-04;
dn[4][4]=0.77012274E-04;


double M_nh3=17.03026;//kg/kmol	
double epss=0.2957;//nm
double eK=386;// Kelvin
double theta=Tsol*(1/eK);	
double tf=0;
for(int i=0;i<An.length;i++)
{tf+=An[i]*Math.pow(Math.log(theta),i);}
double epsi=Math.exp(tf);
double no=2.1357*Math.sqrt(Tsol*M_nh3)/(epss*epss*epsi);
double toplam=0	;
for(int j=0;j<Cn.length;j++)
{toplam+=Cn[j]*Math.pow(Math.sqrt(theta),-j);}
double b1=no*epss*epss*epss*toplam*0.6022137;
 rf=new ref_CS3("R717");
double TsolC=Tsol-273.15;
double rovis=rf.viscosity_l(TsolC);
double rofluid=rf.rol(TsolC);
double toplin;
double toplout=0;
for(int i=2;i<=4;i++)
{   toplin=0;
	for(int j=0;j<=4;j++)
	{toplin+=dn[i][j]/Math.pow(theta,j);}
	toplout+=rofluid*toplin;  
}
double n1=b1*rofluid+toplout;
return rovis; 
}

public double vis_sol(double Tsol,double x)
{

double f1=6.38*Math.pow(1-x,1.125*x)*(1-Math.exp(-0.585*x*Math.pow(1-x,0.18)))*Math.log(Math.sqrt(nh3_vis(Tsol)*vis_water(Tsol)));
double nsol=(0.534-(0.815*Tsol/Tcc))*f1;
double lnvis=x*Math.log(nh3_vis(Tsol))+(1-x)*Math.log(vis_water(Tsol))+nsol;	
double vis=Math.exp(lnvis);	
return vis;//x*nh3_vis(Tsol)+(1-x)*vis_water(Tsol);   
}

public double surftens(double Tsol,double x)
{
ref_CS3EN df=new ref_CS3EN("R717"); 	
double tsol=Tsol-273.15;
double nh3tens=df.surfacetension(tsol);
double h20tens=df.surfacetension(tsol);
double fx=(1.442*(1-x)*(1-Math.exp(-2.5*x*x*x*x)))+(1.106*x*(1-Math.exp(-2.5*(1-x)*(1-x)*(1-x)*(1-x)*(1-x)*(1-x))));
double dtsol=-1*(h20tens-nh3tens)*fx;
return x*nh3tens+(1-x)*h20tens+dtsol;
}
public double ro_water(double Tsol)
{
ref_CS3 rff=new ref_CS3();
return rff.rol(Tsol-273);
}
public double ro_nh3(double Tsol)
{
ref_CS3 rfff=new ref_CS3("R717");
return rfff.rol(Tsol-273);
}
public double ro_sol(double Tsol,double x)
{
double[] A1={-2.410,8.310,-6.924};
double[] A2={2.118,-4.050,4.443};
double dtsol=Tsol/Tcc;
double s1=0;double s2=0;
for(int i=0;i<A1.length;i++)
{s1+=A1[i]*Math.pow(dtsol,i);}
for(int i=0;i<A2.length;i++)
{s2+=A2[i]*Math.pow(dtsol,i);}	
double A=s1+s2/x;
double rosol=(x*(1-x)-A*x*x*(1-x))*Math.pow(ro_water(Tsol),0.5)*Math.pow(ro_nh3(Tsol),0.5);
return x*ro_nh3(Tsol)+(1-x)*ro_water(Tsol)+rosol;
}
public double mass_diff(double Tsol,double x)
{
double w_H20=2.6;
double w_NH3=1.7;	
double M_NH3=17.03;
double M_H20=18.0152;
double w_sol=x*w_NH3+(1-x)*w_H20;
double M_sol=x*M_NH3+(1-x)*M_H20;
double V_diff=M_NH3/ro_nh3(Tsol);
double D=117.282E-18*Tsol*Math.sqrt(w_sol*M_sol)/(vis_sol(Tsol,x)*Math.pow(V_diff,0.6));
return D;
}
public double vis_vnh3(double Tsol)
{ref_CS3 ref1=new ref_CS3("R717");
 return  ref1.viscosity_v(Tsol-273.15);}
public double k_vnh3(double Tsol)
{ref_CS3 ref2=new ref_CS3("R717");
 return  ref2.k_v(Tsol-273.15);}
 public double vis_vh20(double Tsol)
{ref_CS3 ref3=new ref_CS3();
 return  ref3.viscosity_v(Tsol-273.15);}
 public double k_vh20(double Tsol)
{ref_CS3 ref4=new ref_CS3();
 return  ref4.k_v(Tsol-273.15);}
public double kv_sol(double Tsol,double y)
{
double MNH3=17.0315;
double MH20=18.0152;
double to=1-(Tsol/Tcc);  	
double[] AA={-0.48173,20.04383,0.0,0.0};	
double phi12=Math.pow((1+Math.sqrt(vis_vnh3(Tsol)/vis_vh20(Tsol))*Math.pow(MH20/MNH3,0.25)),2)/(Math.sqrt(8*(1+(MNH3/MH20))));
double phi21=phi12*vis_vh20(Tsol)*MNH3/(vis_vnh3(Tsol)*MH20);
double kv=((y*k_vnh3(Tsol))/(y+(1-y)*phi12))+(((1-y)*k_vh20(Tsol))/((1-y)+y*phi21));
return (y*k_vnh3(Tsol))+(1-y)*k_vh20(Tsol); 
}
public double vissol_v(double Tsol,double y)
{
double MNH3=17.0315;
double MH20=18.0152;	
double phi12=Math.pow((1+Math.sqrt(vis_vnh3(Tsol)/vis_vh20(Tsol))*Math.pow(MH20/MNH3,0.25)),2)/(Math.sqrt(8*(1+(MNH3/MH20))));
double phi21=phi12*vis_vh20(Tsol)*MNH3/(vis_vnh3(Tsol)*MH20);
double etav=((y*vis_vnh3(Tsol))/(y+(1-y)*phi12))+(((1-y)*vis_vh20(Tsol))/((1-y)+y*phi21));	
return  etav;	
}
public double rovv_nh3(double Tsol)
{ref_CS3 ref5=new ref_CS3("R717");
return ref5.rov(Tsol-273.15);
}
public double rovv_h20(double Tsol)
{ref_CS3 ref6=new ref_CS3();
return ref6.rov(Tsol-273.15);
}
public double rov_sol(double Tsol,double y)
{
double A=82.0;double B=0.5;double C=-0.05;double D=2.75;double J=9.952;double K=3.884;
double 	dtsol=Tsol/Tcc;
double romax=Math.exp(J-K/dtsol);
double roso=A*Math.pow(1-y,B)*(1-Math.exp(C*(Math.pow(y,D))))*romax;
return y*rovv_nh3(Tsol)+(1-y)*rovv_h20(Tsol)+roso;
}
public double nh3_cpv(double Tsol)
{ref_CS3 ref7=new ref_CS3("R717");
return ref7.Cpv(Tsol-273.15);
}
public double h20_cpv(double Tsol)
{ref_CS3 ref8=new ref_CS3();
return ref8.Cpv(Tsol-273.15);
}
public double sol_Cpv(double Tsol,double y)
{return y*nh3_cpv(Tsol)+(1-y)*h20_cpv(Tsol);}
public double sg(double Ti,double pi,double x)
{
pi=pi/100;	
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;
double[] A_nh3={3.971423E-02,-1.790557E-05,-1.308905E-02,3.752836E-03};	
double[] A_h20={2.748796E-02,-1.016665E-05,-4.452025E-03,8.389246E-04};
double[] B_nh3={1.634519E01,-6.508119,1.448937};
double[] B_h20={1.214557E01,-1.898065,2.911966E-01};
double[] C_nh3={-1.049377E-02,-8.288224,-6.647257E02,-3.045352E03};
double[] C_h20={2.136131E-02,-3.169291E01,-4.634611E04,0.0};
double[] D_nh3={3.673647,9.989629E-02,3.617622E-02};
double[] D_h20={4.019170,-5.175550E-02,1.951939E-02};
double hg_nh3=26.468879;
double hg_h20=60.965058;

double sgo_nh3=8.339026;
double sgo_h20=13.453430;

double ratio=(17*(x)+18*(1-x));
double sg_nh3=(-R/17)*(-sgo_nh3+D_nh3[0]+(D_nh3[1]*T)+(T*T*D_nh3[2])+(-D_nh3[0]*Math.log(T/T0_nh3)-D_nh3[0])+(-2*T*D_nh3[1]+D_nh3[1]*T0_nh3)-(T*D_nh3[2])+(Math.log(p/P0_nh3))+(C_nh3[1]*((-3*p/Math.pow(T,4))+(3*P0_nh3/(Math.pow(T0_nh3,4)))))+(C_nh3[2]*((-11*p/Math.pow(T,12))+(11*P0_nh3/Math.pow(T0_nh3,12))))+0.333*C_nh3[3]*((-11*p*p*p/Math.pow(T,12))+(11*P0_nh3*P0_nh3*P0_nh3/Math.pow(T0_nh3,12))));	
double sg_h20=(-R/18)*(-sgo_h20+D_h20[0]+(D_h20[1]*T)+(T*T*D_h20[2])+(-D_h20[0]*Math.log(T/T0_h20)-D_h20[0])+(-2*T*D_h20[1]+D_h20[1]*T0_h20)-(T*D_h20[2])+(Math.log(p/P0_h20))+(C_h20[1]*((-3*p/Math.pow(T,4))+(3*P0_h20/(Math.pow(T0_h20,4)))))+(C_h20[2]*((-11*p/Math.pow(T,12))+(11*P0_h20/Math.pow(T0_h20,12))))+0.333*C_h20[3]*((-11*p*p*p/Math.pow(T,12))+(11*P0_h20*P0_h20*P0_h20/Math.pow(T0_h20,12))));		
double smixx=(-R/ratio)*(x*Math.log(x)+(1-x)*Math.log(1-x));
return x*sg_nh3+(1-x)*sg_h20+smixx; 	
}
public double s_l(double pi,double Ti,double x) 
{
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;
double[] A_nh3={3.971423E-02,-1.790557E-05,-1.308905E-02,3.752836E-03};	
double[] A_h20={2.748796E-02,-1.016665E-05,-4.452025E-03,8.389246E-04};
double[] B_nh3={1.634519E01,-6.508119,1.448937};
double[] B_h20={1.214557E01,-1.898065,2.911966E-02};
double so_nh3=1.644773;
double so_h20=5.733498;
double s_ll_h20=(R/18)*(-so_h20+B_h20[0]+(B_h20[1]*T)+(B_h20[2]*T*T)-(B_h20[0]*Math.log(T/T0_h20)+B_h20[0])-(2*B_h20[1]*T)+(B_h20[1]*T0_h20)-(B_h20[2]*T)+(A_h20[2]+2*A_h20[3]*T)*(p-P0_h20));
double s_ll_nh3=(R/17)*(-so_nh3+B_nh3[0]+(B_nh3[1]*T)+(B_nh3[2]*T*T)-(B_nh3[0]*Math.log(T/T0_nh3)+B_nh3[0])-(2*B_nh3[1]*T)+(B_nh3[1]*T0_nh3)-(B_nh3[2]*T)+(A_nh3[2]+2*A_nh3[3]*T)*(p-P0_nh3));	
return x*s_ll_nh3+(1-x)*s_ll_h20+smix(x)+se(Ti,pi,x);	
}
public double se(double Ti,double pi,double x)
{
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;	
double E1=-41.733398;
double E2=0.02414;
double E3=6.702285;
double E4=-0.011475;
double E5=63.608967;
double E6=-62.490768;
double E7=1.761064;
double E8=0.008626;
double E9=0.387983;
double E10=0.004772;
double E11=-4.648107;
double E12=0.836376;
double E13=-3.553627;
double E14=0.000904;
double E15=24.361723;
double E16=-20.736547;			
double se=(-R/((17*x)+(18*(1-x))))*((E3+E4*p)-(E5/T/T)-(2*E6/T/T/T)+((E9+(E10*p)-(E11/T/T)-(2*E12/T/T/T))*(2*x-1))+(((-E15/T/T)-(2*E16/T/T/T))*(2*x-1)*(2*x-1)));
return se;
}
public double he(double Ti,double pi,double x)
{
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;	
double E1=-41.733398;
double E2=0.02414;
double E3=6.702285;
double E4=-0.011475;
double E5=63.608967;
double E6=-62.490768;
double E7=1.761064;
double E8=0.008626;
double E9=0.387983;
double E10=0.004772;
double E11=-4.648107;
double E12=0.836376;
double E13=-3.553627;
double E14=0.000904;
double E15=24.361723;
double E16=-20.736547;		
double val1=(-E4-(E8*p)-(2*E11/T)-(3*E12/T/T))*(2*x-1);
double val2=(-E13-(E14*p)-(2*E15/T)-(3*E16/T/T))*(2*x-1)*(2*x-1);
double h_e=(-R/((17*x)+(18*(1-x))))*TB*((-E1-(E2*p)-(2*E5/T)-(3*E6/T/T))+val1+val2)*(1-x);	
return h_e;
}
public double smix(double x)
{
double Ru=8.314;
double R=Ru/(17*x+18*(1-x));
double smixx=-R*(x*Math.log(x)+(1-x)*Math.log(1-x));
return smixx;	
}
public double se1(double Ti,double pi,double x)
{
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;	
double E1=-4.626129E01;
double E2=2.060225E-02;
double E3=7.292369;
double E4=-1.032613E-02;
double E5=8.074824E01;
double E6=-8.461214E01;
double E7=2.452882E01;
double E8=9.598767E-03;
double E9=-1.475383;
double E10=-5.038107E-03;
double E11=-9.640398E01;
double E12=1.226973E02;
double E13=-7.582637;
double E14=6.012445E-04;
double E15=5.487018E01;
double E16=-7.667596E01;		
double se11=-E3-(E4*p)+(E5/T/T)+(2*E6/T/T/T);
double se21=(2*x-1)*(-E9-(E10*p)+(E11/T/T)+(2*E12/T/T/T));
double se31=(2*x-1)*(2*x-1)*((E15/T/T)+(2*E16/T/T/T));
return se11+se21+se31;		
}
public double slson(double Ti,double pi,double x)
{
pi=pi/100;
double R=8.314;//kj/kmolK
double TB=100;//K	
double PB=10;//bar
double T0_nh3=3.2252;
double P0_nh3=2.0000;
double T0_h20=5.0705;
double P0_h20=3.0000;
double T=Ti/TB;
double p=pi/PB;
double[] A_nh3={3.971423E-02,-1.790557E-05,-1.308905E-02,3.752836E-03};	
double[] A_h20={2.748796E-02,-1.016665E-05,-4.452025E-03,8.389246E-04};
double[] B_nh3={1.634519E01,-6.508119,1.448937};
double[] B_h20={1.214557E01,-1.898065,2.911966E-01};
double so_1nh3=1.644773;
double so_1h20=5.733498;
double slh20=(R/18)*(1-x)*(so_1h20+(B_h20[0]*Math.log10(T/T0_h20))+(B_h20[1]*(T-T0_h20))+(0.5*B_h20[2]*(T*T-T0_h20*T0_h20))+((-A_h20[2]-2*A_h20[3]*T)*(p-P0_h20)));
double slnh3=(R/17)*(x)*(so_1nh3+(B_nh3[0]*Math.log10(T/T0_nh3))+(B_nh3[1]*(T-T0_nh3))+(0.5*B_nh3[2]*(T*T-T0_nh3*T0_nh3))+((-A_nh3[2]-2*A_nh3[3]*T)*(p-P0_nh3)));	
double smix=((R/(17*x+18*(1-x)))*(R/(17*x+18*(1-x)))*(((1-x)*Math.log(1-x))+(x*Math.log(x))))+((R/(17*x+18*(1-x)))*x*(1-x)*se1(Ti,pi,x));	
return slh20+slnh3-smix;
}
////Helmholtz free energy formulation of the mixture (water+ammonia)
///contributes ideal gas  properties and residual parts of the helmholtz free energy
public double[] ideal_gas_part1(double Tsol,double p,double x)
{
// Tsol in Kelvin
// p in kPa for water 
// steam and ammonia property 
//   s1[0]="P :";
//   s1[1]="T :";
//   s1[2]="v :";
//   s1[3]="h :";
//   s1[4]="u :";
//   s1[5]="s :";
//   s1[6]="x :";
//   s1[7]="ro:";  
double[] proper=st.property_SI("tp",Tsol-273,p/100.0);//in  C and bar	
double rowat1=proper[7];//kg/m3
double[] amn_prop2=amn.property_SI("tp",Tsol-273,p);
double roamn1=amn_prop2[7];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x))/(((1-x)*0.018015268)+(x*0.01703026));
double Tn=500.0;//K
double ron=15000;//mol/m3
double to0=Tn/Tsol;	
double delta0=ro/ron;	
System.out.println("ro="+ro+"ron="+ron);
double[] aa=new double[2];
aa[0]=to0;
aa[1]=delta0; 
return aa;
}


public double ideal_gas_part2(double Tsol,double p,double x)
{
double[] a0={-7.720435,8.649358,3.00632,0.012436,0.97315,1.27950,0.96956,0.24873,-16.444285,4.036946,-1.0,10.69955,-1.775436,0.82374034};
double[] teta={0.0,0.0,0.0,1.666,4.578,10.018,11.964,35.600};
double[] t={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1/3,-1.5,-1.75};	
		
double top1=0.0;
double aa[]=ideal_gas_part1(Tsol,p,x);
double to0=aa[0];
double delta0=aa[1];
for(int i=3;i<=7;i++)
{top1+=a0[i]*Math.log(1.0-Math.exp(-teta[i]*to0));}

double top2=0.0;	

for(int j=11;j<=13;j++)
{top2+=a0[j]*Math.pow(to0,t[j]);}

double ideal=Math.log(delta0)+ ((1-x)*(a0[0]+a0[1]*to0+ a0[2]*Math.log(to0) +Math.log(1-x)+top1))+( x*(a0[8]+(a0[9]*to0)+a0[10]*Math.log(to0)+Math.log(x)+top2));	
return ideal;	
}
public double[] residual_part1(double Tsol,double p,double x)
{
double[] amn_prop1=amn.property_SI("tp",Tsol-273,p);
double roamn=1/amn_prop1[2];	
double properties1[]=st.property_SI("tp",Tsol-273,p/100);
double rowat=1/properties1[2];
double ro=((1-x)*rowat)+((x)*roamn);//kg/m3
//water
double Tc01=647.096;//K
double rocrit1=322;//kg/m3
double M1=0.018015268;//kg/mol	
//ammonia
double Tc02=405.40;//K	
double rocrit2=225;//kg/m3	
double M2=0.01703026;//kg/mol	
//reducing functions
double kv=1.2395117;
double kt=0.9648407;
double alfa=1.125455;
double beta=0.8978069;
ro=ro/((M2*x)+(M1*(1-x)));//mol/m3
double Tc12=kt*0.5*(Tc01+Tc02); 	
double Tn1=((1-x)*(1-x)*Tc01)+(x*x*Tc02)+((2*x*(1-Math.pow(x,alfa)))*Tc12);
double rocrit1N=rocrit1/M1;//water -- molar based critical density 
double rocrit2N=rocrit2/M2;//ammonia -- molar based critical density 
double ro12=1/(kv*0.5*((1/rocrit1N)+(1/rocrit2N)));
double rox1=1/(((1-x)*(1-x)*(1/rocrit1N))+(x*x*(1/rocrit2N))+(2*x*(1-Math.pow(x,beta))*(1/ro12)));
double tox=Tn1/Tsol;
double deltax=ro/rox1;
double[] bb=new double[3];
bb[0]=tox;
bb[1]=deltax;
bb[2]=ro;
return bb;
}
public double[] residual_ammoniacoeff(double Tsol,double p)
{
//R. Tillner-Roth, F. Harms-Watzenberg, and H.D. Baehr, Eine neue
//Fundamentalgleichung für Ammoniak, Proc. 20th DKV-Tagung Heidelberg, Germany, Vol. II,
//167 (1993);
double[] amn_prop=amn.property_SI("tp",Tsol-273,p);
double ro=1/amn_prop[2];//kg/m3
double Tcnh3=405.40;//K	
double M2=0.01703026;//kg/mol
double rocritnh3=225/M2;//mol/m3	
ro/=M2;
double tonh3=Tcnh3/Tsol;
double deltanh3=ro/rocritnh3;
double[] cc =new double[2];
cc[0]=tonh3;
cc[1]=deltanh3;
return cc;
}
public double residual_ammonia(double deltanh3,double tonh3) 
{
double[] anh3={-1.8588140E+00,4.5544310E-02,7.2385480E-01,1.2294700E-02,2.1418820E-11,-1.4300200E-02,3.4413240E-01,-2.8735710E-01,2.3525890E-05,-3.4971110E-02,1.8311170E-03,2.3978520E-02,-4.0853750E-02,2.3792750E-01,-3.5489720E-02,-1.8237290E-01,2.2815560E-02,-6.6634440E-03,-8.8474860E-03,2.2726350E-03,-5.5886550E-04};
double[] tnh3={1.5,-0.5,0.5,1,3,0,3,4,4,5,5,3,6,8,8,10,10,5,7.5,15,30};
double[] dnh3={1,2,1,4,15,3,3,1,8,2,8,1,1,2,3,2,4,3,1,2,4};
double[] enh3={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3}; 	
double s1=0;	
double s2=0;		
for(int j=0;j<=4;j++)
{s1+=anh3[j]*Math.pow(tonh3,tnh3[j])*Math.pow(deltanh3,dnh3[j]);}	
for(int i=5;i<=20;i++)
{s2+=anh3[i]*Math.pow(tonh3,tnh3[i])*Math.pow(deltanh3,dnh3[i])*Math.exp(-Math.pow(deltanh3,enh3[i]));}
return s1+s2;	
}
public double[] residual_watercoeff(double Tsol,double p)
{//The IAPWS-95 formulation -- fundamental equation for the specific Helmholtz free energy
 // residual part of helmholtz free energy
double properties[]=st.property_SI("tp",Tsol-273,p/100);
double ro=1/properties[2];//kg/m3
double Tc01=647.096;//K
double rocrit1=322;//kg/m3
double M1=0.018015268;//kg/mol	
double towat=Tc01/Tsol;
double deltawat=ro/(rocrit1);
double[] rr=new double[3];
rr[0]=towat;
rr[1]=deltawat;
rr[2]=ro;
return rr;
}
public double residual_water(double deltawat,double towat)
{
double[] n={0.12533547935523E-01,0.78957634722828E+01,-0.87803203303561E+01,0.31802509345418,-0.26145533859358,-0.78199751687981E-02,0.88089493102134E-02,-0.66856572307965,0.20433810950965,-0.66212605039687E-04,-0.19232721156002,-0.25709043003438,0.16074868486251,-0.40092828925807E-01,0.39343422603254E-06,-0.75941377088144E-05,0.56250979351888E-03,-0.15608652257135E-04,0.11537996422951E-08,0.36582165144204E-06,-0.13251180074668E-11,-0.62639586912454E-09,-0.10793600908932,0.17611491008752E-01,0.22132295167546,-0.40247669763528,0.58083399985759,0.49969146990806E-02,-0.31358700712549E-01,-0.74315929710341,0.47807329915480,0.20527940895948E-01,-0.13636435110343,0.14180634400617E-01,0.83326504880713E-02,-0.29052336009585E-01,0.38615085574206E-01,-0.20393486513704E-01,-0.16554050063734E-02,0.19955571979541E-02,0.15870308324157E-03,-0.16388568342530E-04,0.43613615723811E-01,0.34994005463765E-01,-0.76788197844621E-01,0.22446277332006E-01,-0.62689710414685E-04,-0.55711118565645E-09,-0.19905718354408,0.31777497330738,-0.11841182425981,-0.31306260323435E+02,0.31546140237781E+02,-0.25213154341695E+04,-0.14874640856724,0.31806110878444};
double[] c={0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,6,6,6,6,0,0,0};	
double[] d={1,1,1,2,2,3,4,1,1,1,2,2,3,4,4,5,7,9,10,11,13,15,1,2,2,2,3,4,4,4,5,6,6,7,9,9,9,9,9,10,10,12,3,4,4,5,14,3,6,6,6,3,3,3};
double[] tt={-0.5,0.875,1,0.5,0.75,0.375,1,4,6,12,1,5,4,2,13,9,3,4,11,4,13,1,7,1,9,10,10,3,7,10,10,6,10,10,1,2,3,4,8,6,9,8,16,22,23,23,10,50,44,46,50,0,1,4};
double[] alfa={20,20,20,3.5,3.5};
double[] beta={150,150,150,0.3,0.3};
double[] gamma={1.21,1.21,1.25};
double[] eps={1,1,1};

double[] b={0.85,0.95};
double[] B={0.2,0.2};	
double[] C={28,32};
double[] D={700,800};
double[] A={0.32,0.32};	
	
double ss1=0.0;
double ss2=0.0;	
double ss3=0.0;
double ss4=0.0;

for(int i=0;i<=6;i++)
{ss1+=n[i]*Math.pow(deltawat,d[i])*Math.pow(towat,tt[i]);}

for(int j=7;j<=50;j++)	
{ss2+=n[j]*Math.pow(deltawat,d[j])*Math.pow(towat,tt[j])*Math.exp(-Math.pow(deltawat,c[j]));}

for(int k=51;k<=53;k++)
{ss3+=n[k]*Math.pow(deltawat,d[k])*Math.pow(towat,tt[k])*Math.exp((-alfa[k-51]*(deltawat-eps[k-51])*(deltawat-eps[k-51]))+(-beta[k-51]*(towat-gamma[k-51])*(towat-gamma[k-51])));}	
	
for(int l=54;l<=55;l++)
{ss4+=n[l]*Math.pow(((((1-towat)+(A[l-54]*(Math.pow((deltawat-1)*(deltawat-1),1/(2*beta[l-51])))))*((1-towat)+(A[l-54]*(Math.pow((deltawat-1)*(deltawat-1),1/(2*beta[l-51]))))))+(B[l-54]*Math.pow(((deltawat-1)*(deltawat-1)),alfa[l-51]))),b[l-54])*deltawat*Math.exp((-C[l-54]*(deltawat-1)*(deltawat-1))+(-D[l-54]*(towat-1)*(towat-1)));    }
	
return ss1+ss2+ss3+ss4;	
}
public double residual_part2(double deltax,double rox,double x)
{
// departure functions
double gamma=0.5248379;
double[] a1={-1.855822E-02,5.258010E-02,3.552874E-10,5.451379E-06,-5.998546E-13,-3.687808E-06,0.2586192,-1.368072E-08,1.226146E-02,-7.181443E-02,9.970849E-02,1.0584086E-03,-0.1963687,-0.7777897}; 	
double[] t1={1.5,0.5,6.5,1.75,15,6,-1,4,3.5,0,-1,8,7.5,4}; 	
double[] d1={4,5,15,12,12,15,4,15,4,5,6,10,6,2};
double[] e1={0,1,1,1,1,2,1,1,1,1,2,2,2,2};		
double sum1=0;
double sum2=0;	
for(int i=1;i<=5;i++)
{sum1+=a1[i]*exp(-pow(deltax,e1[i]))*pow(rox,t1[i])*pow(deltax,d1[i]);}
for(int j=6;j<=12;j++)
{sum2+=a1[j]*exp(-pow(deltax,e1[j]))*pow(rox,t1[j])*pow(deltax,d1[j]);}
double sum3=(a1[0]*pow(rox,t1[0])*pow(deltax,d1[0]))+sum1+(x*sum2)+(a1[13]*x*x*exp(-pow(deltax,e1[13]))*pow(rox,t1[13])*pow(deltax,d1[13]));
return (x*(1-pow(x,gamma)))*sum3;
}
public double dQ0dr0(double Tsol,double p,double x)
{
double dx=0.0001;
double[] coeff=ideal_gas_part1(Tsol,p,x);
double ro0=coeff[0];
double delta0=coeff[1];
double fx=ideal_gas_part2(delta0,ro0,x);	
double fxdx=ideal_gas_part2(delta0,ro0+dx,x);		
return (fxdx-fx)/dx;	
}
public double dQ02dr02(double Tsol,double p,double x)
{
double dr=0.0001;
double[] coeff1=ideal_gas_part1(Tsol,p,x);
double ro0=coeff1[0];
double delta0=coeff1[1];	
double fx0=ideal_gas_part2(delta0,ro0,x);	
double fxrr=ideal_gas_part2(delta0,ro0+dr,x);
double fxr=ideal_gas_part2(delta0,ro0-dr,x);	
return (fxrr-(2*fx0)+fxr)/dr/dr;
}
public double[] dQ(double Tsol,double p,double x)
{// Q ya göre türev alır
double dr=0.0001;
double[] res1=residual_part1(Tsol,p,x);
double to=res1[0]; 
double delta=res1[1];
double[] watercoeff=residual_watercoeff(Tsol,p);
double towat=watercoeff[0];	
double deltawat=watercoeff[1];
double[] amncoeff=residual_ammoniacoeff(Tsol,p);
double tonh3=amncoeff[0];
double deltanh3=amncoeff[1];
double fresnh3=x*residual_ammonia(deltanh3,tonh3);//
double fres=residual_part2(delta,to,x);	//
double freswat=(1-x)*residual_water(deltawat,towat);//
double dfrestoback=residual_part2(delta,to-dr,x);
double dfrestofor=residual_part2(delta,to+dr,x);
double dfresdeltafor=residual_part2(delta+dr,to,x);
double dfresdeltaback=residual_part2(delta-dr,to,x);
double dfresxfor=residual_part2(delta,to,x+dr);
double dfreswattoback=(1-x)*residual_water(deltawat,towat-dr);
double dfreswattofor=(1-x)*residual_water(deltawat,towat+dr);
double dfreswatdeltafor=(1-x)*residual_water(deltawat+dr,towat);
double dfreswatdeltaback=(1-x)*residual_water(deltawat-dr,towat);
double dfreswatxfor=-residual_water(deltawat,towat);
double dfresnh3toback=x*residual_ammonia(deltanh3,tonh3-dr);
double dfresnh3tofor=x*residual_ammonia(deltanh3,tonh3+dr);
double dfresnh3deltafor=x*residual_ammonia(deltanh3+dr,tonh3);
double dfresnh3deltaback=x*residual_ammonia(deltanh3-dr,tonh3);
double dfresnh3xfor=residual_ammonia(deltanh3,tonh3);
double dtoptofor=dfrestofor+dfreswattofor+dfresnh3tofor;
double dtopdeltafor=dfresnh3deltafor+dfreswatdeltafor+dfresdeltafor;
double dtoptoback=dfrestoback+dfreswattoback+dfresnh3toback;
double dtopdeltaback=dfresdeltaback+dfreswatdeltaback+dfresnh3deltaback;
double dtopxfor=dfresxfor+dfreswatxfor+dfresnh3xfor;
double top=fres+freswat+fresnh3;
double[] Q=new double[14];
Q[0]=(dtoptofor-top)/dr;//dQr/dr
Q[1]=(dtopdeltafor-top)/dr;//dQr/d(delta)
Q[2]=(dtoptofor-(2*top)+dtoptoback)/dr/dr;//dQr2/d(to)2 
Q[3]=Q[0]*Q[1];//dQr2/(d(delta)*d(to));
Q[4]=(dtopdeltafor-(2*top)+dtopdeltaback)/dr/dr;//dQr2/d(delta)2
Q[5]=(dtopxfor-top)/dr;//dQr/dx
Q[6]=0;
Q[7]=0;
Q[8]=to;
Q[9]=delta;
Q[10]=towat;
Q[11]=deltawat;
Q[12]=tonh3;
Q[13]=deltanh3;
return Q;
}
double enthalpy(double Tsol,double p,double x)
{
double Rm=8.314472;//J/molK	

double[] part1=ideal_gas_part1(Tsol,p,x);
double to0=part1[0];
double delta0=part1[1];	
double[] res1=residual_part1(Tsol,p,x);
double to=res1[0]; 
double delta=res1[1];
double[] watercoeff=residual_watercoeff(Tsol,p);
double towat=watercoeff[0];	
double deltawat=watercoeff[1];
double[] amncoeff=residual_ammoniacoeff(Tsol,p);
double tonh3=amncoeff[0];
double deltanh3=amncoeff[1];	
double del=delta+deltawat+deltanh3;
double t=to+towat+tonh3;
double[] dq=dQ(Tsol,p,x);
double dqddelta=dq[1];
double dqdto=dq[0]; 
double dq0dr0=dQ0dr0(Tsol,p,x);
double h=((1+(del*dqddelta)+(to0*dq0dr0)+(t*dqdto))*Rm*Tsol);///(0.018*x+0.017*(1-x));
return h;
}
public String toStringl(double Tsol,double p,double x)
   {// p in kPa 
    // Tsol in Kelvin
    // x in mole fraction
   
       String s3="";
       s3+="Gaz faz denge sıcaklığı "+T_g( p,x)+" K \n"; 
       s3+="Sıvı faz denge sıcaklığı "+T_l( p,x)+" K \n";   
	   s3+="Sıvı faz mol oranı "+x+" iken kütle oranı "+mole_to_mass(x)+" \n";
	   s3+="Basınç "+p+" kPa ve sıvı faz mol oranı "+x+" iken gaz faz mol oranı "+y_Px(p,x) +"\n";
	   s3+="Karışım sıcaklığı "+Tsol+" K ve sıvı faz mol oranı "+x+" iken  doyma basıncı "+P(Tsol,x)+" kPa \n";
	   s3+="Karışım sıvı faz entalpisi "+hlnew(Tsol,p,x)+ " kJ/kg \n";
	   s3+="Karışımın kritik sıcaklığı "+T_crit_sol(x)+" K \n";
	   s3+="Karışımın kritik basıncı " + P_crit_sol(x)+" kPa \n";
	   s3+="Karışım sıvı faz ısı kapasitesi " + Cp_sol(Tsol,x) +" kJ/kgK \n";
	   s3+="Karışımın sıvı faz ısı iletim katsayısı "+k_sol(Tsol,x)+" W/mK \n ";  
	   s3+="Karışımın sıvı faz yüzey tansiyonu " +surftens(Tsol,x)+" N/m  \n";
       s3+="Karışımın sıvı faz yoğunluğu "  +ro_sol(Tsol,x)+" kg/m3 \n";
       s3+="Karışımın sıvı faz entropisi " +slson(Tsol,p,x)+" kJ/kgK \n";
        
	   return s3;
	  }

     public void yazdırl(double Tsol,double p,double x)
     {
	     String s4=toStringl( Tsol,p,x);
	     JOptionPane.showMessageDialog(null,s4);
	     System.out.println(s4);  
	     }
     
	  public String toStringv(double Tsol,double p,double x)
	  {
		 // p in kPa 
         // Tsol in Kelvin
         // x in mole fraction 		  
		String s5="";
		s5+="Liquid phase equilibrium temperature "+T_l( p,x)+" K \n"; 
		s5+="Gas phase equilibrium temperature "+T_g( p,x)+" K \n"; 
		s5+="Pressure "+p+" kPa and liquid phase mole ratio "+x+" gas pahse mole ratio "+y_Px(p,x) +"\n"; 
		s5+="mixture gas phase temperature "+Tsol+" K and liquid phase mole ratio "+x+" iken  doyma basıncı "+P(Tsol,x)+" kPa \n"; 
		s5+="mixture gas phase critical temperature "+T_crit_sol(x)+" K \n";
	    s5+="mixture gas phase critical pressure " + P_crit_sol(x)+" kPa \n";  
		s5+="mixture gas phase enthaply "+h_g_t_p(Tsol,p,x)+ " kJ/kg \n";  
	    s5+="mixture gas phase thermal conductivity "+kv_sol(Tsol,x)+" W/mK \n";
        
        s5+="mixture gas pahse density " +rov_sol(Tsol, x)+" kg/m3 \n";
        s5+="mixture gas pahse specific heat " +sol_Cpv( Tsol,x)+" kJ/kgK \n";
        s5+="mixture gas phase entropy " +sg(Tsol,p,x) + "kJ/kgK \n";
		
        return s5;
        }   
      
 public void yazdırv(double Tsol,double p,double x)
         {
	     String s6=toStringv( Tsol,p,x);
	     JOptionPane.showMessageDialog(null,s6);
	     System.out.println(s6);  
	     }


 public String toStringlv(double Tsol,double p,double x)
   	{
	   String s7="";   	
	   s7+="Gas phase equilibrium temperature "+T_g( p,x)+" K \n"; 	   
	   s7+="Liquid pahse equilibrium temperature "+T_l( p,x)+" K \n"; 
	   s7+="temperature of the mixture "+Tsol+"K \n";
	   s7+="Critical temperature of the mixture "+T_crit_sol(x)+" K \n";
	   s7+="Critical pressure of the mixture " + P_crit_sol(x)+" kPa \n";  	
       s7+="mixture enthalpy " + hlv(Tsol,p,x)+" kJ/kg \n";
       s7+="mixture entropy " + slv(Tsol,p,x)+" kJ/kg \n";
       s7+="mixture specific heat "+Cplv(Tsol,p,x)+" kJ/kgK \n";
       s7+="mixture thermal conductivity coefficient " +klv(Tsol,p,x)+" W/mK \n";
       s7+="mixture density "+ro_lv(Tsol,p,x)+ " kg/m3 \n";
       return s7;	   	
	}     	     
	public void yazdırlv(double Tsol,double p,double x)
	{
		String s8=toStringlv( Tsol,p,x);
	     JOptionPane.showMessageDialog(null,s8);
	     System.out.println(s8);  
	}	
 public double[] properties_liquid(double Tsol,double p,double x)
 {
   	 double[] propliq=new double[12];
   	 propliq[0]=T_l(p,x);
   	 propliq[1]=mole_to_mass(x);                  
   	 propliq[2]=y_Px(p,x);
   	 propliq[3]=P(Tsol,x);
   	 propliq[4]=hlnew(Tsol,p,x);
   	 propliq[5]=T_crit_sol(x);
   	 propliq[6]=P_crit_sol(x);
   	 propliq[7]=Cp_sol(Tsol,x);
	 propliq[8]=k_sol(Tsol,x);
	 propliq[9]=surftens(Tsol,x);
	 propliq[10]=ro_sol(Tsol,x);
	 propliq[11]=slson(Tsol,p,x);	 
	 return propliq;
}   
public double[] properties_vap(double Tsol,double p,double x)
{	double[] propvap=new double[10];
	propvap[0]=T_g(p,x);
	propvap[1]=y_Px(p,x);
	propvap[2]=P(Tsol,x);
	propvap[3]=T_crit_sol(x);
	propvap[4]=P_crit_sol(x);
	propvap[5]=h_g_t_p(Tsol,p,x);
	propvap[6]=kv_sol(Tsol,x); 
	propvap[7]=rov_sol(Tsol, x);
	propvap[8]=sol_Cpv( Tsol,x);
	propvap[9]=sg(Tsol,p,x);	
	return propvap;
}
public double[] properties_lv(double Tsol,double p,double x)
{
	double[] proplv=new double[5];
	proplv[0]=hlv(Tsol,p,x);
	proplv[1]=slv(Tsol,p,x);
	proplv[2]=Cplv(Tsol,p,x);
	proplv[3]=klv(Tsol,p,x);
	proplv[4]=ro_lv(Tsol,p,x);
	return proplv;
}    
	public double[] propertymix(double Tinit,double p ,double x)
	{
	   double Tsatl=T_l(p,x);
       double Tsatg=T_g(p,x); 		   
	   double pr[]=new double[2];	
	   if(Tinit<=Tsatl)
	  { 
		double prl[]=properties_liquid(Tinit,p,x);
		 pr[0]=prl[4];
	     pr[1]=prl[11];           	  
	  }	   
	   else if((Tinit>Tsatl)&&(Tinit<Tsatg))
	   {
		   double prlv[]=properties_lv(Tinit,p,x);
		   pr[0]=prlv[0];
		   pr[1]=prlv[1];		   
	   }
	   else if(Tinit>=Tsatg)
	   {
		   double prv[]=properties_vap(Tinit,p,x);
		   pr[0]=prv[5];
		   pr[1]=prv[9];		   
	   }				
		return pr;
	}         
}






 