// ======================================================
// Thermodynamics package in java
// water_lithiumbromide_mix class to calculate properties of water 
// and lithium bromide mixture
// Dr. Turhan Coban
// File Name   : water_lithiumbromide_mix.java
// requires    : steam.java
// =====================================================
// Reference : 2008 ASHRAE Handbook -HVAC Systems and equipment
import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class water_lithiumbromide_mix
{
	steam st;
double A[]={-2024.33,163.309,-4.88161,6.302948e-2,-2.913705e-4};
double B[]={18.2829,-1.1691757,3.248041e-2,-4.034184e-4,1.8520569e-6};
double C[]={-3.7008214e-2,2.8877666e-3,-8.1313015e-5,9.9116628e-7,-4.4441207e-9};
double Ai[]={-2.00755,0.16976,-3.133362e-3,1.97668e-5};
double Bi[]={124.937,-7.71649,0.152286,-7.95090e-4};
double Ci=7.05;
double Di=-1596.49;
double Ei=-104095.5;
public water_lithiumbromide_mix()
{st=new steam();}

double P(double tP)
{
//mixture saturation pressure kPa
//çözelti doyma basıncı	
//TP mixture saturation temperature K
//TP çözelti doyma sıcaklığı
  double sp[]=st.property("tx",tP,0.0);//doymuş buhar at yaş termometre temperatur
  //sp[0] p
  //sp[1] t
  //sp[2] v
  //sp[3] h
  //sp[4] u
  //sp[5] s
  return sp[0]*100.0;
}

double t_P(double P)
{
//mixture saturation pressure kPa
//çözelti doyma basıncı	
//TP mixture saturation temperature K
//TP çözelti doyma sıcaklığı;
double p1=P/100.0;
  double sp[]=st.property("px",p1,1.0);//doymuş buhar at yaş termometre temperatur
  //sp[0] p
  //sp[1] t
  //sp[2] v
  //sp[3] h
  //sp[4] u
  //sp[5] s
  return sp[1];
}

public double[] steam_property(String s1,double v1,double v2)
{  // properties of pure steam and water
   double sp[]=st.property(s1,v1,v2);
   // sp[0] p
   // sp[1] t
   // sp[2] v
   // sp[3] h
   // sp[4] u
   // sp[5] s
   return sp;
}

double f(double tw,double t, double X)
{return t_w(t,X)-tw;}

public  double X(double tw,double t)
{
// percentage of mixture when water(refrigerant) and mixture temperatures are known
// soğutkan(su) ve lityum bromid sıcaklıkları bilindiğinde karışım yüzdelerinin hesaplanması 
double test;
double p=0;
double es,ea;
double f1,f2,f3,fp;
int maxit=500,iter=0;
double tol=1.0e-15;
es=0.0000001;
double x1=0.2;f1=f(tw,t,x1);
double x2=0.7;f2=f(tw,t,x2);
double x3=(x1+x2)/2.0;f3=f(tw,t,x3);
if(f1==0) return x1;
else if(f2==0) return x2;
else if(f3==0) return x3; 
if(f1*f2>0) System.out.println("verilen bölgede kök yok");
p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1));
fp=f(tw,t,p);
ea=Math.abs(x3-p);
while((ea>es)&&(iter<maxit))
{	if(Math.abs(f3)<tol) return x3; 
    if((p<x1) && (p>x2)) 
    {p=(x1+x2)/2.0;    
     if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;}
     else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;}
    }
    else
    {        
    if(p>x3) {x1=x3;f1=f3;x3=p;f3=fp;}
    else if(p<x3) {x2=x3;f2=f3;x3=p;f3=fp;}
    p=-(f2*f3*x1*(f2-f3)+f3*f1*x2*(f3-f1)+f1*f2*x3*(f1-f2))/((f1-f2)*(f2-f3)*(f3-f1));
    fp=f(tw,t,p);;
    ea=Math.abs(x3-p);
    }
    iter++;
} 
if(iter>=maxit) JOptionPane.showMessageDialog(null,"Uyarı maximum iterasyon sayısı aşıldı \n"+
   " çözüm geçerli olmıyabilir","MAKSİMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE); 
return p;
}

