class f_B_Spline extends f_x
{
  double a[];
  double ti[];
  double yi[];   
  public f_B_Spline(double ai[][],int n,int m)
  {	  
  ti=new double[ai[n].length]; 
  yi=new double[ai[m].length];
  for(int i=0;i<ai[n].length;i++) {ti[i]=ai[n][i];yi[i]=ai[m][i];} 
  a=B_Spline(ti,yi);
  }
  
  public f_B_Spline(double ti1[],double yi1[])
  {	
  ti=new double[ti1.length]; 
  yi=new double[yi1.length];
  for(int i=0;i<ti1.length;i++) {ti[i]=ti1[i];yi[i]=yi1[i];} 
  a=B_Spline(ti,yi);
  }
  
  public double func(double x)
  {
  double h[]=hi(ti);
  int i;
  int n=ti.length-1;
  double nn=n+2;
  double d,e;
  for(i=(n-1);i>=1;i--)
    {if((x-ti[i])>=0) break;}
  i++;
  d=(a[i+1]*(x-ti[i-1])+a[i]*(ti[i]-x+h[i+1]))/(h[i]+h[i+1]);
  e=(a[i]*(x-ti[i-1]+h[i-1])+a[i-1]*(ti[i-1]-x+h[i]))/(h[i-1]+h[i]);
  double bs=(d*(x-ti[i-1])+e*(ti[i]-x))/h[i];
  return bs;
  }
   
  public static double[] hi(double ti[])
  {int n=ti.length-1;//ti 0..n
  int nn=n+2;
  double h[]=new double[nn];
  for(int i=1;i<=n;i++)
     {h[i]=ti[i]-ti[i-1];
     }
  h[0]=h[1];
  h[n+1]=h[n];
  return h;
  }

  public static double [] B_Spline(double ti[],double yi[])
  { //katsayıların hesabı
  int n=ti.length-1;
  int nn=n+2;
  int i;
  double delta,gamma,p,q,r;
  double a[]=new double[nn];
  double h[]=new double[nn];
  h=hi(ti);
  delta=-1;
  gamma=2.0*yi[0];
  p=delta*gamma;
  q=2.0;
  for(i=1;i<=n;i++)
  {r=h[i+1]/h[i];
  delta=-r*delta;
  gamma=-r*gamma+(r+1)*yi[i];
  p=p+gamma*delta;
  q=q+delta*delta;
  }
  a[0]=-p/q;
  for(i=1;i<=(n+1);i++)
  {a[i]=((h[i-1]+h[i])*yi[i-1]-h[i]*a[i-1])/h[i-1];}
  return a;
 }

 public  void  B_Spline_plot(int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=ti.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=ti[i];z[1][k]=func(z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(ti[i+1]-ti[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=func(z[0][k]);k++;}
       }
       z[0][k]=ti[i];z[1][k]=func(z[0][k]);
       Plot pp=new Plot(z[0],z[1]);
       pp.setPlotType(0,0); 
       pp.addData(ti,yi);
       pp.setPlotType(1,24);
       //pp.setPlabel("R134a");
       //pp.setXlabel("T derece C");
       //pp.setYlabel("h ");
       pp.plot();   
    //return z;
 }    
}

public class ref_BS_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 f_B_Spline VPb; // buhar basıncı (buble point)
public f_B_Spline VPd; // buhar basıncı (dew point)
public f_B_Spline VTb; // buhar sıcaklığı (buble point)
public f_B_Spline VTd; // buhar sıcaklığı (dew point) 
public f_B_Spline cpl;  // ideal gas heat capacitiy
public f_B_Spline cpv;  // ideal gas heat capacitiy
public f_B_Spline rol;  // saturated liquid density
public f_B_Spline rov;  // saturated vapor density
public f_B_Spline visl; // saturated liquid viscosity
public f_B_Spline visv; // saturated vapor viscosity
public f_B_Spline kl; // saturated liquid thermal conductivity
public f_B_Spline kv; // saturated vapor thermal conductivity
public f_B_Spline st; // saturated liquid-vapor interphase surface tension
public f_B_Spline hl; // saturated liquid enthalpy
public f_B_Spline hv; // saturated vapour enthalpy
public f_B_Spline sl; // saturated liquid enthalpy
public f_B_Spline sv; // saturated vapour enthalpy
public f_B_Spline soundvl;// liquid sound velocity
public f_B_Spline soundvv;// vapor sound velocity

   public ref_BS_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=new f_B_Spline(a,1,0);
   VPd=new f_B_Spline(a,2,0);
   
   VTb=new f_B_Spline(a,0,1);
   VTd=new f_B_Spline(a,0,2);
   
