public class iciceboruid
{ liquid_CS boru_ici_sivi;   //(boru içi)
  liquid_CS boru_disi_sivi;  //(boru dışı)
  double d0,di; //iç boru için
  double D;     //dış boru çapı
  double L; //borunun boyu
  double mi,md;//kütlesel debi;
  double k;//ısı iletim katsayısı
  double eps;
  double tsoguk[];
  double tsicak[];
  double x[];
  double dL;
  double dh;
  double Q;
  double Ri,R0;//boru içi ve dışı dirençler;
  double dPi,dPd;//basınç düşümü;
  int N; //toplam boyun kaç parçaya bölündüğü
  public iciceboruid(String sivi1, String sivi2,double mii,double mdi,double d0i,double dii,double Di,double Li,double dLi)
  {d0=d0i;di=dii;D=Di;L=Li;mi=mii;md=mdi;
   dL=dLi; 
   N=(int)(L/dLi+0.000001);
   tsoguk=new double[N+1];
   tsicak=new double[N+1];
   x=new double[N+1];
   eps=45e-6;
   k=54.0;
   Ri=0.000176;
   R0=0.000352;
   boru_ici_sivi=new liquid_CS(sivi1);
   boru_disi_sivi=new liquid_CS(sivi2);
   dh=D-d0;
  }
  
  public double Vi(double t)
  {double ro=boru_ici_sivi.ro(0.0,t);
   double Ai=Math.PI*(di*di)/4.0;
   double v=mi/ro/Ai;
   return v;
  }
  
  //boru içi Reynold sayısı t derece C
  public double Rei(double t)
  {double mui=boru_ici_sivi.vis(0.0,t);
   double ro=boru_ici_sivi.ro(0.0,t);
   double Re=ro*Vi(t)*di/mui;
   return Re;
  }
  
 public  double f(double Re,double eod)
 {
 //Goudar Sonnad equation 
 // for Re>2100 and any e/D
 double a=2/Math.log(10);
 double b=eod/3.7;
 double d=Math.log(10.0)/5.02*Re;
 double s=b*d+Math.log(d);
 double q=Math.pow(s,(s/(s+1)));
 double g=b*d+Math.log(d/q);
 double z=Math.log(q/g);
 double dLA=(g/(g+1))*z;
 double dCFA=dLA*(1+z/2.0/((g+1)*(g+1)+(z/3)*(2.0*g-1)));
 double f1=a*(Math.log(d/q)+dCFA);
 f1=1.0/(f1*f1);
 return f1;
 }
 
 public  double dPi(double ti)
 {double Re=Rei(ti);
  double eod=eps/di;
  double f=f(Re,eod);
  double ro=boru_ici_sivi.ro(0.0,ti);
  double v=Vi(ti);
  double dP=dL/di*f*ro*v*v/2.0;
  return dP; 	
 }
  
 public double Nui(double t)
 {  double eod=eps/di;
    double Re=Rei(t);
	double f=f(Re,eod); 
	double Pr=boru_ici_sivi.Pr(0.0,t);
	double Nu=(f/8.0)*(Re-1000.0)*Pr;
	double A=1.0+12.7*Math.sqrt(f/8.0)*(Math.pow(Pr,(2.0/3.0))-1.0);
	Nu/=A;
	return Nu;
 }
 
 public double hi(double t)
 {  double Nu=Nui(t);
    double k=boru_ici_sivi.k(0.0,t);
    double h=Nu*k/di;
	return h;
 }
 //boru dışı ısı transferi
  
  public double Vd(double t)
  {double ro=boru_disi_sivi.ro(0.0,t);
   double Ad=Math.PI*(D*D-d0*d0)/4.0;
   double v=md/ro/Ad;
   return v;
  }
  
  public double Red(double t)
  {double mud=boru_disi_sivi.vis(0.0,t);
   double ro=boru_disi_sivi.ro(0.0,t);
   double Re=ro*Vd(t)*dh/mud;
   return Re;
  }
  public  double dPd(double td)
 {double Re=Red(td);
   double eod=eps/d0;
  double f=f(Re,eod);
  double ro=boru_disi_sivi.ro(0.0,td);
  double v=Vd(td);
  double dP=dL/d0*f*ro*v*v/2.0;
  return dP; 	
 }  
 public double Nud(double t)
 {  double eod=eps/dh;
    double Re=Red(t);
	double f=f(Re,eod); 
	double Pr=boru_disi_sivi.Pr(0.0,t);
	double Nu=(f/8.0)*(Re-1000.0)*Pr;
	double A=1.0+12.7*Math.sqrt(f/8.0)*(Math.pow(Pr,(2.0/3.0))-1.0);
	Nu/=A;
	return Nu;
 }
 public double hd(double t)
 {  double Nu=Nud(t);
    double k=boru_disi_sivi.k(0.0,t);
    double h=Nu*k/dh;
	return h;
 }
  
