public class ref_cycle5
{
// Two stage flash chamber 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 Wp;//compressor work
double Wpi;//isentropic compressor work
double dPevap1;// evaporator pressure drop saturated region
double dPevap2;// evaporator pressure drop superheated region
double dPcond1;// condenser pressure drop superheated region
double dPcond2;// condenser pressure drop saturated region
double dPcond3;// condenser pressure drop liquid region
double dh;     // expansion valve enthalpy chenge (heat loss)
double dT1,dT6;
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 T2, double P2,double dT6i,double dPevap1i,double dPevap2i,double dPcond1i,double dPcond2i,double dPcond3i,double dhi)
{
m=mi;
refname=refnamei;
dPevap1=dPevap1i;
dPevap2=dPevap2i;
dPcond1=dPcond1i;
dPcond2=dPcond2i;
dPcond3=dPcond3i;
dh=dhi;
dT1=dT1i;
dT6=dT6i;
b=new refrigerantEN(refname);
T=new double[9];
P=new double[9];
h=new double[9];
s=new double[9];
x=new double[9];
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]=P2-dPcond1;//condenser saturation inlet (x=1)
P[6]=P[5]-dPcond2;//condenser saturation exit (x=0)
P[3]=P[6]-dPcond3;//condenser  exit liquid state)
T[2]=T2;
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];
System.out.println("point = "+0+" T="+T[0]+"P="+P[0]+"h="+h[0]+"s="+s[0]+"x="+x[0]);
// 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];
System.out.println("point = "+1+" T="+T[1]+"P="+P[1]+"h="+h[1]+"s="+s[1]+"x="+x[1]);
/*
// isentropic compressor exit point 7
s[7]=s[1];
P[7]=P[2];
a1=b.property("ps",P[7],s[7]);
h[7]=a1[3];
T[7]=a1[1];
System.out.println("point = "+7+" T="+T[7]+"P="+P[7]+"h="+h[7]+"s="+s[7]+"x="+x[7]);
*/
//compressor exit point 2
a1=b.property("tp",T[2],P[2]);
h[2]=a1[3];
s[2]=a1[5];
x[2]=a1[6];
// check if cycle is supercritical
if(P[3]>Pc) 
{System.out.println("condenser is supercritical");
supercritical=true;
}
else supercritical=false;
//expansion valve input
if(!supercritical)
{
//saturation point
a1=b.property("px",P[5],1.0);
h[5]=a1[3];
T[5]=a1[1];
s[5]=a1[5];	
a1=b.property("px",P[6],0.0);
h[6]=a1[3];
T[6]=a1[1];
s[6]=a1[5];	
T[3]=T[6]-dT6;
a1=b.property("tp",T[3],P[3]);
}
else
{
T[3]=Tc-dT6;
a1=b.property("tp",T[3],P[3]);	
}
h[3]=a1[3];
T[3]=a1[1];
s[3]=a1[5];
// point 4 evaporator inlet, expansion valve output
h[4]=h[3]+dh;
a1=b.property("ph",P[4],h[4]);
T[4]=a1[1];
s[4]=a1[5];		
// isentropik compressor
Wpi=m*(h[7]-h[1]);
//compressor
Wp=m*(h[2]-h[1]);
Qevaporator=m*(h[1]-h[4]);
Qcondenser=m*(h[2]-h[3]);
COP_evaporator=Qevaporator/Wp;
COP_condenser_carnot=(T[2]+273.15)/(T[2]-T[1]);
COP_condenser=Qcondenser/Wp;
COP_evap_carnot=(T[1]+273.15)/(T[2]-T[1]);
}

public String toString()
{
cycle();
String ss="R134a ideal refrigeration cycle 2\n";
ss+=" compressor work = "+Wp+" kW\n";
ss+=" isentropic compressor work = "+Wpi+" 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 input = "+h[1] +" kJ/kg\n";
ss+=" T1 compressor input = "+T[1] +" derece C\n";
ss+=" P1 compressor input = "+P[1] +" kPa \n";
ss+=" s1 compressor input = "+s[1] +" kJ/kgK\n";
ss+=" x1 compressor input = "+x[1] +" kgvapor/kgtotal"+"\n";
ss+=" h2 compressor input = "+h[2] +" kJ/kg\n";
ss+=" T2 compressor output = "+T[2] +" derece C\n";
ss+=" P2 compressor output = "+P[2] +" kPa \n";
ss+=" s2 compressor output = "+s[2] +" kJ/kgK\n";
ss+=" x2 compressor output = "+x[2] +" kgvapor/kgtotal"+"\n";
if(!supercritical)
{
ss+=" h5 compressor input = "+h[5] +" kJ/kg\n";
ss+=" T5 condenser saturation = "+T[5] +" derece C\n";
ss+=" P5 condenser saturation = "+P[5] +" kPa \n";
ss+=" s5 condenser saturation = "+s[5] +" kJ/kgK\n";
ss+=" x5 condenser saturation = "+x[5] +" kgvapor/kgtotal"+"\n";
ss+=" h6 compressor input = "+h[6] +" kJ/kg\n";
ss+=" T6 condenser saturation = "+T[6] +" derece C\n";
ss+=" P6 condenser saturation = "+P[6] +" kPa \n";
ss+=" s6 condenser saturation = "+s[6] +" kJ/kgK\n";
ss+=" x6 condenser saturation = "+x[6] +" kgvapor/kgtotal"+"\n";
}
ss+=" h3 turbine input = "+h[3] +" kJ/kg\n";
ss+=" T3 turbine input = "+T[3] +" derece C\n";
ss+=" P3 turbine input = "+P[3] +" kPa \n";
ss+=" s3 turbine input = "+s[3] +" kJ/kgK\n";
ss+=" x3 turbine input = "+x[3] +" kgvapor/kgtotal"+"\n";
ss+=" h4 turbine output = "+h[4] +" kJ/kg\n";
ss+=" T4 turbine output = "+T[4] +" derece C\n";
ss+=" s4 turbine output = "+s[4] +" kJ/kgK\n";
ss+=" x4 turbine output = "+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;
for(int i=0;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};
return a;
}

public void plot(double ti)
{
 Plot pp=new Plot(TS(ti));
 double a[][]=line(P[3],P[4],h[3],h[4],15);
 double tt[]=a[0];
 double ss[]=a[1];
  pp.addData(ss,tt);
 double b[][]=new double[2][15];
 double c[][];
 if(!supercritical)
 { c=line1(T[2],T[5],P[2],P[5],13);
   for(int i=0;i<c[0].length;i++)
   {b[0][i]=c[0][i];b[1][i]=c[1][i];}
    b[0][13]=T[6];b[1][13]=s[6];
    b[0][14]=T[3];b[1][14]=s[3];
 }
 else
 {c=line1(T[2],T[3],P[2],P[3],15);b=c;}  
 tt=b[0];
 ss=b[1];
 pp.addData(ss,tt);	 
 double t1[]=new double[4];
 double s1[]=new double[4];
 t1[0]=T[4];t1[1]=T[0];t1[2]=T[1];t1[3]=T[2];
 s1[0]=s[4];s1[1]=s[0];s1[2]=s[1];s1[3]=s[2];
 pp.addData(s1,t1);
 pp.setPlabel("Ideal refrigeration cycle 1 refrigeration R134a ");
 pp.setYlabel("T, degree C");
 pp.setXlabel("s entropy kJ/kgK");
 pp.plot();
}
}