public class ref_CS_Data extends Object
{
//this class reads and store Modified benedict Webb Rubin  data
public double a[][];
public String refrigerantName;
public String openName;
public String ekbilgi;
public double M,BP,FP,Tc,Pc,roc;

public double VPb[][]; // buhar basıncı (buble point)
public double VPd[][]; // buhar basıncı (dew point)

public double VTb[][]; // buhar sıcaklığı (buble point)
public double VTd[][]; // buhar sıcaklığı (dew point) 

public double cpl[][];  // ideal gas heat capacitiy
public double cpv[][];  // ideal gas heat capacitiy
public double rol[][];  // saturated liquid density
public double rov[][];  // saturated vapor density
public double visl[][]; // saturated liquid viscosity
public double visv[][]; // saturated vapor viscosity
public double kl[][]; // saturated liquid thermal conductivity
public double kv[][]; // saturated vapor thermal conductivity
public double st[][]; // saturated liquid-vapor interphase surface tension
public double hl[][]; // saturated liquid enthalpy
public double hv[][]; // saturated vapour enthalpy
public double sl[][]; // saturated liquid enthalpy
public double sv[][]; // saturated vapour enthalpy
public double soundvl[][];// liquid sound velocity
public double soundvv[][];// vapor sound velocity

   public ref_CS_Data(String refrigerantNamei,String openNamei,double Mi,double BPi,double FPi,double Tci,double Pci,double roci)
   {
   refrigerantName=refrigerantNamei;
   a=Text.readDoubleT(refrigerantName+".txt");
   ekbilgi=" ";
   openName=openNamei;
   int n=a.length;
   int i,j;
   M=Mi;
   BP=BPi;
   FP=FPi;
   Tc=Tci;
   Pc=Pci;
   roc=roci;
   if(n==20)
   {
   VPb=cubic_spline(a[1],a[0],0.0,0.0);
   VPd=cubic_spline(a[2],a[0],0.0,0.0);
   
   VTb=cubic_spline(a[0],a[1],0.0,0.0);
   VTd=cubic_spline(a[0],a[2],0.0,0.0);
   
   cpl=cubic_spline(a[1],a[9],0.0,0.0);
   cpv=cubic_spline(a[2],a[10],0.0,0.0);
   
   visl=cubic_spline(a[1],a[14],0.0,0.0);
   visv=cubic_spline(a[2],a[15],0.0,0.0);
   
   kl=cubic_spline(a[1],a[16],0.0,0.0);
   kv=cubic_spline(a[2],a[17],0.0,0.0);
   
   st=cubic_spline(a[1],a[17],0.0,0.0);
   
   rol=cubic_spline(a[1],a[3],0.0,0.0);
   
   double ai8[]=new double[a[4].length];
   for(i=0;i<ai8.length;i++) {ai8[i]=1.0/a[4][i];}
   rov=cubic_spline(a[1],ai8,0.0,0.0);
   
   hl=cubic_spline(a[1],a[5],0.0,0.0);
   hv=cubic_spline(a[2],a[6],0.0,0.0);
   
   sl=cubic_spline(a[1],a[7],0.0,0.0);
   sv=cubic_spline(a[2],a[8],0.0,0.0);
   
   soundvl=cubic_spline(a[1],a[12],0.0,0.0);
   soundvv=cubic_spline(a[2],a[13],0.0,0.0);
   }
   else
   {
   VPb=cubic_spline(a[0],a[1],0.0,0.0);
   VPd=VPb;
   VTb=cubic_spline(a[1],a[0],0.0,0.0);
   VTd=VTb;
   cpl=cubic_spline(a[0],a[8],0.0,0.0);
   cpv=cubic_spline(a[0],a[9],0.0,0.0);
   
   visl=cubic_spline(a[0],a[13],0.0,0.0);
   visv=cubic_spline(a[0],a[14],0.0,0.0);
   
   kl=cubic_spline(a[0],a[15],0.0,0.0);
   kv=cubic_spline(a[0],a[16],0.0,0.0);
   
   st=cubic_spline(a[0],a[17],0.0,0.0);
   
   rol=cubic_spline(a[0],a[2],0.0,0.0);
   
   double ai8[]=new double[a[3].length];
   for(i=0;i<ai8.length;i++) {ai8[i]=1.0/a[3][i];}
   rov=cubic_spline(a[0],ai8,0.0,0.0);
   
   hl=cubic_spline(a[0],a[4],0.0,0.0);
   hv=cubic_spline(a[0],a[5],0.0,0.0);
   
   sl=cubic_spline(a[0],a[6],0.0,0.0);
   sv=cubic_spline(a[0],a[7],0.0,0.0);
   
   soundvl=cubic_spline(a[0],a[11],0.0,0.0);
   soundvv=cubic_spline(a[0],a[12],0.0,0.0);
   }
   }
   
