//======================================================
// java Termodinamik paketi
// Class sogut soğutma akışkanların özellikleri
// Dr. Turhan Coban
// Ege Üniversitesi Mühendislik fakultesi
// Makina Bölümü
// 
//===========================================================
// Dosya adı   : MBWR.java
// Bu dosya soğutucu akışkanların termodinamik özelliklerini 
// Değiştirilmiş Benedict-Webb-Rubin hal denklemiyle hesaplar
// ==========================================================

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class MBWR_CS
{
public MBWR_CS_Data rd;

public MBWR_CS(String rName)
{readref(rName);}

double P(double t,double v)
{
// Değiştirilmiş Benedict Webb Rubin Denklemi
// v m^3/kg özgül hacim
// V  m^3/kmol veya litre/mol özgül hacim
// t  Sıcaklık derece C
// T  Sıcaklık derece K
// P Basınç kPa
double V=v*rd.M;
double Vc=rd.Vc*rd.M;
double T=t+273.15;
double T2=T*T;
double T3=T2*T;
double T4=T2*T2;
double pi=0;
double a[];
a=new double[15];
a[0]=rd.R*T;
a[1]=rd.B[0]*T + rd.B[1]*Math.sqrt(T) + rd.B[2] + rd.B[3]/T + rd.B[4]/T2;
a[2]=rd.B[5]*T + rd.B[6] + rd.B[7]/T + rd.B[8]/T2;
a[3]=rd.B[9]*T + rd.B[10] + rd.B[11]/T;
a[4]=rd.B[12];
a[5]=rd.B[13]/T + rd.B[14]/T2;
a[6]=rd.B[15]/T;
a[7]=rd.B[16]/T + rd.B[17]/T2;
a[8]=rd.B[18]/T2;
a[9]=rd.B[19]/T2 + rd.B[20]/T3;
a[10]=rd.B[21]/T2 + rd.B[22]/T4;
a[11]=rd.B[23]/T2 + rd.B[24]/T3;
a[12]=rd.B[25]/T2 + rd.B[26]/T4;
a[13]=rd.B[27]/T2 + rd.B[28]/T3;
a[14]=rd.B[29]/T2 + rd.B[30]/T3 + rd.B[31]/T4;
double vn[];
vn=new double[15];
double v1=V;
for(int i=1;i<14;i++){vn[i]=v1;v1*=V;}
double C=Math.exp(-rd.Vc*rd.Vc/v/v);
pi=a[0]/V+a[1]/vn[2]+a[2]/vn[3]+a[3]/vn[4]+a[4]/vn[5]+a[5]/vn[6]+a[6]/vn[7]+a[7]/vn[8]+a[8]/vn[9];
pi+=C*(a[9]/vn[3]+a[10]/vn[5]+a[11]/vn[7]+a[12]/vn[9]+a[13]/vn[11]+a[14]/vn[13]);
return pi*rd.mf;
}
  public double dPdT(double t, double v)
  {
  double pi=0;
  double V=v*rd.M;
  double T=t+273.15;
  double T2=T*T;double T3=T2*T;double T4=T2*T2;double T5=T4*T;
  double vc2=rd.Vc*rd.Vc;
  double da[];
  da=new double[15];
  da[0]=rd.R;
  da[1]=rd.B[0] +0.5* rd.B[1]/Math.sqrt(T) - rd.B[3]/T2 -2*rd.B[4]/T3;
  da[2]=rd.B[5] - rd.B[7]/T2 -2* rd.B[8]/T3;
  da[3]=rd.B[9] - rd.B[11]/T2;
  da[4]=0;
  da[5]=-rd.B[13]/T2 - 2.0*rd.B[14]/T3;
  da[6]=-rd.B[15]/T2;
  da[7]=-rd.B[16]/T2 -2.0*rd.B[17]/T3;
  da[8]=-2.0*rd.B[18]/T3;
  da[9]=-2.0*rd.B[19]/T3 - 3.0* rd.B[20]/T4;
  da[10]=-2.0*rd.B[21]/T3 - 4.0*rd.B[22]/T5;
  da[11]=-2.0*rd.B[23]/T3 -3.0* rd.B[24]/T4;
  da[12]=-2.0*rd.B[25]/T3 -4.0* rd.B[26]/T5;
  da[13]=-2.0*rd.B[27]/T3 -3.0* rd.B[28]/T4;
  da[14]=-2.0*rd.B[29]/T3 -3.0* rd.B[30]/T4 -4.0* rd.B[31]/T5;
  double vn[];
  vn=new double[15];
  double v1=V;
  for(int i=1;i<14;i++){vn[i]=v1;v1*=V;}
  double C=Math.exp(-rd.Vc*rd.Vc/v/v);
  pi=da[0]/V+da[1]/vn[2]+da[2]/vn[3]+da[3]/vn[4]+da[4]/vn[5]+da[5]/vn[6]+da[6]/vn[7]+
  da[7]/vn[8]+da[8]/vn[9];
  pi+=C*(da[9]/vn[3]+da[10]/vn[5]+da[11]/vn[7]+da[12]/vn[9]+
  da[13]/vn[11]+da[14]/vn[13]);
  return pi*rd.mf;
  }

public  double[] koksinirisapta(double t, double Pi,double x1,double x2,int n)
{
	// koklerin yer aldığı alt bölgeleri saptar
	// n : verilen bölgeyi böldüğümüz alt bölge sayısı
	// x1,x2 : sınır değerleri
	int nb;
	int i;
	double x,fp,fc,dx;
	double xb[]=new double[2];
	nb=0;
	dx=(x2-x1)/n;
	x=x1;
	fp=P(t,x1)-Pi;
	for (i=1;i<=n;i++) 
	{
		x+=dx;
		fc=P(t,x)-Pi;
		// eğer kök olan bölge bulunduysa.....
		if (fc*fp < 0.0 || fp==0) {
			xb[0]=x-dx;
			xb[1]=x;
			nb++;
			break;
		}
		fp=fc;
	}	
if( nb == 0)
	JOptionPane.showMessageDialog(null,"arama tamamlandı kök olan bölge bulunamadı "
    ,"ARAMA SONUÇLARI UYARISI",JOptionPane.WARNING_MESSAGE);	
return xb;
}

public double[] genislet(double t,double Pi,double x1,double x2)
{
	//kök olan bir bölge bulana kadar verilen sınırı genişletir
	int NTRY=50;
	double a[]=new double[2];
	double FACTOR=1.05;
	int j;
	double f1,f2;
	if (x1 == x2) System.out.println("Bad initial range in ZBRAC");
	f1=P(t,x1)-Pi;
	f2=P(t,x2)-Pi;
	for (j=1;j<=NTRY;j++) {
		if (f1*f2 < 0.0) {a[0]=x1;a[1]=x2;return a;}
		if (Math.abs(f1) < Math.abs(f2))
			f1=P(t,(x1 += FACTOR*(x1-x2)))-Pi;
		else
			f2=P(t,(x2 += FACTOR*(x2-x1)))-Pi;
	}
	{a[0]=x1;a[1]=x2;return a;}
}


public double v_tp(double t,double Pi)
{
double x1=1.0/2000.0;
double x2=2000.0;
	double xacc=1.0e-10; //iterasyonlar arasındaki fark, bu rakamdan 
// küçük olduğunda çözüme ulaştığımızı kabul edeceğiz
double h=0.0001; //türev step büyüklüğü
int MAXIT=100;  //Maksimum iterasyon sayısı
int j;
double dfr,dx,dxold,fr,fh,fl;
double temp,b,a,r;
fl=P(t,x1)-Pi;
fh=P(t,x2)-Pi;
//Bölgemizde kök değeri varmı kontrol et
if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0))
System.out.println("verilen sınırlar içinde kök değeri yok");
if (fl == 0.0) return x1;
if (fh == 0.0) return x2;
if (fl < 0.0)   { a=x1;b=x2;} 
else            { b=x1;a=x2;}
r=0.5*(x1+x2); //orta nokta değeri 
dxold=Math.abs(x2-x1); 
dx=dxold; 
fr=P(t,r)-Pi;   //fonksiyonun r noktasındaki değeri
dfr=dPdT(t,r); //fonksiyonun türevinin r noktasındaki değeri
for (j=1;j<=MAXIT;j++) 
{ 
if ((((r-b)*dfr-fr)*((r-a)*dfr-fr) > 0.0) || (Math.abs(2.0*fr) > Math.abs(dxold*dfr))) 
    { //ikiye bölme 
	  dxold=dx;
      dx=0.5*(b-a);
      r=a+dx;
      if (a == r) return r; //çözüm!!!!
    } 
else{ //Newton-Raphson
	  dxold=dx;
      dx=fr/dfr;
      temp=r;
      r -= dx;
      if (temp == r) return r;  //çözüm!!!!!
    }
if (Math.abs(dx) < xacc) return r; //çözüm!!!!!
fr=P(t,r)-Pi;
dfr=dPdT(t,r);
if (fr < 0.0) 
a=r;
else
b=r;
}
System.out.println("Maksimum iterasyon sayısı aşıldı");
return 0.0; //program buraya gelmemeli 
}

  
public double Ps(double t)
{return rd.Ps(t);}

