//====================================================== 
// Soğutucu akışkanlar termodinamik özellikleri
// ISO17584 Hal denklemi
// Dr. Turhan Coban
// EGE Üniversitesi Mühendislik Fakultesi, Makina Bölümü
// turhan.coban@ege.edu.tr
// düzeltme ve eklemeler : Halil Atalay
// EGE Üniversitesi Mühendislik Fakultesi, Makina Bölümü
// halilatalay16@gmail.com
// =====================================================
import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class refISO17584_mix extends refISO17584
{
ref_CS3 r;
refISO17584MixData rd;
public String isimler[]={"R404A","R407C","R410A","R507A"};

public refISO17584_mix()
{
 r=new ref_CS3("R404A");
 readref("R404A");
}

public refISO17584_mix(String rName)
{ r=new ref_CS3(rName);
  readref(rName);
}

public double to_yildiz()
{
double to1=0;
int n=rd.x.length;
for(int i=0;i<n;i++)
{to1+=rd.x[i]*rd.refcd[i].rd.Ts;
//System.out.println("n="+n+"i="+i+"rd.refcd[i].rd.Ts="+rd.refcd[i].rd.Ts);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
to1+=rd.x[i]*rd.x[j]*rd.fi19[i][j];
//System.out.println("to1="+to1+"i="+i+"j="+j+"rd.x[i]="+rd.x[i]+"rd.x[j]="+rd.x[j]+"rd.fi19[i][j]="+rd.fi19[i][j]);
}
}
return to1;
}
public double ro_yildiz()
{
double Vs=1.0/rd.ros;
double delta1=0;
int n=rd.x.length;
for(int i=0;i<n;i++)
{delta1=rd.x[i]/rd.refcd[i].rd.ros;
//System.out.println("i="+i+"rd.refcd[i].rd.ros="+rd.refcd[i].rd.ros);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
delta1+=rd.x[i]*rd.x[j]*rd.fi20[i][j];
//System.out.println("delta1="+delta1+"i="+i+"j="+j+"rd.x[i]="+rd.x[i]+"rd.x[j]="+rd.x[j]+"rd.fi20[i][j]="+rd.fi20[i][j]);

}
}
return 1.0/delta1;
}

public int ik(int i,int j)
{
//if(i==0 && j==1) return 0;
if(i==0 && j==2) return 1;
else if(i==1 && j==2) return 2; 
else return 0;
}

public double fi_excess(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1.0/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double lk=0,dk=0,tk=0,nk=0;
if(i==j) return 0;
else
{
int n=rd.N_mix[0].length;
for(int k=0;k<n;k++)
{
 lk=rd.l_mix[ik(i,j)][k];
 dk=rd.d_mix[ik(i,j)][k];
 tk=rd.t_mix[ik(i,j)][k];
 nk=rd.N_mix[ik(i,j)][k];
 double  delta_lk=Math.pow(delta,lk);
 fi+=nk*Math.pow(delta,dk)*Math.pow(to,tk)*Math.exp(-delta_lk);
//System.out.println("k="+k+"fi="+fi+"nk="+nk+"dk="+dk+"tk="+tk+"lk="+lk+"delta_lk="+delta_lk+"Math.exp(-delta_lk)="+Math.exp(-delta_lk));
}
 fi*=rd.F[i][j];
//System.out.println("i="+i+"j="+j+"rd.F[i][j]="+rd.F[i][j]);
 return fi;
}
}

public double dfi_excess_ddelta(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1.0/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double lk=0,dk=0,tk=0,nk=0;
int n=rd.N_mix[0].length;
if(i==j) return 0;
else
{
for(int k=0;k<n;k++)
{ 
  lk=rd.l_mix[ik(i,j)][k];
  dk=rd.d_mix[ik(i,j)][k];
  tk=rd.t_mix[ik(i,j)][k];
  nk=rd.N_mix[ik(i,j)][k];
  double delta_lk=Math.pow(delta,lk);
  fi+=nk*Math.pow(to,tk)*Math.pow(delta,dk)*Math.exp(-delta_lk)*(dk-lk*delta_lk);
//System.out.println("n="+n+"k="+k+"fi="+fi+"nk="+nk+"dk="+dk+"tk="+tk+"lk="+lk);

  }
fi*=rd.F[i][j];
//System.out.println("i="+i+"j="+j+"rd.F[i][j]="+rd.F[i][j]);

return fi;
}
}

public double d2fi_excess_ddelta2(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.N_mix[0].length;
double delta_lk=0,lk=0,dk=0,tk=0,nk=0;
if(i==j) return 0;
else
for(int k=0;k<n;k++)
{
 lk=rd.l_mix[ik(i,j)][k];
 delta_lk=Math.pow(delta,lk);
 dk=rd.d_mix[ik(i,j)][k];
 tk=rd.t_mix[ik(i,j)][k];
 nk=rd.N_mix[ik(i,j)][k];
 fi+=nk*Math.pow(to,tk)*Math.pow(delta,dk)*Math.exp(-delta_lk)*
(delta_lk*(lk*lk*(delta_lk-1.0)-lk*(2.0*dk-1.0))+dk*(dk-1.0));
}
fi*=rd.F[i][j];
return fi;
}


public double dfi_excess_dto(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.N_mix[0].length;
double lk=0,dk=0,tk=0,nk=0;
if(i==j) return 0;
else
{
for(int k=0;k<n;k++)
{
 lk=rd.l_mix[ik(i,j)][k];
 dk=rd.d_mix[ik(i,j)][k];
 tk=rd.t_mix[ik(i,j)][k];
 nk=rd.N_mix[ik(i,j)][k];
 double  delta_lk=Math.pow(delta,lk);
 fi+=nk*Math.pow(to,tk)*Math.pow(delta,dk)*Math.exp(-delta_lk)*tk;
//System.out.println("n="+n+"k="+k+"nk="+nk+"tk="+tk+"dk="+dk+"lk="+lk);
}
fi*=rd.F[i][j];
//System.out.println("i="+i+"j="+j+"rd.F[i][j]="+rd.F[i][j]);
}
return fi;
}

public double d2fi_excess_dto2(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.N_mix[0].length;
double delta_lk=0,lk=0,dk=0,tk=0,nk=0;
if(i==j) return 0;
else
{for(int k=0;k<n;k++)
{lk=rd.l_mix[ik(i,j)][k];
 delta_lk=Math.pow(delta,lk);
 dk=rd.d_mix[ik(i,j)][k];
 tk=rd.t_mix[ik(i,j)][k];
 nk=rd.N_mix[ik(i,j)][k];
{fi+=nk*Math.pow(to,tk)*Math.pow(delta,dk)*Math.exp(-delta_lk)*(tk*(tk-1.0));}
fi*=rd.F[i][j];
}
return fi;
}
}