   public void ekbilgi(String s)
   {ekbilgi=s;}
   
  public static double [] thomas(double a[][],double r[])
  {
  //
  int n=a.length;
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double x[]=new double[n];
  for(int i=0;i<n;i++)     {f[i]=a[i][i];}
  for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];}
  for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];}
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
  
  public static double [] thomas(double f[],double e[],double g[],double r[])
  {
  int n=f.length;
  double x[]=new double[n];
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
    
  public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn)
  {
  int n=xi.length;
  double h[]=new double[n];
  double w[]=new double[n];
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double d[]=new double[n];
  double x[]=new double[n];
  double S[][]=new double[4][n];
  int k;	  
  for(k=0;k<(n-1);k++)
     {h[k]=xi[k+1]-xi[k];
      w[k]=(yi[k+1]-yi[k])/h[k];      
	 }
  d[0]=c0;
  d[n-1]=cn;
  for(k=1;k<(n-1);k++)
     {d[k]=6.0*(w[k]-w[k-1]);}
  f[0]=1.0;
  f[n-1]=1.0;
  g[0]=0.0;
  g[n-1]=0.0;
  e[0]=0.0;
  e[n-1]=0.0;
  for(k=1;k<(n-1);k++)
  {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];}
  S[2]=thomas(f,e,g,d);
  S[3]=xi;
  for(k=0;k<(n-1);k++)
    {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]);
     S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]);
    } 
  return S;    	 	     	 
  }
  
  public static double funcSpline(double S[][],double x)
  {
  int n=S[0].length;
  double xx1=0;
  double xx2=0;
  double y=0;
  double hk=0;
  for(int k=0;k<(n-1);k++)
     {if(S[3][k]<=x && x<=S[3][k+1]) 
      {hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk);
       break;
       }
     }
       if(y==0 && S[3][n-2]<=x )
       {    
	   int k=n-2;
	   hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk);
       }
  return y;
  }    
   
 public static double[][]  funcSpline(double xi[],double yi[],int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double S[][]=cubic_spline(xi,yi,0,0);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);
    return z;
 }
 public static void plotSpline(double xi[],double yi[],int aradegersayisi)
 {	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double S[][]=cubic_spline(xi,yi,0,0);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);
       Plot pp=new Plot(z[0],z[1]);
       pp.setPlotType(0,0); 
       pp.addData(xi,yi);
       pp.setPlotType(1,24);
       //pp.setPlabel("R134a");
       //pp.setXlabel("T derece C");
       //pp.setYlabel("h ");
       pp.plot();  
       }
 // Türev formülleri
