//======================================================
// java Termodinamik paketi
// Class sogutmacevrimi1 
// Dr. Turhan Coban
// Ege Üniversitesi Mühendislik fakultesi
// Makina Bölümü
// 
//======================================================
// Dosya adı   : sogutmacevrimi1.java
// Bu dosya soğutma soğutma çevrimini hesaplar 
// 
// =====================================================
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
//---- cases of String s --------
//    tv tp th tu ts tx
// pv    pt ph pu ps px
// vp    vt

//---- Exit properties ---------
// a[0] P Pressure kPa
// a[1] t temperature degree C
// a[2] v specific volume m^3/kg
// a[3] h KJ/kg 
// a[4] u KJ/kg
// a[5] s KJ/kgK
// a[6] x kg vapor/kg total phase
// a[7] ro density kg/m^3

class refrigerationcycle
{
  refrigerant rs;
  double T[];
  double P[];
  double h[];
  double s[];
  double COP_evap;
  double COP_cond;
  double W;
  double Q_cond;
  double Q_evap;
  double isentropic_efficiency;
  double mechanical_efficiency;
  double m;
  double a[]=new double[5];
  double ZX[]=new double[8];
  double ZY[]=new double[8];
  double WX[]=new double[8];
  double WY[]=new double[8];
  
  double ZXi[]=new double[2];
  double ZYi[]=new double[2];
  double WXi[]=new double[2];
  double WYi[]=new double[2];
  String s2[];
  
  public refrigerationcycle(String s1, double Q_evap,
                          double Tevap,double dPevap,double dTevap,
                          double Tcond,double dPcond,double dTcond,
                          double dhexpansion,double Tkompout,double etam)
  {
   /*	  
   s1          : refrigerant name
   Tevap       : evaporation (saturation) temperature
   dTevap      : degree of superheat in evaporator
   dPevap      : evaporator pressure drop
   Tcond       : condensing (saturation) temperature
   dTcond      : degree of subcooling in condenser
   dPcond      : condenser pressure drop
   dhexpansion : entalphy increase in expansion valve
   Tkompout    : compressor output temperature
   etam        : mechanical efficiency
   */
  yenidenhesapla(s1,Q_evap,Tevap,dPevap,dTevap,Tcond,dPcond,dTcond,dhexpansion,Tkompout,etam);
 
  }
  
