public class liquid_CS_Data extends Object
{
//this class reads and store liquid thermophysical properties data
public double a[][];
public String liquidName;
public String openName;
public String ekbilgi;

public double M;       // kg/kmol
public double h0;      // KJ/kmol at temperature a[0][0]=Tref
public double s0;      // KJ/kmolK at temperature a[0][0]=Tref
public double cpl[][];  // ideal gas heat capacitiy
public double rol[][];  // saturated liquid density
public double visl[][]; // saturated liquid viscosity
public double kl[][]; // saturated liquid thermal conductivity
// order of data
// a[0] temperature degree K
// a[1] ro density kg/m^3
// a[2] Cpl liquid specific heat
// a[3] ml viscosity
//a [4] kl thermal conductivity 
   public liquid_CS_Data(String liquidNamei,String openNamei,double ai[][])
   {
   M=18.0;
   h0=200.0;
   s0=1.0;
   liquidName=liquidNamei;
   a=ai;
   ekbilgi=" ";
   openName=openNamei;
   int n=a.length;
   int i,j;  
   cpl=cubic_spline(a[0],a[2],0.0,0.0);
   rol=cubic_spline(a[0],a[1],0.0,0.0);
   visl=cubic_spline(a[0],a[3],0.0,0.0);   
   kl=cubic_spline(a[0],a[4],0.0,0.0);
   }	   
	   
   public liquid_CS_Data(String liquidNamei,String openNamei,double ai[][],double Mi, double h0i,double s0i)
   {
   liquidName=liquidNamei;
   a=ai;
   ekbilgi=" ";
   openName=openNamei;
   int n=a.length;
   int i,j; 
   M=Mi;
   h0=h0i;
   s0=s0i;  
   cpl=cubic_spline(a[0],a[2],0.0,0.0);
   rol=cubic_spline(a[0],a[1],0.0,0.0);
   visl=cubic_spline(a[0],a[3],0.0,0.0);   
   kl=cubic_spline(a[0],a[4],0.0,0.0);
   }
   
