public class rankine2
{
// use class steam 
// steam b=new steam();
// double a[]=b.property("tp",30.0,1.01325);
// inlet parameters
// termodynamic couple "tv" "tp" "th" "tu" "ts"tx"
//                  "pv" "pt" "ph" "pu" "ps" "px"
// exit parameters(a) a[0]=P a[1]=T a[2]=V a[3]=h
// a[4]=u a[5]=s a[6]=x
double T[];
double P[];
double h[];
double s[];
double x[];
double etait1,etait2;
double etaip1,etaip2;
double etacevrim;
double m;
double Wt,Wt1,Wt2,Wp,Wp1,Wp2,W;
double Qkazan,Qkaynama,Qsuperheat,Qyogusturucu;
double y;//türbinden ara ısıtıcıya gönderilen buhar yüzdesi
steam b;

public rankine2(double mi,double T1,double P1, double T3,double P3,double P4,double etait1i, double etait2i,double etaip1i,double etaip2i)
{
m=mi;
b=new steam();
T=new double[16];
P=new double[16];
h=new double[16];
s=new double[16];
x=new double[16];
//Turbine 1 inlet girişi
T[1]=T1;
P[1]=P1;
T[3]=T3;
P[3]=P3;
P[2]=P3;
P[0]=P3;//2i
P[4]=P4;
P[11]=P4;//4i
P[9]=P4;
P[5]=P4;
P[6]=P[2];
P[7]=P[6];
P[12]=P[6];//6i
P[10]=P1;
P[8]=P1;
P[13]=P1;//8i
P[14]=P1;
etait1=etait1i;
etait2=etait2i;
etaip1=etaip1i;
etaip2=etaip2i;
} 
public void cevrimhesabi()
{
// turbine 1 inlet
double a1[]=b.property("tp",T[1],P[1]);
h[1]=a1[3];
s[1]=a1[5];
// isentropic turbine1 exit
s[0]=s[1];
//P[0]=P[2];
double a0[]=b.property("ps",P[0],s[0]);
T[0]=a0[1];
h[0]=a0[3];
s[0]=a0[5];
x[0]=a0[6];
// turbine1 exit
h[2]=h[1]-etait1*(h[1]-h[0]);
double a2[]=b.property("ph",P[2],h[2]);
T[2]=a2[1];
x[2]=a2[6];
s[2]=a2[5];
// turbine1 exit saturation point
x[15]=1.0;
P[15]=P[2];
double a15[]=b.property("px",P[15],x[15]);
T[15]=a15[1];
h[15]=a15[3];
s[15]=a15[5];
// Türbin 2 inlet
double a3[]=b.property("tp",T[3],P[3]);
h[3]=a3[3];
s[3]=a3[5];
// 11 : Turbine2 isentropic
s[11]=s[3];
//P[11]=P[4];
double a11[]=b.property("ps",P[11],s[11]);
T[11]=a11[1];
h[11]=a11[3];
x[11]=a11[6];
// gerçek türbin 2 çıkışı
h[4]=h[3]-etait2*(h[3]-h[11]);
double a4[]=b.property("ph",P[4],h[4]);
T[4]=a4[1];
x[4]=a4[6];
s[4]=a4[5];

// yoğuşturucu doyma buhar noktası
x[9]=1.0;
double a9[]=b.property("px",P[9],x[9]);
T[9]=a9[1];
h[9]=a9[3];
s[9]=a9[5];
// yoğuşturucu çıkışı
x[5]=0.0;
double a5[]=b.property("px",P[5],x[5]);
T[5]=a5[1];	
h[5]=a5[3];
s[5]=a5[5];

// isentropik pompa 1 (12=6i)
s[12]=s[5];
double a12[]=b.property("ps",P[12],s[12]);
h[12]=a12[3];
s[12]=a12[5];
// gerçek pompa 1
h[6]=h[5]+(h[12]-h[5])/etaip1;
double a6[]=b.property("ph",P[6],h[6]);
T[6]=a6[1];
s[6]=a6[5];
//ara kızdırıcı çıkışı
x[7]=0.0;
double a7[]=b.property("px",P[7],x[7]);
T[7]=a7[1];	
h[7]=a7[3];
s[7]=a7[5];

// isentropik pompa 2 (13=8i)
s[13]=s[7];
double a13[]=b.property("ps",P[13],s[13]);
h[13]=a13[3];
s[13]=a13[5];
// gerçek pompa 2
h[8]=h[7]+(h[13]-h[7])/etaip2;
double a8[]=b.property("ph",P[8],h[8]);
T[8]=a8[1];
s[8]=a8[5];

// kazan kaynama  bölgesi girişi
x[10]=0.0;
double a10[]=b.property("px",P[10],x[10]);
T[10]=a10[1];
h[10]=a10[3];
s[10]=a10[5];

// kazan kızgın buhar bölgesi çıkışı

x[14]=1.0;
double a14[]=b.property("px",P[14],x[14]);
T[14]=a14[1];
h[14]=a14[3];
s[14]=a14[5];
// 
/*
if(s[2]>s[8])
{double dt1=(T[2]-T[8])/19;
double t=T[8];
for(int i=0;i<20;i++)
{double b8[]=b.property("tp",t,P[8]);
c3[0][i]=b8[5];c3[1][i]=b8[1];t+=dt1;
}
}
double dt=(T[1]-T[7])/19;
double t=T[7];
for(int i=0;i<20;i++)
{double b7[]=b.property("tp",t,P[7]);
c1[0][i]=b7[5];c1[1][i]=b7[1];t+=dt;
}
*/
y=(h[7]-h[6])/(h[2]-h[6]);
Wt1=m*(h[1]-h[2]);
Wt2=m*(1-y)*(h[3]-h[4]);
Wt=Wt1+Wt2;
Wp1=m*(1-y)*(h[6]-h[5]);
Wp2=m*(h[8]-h[7]);
Wp=Wp1+Wp2;
W=Wt-Wp;
Qkaynama=m*(h[1]-h[8]);
Qsuperheat=m*(1-y)*(h[3]-h[2]);
Qkazan=Qkaynama+Qsuperheat;
Qyogusturucu=m*(1-y)*(h[4]-h[5]);
etacevrim=W/Qkazan;
}

public String cikti()
{
cevrimhesabi();
String s="Rankine cycle with an open water heater and two stage turbine with reheating\n";
s+="percantage of steam going into water heater="+y+"\n";
s+="percantage of steam going into second turbine="+(1-y)+"\n";
s+=" system efficiency    = "+etacevrim+"\n";
s+=" Turbine 1 power exit    = "+Wt1+" kW\n";
s+=" Turbine 2 power exit  = "+Wt2+" kW\n";
s+=" Total turbine  1 power exit    = "+Wt+" kW\n";

s+=" Pump 1 inlet     = "+Wp1+" kW\n";
s+=" Pump 2 inlet      = "+Wp2+" kW\n";
s+=" Total pumppower inlet= "+Wp+" kW\n";	
s+=" Net power output    = "+W +" kW\n";
s+=" Boiler heat input = "+Qkaynama +" kW\n";
s+=" Reheater heat input = "+Qsuperheat +" kW\n";
s+=" Boiler-superheater total heat input = "+Qkazan +" kW\n";
s+=" Condenser heat output = "+Qyogusturucu +" kW\n";
return s;
}

public double[][] TS1()
{
double a[][]=Text.readDoubleT("steam.txt");
return a;
}	
	
public double[][] TS()
{
double a[][]=new double[2][201];
double tc=b.Tc-273.15;
double dt=(tc-10.0)/100.0;
int i=0;
double t;
for(t=10.0;t<=tc;t+=dt)
{double a1[]=b.property("tx",t,0.0);
 a[1][i]=t;
 a[0][i]=a1[5];
 i++;
}
double a3[]=b.property("tx",tc,1.0);
 a[1][i]=t;
 a[0][i]=a3[5];
 i++;
for(t=tc;t>=10;t-=dt)
{double a2[]=b.property("tx",t,1.0);
 a[1][i]=t;
 a[0][i]=a2[5];
 i++;
}
return a;	
}

public void plot()
{
 Plot pp=new Plot(TS1());
 double TT1[],ss1[];
 double TT2[],ss2[];
 double TT3[],ss3[];
  double TT4[],ss4[];
 TT1=new double[10];
 TT2=new double[10];
 TT3=new double[10];
 TT4=new double[10];
 ss1=new double[10];
 ss2=new double[10];
 ss3=new double[10];
 ss4=new double[10];
 double a3[];
 //For more realistic TS diagram
 //constant pressure lines draw in details
 for(int i=0;i<10;i++)
 {TT1[i]=T[14]+i*(T[1]-T[14])/10;
 a3=b.property("tp",TT1[i],P[1]);
  ss1[i]=a3[5];
 }
 for(int i=0;i<10;i++)
 {TT2[i]=T[2]+i*(T[3]-T[2])/10;
 a3=b.property("tp",TT2[i],P[2]);
  ss2[i]=a3[5];
 }
 for(int i=0;i<10;i++)
 {TT3[i]=T[15]+i*(T[2]-T[15])/10;
 a3=b.property("tp",TT3[i],P[2]);
  ss3[i]=a3[5];
 }
 for(int i=0;i<10;i++)
 {TT4[i]=T[4]+i*(T[9]-T[4])/10;
 a3=b.property("tp",TT4[i],P[4]);
  ss4[i]=a3[5];
 }
 double t1[]={T[1],TT2[0],TT2[1],TT2[2],TT2[3],TT2[4],TT2[5],TT2[6],TT2[7],TT2[8],TT2[9],T[3],T[4],TT4[0],TT4[1],TT4[2],TT4[3],TT4[4],TT4[5],TT4[6],TT4[7],TT4[8],TT4[9],T[9],T[5],T[6],T[7],T[8],T[10],TT1[0],TT1[1],TT1[2],TT1[3],TT1[4],TT1[5],TT1[6],TT1[7],TT1[8],TT1[9],T[1]};
 double s1[]={s[1],ss2[0],ss2[1],ss2[2],ss2[3],ss2[4],ss2[5],ss2[6],ss2[7],ss2[8],ss2[9],s[3],s[4],ss4[0],ss4[1],ss4[2],ss4[3],ss4[4],ss4[5],ss4[6],ss4[7],ss4[8],ss4[9],s[9],s[5],s[6],s[7],s[8],s[10],ss1[0],ss1[1],ss1[2],ss1[3],ss1[4],ss1[5],ss1[6],ss1[7],ss1[8],ss1[9],s[1]};
 double t2[]={T[7],T[15],TT3[0],TT3[1],TT3[2],TT3[3],TT3[4],TT3[5],TT3[6],TT3[7],TT3[8],TT3[9],T[2]};
 double s2[]={s[7],s[15],ss3[0],ss3[1],ss3[2],ss3[3],ss3[4],ss3[5],ss3[6],ss3[7],ss3[8],ss3[9],s[2]};
 double t3[]={T[1],T[0]};
 double s3[]={s[1],s[0]};
  double t4[]={T[3],T[11]};
 double s4[]={s[3],s[11]};
 pp.addData(s1,t1);
 pp.addData(s2,t2);
 pp.addData(s3,t3,1);
 pp.addData(s4,t4,1);
 pp.setPlabel("Rankine cycle");
 pp.setYlabel("T, degree C");
 pp.setXlabel("s entropy kJ/kgK");
 pp.plot();
}
}