// =================       
  public static double dfSpline(double xi[],double yi[],double c0,double cn,double x)
  { //kübik şerit türev formülü 
	double S[][]=cubic_spline(xi,yi,c0,cn);	
    return dfSpline(S,x);
  }
  
  public static double dfSpline(double S[][],double x)
  {
    //kübik şerit türev formülü 
  int n=S[0].length;
  double xx1=0;
  double xx2=0;
  double y=0;
  double hk=0;
  for(int k=0;k<(n-1);k++)
     {if(S[3][k]<=x && x<=S[3][k+1]) 
      {hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]-S[1][k]+(xx1*xx1*S[2][k+1]-xx2*xx2*S[2][k])/(2.0*hk);
       break;
       }
     }
       if(y==0 && S[3][n-2]<=x )
       {    
	   int k=n-2;
	   hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk);
       }
  return y;
  } 
       
 public static double  dfSpline(double xi[],double yi[],double x)
 {
    //doğal kübik şerit türev formülü 
	double S[][]=cubic_spline(xi,yi,0,0);	
    return dfSpline(S,x);
 } 
   public static double intSpline(double S[][],double a,double b)
  {
    //kübik şerit türev formülü 
  int n=S[0].length;
  double xx1=0;
  double xx2=0;
  double y1,y2;
  double hk=0;
  double toplam=0;
  y1=0;y2=0;
  for(int k=0;k<(n-1);k++)
     { hk=(S[3][k+1]-S[3][k]);
       //System.out.println("x1="+S[3][k]+"x2="+S[3][k+1]+"a="+a+"b="+b);
      if(a>S[3][k+1])
      {toplam=0;//System.out.println("şart 1 toplam = "+toplam);
      }//şart1
	  else if(S[3][k]<=a && a<=S[3][k+1] && b>S[3][k+1])//şart 2 
       {
       xx1=(a-S[3][k]);
       xx2=(S[3][k+1]-a);
       y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       xx1=hk;
       xx2=0;
       y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       toplam+=(y2-y1);
       //System.out.println("şart 2toplam = "+toplam+"y1="+y1+"y2="+y2);
       }
       else if(S[3][k]<=a && a<=S[3][k+1] && S[3][k]<=b && b<=S[3][k+1])//şart 3 
       {
       xx1=(a-S[3][k]);
       xx2=(S[3][k+1]-a);
       y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       xx1=(b-S[3][k]);
       xx2=(S[3][k+1]-b);
       y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       toplam+=(y2-y1);
       //System.out.println("şart 3 toplam = "+toplam+"y1="+y1+"y2="+y2);
       }
       else if(a<S[3][k] && b>=S[3][k+1]) //şart 4
       {
       xx1=0;
       xx2=hk;
       y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       xx1=hk;
       xx2=0;
       y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
       toplam+=(y2-y1);
       //System.out.println("şart 4 toplam = "+toplam+"y1="+y1+"y2="+y2);
       }
       else if(S[3][k]<=b && b<=S[3][k+1])//şart 5
       {xx1=0;
        xx2=hk;
        y1=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
        xx1=(b-S[3][k]);
        xx2=(S[3][k+1]-b);
        y2=S[0][k]*xx1*xx1/2.0-S[1][k]*xx2*xx2/2.0+(xx1*xx1*xx1*xx1*S[2][k+1]-xx2*xx2*xx2*xx2*S[2][k])/(24.0*hk);
        toplam+=(y2-y1);
        //System.out.println("şart 5 toplam = "+toplam+"y1="+y1+"y2="+y2);
        }
        else break;
     }
  return toplam;
  }
    
  public static double intSpline(double xi[],double yi[],double c0,double cn,double a,double b)
  {double S[][]=cubic_spline(xi,yi,c0,cn);	
   return intSpline(S,a,b);
  }
   //ideal gaz specific heat 
   public double Cpl(double t)
   {return funcSpline(cpl,t);}
   
   public double dCpldT(double t)
   {return dfSpline(cpl,t);}
   
   public double integralCpl(double t1,double t2)
   {return intSpline(cpl,t1,t2);}
 
   public double Cpv(double t)
   {return funcSpline(cpv,t);}
   
   public double dCpvdT(double t)
   {return dfSpline(cpv,t);}
   
   public double integralCpv(double t1,double t2)
   {return intSpline(cpv,t1,t2);}
     
   public double Psb(double t)
   {return funcSpline(VPb,t)*1e3;}
   
   public void plot_Psb(int aradegersayisi)
   {   int n=a.length;
	   if(n==20){plotSpline(a[1],a[0],aradegersayisi);}
	   else     {plotSpline(a[0],a[1],aradegersayisi);}   
   }
   public void plot_hl(int aradegersayisi)
   {   int n=a.length;
	   if(n==20){plotSpline(a[1],a[5],aradegersayisi);}
	   else     {plotSpline(a[0],a[4],aradegersayisi);}   
   }
   public double dPsbdT(double t)
   {return dfSpline(VPb,t)*1e3;}
   
   public double Psd(double t)
   {return funcSpline(VPd,t)*1e3;}
   
   public double Tsb(double ps)
   {return funcSpline(VTb,ps*1e-3);}
   
   public double Tsd(double ps)
   {return funcSpline(VTd,ps*1e-3);}
      
   public double dPsddT(double t)
   {return dfSpline(VPd,t)*1e3;}
   
   public double rol(double t)
   {return  funcSpline(rol,t);}
   
   public double vl(double t)
   {return 1.0/rol(t);}
   
   public double rov(double t)
   {return funcSpline(rov,t);}
   
   public double vv(double t)
   {return 1.0/rov(t);}
   
   public double viscosity_l(double t)
   {return funcSpline(visl,t)*1.0e-6;}
   
   public double viscosity_v(double t)
   {return funcSpline(visv,t)*1.0e-6;}
   
   public double k_l(double t)
   {return funcSpline(kl,t)*1e-3;}
   
   public double k_v(double t)
   {return funcSpline(kv,t)*1e-3;}
   
   public double h_l(double t)
   {return funcSpline(hl,t);}
   
   public double h_v(double t)
   {return funcSpline(hv,t);}
   
   public double h_lv(double t)
   {return h_v(t)-h_l(t);}
   
   public double s_l(double t)
   {return funcSpline(sl,t);}
   
   public double s_v(double t)
   {return funcSpline(sv,t);}
   
   public double s_lv(double t)
   {return s_v(t)-s_l(t);}
   
   public double soundv_l(double t)
   {return funcSpline(soundvl,t);}
   
   public double soundv_v(double t)
   {return funcSpline(soundvv,t);}
   
   public double yuzeygerilim(double t)
   {return funcSpline(st,t)*1e-3;}
}