public double d2fi_excess_dto_ddelta(int i, int j,double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.N_mix[0].length;
double delta_lk=0,lk=0,dk=0,tk=0,nk=0;
if(i==j) return 0;
else
{for(int k=0;k<n;k++)
{lk=rd.l_mix[ik(i,j)][k];
 delta_lk=Math.pow(delta,lk);
 dk=rd.d_mix[ik(i,j)][k];
 tk=rd.t_mix[ik(i,j)][k];
 nk=rd.N_mix[ik(i,j)][k];
 fi+=nk*Math.pow(to,tk)*Math.pow(delta,dk)*Math.exp(-delta_lk)*(tk*(tk-lk*delta_lk));}
fi*=rd.F[i][j];
return fi;
}
}

public double fi_mix_r(double t,double v)
{
double fi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.x.length;
for(int i=0;i<n;i++)
{
fi+=rd.x[i]*rd.refcd[i].fi_r(t,v);
//System.out.println("n="+n+"i="+i+"rd.x[i]="+rd.x[i]+"rd.refcd[i].fi_r(t,v)="+rd.refcd[i].fi_r(t,v)+"to="+to+"delta="+delta);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
fi+=rd.x[i]*rd.x[j]*fi_excess(i,j,t,v);
//System.out.println("i="+i+"j="+j+"rd.x[i]="+rd.x[i]+"rd.x[j]="+rd.x[j]+"fi_excess(i,j,t,v)="+fi_excess(i,j,t,v));
}
}
return fi;
}


public double dfi_r_mix_ddelta(double t,double v)
{
	int ik=0;
double dfi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1.0/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.x.length;
for(int i=0;i<n;i++)
{dfi+=rd.x[i]*delta*rd.refcd[i].dfi_r_ddelta(t,v);}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{ dfi+=rd.x[i]*rd.x[j]*dfi_excess_ddelta(i,j,t,v);
//System.out.println("n="+n+"i="+i+"j="+j+"dfi/delta="+dfi/delta);
}
}
return dfi/delta;
}

public double dfi_r_mix_dto(double t,double v)
{
	int ik=0;
double dfi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1.0/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.x.length;
for(int i=0;i<n;i++)
{dfi+=rd.x[i]*to*rd.refcd[i].dfi_r_dto(t,v);
//System.out.println("n="+n+"i="+i+"rd.x[i]="+rd.x[i]+"to="+to+"rd.refcd[i].dfi_r_dto(t,v)="+rd.refcd[i].dfi_r_dto(t,v));
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{ dfi+=rd.x[i]*rd.x[j]*dfi_excess_dto(i,j,t,v);
//System.out.println("dfi/to="+dfi/to);
}
}
return dfi/to;
}

public double dfi_r2_mix_ddelta2(double t,double v)
{
int ik=0;
double dfi=0;
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1.0/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
int n=rd.x.length;
for(int i=0;i<n;i++)
{dfi+=rd.x[i]*delta*delta*dfi_r2_ddelta2(t,v);}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
dfi+=rd.x[i]*rd.x[j]*d2fi_excess_ddelta2(i,j,t,v);
}
}
return dfi/(delta*delta);
}

public double fi_mix_id(double t,double v)
{
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double Vs=1.0/rd.ros;
double ro=1/V;
double delta=ro/ro_yildiz();
double fi=0;
double x1,x2;
double s;
int n=rd.x.length;
for(int i=0;i<n;i++)
{fi+=rd.x[i]*rd.refcd[i].fi_id1(t,v)+rd.x[i]*Math.log(rd.x[i]);
//System.out.println("n="+n+"i="+i+"rd.x[i]="+rd.x[i]+"Math.log(rd.x[i])="+Math.log(rd.x[i]));
}
fi+=rd.f3+rd.f4/T;
//System.out.println("fi="+fi+"rd.f3="+rd.f3+"rd.f4="+rd.f4);
return fi;
}


public double dfi_id_mix_dto(double t,double v)
{
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double Vs=1.0/rd.ros;
double ro=1/V;
double delta=ro/ro_yildiz();
double dfi=0;
int n=rd.x.length;
for(int i=0;i<n;i++)
{dfi+=rd.x[i]*to*rd.refcd[i].dfi_id_dto(t,v);}
dfi+=rd.f4/T;
//System.out.println("dfi/to="+dfi/to+);
return dfi/to;
}