 public void yenidenhesapla(String s1, double Q_evap,
                          double Tevap,double dPevap,double dTevap,
                          double Tcond,double dPcond,double dTcond,
                          double dhexpansion,double Tkompout,double etam) 
  {
  rs=new refrigerant(s1);
  T=new double[9];
  P=new double[9];
  h=new double[9];
  s=new double[9];
    // point 6 condenser saturated vapor point
  T[6]=Tcond;
  a=rs.property("tx",T[6],1.0);
  h[6]=a[3];
  s[6]=a[5];
  P[6]=a[0];
  ZX[2]=h[6];
  ZY[2]=P[6];
  WX[2]=s[6];
  WY[2]=T[6];
  //point 7 condenser saturated liquid point
  P[7]=P[6]-dPcond;
  a=rs.property("px",P[7],0.0);
  h[7]=a[3];
  s[7]=a[5];
  T[7]=a[1];
  ZX[3]=h[7];
  ZY[3]=P[7];
  WX[3]=s[7];
  WY[3]=T[7];
  // point 3 condenser exit ?????
  T[3]=T[7]-dTcond;
  P[3]=P[7];
  a=rs.property("tx",T[3],0.0);
  h[3]=a[3];
  s[3]=a[5]-(h[3]-h[7])/(T[3]+273);
  ZX[4]=h[3];
  ZY[4]=P[3];
  WX[4]=s[3];
  WY[4]=T[3];
  //point 8 evaporator inlet saturated liquid
  T[8]=Tevap;
  a=rs.property("tx",T[8],0.0);
  h[8]=a[3];
  s[8]=a[5];
  P[8]=a[0];
  //point 0 evaporator inlet saturated vapor
  P[0]=P[8]-dPevap;
  a=rs.property("px",P[0],1.0);  
  T[0]=a[1];
   //System.out.println("T0="+ T[0]+"P0="+P[0]);
  h[0]=a[3];
  s[0]=a[5];
  ZX[6]=h[0];
  ZY[6]=P[0];
  WX[6]=s[0];
  WY[6]=T[0];
  //point 4 evaporator inlet
  h[4]=h[3]+dhexpansion;
  
  double x4=(h[4]-h[8])/(h[0]-h[8]);
  s[4]=s[8]*(1.0-x4)+s[0]*x4;
  h[4]=h[8]*(1.0-x4)+h[0]*x4;
  P[4]=P[8]*(1.0-x4)+P[0]*x4;
  T[4]=T[8]*(1.0-x4)+T[0]*x4;
  //System.out.println("x4="+x4+"h4="+h[4]+"T4="+ T[4]+"P4="+P[4]);
  ZX[5]=h[4];
  ZY[5]=P[4];
  WX[5]=s[4];
  WY[5]=T[4];
 
  // point 1 (compressor inlet)
  T[1]=T[0]+dTevap;
  P[1]=P[0]; 
  //System.out.println("T1="+ T[1]+"P1="+P[1]);
  if(dTevap==0)
  {
  T[1]=T[0];
  h[1]=h[0];
  P[1]=P[0];
  s[1]=s[0];
  }
  else
  {
  a=rs.property("tp",T[1],P[1]);
  h[1]=a[3];
  s[1]=a[5];
  }
  ZX[0]=h[1];
  ZY[0]=P[1];
  WX[0]=s[1];
  WY[0]=T[1];
  ZXi[0]=h[1];
  ZYi[0]=P[1];
  WXi[0]=s[1];
  WYi[0]=T[1];
  ZX[7]=h[1];
  ZY[7]=P[1];
  WX[7]=s[1];
  WY[7]=T[1];
  // point 2
  // point 2 compressor exit-condenser inlet
  T[2]=Tkompout;
  P[2]=P[6];
  a=rs.property("tp",T[2],P[2]);
  h[2]=a[3];
  s[2]=a[5]; 
  //if(s[2]<s[1]) System.out.println("ERROR wrong temperature value is given for compressor exit negative entropy change increse the compressor exit temperature");
  
  // point 2s (5) isentropic compressor exit
  P[2] = P[6];
  a=rs.property("ps",P[2],s[1]);
  h[5]=a[3];
  s[5]=a[5];
  T[5]=a[1];
  P[5]=P[3];
  isentropic_efficiency=(h[5]-h[1])/(h[2]-h[1]); //???????
  ZX[1]=h[2];
  ZY[1]=P[2];
  WX[1]=s[2];
  WY[1]=T[2]; 
     
  ZXi[1]=h[5];
  ZYi[1]=P[5];
  WXi[1]=s[5];
  WYi[1]=T[5]; 
  m=Q_evap/(h[1]-h[4]);
  Q_cond = m*( h[2] - h[3] );
  W=m*(h[2]-h[1])/etam;
  mechanical_efficiency=etam;
  COP_evap=Q_evap/W;
  COP_cond=Q_cond/W;  
    }
  
  public void plot_Ph()
  {
  double t; 	
  double tc=rs.r.Tc;  
  double x[],y[];int i=0;
  x=new double[800];
  y=new double[800];
  double tmin=rs.r.rd.tmin;
  //System.out.println("soğutkan = "+rs.r.refrigerantName);
  for(t=tmin;t<=(tc-2);t+=2.0)
     { a=rs.property("tx",t,0.0);
	   x[i]=a[3];//h
	   y[i]=a[0];//P
       i++;
     }
       a=rs.property("tx",tc,0.0);
	   x[i]=a[3];//h
	   y[i]=a[0];//P
  i++;
  double tt=tc;
   for(t=tt;t>=tmin;t-=2.0)
     {a=rs.property("tx",t,1.0);
	   x[i]=a[3];//h
	   y[i]=a[0];//P
       i++;}
       a=rs.property("tx",tc,1.0);
	   x[i]=a[3];//h
	   y[i]=a[0];//P        
  double xx[]=new double[i];
  double yy[]=new double[i];
  for(int j=0;j<i;j++) {xx[j]=x[j];yy[j]=y[j];}
  Plot pp=new Plot(xx,yy);
  pp.addData(ZX,ZY,5,0,0,255);
  pp.addData(ZXi,ZYi,4,0,0,0);
  pp.setYlogScaleOn();
  pp.setPlabel("Pressure - Enthalpy diagram : "+rs.r.refrigerantName);
  pp.setXlabel("Enthalpy h KJ/kg");
  pp.setYlabel("Pressure P kPa");
  pp.setXtic(10);
  pp.setYtic(10);
  pp.setXgrid(1);
  pp.setYgrid(1);
  pp.plot();
  }
  