   cpl=new f_B_Spline(a,1,9);
   cpv=new f_B_Spline(a,2,10);
   
   visl=new f_B_Spline(a,1,14);
   visv=new f_B_Spline(a,2,15);
   
   kl=new f_B_Spline(a,1,16);
   kv=new f_B_Spline(a,2,17);
   
   st=new f_B_Spline(a,1,18);
   
   rol=new f_B_Spline(a,1,3);
   
   double ai8[]=new double[a[4].length];
   for(i=0;i<ai8.length;i++) {ai8[i]=1.0/a[4][i];}
   rov=new f_B_Spline(a[1],ai8);
   
   hl=new f_B_Spline(a,1,5);
   hv=new f_B_Spline(a,2,6);
   
   sl=new f_B_Spline(a,1,7);
   sv=new f_B_Spline(a,2,8);
   
   soundvl=new f_B_Spline(a,1,12);
   soundvv=new f_B_Spline(a,2,13);
   }
   else
   {
   VPb=new f_B_Spline(a,0,1);
   VPd=VPb;
   VTb=new f_B_Spline(a,1,0);
   VTd=VTb;
   cpl=new f_B_Spline(a,0,8);
   cpv=new f_B_Spline(a,0,9);   
   visl=new f_B_Spline(a,0,13);
   visv=new f_B_Spline(a,0,14);   
   kl=new f_B_Spline(a,0,15);
   kv=new f_B_Spline(a,0,16);
   st=new f_B_Spline(a,0,17);   
   rol=new f_B_Spline(a,0,2);   
   double ai8[]=new double[a[3].length];
   for(i=0;i<ai8.length;i++) {ai8[i]=1.0/a[3][i];}
   rov=new f_B_Spline(a[0],ai8); 
   hl=new f_B_Spline(a,0,4);
   hv=new f_B_Spline(a,0,5);   
   sl=new f_B_Spline(a,0,6);
   sv=new f_B_Spline(a,0,7);   
   soundvl=new f_B_Spline(a,0,8);
   soundvv=new f_B_Spline(a,0,12);
   }
   }
   
   public void ekbilgi(String s)
   {ekbilgi=s;}
   
   public double Cpv(double t)
   {return cpv.func(t);}
   public void plot_Cpv(int aradegersayisi)
   {cpv.B_Spline_plot(aradegersayisi);}
   //ideal gaz specific heat 
           
   public double Psb(double t)
   {return VPb.func(t)*1e3;}
   
   public void plot_Psb(int aradegersayisi)
   {VPb.B_Spline_plot(aradegersayisi);}   
   
   public double Psd(double t)
   {return VPd.func(t)*1e3;}
   
   public void plot_Psd(int aradegersayisi)
   {VPb.B_Spline_plot(aradegersayisi);}
   
   public void plot_hl(int aradegersayisi)
   {hl.B_Spline_plot(aradegersayisi);}   
   
   public void plot_hv(int aradegersayisi)
   {hv.B_Spline_plot(aradegersayisi);}
           
   public double Tsb(double ps)
   {return VTb.func(ps)*1e-3;}
   
   public double Tsd(double ps)
   {return VTd.func(ps)*1e-3;}
      
   public double dPsddT(double t)
   {return VPd.func(t)*1e3;}
   
   public double rol(double t)
   {return  rol.func(t);}
   
   public double vl(double t)
   {return 1.0/rol(t);}
   
   public double rov(double t)
   {return rov.func(t);}
   
   public double vv(double t)
   {return 1.0/rov(t);}
   
   public double viscosity_l(double t)
   {return visl.func(t)*1.0e-6;}
   
   public double viscosity_v(double t)
   {return visv.func(t)*1.0e-6;}
   
   public double k_l(double t)
   {return kl.func(t)*1e-3;}
   
   public double k_v(double t)
   {return kv.func(t)*1e-3;}
   
   public double h_l(double t)
   {return hl.func(t);}
   
   public double h_v(double t)
   {return hv.func(t);}
   
   public double h_lv(double t)
   {return h_v(t)-h_l(t);}
   
   public double s_l(double t)
   {return sl.func(t);}
   
   public double s_v(double t)
   {return sv.func(t);}
   
   public double s_lv(double t)
   {return s_v(t)-s_l(t);}
   
   public double soundv_l(double t)
   {return soundvl.func(t);}
   
   public double soundv_v(double t)
   {return soundvv.func(t);}
   
   public double yuzeygerilim(double t)
   {return st.func(t)*1e-3;}
   
   public double Cpl(double t)
   {return cpl.func(t);}
}
