public class ref_cycle4
{
// a R134 ideal refrigeration  cycle
// refrigerantEN b=new refrigerantEN("R134a");
// double a[]=b.property("tp",30.0,1.01325);
///---- String s values and meanings --------
//    tv=temperature-specific volume 
//    tp=temperature-pressure 
//    th=temperature-enthalpy
//    tu=temperature-internal energy
//    ts=temperature-entropy
//    tx=temperature quality
//    pv=pressure-specific volume
//    pt=pressure-temperature
//    ph=pressure-enthapy
//    pu=pressure-internal energy
//    ps=pressure-entropy
//    px=pressure-quality
//    vp=specific volume-pressure
//    vt=specific volume-temperature
//---- output values (method property) ---------
// r[0] P Pressure kPa
// r[1] t temperature degree C
// r[2] v specific volume m^3/kg
// r[3] h enthapy KJ/kg 
// r[4] u internal energy KJ/kg
// r[5] s entropy KJ/kgK
// r[6] x quality kg vapour/kg total
// r[7] ro density kg/m^3

double T[];
double P[];
double h[];
double s[];
double x[];
boolean supercritical=false;
String refname; //refrigerant name
double Tc,Pc;
double COP_condenser,COP_evaporator,COP_evap_carnot,COP_condenser_carnot;
double m;//kg/s mass flow rate of refrigerant
double Wp1,Wp2;//compressor work
double Wp1i,Wp2i;//isentropic compressor work
double Wp;//net compressor work
double dPevap1;// evaporator pressure drop saturated region
double dPevap2;// evaporator pressure drop superheated region
double dPcomp1;// evaporator pressure drop saturated region
double dPcond1;// condenser pressure drop superheated region
double dPcond2;// condenser pressure drop saturated region
double dPcond3;// condenser pressure drop liquid region
double etaisent2,etaisent6; //isentropic efficiencies of turbines
double dh1,dh2;     // expansion valve enthalpy chenge (heat loss)
double dT1,dT7;
double eta_isent; // isentropic efficiency of compressor
double Qevaporator,Qcondenser; //condenser and evaporator heat transfer
refrigerantEN b; //refrigerantEN class object for properties
double c1[][];
double c3[][];
public ref_cycle4(String refnamei,double mi,double dT1i,double P1, double P2,double etaisent2i,double P6,double etaisent6i,double dT7i,
double dPevap1i,double dPevap2i,double dPcomp1i,double dPcond1i,double dPcond2i,double dPcond3i,double dh1i,double dh2i)
{
m=mi;
refname=refnamei;    // refrigerant name
dPevap1=dPevap1i;    // evaporator pressure drop saturated region
dPevap2=dPevap2i;    // evaporator pressure drop superheated region and compressor 1 inlet valve
dPcomp1=dPcomp1;     // compressor 1 exit valve
dPcond1=dPcond1i;    // condenser pressure drop superheated region compressor2 exit valve
dPcond2=dPcond2i;    // condenser pressure drop saturated region
dPcond3=dPcond3i;    // condenser pressure drop liquid region
dh1=dh1i; dh2=dh2i;  // enthalpy drop(heat gain) in expansion valves
dT1=dT1i;
dT7=dT7i;
etaisent2=etaisent2i;
etaisent6=etaisent6i;
b=new refrigerantEN(refname);
T=new double[14];
P=new double[14];
h=new double[14];
s=new double[14];
x=new double[14];
Tc=b.r.Tc;//critical temperature degree C
Pc=b.r.Pc;//critical pressure kPa
P[1]=P1;//compressor inlet
P[2]=P2;//compressor outlet, evaporator superheated outlet
P[0]=P1-dPevap1; //evaporator saturation exit(x=1) pressure
P[4]=P[0]-dPevap2;//evaporator saturation inlet pressure
P[5]=P[2]-dPcomp1;// mixing point pressure
P[9]=P[5];//flash tank saturated vapour exit
P[8]=P[5];//flash tank inlet
P[3]=P[5];//flash tank saturated liquid exit
P[6]=P6;//condenser inlet
P[12]=P[6];// 12=6i isentropic compressor exit point
P[10]=P[6]-dPcond1; // condenser  exit liquid state)
P[13]=P[10]-dPcond2;// condenser saturated liquid point
P[7]=P[13]-dPcond3; // condenser exit (liquid)
c1=new double[2][20];
c3=new double[2][20];
} 

public void cycle()
{
// compressor saturation point 0
double a1[]=b.property("px",P[0],1.0);
T[0]=a1[1];
h[0]=a1[3];
s[0]=a1[5];
x[0]=a1[6];
// compressor inlet, evaporator exit point 1
T[1]=T[0]+dT1;
a1=b.property("tp",T[1],P[1]);
h[1]=a1[3];
s[1]=a1[5];
x[1]=a1[6];
// isentropic compressor exit point 11 (2i)
s[11]=s[1];
P[11]=P[2];
a1=b.property("ps",P[11],s[11]);
h[11]=a1[3];
T[11]=a1[1];
h[2]=h[1]+(h[11]-h[1])/etaisent2;
// compressor 1 exit point 2
a1=b.property("ph",P[2],h[2]);
s[2]=a1[5];
T[2]=a1[1];
// point 10 condenser saturated vapour point
x[10]=1.0;
a1=b.property("px",P[10],x[10]);
T[10]=a1[1];
h[10]=a1[3];
s[10]=a1[5];
// point 13 condenser saturated liquid point
x[13]=0.0;
a1=b.property("px",P[13],x[13]);
T[13]=a1[1];
h[13]=a1[3];
s[13]=a1[5];
T[7]=T[13]-dT7;
// point 7 condenser exit
if(T[7]==T[13] && P[7]==P[13]) 
{h[7]=h[13];T[7]=T[13];s[7]=s[13];x[7]=x[13];
}
else
{
 a1=b.property("tp",T[7],P[7]);
 h[7]=a1[3];
 s[7]=a1[5];
 x[7]=a1[6];
}
//point 3 flash chamber saturated liquid 
x[3]=0.0;
a1=b.property("px",P[3],x[3]);
T[3]=a1[1];
h[3]=a1[3];
s[3]=a1[5];
x[3]=a1[6];
//point 4 flash chamber saturated liquid 
h[4]=h[3]-dh2;
a1=b.property("ph",P[4],h[4]);
T[4]=a1[1];
h[4]=a1[3];
s[4]=a1[5];
x[4]=a1[6];
//point 9 flash chamber saturated vapour 
x[9]=1.0;
a1=b.property("px",P[9],x[9]);
T[9]=a1[1];
h[9]=a1[3];
s[9]=a1[5];
// point 8 flash chamber input
h[8]=h[7]+dh2;
a1=b.property("ph",P[8],h[8]);
T[8]=a1[1];
s[8]=a1[5];
x[8]=a1[6]; //indicates seperation parameter as well
// point 5 compressor 2 inlet point
h[5]=x[8]*h[9]+(1.0-x[8])*h[2];
a1=b.property("ph",P[5],h[5]);
T[5]=a1[1];
s[5]=a1[5];
//point 12 isentropic compressor2 output
s[12]=s[5];
P[12]=P[6];
a1=b.property("ps",P[12],s[12]);
h[12]=a1[3];
T[12]=a1[1];
h[6]=h[5]+(h[12]-h[5])/etaisent6;
a1=b.property("ph",P[6],h[6]);
T[6]=a1[1];
s[6]=a1[5];
x[6]=a1[6];
 // isentropik compressor 1
Wp1i=m*(1.0-x[8])*(h[11]-h[1]);
//compressor 1
Wp1=m*(1.0-x[8])*(h[2]-h[1]);
Wp=Wp1+Wp2;
// isentropik compressor 2
Wp2i=m*(h[12]-h[5]);
//compressor 2
Wp2=m*(h[6]-h[5]);
Qevaporator=m*(1.0-x[8])*(h[1]-h[4]);
Qcondenser=m*(h[6]-h[7]);
COP_evaporator=Qevaporator/Wp;
COP_condenser_carnot=(T[6]+273.15)/(T[6]-T[0]);
COP_condenser=Qcondenser/Wp;
COP_evap_carnot=(T[0]+273.15)/(T[6]-T[0]);
}

public String toString()
{
cycle();
String ss="two stage flash point refrigeration cycle\n";
ss+="Flash ratio : "+x[8]+"\n";
ss+=" compressor1 work = "+Wp1+" kW\n";
ss+=" compressor2 work = "+Wp2+" kW\n";
ss+=" total compressor work = "+Wp+" kW\n";
ss+=" isentropic compressor 1 work = "+Wp1i+" kW\n";
ss+=" isentropic compressor 2 work = "+Wp2i+" kW\n";
ss+=" total isentropic compressor work = "+(Wp1i+Wp2i)+" kW\n";
ss+=" condenser heat output = "+Qcondenser+" kW\n";
ss+=" evaporator heat input = "+Qevaporator+" kW\n";
ss+=" COP evaporator    = " + COP_evaporator+" kW\n";
ss+=" COP condenser     = " + COP_condenser+" kW\n";
ss+=" COP evaporator  carnot = " + COP_evap_carnot+" kW\n";
ss+=" COP condenser   carnot = " + COP_condenser_carnot+" kW\n";

ss+=" h1 compressor 1 inlet = "+h[1] +" kJ/kg\n";
ss+=" T1 compressor 1 inlet = "+T[1] +" derece C\n";
ss+=" P1 compressor 1 inlet = "+P[1] +" kPa \n";
ss+=" s1 compressor 1 inlet = "+s[1] +" kJ/kgK\n";
ss+=" x1 compressor 1 inlet = "+x[1] +" kgvapor/kgtotal"+"\n";

ss+=" h2 compressor 1 exit = "+h[2] +" kJ/kg\n";
ss+=" T2 compressor 1 exit = "+T[2] +" derece C\n";
ss+=" P2 compressor 1 exit = "+P[2] +" kPa \n";
ss+=" s2 compressor 1 exit = "+s[2] +" kJ/kgK\n";
ss+=" x2 compressor 1 exit = "+x[2] +" kgvapor/kgtotal"+"\n";

ss+=" h5 compressor 2 inlet = "+h[5] +" kJ/kg\n";
ss+=" T5 compressor 2 inlet = "+T[5] +" derece C\n";
ss+=" P5 compressor 2 inlet = "+P[5] +" kPa \n";
ss+=" s5 compressor 2 inlet = "+s[5] +" kJ/kgK\n";
ss+=" x5 compressor 2 inlet = "+x[5] +" kgvapor/kgtotal"+"\n";

ss+=" h6 compressor 2 exit = "+h[6] +" kJ/kg\n";
ss+=" T6 compressor 2 exit = "+T[6] +" derece C\n";
ss+=" P6 compressor 2 exit = "+P[6] +" kPa \n";
ss+=" s6 compressor 2 exit = "+s[6] +" kJ/kgK\n";
ss+=" x6 compressor 2 exit = "+x[6] +" kgvapor/kgtotal"+"\n";

ss+=" h8 flash chamber input = "+h[3] +" kJ/kg\n";
ss+=" T8 flash chamber input = "+T[3] +" derece C\n";
ss+=" P8 flash chamber input = "+P[3] +" kPa \n";
ss+=" s8 flash chamber input = "+s[3] +" kJ/kgK\n";
ss+=" x8 flash chamber input = "+x[3] +" kgvapor/kgtotal"+"\n";

ss+=" h4 expansion valve exit-evaporator inlet = "+h[4] +" kJ/kg\n";
ss+=" T4 expansion valve exit-evaporator inlet = "+T[4] +" derece C\n";
ss+=" P4 expansion valve exit-evaporator inlet = "+P[4] +" kPa \n";
ss+=" s4 expansion valve exit-evaporator inlet = "+s[4] +" kJ/kgK\n";
ss+=" x4 expansion valve exit-evaporator inlet = "+x[4] +" kgvapor/kgtotal"+"\n";
return ss;
}
		
public double[][] TS(double ti)
{
double a[][]=new double[2][10000];
double tc=b.r.Tc;
double dt=(tc-1.0)/100.0;
int i=0;
double t;
for(t=ti;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>=ti;t-=dt)
{double a2[]=b.property("tx",t,1.0);
 a[1][i]=t;
 a[0][i]=a2[5];
 i++;
}
int n=i;
double b[][]=new double[2][n];
for(i=0;i<n;i++) {b[0][i]=a[0][i];b[1][i]=a[1][i];}
return b;	
}
public double[][] line(double P1,double P2,double hi,double ho,int n)
{
double TT1[]=new double[n];
double ss1[]=new double[n];
double aa[];
double p,h;
for(int i=0;i<n;i++)
{p=P1+i*(P2-P1)/(n-1);
 h=hi+i*(ho-hi)/(n-1);
 aa=b.property("ph",p,h);
 TT1[i]=aa[1];
 ss1[i]=aa[5];
}
double a[][]={TT1,ss1};
return a;
}
public double[][] line1(double T1,double T2,double Pi,double Po,int n)
{
double TT1[]=new double[n];
double ss1[]=new double[n];
double aa[];
double P;
double x=1.0;
aa=b.property("tx",T1,x);
TT1[0]=T1;
ss1[0]=aa[5];
for(int i=1;i<n;i++)
{TT1[i]=T1+i*(T2-T1)/(n-1);
 P=Pi+i*(Po-Pi)/(n-1);
 aa=b.property("tp",TT1[i],P);
 ss1[i]=aa[5];
}
double a[][]={TT1,ss1};
Text.printT(a);
return a;
}

public void plot(double ti)
{
 Plot pp=new Plot(TS(ti));
 //enthalpy line
 double a34[][]=line(P[3],P[4],h[3],h[4],15);
 double a78[][]=line(P[7],P[8],h[7],h[7],15);
 double tt[]=a34[0];
 double ss[]=a34[1];
 pp.addData(ss,tt);

 tt=a78[0];
 ss=a78[1];
 pp.addData(ss,tt);

 double a10[][]=line1(T[0],T[1],P[0],P[1],15);
 tt=a10[0];
 ss=a10[1];
 pp.addData(ss,tt);
 
 double a92[][]=line1(T[9],T[2],P[9],P[2],16);
  tt=a92[0];
  ss=a92[1];
  pp.addData(ss,tt);
  double a10_6[][]=line1(T[10],T[6],P[10],P[6],16);
  tt=a10_6[0];
  ss=a10_6[1];
  pp.addData(ss,tt);
 double tt1[]={T[10],T[13],T[7]};
 double ss1[]={s[10],s[13],s[7]};
 pp.addData(ss1,tt1);
 double tt2[]={T[4],T[0]};
 double ss2[]={s[4],s[0]};
 pp.addData(ss2,tt2);
 double tt3[]={T[9],T[3]};
 double ss3[]={s[9],s[3]};
 pp.addData(ss3,tt3);
 double tt4[]={T[1],T[2]};
 double ss4[]={s[1],s[2]};
 pp.addData(ss4,tt4);
  double tt5[]={T[5],T[6]};
 double ss5[]={s[5],s[6]};
 pp.addData(ss5,tt5);
 pp.addData(ss4,tt4);
  double tt6[]={T[1],T[11]};
 double ss6[]={s[1],s[11]};
 pp.addData(ss6,tt6,1);
  double tt7[]={T[5],T[12]};
 double ss7[]={s[5],s[12]};
 pp.addData(ss7,tt7,1);
 pp.setPlabel("Flash chamber 2 stage refrigeration cycle refrigerant :"+refname);
 pp.setYlabel("T, degree C");
 pp.setXlabel("s entropy kJ/kgK");
 pp.plot();
}
}