import javax.swing.*;

public class VDW_CO2
{
//Cp katsayıları
public double c[];
public double Ru;
public double a,b;
public double Tc,Pc;
public double Tref,vref,Pref;
public double href,sref,uref;
public  VDW_CO2()
{ double ci[]={1.401,8.735e-3,-6.607e-6,2.002e-9,0};
  c=ci;
  Ru=4.187e3;//J/kmolK
  Tc=304.1;//K
  Pc=73.8e5;//Pa
  b=Ru*Tc/(8.0*Pc);
  a=27.0/64.*Ru*Ru*Tc*Tc/Pc;
  Tref=273.0;//K
  vref=1.0;//m^3/kmol 
  Pref=P(Tref,vref);
  href=0;//J/kmol
  sref=1e3;//J/kmolK
  uref=href-Pref*vref; 
}

public double P(double T,double v)
{//T K v m^3/kmol
 double P1=Ru*T/(v-b)-a/(v*v);
 return P1;
}

public double Cv(double T)
{   //J/kmol K
	//T K v m^3/kmol
 double CV1=c[0]+c[1]*T+c[2]*T*T+c[3]*T*T*T;
 return CV1;
}
public double integCv(double T)
{//T K v m^3/kmol
 double CV1=c[0]*(T-Tref)+c[1]/2.0*(T*T-Tref*Tref)+
 c[2]/3.0*(T*T*T-Tref*Tref*Tref)+c[3]/4.0*(T*T*T*T-Tref*Tref*Tref*Tref);
 return CV1;
}
public double integCvboluT(double T)
{//T K v m^3/kmol
 double CV1=c[0]*Math.log(T/Tref)+c[1]*(T-Tref)+
 c[2]/2.0*(T*T-Tref*Tref)+c[3]/3.0*(T*T*T-Tref*Tref*Tref);
 return CV1;
}

public double u(double T,double v)
{double u1=uref+integCv(T)-a*(1.0/v-1.0/vref);
 return u1; //J/kmol
}

public double h(double T,double v)
{double h1=u(T,v)+P(T,v)*v;
 return h1; //J/kmol
}

public double s0(double T)
{return sref+integCvboluT(T);//J/kmolK
}

public double s(double T,double v)
{double s1=s0(T)+Ru*Math.log((v-b)/(vref-b));
 return s1; //J/kmolK
}
public  double fP(double v,double T,double Pi)
{double pp=P(T,v)-Pi;
 return pp;
}

public  double brent(double x1,double x2,double T,double Pi)
{
    // roots of the nonlinear equation: Brent method
    // referance : Numerical Recipes in C, second edition, William H. Press, 
    // Saul A. Teukolsky, William T. Vetterling, Brian P. Flannery
    // cambridge university press	
	double ITMAX=200;
               double EPS=2.22044604925031e-8;
	int iter;
	double a=x1,b=x2,c,d,e,min1,min2;
	double tol=1.0e-8;
	c=0;
	d=0;
	e=0;
	double fa=fP(a,T,Pi),fb=fP(b,T,Pi),fc,p,q,r,s,tol1,xm;
	if (fb*fa > 0.0) JOptionPane.showMessageDialog(null,"Root limit selection is uncorrect \n result may be wrong"
	,"ROOT LIMITS WARNING",JOptionPane.WARNING_MESSAGE);
	fc=fb;
	for (iter=1;iter<=ITMAX;iter++) 
	{
		if (fb*fc > 0.0) {
			c=a;
			fc=fa;
			e=d=b-a;
		}
		if (Math.abs(fc) < Math.abs(fb)) {
			a=b;
			b=c;
			c=a;
			fa=fb;
			fb=fc;
			fc=fa;
		}
		tol1=2.0*EPS*Math.abs(b)+0.5*tol;
		xm=0.5*(c-b);
		if (Math.abs(xm) <= tol1 || fb == 0.0) return b;
		if (Math.abs(e) >= tol1 && Math.abs(fa) > Math.abs(fb)) {
			s=fb/fa;
			if (a == c) {
				p=2.0*xm*s;
				q=1.0-s;
			} else {
				q=fa/fc;
				r=fb/fc;
				p=s*(2.0*xm*q*(q-r)-(b-a)*(r-1.0));
				q=(q-1.0)*(r-1.0)*(s-1.0);
			}
			if (p > 0.0)  q = -q;
			p=Math.abs(p);
			min1=3.0*xm*q-Math.abs(tol1*q);
			min2=Math.abs(e*q);
			if (2.0*p < (min1 < min2 ? min1 : min2)) {
				e=d;
				d=p/q;
			} else {
				d=xm;
				e=d;
			}
		} else {
			d=xm;
			e=d;
		}
		a=b;
		fa=fb;
		if (Math.abs(d) > tol1)
			b += d;
		else
			b += (xm > 0.0 ? Math.abs(tol1) : -Math.abs(tol1));
		fb=fP(b,T,Pi);
		System.out.println("b="+b);
	}
return b;
}

public double v(double Pi,double T)
{ double v1=Ru*T/Pi;
  return brent(v1-5,v1+5,T,Pi); 
}

public String toString(double T,double v)
{String s="CO2 özellikleri\n";
 s+="P="+P(T,v)+"Pa\n";
 s+="Cv="+Cv(T)+"J/kmolK\n";
 return s;
}

}