//NewGuide_State.java
/* A Java class for
 * Coaxial.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */

import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.net.URL;
import java.text.*;
import java.util.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;

public class NewGuide_State {
    //public boolean IsTEmode;
    public double a, b, w, a_mil, b_mil, w_mil; // waveguide dimensions
    public double a_minimum, a_maximum, a_mil_minimum, a_mil_maximum;
    public double epsilon_r, Aprime, Qprime, DeltaZ,xprime;
    public double mu_r, mu_r0, lambda_0; 
    public double sigma2, sigma_metal, rho_metal, Resistance, Resistance_surf;
    public double alpha_d, alpha_c, alpha_c2, alpha_c1, alpha_r, loss_tangent, skin_depth, alpha_total;
    public double frequency, frequency_minimum, frequency_maximum, frequency_c;
    public double angular_frequency, beta_total;
    public double guide_wavelength0, guide_wavelength;
    public double b_minimum, b_maximum, b_mil_minimum, b_mil_maximum, maximum_h;
    public double w_minimum, w_maximum, w_mil_minimum, w_mil_maximum;
    public double delta_geometry1, delta_geometry2;
    public double phase_velocity, phase_velocity0, light_velocity, light_velocity0;
    public double K, factor_less, factor_more;
    public double Capacitance1, Capacitance2, Capacitance, Inductance, Conductance;
    public Complex ZeroC, Z0;
    public double ratio;
    public double Z0_0, medium_impedance, Z0f_max, Z0f_min, Z0g_max, Z0g_min;
    public double x[], F_array[], G_array[];
    public double x_geom1[], x_geom2[];
    //public boolean IsImpedanceOn;
    //public boolean IsWarningOn1;
    //public boolean IsWarningOn2;
    //public boolean IsSafetyOn;
    //public boolean IsPrintSafetyOn;
    public boolean IsMilsOn;
    public boolean IsWZero, OptionOne, OptionTwo;
    
    public static final double mil_factor = 0.0254; // 1 mil = 0.0254 mm
    //public static final double epsilon0 = 8.8541878176E-12; //Units: F/m
    // Approximate epsilon for phase velocity
    public static final double epsilon0 = 8.841941286E-12; //Units: F/m
    public static final double mu0 = 1.25663706144E-6; //Units H/m
    
    public boolean LicenseExpired;
    public int this_month, today_week, this_year, this_hour, this_minute, today_month, 
	       today_year,this_zone, saving_time;       
    GregorianCalendar Greg = new GregorianCalendar();

    public Font ttfFont, sanSerifFont, serifFont, symbolFont;

    
    public NewGuide_State(){
	
	a = 10.0; // mm Outer Radius
	a_minimum = 1.0;
	//a_maximum = 11.0;
	a_maximum = 20.0;
	
	b = 0.005; //mm
	b_minimum = 0.0;
	b_maximum = 0.5;
	
	w = 1.50;  //mm Inner Radius
	w_minimum = 0.0;
	w_maximum = 10.0;
	
	a_mil = a/mil_factor;
	a_mil_minimum = a_minimum/mil_factor;
	a_mil_maximum = a_maximum/mil_factor;
	
	b_mil = b/mil_factor;
	b_mil_minimum = b_minimum/mil_factor;
	b_mil_maximum = b_maximum/mil_factor;
	
	w_mil = w/mil_factor;
	w_mil_minimum = w_minimum/mil_factor;
	w_mil_maximum = w_maximum/mil_factor;
	
	epsilon_r = 2.3;
	mu_r = 1.0;
	
	loss_tangent = 0.0;
	rho_metal = 1.725e-8;
	sigma_metal = 1.0/rho_metal;
	sigma2 = 0.0;
	medium_impedance = Math.sqrt(mu0*mu_r/(epsilon0*epsilon_r));
	frequency = 1.0E9;
	frequency_minimum = 0.0;
	frequency_maximum = 20.0E9;
	angular_frequency = 2.0 * Math.PI * frequency;
	frequency_c = 0.0;
	
	loss_tangent = sigma2/(angular_frequency*epsilon_r*epsilon0);
	//sigma2 = loss_tangent * (angular_frequency*epsilon_r*epsilon0);
	skin_depth = 1.0/Math.sqrt(Math.PI*frequency*mu0*sigma_metal);
	
	delta_geometry2 = (w_maximum - w_minimum)/10000;
	delta_geometry1 = (a_maximum - a_minimum)/10000;
	Z0_0 = 0.5*Math.log(a/w)*medium_impedance/Math.PI;
	Z0 = new Complex(Z0_0,0.0);
	lambda_0 = 1.0/Math.sqrt(epsilon0*mu0)/frequency;
	ZeroC = new Complex(0.0,0.0);
	IsMilsOn = false;
	IsWZero = false;
	OptionOne = true;
        OptionTwo = false;

	this_month = Greg.get(Calendar.MONTH);
	//System.out.println("  This is the month = "+this_month);
	today_week = Greg.get(Calendar.DAY_OF_WEEK);
	//System.out.println("  This is the day_week = "+today_week);
	this_year = Greg.get(Calendar.YEAR);
	this_hour = Greg.get(Calendar.HOUR_OF_DAY);
	//System.out.println("  This is the year = "+this_year);
	this_minute = Greg.get(Calendar.MINUTE);
	//System.out.println("  This is the minute = "+this_minute);
	today_month = Greg.get(Calendar.DAY_OF_MONTH);
	//System.out.println("  This is the day_month = "+today_month);
	today_year = Greg.get(Calendar.DAY_OF_YEAR);
	//System.out.println("  This is the day_year = "+today_year);
	this_zone = Greg.get(Calendar.ZONE_OFFSET);
	//System.out.println("  This is the zone_offset = "+this_zone/3600000);
	saving_time = Greg.get(Calendar.DST_OFFSET);
	//System.out.println("  This is the dst_offset = "+saving_time/3600000);
	
	ignition();
	scan_coefficients();
 }
    
