public class iciceisidegistirici1
{ liquid_CS icsivi;
  liquid_CS dissivi;
  double D;
  double di;
  double dd;
  double mi;
  double md;
  double Th[],Tc[],x[];
  double L;
  double dx;
  int N;
  double Q;
  double Ric,Rdis,kboru;
  double Pic,Pdis;
 public iciceisidegistirici1(String ic,String dis,double D_i,
 double di_i,double dd_i,double mi_i,double md_i,double Li,int Ni)
 {icsivi=new liquid_CS(ic);
  dissivi=new liquid_CS(dis);
  D=D_i;
  L=Li;
  N=Ni;
  Th=new double[N+1];
  Tc=new double[N+1];
  x=new double[N+1];
  dx=L/N;
  di=di_i;
  dd=dd_i;
  mi=mi_i;
  md=md_i;
  kboru=15.1;//paslanmaz çelik
  Ric=1e-4;//kirlilik direnci
  Rdis=1e-4;//kirlilik direnci
 }
 public  double f(double Re,double eod)
 {
 //Goudar equation 
 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 Vic(double t)
 {double A=Math.PI*di*di/4.0;
  double ro=icsivi.ro(0.0,t);
  double V=mi/(A*ro);
  return V;
 }
 public double Reic(double t)
 {double V=Vic(t);
  double ro=icsivi.ro(0.0,t);
  double mu=icsivi.vis(0.0,t);
  double Re=ro*V*D/mu;
  return Re;
 }
 public double hic(double t)
 { double Re=Reic(t);
   double Pr=icsivi.Pr(0.0,t);
   double e=46e-6;
   double eod=e/di;
   double f=f(Re,eod);
   double Nu=f/8.0*(Re-1000.0)*Pr/
   (1.0+12.7*Math.sqrt(f/8.0)*(Math.pow(Pr,(2/3))-1.0));
   double k=icsivi.k(0.0,t);
   double h=Nu*k/D;
   return h;
 }
 public double Vdis(double t)
 {double A=Math.PI*(D*D-dd*dd)/4.0;
  double ro=dissivi.ro(0.0,t);
  double V1=md/(A*ro);
  return V1;
 }
  public double Redis(double t)
 {double V=Vdis(t);
  double ro=dissivi.ro(0.0,t);
  double mu=dissivi.vis(0.0,t);
  double Dh=D-dd;
  double Re=ro*V*Dh/mu;
  return Re;
 }
  public double hdis(double t)
 { double Re=Redis(t);
   double Pr=dissivi.Pr(0.0,t);
   double e=46e-6;
   double Dh=D-dd;
   double eod=e/Dh;
   double f=f(Re,eod);
   double Nu=f/8.0*(Re-1000.0)*Pr/
   (1.0+12.7*Math.sqrt(f/8.0)*(Math.pow(Pr,(2/3))-1.0));
   double k=dissivi.k(0.0,t);
   double h=Nu*k/D;
   return h;
 }
 public double U(double tdis,double tic)
 {double hdis=hdis(tdis);
  double hic=hic(tic);
  double R=dd/(hic*di)+Ric*dd/di+Math.log(dd/di)*dd/(2.0*kboru)+Ric+1.0/hdis;
  double U=1.0/R;
  return U; 
 }
 public void hesapla(double Tc0,double Th0)
 {Th[0]=Th0;
  Tc[0]=Tc0;
  x[0]=0;
  double dQ=0;
  Q=0;
  double dA=Math.PI*dd*dx;
  double Reic,Redis,Vic,Vdis,roic,rodis,dPic,dPdis,fic,fdis;
  double e=46e-6;
  double Dh=D-dd;
  double eod_dis=e/Dh;
  double eod_ic=e/di;
  Pic=0;Pdis=0;
  for(int i=0;i<N;i++)
  {dQ=U(Tc[i],Th[i])*dA*(Th[i]-Tc[i]);
   fdis=f(Redis(Tc[i]),eod_dis);
   fic= f(Reic(Th[i]),eod_ic);
   roic=icsivi.ro(0.0,Th[i]);
   rodis=dissivi.ro(0.0,Tc[i]);
   Vic=Vic(Th[i]);
   Vdis=Vdis(Tc[i]);
   System.out.println("fdis="+fdis+"fic="+fic+"Redis(Tc[i])"+Redis(Tc[i])+"Reic(Th[i])"+Reic(Th[i])+"Vic="+Vic+"Vdis="+Vdis);
   dPdis=fdis*dx/Dh*rodis*Vdis*Vdis/2;
   dPic=fic*dx/di*roic*Vic*Vic/2;
   Pdis+=dPdis;
   Pic+=dPic;
   Q+=dQ;   
   Th[i+1]=Th[i]-dQ/(mi*icsivi.Cp(0.0,Th[i])*1e3);
   Tc[i+1]=Tc[i]+dQ/(md*dissivi.Cp(0.0,Tc[i])*1e3);
   x[i+1]=dx*(i+1);
  }
 }
 public String toString()
 {String s="İç içe borulu ısı değiştirici";
  s+="ısı değitirici dış çapı = "+D+"\n";
  s+="ısı değitirici iç boru dış çapı = "+dd+"\n";
  s+="ısı değitirici iç boru iç çapı = "+di+"\n";
  s+="ısı değitirici dış boru debisi = "+md+"kg/s\n";
  s+="ısı değitirici iç boru debisi = "+mi+"kg/s\n";
  s+="ısı değitirici dış basınç düşümü = "+Pdis+"Pa\n";
  s+="ısı değitirici iç basınç düşümü = "+Pic+"Pa\n";
  s+="ısı değitirici ısı transferi = "+Q+"kW/s\n";
  return s;
 }
  public void hesapla1(double TcN,double Th0)
 {Th[0]=Th0;
  Tc[0]=TcN;
  x[0]=0;
  double dQ=0;
  Q=0;
  double dA=Math.PI*dd*dx;
  for(int i=0;i<N;i++)
  {dQ=U(Tc[i],Th[i])*dA*(Th[i]-Tc[i]);
   Q+=dQ;   
   Th[i+1]=Th[i]-dQ/(mi*icsivi.Cp(0.0,Th[i])*1e3);
   Tc[i+1]=Tc[i]-dQ/(md*dissivi.Cp(0.0,Tc[i])*1e3);
   x[i+1]=dx*(i+1);
  }
 }
 public void hesapla_ters(double Tc0,double Th0)
 {Th[0]=Th0;
  Tc[0]=Tc0+2;
  double xx[],yy[];
  xx=new double[20];
  yy=new double[20];
  double dTc=((Th0-2)-Tc[0])/20.0;
  for(int i=0;i<20;i++)
  {  yy[i]=Tc[0];
	 hesapla1(Tc[0],Th0);
	 xx[i]=Tc[N];
	 Tc[0]+=dTc;
  }
  double S[][]=NA49.cubic_spline(xx,yy,0.0,0.0);
  double Tci=NA49.funcSpline(S,Tc0);
  hesapla1(Tci,Th0);
  }
  
 public void plot()
 {Plot p=new Plot(x,Th);
  p.addData(x,Tc);
  p.plot();
 }
 public static void main(String arg[])
 {     double L=20.0;
       int N=1000;
     //(String ic,String dis,double D_i,
     //double di_i,double dd_i,double mi_i,double md_i,double Li,int Ni)
	 iciceisidegistirici1 id=new iciceisidegistirici1("su","su",0.12,0.08,0.077,0.5,0.5,L,N);
    id.hesapla(20.0,90.0);
    System.out.println(id.toString());
    id.plot();
 }
}