  public void plot_Ts()
  {
  double t; 	
  double tc=rs.r.Tc;  
  double x[],y[];int i=0;
  x=new double[800];
  y=new double[800];
  double tmin=rs.r.rd.tmin;
  //System.out.println("soğutkan = "+rs.r.refrigerantName);
  for(t=tmin;t<=(tc-2);t+=2.0)
     { a=rs.property("tx",t,0.0);
	   x[i]=a[5];y[i]=a[1];
	   //System.out.println(x[i]+" "+y[i]);
       i++;}
       a=rs.property("tx",tc,0.0);
	   x[i]=a[5];y[i]=a[1];
	   //System.out.println(x[i]+" "+y[i]);
  i++;
  double tt=tc;
   for(t=tt;t>=tmin;t-=2.0)
     {a=rs.property("tx",t,1.0);
	   x[i]=a[5];y[i]=a[1];
	   //System.out.println(x[i]+" "+y[i]);
       i++;}
       a=rs.property("tx",tc,1.0);
	   x[i]=a[5];y[i]=a[1];  
	   //System.out.println(x[i]+" "+y[i]);      
  double xx[]=new double[i];
  double yy[]=new double[i];
  for(int j=0;j<i;j++) {xx[j]=x[j];yy[j]=y[j];}
  Plot pp=new Plot(xx,yy);
  pp.addData(WX,WY,5,0,0,255);
  pp.addData(WXi,WYi,4,0,0,255);
  //pp.setYlogScaleOn();
  //pp.setPlabel("Sıcalık - Entropi Diyagramı");
  pp.setPlabel("Temperature - Entropy diagram : "+rs.r.refrigerantName);
  pp.setXlabel("Entropy h KJ/kgK");
  pp.setYlabel("Temperature T degree C");
  pp.setXtic(10);
  pp.setYtic(10);
  pp.setXgrid(1);
  pp.setYgrid(1);
  pp.plot();
  }
  
  public double[] cikti()
  {
   double a[]=new double[18];
   a[0]=m;
   a[1]=W;
   a[2]=Q_evap;
   a[3]=Q_cond;
   a[4]=COP_evap;
   a[5]=COP_cond;
   a[6]=T[1];
   a[7]=P[1];
   a[8]=h[1];
   a[9]=T[2];
   a[10]=P[2];
   a[11]=h[2];
   a[12]=T[3];
   a[13]=P[3];
   a[14]=h[3];
   a[15]=T[4];
   a[16]=P[4];
   a[17]=h[4];
   return a;
  }
  
