import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class f_G1 extends f_xj;
{}
public class f_G extends fi_xj
{ double n0[];
  double n[];
  double N[][];
  double R=8.3145;
  int NG,na,n1; //number of species, number of atoms
  String name[]; //species name
  double TR,TP;
  chemicalReaction cr;
  double A[][];
  double b0[];
  Gas g[];
  double Pref=1.01325;//bar
  double P;
  public f_G(double n0i[],String  namei[],double T1i,double T2i,double Pi)
  { NG=n0i.length;
    int nn1=namei.length;
    if(NG!=nn1) System.out.println("array size error nn="+NG+"nn1="+nn1);
    P=Pi;
   n0=new double[NG];
   n=new double[NG];
   g=new Gas[NG];
   name=new String[NG];
   TR=T1i;
   TP=T2i;
   for(int i=0;i<NG;i++)
   {
	n0[i]=n0i[i];
    name[i]=namei[i];
    g[i]=new Gas(name[i]);
   }
   N=new double[NG][2];
   for(int i=0;i<NG;i++)
   {  N[i][0]=n0[i];
      N[i][1]=n[i];
   }
   cr=new chemicalReaction("base reaction",name,N);
   cr.conservation();
   A=cr.A;
   b0=cr.b0;
   na=A.length;//number of atoms
   n1=NG+na;
  }
  
  public double[] func(double x[])
  { double b[]=new double[n1];
    double b1[]=new double[NG];
    // Total mole of species
    double nt=0;
    for(int i=0;i<NG;i++)
    {nt+=x[i];}
    //atomic balance for new species vector
	for(int i=0;i<na;i++)
	{ for(int j=0;j<NG;j++)
      { double carpim=A[i][j]*x[j];	
		b1[i]+=carpim;		
      }
      b[i+NG]=b1[i]-b0[i];						
    }
    double mu[]=new double[NG];
    double total=0;
    for(int j=0;j<NG;j++)
	{ mu[j]=g[j].gt0(TP)+R*TP*Math.log(x[j]/nt*P/Pref);
	  for(int i=0;i<na;i++)
	  {total+=x[i+NG]*A[i][j];}
	  b[j]=mu[j]+total;
	} 
   return b; 
  }
}