//======================================================
// Thermodynamic Package in java
// Class kazan
// Dr. Turhan Coban
// Ege University, School of Engineering, 
// Department of Mechanical Engineering
// Bornova İZMİR - TURKEY
// boiler combustion efficiency

import java.io.*;
import java.net.*;
import java.util.*;

class kazan
{
Gas1 fuel[];
Gas1 air[];
Gas1 exhaust[];
Gas1 exhaustanallysis[];

double Tfuel;
double Tair;
double Texhaust;
double excessaircoefficient;
double NT;
double Q,Qmax,eff;

public kazan(String fuelismi[],double fuelmol[],double Ty,double Th,double Te,double hk,boolean ustisildeger)
{ // Yakıt ve air karbon oranı verildiğinde verim analizi yapar
  //Ty : yakıt sıcaklığı derece K
  //Th : air sıcaklığı derece K
  //Te : exhaust gazı sıcaklığı K
  //ustisildeger : true  : üst ısıl değer cinsinden yakıt verimi
  //ustisildeger : false : alt ısıl değer cinsinden yakıt verimi  
int n=fuelismi.length;
excessaircoefficient=hk;
fuel=new Gas1[n];
NT=0;
for(int i=0;i<n;i++)
  {NT+=fuelmol[i];}
for(int i=0;i<n;i++)
  {fuelmol[i]=fuelmol[i]/NT;fuel[i]=new Gas1(fuelismi[i],fuelmol[i]);}      
double Csay=0;
double Nsay=0;
double Osay=0;
double Hsay=0;
double Ssay=0;
double xO2=0;
double xN2=0;
for(int i=0;i<n;i++)
  {int m=fuel[i].atomList.length;
  for(int j=0;j<m;j++) 
    {if(fuel[i].atomList[j].symbol.equals("C")) Csay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("H")) Hsay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("O")) Osay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("S")) Ssay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("N")) Nsay+=fuel[i].N*fuel[i].atomList[j].N;
     else {System.out.println("listede olmıyan bir gaz mevcut : "+fuel[i].atomList[j].symbol);}
    }
  }
  xO2=Csay+Ssay+Hsay/4.0-Osay/2.0;//ideal yanma için gerekli O miktarı
  //ideal gaz çıkış atomları
  double hfuel=0;
  double hair=0;
  double hexhaust=0;
  for(int i=0;i<n;i++)
  {hfuel+=fuel[i].HT(298.0);}
  air=new Gas1[2];
  air[0]=new Gas1("o2",xO2);
  air[1]=new Gas1("n2",(xO2*79.0/21.0));
  hair=air[0].HT(298.0)+air[1].HT(298.0);
  exhaust=new Gas1[5];
  exhaust[0]=new Gas1("co2",Csay);
  if(ustisildeger) exhaust[1]=new Gas1("h2ol",(Hsay/2.0));
  else exhaust[1]=new Gas1("h2o",(Hsay/2.0));
  exhaust[2]=new Gas1("so2",Ssay);
  exhaust[3]=new Gas1("n2",(xO2*79.0/21.0+Nsay/2.0));
  //System.out.println("hh2o="+exhaust[1].HT(298.0));
  hexhaust=exhaust[0].HT(298.0)+exhaust[1].HT(298.0)+exhaust[2].HT(298.0)+exhaust[3].HT(298);
  //System.out.println("co2="+Csay+"H2o="+(Hsay/2.0)+"So2"+Ssay+"n2="+(xO2*79.0/21.0+Nsay/2.0)+"hexhaust="+hexhaust);
  Qmax=hexhaust-hair-hfuel;
  double xO2gercek=xO2*excessaircoefficient;
  hfuel=0;
  for(int i=0;i<n;i++)
  {hfuel+=fuel[i].HT(Ty);}
  air[0]=new Gas1("o2",xO2gercek);
  air[1]=new Gas1("n2",(xO2gercek*79.0/21.0));
  hair=air[0].HT(Th)+air[1].HT(Th);
  exhaust[1]=new Gas1("h2o",(Hsay/2.0));
  exhaust[3]=new Gas1("n2",(xO2gercek*79.0/21.0+Nsay/2.0));
  exhaust[4]=new Gas1("o2",(xO2gercek-xO2));
  hexhaust=exhaust[0].HT(Te)+exhaust[1].HT(Te)+exhaust[2].HT(Te)+exhaust[3].HT(Te)+exhaust[4].HT(Te);
  Q=hexhaust-hair-hfuel;
  eff=Q/Qmax;
  }