   public void ekbilgi(String s)
   {ekbilgi=s;}
  //band matrix solution for cubic spline applications 
  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;      
  }
  //cubic spline curve fitting  
  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;    	 	     	 
  }
  //cubic spline curve fitting function
  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;
  }    
 //Cubic spline interpolation output   
 public static double[][]  funcSpline(double xi[],double yi[],int numberofmidpoints)
 {
	//numberofmidpoints:in  x--o--o--x--o--o--x if x is x 
	// are experimental points number of midpoints are 2
	int n=xi.length;
	int nn=(n-1)*(numberofmidpoints+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<numberofmidpoints;j++) 
             {dx=(xi[i+1]-xi[i])/((double)numberofmidpoints+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;
 }
 // derivative formulas
// =================       
  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)
  {
    //cubic spline derivative formula
  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)
 {
    //natural cubic spline derivative formula 
	double S[][]=cubic_spline(xi,yi,0,0);	
    return dfSpline(S,x);
 } 
  
  public static double intSpline(double S[][],double a,double b)
  {
  //cubic spline function integral
  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]);
      if(a>S[3][k+1])
      {toplam=0;
      }//condition 1
	  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);
       }
       else if(S[3][k]<=a && a<=S[3][k+1] && S[3][k]<=b && b<=S[3][k+1])//şart 3 
       { //condition 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);
       }
       else if(a<S[3][k] && b>=S[3][k+1]) //condition 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);
       }
       else if(S[3][k]<=b && b<=S[3][k+1])//condition 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 s0(double S[][],double a,double b)
  {
  // cubic spline function integral of (S(x)/x)
  int n=S[0].length;
  double xx1=0;  //x
  //ak S[0][k]
  //bk S[1][k]
  //ck S[2][k]
  //xk s[3][k]
  double xx2=0;  
  double y1,y2;
  double hk=0;
  double toplam=0;
  y1=0;y2=0;
  double xk1=0,xk=0,ak=0,bk=0,ck=0,ck1=0;
  for(int k=0;k<(n-1);k++)
     { hk=(S[3][k+1]-S[3][k]);
       xk=S[3][k];
       xk1=S[3][k+1];
       ak=S[0][k];
       bk=S[1][k];
       ck=S[2][k];
       ck1=S[2][k+1];
      if(a>xk1)
      {toplam=0;
      }//şart1
	  else if(xk<=a && a<=xk1 && b>xk1)//şart 2 
       {
       xx1=(xk1-a);
       xx2=(xk1/a);
       y1=(ak-bk)*xx1+(bk*xk1-ak*xk)*Math.log(xx2);
       xx1=xk1;
       xx2=a;
       y1+=ck1*((xx1*xx1*xx1-xx2*xx2*xx2)/3.0-
       1.5*(xx1*xx1-xx2*xx2)*xk+
       3.0*xk*xk*(xx1-xx2)-
       xk*xk*xk*Math.log(xx1/xx2))/6/hk;
       y1+=ck*(xk1*xk1*xk1*Math.log(xx1/xx2)-
       3.0*xk1*xk1*(xx1-xx2)+1.5*xk1*(xx1*xx1-xx2*xx2)-
       1.0/3.0*(xx1*xx1*xx1-xx2*xx2*xx2))/6/hk;
       toplam+=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=(b-a);
       xx2=(b/a);
        y1=(ak-bk)*xx1+(bk*xk1-ak*xk)*Math.log(xx2);
       xx1=b;
       xx2=a;
       y1+=ck1*((xx1*xx1*xx1-xx2*xx2*xx2)/3.0-
       1.5*(xx1*xx1-xx2*xx2)*xk+
       3.0*xk*xk*(xx1-xx2)-
       xk*xk*xk*Math.log(xx1/xx2))/6/hk;
       y1+=ck*(xk1*xk1*xk1*Math.log(xx1/xx2)-
       3.0*xk1*xk1*(xx1-xx2)+1.5*xk1*(xx1*xx1-xx2*xx2)-
       1.0/3.0*(xx1*xx1*xx1-xx2*xx2*xx2))/6/hk;
       toplam+=y1;
       }
       else if(a<S[3][k] && b>=S[3][k+1]) //şart 4
       {
       xx1=(xk1-xk);
       xx2=(xk1/xk);
        y1=(ak-bk)*xx1+(bk*xk1-ak*xk)*Math.log(xx2);
       xx1=xk1;
       xx2=xk;
       y1+=ck1*((xx1*xx1*xx1-xx2*xx2*xx2)/3.0-
       1.5*(xx1*xx1-xx2*xx2)*xk+
       3.0*xk*xk*(xx1-xx2)-
       xk*xk*xk*Math.log(xx1/xx2))/6/hk;
       y1+=ck*(xk1*xk1*xk1*Math.log(xx1/xx2)-
       3.0*xk1*xk1*(xx1-xx2)+1.5*xk1*(xx1*xx1-xx2*xx2)-
       1.0/3.0*(xx1*xx1*xx1-xx2*xx2*xx2))/6/hk;
       toplam+=y1;
       }
       else if(S[3][k]<=b && b<=S[3][k+1])//şart 5
       {       xx1=(b-xk);
       xx2=(b/xk);
       y1=(ak-bk)*xx1+(bk*xk1-ak*xk)*Math.log(xx2);
       xx1=b;
       xx2=xk;
       y1+=ck1*((xx1*xx1*xx1-xx2*xx2*xx2)/3.0-
       1.5*(xx1*xx1-xx2*xx2)*xk+
       3.0*xk*xk*(xx1-xx2)-
       xk*xk*xk*Math.log(xx1/xx2))/6/hk;
       y1+=ck*(xk1*xk1*xk1*Math.log(xx1/xx2)-
       3.0*xk1*xk1*(xx1-xx2)+1.5*xk1*(xx1*xx1-xx2*xx2)-
       1.0/3.0*(xx1*xx1*xx1-xx2*xx2*xx2))/6/hk;
       toplam+=y1;
        }
        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);
  }
//specific heat of liquid   
   public double Cp_l(double t)
   { return funcSpline(cpl,t);}     
   
   public double Cpl(double t)
   { return funcSpline(cpl,t);} 
       
   public double Cp(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);}
   
//enthalpy of liquid   
   public double dh(double t1,double t2)
   {return intSpline(cpl,t1,t2);}
   
   public double h_l(double t)
   {return intSpline(cpl,a[0][0],t)+h0;}
   public double hl(double t)
   {return intSpline(cpl,a[0][0],t)+h0;}
//entropy of liquid   
   public double s_l(double t)
   {return s0(cpl,a[0][0],t)+s0;}
   public double sl(double t)
   {return s0(cpl,a[0][0],t)+s0;}
   public double ro_l(double t)
   {return  funcSpline(rol,t);}
   public double rol(double t)
   {return  funcSpline(rol,t);}
   public double v_l(double t)
   {return 1.0/ro_l(t);}
   public double vl(double t)
   {return 1.0/ro_l(t);}
   public double viscosity_l(double t)
   {return funcSpline(visl,t);}
// thermal conductivity of liquid
   public double k_l(double t)
   {return funcSpline(kl,t);}
   public double kl(double t)
   {return funcSpline(kl,t);}
   //Prandtl number of liquid
   public double Pr_l(double t)
   {return Cp_l(t)*1e3*viscosity_l(t)/k_l(t);}
   public double Pr(double t)
   {return Cp_l(t)*1e3*viscosity_l(t)/k_l(t);} 
   public double Prl(double t)
   {return Cp_l(t)*1e3*viscosity_l(t)/k_l(t);}
//Thermal diffusivity of liquid   
   public double alfa_l(double t)
   {return k_l(t)/ro_l(t)/(Cp_l(t)*1e3);}  
   public double alfal(double t)
   {return k_l(t)/ro_l(t)/(Cp_l(t)*1e3);}
   

     
   
}