public double P(double t,double v)
{
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double R=rd.R;
double p=R*T*ro*(1.0+delta*dfi_r_mix_ddelta(t,v));
return p;
}
double dPdv(double t, double v)
{
//derivative of pressure dp(t,v)/dv
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
return rd.R*T*(1.0+2.0*ro*fi_mix_r(t,v)+4.0*ro*ro*dfi_r_mix_ddelta(t,v)+
ro*ro*ro*dfi_r2_mix_ddelta2(t,v))*(-1/(v*v)*1e-3);
}
double u(double t,double v)
{  //internal energy
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double R=rd.R;
double uu= R*T*(to*dfi_id_mix_dto(t,v)+to*dfi_r_mix_dto(t,v));
//System.out.println("uu/rd.M="+uu/rd.M+"R="+R+"dfi-id_mix_dto(t,v)="+dfi_id_mix_dto(t,v)+"dfi_r_mix_dto(t,v)="+dfi_r_mix_dto(t,v)+"rd.M="+rd.M);
return uu/rd.M;
}
double h(double t,double v)
{ //enthalpy
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double R=rd.R;
double hh= R*T*(1.0+to*dfi_id_mix_dto(t,v)+to*dfi_r_mix_dto(t,v)+delta*dfi_r_mix_ddelta(t,v));
//System.out.println("R="+R+"T="+T+"to="+to+"delta="+delta+"rd.M="+rd.M+"to*dfi_id_mix_dto(t,v)="+to*dfi_id_mix_dto(t,v)+"to*dfi_r_mix_dto(t,v)="+to*dfi_r_mix_dto(t,v)+"delta*dfi_r_mix_ddelta(t,v)="+delta*dfi_r_mix_ddelta(t,v));
return hh/rd.M;
}
double s(double t,double v)
{ // entropy
double T=t+273.15;  //K
double to=to_yildiz()/T;
double V=v*rd.M;  //m^3/kmol
double ro=1/V;
double Vs=1.0/rd.ros;
double delta=ro/ro_yildiz();
double R=rd.R;
double ss= R*(-(fi_mix_id(t,v)+fi_mix_r(t,v))+to*dfi_id_mix_dto(t,v)+to*dfi_r_mix_dto(t,v));
return ss/rd.M;
}
public boolean readref(String rName)
{  boolean b2=false;
 if(rName.equals("R404A"))
 {    
 String rcName[]={"R125","R143a","R134a"};
 double rxi[]={0.35781678,0.60391922,0.038264};
 double Mi=97.604;
 double Ni[][]={{-0.013073,0.018259,0.0000081299,0.0078496},
 {-0.013073,0.018259,0.0000081299,0.0078496},
 {-0.013073,0.018259,0.0000081299,0.0078496}};
 double tki[][]={{7.4,0.35,10.0,5.3},{7.4,0.35,10.0,5.3},{7.4,0.35,10.0,5.3}};
 double dki[][]={{1.0,3.0,11.0,2.0},{1.0,3.0,11.0,2.0},{1.0,3.0,11.0,2.0}};
 double lki[][]={{1.0,1.0,2.0,3.0},{1.0,1.0,2.0,3.0},{1.0,1.0,2.0,3.0}};
 double fi19i[][]={{0.0,5.551,-0.4326},{5.551,0.0,2.324},{-0.4326,2.324,0.0}};
 double fi20i[][]={{0.0,-0.0004452,-0.0003453},{-0.0004452,0.0,0.0006182},{-0.0003453,0.0006182,0.0}};
 double Fi[][]={{0.0,1.1697,1.0},{1.1697,0.0,0.5557},{1.0,0.5557,0.0}};
 double f3i=0.753387285;
 double f4i=17.4959977;
 //refISO17584MixData(String refrigerantNamei,String refcNamei[],double Fi[][],double fi19i[],double fi20i[],double xi[],double f3i,double f4i,double Mi)
 rd=new refISO17584MixData("R404A",rcName, Fi,fi19i,fi20i,rxi,f3i,f4i,Mi);
 rd.input_mix(Ni,tki,dki,lki,rxi);
 b2=true;        	 
}
if(rName.equals("R407C"))
 {    
 String rcName[]={"R32","R125","R134a"};
 double rxi[]={0.38110942,0.17955889,0.43933169};
 double Mi=86.2037;
 double Ni[][]={{-0.0072955,0.078035,0.61007,0.64246,0.014965,-0.34049,0.085658,-0.064429},
                 {0.22909,0.094074,0.00039876,0.021133,0.0,0.0,0.0,0.0},
                 {-0.013073,0.018259,0.0000081299,0.0078496,0.0,0.0,0.0,0.0}};
 double tki[][]={{4.50,0.57,1.90,1.20,0.50,2.60,11.40,4.50},
               {1.9,0.25,0.07,2.0,0.0,0.0,0.0,0.0},
               {7.4,0.35,10.0,5.3,0.0,0.0,0.0,0.0}};
 double dki[][]={{2.0,5.0,1.0,3.0,9.0,2.0,3.0,6.0},
               {1,3,8,1,0,0,0,0},
               {1,3,11,2,0,0,0,0}};
 double lki[][]={{1.0,1.0,2.0,2.0,2.0,3.0,3.0,3.0},
               {1,1,1,2,0,0,0,0},
               {1,1,2,3,0,0,0,0}};
 double fi19i[][]={{0.0,28.95,7.909},{28.95,0.0,-0.4326},{7.909,-0.4326,0.0}};
 double fi20i[][]={{0.0,-0.006008,-0.002039},{-0.006008,0.0,-0.0003453},{-0.002039,-0.0003453,0.0}};
 double Fi[][]={{0.0,1.0,1.0},{1.0,0.0,1.0},{1.0,1.0,0.0}};
 double f3i=1.04370879;
 double f4i=-8.74106803;
 //refISO17584MixData(String refrigerantNamei,String refcNamei[],double Fi[][],double fi19i[],double fi20i[],double xi[],double f3i,double f4i,double Mi)
 rd=new refISO17584MixData("R407C",rcName, Fi,fi19i,fi20i,rxi,f3i,f4i,Mi);
 rd.input_mix(Ni,tki,dki,lki,rxi);
 b2=true;        	 
}
 if(rName.equals("R410A"))
 {    
 String rcName[]={"R32","R125"};
 double rxi[]={0.69761470,0.30238530};
 double Mi=72.5855;
 double Ni[][]={{-0.0072955,0.078035,0.61007,0.64246,0.014965,-0.34049,0.085658,-0.064429},
                {-0.0072955,0.078035,0.61007,0.64246,0.014965,-0.34049,0.085658,-0.064429},
                {-0.0072955,0.078035,0.61007,0.64246,0.014965,-0.34049,0.085658,-0.064429}};
 double tki[][]={{4.50,0.57,1.90,1.20,0.50,2.60,11.40,4.50},
                 {4.50,0.57,1.90,1.20,0.50,2.60,11.40,4.50},
                 {4.50,0.57,1.90,1.20,0.50,2.60,11.40,4.50}};
 double dki[][]={{2.0,5.0,1.0,3.0,9.0,2.0,3.0,6.0},
                 {2.0,5.0,1.0,3.0,9.0,2.0,3.0,6.0},
                 {2.0,5.0,1.0,3.0,9.0,2.0,3.0,6.0}};
 double lki[][]={{1.0,1.0,2.0,2.0,2.0,3.0,3.0,3.0},
                 {1.0,1.0,2.0,2.0,2.0,3.0,3.0,3.0},
                 {1.0,1.0,2.0,2.0,2.0,3.0,3.0,3.0}};
 double fi19i[][]={{0.0,28.95},{28.95,0.0}};
 double fi20i[][]={{0.0,-0.006008},{-0.006008,0.0}};
 double Fi[][]={{0.0,1.0},{1.0,0.0}};
 double f3i=0.617469323;
 double f4i=-0.596795;
 //refISO17584MixData(String refrigerantNamei,String refcNamei[],double Fi[][],double fi19i[],double fi20i[],double xi[],double f3i,double f4i,double Mi)
 rd=new refISO17584MixData("R410A",rcName, Fi,fi19i,fi20i,rxi,f3i,f4i,Mi);
 rd.input_mix(Ni,tki,dki,lki,rxi);
 b2=true;        	 
}
 if(rName.equals("R507A"))
 {    
 String rcName[]={"R125","R143a"};
 double rxi[]={0.41183971,0.58816029};
 double Mi=98.8594;
 double Ni[][]={{-0.013073,0.018259,0.0000081299,0.0078496}};
 double tki[][]={{7.40,0.35,10.0,5.30}};
 double dki[][]={{1.0,3.0,11.0,2.0}};
 double lki[][]={{1.0,1.0,2.0,3.0}};
 double fi19i[][]={{0.0,5.551},{5.551,0.0}};
 double fi20i[][]={{0.0,-0.0004452},{-0.0004452,0.0}};
 double Fi[][]={{0.0,1.1697},{1.1697,0.0}};
 double f3i=0.630988493;
 double f4i=19.345427;
 //refISO17584MixData(String refrigerantNamei,String refcNamei[],double Fi[][],double fi19i[],double fi20i[],double xi[],double f3i,double f4i,double Mi)
 rd=new refISO17584MixData("R507A",rcName, Fi,fi19i,fi20i,rxi,f3i,f4i,Mi);
 rd.input_mix(Ni,tki,dki,lki,rxi);
 b2=true;        	 
}
return b2;
}
// saturation state
//=========================================================

// Doyma termodinamik ?zellikleri
//doyma gaz ?zg?l hacim
//doyma s?v? ?zg?l hacim
double vf(double t)
{return 1.0/r.rd.rol(t);}
// doyma gaz ?zg?l hacim
double vg(double t)
{return 1.0/r.rd.rov(t);}


//double Ps(double t)
//{return r.rd.Psb(t);}

double Psb(double t)
{return r.rd.Psb(t);}

double Psd(double t)
{return r.rd.Psd(t);}