   public String[][] toString(String s1, double Q_evap,
                          double Tevap,double dPevap,double dTevap,
                          double Tcond,double dPcond,double dTcond,
                          double dhexpansion,double Tkompout,double etam)
   {
   yenidenhesapla(s1,Q_evap,Tevap,dPevap,dTevap,Tcond,dPcond,dTcond,dhexpansion,Tkompout,etam);  
   String s2[][]=new String[31][3];
   s2[0][0]=" Refrigerant name :";
   s2[1][0]="m mass flow rate  :";
   s2[2][0]="W compressor   :";
   s2[3][0]="Q evaporation   :";
   s2[4][0]="Q condensing      :";
   s2[5][0]="COP buharlaşma :";
   s2[6][0]="COP evapotation    :";
   s2[7][0]="evaporator exit T1             :";
   s2[8][0]="evaporator exit P1             :";
   s2[9][0]="evaporator exit h1             :";
   s2[10][0]="compressor exit T2             :";
   s2[11][0]="compressor exit P2             :";
   s2[12][0]="compressor exit h2             :";
   s2[13][0]="condenser exit T3             :";
   s2[14][0]="condenser exit P3             :";
   s2[15][0]="condenser exit h3             :";
   s2[16][0]="expansion valve exit T4             :";
   s2[17][0]="expansion valve exitP4             :";
   s2[18][0]="expansion valve exit h4             :";
   s2[19][0]="isentropic compressor exit T5 :";
   s2[20][0]="isentropic compressor exit P5 :";
   s2[21][0]="isentropic compressor exit h5 :";
   s2[22][0]="condenser saturation vapor x=1 T6 :";
   s2[23][0]="condenser saturation vapor x=1 P6 :";
   s2[24][0]="condenser saturation vapor x=1 h6 :";
   s2[25][0]="condenser saturation liquid x=0 T7 :";
   s2[26][0]="condenser saturation liquid x=0 P7 :";
   s2[27][0]="condenser saturation liquid x=0 h7 :";
   s2[28][0]="evaporator saturation vapor x=1 T0 :";
   s2[29][0]="evaporator saturation vapor x=1 P0 :";
   s2[30][0]="evaporator saturation vapor x=1 h0 :";
   s2[0][2]=" ";
   s2[1][2]="kg/s";
   s2[2][2]="kW  ";
   s2[3][2]="kW  ";
   s2[4][2]="kW  ";
   s2[5][2]="    ";
   s2[6][2]="    ";
   s2[7][2]="degree C";
   s2[8][2]="kPa     ";
   s2[9][2]="kJ/kg";
   s2[10][2]="degree C";
   s2[11][2]="kPa     ";
   s2[12][2]="kJ/kg";
   s2[13][2]="degree C";
   s2[14][2]="kPa     ";
   s2[15][2]="kJ/kg";
   s2[16][2]="degree C";
   s2[17][2]="kPa     ";
   s2[18][2]="kJ/kg";
   s2[19][2]="degree C";
   s2[20][2]="kPa     ";
   s2[21][2]="kJ/kg";
   s2[22][2]="degree C";
   s2[23][2]="kPa     ";
   s2[24][2]="kJ/kg";
   s2[25][2]="degree C";
   s2[26][2]="kPa     ";
   s2[27][2]="kJ/kg";
   s2[28][2]="degree C";
   s2[29][2]="kPa     ";
   s2[30][2]="kJ/kg";
   s2[0][1]=""+rs.r.rd.openName;
   s2[1][1]=""+m;
   s2[2][1]=""+W;
   s2[3][1]=""+Q_evap;
   s2[4][1]=""+Q_cond;
   s2[5][1]=""+COP_evap;
   s2[6][1]=""+COP_cond;
   s2[7][1]=""+T[1];
   s2[8][1]=""+P[1];
   s2[9][1]=""+h[1];
   s2[10][1]=""+T[2];
   s2[11][1]=""+P[2];
   s2[12][1]=""+h[2];
   s2[13][1]=""+T[3];
   s2[14][1]=""+P[3];
   s2[15][1]=""+h[3];
   s2[16][1]=""+T[4];
   s2[17][1]=""+P[4];
   s2[18][1]=""+h[4];
   s2[19][1]=""+T[5];
   s2[20][1]=""+P[5];
   s2[21][1]=""+h[5];
   s2[22][1]=""+T[6];
   s2[23][1]=""+P[6];
   s2[24][1]=""+h[6];;
   s2[25][1]=""+T[7];
   s2[26][1]=""+P[7];
   s2[27][1]=""+h[7];
   s2[28][1]=""+T[7];
   s2[29][1]=""+P[7];
   s2[30][1]=""+h[7];
   return s2;
}
 
}  