public double dPsdT(double t)
{return rd.dPsdT(t);}


public double vg(double t)
{return rd.vg(t);}


public double vl(double t)
{return rd.vl(t);}

public double rol(double t)
{return rd.rol(t);}


public double rog(double t)
{return rd.rog(t);}

public double dPs(double t)
{ //numerical derivative
	double h=0.1; 
  return (-Ps(t+2.0*h)+8.0*Ps(t+h)-8.0*Ps(t-h)+Ps(t-2.0*h))/(12.0*h);
}

double hfg(double t)
{double T=t+273.15;
double vl=vl(t);
double vg=vg(t);
return T*(vg-vl)*dPsdT(t);
}

double hfg1(double t)
{   //calculated with numerical derivative
	double T=t+273.15;
    double vl=vl(t);
    double vg=vg(t);
    return T*(vg-vl)*dPs(t);
}

double sfg(double t)
{double T=t+273.15;
return (vg(t)-vl(t))*dPsdT(t);
}

public String readref(String rName)
{
String s="";
ArrayList<MBWR_CS_Data> gi=new ArrayList<MBWR_CS_Data>();
    rd=new MBWR_CS_Data("R134a","1,1,1,2-tetrafluoroethane",100.0,100.0,
    102.03,0.08314471,-26.06,101.108,4060.3,515.3,0.00194,
    -6.5455235227E-02,
     5.8893751817E+00,
    -1.3761788409E+02,
    2.2693168845E+04,
    -2.9262613296E+06,
    -1.1923776190E-04,
    -2.7214194543E+00,
    1.6295253680E+03,
    7.2942203182E+05,
    -1.1724519115E-04,
    8.6864510013E-01,
    -3.0660168246E+02,
    -2.5664047742E-02,
    -2.4381835971E+00,
    -3.1603163961E+02,
    3.4321651521E-01,
    -1.0154368796E-02,
    1.1734233787E+00,
    -2.7301766113E-02,
    -6.6338502898E+05,
    -6.4754799101E+07,
    -3.7295219382E+04,
    1.2614735899E+09,
    -6.4742200070E+02,
    1.2362450399E+05,
    -1.5699196293E+00,
    -5.1848932204E+05,
    -8.1396321392E-02,
    3.0325168842E+01,
    1.3399042297E-04,
    -1.5856192849E-01,
    9.0679583743E+00,
    0.0,200.0,1.0,198.36,0.7262);

    try{
    gi.add(rd);
       } catch(ArrayIndexOutOfBoundsException e) {System.out.println("Array Index Out Of Bounds Exception");}
    rd=new MBWR_CS_Data("R123","2,2 dichloro-1,1,1-trifluoroethane",0.9980793432775332,1.0,
    152.93,8.314471,27.85,183.68,3668.0,550.0,0.0018181818,
   -1.9737179010E+00,
    1.4351227567E+02,
   -2.9709876039E+03,
    4.5057867093E+05,
   -4.0472167920E+07,
    1.0191935793E-01,
   -1.9633752269E+02,
    9.2512206444E+04,
   -4.0076301858E+07,
   -2.0404730428E-02,
    3.6819648916E+01,
   -1.1445252703E+04,
   -5.5096827243E-01,
   -7.7622951034E+00,
    1.2172130502E+05,
   -8.2869171955E+00,
    7.2619253573E-01,
   -1.4363099944E+03,
    6.4252739335E+01,
    3.3524849016E+07,
   -2.0683821999E+09,
    1.8256503429E+06,
    4.2391447763E+10,
    2.6036695332E+04,
    1.3075003968E+07,
    1.1568895787E+03,
   -7.4177654079E+06,
   -2.4495004199E+00,
    4.9612079844E+03,
    4.0451631009E-03,
    1.2149154232E+02,
    2.2624786818E+04,
    0.0,200.0,1.0,179.7,1.6580);
    try{
    gi.add(rd);
       } catch(ArrayIndexOutOfBoundsException e) {System.out.println("Array Index Out Of Bounds Exception");}
    
    int i;
    Iterator n1=gi.iterator();
    while(n1.hasNext())
    {   rd=(MBWR_CS_Data)n1.next();
    s+=rd.refrigerantName+" ";
    if (rd.refrigerantName.equals(rName)) {break;}
    }   
    return s;
    }
}