//======================================================
// Numerical Analysis package in java
// example to show differential equation solution
// and differentiation (derivative) functions
// Dr. Turhan Coban
// =====================================================
import java.io.*;

class fm1 extends f_xi
{
double T=1500.0;//degree K;
double nfi;
public  fm1(double nfi1,double Ti)
{nfi=nfi1;T=Ti;}

//multivariable function
 public double func(double x[],int x_ref)
 {
   //x[0] is x, x[1] is nfuel  x[2] is nCO2
   double y=1.0;
   double nCO=3.0*(nfi-x[1])-x[2];
   double nO2=(3.5*x[1])+1.5*nfi-0.5*x[2];
   double nH2O=4.0*(nfi-x[1]);
   double nCO2f=3.0*nfi;
   if(x_ref==0) {y=-1.0e12*Math.exp(-30000.0/(1.987*T))*Math.pow(x[1],0.1)*Math.pow(nO2,1.65);return y;}
   else if(x_ref==1) {y=pow(10,14.6)*exp(-40000.0/(1.987*T))*nCO*pow(nO2,0.25)*pow(nH2O,0.5)-
   5.0e8*exp(-40000.0/(1.987*T))*x[2];return y;}
   else return 0;
 }
}


class diferansiyel_kinetics
{

public static double[][] RK6(f_xi fp,double x0,double xn,double f0[],int N)
{
//6th order Runge Kutta Method
//fp  : given set of derivative functions dfj/dxi(fj,x)
// xo : initial value of the independent variable
// xn : final value of the independent variable
// f0 : initial value of the dependent variable
// N  : number of dependent variable to be calculated
// fi : dependent variable
double h=(xn-x0)/N;
int M=f0.length;
double fi[][];
fi=new double[M][N+1];
double xi[]=new double[M+1];
double k[]=new double[6];
int i,j;
double x;
for(j=0;j<M;j++)
  {
  fi[j][0]=f0[j];
  xi[j+1]=f0[j];
  }
for(x=x0,i=0;i<N;x+=h,i++)
  {
  for(j=1;j<=M;j++)
    {
    xi[0]=x;
    xi[j]=fi[j-1][i];
    k[0]=h*fp.func(xi,j-1);
    xi[0]=x+h/2.0;
    xi[j]=fi[j-1][i]+k[0]/2;
    k[1]=h*fp.func(xi,j-1);
    xi[0]=x+h/2.0;
    xi[j]=fi[j-1][i]+k[0]/4.0+k[1]/4.0;
    k[2]=h*fp.func(xi,j-1);
    xi[0]=x+h;
    xi[j]=fi[j-1][i]-k[1]+2.0*k[2];
    k[3]=h*fp.func(xi,j-1);
    xi[0]=x+2.0/3.0*h;
    xi[j]=fi[j-1][i]+7.0/27.0*k[0]+10.0/27.0*k[1]+1.0/27.0*k[3];
    k[4]=h*fp.func(xi,j-1);
    xi[0]=x+1.0/5.0*h;
    xi[j]=fi[j-1][i]+28.0/625.0*k[0]-1.0/5.0*k[1]+546.0/625.0*k[2]+54.0/625.0*k[3]-378/625.0*k[4];
    k[5]=h*fp.func(xi,j-1);
    fi[j-1][i+1]=fi[j-1][i]+k[0]/24.0+5.0*k[3]/48.0+27.0*k[4]/56.0+125.0*k[5]/336.0;
    xi[j]=fi[j-1][i];
    }
  }
double a[][]=new double[M+1][N+1];
for(x=x0,i=0;i<=N;x+=h,i++)
{
  a[0][i]=x;
  for(j=1;j<=M;j++)
  {
  a[j][i]=fi[j-1][i];
  }
}

return a;
}


  public static void main(String args[]) throws IOException
  {
  double x[];
  x=new double[1];
  double p=1.0; //atm;
  double R=82.05; // (cm^3 atm/K/gmol)
  double T=1500.0;
  double n=p/(R*T);

  double y[]=new double[2];
  y[0]=1./24.8*n;//initial amount of fuel
  y[1]=0.0;      //initial amount of CO2
  double nCO=0;
  double nCO2f=3.0*y[0];
  fm1 b3=new fm1(y[0],T);
  int N=1000;
  double a[][]=RK6(b3,0.0,3.0e-3,y,N);
  double z[][]=new double[4][a[0].length];
  for(int i=0;i<a[0].length;i++)
  {z[0][i]=a[0][i]*1e3;//milisecond
   z[1][i]=a[1][i]/y[0];
   z[2][i]=a[2][i]/nCO2f;
   nCO=3.0*(y[0]-a[1][i])-a[2][i];
   z[3][i]=nCO/nCO2f;
  }
  String s="C3H8 reaction";
  String s1[]={"time miliseconds","nfuel/nfuel(initial)","nCO2/nCO2(final)","nCO/nCO2(final)"};
  Text.printT(z,s1,s);
  Plot pp=new Plot(z[0],z[1]);
  pp.addData(z[0],z[2]);
  pp.addData(z[0],z[3]);
  pp.plot();
 }
}
