import java.io.*;
import java.util.*;
import Text;
import Matrix;

class PengRobinson
{
private double a,b,b0;
private double R=8314.5;
private double fw;
private double M,Tfb,Tb,Tc,Pc,Vc,Zc,w,Dipm;
File PRFile;
int ierror;
String PRName;
  public PengRobinson(String name) throws IOException
  {
  PRFile=new File("PengRobinson.dat");
  PRFile=new File(PRFile.getAbsolutePath());
  try
     {
     DataInputStream cfin=new DataInputStream(new FileInputStream(PRFile));
     try
       {
       while(cfin!=null)
         {
         PRName=Text.readString(cfin);
         if(PRName.equals(name)) {ierror=0;break;}
         }
       }
       catch(EOFException e_eof)
         {
         System.out.println("error required Peng Robinston equation "+name+" is not found");
         cfin.close();return;
         }
         M    =Text.readDouble(cfin);
         Tfb  =Text.readDouble(cfin);
         Tb   =Text.readDouble(cfin);
         Tc   =Text.readDouble(cfin);
         Pc   =Text.readDouble(cfin);
         Vc   =Text.readDouble(cfin);
         Zc   =Text.readDouble(cfin);
         w    =Text.readDouble(cfin);
         Dipm =Text.readDouble(cfin);
         a  = 0.07780*R*Tc/Pc;
         fw = 0.37464+1.54226*w-0.26992*w*w;
         b0 = 0.45724*R*R*Tc*Tc/Pc;
       }
     catch(FileNotFoundException fnfe) {System.out.println("File Not Found");
     }
  } //end of constructor

  public double P(double T,double V)
  {
   double Tr,Vr;
   Tr=T/Tc;
   Vr=V/Vc;
   double cof;
   cof=(1-Math.sqrt(Tr));
   b=b0*(1+fw*cof*cof);
   return (R*T/(V-b)-a/(V*V+2.*b*V-b*b));
  }

  public double V(double T,double P)
  {
  double Tr;
  Tr=T/Tc;
  double cof;
  cof=(1-Math.sqrt(Tr));
  b=b0*(1+fw*cof*cof);
  double e[];
  e=new double[4];
  double AS,BS;
  AS=a*P/(R*R*T*T);
  BS=b*P/(R*T);
  e[0]=1;
  e[1]=-(1+BS-2*BS);
  e[2]=(AS-BS*BS-2*BS-2*BS*BS);
  e[3]=-AS*BS+BS*BS+BS*BS*BS;
  complex rt[];
  rt=new complex[3];
  rt=Matrix.polyRootsC(e);
  Matrix.toString(rt);
  return 1;
  }
}
