public class sogutmakulesi1
{
yashava yh;
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;
  */
  //buhar opsiyonları
  //sp[0] p
  //sp[1] t
  //sp[2] v
  //sp[3] h
  //sp[4] u
  //sp[5] s
//cases of String s
// tv tp th tu ts tx
// pv    pt ph pu ps px
// vp    vt
double P;
double Mx,My;//mesh size  
public sogutmakulesi1(double Pi)
{
P=Pi;
yh=new yashava();
s=new steam();
Mx=0.025;
}


public double KaV_bolu_L(double mhava,double msu,double twi,double two, double t_kh, double t_yh,boolean plot)
{
   double p00,p10,p20,p01;
// twi su giriş sıcaklığı
// two su çıkış sıcaklığı
// t_kh  giriş havası kuru hava sıcaklığı
// t_yh  giriş havası yaş hava sıcaklığı
// msu_bolu_mhava  su hava debi oranı
// root fiding
double ha,hwi,hwo;
double hsu;
double hsu_onceki;
double dhsu;
double tsu;
double Tavg=0;
My=Mx*msu/mhava;
double Ha[][]=new double[41][41];
double Hw[][]=new double[41][41];
double Tw[][]=new double[41][41];
double c[]=yh.s.property("tp",two,P);
hsu=c[3];
double a1[]=yh.property("tdb_twb", t_kh,t_yh,P);
ha=a1[8];
//m x direction n y direction step
int k,n,m,klast;
klast=0;
for(n=0;n<40;n++)
{Ha[0][n]=ha;}
double a2[]=yh.property("tdb_rh", twi,1.0,P);
// enthalpy of saturated air at water temperature
hwi=a2[8];
double a2a[]=yh.property("tdb_rh", two,1.0,P);
// enthalpy of saturated air at water temperature
hwo=a2a[8];
//face at top
for(m=0;m<40;m++)
{Hw[m][0]=hwi;Tw[m][0]=twi;}
System.out.println("ha0="+ha+"tw="+twi+"hw="+hwi);
// face at top
for(m=1;m<40;m++)
{Ha[m][0]=(Ha[m-1][0]+Mx/2*(2.0*Hw[m-1][0]-Ha[m-1][0]))/(1.0+Mx/2.0);
System.out.println("Ha["+m+"[0] ="+Ha[m][0]);
}
for(n=1;n<40;n++)
{
double sp[]=s.property("tp",Tw[0][n-1],P);
double hv=sp[3];
double Cp=hv/Tw[0][n-1];
System.out.println("Cp="+Cp);
//initial guess
p00 = Tw[0][n-1]-My/Cp*(Hw[0][n-1]-Ha[0][n-1]);
double a3[]=yh.property("tdb_rh",p00,1.0,P);
Hw[0][n] = a3[8];
double sp1[]=s.property("tp",p00,P);
double hv1=sp1[3];
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,1.0,P);
//enthalpy of saturated air at water temperature
Hw[0][n]=a4[8];
double sp2[]=s.property("tp",p10,P);
double hv2=sp2[3];
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",p01,1.0,P);
//enthalpy of saturated air at water temperature
Hw[0][n]=a5[8]; 
double sp3[]=s.property("tp",p01,P);
double hv3=sp2[3];
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);
//enthalpy of saturated air at water temperature
Hw[0][n]=a6[8];
System.out.println("Hw[0]["+n+"] ="+Hw[0][n]+"Tw[0]["+n+"] ="+Tw[0][n]);
}

for(k=1;k<40;k++)
{
	
for(m=1;m<=k;m++)
{
double sp[]=s.property("tp",Tw[m][k-1],P);
double hv=sp[3];
double Cp=hv/Tw[m][k-1];
Ha[m][k]=Ha[m-1][k]+Mx/2*(2*Hw[m][k-1]-Ha[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,1.0,P);
//enthalpy of saturated air at water temperature
Hw[m][k]=a3[8];
double sp1[]=s.property("tp",p00,P);
double hv1=sp[3];
Cp=hv1/p00;
Ha[m][k]=Ha[m-1][k]+Mx/2*(Hw[m][k-1]+Hw[m][k]-Ha[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],1.0,P);
//enthalpy of saturated air at water temperature
Hw[m][k]=a4[8];
double sp2[]=s.property("tp",p10,P);
double hv2=sp2[3];
Cp=hv2/p10;
Ha[m][k]=Ha[m-1][k]+Mx/2*(Hw[m][k-1]+Hw[m][k]-Ha[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,1.0,P);
//enthalpy of saturated air at water temperature
Hw[m][k]=a5[8];
double sp2a[]=s.property("tp",p01,P);
double hv2a=sp2a[3];
Cp=hv2a/p01;
Ha[m][k]=Ha[m-1][k]+Mx/2*(Hw[m][k-1]+Hw[m][k]-Ha[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],1.0,P);
//enthalpy of saturated air at water temperature
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 sp[]=s.property("tp",Tw[k][n-1],P);
double hv=sp[3];
double Cp=hv/Tw[k][n-1];
Ha[k][n]=Ha[k-1][n]+Mx/2*(2.0*Hw[k-1][n]-Ha[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,1.0,P);
//enthalpy of saturated air at water temperature
Hw[k][n]=a3[8];
double sp1[]=s.property("tp",p00,P);
double hv1=sp1[3];
Cp=hv1/p00;
Ha[k][n]=Ha[k-1][n]+Mx/2*(Hw[k][n]+Hw[k-1][n]-Ha[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,1.0,P);
//enthalpy of saturated air at water temperature
Hw[k][n]=a4[8];
double sp2[]=s.property("tp",p10,P);
double hv2=sp1[3];
Cp=hv2/p10;
Ha[k][n]=Ha[k-1][n]+Mx/2*(Hw[k][n]+Hw[k-1][n]-Ha[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,1.0,P);
Hw[k][n]=a5[8];
double sp2b[]=s.property("tp",p10,P);
double hv2b=sp2b[3];
Cp=hv2b/p10;
Ha[k][n]=Ha[k-1][n]+Mx/2*(Hw[k][n]+Hw[k-1][n]-Ha[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],1.0,P);
Hw[k][n]=a5[8];
Ha[k][n]=Ha[k-1][n]+Mx/2*(Hw[k][n]+Hw[k-1][n]-Ha[k-1][n])/(1+Mx/2);
}
Tavg=0;
for(int w=0;w<=k;w++)
{Tavg+=Tw[w][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,false,"Tw","x","y");
 pp.plot();
}
return klast*My;
}
 
}