    public void ignition(){
	Complex factor1, factor2;
	
	
	angular_frequency = 2.0 * Math.PI * frequency;
	light_velocity = 1.0/(Math.sqrt(epsilon0*epsilon_r*mu0*mu_r));
	light_velocity0 = 1.0/(Math.sqrt(epsilon0*mu0));
	lambda_0 = light_velocity0/frequency;
	sigma_metal = 1.0/rho_metal;
	
	if(!IsMilsOn){
	    a_mil = a/mil_factor;
	    a_mil_minimum = a_minimum/mil_factor;
	    a_mil_maximum = a_maximum/mil_factor;
	
	    w_mil = w/mil_factor;
	    w_mil_minimum = w_minimum/mil_factor;
	    w_mil_maximum = w_maximum/mil_factor;
	}
	if(IsMilsOn){
	    a = a_mil*mil_factor;
	    a_minimum = a_mil_minimum*mil_factor;
	    a_maximum = a_mil_maximum*mil_factor;
	
	    w = w_mil*mil_factor;
	    w_minimum = w_mil_minimum*mil_factor;
	    w_maximum = w_mil_maximum*mil_factor;
	}
	
	delta_geometry2 = (w_maximum - w_minimum)/10000;
	delta_geometry1 = (a_maximum - a_minimum)/10000;
	
	Inductance = mu_r*mu0*Math.log(a/w)/(2.0*Math.PI);
	Capacitance = epsilon_r*2.0*Math.PI*epsilon0/Math.log(a/w);
	Conductance = sigma2*2.0*Math.PI/Math.log(a/w);
	Resistance_surf = Math.sqrt(Math.PI*frequency*mu0*rho_metal);
	Resistance = 0.5*Resistance_surf/Math.PI*(1.0/(a*0.001) + 1.0/(w*0.001));
	factor1 = new Complex(Resistance,angular_frequency*Inductance);
	factor2 = new Complex(Conductance,angular_frequency*Capacitance);
	
	if(a <= w) {
	    Z0 = new Complex(0.0,0.0);
	}
	else{
	    
	    if(Conductance ==0.0 && Resistance == 0.0){
		Z0 = new Complex(Math.sqrt(Inductance/Capacitance), 0.0);
	    }
	    else{
		
		Z0 = Complex.Pow(Complex.Divide(factor1,factor2),0.5);
		
	    }
	}
	loss_tangent = sigma2/(angular_frequency*epsilon_r*epsilon0);
	skin_depth = 1.0/Math.sqrt(Math.PI*frequency*mu0*sigma_metal);
	frequency_c = light_velocity/(Math.PI*(a+w)*0.001);
	
	
      //Conductor loss - attenuation constant 
      
      
	//alpha_c = Resistance *(1.0/a + 1.0/w)/(2*medium_impedance*Math.log(a/w));
	alpha_c = Resistance*0.5*Math.sqrt(Capacitance/Inductance);
	//alpha_d = 27.3 * Math.sqrt(epsilon_r)*loss_tangent/lambda_0;
	alpha_d = 0.5*Conductance*Math.sqrt(Inductance/Capacitance);
	alpha_total = Complex.Real(Complex.Pow(Complex.Multiply(factor1,factor2),0.5));
	beta_total  = Complex.Imaginary(Complex.Pow(Complex.Multiply(factor1,factor2),0.5));
	//System.out.println("alpha_total = "+alpha_total+"   beta_total = "+beta_total);
     
	if(w == 0.0){
	    IsWZero = true;
	}
	else{
	    IsWZero = false;
	}

}
    
public void scan_coefficients(){
	double tCapacitance, tInductance, tConductance, tResistance;
	Complex tfactor1, tfactor2;
	Complex Impedance;
	x = new double[10001];
	x_geom1 = new double[10001];
	x_geom2 = new double[10001];
	F_array = new double[10001];
	G_array = new double[10001];
	
	for(int i=0;i<10001;i++){
	    x_geom1[i] = a_minimum+(double)i*delta_geometry1;
	    x_geom2[i] = w_minimum+(double)i*delta_geometry2;
	}
	    
    for(int i = 0; i<10001; i++){
	    
	    tInductance = mu_r*mu0*Math.log(x_geom1[i]/w)/(2.0*Math.PI);
	    tCapacitance = epsilon_r*2.0*Math.PI*epsilon0/Math.log(x_geom1[i]/w);
	    tConductance = sigma2*2.0*Math.PI/Math.log(x_geom1[i]/w);
	    tResistance = Math.sqrt(Math.PI*frequency*mu0*rho_metal);
	
	if(x_geom1[i] <= w) {
	    Impedance = new Complex(0.0,0.0);
	}
	else{
	    
	    if(tConductance ==0.0 && tResistance == 0.0){
		Impedance = new Complex(Math.sqrt(tInductance/tCapacitance), 0.0);
	    }
	    else{
		tfactor1 = new Complex(tResistance,angular_frequency*tInductance);
		tfactor2 = new Complex(tConductance,angular_frequency*tCapacitance);
		Impedance = Complex.Pow(Complex.Divide(tfactor1,tfactor2),0.5);
		
	    }
	}
	if(Impedance != Impedance){Impedance = new Complex(0.0,0.0);}
	    
	    x[i] = x_geom1[i];
	    
		F_array[i] = Complex.Real(Impedance);
    } 
	
    if(w == 0.0){
	   
		for(int i = 0; i<10001; i++){
		    F_array[i]=1.0;
    		}
		Z0f_max = 1.0;
		Z0f_min = 0.0;
		
    }
    else{
	  
	    if(a_minimum == 0.0){
		Z0f_min = 0.0;
	    }
	    else{
		Z0f_min = Math.max((double)((int)F_array[0])-1.0, 0.0);
	    }
	    Z0f_max = (double)((int)F_array[10000])+1;	    
    }
	
    for(int i = 0; i<10001; i++){
	    
	    tInductance = mu_r*mu0*Math.log(a/x_geom2[i])/(2.0*Math.PI);
	    tCapacitance = epsilon_r*2.0*Math.PI*epsilon0/Math.log(a/x_geom2[i]);
	    tConductance = sigma2*2.0*Math.PI/Math.log(a/x_geom2[i]);
	    tResistance = Math.sqrt(Math.PI*frequency*mu0*rho_metal);
	
	if(a <= x_geom2[i]) {
	    Impedance = new Complex(0.0,0.0);
	}
	else{
	    
	    if(tConductance ==0.0 && tResistance == 0.0){
		Impedance = new Complex(Math.sqrt(tInductance/tCapacitance), 0.0);
	    }
	    else{
		tfactor1 = new Complex(tResistance,angular_frequency*tInductance);
		tfactor2 = new Complex(tConductance,angular_frequency*tCapacitance);
		Impedance = Complex.Pow(Complex.Divide(tfactor1,tfactor2),0.5);
		
	    }
	}
	if(Impedance != Impedance){Impedance = new Complex(0.0,0.0);}
	    
	
	    x[i] = x_geom2[i];
	    G_array[i] = Complex.Real(Impedance);
	}
	

	    if(w_minimum == 0){G_array[0] = G_array[1];}
	    Z0g_max = (double)((int)G_array[0])+1.0;
	    if(G_array[10000] <= 0.0){
		Z0g_min = (double)((int)G_array[10000]);
	    }
	    else{
		Z0g_min = (double)((int)G_array[10000])-1.0;
	    }
	
   } 
    
}