//double Ts(double ps)
//{return r.Tsd(ps);}
double Tsb(double ps)
{return r.Tsb(ps);}

double Tsd(double ps)
{return r.Tsd(ps);}



// doyma s?v? entalpi
double hf(double t)
{return r.rd.h_l(t);}
//doyma gaz entalpi

double hg(double t)
{return r.rd.h_v(t);}

//doyma s?v? i? enerji
double uf(double t)
{return r.u_l(t);}

//doyma gaz i? enerji

double ug(double t)
{return r.u_v(t);}

// doyma s?v? entropi
double sf(double t)
{return r.s_l(t);}
//doyma gaz entropi

double sg(double t)
{return r.rd.s_v(t);}

double vsl_t(double t)
{return r.rd.vl(t);}

double vsg_t(double t)
{return r.rd.vv(t);}

double vg_app(double t)
{return 1.0/r.rov(t);}


double v_tp(double t,double P)
{ // P kPa
  // Pc kritik basınç r.Pc
  double Tc=r.Tc;
  double Td=Tsd(P);
  double Tb=Tsb(P);
  double Pd=Psd(t);
  double Pb=Psb(t);
double R=rd.R;

// brent metodu ile kök değerini bulur
double vc,vg,vf;
double x1=0,x2=0;
vc=r.vc;
vg=vg(t);
vf=vf(t);
double b=(R*1e3/r.M)*(t+273.15)/(P*1.0e3);
//System.out.println("R="+R+"b="+b+"vg="+vg);
if(P<Pd) {x1=vg;x2=b;}//return v_tp1(t,P);} //supercritical or superheated gas
else if(P>Pb) {x1=5e-5;x2=vf;}  //liquid
else {x1=vf;x2=vg;} //saturated
//System.out.println("R="+R+"b="+b+"vg="+vg+"x1="+x1+"x2="+x2);
double test;
double p=0;
double es,ea;
double f1,f2,f3,fp;
int maxit=500,iter=0;
double tol=1.0e-5;
es=0.0001;
f1=P(t,x1)-P;
f2=P(t,x2)-P;
//System.out.println("f1="+f1+"f2="+f2+"P="+P);
double x3=(x1+x2)/2.0;f3=P(t,x3)-P;
if(f1==0) return x1;
else if(f2==0) return x2;
else if(f3==0) return x3; 
//if(f1*f2>0) System.out.println("verilen bölgede kök yok");
p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1));
fp=P(t,p)-P;
ea=Math.abs(x3-p);
while((ea>es)&&(iter<maxit))
{	if(Math.abs(f3)<tol) return x3; 
    if((p<x1) && (p>x2)) 
    {p=(x1+x2)/2.0;    
     if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;}
     else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;}
       //System.out.println("p="+p);
    }
    else
    {        
    if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;}
    else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;}
    p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1));
    fp=P(t,p)-P; 
    ea=Math.abs(x3-p);
    //System.out.println("p="+p);
    }
    iter++;
} 
//System.out.println("p="+p);
return p;
}


public double v_tp1(double t,double Pi)
{
//specific volume as a function of temperature and pressure	
// x in f(x)=0 given also df(x)/dx 
// Newton - Raphson type method 
double T=t+273.15;
double x=(T*8.3145/Pi)/r.M;
int nmax=500;
double tolerance=1.0e-8;
double fx,dfx,dfx1;
for(int i=0;i<nmax;i++)
{
fx=P(t,x)-Pi;
dfx1=(P(t,(x+0.01))-P(t,x))/0.01;
dfx=dPdv(t,x);
x-=fx/dfx1;
//System.out.println("i="+i+"x="+x+"fx="+fx+"dfx="+dfx+"dfx1="+dfx1);
if(Math.abs(fx)<tolerance)
{ return x; }
}
return x;
}

public double[] v_tp_braket(double t,double Pi,double x1,double x2)
{
	//returns a bracketed root 
	//if a root is not bracketed, enlarge region outwards each time
	int NTRY=50;
	double a[]=new double[2];
	double FACTOR=1.343253;
	int j;
	double f1,f2;
	if (x1 == x2) System.out.println("Bad initial range in ZBRAC");
	f1=P(t,x1)-Pi;
	f2=P(t,x2)-Pi;
	for (j=1;j<=NTRY;j++) {
		if (f1*f2 < 0.0) {a[0]=x1;a[1]=x2;return a;}
		if (Math.abs(f1) < Math.abs(f2))
			f1=P(t,(x1 += FACTOR*(x1-x2)))-Pi;
		else
			f2=P(t,(x2 += FACTOR*(x2-x1)))-Pi;
	}
	{a[0]=x1;a[1]=x2;return a;}
}

public double v_th(double t,double hi)
{
// bisection method
// defination of variables :
// xl : lower guess
// xu : upper guess
// xr : root estimate
// es : stopping criterion
// ea :approximate error
// maxit : maximum iterations
// iter  : number of iteration
double xl=0.94;
double xu=726025;
double test;
double xr=0;
double es,ea;
double fxl,fxr;
double tolerance=1e-20;
int maxit=500,iter=0;
es=0.000001;
ea=1.1*es;
     //double ps=Ps(t);
     double vsl=vsl_t(t);
     double vsg=vsg_t(t);
     double hsl=h(t,vsl);
     double hsg=h(t,vsg);
    if( (t+273.15) >rd.Tc)
    { if(hi< hsl) {xl=0.94;xu=vsl*1e3;}
      else  {xl=vsl*1e3;xu=726025;}
    }
    else if((t+273.15) <rd.Tc)
    {
      double xx1=(hi-hsl)/(hsg-hsl);
      double xx=(hsl-hi)/hsl;
      if(hi<=hsl*1.1) {xl=vsl*1e3*0.9;xu=1.1*vsl*1e3;}
      //else if (hi>=hsl && hi<=hsg) {return vsl*(1-xx1)+vsg*xx1;}
      else if (hi>=hsg) {xl=vsg_t(t)*1e3;xu=726025;}
      else {xl=vsl*1e3*0.9;xu=xu=1.1*vsg*1e3;}
    }
while((ea>es)&&(iter<maxit))
{
xr=(xl+xu)/2.0;
iter++;
if((xl+xu)!=0)
  { ea=Math.abs((xu-xl)/(xl+xu))*100;}
fxl=h(t,xl*1e-3)-hi;
fxr=h(t,xr*1e-3)-hi;
test= fxl*fxr;
if(fxl==0) return xl;
if(test==0.0 || (Math.abs(fxl) < tolerance) )       ea=0;
else if(test<0.0)  xu=xr;
else
  {
  xl=xr;
  }
} //end of while
return xr*1e-3;
}

