//======================================================
// Thermodynamic package
// Wet air simulation
// Dr. Turhan Coban
// EGE University, School of Engineering, Department of Mechanical eng.
// web:www.turhancoban.com
// =====================================================

/*
 * This program defines several psychrometric processes
*/
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 wetairprocessTable extends JApplet implements ActionListener
{ private static final long serialVersionUID = 9048579856L;
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JPanel panel[];
  JTextArea output;
  wetairprocess thp;
  public double P;
  final static String baslik[]={
    "mixing with mass flow rates",
    "mixing with volumetric flow rates",
    "cooling",
    "cooling - dehumidifying - heating",
    "cooling - dehumidifying - mixing",
    "heating",
    "heating - humidifying",
    "heating_adiabatic_humidifying",
    "adiabatic cooling",
    "summer air conditioning","winter_air conditioning"};
    JButton b1[]; 
    girdiP gp1[];


    public void setP(double ppi)
    {P=ppi;}
	
    public double getP(double ppi)
    {return P;}
	
    public double readP()
    {
	// pressure input from screen    
    // not that you can change pressure value inside program but you can draw 
    // psychrometric diagram only once for the pressure set up by this method
    String s;
    s=JOptionPane.showInputDialog("Enter system pressure as bar (1 atm = 1.01325 bar)");
    double pi=Double.parseDouble(s);     
    if(pi<=0 ) pi=1.01325;
    P=pi;
    thp=new wetairprocess(0.0,50.0,P);
    return pi;
    }
   
   public void setArea()
   {
    String s="";  
    s+="Program : Dr. Turhan Çoban, \n";
    s+="Ege University, School of Engineering, Department of Mechanical eng.\n";
    s+="web : www.turhancoban.com\n";   
    output.setText(s);
    output.setBackground(getBackground());
   }
    public void init()
    {
	init1(readP());
	}
	
    public void init1(double pp1)
    {      
      b1=new JButton[baslik.length];
      for(int iz=0;iz<b1.length;iz++)
      {  b1[iz]=new JButton("i = "+iz+" calculate the process");
	  b1[iz].addActionListener(this);}
	  double P1=pp1;
      String s=""; 
    s+="Program : Dr. Turhan Çoban, \n";
    s+="Ege University, School of Engineering, Department of Mechanical eng.\n";
    s+="web : www.turhancoban.com\n";   
      P=pp1;
      output=new JTextArea(s);    
      output.setBackground(getBackground());
      JTabbedPane tabbedPane = new JTabbedPane();
        panel=new JPanel[11];
        gp1=new girdiP[11];
        for(int i=0;i<11;i++)
        {panel[i]=new JPanel();
        } 
        //Wet air processes       
        //mixing with mass flow rates
        String s1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","mass flow rate m2","exit temperature T2","exit relative humidity "+'\u03A6'+"2"};        
        String s2[]={"kg/s","degree C","0..1","kg/s","degree C","0..1"};
        String s3="mixing with mass flow rates";
        double t[]={183.0,10.0,0.8,111.0,32.0,0.6};
        gp1[0]=new girdiP(s1,s2,t,s3);
        panel[0].add(gp1[0]);
        //mixing with volumetric flow rates
        // V1 volumetric flow rate,    first channel m^3/hour
        // T1 inlet temperature,  first channel degree C
        // f1 inlet relative humidity, first channel 0-1
        // V2 volumetric flow rate,    second channel m^3/hour
        // T2 inlet temperature,  second channel degree C
        // f2 inlet relative humidity, second channel 0-1  
        String s1_1[]={"volumetric flow rate V1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","volumetric flow rate V2","exit temperature T2","exit relative humidity "+'\u03A6'+"2"};        
        String s1_2[]={"m^3/hour","degree C","0..1","m^3/hour","degree C","0..1"};
        String s1_3="mixing with volumetric flow rates";
        double t_1[]={183.0,10.0,0.8,111.0,32.0,0.6};
        gp1[1]=new girdiP(s1_1,s1_2,t_1,s1_3);
        panel[1].add(gp1[1]);
        //cooling
        //double a[][]=thp.cooling(1.0, 35.0,0.5,25.0);
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 exit temperature,  degree C  
        String s2_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","exit temperature T2"};        
        String s2_2[]={"kg/s ","degree C","0..1","degree C"};
        String s2_3="cooling";
        double t_2[]={1.0,35.0,0.5,25.0};
        gp1[2]=new girdiP(s2_1,s2_2,t_2,s2_3);
        panel[2].add(gp1[2]);
        //cooling-dehumidifying-heating
        //double a[][]=thp.cooling_dehumidifying_heating(1.0,32.0,0.7,22.0,0.45);
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 exit temperature,  degree C
        // f2 exit relative humidity, 0-1  
        String s3_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","exit temperature T2","exit relative humidity "+'\u03A6'+"2"};        
        String s3_2[]={"kg/s ","degree C","0..1","degree C","0..1"};
        String s3_3="Cooling-dehumidifying-heating";
        double t_3[]={1.0,32.0,0.7,22.0,0.45};
        gp1[3]=new girdiP(s3_1,s3_2,t_3,s3_3);
        panel[3].add(gp1[3]);
        // Cooling-dehumidifying - mixing
        //double a[][]=thp.cooling_dehumidifying_mixing(1.0,35.0,0.5,0.5,10.0);
        // m1  soğutulan mass flow rate, kg/s
        // T1  cooling process inlet temperature,  degree C
        // f1  cooling process inlet relative humidity, 0-1
        // m2  non-cooling mixture mass flow rate, kg/s
        // T2  cooling process exit temperature,  degree C
        String s4_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","mass flow rate m2","exit temperature T2"};        
        String s4_2[]={"kg/s ","degree C","0..1","kg/s","degree C"};
        String s4_3="Cooling-dehumidifying - mixing";
        double t_4[]={1.0,35.0,0.5,0.5,10.0};
        gp1[4]=new girdiP(s4_1,s4_2,t_4,s4_3);
        panel[4].add(gp1[4]);
        //heating
        //double a[][]=thp.heating(1.0,8.0,0.3,32.0);
        // heating process
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 exit temperature,  degree C  
        String s5_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","exit temperature T2"};        
        String s5_2[]={"kg/s ","degree C","0..1","degree C"};
        String s5_3="heating";
        double t_5[]={1.0,8.0,0.3,32.0};
        gp1[5]=new girdiP(s5_1,s5_2,t_5,s5_3);
        panel[5].add(gp1[5]);
        //heating - humidifying
        //double a[][]=thp.heating_humidifying(1.0,8.0,0.3,32.0,20.0,0.5);
        // heating and humidifying process
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 heating process exit temperature degree C
        // T3 nemlendirma process exit temperature,  degree C 
        // f3 humidifying process exit relative humidity, 0-1
        String s6_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","heating process exit temperature T2","humidifying process exit temperature T3","humidifying prosei exit relative humidity "+'\u03A6'+"3"};        
        String s6_2[]={"kg/s ","degree C","0..1","degree C","degree C","0..1"};
        String s6_3="heating and humidifying";
        double t_6[]={1.0,8.0,0.3,32.0,20.0,0.5};
        gp1[6]=new girdiP(s6_1,s6_2,t_6,s6_3);
        panel[6].add(gp1[6]);
        // heating and humidifying process        
        //double a[][]=thp.heating_adiabatic_humidifying(1.0,8.0,0.3,32.0,20.0);
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 heating process exit temperature degree C
        // T3 adiabatic nemlendirma process exit temperature,  degree C 
        String s7_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","heating process exit temperature T2","humidifying process exit temperature T3"};        
        String s7_2[]={"kg/s ","degree C","0..1","degree C","degree C","0..1"};
        String s7_3="heating_adiabatic_humidifying";
        double t_7[]={1.0,8.0,0.3,32.0,20.0};
        gp1[7]=new girdiP(s7_1,s7_2,t_7,s7_3);
        panel[7].add(gp1[7]);  
        //adiabatic_cooling(double m, double T1, double f1,double T2)
        // adiabatic cooling
        // m  mass flow rate,    kg/s
        // T1 inlet temperature,  degree C
        // f1 inlet relative humidity, 0-1
        // T2 exit temperature,   degree C 
        String s8_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","humidifying process exit temperature T2"};        
        String s8_2[]={"kg/s ","degree C","0..1","degree C","degree C"};
        String s8_3="adiabatic humidifying-cooling";
        double t_8[]={1.0,32.0,0.06761043214044153,26.0};
        gp1[8]=new girdiP(s8_1,s8_2,t_8,s8_3);
        panel[8].add(gp1[8]);  
//=======   
        // summer air conditioning process        
        //double a[][]=thp.summer_air_conditioning(m1,m3,T2,f2,
        //                  T4,f4,T6,f6,dQsensible,dQlatent)
       // m1 mass flow rate,    room entrance kg/s
  // m3 mass flow rate,    eksoz havası kg/s
  // T2 room temperature,    degree C
  // f2 room relative humidity,   0-1
  // T4 outside temperature, degree C
  // f4 outside relative humidity 0-1
  // T6 evaporator exit temperature
  // f6 evaporaor exit realative humidity
  // dQsensible sensible heat girişi kW
  // dQlatent   latent heat girişi kW String s7_1[]={"mass flow rate m1","inlet temperature T1","inlet relative humidity "+'\u03A6'+"1","heating process exit temperature T2","humidifying process exit temperature T3"};        
        String s9_1[]={"m1 mass flow rate","m3 mass flow rate","T2",'\u03A6'+"2","T4",'\u03A6'+"4","T6",'\u03A6'+"6","dQsensible","dQlatent"};
        String s9_2[]={"kg/s ","kg/s ","degree C","0..1","degree C","0..1","degree C","0..1","kW","kW"};
        String s9_3="Summer air conditioning";
        double t_9[]={30.0,4.5,25.0,0.5,35.0,0.4,10.0,0.95,205.0,88.0};
        gp1[9]=new girdiP(s9_1,s9_2,t_9,s9_3);
        panel[9].add(gp1[9]);  
 
//========   
        // Winter air conditioning process
        //
        //
        String s10_1[]={"room inlet temperature","room exit temperature","room exit relative humidity '\u03A6' 2 "," outside inlet temperature ","outside relative humidityi '\u03A6'4 ","preheater exit temperature","sensible heat ","latent heat"};
        String s10_2[]={"degree C ","degree C ","0..1","degree C","0..1","degree C","kW","kW"};
        String s10_3="Winter air conditioning";
        double t_10[]={45,21,0.4,0,1.0,25.0,100.0,25.0};
        gp1[10]=new girdiP(s10_1,s10_2,t_10,s10_3);
        panel[10].add(gp1[10]);  

        
        for(int i=0;i<11;i++)
        {
        panel[i].add(b1[i]); 
	    tabbedPane.addTab(baslik[i],panel[i]);}
        add(  tabbedPane, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==b1[0])
    {
    double a[]=gp1[0].readText();
    thp.mixture_mass_based(a[0],a[1],a[2],a[3],a[4],a[5]);
    }
    else if (e.getSource()==b1[1])
    {
    double a[]=gp1[1].readText();
    thp.mixture_volumetric(a[0],a[1],a[2],a[3],a[4],a[5]);
    }
    else if (e.getSource()==b1[2])
    {
    double a[]=gp1[2].readText();
     thp.cooling(a[0],a[1],a[2],a[3]);
    }
    else if (e.getSource()==b1[3])
    {
    double a[]=gp1[3].readText();
     thp.cooling_dehumidifying_heating(a[0],a[1],a[2],a[3],a[4]);
    }
    else if (e.getSource()==b1[4])
    {
    double a[]=gp1[4].readText();
     thp.cooling_dehumidifying_mixing(a[0],a[1],a[2],a[3],a[4]);
    }
    else if (e.getSource()==b1[5])
    {
    double a[]=gp1[5].readText();
     thp.heating(a[0],a[1],a[2],a[3]);
    }
    else if (e.getSource()==b1[6])
    {
    double a[]=gp1[6].readText();
     thp.heating_humidifying(a[0],a[1],a[2],a[3],a[4],a[5]);
    }
    else if (e.getSource()==b1[7])
    {
    double a[]=gp1[7].readText();
     thp.heating_adiabatic_humidifying(a[0],a[1],a[2],a[3],a[4]);
    }
    else if (e.getSource()==b1[8])
    {
    double a[]=gp1[8].readText();
     thp.adiabatic_cooling(a[0],a[1],a[2],a[3]);
    }
    else if (e.getSource()==b1[9])
    {
    double a[]=gp1[9].readText();
     thp.summer_air_conditioning(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
    }
    else if (e.getSource()==b1[10])
    {
    double a[]=gp1[10].readText();
     thp.winter_air_conditioning(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
    }
    repaint();
  }            




  public static String toString(double left, int w, int d)
  // converts a double to a string with given width and decimals.
  {
	NumberFormat df=NumberFormat.getInstance(Locale.US);
	df.setMaximumFractionDigits(d);
    df.setMinimumFractionDigits(d);
    df.setGroupingUsed(false);
    String s = df.format(left);
    while (s.length() < w)
      s = " " + s;
    if (s.length() > w)
    {
        s = "";
        for (int i=0; i<w; i++)
          s = s + "-";
    }
    return s;
 }
 


    public static void open()
    {
       // main program for applet input
	   JFrame f = new JFrame("Wet air processes");
       f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	   JApplet applet = new wetairprocessTable();
       f.getContentPane().add("Center", applet);
       applet.init();
       f.pack();
       f.setSize(new Dimension(1000,800));
       f.setVisible(true);
    }
    
    public static void main(String s[])
    {
        //  main program
	    JFrame f = new JFrame("wet air and psychrometric processes");
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    JApplet applet = new wetairprocessTable();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(1000,800));
        f.setVisible(true);
    }    
}

