import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class f_equilibrium extends fi_xj
{ public double n0[];
  public double n[];
  int nn,n1,n2;
  String name[];
  double TR,TP;
  public chemicalReaction cr;
  public double A[][];
  public double b[];
  public Gas g[];
  double Pref=1.01325;//bar
  double P;
  chemicalReaction chr[];
  public f_equilibrium(double n0i[],String  namei[], chemicalReaction chri[],double T1i,double T2i,double Pi)
  { nn=n0i.length;
    int nn1=namei.length;
    if(nn!=nn1) System.out.println("array size error nn="+nn+"nn1="+nn1);
    P=Pi;
    n2=chri.length;
    n1=nn-n2;
   n0=new double[nn];
   n=new double[nn];
   g=new Gas[nn];
   name=new String[nn];
   TR=T1i;
   TP=T2i;
   chr=new chemicalReaction[n2];
   for(int i=0;i<n2;i++) {chr[i]=chri[i];}
   for(int i=0;i<nn;i++)
   {
	n0[i]=n0i[i];
    name[i]=namei[i];
    g[i]=new Gas(name[i]);
   }
   double N[][]=new double[nn][2];
   for(int i=0;i<nn;i++)
   {  N[i][0]=n0[i];
      N[i][1]=n[i];
   }
   cr=new chemicalReaction("base reaction",name,N);
   cr.conservation();
   A=cr.A;
   b=cr.b0;
  }
  public double[] func(double n[])
  { double b1[]=new double[nn];
    double nt=0;
    for(int i=0;i<nn;i++)
    {nt+=n[i];}
	for(int ii=0;ii<n1;ii++)
	{ for(int ij=0;ij<nn;ij++)
      { double carpim=A[ii][ij]*n[ij];	
		b1[ii]+=carpim;		
      }
      b1[ii]=b1[ii]-b[ii];						
    }
    double lnK;
    double total;
    double mu[][]=new double[nn][2];
    double nx[];
    for(int ii=n1;ii<nn;ii++)
	{ 
	  int ij=ii-n1;
	  int nn2=chr[ij].ngas;
	  nx=new double[nn2];
	  for(int i=0;i<nn;i++)
         {  
	         for(int j=0;j<nn2;j++)
            {  if(name[i].equals(chr[ij].name[j])) 
               {   mu[i][0]=chr[ij].N[j][0];mu[i][1]=chr[ij].N[j][1];
                   nx[j]=n[i];
                   
	               break;
	           }
           }
         }
	  total=0;
	  for(int i=0;i<chr[ij].ngas;i++)
      { if(nx[i]!=0)
        {total-=mu[i][0]*Math.log(nx[i]/nt*P/Pref);
         total+=mu[i][1]*Math.log(nx[i]/nt*P/Pref);
        }
	  }
      lnK=chr[ij].lnK(TR,TP);      
      b1[ii]=lnK-total;	
	}	 
   return b1;
  }
     public  void equilibrium_print(double ni[])
     { 
	 double x[]=new double[nn];
	 double x0[]=new double[nn];
	 double nnt=0;
	 double nn0t=0;
	 for(int i=0;i<nn;i++){nnt+=ni[i];nn0t+=n0[i];}
	 for(int i=0;i<nn;i++){x[i]=ni[i]/nnt;x0[i]=n0[i]/nn0t;}
     JTextArea jta;
     JPanel jpan=new JPanel();
     jpan.setLayout(new BorderLayout());
     String s="";
     s+="Treactant = "+TR+" degree K\n";
     s+="Tproduct  = "+TP+ " degree K\n";
     s+="P         = "+P+" bar\n";
     s+="==========================================\n"; 
     for(int i=0;i<n2;i++)
     {
     s+="Equilibrium Reaction   : "+chr[i].toStringE()+"\n";
     s+="Equilibrium constant K = "+chr[i].K(TR,TP)+"\n";
     s+="Equilibrium constant lnK = "+chr[i].lnK(TR,TP)+"\n"; 
     s+="==========================================\n";  
     }
     
     //for(int j=0;j<nn;j++)
     //{s+="n_"+name[j]+" = ["+n0[j]+" ,  "+ni[j]+" ]\n";}
     jta=new JTextArea(s);
     
     String heading[]={"name","n0 mole in","x0 mole ratio in","n mole out","x mole ratio out"};
     String s1[][]=new String[nn+1][5];     
     for(int i=0;i<nn;i++)
     {s1[i][0]=name[i];
      s1[i][1]=""+n0[i];
      s1[i][2]=""+x0[i];
      s1[i][3]=""+ni[i];
      s1[i][4]=""+x[i];
     }
     s1[nn][0]="total";
     s1[nn][1]=""+nn0t;
     s1[nn][2]=""+1;
     s1[nn][3]=""+nnt;
     s1[nn][4]=""+1;
	 JTable jt;
	 genelModel gm=new genelModel(s1,heading);
	 jt=new JTable(gm);
	  jpan.add(jta,BorderLayout.NORTH);
	  jpan.add(new JScrollPane(jt),BorderLayout.SOUTH);
	 String bb="Equilibrium Chemical Reaction";
	 JFrame cerceve=new JFrame(bb);
     cerceve.addWindowListener(new BasicWindowMonitor());
     cerceve.getContentPane().add(new JScrollPane(jpan));
     cerceve.pack();
     cerceve.setVisible(true);
	 }
}