import java.io.*;
import javax.swing.*;
import javax.tools.*;
import java.util.*;

class kanatT
 {
  double Tw[];
  double Qw[];
  double xw[];
  Gas1 g;
  double L; // kanat boyu
  double W; // kanat yüksekliği
  double etkal; //kanat metali et kalınlığı
  double P; //hava basıncı
  double dx; //sonlu fark kanat boyu
  int N;  //sonlu fark kanat parçacık sayısı
  double Qmax;
  double t0;
  double t1;
  public kanatT(double Li, double Wi,double etkali,double Pi,double dxi)
  {g=new Gas1("air");
   L=Li;
   W=Wi;
   etkal=etkali;
   P=Pi;
   dx=dxi;
   N=(int)(L/dx+1.01);
   //System.out.println("N="+N);
   Tw=new double[N];
   Qw=new double[N];
   xw=new double[N];
  }
    
  public void PlotT()
  {	Plot pp=new Plot(xw,Tw);
    pp.setPlabel("Fin Temperature gradient");
    pp.setXlabel("x distance, m");
    pp.setYlabel("T temperature degree C");
    pp.plot();
  }
  public void PlotQ()
  {	Plot pp=new Plot(xw,Qw);
    pp.setPlabel("Fin Heat Transfer Percentage Q=percentage*Qmax, Qmax="+Qmax+" W");
    pp.setXlabel("x distance, m");
    pp.setYlabel("Q/Qmax");
     pp.plot();
  }
  public static double kmetal(double t)
  {double T=t+273.15;
   double a=71.62587939698594-0.03447361809045638*T-7.927135678388277E-6*T*T;
  return a;
  }
  	 	   	 
  public double Rayleigh(double t,double tinf)
  { // Doğal taşınım Rayleigh sayısı
    // t derece C kanat yüzey sıcaklığı
    // tinf derece C  dış hava scaklığı
    // Gas1 g=new Gas1("air");
   double tf=(t+tinf)/2.0;
   double Tf=tf+273.15;
   double T=t+273.15;  //K
   double beta=1/Tf;
   double gyc=9.806;
   double ro=g.M/g.v(Tf,P);
   double nu=g.vis(Tf)/ro;
   double alfa=g.alfa(Tf,P);
   // System.out.println("L="+L+"t="+t+"tinf="+tinf+"tf="+tf+"vis="+g.vis(Tf)+"ro="+ro+"nu="+nu);
   // System.out.println("gy="+gyc+"nu="+nu+"beta="+beta+"alfa="+alfa);
   double R=gyc*beta*(t-tinf)*L*L*L/(alfa*nu);
   return R;
  }
  
  public double h(double t,double tinf)
  {  // Doğal taşınım ısı transferi hava için
     // dik konumlu levha
  Gas1 g=new Gas1("air");
  double tf=(t+tinf)/2.0;
  double Tf=tf+273.15;
  double Pr=g.Prandtl(Tf);
  double Ra=Rayleigh(t,tinf);
  //System.out.println("Ra="+Ra);
  double k=g.k(Tf);
  double Nu=(0.825+0.387*Math.pow(Ra,(1.0/6.0))/Math.pow((1.0+Math.pow((0.492/Pr),(9.0/16.0))),(8.0/27.0)));
  Nu=Nu*Nu;
  //System.out.println("Nu="+Nu+"k="+k+"L="+L);
  double hh=Nu*k/L;
  return hh;
  }

  public double Qidealkanat1(double t0,double tinf)
  {//bu yaklaşımda ortalama bir ısı transferi katsayısı için 
   // analitik çözüm sonucu çıkan sonuç bulunmaktadır. 
   double cevre=2*W+2*etkal;
   double A=W*etkal;
   int say=0;
   double h=0;
   double h1=0;
   double k1=0;
   double k=0;
   for(double t=t0;t>tinf;t-=0.1)
   {h1=h(t,tinf);h+=h1;k1=kmetal(t);k+=k1;say++;
   //System.out.println("h="+h1+"k="+k1);
   }
   h/=say;k/=say;
   //System.out.println("h="+h+"k="+k+"A="+A+"cevre="+cevre);
   double Q=Math.sqrt(h*k*cevre*A)*(t0-tinf);	  
   return Q;
   }
  
  public double Qidealkanat(double t0,double tinf)
  {
  double Li=20.0;     //  uzunluk	  
  double t=t0;
  double ddx=0.001;
  double dA=2.0*dx*W;
  double dQ=0;
  double Q=0;
  int i=0;
  for(double x=0;x<=Li;x+=ddx)
  {dQ=h(t,tinf)*dA*(t-tinf);
   Q+=dQ;
   t=t-dQ/(kmetal(t)*(W*etkal))*ddx;
   i++;
  }	
  //System.out.println("t="+t);  
  return Q;
  }
  
    
  public double Qkanat(double t0,double tinf)
  { 
  double t=t0;
  double dA=2.0*dx*W;
  double dQ=0;
  double Q=0.0;
  int i=0;
  for(double x=0;x<=L;x+=dx)
  {
   Tw[i]=t;
   xw[i]=x;
   Qw[i]=Q;
   dQ=h(t,tinf)*dA*(t-tinf);
   Q+=dQ;
   //dQ=-kmetal*(W*t)*dt/dx
   t=t-dQ/(kmetal(t)*(W*etkal))*dx;
   i++;
  }
  t1=t;
  Qmax=Qidealkanat(t0,tinf);
  //System.out.println("Qmax in = "+Qmax);
  for(i=0;i<N;i++)
  { Qw[i]=Qw[i]/Qmax;}	    
  return Q;
  }
  
  public static void main(String args[])
  {
  double t=90;
  double tinf=23;
  double L=0.71;
  double p=1.01325;
  kanat kk=new kanat(0.05,0.7,0.67e-3,1.01325,0.001);
  double h1=kk.h(t,tinf);
  System.out.println("h="+h1);
  //System.out.println("Qideal1="+kk.Qidealkanat1(t,tinf)+"Qideal="+kk.Qidealkanat(t,tinf));
  System.out.println("Q="+kk.Qkanat(t,tinf));
  kk.PlotQ();
  kk.PlotT();
  }
 }