public class recuperator
{
moist_air ma;
yashava yh;
Gas1 a;
steam s;
double[][] xArr;
double[][] yArr;
double[][] TArr;

  //options for string s: "tdb_twb"  kuru termometre - yaş termometre sıcaklığı
  //                      "tdb_rh"   kuru termometre sıcaklığı - bağıl nem
  //                      "tdb_w"    kuru termometre sıcaklığı - özgül nem
  //                      "tdb_tdew" kuru termometre sıcaklığı - çiğ noktası sıcaklığı
  //                      "tdb_pv"   kuru termometre sıcaklığı - su buharı basıncı
  //                      "tdb_dos"  kuru termometre sıcaklığı - degree of saturation
  //                      "tdb_h"    kuru termometre sıcaklığı - entalpi
  //                      "tdb_s"    kuru termometre sıcaklığı - entropi
  //                      "w_rh"     özgül nem    - bağıl humifdity
  //                      "w_h"      özgül nem    -  entalpi
  /*
  aa[0]=P;
  aa[1]=t;
  aa[2]=ap[2];
  aa[3]=pv;
  aa[4]=Pa;
  aa[5]=w;
  aa[6]=rh;
  aa[7]=dos;
  aa[8]=h;
  aa[9]=s;
  aa[10]=tdp;
  aa[11]=ha;
  aa[12]=hv;
  aa[13]=w*hv;
  */
double P;
double Mx,My;//mesh size  
public recuperator(double Pi)
{
P=Pi;
yh=new yashava();
s=new steam();
ma=new moist_air();
a=new Gas1("air");
Mx=0.015;
}


public double heat_exchanger(double mtazehava,double miçhava,double twi,double tai,boolean plot)
{
   double p00,p10,p20,p01;
// twi iç hava giriş sıcaklığı
// tai taze hava giriş sıcaklığı 
// miçhava_bolu_mtazehava iç hava taze hava debi oranı
// root finding
double ha,hai,hwi,hwo;
double t_air;
double ta;
double two;
double Tavg=0;
double Ha[][]=new double[24][24];
double Ta[][]=new double[24][24];
double Hw[][]=new double[24][24];
double Tw[][]=new double[24][24];
double [] propa=yh. property("tdb_rh",tai,1.0,P);
double w=propa[5];
double [] propc=yh.property("tdb_rh",tai,1.0,P);
double t=propc[1];
double Cp1=ma.Cp_moistw(t,w);
double K=ma.k_moistw(t,w);
My=Mx*(mtazehava/miçhava);
double [] propd=yh.property("tdb_rh",tai,1.0,P);
double rh=propd[6];
/*double c[]=yh.s.property("tp",tai,P);
t_air=c[1];*/
double a1[]=yh.property("tdb_rh",tai,rh,P);
two=a1[1];
//m x direction n y direction step
int k,n,m,klast;
klast=0;
double a2[]=yh.property("tdb_rh", twi,rh,P);
hwi=a2[8];
double a2a[]=yh.property("tdb_rh",tai,rh,P);
hai=a2a[8];
for(n=0;n<23;n++)
{Ha[0][n]=hai;Ta[0][n]=tai;}
//face at top
for(m=0;m<23;m++)
{Hw[m][0]=hwi;Tw[m][0]=twi;}
System.out.println("tw="+twi+"hw="+hwi);
// face at top
for(m=1;m<23;m++)
{Ta[m][0]=(Ta[m-1][0]+Mx/2*(2.0*Tw[m-1][0]-Ta[m-1][0]))/(1.0+Mx/2.0);
System.out.println("Ta["+m+"][0] ="+Ta[m][0]);
}
for(n=1;n<23;n++)
{
double[]prop=yh.property("tdb_rh",Tw[0][n-1],rh,P);	
//double[]prop=a.property(Tw[0][n-1],P);
double hv=prop[8];
double Cp=hv/Tw[0][n-1];
System.out.println("Cp="+Cp);
//initial guess
p00 = (Tw[0][n-1]-(My/(2*Cp))*(Hw[0][n-1]-2*Ha[0][n-1]));
double a3[]=yh.property("tdb_rh",p00,rh,P);
Hw[0][n] = a3[8];
double []prop1=yh.property("tdb_rh",p00,rh,P);
//double []prop1=a.property(p00,P);
double hv1=prop1[8];
Cp=hv1/p00;
//first guess
p10 = Tw[0][n-1]-My/(2.0*Cp)*(Hw[0][n-1]+Hw[0][n]-2.0*Ha[0][n-1]);
//aitken interpolation correection step
double a4[]=yh.property("tdb_rh",p10,rh,P);
Hw[0][n]=a4[1];
double [] prop2=yh.property("tdb_rh",p10,rh,P);
//double [] prop2=a.property(p10,P);
double hv2=prop2[8];
Cp=hv2/p10;
p20=Tw[0][n-1]-My/(2.0*Cp)*(Hw[0][n-1]+Hw[0][n]-2.0*Ha[0][n-1]);
p01 = p00  - (p10 - p00)*(p10 - p00)/( p20 - 2* p10 + p00);
double a5[]=yh.property("tdb_rh",p10,rh,P);
Hw[0][n]=a5[8]; 
double []prop3=yh.property("tdb_rh",p01,rh,P);
//double []prop3=a.property(p01,P);
double hv3=prop3[8];
Cp=hv3/p01;
Tw[0][n]=Tw[0][n-1]-My/(2.0*Cp)*(Hw[0][n-1]+Hw[0][n]-2.0*Ha[0][n-1]);
double a6[]=yh.property("tdb_rh",Tw[0][n],1.0,P);
Hw[0][n]=a6[8];
System.out.println("Tw[0]["+n+"] ="+Tw[0][n]);
}

for(k=1;k<23;k++)
{
	
for(m=1;m<=k;m++)
{
double []prop=yh.property("tdb_rh",Tw[m][k-1],rh,P);
//double []prop=a.property(Tw[m][k-1],P);
double hv=prop[8];
double Cp=hv/Tw[m][k-1];
Ta[m][k]=Ta[m-1][k]+Mx/2*(2*Tw[m][k-1]-Ta[m-1][k])/(1+Mx/2);
p00=Tw[m][k-1]-My/(2.0*Cp)*(2.0*Hw[m][k-1]-Ha[m][k-1]-Ha[m][k]);
double a3[]=yh.property("tdb_rh",p00,rh,P);
Hw[m][k]=a3[8];
double []prop1=yh.property("tdb_rh",p00,rh,P);
//double []prop1=a.property(p00,P);
double hv1=prop[8];
Cp=hv1/p00;
Ta[m][k]=Ta[m-1][k]+Mx/2*(Tw[m][k-1]+Tw[m][k]-Ta[m-1][k])/(1+Mx/2);
p10=(Tw[m][k-1]-My/(2.0*Cp)*(Hw[m][k-1]+Hw[m][k]-Ha[m][k-1]-Ha[m][k]));
double a4[]=yh.property("tdb_rh",Tw[m][k],rh,P);
Hw[m][k]=a4[8];
double []prop2=yh.property("tdb_rh",p10,rh,P);
//double []prop2=a.property(p10,P);
double hv2=prop2[8];
Cp=hv2/p10;
Ta[m][k]=Ta[m-1][k]+Mx/2*(Tw[m][k-1]+Tw[m][k]-Ta[m-1][k])/(1+Mx/2);
p20=Tw[m][k-1]-My/(2.0*Cp)*(Hw[m][k-1]+Hw[m][k]-Ha[m][k-1]-Ha[m][k]);
p01 = p00  - ((p10 - p00)*(p10 - p00))/( p20 - 2* p10 + p00);
double a5[]=yh.property("tdb_rh",p01,rh,P);
Hw[m][k]=a5[1];
double []prop2a=yh.property("tdb_rh",p01,rh,P);
//double[]prop2a=a.property(p01,P);
double hv2a=prop2a[8];
Cp=hv2a/p01;
Ta[m][k]=Ta[m-1][k]+Mx/2*(Tw[m][k-1]+Tw[m][k]-Ta[m-1][k])/(1+Mx/2);
Tw[m][k]=Tw[m][k-1]-My/(2.0*Cp)*(Hw[m][k-1]+Hw[m][k]-Ha[m][k-1]-Ha[m][k]);
double a6[]=yh.property("tdb_rh",Tw[m][k],rh,P);
Hw[m][k]=a6[8];
//System.out.println("m="+m+"k="+k+"Hw["+m+"]["+k+"] ="+Hw[m][k]+"Tw["+m+"]["+k+"] ="+Tw[m][k]);
}
for(n=1;n<=k;n++)
{
double []prop=yh.property("tdb_rh",Tw[k][n-1],rh,P);
//double []prop=a.property(Tw[k][n-1],P);
double hv=prop[8];
double Cp=hv/Tw[k][n-1];
Ta[k][n]=Ta[k-1][n]+Mx/2*(2.0*Tw[k-1][n]-Ta[k-1][n])/(1+Mx/2);
p00=Tw[k][n-1]-My/(2.0*Cp)*(2.0*Hw[k][n-1]-Ha[k][n-1]-Ha[k][n]);
double a3[]=yh.property("tdb_rh",p00,rh,P);
Hw[k][n]=a3[8];
double []prop1=yh.property("tdb_rh",p00,rh,P);
//double []prop1=a.property(p00,P);
double hv1=prop1[8];
Cp=hv1/p00;
Ta[k][n]=Ta[k-1][n]+Mx/2*(Tw[k][n]+Tw[k-1][n]-Ta[k-1][n])/(1+Mx/2);
p10=Tw[k][n-1]-My/(2.0*Cp)*(Hw[k][n-1]+Hw[k][n]-Ha[k][n-1]-Ha[k][n]);
double a4[]=yh.property("tdb_rh",p10,rh,P);
Hw[k][n]=a4[8];
double []prop2=yh.property("tdb_rh",p10,rh,P);
//double []prop2=a.property(p10,P);
double hv2=prop1[8];
Cp=hv2/p10;
Ta[k][n]=Ta[k-1][n]+Mx/2*(Tw[k][n]+Tw[k-1][n]-Ta[k-1][n])/(1+Mx/2);
p20=Tw[k][n-1]-My/(2.0*Cp)*(Hw[k][n-1]+Hw[k][n]-Ha[k][n-1]-Ha[k][n]);
p01 = p00  -((p10 - p00)*(p10 - p00))/( p20 - 2* p10 + p00);
double a5[]=yh.property("tdb_rh",p01,rh,P);
Hw[k][n]=a5[1];
double []prop2b=yh.property("tdb_rh",p10,rh,P);
//double []prop2b=a.property(p10,P);
double hv2b=prop2b[8];
Cp=hv2b/p10;
Ta[k][n]=Ta[k-1][n]+Mx/2*(Tw[k][n]+Tw[k-1][n]-Ta[k-1][n])/(1+Mx/2);
Tw[k][n]=Tw[k][n-1]-My/(2.0*Cp)*(Hw[k][n-1]+Hw[k][n]-Ha[k][n-1]-Ha[k][n]);
double a6[]=yh.property("tdb_rh",Tw[k][n],rh,P);
Hw[k][n]=a5[8];
Ta[k][n]=Ta[k-1][n]+Mx/2*(Tw[k][n]+Tw[k-1][n]-Ta[k-1][n])/(1+Mx/2);
//System.out.println("k="+k+"n="+n+"Hw["+k+"]["+n+"] ="+Hw[k][n]+"Tw["+k+"]["+n+"] ="+Tw[k][n]);
}
Tavg=0;
for(int a=0;a<=k;a++)
{Tavg+=Tw[a][k];}
Tavg/=(k+1);
System.out.println("k="+k+"Tavg="+Tavg);
if(Tavg<=two) {klast=k;break;}
}
System.out.println("Tavg = "+Tavg);
double T[][]=new double[klast][klast];
for(int i=0;i<klast;i++)
{for(int j=0;j<klast;j++)
{T[klast-1-i][klast-1-j]=Tw[j][i];
System.out.print(Tw[j][i]+" ");
 }
System.out.println("");
}
if(plot)
{ContourPlot pp=new ContourPlot(T,0.0,1.0,0.0,1.0,20,true,"Tw","x","y");
 pp.plot();
}

return klast*My;
}
 
}