double t_w(double t, double X)
{
// equilibrium temperature of liquid mixture degree C
// sıvı karışım sıcaklığı derece C
// x kg Lithiumbromide/kg mix mixture mass percentage
// x sıvı karışım yüzdesi
// equation valid between %40-%70
// denklem %40-%70 karışım oranları için geçerlidir
// equilibrium t temperature -15 C<= t <= 100 C
// t su sıcaklığı derece C 
// water(refrigerant) temperature is valid between 5<=tl<=175
// su(soğutkan) sıcaklığı 5 ile 175 derece arasında geçerlidir
double x=X*100.0;
double tt;
double xx=Ai[0]+Ai[1]*x+Ai[2]*x*x+Ai[3]*x*x*x;
double yy=Bi[0]+Bi[1]*x+Bi[2]*x*x+Bi[3]*x*x*x;
tt=yy+t*xx;
return tt;
}

double t_l(double tw, double X)
{
// water temperature of liquid mixture degree C
// su sıcaklığı derece C
// x kg Lithiumbromide/kg mix mixture mass percentage
// x sıvı karışım yüzdesi
// equation valid between %40-%70
// denklem %40-%70 karışım oranlrı için geçerlidir
// t temperature of liquid mixture 5 C<= t <= 175 C
// t sıvı karışım sıcaklığı 
double x=X*100.0;
double tt;
double xx=Ai[0]+Ai[1]*x+Ai[2]*x*x+Ai[3]*x*x*x;
double yy=Bi[0]+Bi[1]*x+Bi[2]*x*x+Bi[3]*x*x*x;
tt=(tw-yy)/xx;
return tt;
}

double h_ltw(double tw,double X)
{//saturated water temperature and mix mass percentage is given
double x=X*100.0;
double tt;
double xx=A[0]+A[1]*x+A[2]*x*x+A[3]*x*x*x+A[4]*x*x*x*x;
double yy=B[0]+B[1]*x+B[2]*x*x+B[3]*x*x*x+B[4]*x*x*x*x;
double zz=C[0]+C[1]*x+C[2]*x*x+C[3]*x*x*x+C[4]*x*x*x*x;
tt=xx+tw*yy+tw*tw*zz;
return tt;
}
double h_lt(double t,double X)
{ //mixture temperature and mix ratio is given
double tw=t_w(t,X);
double x=X*100.0;
double tt;
double xx=A[0]+A[1]*x+A[2]*x*x+A[3]*x*x*x+A[4]*x*x*x*x;
double yy=B[0]+B[1]*x+B[2]*x*x+B[3]*x*x*x+B[4]*x*x*x*x;
double zz=C[0]+C[1]*x+C[2]*x*x+C[3]*x*x*x+C[4]*x*x*x*x;
tt=xx+tw*yy+tw*tw*zz;
return tt;
}

double ro_lt(double t,double X)
{   double x=X*100.0;
    double T=t+273.15;
	double A1=-0.014396*x+0.19405;
    double A2=30.723*x+51.718;
    double ro;
    if(x>=65 && x<=70) ro=A1*T+A2;
    else
    {  double sp[]=st.property("tx",t,0.0);//doymuş buhar at yaş termometre temperatur
       // sp[0] p
       // sp[1] t
       // sp[2] v
       // sp[3] h
       // sp[4] u
       // sp[5] s
       double ro_0=1.0/sp[2];
       double B1=-0.014396*65+0.19405;
       double B2=30.723*65+51.718;
       double ro_65=B1*T+B2;
       ro=ro_0+(ro_65-ro_0)/65*x;
    }
 return ro;   
}

}