//State.java
/* A Java class for
 * LossyWide.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;

/*
    R : Series resistance per unit length, for both conductors, in Ohms/m
    L : Series inductance per unit length, for both conductors, in H/m
    G : Shunt conductance per unit length, in S/m
    C : Shunt capacitance per unit length, in F/m
    Gamma : Propagation Constant
    Z0 : Characteristic Impedance
*/
public class Trans_State {
    double L, C, G, R;
    Complex gamma, Gammain, GammaL, Z0;
    Complex Zin, Yin; //Input Impedance
    Complex Vin, Iin; //Input Voltage and Currents
    Complex VPlus, one;
    double alpha, beta;
    protected double frequency, angular_frequency;
    double xpos, lineLength, lineLength_part1, lineLength_part2, VSWR;
    int linecounter1 = 1000, linecounter2 = 0;
    double epsilon_r, mu_r;
    public double x[], y1[], y2[], y3[], y4[], y3min[], y4min[], y5[], y6[], y5A[], y6A[], y5B[], y6B[], y5C[], y6C[]; //cursor position, variables
    double y1T[], y2T[]; //time dependent variables
    Complex ZL, YL; //Load
    Complex Voltage[], Current[], Power[];
    Generator generator;
    public static final int NPoints=301;
    public int NTime, ctime, dtime;
    boolean IsLowLoss, IsLossLess, is_Load_Ztype;//true for low loss, false for high loss.
    boolean IsLargerThanOne;
    Complex timeFactor;
    public double wavelength;
    public double Power_input;
    public double Power_load;
    public double Power_generator;
    public double Power_Zg;
    public double Power_flow, Power_X;
    private static final double epsilon0 = 8.8541878176E-12; // Units: F/m - exact value
    private static final double mu0 = 1.25663706144E-6; //      Units H/m  - exact value
    //private static final double light_velocity = Math.sqrt(1.0/(epsilon0*mu0)); //  Units m/s - exact value
    private static final double light_velocity = 3.0e8; //  Units m/s - approximate value as in textbook
    
    public int IsPhasorPlot = 1;
    public int IsTimePlot = 1;
    public boolean IwantTraceOn;
    public boolean IsLoadShort, IsLoadOpen, IsLoadRegular, IsLoadImaginary, IsLoadReal, GetPowerEnvelope;
    
    public boolean LicenseExpired;
    public boolean IsIncident, IsReflected, IsTotal, IsTcpVisible, IsEnvelope, IsTronRunning;
    public boolean OptionOne, OptionTwo, OptionThree;
        
    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, italicFont;


    public Trans_State(){
	
	L = 0.3794;
	C = 67.447;
	R = 1.0;
	G = 0.0033;    
	
	IsLowLoss=true;
        IsLossLess=true; // alpha must be = 0 for true.
	IsLargerThanOne = false;
	is_Load_Ztype = true;
	frequency = 1.0E9;
	
	
	lineLength=1.0;
	lineLength_part1 = 1.0;
	lineLength_part2 = 0.0;
        
        IsIncident = false;
        IsReflected = false;
        IsTotal =true;
        IsTcpVisible = true;
        IsEnvelope = true;
        IsTronRunning = false;
        
        IsLoadRegular = true;
        IsLoadReal = true;
        IsLoadShort = false;
        IsLoadOpen = false;
        IsLoadImaginary = false;
        GetPowerEnvelope = false;
        
        OptionOne = true;
        OptionTwo = false;
        OptionThree = false;
        
        IwantTraceOn = false;
	
	xpos = 0.0;
	
	epsilon_r = 1.0;
	mu_r = 1.0;
	
	wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency);
	one = new Complex(1.0,0.0);
	ZL=new Complex(100.0,0.0);
	YL=new Complex(0.01,0.0);
	Zin = new Complex(100.0,0.0);
	Z0 = new Complex(50.0,0.0);
	alpha = 0.0;
	beta = 2.0*Math.PI;
	
