public class ref_cycle1
{
// a R134 ideal carnot 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[];
double COP_condenser,COP_evaporator,COP_evap_carnot,COP_condenser_carnot;
double m;//kg/s
double Wp,W;//work
double Qevaporator,Qcondenser;
refrigerantEN b;
double c1[][];
double c3[][];
public ref_cycle1(double mi,double P1, double P2)
{
m=mi;
b=new refrigerantEN("R134a");
T=new double[9];
P=new double[9];
h=new double[9];
s=new double[9];
x=new double[9];

P[1]=P1;//compressor inlet
P[2]=P2;//compressor outlet
c1=new double[2][20];
c3=new double[2][20];
} 
public double[][] line(double P1,double P2,double hi,int n)
{
double TT1[]=new double[n];
double ss1[]=new double[n];
double aa[];
double p;
for(int i=0;i<n;i++)
{p=P1+i*(P2-P1)/(n-1);
 aa=b.property("ph",p,hi);
 TT1[i]=aa[1];
 ss1[i]=aa[5];
}
double a[][]={TT1,ss1};
return a;
}

public void cycle()
{
// isentropic compressor output
double a1[]=b.property("px",P[2],1.0);
h[2]=a1[3];
s[2]=a1[5];
T[2]=a1[1];
// isentropik compressor input
s[1]=s[2];
a1=b.property("ps",P[1],s[1]);
T[1]=a1[1];
h[1]=a1[3];
x[1]=a1[6];

//expansion valve input-condenser output
P[3]=P[2];
a1=b.property("px",P[3],0.0);
h[3]=a1[3];
s[3]=a1[5];
T[3]=a1[1];
//expansion valve output
P[4]=P[1];
h[4]=h[3];
a1=b.property("ph",P[4],h[4]);
s[4]=a1[5];
T[4]=a1[1];
x[4]=a1[6];


// isentropik compressor
Wp=m*(h[2]-h[1]);
W=Wp;
Qevaporator=m*(h[1]-h[4]);
Qcondenser=m*(h[2]-h[3]);
COP_evaporator=Qevaporator/W;
COP_condenser_carnot=(T[2]+273.15)/(T[2]-T[1]);
COP_condenser=Qcondenser/W;
COP_evap_carnot=(T[1]+273.15)/(T[2]-T[1]);
}

public String toString()
{
cycle();
String ss="R134a ideal refrigeration cycle 1\n";
ss+=" compressor work = "+Wp+" kW\n";
ss+=" net work input= "+W+" 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+=" T2 compressor output = "+T[3] +" derece C\n";
ss+=" P2 compressor output = "+P[3] +" kPa \n";
ss+=" s2 compressor output = "+s[2] +" kJ/kgK\n";
ss+=" x2 compressor output = "+x[2] +" 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 a[][]=new double[2][421];
double tc=b.r.Tc;
System.out.println("tc="+tc);
double dt=(tc-10.0)/100.0;
int i=0;
double t;
for(t=-90.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>=-90;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(TS());
 double a[][]=line(P[3],P[4],h[3],15);
 double tt[]=a[0];
 double ss[]=a[1];
 System.out.println("n="+tt.length);
 double t1[]=new double[19];
 double s1[]=new double[19];
 t1[0]=T[1];t1[1]=T[2];t1[2]=T[3];
 s1[0]=s[1];s1[1]=s[2];s1[2]=s[3];
 for(int i=0;i<tt.length;i++)
 {t1[i+3]=tt[i];s1[i+3]=ss[i];}
 t1[18]=T[1];
 s1[18]=s[1];
 pp.setPlabel("Ideal refrigeration cycle 1 refrigeration R134a ");
 pp.setYlabel("T, degree C");
 pp.setXlabel("s entropy kJ/kgK");
 pp.addData(s1,t1);
 pp.plot();
}
}