import javax.swing.JOptionPane;
import java.util.Locale;

public class recuperator1
{ // yüksek kanatlı boru ısı değiştirici
  Gas hot_sivi; // soğuk sıvı (boru içi)
  Gas cold_sivi;      // sıcak sıvı (boru dışı)
  double Thot[][];
  double Tcold[][];
  double x[];      // ısı değiştirici boy koordinatı
  double y[];      //ısı değiştirici en koordinatı
  double Q;        // ısı transferi
  double Qlmtd;    // ısı transferi LMTD metodu
  double LMTD;
  double KL;       // yerel basınç düşüş katsayısı
  double A;    // boru yüzey alanı
  double mhot;   // soğuk akışkan debisi
  double mcold;   // sıcak gaz akışkan debisi
  double dmhot;
  double dmcold;
  double Ric,Rdis;  // Boru yüzeyi ısıl direnç katsayıları
  double klevha;     // levha ısı iletim katsayısı
  double dL;        // sonlu farklar boru bölüm sayısı
  int n_x;     // sonlu farklar step sayısı
  int n_y;
  double L,W,dz,Z;
  int nplate;
  double kboru;
  double platethickness;
  double eod=1e-4;
  double Thotin,Thotout;
  double Tcoldin,Tcoldout;
  double UU[][];
 public recuperator1(String ihot_sivi,
 String icold_sivi,double mhoti,double mcoldi,double Li, double Wi,double dzi,double dLi,int nplatei)
 {hot_sivi=new Gas(ihot_sivi);
  cold_sivi=new Gas(icold_sivi);
  L=Li;
  W=Wi;
  dL=dLi;
  n_x=(int)(L/dL)+1;
  n_y=(int)(W/dL)+1;
  W=Wi;
  dL=dLi;
  nplate=nplatei;
  dz=dzi;
  Z=dz*nplate;
  mhot=mhoti;
  mcold=mcoldi;
  dmhot=mhot/nplate;
  dmcold=mcold/nplate;
  kboru=210.0; //(w/mK)
  platethickness=1e-3; //m
  System.out.println("n_x="+n_x+"n_y="+n_y+"L="+L+"W="+W);
  Thot=new double[n_x][n_y];
  Tcold=new double[n_x][n_y];
  UU=new double[n_x][n_y];
  Thotin=0;
  Thotout=0;
  Tcoldin=0;
  Tcoldout=0;
  }  
  // eps     : pipe equivalent roughness m
  //          -------------------------------------------
  //          value of equivalent roughness for some pipes:
  //          Pipe             equivalent rougness m
  //          -------------    ---------------------
  //          Riveted steel    0.9e-3 - 9e-3
  //          Concrete         0.3e-3 - 3e-3
  //          Wood stave       0.18e-3 - 0.9 e-3
  //          Cast iron        0.26e-3
  //          Galvanised iron  0.15e-3
  //          Commercial steel 0.045e-3
  //          wrought iron     0.045e-3
  //          drawn tubing     0.0015e-3
  //          Plastic,glass    0.0 (smooth)
  //          -------------------------------------------
// boru içi 
 public double f(double Re)
 {
 //Haaland equation
 //double f1=-1.8*Math.log10(Math.pow((eod/3.7),1.11)+6.9/ Re);
 //f1=1.0/(f1*f1);
 
 //Swamee-Jain equation
 //double A=Math.log10(eod/3.7+5.74/Math.pow(Re,0.9));
 //double f1=0.25/(A*A);

 //Serghides equation
 //double A1=-2.0*Math.log10(eod/3.7+12.0/Re);
 //double B1=-2.0*Math.log10(eod/3.7+2.51*A1/Re);
 //double C1=-2.0*Math.log10(eod/3.7+2.51*B1/Re);
 //double f1=A1-((B1-A1)*(B1-A1))/(C1-2.0*B1+A1);
 //f1=1.0/(f1*f1);
 //System.out.println("Sergides equation f1="+f1);
 //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);
 //System.out.println("Goudar equation f1="+f1);
 return f1;
 }
//boru içi 

//boru içi 
public double  velocity_x(double t)
{
// velocity of the pipe m/x
double x=0.0;
double mboru=dmhot/2.0;
double T=t+273.15;
double aa[]=hot_sivi.property(T,1.01325);
double ro=aa[15]/aa[2];
double A=W*dz/2.0;
return (mboru/ro/A);
}
public double  velocity_y(double t)
{
// velocity of the pipe m/x
double x=0.0;
double mboru=dmcold/2.0;
double T=t+273.15;
double aa[]=cold_sivi.property(T,1.01325);
double ro=aa[15]/aa[2];
double A=L*dz/2.0;
return (mboru/ro/A);
}
//boru içi 
public double  Re_x(double t)
{
// reynold's number boru içi
double T=t+273.15;
double aa[]=hot_sivi.property(T,1.01325);
double ro=aa[15]/aa[2];
double vis=aa[13];
double vv=velocity_x(t);
double A=W*dz/2.0;
double P=2.0*W+dz;
double dh=4*A/P;
double res=(dh*vv*ro/vis);
return res;
}
public double  Re_y(double t)
{
// reynold's number boru içi
double T=t+273.15;
double aa[]=cold_sivi.property(T,1.01325);
double ro=aa[15]/aa[2];
double vis=aa[13];
double vv=velocity_x(t);
double A=W*dz/2.0;
double P=2.0*W+dz;
double dh=4*A/P;
double res=(dh*vv*ro/vis);
return res;
}
//boru içi 
public double  h_x(double t)
{
//convective heat transfer coefficient
double Rey=Re_x(t);
double Nu=0;
double Prt=1.0;
double T=t+273.15;
double aa[]=hot_sivi.property(T,1.01325);
double A=W*dz/2.0;
double P=2.0*W+dz;
double dh=4*A/P;
if(Rey<2300)
  { Nu=3.66; }
else
  {
  //double fr  = f(m,t);
  double fr  = f(Rey);
  //System.out.println("f="+fr);
  Prt = aa[16];
  // Gnielinski equation
  // Gnielinski, V, Int. Chem Eng., 16, 359,1976
  // Valid for 2000 < Re <5e5, 0.5 < Pr < 2000
  Nu=fr/8.0*(Rey-1000.0)*Prt/
  (1+12.7* Math.sqrt(fr/8.0)*( Math.pow(Prt,(2.0/3.0))-1.0));
  }
  double kt  =  aa[14];
  double ss=Nu*kt/dh;
  //System.out.println("Re="+Rey+"Nu="+Nu+"h="+ss+"Prt="+Prt+"kt="+kt+"di="+di);
  return ss;
}
public double  h_y(double t)
{
//convective heat transfer coefficient
double Rey=Re_y(t);
double Nu=0;
double Prt=1.0;
double T=t+273.15;
double A=L*dz/2.0;
double P=2.0*L+dz;
double dh=4*A/P;
double aa[]=cold_sivi.property(T,1.01325);
if(Rey<2300)
  { Nu=3.66; }
else
  {
  //double fr  = f(m,t);
  double fr  = f(Rey);
  //System.out.println("f="+fr);
  Prt = aa[16];
  // Gnielinski equation
  // Gnielinski, V, Int. Chem Eng., 16, 359,1976
  // Valid for 2000 < Re <5e5, 0.5 < Pr < 2000
  Nu=fr/8.0*(Rey-1000.0)*Prt/
  (1+12.7* Math.sqrt(fr/8.0)*( Math.pow(Prt,(2.0/3.0))-1.0));
  }
  double kt  =  aa[14];
  double ss=Nu*kt/dh;
  //System.out.println("Re="+Rey+"Nu="+Nu+"h="+ss+"Prt="+Prt+"kt="+kt+"di="+di);
  return ss;
}

public double U0(double t_x,double t_y)
{
double hi=h_x(t_x);    // boru içi ısı transferi
double ho=h_y(t_y); // boru dışı ısı transferi
double Uboru=1.0/(1.0/ho+1.0/hi+platethickness/kboru);
//System.out.println("hi="+hi+"h0="+ho+"Uboru="+Uboru);
return Uboru;
}

public void hesapla(double t_x, double t_y)
{    Thotin=t_x;
     Tcoldin=t_y;  
	double Q=0;
	double dQ=0;
    double dA=dL*dL;
    double Cphot=0.0;
    double Cpcold=0.0;
    double U=0;

    int i,j;
	for(j=0;j<n_y;j++)
	{Thot[0][j]=t_y;}	
	for(i=0;i<n_x;i++)
	{Tcold[i][0]=t_x;}	
	
	for(j=1;j<n_y;j++)
	{for(i=1;i<n_x;i++)
	 { 
		 U=U0(Thot[i-1][j-1],Tcold[i-1][j-1]);
	   UU[i][j-1]=U;
	   if(j==1 && i==1) UU[i-1][j-1]=U;
	  dQ=U*dA*(Tcold[i-1][j-1]-Thot[i-1][j-1]);
      double aa[]=hot_sivi.property((Thot[i-1][j-1]+273.15),1.01325);
      Cphot=aa[9]/aa[15]*1e3;
	  Thot[i][j-1]=Thot[i-1][j-1]+dQ/(Cphot*dmhot/2.0);
    }
    for(i=1;i<n_x+1;i++)
    {
	 U=U0(Thot[i-1][j-1],Tcold[i-1][j-1]);
	 UU[i-1][j]=U;
	 dQ=U*dA*(Tcold[i-1][j-1]-Thot[i-1][j-1]);
	 Q+=dQ;
	 double aa1[]=cold_sivi.property((Tcold[i-1][j-1]+273.15),1.01325);
     Cpcold=aa1[9]/aa1[15]*1e3;
	 Tcold[i-1][j]=Tcold[i-1][j-1]-dQ/(Cpcold*dmcold/2.0);
	 }
    }

    j=n_y;
    for(i=1;i<n_x;i++)
	{U=U0(Thot[i-1][j-1],Tcold[i-1][j-1]);
	 UU[i][j-1]=U;
	 dQ=U*dA*(Tcold[i-1][j-1]-Thot[i-1][j-1]);
     double aa[]=hot_sivi.property((Thot[i-1][j-1]+273.15),1.01325);
     Cphot=aa[9]/aa[15]*1e3;
	 Thot[i][j-1]=Thot[i-1][j-1]+dQ/(Cphot*dmhot/2.0);
    } 
    Q*=nplate; 
    Thotout=0;
    Tcoldout=0; 
    for(j=0;j<n_y;j++)
	{Thotout+=Thot[n_x-1][j];
	 UU[n_x-1][j]=U;
	//System.out.println("Thotout="+Thotout);
	}	
	Thotout/=n_y;
	System.out.println("Thotout="+Thotout);
	for(i=0;i<n_x;i++)
	{Tcoldout+=Tcold[i][n_y-1];
	 UU[i][n_y-1]=U;
	}
	
	Tcoldout/=n_x;	 
	System.out.println("Tcoldout="+Tcoldout); 
	System.out.println("Q="+Q);     
}

}