public class brayton2
{
Gas g;
double PP1[],vv1[];
double PP2[],vv2[];
double TT1[],ss1[];
double TT2[],ss2[];
double Wturbine1,Wturbine2,Wturbine,Wcompressor1,Wcompressor2,Wcompressor,Wnet,Q1,Q2,Qregenerator,Qintercooler,Qnet,eta;
double T[],P[],h[],s[];
public brayton2(double m,double T1,double P1,double P2,double P4,double T3,double T6,double T8,double dT,double etacompressor1,double etacompressor2,double etaturbine1,double etaturbine2)
{  T=new double[15];
   P=new double[15];
   h=new double[15];
   s=new double[15];
   TT1=new double[25];ss1=new double[25];
   TT2=new double[2];ss2=new double[2];
   g=new Gas("air");
   g.mole=false;
// 1
P[1]=P1;T[1]=T1;
P[2]=P2; 
P[3]=P2;T[3]=T3;
P[4]=P4;
P[5]=P4;
P[6]=P4;T[6]=T6;
P[7]=P2;
P[8]=P2;T[8]=T8;
P[9]=P1;
P[10]=P1;
double a[]=g.property(T[1],P[1]);
h[1]=a[3];
s[1]=a[5];
TT1[0]=T[1];ss1[0]=s[1];
double pr1=a[17];
// 2i = 11
double pr2=P2/P1*pr1;
double T2i=g.Ti(pr2,P2);

a=g.property(T2i,P2);
double h2i=a[3];
double s2i=a[5];
double Cp2i=a[9];
T[11]=T2i;
s[11]=s2i;
TT1[1]=T2i;ss1[1]=s2i;
// 2 
h[2]=(h2i-h[1])/etacompressor1+h[1];
T[2]=(h[2]-h2i)/Cp2i+T2i;
a=g.property(T[2],P[2]);
s[2]=a[5];
// 3
a=g.property(T[3],P[3]);
h[3]=a[3];
s[3]=a[5];
double pr3=a[17];
// 4i = 12 isentropic process
double pr4=P[4]/P[3]*pr3;
double T4i=g.Ti(pr4,P[4]);
a=g.property(T4i,P[4]);
double h4i=a[3];
double s4i=a[5];
double Cp4i=a[9];
T[12]=T4i;
s[12]=s4i;
//4 
h[4]=(h4i-h[3])/etacompressor2+h[3];

T[4]=(h[4]-h4i)/Cp4i+T4i;
a=g.property(T[4],P[4]);
s[4]=a[5];
System.out.println("h4i="+h4i+"h4="+h[4]+"T4i="+T4i+"T4="+T[4]+"s4i="+s4i+"s4="+s[4]+"s3="+s[3]);
double Cp4=a[9];
//6
a=g.property(T[6],P[6]);
h[6]=a[3];
s[6]=a[5];
double Cp6=a[9];
double pr6=a[17];
//7i 
double pr7=P[7]/P[6]*pr6;
double T7i=g.Ti(pr7,P[7]);
a=g.property(T7i,P[7]);
double h7i=a[3];
double s7i=a[5];
double Cp7i=a[9];
T[13]=T7i;
s[13]=s7i;
//7
h[7]=h[6]-etaturbine1*(h[6]-h7i);
T[7]=(h[7]-h7i)/Cp7i+T7i;
a=g.property(T[7],P[7]);
h[7]=a[3];
s[7]=a[5];
//8
a=g.property(T[8],P[8]);
h[8]=a[3];
s[8]=a[5];
double pr8=a[17];
//9i 
double pr9=P[9]/P[8]*pr8;
System.out.println("pr8="+pr8+"pr9="+pr9+"P8="+P[8]+"P[9]="+P[9]);
double T9i=g.Ti(pr9,P[9]);
a=g.property(T9i,P[9]);
double h9i=a[3];
double s9i=a[5];
double Cp9i=a[9];
T[14]=T9i;
s[14]=s9i;
//9
h[9]=h[8]-etaturbine2*(h[8]-h9i);
T[9]=(h[9]-h9i)/Cp9i+T9i;
a=g.property(T[9],P[9]);
s[9]=a[5];
System.out.println("h9="+h[9]+"h9i="+h9i+"Cp9i="+Cp9i+"t9i="+T9i+"t9="+(T[9]-273.15)+"degree C");

//10
T[10]=T[4]+dT;
a=g.property(T[10],P[10]);
h[10]=a[3];
s[10]=a[5];
//5
h[5]=h[4]+(h[9]-h[10]);
double Cp5=(Cp4+Cp6)/2.0;
double T5e=h[5]/Cp5;

T[5]=g.T('h',h[5],P[5]);
System.out.println("T5e="+T5e+"T5="+T[5]);
a=g.property(T[5],P[5]);
s[5]=a[5];

Wturbine1=m*(h[6]-h[7]);
Wturbine2=m*(h[8]-h[9]);
Wturbine=Wturbine1+Wturbine2;
Wcompressor1=m*(h[2]-h[1]);
Wcompressor2=m*(h[4]-h[3]);
Wcompressor=Wcompressor1+Wcompressor2;
Q1=m*(h[6]-h[5]);
Q2=m*(h[8]-h[7]);
Qregenerator=m*(h[5]-h[4]);
Qintercooler=m*(h[2]-h[3]);
Qnet=Q1+Q2;
System.out.println("Q1="+Q1+"Q2="+Q2+"Qnet="+Qnet);
Wnet=Wturbine-Wcompressor;
eta=Wnet/Qnet;
}
public double[][] line(double T1,double T2,double Pi,int n)
{
double TT1[]=new double[n];
double ss1[]=new double[n];
double aa[];
for(int i=0;i<n;i++)
{TT1[i]=T1+i*(T2-T1)/(n-1);
 aa=g.property(TT1[i],Pi);
 ss1[i]=aa[5];
}
double a[][]={TT1,ss1};
return a;
}
public void plot()
{double a[][]=new double[2][61];
 double c1[][]=new double[2][3];
 double c3[][]=new double[2][3];
 double c6[][]=new double[2][3];
 double c8[][]=new double[2][3];
 c1[0][0]=T[1];
 c1[1][0]=s[1];
 c1[0][1]=T[11];
 c1[1][1]=s[11];
 c1[0][2]=T[2];
 c1[1][2]=s[2];
 
 c3[0][0]=T[3];
 c3[1][0]=s[3];
 c3[0][1]=T[12];
 c3[1][1]=s[12];
 c3[0][2]=T[4];
 c3[1][2]=s[4];
 
 c6[0][0]=T[6];
 c6[1][0]=s[6];
 c6[0][1]=T[13];
 c6[1][1]=s[13];
 c6[0][2]=T[7];
 c6[1][2]=s[7];
 
 c8[0][0]=T[8];
 c8[1][0]=s[8];
 c8[0][1]=T[14];
 c8[1][1]=s[14];
 c8[0][2]=T[9];
 c8[1][2]=s[9];
 
 int i=0;
 a[0][0]=T[1];
 a[1][0]=s[1];
 double b[][]=line(T[2],T[3],P[2],15);
 for(i=0;i<15;i++)
 {a[0][i+1]=b[0][i];
  a[1][i+1]=b[1][i];
 }
 b=line(T[4],T[6],P[4],15);
 for(i=0;i<15;i++)
 {a[0][i+16]=b[0][i];
  a[1][i+16]=b[1][i];
 }
 b=line(T[7],T[8],P[7],15);
 for(i=0;i<15;i++)
 {a[0][i+31]=b[0][i];
  a[1][i+31]=b[1][i];
 }
  b=line(T[9],T[10],P[9],15);
 for(i=0;i<15;i++)
 {a[0][i+46]=b[0][i];
  a[1][i+46]=b[1][i];
 }
 Plot p=new Plot(a[1],a[0]);
 p.addData(c1[1],c1[0],1);
 p.addData(c3[1],c3[0],1);
 p.addData(c6[1],c6[0],1);
 p.addData(c8[1],c8[0],1);
 p.addData(s,T,22);
 Text.printT(a);
 p.plot();
}

public String toString()
{
String ss=" Brayton cycle \n";
ss+="low pressure turbine power output W turbine = "+Wturbine1+" kW \n";
ss+="high pressure turbine power output W turbine = "+Wturbine2+" kW \n";
ss+="turbine power output W turbine = "+Wturbine+" kW \n";
ss+="low pressure compressor power output W turbine = "+Wcompressor1+" kW \n";
ss+="high pressure compressor power output W turbine = "+Wcompressor2+" kW \n";
ss+="compressor power input W turbine = "+Wcompressor+" kW \n";
ss+="Net power output  Wnet = "+Wnet+" kW\n";
ss+="Combustion chamber heat input Q combustion = "+Qnet+" kW\n";
ss+="Cycle efficiency eta  = "+eta+"\n";
ss+="Equivalent temperature carnot cycle efficiency eta  = "+(1.0-T[1]/T[8])+"\n";
ss+="Regenerator heat transfer   = "+Qregenerator+" kW\n";
ss+="Termodynamic states : \n";
for(int i=1;i<11;i++)
{ss+="P["+i+"] = "+P[i]+"bar T["+i+"] = "+(T[i]-273.15)+" degree C h["+i+"] = "+h[i]+" kJ/kg s["+i+"] = "+s[i]+" kJ/kgK\n";}
return ss;
}
}