	generator = new Generator();
	
	//graph arrays
	x=new double[NPoints];
	y1=new double[NPoints];
	y2=new double[NPoints];
        y3=new double[NPoints];
	y4=new double[NPoints];
        y3min=new double[NPoints];
	y4min=new double[NPoints];
        
        y5=new double[NPoints];
        y6=new double[NPoints];
        y5A=new double[NPoints];
        y6A=new double[NPoints];
        y5B=new double[NPoints];
        y6B=new double[NPoints];
        y5C=new double[NPoints];
        y6C=new double[NPoints];
        
        y1T=new double[NPoints];
	y2T=new double[NPoints];
        for(int i=0; i<x.length;i++){
	    x[i]=i*lineLength/(NPoints-1);
	}
	MaestroA.randomizer(y1,2.0,-2.0,1);
	MaestroA.randomizer(y2,2.0,-2.0,2);
	
	//Time dependent arrays
	Voltage = new Complex[NPoints];
	Current = new Complex[NPoints];
	Power = new Complex[NPoints];
	
	//Timefactor
	NTime=360;
	ctime=0;
	dtime=10;
	timeFactor = new Complex(Math.cos(2.0*Math.PI*ctime/NTime),
				 Math.sin(2.0*Math.PI*ctime/NTime));
				 
	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);
				 
	pre_ignition();
	ignition();
    }
    
    public void pre_ignition(){
        
        
	//graph arrays
	x=new double[NPoints];
	y1=new double[NPoints];
	y2=new double[NPoints];
        y3=new double[NPoints];
	y4=new double[NPoints];
        y3min=new double[NPoints];
	y4min=new double[NPoints];
        y5=new double[NPoints];
        y6=new double[NPoints];
        y5A=new double[NPoints];
        y6A=new double[NPoints];
        y5B=new double[NPoints];
        y6B=new double[NPoints];
        y5C=new double[NPoints];
        y6C=new double[NPoints];
        
        y1T=new double[NPoints];
	y2T=new double[NPoints];
        for(int i=0; i<x.length;i++){
	    x[i]=i*lineLength/(NPoints-1);
	}
	MaestroA.randomizer(y1,2.0,-2.0,1);
	MaestroA.randomizer(y2,2.0,-2.0,2);
	
	//Time dependent arrays
	Voltage = new Complex[NPoints];
	Current = new Complex[NPoints];
	Power = new Complex[NPoints];
	
	angular_frequency = 2*Math.PI*frequency;
	
	//wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency);
	
	if(!IsLowLoss){
		    Z0=getZ0(R,L*1.0e-6,G,C*1.0e-12,angular_frequency);
		    gamma = getgamma(R,L*1.0e-6,G,C*1.0e-12,angular_frequency);
		    
		    beta = Complex.Imaginary(gamma);
		    wavelength = 2*Math.PI/beta;
		    beta = 2.0*Math.PI;
		    alpha = Complex.Real(gamma)*wavelength;
		    epsilon_r = Math.pow(light_velocity,2)*L*C*1.0e-18;
                    IsLossLess = false;
	}
	if(IsLowLoss){
		    beta = 2.0*Math.PI;
		    wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency);
                    if(alpha == 0.0){
                        IsLossLess = true;
                    }
                    else{
                        IsLossLess = false;
                    }
        }
	
	//Compute Gamma at Load
	    Complex GammaShort = new Complex(-1.0,0.0);
	    Complex GammaOpen  = new Complex(1.0,0.0);
	    
	if(is_Load_Ztype){
	    GammaL = EMF.computeGamma(ZL,Z0);
	    if(GammaL == GammaShort){//short
		YL = new Complex(0.0,-1.0E135);
	    }
	    else{
	    YL = Complex.Divide(one,ZL);
	    }
	}
	else{
	    GammaL = EMF.computeGamma(YL,Z0,false);
	    if(GammaL == GammaOpen){//open
		ZL = new Complex(0.0,-1.0E135);
	    }
	    else{
		ZL = Complex.Divide(one,YL);
	    }
	}
	VSWR = (1.0+Complex.Magnitude(GammaL))/(1-Complex.Magnitude(GammaL));
	VPlus = EMF.computeVPlus(generator,GammaL,Z0,lineLength,alpha,wavelength);    
	
		    //voltage reflection coefficient is larger than one!!!!
		    if(Complex.Magnitude(GammaL) > 1.0){
			IsLargerThanOne = true;
		    } 
		    else{
			IsLargerThanOne = false;
		    }  
    }
    
    public void ignition(){
	Complex temp2,temp3,temp4,Zin2,Yin2;
	
	if(!IsLowLoss){
		    Z0=getZ0(R,L*1.0e-6,G,C*1.0e-12,angular_frequency);
		    gamma = getgamma(R,L*1.0e-6,G,C*1.0e-12,angular_frequency);
		    alpha = Complex.Real(gamma);
		    beta = Complex.Imaginary(gamma);
		    wavelength = 2*Math.PI/beta;
		    alpha = Complex.Real(gamma)*wavelength;
		    beta = 2.0*Math.PI;
		    
	}
	if(IsLowLoss){
		    beta = 2.0*Math.PI;
		    wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency);
	}
	//Compute Gamma at Load
	Complex GammaShort = new Complex(-1.0,0.0);
	Complex GammaOpen  = new Complex(1.0,0.0);
	    
	if(is_Load_Ztype){
	    GammaL = EMF.computeGamma(ZL,Z0);
	    if(GammaL == GammaShort){//short
		YL = new Complex(0.0,-1.0E135);
	    }
	    else{
	    YL = Complex.Divide(one,ZL);
	    }
                
                if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
                    IsLoadShort = true;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if((Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)<-1.0E130)||Complex.Magnitude(ZL)>1.0E130){
                    IsLoadOpen = true;
                    IsLoadShort = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if((Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)!=0.0) ||
                    (Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)==0.0))
                {
                    IsLoadRegular = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    
                    if(Complex.Imaginary(ZL)==0.0){IsLoadReal = true;}
                    else{IsLoadReal = false;}
                }

	}
	else{
	    GammaL = EMF.computeGamma(YL,Z0,false);
	    if(GammaL == GammaOpen){//open
		ZL = new Complex(0.0,-1.0E135);
	    }
	    else{
		ZL = Complex.Divide(one,YL);
	    }
            
                if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)==0.0){
                    IsLoadShort = false;
                    IsLoadOpen = true;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if((Complex.Real(YL)==0.0 && Complex.Imaginary(YL)<-1.0E130)||Complex.Magnitude(YL)>1.0E100 ||
                    Complex.Magnitude(ZL)==0.0){
                    IsLoadOpen = false;
                    IsLoadShort = true;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = false;
                    IsLoadReal = false;
                }
                else if((Complex.Real(YL)!=0.0 && Complex.Imaginary(YL)!=0.0) ||
                    (Complex.Real(YL)!=0.0 && Complex.Imaginary(YL)==0.0))
                {
                    IsLoadRegular = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    if(Complex.Imaginary(YL)==0.0){IsLoadReal = true;}
                    else{IsLoadReal = false;}
                }
	}
	
	//Compute Gamma at xpos
	if(alpha == 0.0){
	    Gammain = EMF.computeGammaAt(GammaL,xpos);
	}
	else{
	    Gammain = EMF.computeGammaAt(GammaL,alpha,xpos,wavelength);
	}
	
	//Compute V+
	VPlus = EMF.computeVPlus(generator,GammaL,Z0,lineLength,alpha,wavelength);
	
	//Compute Zin
	
	if(alpha == 0.0){
	    Zin = EMF.computeZinAt(GammaL,Complex.Real(Z0),xpos,true);
	    Yin = EMF.computeYinAt(GammaL,Complex.Real(Z0),xpos,true);
	}
	else{
	    Zin = EMF.computeZinAt(GammaL,Z0,xpos,alpha,wavelength);
	    Yin = EMF.computeYinAt(GammaL,Z0,xpos,alpha,wavelength);
	}
	//Compute Vin
	{
	    Complex one = new Complex(1.0,0.0);
	    Complex factor = new Complex(alpha*xpos,2*Math.PI*xpos);
	    Complex Expo = Complex.Exp(factor);
	    Complex Reflecto = Complex.Add(one,Gammain);
	    Complex Reflecto2 = Complex.Subtract(one,Gammain);
	    Complex VoltScal = Complex.Multiply(Expo,Reflecto);
	    Complex CurScal = Complex.Divide(Complex.Multiply(Expo,Reflecto2),Z0);
	    Vin=Complex.Multiply(VPlus,VoltScal);
	    if(Zin.Real()==0.0 && Zin.Imaginary()==0.0){
		Iin=Complex.Multiply(VPlus,CurScal);
	    }
	    else
		//Compute Iin
		Iin=Complex.Divide(Vin,Zin);
	    }
	
	
	//Compute Power
	    Complex one = new Complex(1.0,0.0);
	    Complex factor_in = new Complex(alpha*lineLength,2*Math.PI*lineLength);
	    Complex Expo_in = Complex.Exp(factor_in);
	    Complex Gamma_in = EMF.computeGammaAt(GammaL,alpha,lineLength,wavelength);
	    Complex Reflecto_in = Complex.Add(one,Gamma_in);
	    Complex VoltScal_in = Complex.Multiply(Expo_in,Reflecto_in);
	    Complex V_input=Complex.Multiply(VPlus,VoltScal_in);
	    Complex V_load = Complex.Multiply(VPlus,Complex.Add(one,GammaL));
	    Complex I_load = Complex.Divide(Complex.Multiply(VPlus,Complex.Subtract(one,GammaL)),Z0);
	    
	    Complex Z_input = EMF.computeZinAt(GammaL,Z0,lineLength,alpha,wavelength);
	    Complex Y_input = EMF.computeYinAt(GammaL,Z0,lineLength,alpha,wavelength);
	    
	    Complex I_input = Complex.Multiply(V_input,Y_input);
	    
	    Power_input = 0.5 * Complex.Real(Complex.Multiply(V_input,Complex.Conjugate(I_input)));
	    //Power_load  = 0.5 * Math.pow(Complex.Magnitude(VPlus),2)/Z0 * (1 - Math.pow(Complex.Magnitude(GammaL),2));
	    Power_load = 0.5 * Complex.Real(Complex.Multiply(V_load,Complex.Conjugate(I_load)));
	    Power_generator = 0.5 * Complex.Real(Complex.Multiply(generator.getVg(),Complex.Conjugate(I_input))); 
	    Power_Zg = 0.5 * Complex.Real(Complex.Multiply(Complex.Subtract(generator.getVg(),V_input),Complex.Conjugate(I_input))); 
	    Power_flow = 0.5 * Complex.Real(Complex.Multiply(Vin,Complex.Conjugate(Iin)));
            
            
      	//Complex one = new Complex(1.0,0.0);
	for(int i=0; i<x.length;i++)
	{
	    x[i] = i * lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
		                
                 if(IsTotal){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(one,Gammain)),Z0);   
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
                                                Complex.Add(one,Gammain));     
                }
                else if(IsIncident){
                    Current[i]=Complex.Divide(Complex.Multiply(VPlus,tmp1),Z0); 
                    Voltage[i]=Complex.Multiply(VPlus,tmp1); 
                }
                else if(IsReflected){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(new Complex(0.0,0.0),Gammain)),Z0);     
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),Gammain);
                }
		
		//if(IsLowLoss && alpha == 0.0 &&(Complex.Real(ZL)==Complex.Real(Z0) && Complex.Imaginary(ZL)==0.0)){
                //    y3[i] = Complex.Magnitude(VPlus);
		//    y4[i] = Complex.Magnitude(VPlus)/Complex.Real(Z0);
                //    y3min[i] = -y3[i];
		//    y4min[i] = -y4[i];
                //}
		//else
                {
                    y3[i] = Voltage[i].Magnitude();
		    y4[i] = Current[i].Magnitude();
                    y3min[i] = -y3[i];
		    y4min[i] = -y4[i];
		}
                
                /*
                if(IsTotal){
                    y5[i]=y5A[i];
                    y6[i]=y6A[i];
                }
                else if(IsIncident){
                    y5[i]=y5B[i];
                    y6[i]=y6B[i];
                }
                else if(IsReflected){
                    y5[i]=y5C[i];
                    y6[i]=y6C[i];
                }
                */
        }
          
        Complex CurreX, VoltaX, timeFactorX;
        double Power_test;
        Power_X = 0.0;
        
        Gammain = EMF.computeGammaAt(GammaL,alpha,xpos,wavelength);
	Complex tmp1, tmp;
	tmp = new Complex(alpha,beta);
	tmp.Multiply(xpos);
	tmp1=Complex.Exp(tmp);
	timeFactorX = new Complex(1.0,0.0);	
        CurreX=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(one,Gammain)),Z0);   
        VoltaX=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
                                                Complex.Add(one,Gammain));
        for(int ij = 0; ij < 360; ij++){
              timeFactorX.setReal(Math.cos(2.0*Math.PI*ij/360));
              timeFactorX.setImaginary(Math.sin(2.0*Math.PI*ij/360));
              Power_test = Complex.Real(Complex.Multiply(VoltaX,timeFactorX))*
                                   Complex.Real(Complex.Multiply(CurreX,timeFactorX));
              if(Power_test > Power_X){Power_X = Power_test;}
        }
                    
    }
    
    private Complex getgamma(double R, double L, double G, double C, double w){
	Complex t1, t2, t3;
	t1 = new Complex(R,w*L);
	t2 = new Complex(G,w*C);
	t3 = Complex.Multiply(t1,t2);
	t3.Pow(0.5);
	return t3;
    }
    
    private Complex getZ0(double R, double L, double G, double C, double w){
	Complex t1, t2, t3;
	t1 = new Complex(R,w*L);
	t2 = new Complex(G,w*C);
	t3 = Complex.Divide(t1,t2);
	t3.Pow(0.5);
	return t3;
    }
    
    public void ScanTimePowerBounds(){
        pre_ignition();
    	ignition();
       
        Complex Volta=new Complex(1.0,0.0);
        Complex Curre=new Complex(1.0,0.0);
        for(int i=0; i<x.length;i++)
	{
	    x[i] = i * lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
		                
                int TimeFac = 360;
                double MaxPower = 0.0;
                double MinPower = 0.0;
                double PowerRunning = 0.0;
                    
                 
                    Curre=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(one,Gammain)),Z0);   
                    Volta=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
                                                Complex.Add(one,Gammain));     
                    for(int ij = 0; ij < TimeFac; ij++){
                        timeFactor.setReal(Math.cos(2.0*Math.PI*ij/TimeFac));
                        timeFactor.setImaginary(Math.sin(2.0*Math.PI*ij/TimeFac));
                        PowerRunning = Complex.Real(Complex.Multiply(Volta,timeFactor))*
                                   Complex.Real(Complex.Multiply(Curre,timeFactor));
                        if(PowerRunning > MaxPower){MaxPower = PowerRunning;}
                        if(PowerRunning < MinPower){MinPower = PowerRunning;}
                    }    
                    y5A[i] = MaxPower;
                    y6A[i] = MinPower;
                
                    y5B[i] = VPlus.Magnitude2()*Math.exp(2*alpha*x[i])/Complex.Magnitude(Z0);
                    y6B[i] = 0.0;   
                
                    y5C[i] = 0.0;
                    y6C[i] = -VPlus.Magnitude2()*Complex.Magnitude(GammaL)*Complex.Magnitude(GammaL)
                             *Math.exp(-2*alpha*x[i])/Complex.Magnitude(Z0);
	}
    }
    
    public void ScanImpedance(){
	pre_ignition();
	ignition();
	
	double maxBound = 10*Complex.Magnitude(Z0);
	double minBound = 1.0/maxBound;
	Complex one = new Complex(1.0,0.0);
	for(int i = 0; i < x.length; i++){
	   
	    if(i==0){
		x[0]=lineLength/(x.length-1);
		//Compute Gamma at xpos
		Gammain = EMF.computeGammaAt(GammaL,alpha,x[0],wavelength);
		Complex Ztmp = Complex.Multiply(Z0,Complex.Divide(
			Complex.Add(one,Gammain),Complex.Subtract(one,Gammain)));
		if(Complex.Magnitude(Ztmp)<maxBound){
		    x[0]=0;
		    Gammain = EMF.computeGammaAt(GammaL,alpha,x[0],wavelength);
		    Ztmp = Complex.Multiply(Z0,Complex.Divide(
			Complex.Add(one,Gammain),Complex.Subtract(one,Gammain)));
		}
		y1[0]=Ztmp.Real();
		y2[0]=Ztmp.Imaginary();
		
		if(y1[0] < minBound){y1[0] = 0.0;}
		if(y1[0] > maxBound){y1[0] = maxBound;}
		if(y2[0] > maxBound){y2[0] = maxBound;}
		if(y2[0] <-maxBound){y2[0] = -maxBound;}
	    }
	    else{
	    
	    //Compute Zin
	    	 x[i]=i*lineLength/(x.length-1);
	    //Compute Gamma at xpos
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		    Complex Ztmp = Complex.Multiply(Z0,Complex.Divide(
			Complex.Add(one,Gammain),Complex.Subtract(one,Gammain)));
		
		y1[i]=Ztmp.Real();
		y2[i]=Ztmp.Imaginary();
		
		if(y1[i] < minBound){y1[i] = 0.0;}
		if(y1[i] > maxBound){y1[i] = maxBound;}
		if(y2[i] > maxBound){y2[i] = maxBound;}
		if(y2[i] <-maxBound){y2[i] = -maxBound;}
	    }
	}
    }
	
    public void ScanAdmittance(){
	pre_ignition();
	ignition();
	double maxBound = 10.0/Complex.Magnitude(Z0);
	double minBound = maxBound/10000;
	
	Complex one = new Complex(1.0,0.0);
	for(int i = 0; i < x.length; i++){
	if(i == 0){
	     x[1]=lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[1],wavelength);
	    //Compute Zin
	    
		Complex Ztmp = Complex.Divide(Complex.Divide(
			    Complex.Subtract(one,Gammain),Complex.Add(one,Gammain)),Z0);
			    
	    if(Complex.Magnitude(Ztmp)<maxBound){
	    
		x[0]=i*lineLength/(x.length-1);
		//Compute Gamma at xpos
		Gammain = EMF.computeGammaAt(GammaL,alpha,x[0],wavelength);
		//Compute Zin
	    	Ztmp = Complex.Divide(Complex.Divide(
			    Complex.Subtract(one,Gammain),Complex.Add(one,Gammain)),Z0);
	    }
		y1[0]=Ztmp.Real();
		y2[0]=Ztmp.Imaginary();
		
		if(y1[0] < minBound){y1[0] = 0.0;}
		if(y1[0] > maxBound){y1[0] = maxBound;}
		if(y2[0] > maxBound){y2[0] = maxBound;}
		if(y2[0] <-maxBound){y2[0] = -maxBound;}
	
	}
	else{
	    x[i]=i*lineLength/(x.length-1);
	    //Compute Gamma at xpos
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
	    //Compute Zin
	    
		Complex Ztmp = Complex.Divide(Complex.Divide(
			    Complex.Subtract(one,Gammain),Complex.Add(one,Gammain)),Z0);
		
		y1[i]=Ztmp.Real();
		y2[i]=Ztmp.Imaginary();
		
		
		if(y1[i] < minBound){y1[i] = 0.0;}
		if(y1[i] > maxBound){y1[i] = maxBound;}
		if(y2[i] > maxBound){y2[i] = maxBound;}
		if(y2[i] <-maxBound){y2[i] = -maxBound;}
		
	    
	    }
	}
    }
    
    public void ScanVoltage(){
	pre_ignition();
	ignition();
	Complex one = new Complex(1.0,0.0);
	for(int i=0; i<x.length;i++)
	{
	    x[i] = i * lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
		if(IsTotal){
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					    Complex.Add(one,Gammain));   
                }
                else if(IsIncident){
                    Voltage[i]=Complex.Multiply(VPlus,tmp1);  
                }
                else if(IsReflected){
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),Gammain);  
                }
                y1[i]=Voltage[i].Real();
		y2[i]=Voltage[i].Imaginary();
		
	}
    }
    
    public void ScanCurrent(){
	pre_ignition();
	ignition();
	for(int i=0; i<x.length;i++)
	{
		x[i] = i * lineLength/(x.length-1);
		Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
                if(IsTotal){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					Complex.Subtract(one,Gammain)),Z0);     
                }
                else if(IsIncident){
                    Current[i]=Complex.Divide(Complex.Multiply(VPlus,tmp1),Z0); 
                }
                else if(IsReflected){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					Complex.Subtract(new Complex(0.0,0.0),Gammain)),Z0);
                }
                
		y1[i]=Current[i].Real();
		y2[i]=Current[i].Imaginary();
	}
    }
    
    public void ScanPower(){
	pre_ignition();
	ignition();
	Complex one = new Complex(1.0,0.0);
	for(int i=0; i<x.length;i++)
	{
	    
		x[i] = i * lineLength/(x.length-1);
		
		Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
		Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
							   Complex.Subtract(one,Gammain)),Z0);   
		
		Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					    Complex.Add(one,Gammain)); 	    		    
		
		y1[i]=0.5 * Complex.Real(Complex.Multiply(Voltage[i],Complex.Conjugate(Current[i])));
	    
		//y2[i] = Math.pow(Complex.Magnitude(Gammain),2);
		y2[i] = Complex.Magnitude(Gammain);
                
	}
    }
    
    public void ScanVoltageCurrent(){
	pre_ignition();
	ignition();
	Complex one = new Complex(1.0,0.0);
	for(int i=0; i<x.length;i++)
	{
	    x[i] = i * lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
		Complex tmp1, tmp;
		tmp = new Complex(alpha,beta);
		tmp.Multiply(x[i]);
		tmp1=Complex.Exp(tmp);
		                
                 if(IsTotal){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(one,Gammain)),Z0);   
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),
                                                Complex.Add(one,Gammain));     
                }
                else if(IsIncident){
                    Current[i]=Complex.Divide(Complex.Multiply(VPlus,tmp1),Z0); 
                    Voltage[i]=Complex.Multiply(VPlus,tmp1); 
                }
                else if(IsReflected){
                    Current[i]=Complex.Divide(Complex.Multiply(Complex.Multiply(VPlus,tmp1),
					      Complex.Subtract(new Complex(0.0,0.0),Gammain)),Z0);     
                    Voltage[i]=Complex.Multiply(Complex.Multiply(VPlus,tmp1),Gammain);
                }
		
		//if(IsLowLoss && alpha == 0.0 &&(Complex.Real(ZL)==Complex.Real(Z0) && Complex.Imaginary(ZL)==0.0)){
		//    y1[i] = Complex.Magnitude(VPlus);
		//    y2[i] = Complex.Magnitude(VPlus)/Complex.Real(Z0);
                //    y3[i] = Complex.Magnitude(VPlus);
		//    y4[i] = Complex.Magnitude(VPlus)/Complex.Real(Z0);
		//}
		//else
                {
		    y1[i] = Voltage[i].Magnitude();
		    y2[i] = Current[i].Magnitude();
		}
		
	}
    }
    
    public void ScanReflectionCoefficient(){
	pre_ignition();
	ignition();
	for(int i = 0; i < x.length; i++){
	    x[i]=i*lineLength/(x.length-1);
	    Gammain = EMF.computeGammaAt(GammaL,alpha,x[i],wavelength);
	    y1[i] = Gammain.Real();
	    y2[i] = Gammain.Imaginary();
	}
    }
    public double getYRangeMax(int i){//arg=1 for Voltage Max
	Complex VPlus = EMF.computeVPlus(generator,GammaL,Z0,lineLength,alpha,wavelength);
	double tmp= 0.0;
	switch(i){
	    case 1://Voltage
		tmp = 2.0*VPlus.Magnitude()*Math.exp(alpha*lineLength);
		break;
	    case 2://Current
		tmp = 2.0*VPlus.Magnitude()*Math.exp(alpha*lineLength)/Complex.Magnitude(Z0);
		break;
	    case 3://power
		tmp = VPlus.Magnitude2()*Math.exp(2*alpha*lineLength)/Complex.Magnitude(Z0);
                //tmp = tmp+tmp*Complex.Magnitude2(EMF.computeGammaAt(GammaL,alpha,lineLength,wavelength));
		break;
	    case 4:
		if(Power_input <= 0.0){
		    tmp =0.1;
		}
		else{
		tmp = Power_input;
		}
		break;
	    
	}
	return tmp;
    }
    
    
    /*public double getYRangeMax(int a){
	return 1.0;
    }*/
    
    public void setYRangeMax(){
    
    }
    
  
    public synchronized void increment(){
	ctime+=dtime;
	if(ctime>NTime){ ctime -= NTime; }
    }
    
    public synchronized void reset(){
	ctime=0;
    }
    
    public void ScanTimeDependentVoltage(){
	timeFactor.setReal(Math.cos(2.0*Math.PI*ctime/NTime));
	timeFactor.setImaginary(Math.sin(2.0*Math.PI*ctime/NTime));
	ScanVoltage();
	for(int i = 0; i < x.length; i++){
	    Voltage[i].Multiply(timeFactor);
	    y1T[i] = Voltage[i].Real();
	    y2T[i] = 0.0;
	}	
    }
    
    public void ScanTimeDependentCurrent(){
	timeFactor.setReal(Math.cos(2.0*Math.PI*ctime/NTime));
	timeFactor.setImaginary(Math.sin(2.0*Math.PI*ctime/NTime));
	ScanCurrent();
	for(int i = 0; i < NPoints; i++){
	    Current[i].Multiply(timeFactor);
	    y1T[i] = Current[i].Real();
	    y2T[i] = 0.0;
	}	
    }
    
    public void ScanRandom(){
    
    }
    
    
    public void ScanTimeDependentPower(){
	timeFactor.setReal(Math.cos(2.0*Math.PI*ctime/NTime));
	timeFactor.setImaginary(Math.sin(2.0*Math.PI*ctime/NTime));
	ScanVoltageCurrent();
	for(int i = 0; i < NPoints; i++){
            Voltage[i].Multiply(timeFactor);
	    Current[i].Multiply(timeFactor);
	    Power[i] = Complex.Multiply(Voltage[i],Current[i]);
	    //y1[i] = Power[i].Real();
	    y1T[i] = Complex.Real(Voltage[i])*Complex.Real(Current[i]);
	    y2T[i] = 0.0;
      	}	
    }
    
   
    
}/* End of State */
