//Brayton-Ericsson refrigeration cycle
public class brayton3
{
Gas g;
double T[];
double P[];
double s[];
double h[];
double Cp[];
double etakompressor;
double etaturbin;
double Wturbin,Wkompresor,Wnet,Q,Qref,COP,COPref,T1,P1,T2,P2,T3,P3,T4,P4;
public brayton3(double m,double T1,double P1, double P2, double T3,double etakompressori,double etaturbini)
{ set(m,T1,P1,P2,T3,etakompressori,etaturbini);}

public double set(double m,double T1,double P1, double P2, double T3,double etakompressori,double etaturbini)
{ 
  T=new double[6];
  P=new double[6];
  s=new double[6];
  h=new double[6];
  Cp=new double[6];
  g=new Gas("air");
  g.mole=false;
  P[1]=P1;
  P[2]=P2;
  P[4]=P1;
  P[3]=P2;
  T[1]=T1;
  T[3]=T3;
  etakompressor=etakompressori;
  etaturbin=etaturbini;
// 1 
double a1[]=g.property(T[1],P[1]);
h[1]=a1[3];
s[1]=a1[5];
double pr1=a1[17];
// 2i = 0
double pr2=P[2]/P[1]*pr1;
T[0]=g.Ti(pr2,P[2]);
a1=g.property(T[0],P[2]);
h[0]=a1[3];
s[0]=a1[5];
Cp[0]=a1[9];
// 2 
h[2]=(h[0]-h[1])/etakompressor+h[1];
T[2]=(h[2]-h[0])/Cp[0]+T[0];
a1=g.property(T[2],P[2]);
s[2]=a1[5];
//3 
a1=g.property(T[3],P[3]);
h[3]=a1[3];
s[3]=a1[5];
double pr3=a1[17];
double pr4=P1/P2*pr3;
//4i = 5
T[5]=g.Ti(pr4,P[1]);
a1=g.property(T[5],P[1]);
h[5]=a1[3];
s[5]=a1[5];
Cp[5]=a1[9];
//4 
h[4]=h[3]-etaturbin*(h[3]-h[5]);
T[4]=(h[4]-h[5])/Cp[5]+T[5];
a1=g.property(T[4],P[4]);
s[4]=a1[5];
Wturbin=m*(h[3]-h[4]);
Wkompresor=m*(h[2]-h[1]);
Q=m*(h[2]-h[3]);
Qref=m*(h[1]-h[4]);
Wnet=Wkompresor-Wturbin;
COP=Q/Wnet;
COPref=Qref/Wnet;
return T[4];
}
public double[][] line1(double T1,double T2,double P,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],P);
 ss1[i]=aa[5];
}
double a[][]={TT1,ss1};
return a;
}
public String toString()
{
String s=" Brayton cycle \n";
s+="Turbine power output W turbine = "+Wturbin+" kW \n";
s+="Compressor power input W kompresor = "+Wkompresor+" kW\n";	
s+="Net power output  Wnet = "+Wnet+" kW\n";
s+="Heat exchanger heat removal Q  = "+Q+" kW\n";
s+="refrigeration heat removal Qref = "+Qref+" kW \n";
s+="COP refrigeration  = "+COP+"\n";
s+="COP refrigeration  = "+COP+"\n";
s+="Compressor inlet temperature = "+(T[1]-273.15)+" degree C\n";
s+="Compressor inlet pressure  = "+P[1]+" kPa\n";
s+="Compressor exit temperature = "+(T[2]-273.15)+" degree C\n";
s+="Compressor exit  pressure   = "+P[2]+" kPa\n";
s+="Turbine inlet temperature = "+(T[3]-273.15)+" degree C\n";
s+="Turbine inlet pressure   = "+P[3]+" kPA\n";
s+="Turbine exit temperature = "+(T[4]-273.15)+" degree C\n";
s+="Turbine exit pressure   = "+P[4]+" kPa\n";
return s;
}
public void plot()
{ 
 double a23[][]=line1(T[2],T[3],P[2],15);

 double tt[]=a23[0];
 double ss[]=a23[1];
 Plot pp=new Plot(ss,tt);
 double a41[][]=line1(T[4],T[1],P[1],15);

 tt=a41[0];
 ss=a41[1];
 pp.addData(ss,tt);
 double t1[]={T[1],T[2]};
 double s1[]={s[1],s[2]};
 pp.addData(s1,t1);
 double t2[]={T[1],T[0]};
 double s2[]={s[1],s[0]};
 pp.addData(s2,t2,1);
 double t3[]={T[3],T[4]};
 double s3[]={s[3],s[4]};
 pp.addData(s3,t3);
 double t4[]={T[3],T[5],T[4]};
 double s4[]={s[3],s[5],s[4]};
 pp.addData(s4,t4,1);
 pp.plot();
}
}