public kazan(String fuelismi[],double fuelmol[],String exhaustismi[],double exhaustmol[],double Ty,double Th,double Te,double hfk,boolean ustisildeger)
{ //exhaust analizi verildiğinde verimi hesaplar
  //Ty : yakıt sıcaklığı derece K
  //Th : air sıcaklığı derece K
  //Te : exhaust gazı sıcaklığı K
  //ustisildeger : true  : üst ısıl değer cinsinden yakıt verimi
  //ustisildeger : false : alt ısıl değer cinsinden yakıt verimi  
int n=fuelismi.length;
fuel=new Gas1[n];
excessaircoefficient=hfk;
NT=0;
for(int i=0;i<n;i++)
  {NT+=fuelmol[i];}
for(int i=0;i<n;i++)
  {fuelmol[i]=fuelmol[i]/NT;fuel[i]=new Gas1(fuelismi[i],fuelmol[i]);}      
double Csay=0;
double Nsay=0;
double Osay=0;
double Hsay=0;
double Ssay=0;
double xO2=0;
double xN2=0;
for(int i=0;i<n;i++)
  {int m=fuel[i].atomList.length;
  for(int j=0;j<m;j++) 
    {if(fuel[i].atomList[j].symbol.equals("C")) Csay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("H")) Hsay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("O")) Osay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("S")) Ssay+=fuel[i].N*fuel[i].atomList[j].N;
     else if(fuel[i].atomList[j].symbol.equals("N")) Nsay+=fuel[i].N*fuel[i].atomList[j].N;
     else {System.out.println("listede olmıyan bir gaz mevcut : "+fuel[i].atomList[j].symbol);}
    }
  }
Gas1 exhaustanallysis[]=new Gas1[exhaustmol.length];
double NTexhaust=0;
double n_N2=0;  
for(int i=0;i<n;i++)
  {NTexhaust+=exhaustmol[i];}

for(int i=0;i<n;i++)
  {exhaustanallysis[i]=new Gas1(exhaustismi[i],exhaustmol[i]);}  
double CX=0;
double NX=0;
double OX=0;
double HX=0;
double SX=0;
int nn=exhaustanallysis.length;

for(int i=0;i<n;i++)
  {int m=exhaustanallysis[i].atomList.length;
  for(int j=0;j<m;j++) 
    {if(exhaustanallysis[i].atomList[j].symbol.equals("C")) CX+=exhaustanallysis[i].N*exhaustanallysis[i].atomList[j].N;
     else if(exhaustanallysis[i].atomList[j].symbol.equals("H")) HX+=exhaustanallysis[i].N*exhaustanallysis[i].atomList[j].N;
     else if(exhaustanallysis[i].atomList[j].symbol.equals("O")) OX+=exhaustanallysis[i].N*exhaustanallysis[i].atomList[j].N;
     else if(exhaustanallysis[i].atomList[j].symbol.equals("S")) SX+=exhaustanallysis[i].N*exhaustanallysis[i].atomList[j].N;
     else if(exhaustanallysis[i].atomList[j].symbol.equals("N")) NX+=exhaustanallysis[i].N*exhaustanallysis[i].atomList[j].N;
     else {System.out.println("listede olmıyan bir gaz mevcut : "+exhaustanallysis[i].atomList[j].symbol);}
    }
  }
