//======================================================
// java Termodinamik paketi
// Class sogutmacevrimi 
// Dr. Turhan Coban
// Ege Üniversitesi Mühendislik fakultesi
// Makina Bölümü
// 
//======================================================
// Dosya adı   : sogutmacevrimi.java
// Bu dosya soğutma soğutma çevrimini hesaplar 
// 
// =====================================================
import java.io.*;
import java.net.*;
import java.util.*;

//---- 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 sogutmacevrimi
{
  refrigerant rs;
  double T[];
  double P[];
  double h[];
  double COP_evap;
  double COP_cond;
  double W;
  double Q_cond;
  double Q_evap;
  double m;
  double a[]=new double[5];
  double ZX[]=new double[5];
  double ZY[]=new double[5];
  String s;

  public sogutmacevrimi()
  {
  s="R22";
  double Q_evap=10.0;
  double Pevap=245.53;
  double dPevap=0.0;
  double dTevap=0.0;
  double Pcond=1192.12;
  double dPcond=0.0;
  double dTcond=0.0;  
  double dhgenlesme=0.0;
  double isentropikverim=0.9;	  
  rs=new refrigerant(s);
  T=new double[8];
  P=new double[8];
  h=new double[8];
  P[1]=Pevap-dPevap;  
  a=rs.property("px",P[1],1.0);
  T[0]=a[1];
  h[0]=a[3];
  T[1]=T[0]+dTevap;
  if(dTevap==0)
  {
  h[1]=h[0];
  }
  else
  {
  a=rs.property("tp",T[1],P[1]);
  h[1]=a[3];
  }
  ZX[0]=h[1];
  ZY[0]=P[1];
  double entropi1=a[5];
  P[2]=Pcond;
  a=rs.property("ps",P[2],entropi1);
  h[5]=a[3];
  h[2]=h[1]+(h[5]-h[1])/isentropikverim;
  a=rs.property("ph",P[2],h[2]);
  T[2]=a[1];
  ZX[1]=h[2];
  ZY[1]=P[2];
  P[3]=P[2]-dPcond;
  a=rs.property("px",P[3],0.0);
  T[6]=a[1];
  T[3]=T[6]-dTcond;
  a=rs.property("tp",T[3],P[3]);
  h[3]=a[3];
  ZX[2]=h[3];
  ZY[2]=P[3];  
  h[4]=h[3]+dhgenlesme;
  P[4]=Pevap;
  ZX[3]=h[4];
  ZY[3]=P[4];
  a=rs.property("px",P[4],0.0);
  T[4]=a[1];
  m=Q_evap/(h[1]-h[4]);
  Q_cond=m*(h[2]-h[3]);
  W=m*(h[2]-h[1]);
  COP_evap=Q_evap/W;
  COP_cond=Q_cond/W;  
  ZX[4]=ZX[0];
  ZY[4]=ZY[0];
  }
  
  public sogutmacevrimi(String s1, double Q_evap,
                          double Pevap,double dPevap,double dTevap,
                          double Pcond,double dPcond,double dTcond,
                          double dhgenlesme,double isentropikverim)
  {
  
  s=s1;
  rs=new refrigerant(s);
  T=new double[8];
  P=new double[8];
  h=new double[8];
  P[1]=Pevap-dPevap;  
  a=rs.property("px",P[1],1.0);
  T[0]=a[1];
  h[0]=a[3];
  T[1]=T[0]+dTevap;
  if(dTevap==0)
  {
  h[1]=h[0];
  }
  else
  {
  a=rs.property("tp",T[1],P[1]);
  h[1]=a[3];
  }
  ZX[0]=h[1];
  ZY[0]=P[1];
  P[2]=Pcond;
  double entropi1=a[5];
  a=rs.property("ps",P[2],entropi1);
  h[5]=a[3];
  h[2]=h[1]+(h[5]-h[1])/isentropikverim;
  System.out.println("h5="+h[5]+"h2="+h[2]);
  a=rs.property("ph",P[2],h[2]);
  T[2]=a[1];
  ZX[1]=h[2];
  ZY[1]=P[2];
  P[3]=P[2]-dPcond;
  a=rs.property("px",P[3],0.0);
  T[6]=a[1];
  T[3]=T[6]-dTcond;
  a=rs.property("tp",T[3],P[3]);
  h[3]=a[3];
  ZX[2]=h[3];
  ZY[2]=P[3];  
  h[4]=h[3]+dhgenlesme;
  P[4]=Pevap;
  ZX[3]=h[4];
  ZY[3]=P[4];
  a=rs.property("px",P[4],0.0);
  T[4]=a[1];
  m=Q_evap/(h[1]-h[4]);
  Q_cond=m*(h[2]-h[3]);
  W=m*(h[2]-h[1]);
  COP_evap=Q_evap/W;
  COP_cond=Q_cond/W;  
  ZX[4]=ZX[0];
  ZY[4]=ZY[0];
  }
  
  public void yenidenhesapla(String s1, double Q_evap,
                          double Pevap,double dPevap,double dTevap,
                          double Pcond,double dPcond,double dTcond,
                          double dhgenlesme,double isentropikverim)
  {
  s=s1;
  rs=new refrigerant(s);
  T=new double[8];
  P=new double[8];
  h=new double[8];
  P[1]=Pevap-dPevap;  
  a=rs.property("px",P[1],1.0);
  T[0]=a[1];
  h[0]=a[3];
  T[1]=T[0]+dTevap;
  if(dTevap==0)
  {
  h[1]=h[0];
  }
  else
  {
  a=rs.property("tp",T[1],P[1]);
  h[1]=a[3];
  }
  ZX[0]=h[1];
  ZY[0]=P[1];
  double entropi1=a[5];
  P[2]=Pcond;
  a=rs.property("ps",P[2],entropi1);
  h[5]=a[3];
  h[2]=h[1]+(h[5]-h[1])/isentropikverim;
  a=rs.property("ph",P[2],h[2]);
  T[2]=a[1];
  ZX[1]=h[2];
  ZY[1]=P[2];
  P[3]=P[2]-dPcond;
  a=rs.property("px",P[3],0.0);
  T[6]=a[1];
  T[3]=T[6]-dTcond;
  a=rs.property("tp",T[3],P[3]);
  h[3]=a[3];
  ZX[2]=h[3];
  ZY[2]=P[3];  
  h[4]=h[3]+dhgenlesme;
  P[4]=Pevap;
  ZX[3]=h[4];
  ZY[3]=P[4];
  a=rs.property("px",P[4],h[4]);
  T[4]=a[1];
  //System.out.println("T4="+T[4]);
  m=Q_evap/(h[1]-h[4]);
  Q_cond=m*(h[2]-h[3]);
  W=m*(h[2]-h[1]);
  COP_evap=Q_evap/W;
  COP_cond=Q_cond/W;  
  ZX[4]=ZX[0];
  ZY[4]=ZY[0];
  }
  
  public void plot()
  {
  double t; 	  
  double x[],y[];int i=0;
  x=new double[800];
  y=new double[800];
  for(t=-40;t<=rs.r.Tc;t+=2.0)
     {a=rs.property("tx",t,0.0);y[i]=a[0];x[i]=a[3];
     //System.out.println("i="+i+"t= "+t+" x="+x[i]+" y="+y[i]);
     i++;}
  a=rs.property("tx",rs.r.Tc,1.0);y[i]=a[0];x[i]=a[3];
  System.out.println("i="+i+x[i]+" "+y[i]);
  i++;
  double tt=t;
   for(t=tt;t>=-40;t-=2.0)
     {a=rs.property("tx",t,1.0);y[i]=a[0];x[i]=a[3];
     //System.out.println("i="+i+x[i]+" "+y[i]);
     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(ZX,ZY,5,0,0,255);
  pp.setYlogScaleOn();
  pp.setPlabel("Basınç - Entalpi Diyagramı");
  pp.setXlabel("Entalpi h KJ/kg");
  pp.setYlabel("Basınç P kPa");
  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 s, double Q_evap,
                          double Pevap,double dPevap,double dTevap,
                          double Pcond,double dPcond,double dTcond,
                          double dhgenlesme,double isentropikverim)
   {
   yenidenhesapla(s, Q_evap,Pevap,dPevap,dTevap,Pcond,dPcond,dTcond,
   dhgenlesme,isentropikverim);	   
   String s1[][]=new String[18][3];
   s1[0][0]="m :";
   s1[1][0]="W kompresör    :";
   s1[2][0]="Q buharlaşma   :";
   s1[3][0]="Q yoğuşma      :";
   s1[4][0]="COP buharlaşma :";
   s1[5][0]="COP yoğuşma    :";
   s1[6][0]="T1             :";
   s1[7][0]="P1             :";
   s1[8][0]="h1             :";
   s1[9][0]="T2             :";
   s1[10][0]="P2             :";
   s1[11][0]="h2             :";
   s1[12][0]="T3             :";
   s1[13][0]="P3             :";
   s1[14][0]="h3             :";
   s1[15][0]="T4             :";
   s1[16][0]="P4             :";
   s1[17][0]="h4             :";
   s1[0][2]="kg/s";
   s1[1][2]="kW  ";
   s1[2][2]="kW  ";
   s1[3][2]="kW  ";
   s1[4][2]="    ";
   s1[5][2]="    ";
   s1[6][2]="derece C";
   s1[7][2]="kPa     ";
   s1[8][2]="kJ/kg";
   s1[9][2]="derece C";
   s1[10][2]="kPa     ";
   s1[11][2]="kJ/kg";
   s1[12][2]="derece C";
   s1[13][2]="kPa     ";
   s1[14][2]="kJ/kg";
   s1[15][2]="derece C";
   s1[16][2]="kPa     ";
   s1[17][2]="kJ/kg";
   s1[0][1]=""+m;
   s1[1][1]=""+W;
   s1[2][1]=""+Q_evap;
   s1[3][1]=""+Q_cond;
   s1[4][1]=""+COP_evap;
   s1[5][1]=""+COP_cond;
   s1[6][1]=""+T[1];
   s1[7][1]=""+P[1];
   s1[8][1]=""+h[1];
   s1[9][1]=""+T[2];
   s1[10][1]=""+P[2];
   s1[11][1]=""+h[2];
   s1[12][1]=""+T[3];
   s1[13][1]=""+P[3];
   s1[14][1]=""+h[3];
   s1[15][1]=""+T[4];
   s1[16][1]=""+P[4];
   s1[17][1]=""+h[4];
   return s1;
}
 
}  