public double v_tu(double t,double ui)
{
//bisection method
// defination of variables :
// xl : lower guess
// xu : upper guess
// xr : root estimate
// es : stopping criterion
// ea :approximate error
// maxit : maximum iterations
// iter  : number of iteration
double xl=0.94;
double xu=726025;
double test;
double xr=0;
double es,ea;
double fxl,fxr;
double tolerance=1e-20;
int maxit=500,iter=0;
es=0.000001;
ea=1.1*es;
     //double ps=Ps(t);
     double vsl=vsl_t(t);
     double vsg=vsg_t(t);
     double usl=u(t,vsl);
     double usg=u(t,vsg);
    if( (t+273.15) >rd.Tc)
    { if(ui< usl) {xl=0.94;xu=vsl*1e3;}
      else  {xl=vsl*1e3;xu=726025;}
    }
    else if((t+273.15) < rd.Tc)
    {
      double xx1=(ui-usl)/(usg-usl);
      double xx=(usl-ui)/usl;
      if(ui<=usl*1.1) {xl=vsl*1e3*0.9;xu=1.1*vsl*1e3;}
      //else if (ui>=usl && ui<=usg) {return vsl*(1-xx1)+vsg*xx1;}
      else if (ui>=usg) {xl=vsg_t(t)*1e3;xu=726025;}
      else {xl=vsl*1e3*0.9;xu=xu=1.1*vsg*1e3;}
    }
while((ea>es)&&(iter<maxit))
{
xr=(xl+xu)/2.0;
iter++;
if((xl+xu)!=0)
  { ea=Math.abs((xu-xl)/(xl+xu))*100;}
fxl=u(t,xl*1e-3)-ui;
fxr=u(t,xr*1e-3)-ui;
test= fxl*fxr;
if(fxl==0) return xl;
if(test==0.0 || (Math.abs(fxl) < tolerance) )       ea=0;
else if(test<0.0)  xu=xr;
else
  {
  xl=xr;
  }
} //end of while
return xr*1e-3;
}


public double v_ts(double t,double si)
{
// bisection method
// defination of variables :
// xl : lower guess
// xu : upper guess
// xr : root estimate
// es : stopping criterion
// ea :approximate error
// maxit : maximum iterations
// iter  : number of iteration
double xl=0.94;
double xu=726025;
double test;
double xr=0;
double es,ea;
double fxl,fxr;
double tolerance=1e-20;
int maxit=500,iter=0;
es=0.000001;
ea=1.1*es;
     //double ps=Ps(t);
     double vsl=vsl_t(t);
     double vsg=vsg_t(t);
     double ssl=s(t,vsl);
     double ssg=s(t,vsg);
  if( (t+273.15) >rd.Tc)
  { if(si< ssl) {xl=0.94;xu=vsl*1e3;}
    else  {xl=vsg*1e3;xu=726025;}
  }
  else if((t+273.15) <rd.Tc)
  {
     double xx=(ssl-si)/ssl;
     double xi=(si-ssl)/(ssg-ssl);
     if(si==ssl) return vsl;
     else if(si==ssg) return vsg;
     //else if((si>ssl) && (si<ssg) ) {return (1-xi)*vsl+xi*vsg;}
     else if(si<ssl) {xl=0.94;xu=1.1*vsl*1e3;}
     else if (si>ssg) {xl=vsg_t(t)*1e3;xu=726025;}
  }
while((ea>es)&&(iter<maxit))
{
xr=(xl+xu)/2.0;
iter++;
if((xl+xu)!=0)
  { ea=Math.abs((xu-xl)/(xl+xu))*100;}
fxl=s(t,xl*1e-3)-si;
fxr=s(t,xr*1e-3)-si;
test= fxl*fxr;
if(fxl==0) return xl;
if(test==0.0 || (Math.abs(fxl) < tolerance) )       ea=0;
else if(test<0.0)  xu=xr;
else
  {
  xl=xr;
  }
} //end of while
return xr*1e-3;
}

public double t_ph(double pi,double hi)
{
	double xx=0;
double xl=0.1;
double xu=3000;
if(pi<r.Pc)
{
double tsl=Tsb(pi);
double tsg=Tsd(pi);
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
double hsl=h(tsl,vsl);
double hsg=h(tsg,vsg);
if(hi<hsl) {xl=0.1;xu=tsl;}
else if(hi>hsg) {xl=tsg;xu=3000;}
else if(hi>=hsl && hi<=hsg) 
{ xx=(hi-hsl)/(hsg-hsl);
  return tsl*(1.0-xx)+tsg*xx; }
}
int maxit=50,iter=0;
double es=0.000000001;
double dx=Math.abs((xu-xl)/maxit);
double dxp=dx;
double xr=xl;
double fx=hi-h(xr,v_tp(xr,pi));
double fxr=fx;
for(int i=0;i<maxit;i++)
{
xr+=dxp;
if(xr>xu) break;
fx=hi-h(xr,v_tp(xr,pi));
  if(fx*fxr<0)
  {
    if(dxp<es) return xr;
    else
    {
    xr-=dxp;
    dxp/=2.0;
    }
  }
}
return xr;
}

public double v_ph(double pi,double hi)
{
return v_tp(t_ph(pi,hi),pi);
}

public double t_pu(double pi,double ui)
{
double xx=0;
double xl=0.1;
double xu=3000;
if(pi<r.Pc)
{
double tsl=Tsb(pi);
double tsg=Tsb(pi);
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
double usl=h(tsl,vsl);
double usg=h(tsg,vsg);
if(ui<usl) {xl=0.1;xu=tsg;}
else if(ui>usg) {xl=tsl;xu=3000;}
else if(ui>=usl && ui<=usg) 
{ xx=(ui-usl)/(usg-usl);
  return tsl*(1.0-xx)+tsg*xx; 
}
}
int maxit=50,iter=0;
double es=0.000000001;
double dx=Math.abs((xu-xl)/maxit);
double dxp=dx;
double xr=xl;
double fx=ui-u(xr,v_tp(xr,pi));
double fxr=fx;
for(int i=0;i<maxit;i++)
{
xr+=dxp;
if(xr>xu) break;
fx=ui-u(xr,v_tp(xr,pi));
  if(fx*fxr<0)
  {
    if(dxp<es) return xr;
    else
    {
    xr-=dxp;
    dxp/=2.0;
    }
  }
}
return xr;
}

public double v_pu(double pi,double ui)
{
return v_tp(t_pu(pi,ui),pi);
}

public double t_ps(double pi,double si)
{
	double xx=0;
double xl=0.1;
double xu=3000;
if(pi<r.Pc)
{
double tsl=Tsb(pi);
double tsg=Tsd(pi);
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
double ssl=s(tsl,vsl);
double ssg=s(tsg,vsg);
if(si<ssl) {xl=0.1;xu=tsl;}
else if(si>ssg) {xl=tsg;xu=3000;}
else if(si>=ssl && si<=ssg) 
{ xx=(si-ssl)/(ssg-ssl);
  return tsl*(1.0-xx)+tsg*xx; 
}
}
int maxit=50,iter=0;
double es=0.000000001;
double dx=Math.abs((xu-xl)/maxit);
double dxp=dx;
double xr=xl;
double fx=si-s(xr,v_tp(xr,pi));
double fxr=fx;
for(int i=0;i<maxit;i++)
{
xr+=dxp;
if(xr>xu) break;
fx=si-s(xr,v_tp(xr,pi));
  if(fx*fxr<0)
  {
    if(dxp<es) return xr;
    else
    {
    xr-=dxp;
    dxp/=2.0;
    }
  }
}
return xr;
}