double AC=CX/Csay; //exhaust karışımı katsayısı
n_N2=AC*(1.0-NTexhaust);
      
  xO2=Csay+Ssay+Hsay/4.0-Osay/2.0;//ideal yanma için gerekli O miktarı
  //ideal gaz çıkış atomları
  double hfuel=0;
  double hair=0;
  double hexhaust=0;
  for(int i=0;i<n;i++)
  {hfuel+=fuel[i].HT(298.0);}
  air=new Gas1[2];
  air[0]=new Gas1("o2",xO2);
  air[1]=new Gas1("n2",(xO2*79.0/21.0));
  hair=air[0].HT(298.0)+air[1].HT(298.0);
  exhaust=new Gas1[5];
  exhaust[0]=new Gas1("co2",Csay);
  if(ustisildeger) exhaust[1]=new Gas1("h2ol",(Hsay/2.0));
  else exhaust[1]=new Gas1("h2o",(Hsay/2.0));
  exhaust[2]=new Gas1("so2",Ssay);
  exhaust[3]=new Gas1("n2",(xO2*79.0/21.0+Nsay/2.0));
  //System.out.println("hh2o="+exhaust[1].HT(298.0));
  hexhaust=exhaust[0].HT(298.0)+exhaust[1].HT(298.0)+exhaust[2].HT(298.0)+exhaust[3].HT(298);
  //System.out.println("co2="+Csay+"H2o="+(Hsay/2.0)+"So2"+Ssay+"n2="+(xO2*79.0/21.0+Nsay/2.0)+"hexhaust="+hexhaust);
  Qmax=hexhaust-hair-hfuel;
  double xO2gercek=xO2*excessaircoefficient;
  hfuel=0;
  for(int i=0;i<n;i++)
  {hfuel+=fuel[i].HT(Ty);}
  air[0]=new Gas1("o2",xO2gercek);
  air[1]=new Gas1("n2",(xO2gercek*79.0/21.0));
  hair=air[0].HT(Th)+air[1].HT(Th);
  exhaust[1]=new Gas1("h2o",(Hsay/2.0));
  exhaust[3]=new Gas1("n2",(xO2gercek*79.0/21.0+Nsay/2.0));
  exhaust[4]=new Gas1("o2",(xO2gercek-xO2));
  hexhaust=exhaust[0].HT(Te)+exhaust[1].HT(Te)+exhaust[2].HT(Te)+exhaust[3].HT(Te)+exhaust[4].HT(Te);
  Q=hexhaust-hair-hfuel;
  eff=Q/Qmax;
  }
  
public double Qi() {return Q;}
public double Qmax() {return Qmax;}

public double V_NM3_h(double QKW)
{
double Ni=QKW/(-Qi()); //kmol/s
return Ni*8314.5*273.15/1.01325e5*3600.0; //NM^3/saat
}

public double V_M3_h(double QKW,double T,double P)
{//T derece K
 //P basınç bar
 //verilen çevre sıcaklığı ve basınçtaki gaz debisi
double Ni=QKW/(-Qi()); //kmol/s
return Ni*8314.5*T/(P*1e5)*3600.0; //NM^3/saat
}

public double Ni(double QKW)
{
double Ni=QKW/(-Qi()); //kmol/s
return Ni*3600.0; //NM^3/saat
}

public String exhaust()
{
	String s="";
for(int i=0;i<exhaust.length;i++)
{s+=exhaust[i].gasName+" "+exhaust[i].natom+" ";}
return s;
}

public String fuel()
{
String s="";
for(int i=0;i<fuel.length;i++)
{s+=fuel[i].gasName+" "+fuel[i].natom+" ";}
return s;
}

public String air()
{
String s="";
for(int i=0;i<air.length;i++)
{s+=air[i].gasName+" "+air[i].natom+" ";}
return s;
}
public double efficiency() {return eff;}

}
