//======================================================
// Termodinamik-nümerik analiz paketleri
// kazan verim analizi
// Dr. Turhan Coban
// EGE Üniversitesi Mühendislik Fakultesi, Makina Bölümü
// turhan.coban@turhancoban.com
// =====================================================
import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;

public class boilerTableE extends JApplet implements ActionListener,ItemListener
{
  String g[];
  double gn[];
     
  String ex[];
  double exn[];
     
  double Tfuel;
  double Tair;
  double Texhaust;
  double excess_air;
  double QKW; //istenilen ısı exitı
  double nair,nair_tamyanma;
  boolean Higher_heating_value;	
  JPanel panel[];
  JTextArea output;
  JTextField hhv_lhv;
  boilerE k1;
  JButton b1;
  JCheckBox hhv;//higher heating value chech
  JTextArea hhv_out;//higher heating value check result
  girdiPE gp1;
  String state[]={"   Higher Heating Value            ","   LOwer Heating Value             "}; 
   
    public void yakit_sec()
    {
    Object[] o=new Object[]{"Natural gas","Diesel","LPG","biogas"};
    String name=(String)JOptionPane.showInputDialog(null,"Select the fuel mixture : ",
    "Fuel Selection",JOptionPane.QUESTION_MESSAGE,null,o,"Natural gas");
    if(name.equals("Diesel"))
    {
	String g1[]={"c9h20",
"c10h22",
"c11h24",
"c12h26",
"c13h28",
"c14h30",
"c15h32",
"c16h34",
"c17h36",
"c18h38",
"c19h40",
"c20h42",
"c11h16",
"c12h18",
"c13h20",
"c14h22",
"c15h24",
"c16h26",
"c17h28",
"c18h30",
"c10h8",
"c11h10",
"c12h12",
"c13h14",
"c14h16"};
double gn1[]={0.01715,
0.03079,
0.05964,
0.09655,
0.1960,
0.1780,
0.08319,
0.03902,
0.01837,
0.00865,
0.00409,
0.001978,
0.00328,
0.004556,
0.005626,
0.005495,
0.005206,
0.005367,
0.002328,
0.001463,
0.04248,
0.08293,
0.05228,
0.03410,
0.02103};
String ex1[]={"co2","co","so2","n2","o2"};
double exn1[]={0.14100117547298072,0.378e-6,0,0.8424112610189113,0.016587563508107972};
g=g1;
gn=gn1;
ex=ex1;
exn=exn1;
	}
else if(name.equals("Natural Gas"))
{
String g1[]={"ch4","c2h6","c3h8","co2","h2","co","h2o"};
double gn1[]={0.908,0.059,0.006,0.018,0.0,0.0,0.0}; 
String ex1[]={"co2","co","so2","n2","o2"};
double exn1[]={0.11102730877510023 ,0.378e-6,0,0.8718063220150396,0.017166369209860154};
g=g1;
gn=gn1;
ex=ex1;
exn=exn1;
}
else if(name.equals("LPG"))
{
// c3h8 0.3
// c4h10 0.7
String g1[]={"c3h8","c4h10"};
double gn1[]={0.3,0.7}; 
String ex1[]={"co2","co","so2","n2","o2"};
double exn1[]={0.12863173578346163 ,0.378e-6,0,0.8545418425627018,0.016826421653836604};
g=g1;
gn=gn1;
ex=ex1;
exn=exn1;
}
else if(name.equals("biogas"))
{
// h2 0.116
// co 0.174
// ch4 0.082
// co2 0.131
// n2 0.382
String g1[]={"h2","co","ch4","co2","n2"};
double gn1[]={0.116,0.174,0.082,0.131,0.382}; 
String ex1[]={"co2","co","so2","n2","o2"};
double exn1[]={0.18885945343000554 ,0.378e-6,0,0.7990769659788065,0.012063580591187967};
g=g1;
gn=gn1;
ex=ex1;
exn=exn1;
}
else
{
String g1[]={"ch4","c2h6","c3h8","co2","h2","co","h2o"};
double gn1[]={0.908,0.059,0.006,0.018,0.0,0.0,0.0}; 
String ex1[]={"co2","co","so2","n2","o2"};
double exn1[]={0.11102730877510023 ,0.378e-6,0,0.8718063220150396,0.017166369209860154};
g=g1;
gn=gn1;
ex=ex1;
exn=exn1;
}
}
   public String setArea()
   {
	double nyakit=k1.V_NM3_h(QKW); 
	double myakit=k1.m_kg_h(QKW,Tfuel,1.01325);     
    String s="";  
    s+="    Program : Dr. Turhan Çoban, \n";
    s+="    Ege Üniversity, School of Engineering., Department of Mechanical Engineering\n";
    s+="    e-mail : turhan.coban@turhancoban.com\n";
    s+="    =======================================================\n";
    s+="    Combustion efficiency\n";
    s+="    Combustion energy output for 1 kmol fuel  Q="+k1.Qi()+" KJ/kmol fuel\n";
    s+="    Heating value of fuel Q_maximum ="+k1.Qmax()+" KJ/kmol fuel\n";
    s+="    Combustion efficiency Q/Qmaximum= "+k1.efficiency()+"\n";  
    double m1=k1.m_kg_h(QKW,Tfuel,1.01325);
    s+="    fuel inlet temperature   = "+Tfuel+" degree K\n";
    s+="    Air inlet temperature    = "+Tair+" degree K\n";
    s+="    exhaust exit temperature = "+Texhaust+" degree K\n";
    s+="    excess air  = "+excess_air+"\n";
    s+="    Required heat output    = "+QKW+" kW = "+QKW*859.8041557+"kCal/saat\n"; 
    s+="    fuel volumetric flow rate   = "+nyakit+" Nm^3/h\n";
    s+="    fuel mass flow rate    = "+myakit+" kg/h\n";
    s+="    molecular weight of fuel = "+k1.M_fuel()+"kg/kmol\n"; 
    s+="    stoichiometric volumetric air flow rate     = "+nyakit*k1.nair_stoichiometric+"Nm^3/h\n";
    s+="    sctoichiometric oxygen coefficient     = "+k1.nair_stoichiometric/(1.0+79.0/21.0)+"kmol/kmol fuel\n";
    s+="    volumetricair flow rate      = "+nyakit*k1.nair+" Nm^3/h\n";
    s+="    oxygen coefficient  = "+k1.nair/(1.0+79.0/21.0)+"kmol/kmol fuel\n";
    s+="    volumetric air /fuel ratio       = "+k1.kg_air_to_kg_fuel()+"  kg air/kg fuel \n";
    s+="    mass air /fuel ratio       = "+k1.kmol_air_to_kmol_fuel()+"kmol air/kmol fuel \n";
    s+="    Adyabatic flame temperature= "+k1.Ti(g,gn,Tfuel,Tair,excess_air,false,0.0)+" degree K";
    s+="    \n";
    s+="    "+k1.fuel(); 
    s+="    "+k1.air(); 
    s+="    "+k1.exhaust();
    s+="    "+k1.dry_exhaust();
    s+="    "+k1.denklem();     
    return s;
   }

	
    public void init()
    {      
      JTabbedPane jtp=new JTabbedPane();
	  b1=new JButton("Push button to calculate combustion efficency");
	  b1.addActionListener(this);
      hhv=new JCheckBox("Higher heating value ? ",true); 
      hhv.addItemListener(this);     
      JPanel panel[]=new JPanel[2];
      panel[0]= new JPanel(new BorderLayout());
      panel[1]= new JPanel(new FlowLayout());
      yakit_sec();
      Tfuel=298.0;//K
      Tair=298.0;//K
      Texhaust=162.5+298.0;//K
      excess_air=1.08;
      QKW=2816.0085875737277;
      Higher_heating_value=true;//üst ısıl değer üzerinden analiz
      
      String s1[]={"Fuel temperature, Tfuel",
                   "Air temperature, Tair",
                   "Exhaust gas temperature , Teksoz",
                   "excess air coefficient",
                   "Required combustion heat output"};
     String s2[]={"degree K",
                   "degree K",
                   "degree K",
                   " 0..1..",
                   "KW"};
     double t[]={Tfuel,Tair,Texhaust,excess_air,QKW};
     gp1=new girdiPE(g,gn,s1,s2,t,"Combustion efficiency input page");              
     k1=new boilerE(gp1.gas,gp1.ngas_percent,Tfuel,Tair,Texhaust,excess_air,Higher_heating_value); 
     output=new JTextArea(setArea()); 
     output.setFont(new Font("TimesRoman",Font.BOLD,14));
     setLayout(new BorderLayout(5,5));
     panel[0].add(gp1,BorderLayout.NORTH); 
     hhv_lhv=new JTextField(state[0]);
     hhv_lhv.setFont(new Font("TimesRoman",Font.BOLD,14));                      
     panel[1].add(hhv); 
     panel[1].add(hhv_lhv);     
     panel[0].add(panel[1],BorderLayout.CENTER); 
     panel[0].add(b1,BorderLayout.SOUTH);
     JScrollPane skrolPane=new JScrollPane(panel[0]);
     jtp.addTab("input panel",skrolPane);     
     jtp.addTab("output panel",new JScrollPane(output));
     add(jtp);            
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==b1)
    {
      double a[]=gp1.readText();
      double np[]=gp1.readNgas();
      String sg[]=gp1.readgas();
      gn=np;
      g=sg;
      Tfuel=a[0];//K
      Tair=a[1];//K
      Texhaust=a[2];//K
      excess_air=a[3];
      QKW=a[4];
      k1.set_boiler(gp1.gas,gn,Tfuel,Tair,Texhaust,excess_air,Higher_heating_value);
      output.setText(setArea());
    }
    repaint();
  }
              
public void itemStateChanged(ItemEvent e)
{   if(e.getSource()==hhv)
    { if(e.getStateChange()==ItemEvent.SELECTED)
        {Higher_heating_value=true;
        hhv_lhv.setText(state[0]);
        }
      else
        {Higher_heating_value=false;
        hhv_lhv.setText(state[1]);
        }
    }
    double a[]=gp1.readText();
     Tfuel=a[0];//K
     Tair=a[1];//K
     Texhaust=a[2];//K
     excess_air=a[3];
     QKW=a[4];
     k1.set_boiler(gp1.gas,gp1.ngas_percent,Tfuel,Tair,Texhaust,excess_air,Higher_heating_value);
    output.setText(setArea());
    repaint();
}  
    public static void main(String s[])
    {
        //main program
	    JFrame f = new JFrame("Combustion efficiency");
       f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    JApplet applet = new boilerTableE();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(1000,600));
        f.setVisible(true);
    }    
 }

