public class MBWR_CS_Data extends Object
{
//this class reads and store Modified benedict Webb Rubin  data
public String refrigerantName;
public String openName;
public double a[][];
public double mf,mf1;
public double M,R,BP,Tc,Pc,dc,Vc;
public double B[];
public double aVP[][];
public double adf[];
public double aa[];
public double Tr,hr,sr,hlgr,slgr;
public double G[][]; // ideal gas heat capacitiy
public double E[][]; // saturated liquid density
public double F[][]; // saturated vapor density
public double vl[][]; //saturated liquid viscosity
public double vv[][]; //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 hg[][]; //saturated vapour enthalpy
public double sl[][]; //saturated liquid enthalpy
public double sg[][]; //saturated vapour enthalpy
public double svl[][]; //saturated liquid enthalpy
public double svg[][]; //saturated vapour enthalpy

   public MBWR_CS_Data(String refrigerantNamei,String openNamei,double mfi,double mf1i,
   double Mi,double Ri,double BPi,
   double Tci,double Pci, double dci,double vci,
   double b1,double b2, double b3, double b4, double b5, double b6, double b7, double b8, double b9, double b10,
   double b11,double b12, double b13, double b14, double b15, double b16, double b17, double b18, double b19, double b20,
   double b21,double b22, double b23, double b24, double b25, double b26, double b27, double b28, double b29, double b30,
   double b31,double b32,
   double tri,double hri,double sri,double hlgi,double slgi)
   {
   refrigerantName=refrigerantNamei;
   a=Text.readDoubleT(refrigerantName+".txt");
   //System.out.println(Matrix.toString(a));
   openName=openNamei;
   Tr=tri;hr=hri;sr=sri;hlgr=hlgi;slgr=slgi;
   mf=mfi;
   mf1=mf1i;
   M=Mi;
   R=Ri;
   BP=BPi;
   Tc=Tci;Pc=Pci;dc=dci;Vc=vci;
   B=new double[32];
   int i,j;
   aVP=cubic_spline(a[0],a[1],0.0,0.0);
   G=cubic_spline(a[0],a[8],0.0,0.0);
   vl=cubic_spline(a[0],a[13],0.0,0.0);
   vv=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);
   E=cubic_spline(a[0],a[2],0.0,0.0);
   double ai8[]=new double[a[2].length];
   for(i=0;i<ai8.length;i++) {ai8[i]=1.0/a[3][i];}
   F=cubic_spline(a[0],ai8,0.0,0.0);
   hl=cubic_spline(a[0],a[4],0.0,0.0);
   hg=cubic_spline(a[0],a[5],0.0,0.0);
   sl=cubic_spline(a[0],a[6],0.0,0.0);
   sg=cubic_spline(a[0],a[7],0.0,0.0);
   svl=cubic_spline(a[0],a[11],0.0,0.0);
   svg=cubic_spline(a[0],a[11],0.0,0.0);
   B[0]=b1;B[1]=b2;B[2]=b3;B[3]=b4;B[4]=b5;B[5]=b6;B[6]= b7;B[7]= b8;B[8]=b9;B[9]=b10;
   B[10]=b11;B[11]=b12;B[12]=b13;B[13]=b14;B[14]=b15;B[15]=b16;B[16]= b17;B[17]= b18;B[18]=b19;B[19]=b20;
   B[20]=b21;B[21]=b22;B[22]=b23;B[23]=b24;B[24]=b25;B[25]=b26;B[26]= b27;B[27]= b28;B[28]=b29;B[29]=b30;
   B[30]=b31;B[31]=b32;
   }
   
  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;
 }
 // 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);
  }

   public double Cp(double t)
   {return funcSpline(G,t);}
   
   public double dCpdT(double t)
   {return dfSpline(G,t);}
   
   public double integralCp(double t1,double t2)
   {return intSpline(G,t1,t2);}
   
   public double Ps(double t)
   {return funcSpline(aVP,t)*1e3;}
   public double dPsdT(double t)
   {return dfSpline(aVP,t)*1e3;}
   
   public double rol(double t)
   {return  funcSpline(E,t);}
   
   public double vl(double t)
   {return 1.0/rol(t);}
   
   public double rog(double t)
   {return funcSpline(F,t);}
   
   public double vg(double t)
   {return 1.0/rog(t);}
   
   public double viscosity_l(double t)
   {return funcSpline(vl,t)*1.0e-6;}
   
   public double viscosity_g(double t)
   {return funcSpline(vv,t)*1.0e-6;}
   
   public double k_l(double t)
   {return funcSpline(kl,t)*1e-3;}
   
   public double k_g(double t)
   {return funcSpline(kv,t)*1e-3;}
   
   public double h_l(double t)
   {return funcSpline(hl,t);}
   
   public double h_g(double t)
   {return funcSpline(hg,t);}
   
   public double h_lg(double t)
   {return h_g(t)-h_l(t);}
   
   public double s_l(double t)
   {return funcSpline(sl,t);}
   
   public double s_g(double t)
   {return funcSpline(sg,t);}
   
      public double sv_l(double t)
   {return funcSpline(svl,t);}
   
   public double sv_g(double t)
   {return funcSpline(svg,t);}
}