 public double U0(double ti,double td)
 { double Ai=Math.PI*di*di/4.0;
   double A0=Math.PI*d0*d0/4.0;
   double hi=hi(ti);
   double hd=hd(td);
   double U=A0*(1.0/(hi*Ai)+Ri/Ai+Math.log(d0/di)/(2.0*Math.PI*k*dL)+R0/A0+1.0/(hd*A0));
   //System.out.println("Vi="+Vi(ti)+"Vd="+Vd(td)+"hi="+hi+"hd="+hd+"ti="+ti+"td="+td+"U="+1.0/U);
   return 1.0/U;
 }
 
 public String toString()
 {String s="";
  s+="sıcak sıvı (dış boru) = "+boru_disi_sivi.liquidName+"\n";
  s+="soğuk sıvı  (iç boru)  = "+boru_ici_sivi.liquidName+"\n";
  s+="sıcak sıvı sıcaklıkları (dış boru)  = "+tsicak[0]+" "+tsicak[N]+"\n";
  s+="sıcak sıvı sıcaklıkları (dış boru)  = "+tsoguk[0]+" "+tsoguk[N]+"\n";
  s+="sıcak sıvı basınç düşümü (dış boru)  = "+dPd+" Pa\n";
  s+="soğuk sıvı basınç düşümü (iç boru)  = "+dPi+" Pa\n";
  s+="Isı transferi = "+Q+"Watt \n";
  s+="Yüzey alanı = "+Math.PI*d0*L+" m^2 \n";
  return s;
 }
 
 public void hesapla_paralel(double ti, double td)
 { 
   tsicak[0]=td;
   tsoguk[0]=ti;
   double dA0=Math.PI*d0*dL;
   double dQ;
   Q=0;
   double Cpi,Cpd;
   x[0]=0;
   double dpi=0,dpd=0;
   for(int i=0;i<N;i++)
   {dQ=dA0*U0(tsoguk[i],tsicak[i])*(tsicak[i]-tsoguk[i]);
    Cpd=boru_disi_sivi.Cp(0.0,tsicak[i])*1.0e3; 
    Cpi=boru_ici_sivi.Cp(0.0,tsoguk[i])*1.0e3; 
    tsicak[i+1]=tsicak[i]-dQ/(md*Cpd);
    tsoguk[i+1]=tsoguk[i]+dQ/(mi*Cpi);
    dpi+=dPi(tsoguk[i]);
    dpd+=dPd(tsicak[i]);
    x[i+1]=x[i]+dL;
    //System.out.println("dQ="+dQ+"tsic="+tsicak[i]+"tsoguk="+tsoguk[i]);
    Q+=dQ;
   }
   dPi=dpi;
   dPd=dpd; 
 }
 
 public double hesapla_seri1(double ti1, double td)
 { 
   tsicak[0]=td;
   tsoguk[0]=ti1;
   double dA0=Math.PI*d0*dL;
   double dQ;
   Q=0;
   double Cpi,Cpd;
   x[0]=0;
   double dpi=0,dpd=0;
   for(int i=0;i<N;i++)
   {dQ=dA0*U0(tsoguk[i],tsicak[i])*(tsicak[i]-tsoguk[i]);
    Cpd=boru_disi_sivi.Cp(0.0,tsicak[i])*1.0e3; 
    Cpi=boru_ici_sivi.Cp(0.0,tsoguk[i])*1.0e3; 
    tsicak[i+1]=tsicak[i]-dQ/(md*Cpd);
    tsoguk[i+1]=tsoguk[i]-dQ/(mi*Cpi);
    dpi+=dPi(tsoguk[i]);
    dpd+=dPd(tsicak[i]);
    x[i+1]=x[i]+dL;
    //System.out.println("dQ="+dQ+"tsic="+tsicak[i]+"tsoguk="+tsoguk[i]);
    Q+=dQ;
   }
   dPi=dpi;
   dPd=dpd; 
   return tsoguk[N];
 }
 public void hesapla_seri(double ti, double td)
 { double xx[]=new double[11];
   double yy[]=new double[11];
   double ti1=ti+5;
  for(int i=0;i<10;i++)
  {xx[i]=ti1+(td-ti1)/10.0*i;
   yy[i]=hesapla_seri1(xx[i],td);
   System.out.println("i="+i+"  "+xx[i]+" "+yy[i]);
  }
  double aa[][]=cubic_spline(yy,xx,0.0,0.0);
  ti1=funcSpline(aa,ti);
  hesapla_seri1(ti1,td);
   //System.out.println("ti="+ti+"ti1="+ti1);
 }
 
   public static double [] thomas(double f[],double e[],double g[],double r[])
  {
  // 3 band matrix system of equation solving algorithm
  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 void plot()
 {Plot pp=new Plot(x,tsoguk);
  pp.addData(x,tsicak);
  pp.setPlabel("iç içe iki boru ısı değiştirici");
  pp.setXlabel("boy L m");
  pp.setYlabel("Sıcaklık derece C");
  pp.plot();
 }
  	
}