public double v_ps(double pi,double si)
{
return v_tp(t_ps(pi,si),pi);
}

public double v_tx(double t, double x)
{
  double vsl=vsl_t(t);
  double vsg=vsg_t(t);
  return vsl*(1-x)+vsg*x;
}

public String phase(double t, double v)
{

if( Psd(t) > r.Pc)
  return "kritik?st? s?v?";
else
 {
 double ts=t;
 double vsl=vsl_t(ts);
 double vsg=vsg_t(ts);
 if(v<vsl)
   return "sıvı";
 else if(v==vsl)
   return "doymuş sıvı";
 else if(v==vsg)
   return "doymuş buhar";
 else if(v>vsl && v<vsg)
   return "doymuş sıvı buhar karışımı";
 else // (v>vsg)
   return "kızgın buhar";
 }
}

public String phase(double x)
{
//phase of the amonia system
if( x==3)
  return "kritik üstü sıvı";
else
 {
 if(x<0)
   return "sıvı";
 else if( x==0)
   return "doymuş sıvı";
 else if(x==1)
   return "doymuş buhar";
 else if( x>0 && x<1)
   return "doymuş sıvı buhar karışımı";
 else // (x>1)
   return "kızgın buhar";
 }
}

public String phase_EN(double t, double v)
{
t=(t-32)/1.8;
v/=16.01877892;
return phase(t,v);
}

public double t_pv(double p, double v)
{
double ro=1e-3/v;
double tsl=Tsb(p);
double tsg=Tsd(p);
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
double x=200.0;
int nmax=100;
double dx=1.0e-3;
double dx1=2.0e-3;
double x1m;
double tolerance=1.0e-8;
double fx,fxm,fxm1,dfx,dfxm,d2fx;
  if( p > r.Pc)
  { x=p*v/(8314.5/18.02); }
  else if(p < r.Pc)
  {  x=tsg*v/vsg; }
  for(int i=0;i<nmax;i++)
  {
  fx=P(x,v)-p;
  fxm=P((x-dx),v)-p;
  fxm1=P((x-dx1),v)-p;
  dfx=(fx-fxm)/dx;
  dfxm=(fx-fxm1)/dx1;
  d2fx=-6.0/dx/dx*(fx-fxm)+2.0/dx*(2.0*dfx+dfxm);
  x-=(fx/dfx+.5*fx*fx/(dfx*dfx*dfx)*d2fx);
  if(Math.abs(fx)<tolerance)
    { return x; }
  }
  return x;
}

public double x_tv(double t, double v)
{
//phase and/or quality of the steam-water system
double xx=0;
double p=P(t,v);
double T=t+273.15;//K
double Psb=Psb(t);//buble 
double Psd=Psd(t);
double Pc=r.rd.Pc;

double vsl=vsl_t(t);
double vsg=vsg_t(t);

if(t>= r.Tc) xx=3;


else
{
  if(v==vsl)
    xx=0;
  else if(v==vsg)
    xx=1;
  else if(v>vsl && v<vsg)
    xx=(v-vsl)/(vsg-vsl);
  else if(v>vsg )
    xx=2;
  else if(v<vsl )
    xx=-1;  
//System.out.println("p="+p+"pc="+Pc+"psb="+Psb+"psd="+Psd+"v="+v+"vsl="+vsl+"vsg="+vsg+"x="+xx); 
}
   return xx;  
}
public double x_pv(double p, double v)
{
//phase and/or quality 
double xx=0;

double tsl=Tsb(p);
double tsg=Tsd(p);
double Pc=r.rd.Pc;
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
if(p >=Pc )
{ xx=3; }
else
{
 if(v<vsl) xx=-1;
 else if(v>vsg) xx=2;
 else if(v==vsl) xx=0;
 else if(v==vsg) xx=1;
 else {xx=(v-vsl)/(vsg-vsl);}
}
return xx;  
}

public double[] property_EN(String s, double v1, double v2)
{
if(s.charAt(0)=='t') v1=(v1-32.0)/1.8;
else if(s.charAt(0)=='p') v1/=14.504;
else if(s.charAt(0)=='v') v1/=16.01877892;
if(s.charAt(1)=='t') v2=(v2-32)/1.8;
else if(s.charAt(1)=='p') v2/=14.504;
else if(s.charAt(1)=='v') v2/=16.01877892;
else if(s.charAt(1)=='h') v2/=0.42992;
else if(s.charAt(1)=='u') v2/=0.42992;
else if(s.charAt(1)=='s') v2/=0.2388444444;
double pp[]=property_SI(s,v1,v2);
pp[0]*=14.504;
pp[1]=pp[1]*1.8+32;
pp[2]*=16.01877892;
pp[3]*=0.42992;
pp[4]*=0.42992;
pp[5]*=0.2388444444;
pp[7]=1.0/pp[2];
return pp;
}

public double[] property_SI(String s, double v1, double v2)
{
//cases of String s
//    tv tp th tu ts tx
// pv    pt ph pu ps px
// vp    vt
double[] r=new double[11];
char s1=s.charAt(0);
char s2=s.charAt(1);
r[6]=x_tv(v1,v2);
if(s.equals("tv")) //tv se?imi
{
double vsl=vsl_t(v1);
double vsg=vsg_t(v1);	
double qsl=vf(v1);
double qsg=vg(v1);
 r[0]=P(v1,v2);
 r[1]=v1; 
 r[2]=v2; 
 r[3]=h(v1,v2);
 r[4]=u(v1,v2);
 r[5]=s(v1,v2);	
 if(v1<this.r.Tc)
 {r[6]=(v2-qsl)/(qsg-qsl);}
 if(r[6]>=0 && r[6]<=1)
 { r[0]=Psb(v1)*(1.0-r[6])+Psd(v1)*r[6];
   r[1]=v1; 
   r[2]=v2; 
   r[3]=h(v1,vsl)*(1.0-r[6])+ h(v1,vsg)*r[6];
   r[4]=u(v1,vsl)*(1.0-r[6])+ u(v1,vsg)*r[6];
   r[5]=s(v1,vsl)*(1.0-r[6])+ s(v1,vsg)*r[6];
 }
 } //tv se?imi sonu
else if(s.equals("pv"))  // pv se?imi
{
double tsl=Tsb(v1);
double tsg=Tsd(v1);
double vsl=vsl_t(tsl);
double vsg=vsg_t(tsg);
double qsl=vf(tsl);
double qsg=vg(tsg);
r[0]=v1;
r[1]=t_pv(v1,v2);
r[2]=v2;
r[3]=h(v1,v2);
r[4]=u(v1,v2);
r[5]=s(v1,v2);
if(v1<this.r.Pc)
{r[6]=(v2-qsl)/(qsg-qsl);}
else r[6]=3;
 if(r[6]>=0 && r[6]<=1)
 {r[0]=v1;
  r[1]=Tsb(v1)*(1.0-r[6])+Tsd(v1)*r[6];
  r[2]=vsl*(1-r[6])+vsg*r[6];
  r[3]=h(r[1],vsl)*(1-r[6])+ h(r[1],vsg)*r[6];
  r[4]=u(r[1],vsl)*(1-r[6])+ u(r[1],vsg)*r[6];
  r[5]=s(r[1],vsl)*(1-r[6])+ s(r[1],vsg)*r[6];
 }
} //pv se?imi sonu
else if(s.equals("tp")) //tp se?imi
{
   r[0]=v2;
   r[1]=v1;
   r[2]=v_tp(v1,v2);
   r[3]=h(r[1],r[2]);
   r[4]=u(r[1],r[2]);
   r[5]=s(r[1],r[2]);
   r[6]=x_tv(r[1],r[2]);
} //tp se?imi sonu
else if(s.equals("th")) //th se?imi
{
 //double hsl=h(v1,vsl_t(v1));
 //double hsg=h(v1,vsg_t(v1));
 double vth=v_th(v1,v2);
 double hsl=hf(v1);
 double hsg=hg(v1);
 double xx=0;
   r[0]=P(v1,vth);
   r[1]=v1;
   r[2]=vth;
   r[3]=v2;
   r[4]=u(r[1],r[2]);
   r[5]=s(r[1],r[2]);
   if(v1<this.r.Tc)
   r[6]=(v2-hsl)/(hsg-hsl);
 if(r[6]>=0 && r[6]<=1)
 { r[0]=Psb(v1)*(1.0-r[6])+Psd(v1)*r[6];
   r[1]=v1;
   double vsl=vsl_t(v1);
   double vsg=vsg_t(v1);
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=v2;
   r[4]=u(v1,vsl)*(1-r[6])+ u(v1,vsg)*r[6];
   r[5]=s(v1,vsl)*(1-r[6])+ s(v1,vsg)*r[6];
  }
} //th se?imi sonu
else if(s.equals("tu")) //tu se?imi
{
   double vtu=v_tu(v1,v2);
   double usl=uf(v1);
   double usg=ug(v1);
   r[0]=P(v1,vtu);
   r[1]=v1;
   r[2]=vtu;
   r[3]=h(r[1],r[2]);
   r[4]=v2;
   r[5]=s(r[1],r[2]);
   if(v1<this.r.Tc)
   r[6]=(v2-usl)/(usg-usl);
   if(r[6]>=0 && r[6]<=1)
   {
      r[0]=Psb(v1)*(1.0-r[6])+Psd(v1)*r[6];
      r[1]=v1;
      double vsl=vsl_t(v1);
      double vsg=vsg_t(v1);
      r[2]=vsl*(1-r[6])+ vsg*r[6];
      r[3]=h(v1,vsl)*(1-r[6])+ h(v1,vsg)*r[6];
      r[4]=v2;
      r[5]=s(v1,vsl)*(1-r[6])+ s(v1,vsg)*r[6];
  }
} // tu se?imi sonu
else if(s.equals("ts")) //ts se?imi
{
   double psl=Psb(v1);
   double psg=Psb(v1);
   double vts=v_ts(v1,v2);
   double ssl=sf(v1);
   double ssg=sg(v1);
   r[0]=P(v1,vts);
   r[1]=v1;
   r[2]=vts;
   r[3]=h(r[1],r[2]);
   r[4]=u(r[1],r[2]);
   r[5]=v2;
   if(v1<this.r.Tc);
   r[6]=(v2-ssl)/(ssg-ssl);
   if(r[6]>=0 && r[6]<=1)
  { r[0]=Psb(v1)*(1.0-r[6])+Psd(v1)*r[6];
   double vsl=vsl_t(v1);
   double vsg=vsg_t(v1);
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=h(v1,vsl)*(1-r[6])+ h(v1,vsg)*r[6];
   r[4]=u(v1,vsl)*(1-r[6])+ u(v1,vsg)*r[6];
   r[5]=v2;
 }
} //ts se?imi sonu
else if(s.equals("tx"))
{
   r[6]=v2;
   r[0]=Psb(v1)*(1.0-r[6])+Psd(v1)*r[6];
   r[1]=v1;
   double hl=hf(v1);
   double hg=hg(v1);
   double vsl=vsl_t(v1);
   double vsg=vsg_t(v1);
   double ul=hl-r[0]*vsl;
   double ug=hg-r[0]*vsg;
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=hl*(1-r[6])+ hg*r[6];
   r[4]=ul*(1-r[6])+ ug*r[6];
   r[5]=sf(v1)*(1-r[6])+ sg(v1)*r[6];
   //System.out.println("r5=s="+r[5]+"v1=t="+v1+"sf="+sf(v1)+"sg="+sg(v1));
} //tx se?imi sonu
else if(s.equals("vt"))
{
 r=property("tv",v2,v1);
} //vt se?imi sonu
else if(s.equals("vp"))
{
 r=property("pv",v2,v1);
} //vp se?imi sonu
else if(s.equals("pt"))
{
 r=property("tp",v2,v1);
} //pt se?imi sonu
else if(s.equals("ph"))
{

 double tsl=Tsb(v1);
 double tsg=Tsd(v1);
 double vsl=vsl_t(tsl);
 double vsg=vsg_t(tsg);
 double hsl=hf(tsl);
 double hsg=hg(tsg);
 double vph=v_ph(v1,v2);
   r[0]=v1;
   r[1]=t_ph(v1,v2);
   r[2]=vph;
   r[3]=v2;
   r[4]=u(r[1],vph);
   r[5]=s(r[1],vph);
   if( v1 < this.r.Pc)
   { r[6]=(v2-hsl)/(hsg-hsl);}
   else r[6]=3;
 if(r[6]>=0 && r[6]<=1)
 { r[0]=v1;
   r[1]=Tsb(v1)*(1.0-r[6])+Tsd(v1)*r[6];
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=v2;
   r[4]=u(r[1],vsl)*(1-r[6])+ u(r[1],vsg)*r[6];
   r[5]=s(r[1],vsl)*(1-r[6])+ s(r[1],vsg)*r[6];
  }
} //ph se?imi sonul
else if(s.equals("pu"))
{
 double tsl=Tsb(v1);
  double tsg=Tsd(v1);
 double vsl=vsl_t(tsl);
 double vsg=vsg_t(tsg);
 double usl=uf(tsl);
 double usg=ug(tsg);
 double vpu=v_pu(v1,v2);
   r[0]=v1;
   r[1]=t_pu(v1,v2);
   r[2]=vpu;
   r[3]=h(r[1],r[2]);
   r[4]=v2;
   r[5]=s(r[1],r[2]);
   if( v1 < this.r.Pc)
   { r[6]=(v2-usl)/(usg-usl);}
   else r[6]=3;
 if(r[6]>=0 && r[6]<=1)
 { r[0]=v1;
   r[1]=Tsb(v1)*(1.0*r[6])+Tsd(v1)*r[6];
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=h(r[1],vsl)*(1-r[6])+ h(r[1],vsg)*r[6];
   r[4]=v2;
   r[5]=s(r[1],vsl)*(1-r[6])+ s(r[1],vsg)*r[6];
 }
} //pu se?imi sonu 
else if(s.equals("ps"))
{
 double tsl=Tsb(v1);
  double tsg=Tsd(v1);
 double vsl=vsl_t(tsl);
 double vsg=vsg_t(tsg);
 double ssl=sf(tsl);
 double ssg=sg(tsg);
 double vps=v_ps(v1,v2);
   r[0]=v1;
   r[1]=t_ps(v1,v2);
   r[2]=vps;
   r[3]=h(r[1],r[2]);
   r[4]=u(r[1],r[2]);
   r[5]=v2;
   if( v1 < this.r.Pc)
   { r[6]=(v2-ssl)/(ssg-ssl);}
   else r[6]=3;
 if(r[6]>=0 && r[6]<=1)
 { r[0]=v1;
   r[1]=Tsb(v1)*(1.0-r[6])+Tsd(v1)*r[6];
   r[2]=vsl*(1-r[6])+ vsg*r[6];
   r[3]=h(r[1],vsl)*(1-r[6])+ h(r[1],vsg)*r[6];
   r[4]=u(r[1],vsl)*(1-r[6])+ u(r[1],vsg)*r[6];
   r[5]=v2;
  }
} //ps se?imi sonu
else if(s.equals("px"))
{
   r[6]=v2;
   r[0]=v1;
   double tsl=Tsb(v1);
   double tsg=Tsd(v1);
   double hl=hf(tsl);
   double hg=hg(tsg);
   double sl=sf(tsl);
   double sg=sg(tsg);
   double vsl=vsl_t(tsg);
   double vsg=vsg_t(tsg);
   double ul=hl-r[0]*vsl;
   double ug=hg-r[0]*vsg;
   r[1]=tsl*(1.0-r[6])+tsg*r[6];
   r[2]=vsl*(1.0-r[6])+ vsg*r[6];
   r[3]=hl*(1.0-r[6])+ hg*r[6];
   r[4]=ul*(1.0-r[6])+ ug*r[6];
   r[5]=sl*(1.0-r[6])+ sg*r[6];
} //px seçimi sonu
r[7]=1.0/r[2];
return r;
}

public double[] property(String s, double v1, double v2)
{
return property_SI(s,v1,v2);
}


public String[][] toString(String s, double v1, double v2)
{
  String s1[][]=new String[9][3];
   s1[0][0]="P, pressure        ";
   s1[1][0]="T, temperature     ";
   s1[2][0]="v, specific volume ";
   s1[3][0]="h, enthalpy        ";
   s1[4][0]="u, internal energy ";
   s1[5][0]="s, entropy         ";
   s1[6][0]="x, quality         ";
   s1[7][0]="   density         ";
   s1[8][0]="phase              ";
   {
   s1[0][2]=" kpa           ";
   s1[1][2]=" deg C         ";
   s1[2][2]=" m^3/kg        ";
   s1[3][2]=" KJ/kg         ";
   s1[4][2]=" KJ/kg         ";
   s1[5][2]=" KJ/kg         ";
   s1[6][2]=" kg vap/kg mix ";
   s1[7][2]=" kg/m^3        ";
   s1[8][2]=" ";
   }

double pp[]=property(s,v1,v2);
for(int i=0;i<7;i++)
   {s1[i][1]=""+pp[i];}
    s1[7][1]=""+pp[7];
    s1[8][1]=phase(pp[6]);
return s1;
}

public String toString1(String s, double v1, double v2)
{
   String[] s1=new String[7];
   String[] s2=new String[7];
   String s3="";
   double pp[];
   s1[0]="P :";
   s1[1]="T :";
   s1[2]="v :";
   s1[3]="h :";
   s1[4]="u :";
   s1[5]="s :";
   s1[6]="x :";
   s1[7]="  :";

   s2[0]=" kpa           ";
   s2[1]=" deg C         ";
   s2[2]=" m^3/kg        ";
   s2[3]=" KJ/kg         ";
   s2[4]=" KJ/kg         ";
   s2[5]=" KJ/kg         ";
   s2[6]=" kg vap/kg mix ";
   s2[7]=" kg/m^3        ";
pp=property(s,v1,v2);
s3+=phase(pp[6])+"\n";

for(int i=1;i<9;i++)
{
s3+=s1[i-1]+s2[i-1]+pp[i-1]+"\n";
}
return s3;
}

public String toString()
{
   String[] s1=new String[7];
   String[] s2=new String[7];
   String s3="";
   double pp[];
   s1[0]="P :";
   s1[1]="T :";
   s1[2]="v :";
   s1[3]="h :";
   s1[4]="u :";
   s1[5]="s :";
   s1[6]="x :";
   s1[7]="  :"; 
   s2[0]=" kpa           ";
   s2[1]=" deg C         ";
   s2[2]=" m^3/kg        ";
   s2[3]=" KJ/kg         ";
   s2[4]=" KJ/kg         ";
   s2[5]=" KJ/kg K       ";
   s2[6]=" kg vap/kg mix ";
   s2[7]=" kg/m^3        ";

for(int i=0;i<8;i++)
{
s3+=s1[i]+s2[i]+"\n";
}
return s3;
}
   public double v_tpPR(String s,double t, double P)
   {
        // This value is calculated by using Peng-Robinson Equation of state
        // liquid phase compressibility factor
        // if state is above critical liquid compressibility factor will be same as
        // vapor compressibility factor.
        double T=t+273.15;
        double Omega=0.334;
        double R=8314.5;
        double u=2;
        double w=-1;
        double fw=0.37464+1.54226*Omega-0.26992*Omega*Omega;
        double Tr=T/rd.Tc;
        double b=0.07780*R*rd.Tc/(r.Pc*1e5);
        double a=0.45724*R*R*rd.Tc*rd.Tc/(r.Pc*1e5)*
        Math.pow(1+fw*(1.0-Math.pow(Tr,0.5)),2);
        double AY=a*(P*1e5)/(R*R*T*T);
        double BY=b*(P*1e5)/(R*T);
        double A=-(1+BY-u*BY);
        double B=AY+w*BY*BY-u*BY-u*BY*BY;
        double C=-AY*BY-w*BY*BY-w*BY*BY*BY;
        double cof[]=new double[4];
        cof[0]=1;
        cof[1]=A;
        cof[2]=B;
        cof[3]=C;
        double zc[][]=Matrix.poly_roots(cof);
        double nmin=1e99;
        double nmax=-1e99;
        for(int i=0;i<3;i++)
        {
        if((zc[0][i]>=0.0 && zc[0][i]<=1.0) && (zc[1][i]>=0.0 && zc[1][i]<1e-10))
        { if(zc[0][i]<nmin) nmin=zc[0][i];
          if(zc[0][i]>nmax) nmax=zc[0][i];
        }
        }
     double y,zz;
     if(s.equals("l")) zz=nmin;
     else zz=nmax;;
     y=zz*R/r.rd.M*T/(P*1e5);
     return y;
   }




}