//Trans_State.java
//Transmission line with three stubs.
import java.util.*;

public class Trans_State{
    public double xpos, xpos_meters;
    //public double lineLength;
    public double lineLength, lineLength_meters, location1, location2, location3, lineLength_part1, lineLength_part2;
    public int linecounter1 = 1000, linecounter2 = 0;
    public int PlotTimeScale;
    public int plotFlag;
    public int ShuntPlace;
    
    public double frequency_ref, frequency, f_ratio;
    public double epsilon_r,mu_r;
    public double lineZ0;
    public double ZIMP[], eps_r[], lambda[], lambda_ref[];
    public double Section_length[], Section_length_ref[], Section_length_meters[];
    public double locations[], locations_ref[], locations_meters[];
    //Vin and Iin are voltage and current at the cursor
    public Complex Zin, Yin, Gammain, GammaL, Gammainput, Vin, Iin, VPlus, VPmax;
    public double VSWR, VSWRin, wavelength, wavelength_new, ZIMPmin, targetVSWR, targetGamma;
    public double Transformer_meters;
    public Complex ZL, ZL_ref, maximum1;
    public Complex ZS, ZS_ref, YS, YS_ref;
    public Complex YL, YL_ref, maximum2;
    public Complex ZGref, YGref;
    public Generator generator;
    public double Pw[], Ptest;
    public Stub stub[];
    public boolean is_Load_Ztype, is_Shunt_Ztype, zoom_f, LockCursor;
    
    //flags to remember which plot was on
    public boolean WasVoltage, WasCurrent, WasPower;
    public boolean WasSWP, WasVoltPhasor, WasCurPhasor, WasZ, WasY, WasGamma;
    //Plot Parameters
    Complex Voltage[], Current[], Impedance[], Admittance[];
    Complex RefCoef[], Power[];
    double x[];
    double y1[];
    double y2[];
    
    // Voltage envelope
    double yV1[], yV2[];
    
    public static final int NPoints=1000;
    public int NTime, ctime, dtime;
    Complex timeFactor; //jwt is the time factor.
    Complex Gamma0, Gamma1, Gamma2, Gamma3, Gamma4, Gamma5, Gamma6, Gamma7, Gamma8, Gamma9; //Reflection Coefficients at interfaces
    Complex Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9; //Admittances at each interface
    Complex Zinput, Yinput; //Impedance and Admittance at the input terminals 
    public double ZBoundHigh, ZBoundLow;
    int SPos0, SPos1, SPos2, SPos3, SPos4; //Integer representation of the position of the three stubs
    int SPos5, SPos6, SPos7, SPos8, SPos9; //Integer representation of the position of the three stubs
    
    public int iarray[];
    Complex  VPP[]; 
    private static final Complex one = new Complex(1.0,0.0);
    //private static final double epsilon0 = 8.8541878176E-12; // Units: F/m  exact value
    private static final double epsilon0 = 8.841941286E-12; //8.8541878176E-12; //Units: F/m  Approximate value
    private static final double mu0 = 1.25663706144E-6; //Units H/m
    private static final double light_velocity = Math.sqrt(1.0/(epsilon0*mu0)); //  Units m/s
    public boolean IsLoadShort, IsLoadOpen, IsLoadRegular, IsLoadImaginary;
    public boolean IsIncident, IsReflected, IsTotal, IsTcpVisible, IwantTraceOn, TraceWasOn;
    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 Trans_State(){ //main constructor
	targetVSWR = 1.2;
        targetGamma = 0.09090909;
	frequency_ref=3.0E9; // reference frequency
        frequency = 3.0E9; // operating frequency
        f_ratio = frequency / frequency_ref;
        zoom_f = false;
        
        LockCursor = false;
        
        WasVoltage = false;
        WasCurrent = false;
        WasPower = false;
        
        WasSWP = false;
        WasVoltPhasor = false;
        WasCurPhasor = false;
        WasZ = false;
        WasY = false;
        WasGamma = false;
        
	epsilon_r=1.0;
	mu_r=1.0;
        wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency_ref);
        wavelength_new = wavelength;
        Transformer_meters = wavelength * 0.25;
        ShuntPlace = 0;
        
        // Characteristic impedances of the 10 Line Sections
        ZIMP = new double[10];
        
        ZIMP[0]=50.0;
        ZIMP[1]=100.0;
	ZIMP[2]=50.0;
        ZIMP[3]=150.0;
        ZIMP[4]=150.0;
        ZIMP[5]=150.0;
	ZIMP[6]=150.0;
        ZIMP[7]=150.0;
        ZIMP[8]=150.0;
        ZIMP[9]=150.0;
        
        // permittivities of the 10 Line Sections
        eps_r = new double[10];
        
        eps_r[0]=1.0;
        eps_r[1]=1.0;
	eps_r[2]=1.0;
        eps_r[3]=1.0;
        eps_r[4]=1.0;
        eps_r[5]=1.0;
	eps_r[6]=1.0;
        eps_r[7]=1.0;
        eps_r[8]=1.0;
        eps_r[9]=1.0;
        
        lambda = new double[10];
        lambda_ref = new double[10]; // reference wavelengths (at the reference frequency)
        
        lambda[0]=wavelength * f_ratio/ Math.sqrt(eps_r[0]);
        lambda[1]=wavelength * f_ratio / Math.sqrt(eps_r[1]);
        lambda[2]=wavelength * f_ratio / Math.sqrt(eps_r[2]);
        lambda[3]=wavelength * f_ratio/ Math.sqrt(eps_r[3]);
        lambda[4]=wavelength * f_ratio / Math.sqrt(eps_r[4]);
        lambda[5]=wavelength * f_ratio / Math.sqrt(eps_r[5]);
        lambda[6]=wavelength * f_ratio / Math.sqrt(eps_r[6]);
        lambda[7]=wavelength * f_ratio / Math.sqrt(eps_r[7]);
        lambda[8]=wavelength * f_ratio / Math.sqrt(eps_r[8]);
        lambda[9]=wavelength * f_ratio / Math.sqrt(eps_r[9]);
        
        lambda_ref[0]=wavelength / Math.sqrt(eps_r[0]);
        lambda_ref[1]=wavelength / Math.sqrt(eps_r[1]);
        lambda_ref[2]=wavelength / Math.sqrt(eps_r[2]);
        lambda_ref[3]=wavelength / Math.sqrt(eps_r[3]);
        lambda_ref[4]=wavelength / Math.sqrt(eps_r[4]);
        lambda_ref[5]=wavelength / Math.sqrt(eps_r[5]);
        lambda_ref[6]=wavelength / Math.sqrt(eps_r[6]);
        lambda_ref[7]=wavelength / Math.sqrt(eps_r[7]);
        lambda_ref[8]=wavelength / Math.sqrt(eps_r[8]);
        lambda_ref[9]=wavelength / Math.sqrt(eps_r[9]);
        
        
        // Length in wavelength of the 10 Line Sections at the reference frequency
        Section_length = new double[10];
        Section_length_ref = new double[10];
        Section_length_meters = new double[10];
        
        Section_length[0] = 0.125 * f_ratio;
        Section_length[1] = 0.25 * f_ratio;
        Section_length[2] = 1.1250 * f_ratio;
        Section_length[3] = 0.0 * f_ratio;
        Section_length[4] = 0.0 * f_ratio;
        Section_length[5] = 0.0 * f_ratio;
        Section_length[6] = 0.0 * f_ratio;
        Section_length[7] = 0.0 * f_ratio;
        Section_length[8] = 0.0 * f_ratio;
        Section_length[9] = 0.0 * f_ratio;
        
        Section_length_ref[0] = 0.125;
        Section_length_ref[1] = 0.25;
        Section_length_ref[2] = 1.125;
        Section_length_ref[3] = 0.0;
        Section_length_ref[4] = 0.0;
        Section_length_ref[5] = 0.0;
        Section_length_ref[6] = 0.0;
        Section_length_ref[7] = 0.0;
        Section_length_ref[8] = 0.0;
        Section_length_ref[9] = 0.0;
        
        Section_length_meters[0] = Section_length_ref[0] * wavelength / Math.sqrt(eps_r[0]);
        Section_length_meters[1] = Section_length_ref[1] * wavelength / Math.sqrt(eps_r[1]);
        Section_length_meters[2] = Section_length_ref[2] * wavelength / Math.sqrt(eps_r[2]);
        Section_length_meters[3] = Section_length_ref[3] * wavelength / Math.sqrt(eps_r[3]);
        Section_length_meters[4] = Section_length_ref[4] * wavelength / Math.sqrt(eps_r[4]);
        Section_length_meters[5] = Section_length_ref[5] * wavelength / Math.sqrt(eps_r[5]);
        Section_length_meters[6] = Section_length_ref[6] * wavelength / Math.sqrt(eps_r[6]);
        Section_length_meters[7] = Section_length_ref[7] * wavelength / Math.sqrt(eps_r[7]);
        Section_length_meters[8] = Section_length_ref[8] * wavelength / Math.sqrt(eps_r[8]);
        Section_length_meters[9] = Section_length_ref[9] * wavelength / Math.sqrt(eps_r[9]);
        
        
        locations = new double[10];
        locations_ref = new double[10]; // at the central frequency - reference
        locations_meters = new double[10];
        
        lineLength_meters = Section_length_meters[0];
        
        // in the beginning, locations_ref same as locations (same frequency)
        locations[0] = Section_length[0];
        locations_ref[0] = locations[0];
        locations_meters[0] = Section_length_meters[0];
        for(int i = 1; i < 10; i++){
            locations[i] = locations[i-1] + Section_length[i];
            locations_ref[i] = locations[i];
            locations_meters[i] = locations_meters[i-1] + Section_length_meters[i];
            lineLength_meters = lineLength_meters + Section_length_meters[i];
        }
         
        lineLength = locations[9];
        
        //location1 = 0.25*lineLength;
        //location2 = 0.5*lineLength;
        //location3 = 0.75*lineLength;
        
        PlotTimeScale = 2;
        plotFlag = 0;
        
	lineLength_part1 = 1.5;
	lineLength_part2 = 0.0;     
        lineLength = lineLength_part1 + lineLength_part2;
        xpos = lineLength;
        //System.out.println("L = "+lineLength);
        
	lineZ0 = ZIMP[0];
	ZL = new Complex(100.0,0.0);
	YL = new Complex(0.01,0.0);
        ZL_ref = ZL;
        YL_ref = YL;
        
        ZS = new Complex(100.0,100.0);
        YS = new Complex(0.01,-0.01);
        ZS_ref = ZS;
        YS_ref = YS;
        
        ZBoundHigh = 100.0;
        ZBoundLow = 25.0;
        
	generator = new Generator();
        
        ZGref = generator.getZg();
        //System.out.println(ZGref);
        
        stub = new Stub[10];
	//public Stub(double length, double position, double Zchar, boolean isParallel, boolean isOpen, boolean isEnable)
	stub[0] = new Stub(0.125,0.0,ZIMP[0],true,false,false);
	stub[1] = new Stub(0.25,0.0,ZIMP[1],true,false,false);
	stub[2] = new Stub(1.1250,0.0,ZIMP[2],true,false,false);
	stub[3] = new Stub(0.0,0.0,ZIMP[3],true,false,false);
	stub[4] = new Stub(0.0,0.0,ZIMP[4],true,false,false);
	stub[5] = new Stub(0.0,0.0,ZIMP[5],true,false,false);
	stub[6] = new Stub(0.0,0.0,ZIMP[6],true,false,false);
	stub[7] = new Stub(0.0,0.0,ZIMP[7],true,false,false);
	stub[8] = new Stub(0.0,0.0,ZIMP[8],true,false,false);
	stub[9] = new Stub(0.0,0.0,ZIMP[9],true,false,false);
        
	wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency);
	 
        IsLoadRegular = true;
        IsLoadShort = false;
        IsLoadOpen = false;
        IsLoadImaginary = false;
        
        IsIncident = false;
        IsReflected = false;
        IsTotal =true;
        IsTcpVisible = true;
	IwantTraceOn = false;
        TraceWasOn = false;
        
	Pw = new double[3];
	is_Load_Ztype = true;
        is_Shunt_Ztype = true;
	
	//Zin = EMF.computeZinAt(ZL, lineZ0, 0.0, false);
	//Yin = EMF.computeYinAt(YL, lineZ0, 0.0, false);
	
        Zin = EMF.computeZinAt(ZL, ZIMP[0], 0.0, false);
	Yin = EMF.computeYinAt(YL, ZIMP[0], 0.0, false);
	
        //GammaL = EMF.computeGamma(ZL, lineZ0);
	GammaL = EMF.computeGamma(ZL, ZIMP[0]);
	
        Gammain = EMF.computeGammaAt(GammaL, 0.0);
	
        //VPlus = EMF.computeVPlus(generator,ZL,lineZ0,lineLength);
	//Vin = EMF.computeVat(ZL,lineZ0,VPlus,xpos);
	//Iin = EMF.computeIat(ZL,lineZ0,VPlus,xpos);
	VPlus = EMF.computeVPlus(generator,ZL,ZIMP[0],lineLength);
	Vin = EMF.computeVat(ZL,ZIMP[0],VPlus,xpos);
	Iin = EMF.computeIat(ZL,ZIMP[0],VPlus,xpos);
        
        VSWR = EMF.computeSWR(GammaL);
        VSWRin = EMF.computeSWR(Gammain);
        //VSWRin = (1.0+Complex.Magnitude(Gammain))/(1-Complex.Magnitude(Gammain));
        
	//Pw = EMF.PowerDelivered(generator,VPlus,ZL,0.0,lineZ0,lineLength);
	Pw = EMF.PowerDelivered(generator,VPlus,ZL,0.0,ZIMP[0],lineLength);
	//Plot Parameters Initialization
	Voltage = new Complex[NPoints];
	Current = new Complex[NPoints];
	Impedance = new Complex[NPoints];
	Admittance = new Complex[NPoints];
        
	RefCoef = new Complex[NPoints];
	Power = new Complex[NPoints];
	x = new double[NPoints];
	y1 = new double[NPoints];
	y2 = new double[NPoints];
	
        // Voltage envelope for time-dependent plot
        yV1 = new double[NPoints];
        yV2 = new double[NPoints];
	
	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));
	iarray = new int[10];
	VPP = new Complex[10];
                
	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();
    }
    
    public synchronized void ignition(){
        // Lines other than transformer have same characteristics
        double Ztemp1, Ztemp2;
        //System.out.println("VSWR target = "+targetVSWR);
        ZIMP[2] = ZIMP[0];
        eps_r[2] = eps_r[0];
        // CHANGE - IMPOSE SAME epsilon IN ALL LINES
        //eps_r[1] = eps_r[0];
        
        //System.out.println(eps_r[0]+"   "+eps_r[1]+"   "+eps_r[2]);
        
        targetGamma = (targetVSWR -1)/(targetVSWR + 1);
        
        f_ratio = frequency / frequency_ref;
	wavelength = light_velocity/(Math.sqrt(epsilon_r*mu_r)*frequency_ref); //at the refernce frequency
	wavelength_new = wavelength / f_ratio; 
        
        lambda[0]=wavelength_new / Math.sqrt(eps_r[0]);
        lambda[1]=wavelength_new / Math.sqrt(eps_r[1]);
        lambda[2]=wavelength_new / Math.sqrt(eps_r[2]);
        lambda[3]=wavelength_new / Math.sqrt(eps_r[3]);
        lambda[4]=wavelength_new / Math.sqrt(eps_r[4]);
        lambda[5]=wavelength_new / Math.sqrt(eps_r[5]);
        lambda[6]=wavelength_new / Math.sqrt(eps_r[6]);
        lambda[7]=wavelength_new / Math.sqrt(eps_r[7]);
        lambda[8]=wavelength_new / Math.sqrt(eps_r[8]);
        lambda[9]=wavelength_new / Math.sqrt(eps_r[9]);
        
        lambda_ref[0]=wavelength / Math.sqrt(eps_r[0]);
        lambda_ref[1]=wavelength / Math.sqrt(eps_r[1]);
        lambda_ref[2]=wavelength / Math.sqrt(eps_r[2]);
        lambda_ref[3]=wavelength / Math.sqrt(eps_r[3]);
        lambda_ref[4]=wavelength / Math.sqrt(eps_r[4]);
        lambda_ref[5]=wavelength / Math.sqrt(eps_r[5]);
        lambda_ref[6]=wavelength / Math.sqrt(eps_r[6]);
        lambda_ref[7]=wavelength / Math.sqrt(eps_r[7]);
        lambda_ref[8]=wavelength / Math.sqrt(eps_r[8]);
        lambda_ref[9]=wavelength / Math.sqrt(eps_r[9]);
        
        Section_length_ref[2] = 1.25 - Section_length_ref[0];
        Transformer_meters = lambda_ref[1] * 0.25;
        
        Section_length[0] = Section_length_ref[0] * f_ratio;
        Section_length[1] = Section_length_ref[1] * f_ratio;
        Section_length[2] = Section_length_ref[2] * f_ratio;
        Section_length[3] = Section_length_ref[3] * f_ratio;
        Section_length[4] = Section_length_ref[4] * f_ratio;
        Section_length[5] = Section_length_ref[5] * f_ratio;
        Section_length[6] = Section_length_ref[6] * f_ratio;
        Section_length[7] = Section_length_ref[7] * f_ratio;
        Section_length[8] = Section_length_ref[8] * f_ratio;
        Section_length[9] = Section_length_ref[9] * f_ratio;
        
        Section_length_meters[0] = Section_length_ref[0] * lambda_ref[0];
        Section_length_meters[1] = Section_length_ref[1] * lambda_ref[1];
        Section_length_meters[2] = Section_length_ref[2] * lambda_ref[2];
        Section_length_meters[3] = Section_length_ref[3] * lambda_ref[3];
        Section_length_meters[4] = Section_length_ref[4] * lambda_ref[4];
        Section_length_meters[5] = Section_length_ref[5] * lambda_ref[5];
        Section_length_meters[6] = Section_length_ref[6] * lambda_ref[6];
        Section_length_meters[7] = Section_length_ref[7] * lambda_ref[7]; 
        Section_length_meters[8] = Section_length_ref[8] * lambda_ref[8];
        Section_length_meters[9] = Section_length_ref[9] * lambda_ref[9];
        
        //System.out.println(Section_length_meters[0]+"   "+
        //                   Section_length_meters[1]+"   "+
        //                   Section_length_meters[2]);
                
        locations[0] = Section_length[0];
        locations_ref[0] = Section_length_ref[0];
        locations_meters[0] = Section_length_meters[0];
        //lineLength_meters = 0.0;
        
        for(int i = 1; i < 10; i++){
            locations[i] = locations[i-1] + Section_length[i];
            locations_ref[i] = locations_ref[i-1] + Section_length_ref[i];
            locations_meters[i] = locations_meters[i-1] + Section_length_meters[i];
        }
         
        lineLength = locations[9];
        
        lineLength_meters = 0.0;
        for(int i = 0; i < 10; i++){
            lineLength_meters = lineLength_meters + Section_length_meters[i];
        }
        
        if(LockCursor){
            xpos = locations[1];
        }
        
        if(xpos <= locations[0] ){
            xpos_meters = xpos * lambda[0];
        }
        else if(xpos > locations[0] && xpos <= locations[1] ){
            xpos_meters = locations_meters[0] + 
                         (xpos - locations[0]) * lambda[1];
        }
        else if(xpos > locations[1] && xpos <= locations[2] ){
            xpos_meters = locations_meters[1] + 
                         (xpos - locations[1]) * lambda[2];
        }
        else if(xpos > locations[2] && xpos <= locations[3] ){
            xpos_meters = locations_meters[2] + 
                         (xpos - locations[2]) * lambda[3];
        }
        else if(xpos > locations[3] && xpos <= locations[4] ){
            xpos_meters = locations_meters[3] + 
                         (xpos - locations[3]) * lambda[4];
        }
        else if(xpos > locations[4] && xpos <= locations[5] ){
            xpos_meters = locations_meters[4] + 
                         (xpos - locations[4]) * lambda[5];
        }
        else if(xpos > locations[5] && xpos <= locations[6] ){
            xpos_meters = locations_meters[5] + 
                         (xpos - locations[5]) * lambda[6];
        }
        else if(xpos > locations[6] && xpos <= locations[7] ){
            xpos_meters = locations_meters[6] + 
                         (xpos - locations[6]) * lambda[7];
        }
        else if(xpos > locations[7] && xpos <= locations[8] ){
            xpos_meters = locations_meters[7] + 
                         (xpos - locations[7]) * lambda[8];
        }
        else if(xpos > locations[8] && xpos <= locations[9] ){
            xpos_meters = locations_meters[8] + 
                         (xpos - locations[8]) * lambda[9];
        }
        
        Ztemp1 = ZIMP[0] * (1.0 + Complex.Magnitude(GammaL))/(1.0 - Complex.Magnitude(GammaL));
        Ztemp2 = ZIMP[0] * (1.0 - Complex.Magnitude(GammaL))/(1.0 + Complex.Magnitude(GammaL));
        
        ZBoundHigh = Math.max(Ztemp1, Ztemp2);
        ZBoundLow = Math.min(Ztemp1, Ztemp2);
        
        //System.out.println("ZHIGH = "+ZBoundHigh+"   ZLOW = "+ZBoundLow);
        // Load - imaginary part for frequency different from reference 
        double rea_ref = 0.0;
        double img_ref = 0.0;
        double rea = 0.0;
        double img = 0.0;
        if(is_Load_Ztype){
            rea_ref = Complex.Real(ZL_ref);
            img_ref = Complex.Imaginary(ZL_ref);

            if(frequency != frequency_ref){
                rea = rea_ref;
                if(img_ref == 0.0){
                    img = 0.0;
                }
                else if(img_ref > 0.0){
                    img = img_ref * f_ratio;
                }
                else{
                    img = img_ref / f_ratio;
                }
                ZL = new Complex(rea,img);
            }
            else{
                ZL = ZL_ref;
            }
        }
        else{
            rea_ref = Complex. Real(YL_ref);
            img_ref = Complex.Imaginary(YL_ref);

            if(frequency != frequency_ref){
                rea = rea_ref;
                if(img_ref == 0.0){
                    img = 0.0;
                }
                else if(img_ref > 0.0){
                    img = img_ref * f_ratio;
                }
                else{
                    img = img_ref / f_ratio;
                }
                YL = new Complex(rea,img);
            }
            else{ YL = YL_ref;}
        }

        
        // SHUNT ELEMENT - set for frequency change
        double reaS_ref = 0.0;
        double imgS_ref = 0.0;
        double reaS = 0.0;
        double imgS = 0.0;
        if(is_Load_Ztype){
            reaS_ref = Complex.Real(ZS_ref);
            imgS_ref = Complex.Imaginary(ZS_ref);

            if(frequency != frequency_ref){
                reaS = reaS_ref;
                if(imgS_ref == 0.0){
                    imgS = 0.0;
                }
                else if(imgS_ref > 0.0){
                    imgS = imgS_ref * f_ratio;
                }
                else{
                    imgS = imgS_ref / f_ratio;
                }
                ZS = new Complex(reaS,imgS);
            }
            else{
                ZS = ZS_ref;
            }
        }
        else{
            reaS_ref = Complex. Real(YS_ref);
            imgS_ref = Complex.Imaginary(YS_ref);

            if(frequency != frequency_ref){
                reaS = reaS_ref;
                if(imgS_ref == 0.0){
                    imgS = 0.0;
                }
                else if(imgS_ref > 0.0){
                    imgS = imgS_ref * f_ratio;
                }
                else{
                    imgS = imgS_ref / f_ratio;
                }
                YS = new Complex(reaS,imgS);
            }
            else{ YS = YS_ref;}
        }
        
        //---------------------------------------------
        
         
         if(is_Load_Ztype){
	    
		//GammaL = EMF.computeGamma(ZL,lineZ0,true);
		GammaL = EMF.computeGamma(ZL,ZIMP[0],true);
		
                if(Complex.Real(ZL) ==0.0 && Complex.Imaginary(ZL)!=0.0){
		    YL = new Complex(0.0, - 1.0/Complex.Imaginary(ZL));
		}
		else{
		    //YL =  EMF.computeYinAt(GammaL,lineZ0,0.0,true);
                    YL =  EMF.computeYinAt(GammaL,ZIMP[0],0.0,true);
		}
                
                if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
                    IsLoadShort = true;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = 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;
                }
                else if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = 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;
                }
	}
	else{
		//GammaL = EMF.computeGamma(YL,lineZ0,false);
		GammaL = EMF.computeGamma(YL,ZIMP[0],false);
		
                if(Complex.Real(YL) ==0.0 && Complex.Imaginary(YL)!=0.0){
		    ZL = new Complex(0.0, - 1.0/Complex.Imaginary(YL));
		}
		else{
		    //ZL = EMF.computeZinAt(GammaL,lineZ0,0.0,true);
                    ZL = EMF.computeZinAt(GammaL,ZIMP[0],0.0,true);
		}
                
                if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)==0.0){
                    IsLoadShort = false;
                    IsLoadOpen = true;
                    IsLoadImaginary = false;
                    IsLoadRegular = 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;
                }
                else if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = 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(is_Shunt_Ztype){
	    
		if(Complex.Real(ZS) ==0.0 && Complex.Imaginary(ZS)!=0.0){
		    YS = new Complex(0.0, - 1.0/Complex.Imaginary(ZS));
		}
		else if(Complex.Real(ZS) ==0.0 && Complex.Imaginary(ZS)==0.0){
                    YS = new Complex(0.0, - 1.0E130);
                }
                else{
                    YS =  Complex.Divide(one,ZS);
		}
	}
	else{
		if(Complex.Real(YS) ==0.0 && Complex.Imaginary(YS)!=0.0){
		    ZS = new Complex(0.0, - 1.0/Complex.Imaginary(YS));
		}
                else if(Complex.Real(YS) ==0.0 && Complex.Imaginary(YS)==0.0){
                    ZS = new Complex(0.0, - 1.0E130);
                }
		else{
		    ZS = Complex.Divide(one,YS);
		}             
   	}
        
        
        {
            VSWR = EMF.computeSWR(GammaL);
            
            IsTcpVisible = false;   
	    
	    //Y0 is admittance at first interface
	    Y0 = EMF.computeYinAt(YL,ZIMP[0],locations[0],false);
	    if(ShuntPlace == 10){Y0 = Complex.Add(Y0, YS);}
            Gamma0 = EMF.computeGamma(Y0,ZIMP[1],false);
	    
	    //Y1 is admittance at second interface
	    Y1 = EMF.computeYinAt(Y0,ZIMP[1],locations[1]-locations[0],false);
	    if(ShuntPlace == 9){Y1 = Complex.Add(Y1, YS);}
            Gamma1 = EMF.computeGamma(Y1,ZIMP[2],false);
	    
	    //Y2 is admittance at third interface
	    Y2 = EMF.computeYinAt(Y1,ZIMP[2],locations[2]-locations[1],false);
	    //Complex testcomplex = new Complex(100.0,100.0);
            //Y2 = Complex.Add(Y2,Complex.Divide(one,testcomplex));
            if(ShuntPlace == 8){Y2 = Complex.Add(Y2, YS);}
            Gamma2 = EMF.computeGamma(Y2,ZIMP[3],false);
	    
            //Y3 is admittance at fourth interface
	    Y3 = EMF.computeYinAt(Y2,ZIMP[3],locations[3]-locations[2],false);
	    if(ShuntPlace == 7){Y3 = Complex.Add(Y3, YS);}
            Gamma3 = EMF.computeGamma(Y3,ZIMP[4],false);
            
            //Y4 is admittance at fifth interface
	    Y4 = EMF.computeYinAt(Y3,ZIMP[4],locations[4]-locations[3],false);
	    if(ShuntPlace == 6){Y4 = Complex.Add(Y4, YS);}
            Gamma4 = EMF.computeGamma(Y4,ZIMP[5],false);
            
            //Y5 is admittance at sixth interface
	    Y5 = EMF.computeYinAt(Y4,ZIMP[5],locations[5]-locations[4],false);
	    if(ShuntPlace == 5){Y5 = Complex.Add(Y5, YS);}
            Gamma5 = EMF.computeGamma(Y5,ZIMP[6],false);
            
	    //Y6 is admittance at seventh interface
	    Y6 = EMF.computeYinAt(Y5,ZIMP[6],locations[6]-locations[5],false);
	    if(ShuntPlace == 4){Y6 = Complex.Add(Y6, YS);}
            Gamma6 = EMF.computeGamma(Y6,ZIMP[7],false);
            
            //Y7 is admittance at eighth interface
	    Y7 = EMF.computeYinAt(Y6,ZIMP[7],locations[7]-locations[6],false);
	    if(ShuntPlace == 3){Y7 = Complex.Add(Y7, YS);}
            Gamma7 = EMF.computeGamma(Y7,ZIMP[8],false);
            
            //Y8 is admittance at nineth interface
	    Y8 = EMF.computeYinAt(Y7,ZIMP[8],locations[8]-locations[7],false);
	    if(ShuntPlace == 2){Y8 = Complex.Add(Y8, YS);}
            Gamma8 = EMF.computeGamma(Y8,ZIMP[9],false);
            
            //Y9 is admittance at tenth interface (input)
	    Y9 = EMF.computeYinAt(Y8,ZIMP[9],locations[9]-locations[8],false);
	    if(ShuntPlace == 1){Y9 = Complex.Add(Y9, YS);}
            Gamma9 = EMF.computeGamma(Y9,ZIMP[9],false);
            
	    SPos0 = (int)((NPoints-1)*locations[0]/lineLength);
	    SPos1 = (int)((NPoints-1)*locations[1]/lineLength);
	    SPos2 = (int)((NPoints-1)*locations[2]/lineLength);
            SPos3 = (int)((NPoints-1)*locations[3]/lineLength);
	    SPos4 = (int)((NPoints-1)*locations[4]/lineLength);
	    SPos5 = (int)((NPoints-1)*locations[5]/lineLength);
            SPos6 = (int)((NPoints-1)*locations[6]/lineLength);
	    SPos7 = (int)((NPoints-1)*locations[7]/lineLength);
	    SPos8 = (int)((NPoints-1)*locations[8]/lineLength);
            SPos9 = (int)((NPoints-1)*locations[9]/lineLength);
            
            //Find impedance and admittance looking from the input terminals of the transmission line
	    if(ShuntPlace == 1){
                Zinput = EMF.computeZinAt(Gamma9,ZIMP[9],0.0,true);
                Yinput = EMF.computeYinAt(Gamma9,ZIMP[9],0.,true);
            }
            else{    
                Zinput = EMF.computeZinAt(Gamma8,ZIMP[9],lineLength-locations[8],true);
                Yinput = EMF.computeYinAt(Gamma8,ZIMP[9],lineLength-locations[8],true);
            }
            
            //Gammainput = EMF.computeGammaAt(Gamma8,lineLength-locations[8]);
	    /*
            Gammainput = Gammain;
            //System.out.println("Zinput = "+Zinput+"   Gammainput = "+Gammain);
            //--------------------------------------------------------------------
            Complex Zat0, Zat1, Zat2, Zat3;
            Zat0 = ZL;
            Zat1 = EMF.computeZinAt(Gamma1,ZIMP[0],locations[0],true);
            Zat2 = EMF.computeZinAt(Gamma2,ZIMP[1],locations[1],true);
            Zat3 = EMF.computeZinAt(Gamma3,ZIMP[2],locations[2],true);
	    //Find VPlus
	    Complex tempc, VPtest;
	    tempc = new Complex(Math.cos(EMF.beta*(lineLength-locations[2])),
				Math.sin(EMF.beta*(lineLength-locations[2])));
            
            
	    VPP[2] = Complex.Divide(EMF.voltageDivider(generator,Zinput),
				Complex.Multiply(tempc,Complex.Add(1.0,Gammain))
		    );
            
            VPP[1] = Complex.Divide(EMF.voltageDivider(generator,Zinput),
				Complex.Multiply(tempc,Complex.Add(1.0,Gammain))
		    );
            
                    
                    
            //--------------------------------------------------------------------
            VPP[8] = Complex.Divide(
			  EMF.computeVat(Gamma8,ZIMP[9],VPlus,0.0,true),
			  EMF.computeVat(Gamma7,ZIMP[8],one,locations[8]-locations[7],true)
		);
            
            //System.out.println("VPlus = "+VPlus+"   Vin = "+VPtest);
	    //Pw = EMF.PowerDelivered(generator,VPlus,Zinput,0.0,lineZ0,0.0);
	    Pw = EMF.PowerDelivered(generator,VPlus,Zinput,0.0,ZIMP[9],0.0);
	    */
            
            
            //Gammainput = Gamma9;
            Gamma2 = Gammain; Gamma3 = Gammain; Gamma4 = Gammain; Gamma5 = Gammain;
            Gamma6 = Gammain; Gamma7 = Gammain; Gamma8 = Gammain; Gamma9 = Gammain;
            
	    //Find VPlus
	    Complex tempc, tempcc;
	    tempc = new Complex(Math.cos(EMF.beta*(lineLength-locations[8])),
				Math.sin(EMF.beta*(lineLength-locations[8]))
                    );
	    VPlus = Complex.Divide(EMF.voltageDivider(generator,Zinput),
				Complex.Multiply(tempc,
				   Complex.Add(1.0,EMF.computeGammaAt(Gamma8,lineLength-locations[8])
				   )
				)
		    );
            
            Complex VPlusPlus;
            tempcc = new Complex(Math.cos(EMF.beta*(lineLength-locations[1])),
				Math.sin(EMF.beta*(lineLength-locations[1])));
	    VPlusPlus = Complex.Divide(EMF.voltageDivider(generator,Zinput),
				Complex.Multiply(tempcc,
				   Complex.Add(1.0,EMF.computeGammaAt(Gamma1,lineLength-locations[1])
				   )
				)
		    );
	    //Pw = EMF.PowerDelivered(generator,VPlus,Zinput,0.0,lineZ0,0.0);
	    Pw = EMF.PowerDelivered(generator,VPlus,Zinput,0.0,ZIMP[9],0.0);
	    
            //System.out.println(Gammain+"  "+Gamma2+"  "+Gamma1);
            //System.out.println(locations[1]+"   "+lineLength+"    "+EMF.voltageDivider(generator,Zinput)+"   "+VPlusPlus+"  "+Gamma1);
            //System.out.println(Complex.Add(1.0,EMF.computeGammaAt(Gamma1,lineLength-locations[1]))+"   "+tempcc+"  "+Math.cos(EMF.beta*lineLength-locations[1]));
            //System.out.println(Math.cos(EMF.beta*(lineLength-locations[1]))+"   "+EMF.beta);
            
            
            //System.out.println("locationA = "+locations[9]);
                VPP[8] = Complex.Divide(
			  EMF.computeVat(Gamma8,ZIMP[9],VPlus,0.0,true),
			  EMF.computeVat(Gamma7,ZIMP[8],one,locations[8]-locations[7],true)
		);
                
                VPP[7] = Complex.Divide(
			  EMF.computeVat(Gamma7,ZIMP[8],VPP[8],0.0,true),
			  EMF.computeVat(Gamma6,ZIMP[7],one,locations[7]-locations[6],true)
		);
                
                VPP[6] = Complex.Divide(
			  EMF.computeVat(Gamma6,ZIMP[7],VPP[7],0.0,true),
			  EMF.computeVat(Gamma5,ZIMP[6],one,locations[6]-locations[5],true)
		);
                
                VPP[5] = Complex.Divide(
			  EMF.computeVat(Gamma5,ZIMP[6],VPP[6],0.0,true),
			  EMF.computeVat(Gamma4,ZIMP[5],one,locations[5]-locations[4],true)
		);
                
                VPP[4] = Complex.Divide(
			  EMF.computeVat(Gamma4,ZIMP[5],VPP[5],0.0,true),
			  EMF.computeVat(Gamma3,ZIMP[4],one,locations[4]-locations[3],true)
		);
                
                VPP[3] = Complex.Divide(
			  EMF.computeVat(Gamma3,ZIMP[4],VPP[4],0.0,true),
			  EMF.computeVat(Gamma2,ZIMP[3],one,locations[3]-locations[2],true)
		);
                
                VPP[2] = Complex.Divide(
			  EMF.computeVat(Gamma2,ZIMP[3],VPP[3],0.0,true),
			  EMF.computeVat(Gamma1,ZIMP[2],one,locations[2]-locations[1],true)
		);
                
		VPP[1] = Complex.Divide(
			  EMF.computeVat(Gamma1,ZIMP[2],VPP[2],0.0,true),
			  EMF.computeVat(Gamma0,ZIMP[1],one,locations[1]-locations[0],true)
		);
	    
		VPP[0] = Complex.Divide(
			  EMF.computeVat(Gamma0,ZIMP[1],VPP[1],0.0,true),
			  EMF.computeVat(GammaL,ZIMP[0],one,locations[0],true)
		);
                
                VPmax = VPlus;
                for(int i=0 ; i<9; i++){
                    if(Complex.Magnitude(VPP[i]) > Complex.Magnitude(VPmax)){VPmax = VPP[i];}
                }
                
                ZIMPmin = ZIMP[0];
                for(int i=1 ; i<9; i++){
                    if(ZIMP[i]< ZIMPmin){ZIMPmin = ZIMP[i];}
                }
                
                
            //if(xpos <= locations[0]){
            if(xpos < locations[0]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(GammaL,ZIMP[0],xpos,true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(GammaL,ZIMP[0],xpos,true); 
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(GammaL,xpos);
		Vin = EMF.computeVat(GammaL,ZIMP[0],VPP[0],xpos,true);
		Iin = EMF.computeIat(GammaL,ZIMP[0],VPP[0],xpos,true);
                lineZ0 = ZIMP[0];
	    }
	    //else if(xpos <= locations[1] && xpos > locations[0]){
            else if(xpos < locations[1] && xpos >= locations[0]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma0,ZIMP[1],xpos-locations[0],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma0,ZIMP[1],xpos-locations[0],true);
		    Yin = EMF.Inv(Zin);
		}
	        Gammain = EMF.computeGammaAt(Gamma0,xpos-locations[0]);
		Vin = EMF.computeVat(Gamma0,ZIMP[1],VPP[1],xpos-locations[0],true);
		Iin = EMF.computeIat(Gamma0,ZIMP[1],VPP[1],xpos-locations[0],true);
                lineZ0 = ZIMP[1];
	    }
	    //else if(xpos <= locations[2] && xpos > locations[1]){
            else if(xpos <= locations[2] && xpos >= locations[1]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma1,ZIMP[2],xpos-locations[1],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma1,ZIMP[2],xpos-locations[1],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma1,xpos-locations[1]);
		Vin = EMF.computeVat(Gamma1,ZIMP[2],VPP[2],xpos-locations[1],true);
		Iin = EMF.computeIat(Gamma1,ZIMP[2],VPP[2],xpos-locations[1],true); 
                lineZ0 = ZIMP[2];
            }
            
            // CONDITIONS BELOW ARE NOT USED - THEREFORE ABOVE xpos <= locations [2] otherwise only <
            //else if(xpos <= locations[3] && xpos > locations[2]){
            else if(xpos < locations[3] && xpos >= locations[2]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma2,ZIMP[3],xpos-locations[2],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma2,ZIMP[3],xpos-locations[2],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma2,xpos-locations[2]);
		Vin = EMF.computeVat(Gamma2,ZIMP[3],VPP[3],xpos-locations[2],true);
		Iin = EMF.computeIat(Gamma2,ZIMP[3],VPP[3],xpos-locations[2],true); 
                lineZ0 = ZIMP[3];
            }
            //else if(xpos <= locations[4] && xpos > locations[3]){
            else if(xpos < locations[4] && xpos >= locations[3]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma3,ZIMP[4],xpos-locations[3],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma3,ZIMP[4],xpos-locations[3],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma3,xpos-locations[3]);
		Vin = EMF.computeVat(Gamma3,ZIMP[4],VPP[4],xpos-locations[3],true);
		Iin = EMF.computeIat(Gamma3,ZIMP[4],VPP[4],xpos-locations[3],true); 
                lineZ0 = ZIMP[4];
            }
            //else if(xpos <= locations[5] && xpos > locations[4]){
            else if(xpos < locations[5] && xpos >= locations[4]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma4,ZIMP[5],xpos-locations[4],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma4,ZIMP[5],xpos-locations[4],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma4,xpos-locations[4]);
		Vin = EMF.computeVat(Gamma4,ZIMP[5],VPP[5],xpos-locations[4],true);
		Iin = EMF.computeIat(Gamma4,ZIMP[5],VPP[5],xpos-locations[4],true); 
                lineZ0 = ZIMP[5];
            }
            //else if(xpos <= locations[6] && xpos > locations[5]){
            else if(xpos < locations[6] && xpos >= locations[5]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma5,ZIMP[6],xpos-locations[5],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma5,ZIMP[6],xpos-locations[5],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma5,xpos-locations[5]);
		Vin = EMF.computeVat(Gamma5,ZIMP[6],VPP[6],xpos-locations[5],true);
		Iin = EMF.computeIat(Gamma5,ZIMP[6],VPP[6],xpos-locations[5],true); 
                lineZ0 = ZIMP[6];
            }
            //else if(xpos <= locations[7] && xpos > locations[6]){
            else if(xpos < locations[7] && xpos >= locations[6]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma6,ZIMP[7],xpos-locations[6],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma6,ZIMP[7],xpos-locations[6],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma6,xpos-locations[6]);
		Vin = EMF.computeVat(Gamma6,ZIMP[7],VPP[7],xpos-locations[6],true);
		Iin = EMF.computeIat(Gamma6,ZIMP[7],VPP[7],xpos-locations[6],true); 
                lineZ0 = ZIMP[7];
            }   
            //else if(xpos <= locations[8] && xpos > locations[7]){
            else if(xpos < locations[8] && xpos >= locations[7]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma7,ZIMP[8],xpos-locations[7],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma7,ZIMP[8],xpos-locations[7],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma7,xpos-locations[7]);
		Vin = EMF.computeVat(Gamma7,ZIMP[8],VPP[8],xpos-locations[7],true);
		Iin = EMF.computeIat(Gamma7,ZIMP[8],VPP[8],xpos-locations[7],true); 
                lineZ0 = ZIMP[8];
            }              
            //else if(xpos <= locations[9] && xpos > locations[8]){
            else if(xpos < locations[9] && xpos >= locations[8]){
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma8,ZIMP[9],xpos-locations[8],true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma8,ZIMP[9],xpos-locations[8],true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma8,xpos-locations[8]);
		Vin = EMF.computeVat(Gamma8,ZIMP[9],VPlus,xpos-locations[8],true);
		Iin = EMF.computeIat(Gamma8,ZIMP[9],VPlus,xpos-locations[8],true); 
                lineZ0 = ZIMP[9];
            }    
            
            VSWRin = EMF.computeSWR(Gammain);
            //VSWRin = (1.0+Complex.Magnitude(Gammain))/(1-Complex.Magnitude(Gammain));
            /*    
            else {
		if(is_Load_Ztype){
		    Yin = EMF.computeYinAt(Gamma2,ZIMP[3],xpos-location3,true);
		    Zin = EMF.Inv(Yin);
		}
		else { 
		    Zin = EMF.computeZinAt(Gamma2,ZIMP[3],xpos-location3,true);
		    Yin = EMF.Inv(Zin);
		}
		Gammain = EMF.computeGammaAt(Gamma2,xpos-location3);
		    Vin = EMF.computeVat(Gamma2,ZIMP[3],VPlus,xpos-location3,true);
		    Iin = EMF.computeIat(Gamma2,ZIMP[3],VPlus,xpos-location3,true); 
                    lineZ0 = ZIMP[3];
            }*/
                
                /*
                int first = 0, second = 0;
                for(int i=0; i < 10; i++){
                    for(int j=0; j < 10; j++){
                        for(int k=0; k < 10; k++){
                            for(int l=0; l < 10; l++){
                                for(int m=0; m < 10; m++){
                                    first = (i*10000 + j*1000 + k * 100 + l * 10 + m) * 4;
                                    second = m*10000 + l*1000 + k * 100 + j * 10 + i;
                                    if(first == second){
                                        System.out.println(second+"   "+first+" "+i+"  "+j+"  "+k+"  "+l+"  "+m);
                                    }
                                }
                            }
                            
                        }
                    }
                }
                */
	}
	
    }
    
    public synchronized void increment(){
	ctime+=dtime;
	if(ctime>NTime){ ctime -= NTime; }
    }
    
    public synchronized void reset(){
	ctime=0;
    }
    
    public void ScanImpedance(){
        
	int i;
	Complex tempC = new Complex(0.0,0.0);
	
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(YL,ZIMP[0],x[i],false);
	}
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y0,ZIMP[1],x[i]-locations[0],false);
            tempC = EMF.Inv(Admittance[i]);
            //System.out.println(i+"  "+x[i]+"  "+Admittance[i]+"  "+tempC.Real()+"   "+tempC.Imaginary());
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y1,ZIMP[2],x[i]-locations[1],false);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y2,ZIMP[3],x[i]-locations[2],false);
	}
        
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y3,ZIMP[4],x[i]-locations[3],false);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y4,ZIMP[5],x[i]-locations[4],false);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y5,ZIMP[6],x[i]-locations[5],false);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y6,ZIMP[7],x[i]-locations[6],false);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y7,ZIMP[8],x[i]-locations[7],false);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y8,ZIMP[9],x[i]-locations[8],false);
	}
        
	for(i = 0; i < NPoints; i++){
	    Impedance[i] = EMF.Inv(Admittance[i]);
	    y1[i] = Impedance[i].Real();
	    y2[i] = Impedance[i].Imaginary();
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
    } 
    
    public void ScanAdmittance(){	
	int i;
		
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(YL,ZIMP[0],x[i],false);
	}
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y0,ZIMP[1],x[i]-locations[0],false);
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y1,ZIMP[2],x[i]-locations[1],false);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y2,ZIMP[3],x[i]-locations[2],false);
	}
        
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y3,ZIMP[4],x[i]-locations[3],false);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y4,ZIMP[5],x[i]-locations[4],false);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y5,ZIMP[6],x[i]-locations[5],false);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y6,ZIMP[7],x[i]-locations[6],false);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y7,ZIMP[8],x[i]-locations[7],false);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Admittance[i] = EMF.computeYinAt(Y8,ZIMP[9],x[i]-locations[8],false);
	}
	
	for(i = 0; i < NPoints; i++){
	    y1[i] = Admittance[i].Real();
	    y2[i] = Admittance[i].Imaginary();
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
    }
    
    public void ScanReflectionCoefficient(){
	int i;
	
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(GammaL,x[i]);
	}
	
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma0,x[i]-locations[0]);
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma1,x[i]-locations[1]);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma2,x[i]-locations[2]);
	}
        
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma3,x[i]-locations[3]);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma4,x[i]-locations[4]);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma5,x[i]-locations[5]);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma6,x[i]-locations[6]);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma7,x[i]-locations[7]);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    RefCoef[i] = EMF.computeGammaAt(Gamma8,x[i]-locations[8]);
	}
        
	for(i = 0; i < NPoints; i++){
	    y1[i] = RefCoef[i].Real();
	    y2[i] = RefCoef[i].Imaginary();
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
    }
        
    
   public void ScanVoltage(){
	int i;
	
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(GammaL,ZIMP[0],VPP[0],x[i],true);
	}
	
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma0,ZIMP[1],VPP[1],x[i]-locations[0],true);
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma1,ZIMP[2],VPP[2],x[i]-locations[1],true);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma2,ZIMP[3],VPP[3],x[i]-locations[2],true);
	}
        
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma3,ZIMP[4],VPP[4],x[i]-locations[3],true);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma4,ZIMP[5],VPP[5],x[i]-locations[4],true);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma5,ZIMP[6],VPP[6],x[i]-locations[5],true);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma6,ZIMP[7],VPP[7],x[i]-locations[6],true);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma7,ZIMP[8],VPP[8],x[i]-locations[7],true);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma8,ZIMP[9],VPlus,x[i]-locations[8],true);
	}
        
        for(i = 0; i < NPoints; i++){
	    y1[i] = Voltage[i].Real();
	    y2[i] = Voltage[i].Imaginary();
            
            yV1[i] = Voltage[i].Magnitude();
            yV2[i] = - yV1[i];
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
        yV1[NPoints-1] = yV1[NPoints-2];
        yV2[NPoints-1] = yV2[NPoints-2];
    }
   
     public void ScanCurrent(){
	int i;
	
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(GammaL,ZIMP[0],VPP[0],x[i],true);
	}
	
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma0,ZIMP[1],VPP[1],x[i]-locations[0],true);
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma1,ZIMP[2],VPP[2],x[i]-locations[1],true);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma2,ZIMP[3],VPP[3],x[i]-locations[2],true);
	}
	
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma3,ZIMP[4],VPP[4],x[i]-locations[3],true);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma4,ZIMP[5],VPP[5],x[i]-locations[4],true);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma5,ZIMP[6],VPP[6],x[i]-locations[5],true);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma6,ZIMP[7],VPP[7],x[i]-locations[6],true);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma7,ZIMP[8],VPP[8],x[i]-locations[7],true);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Current[i] = EMF.computeIat(Gamma8,ZIMP[9],VPlus,x[i]-locations[8],true);
	}
        
        for(i = 0; i < NPoints; i++){
	    y1[i] = Current[i].Real();
	    y2[i] = Current[i].Imaginary();
            
            yV1[i] = Current[i].Magnitude();
            yV2[i] = - yV1[i];
	}
        
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
        yV1[NPoints-1] = yV1[NPoints-2];
        yV2[NPoints-1] = yV2[NPoints-2];
    }
    
    
    public void ScanVoltageCurrent(){
	int i;
	
	for(i = 0; i < SPos0; i++){
	    x[i]  = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(GammaL,ZIMP[0],VPP[0],x[i],true);
	    Current[i] = EMF.computeIat(GammaL,ZIMP[0],VPP[0],x[i],true);
	}
	
	for(i = SPos0; i < SPos1; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma0,ZIMP[1],VPP[1],x[i]-locations[0],true);
	    Current[i] = EMF.computeIat(Gamma0,ZIMP[1],VPP[1],x[i]-locations[0],true);
	}
	
	for(i = SPos1; i < SPos2; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma1,ZIMP[2],VPP[2],x[i]-locations[1],true);
	    Current[i] = EMF.computeIat(Gamma1,ZIMP[2],VPP[2],x[i]-locations[1],true);
	}
	
        for(i = SPos2; i < SPos3; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma2,ZIMP[3],VPP[3],x[i]-locations[2],true);
	    Current[i] = EMF.computeIat(Gamma2,ZIMP[3],VPP[3],x[i]-locations[2],true);
	}
        
        for(i = SPos3; i < SPos4; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma3,ZIMP[4],VPP[4],x[i]-locations[3],true);
	    Current[i] = EMF.computeIat(Gamma3,ZIMP[4],VPP[4],x[i]-locations[3],true);
	}
        
        for(i = SPos4; i < SPos5; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma4,ZIMP[5],VPP[5],x[i]-locations[4],true);
	    Current[i] = EMF.computeIat(Gamma4,ZIMP[5],VPP[5],x[i]-locations[4],true);
	}
        
        for(i = SPos5; i < SPos6; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma5,ZIMP[6],VPP[6],x[i]-locations[5],true);
	    Current[i] = EMF.computeIat(Gamma5,ZIMP[6],VPP[6],x[i]-locations[5],true);
	}
        
        for(i = SPos6; i < SPos7; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma6,ZIMP[7],VPP[7],x[i]-locations[6],true);
	    Current[i] = EMF.computeIat(Gamma6,ZIMP[7],VPP[7],x[i]-locations[6],true);
	}
        
        for(i = SPos7; i < SPos8; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma7,ZIMP[8],VPP[8],x[i]-locations[7],true);
	    Current[i] = EMF.computeIat(Gamma7,ZIMP[8],VPP[8],x[i]-locations[7],true);
	}
        
        for(i = SPos8; i < NPoints; i++){
	    x[i] = (i+1) * lineLength/(NPoints-1);
	    Voltage[i] = EMF.computeVat(Gamma8,ZIMP[9],VPlus,x[i]-locations[8],true);
	    Current[i] = EMF.computeIat(Gamma8,ZIMP[9],VPlus,x[i]-locations[8],true);
	}
        
	for(i = 0; i < NPoints; i++){
	    y1[i] = Voltage[i].Magnitude();
	    y2[i] = Current[i].Magnitude();
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
    }
    
    public void ScanTimeAveragePower(){
	ScanVoltageCurrent();
	for(int i = 0; i < NPoints; i++){
	    y1[i] = EMF.TimeAveragedPower(Voltage[i],Current[i]);
	    y2[i] = 0.0;
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
    }
    
    public void ScanRandom(){
	for(int i = 0; i < NPoints; i++){
	    x[i] = i * lineLength/(NPoints-1);
	    y1[i] = -100.0 + 200.0*Math.random();
	    y2[i] = -100.0 + 200.0*Math.random();
	}
    }
    
    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 < NPoints; i++){
	    yV1[i] = Voltage[i].Magnitude();
            yV2[i] = - yV1[i];
            Voltage[i].Multiply(timeFactor);
	    y1[i] = Voltage[i].Real();
	    y2[i] = 0.0;
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
        yV1[NPoints-1] = yV1[NPoints-2];
        yV2[NPoints-1] = yV2[NPoints-2];
        y1[0] = y1[1];
        y2[0] = y2[1];
        yV1[0] = yV1[1];
        yV2[0] = yV2[1];
    }
    
    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++){
	    yV1[i] = Current[i].Magnitude();
            yV2[i] = - yV1[i];
            Current[i].Multiply(timeFactor);
	    y1[i] = Current[i].Real();
	    y2[i] = 0.0;
	}
        y1[NPoints-1] = y1[NPoints-2];
        y2[NPoints-1] = y2[NPoints-2];
        yV1[NPoints-1] = yV1[NPoints-2];
        yV2[NPoints-1] = yV2[NPoints-2];
        
        y1[0] = y1[1];
        y2[0] = y2[1];
        yV1[0] = yV1[1];
        yV2[0] = yV2[1];
    }
    
    public void ScanTimeDependentPower(){
		
	timeFactor.setReal(Math.cos(2.0*Math.PI*ctime/NTime));
	timeFactor.setImaginary(Math.sin(2.0*Math.PI*ctime/NTime));
	ScanVoltageCurrent();
        Ptest = 0.0;
        double Pcheck = 0;
	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();
	    y1[i] = Complex.Real(Voltage[i])*Complex.Real(Current[i]);
	    y2[i] = 0.0;
            Pcheck = Complex.Magnitude(Power[i]);
            if(Pcheck > Ptest){Ptest = Pcheck;}
            yV1[i] = 0.0; //Pcheck * Math.cos(Math.PI/180.0(Complex.Arg1(Voltage[i])-Complex.Arg1(Current[i])));
            
            yV2[i] = 0.0;
            //System.out.println(i+"  "+Math.cos(Math.PI/180.0*(Complex.Arg1(Voltage[i])-Complex.Arg1(Current[i]))));
	}
            y1[NPoints-1] = y1[NPoints-2];
            y2[NPoints-1] = y2[NPoints-2];
            yV1[NPoints-1] = yV1[NPoints-2];
            yV2[NPoints-1] = yV2[NPoints-2];
            y1[0] = y1[1];
            y2[0] = y2[1];
            yV1[0] = yV1[1];
            yV2[0] = yV2[1];
    }
    
    public double getYRangeMax(int i){//arg=1 for Voltage Max
	//Complex VPlus = EMF.computeVPlus(generator,ZL,lineZ0,lineLength);
	double tmp=0.0;
	ignition();
	
	/*
	switch(i){
	    case 1://Voltage
		if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		    tmp = 2.0*VPlus.Magnitude();
		}
		else{
		    tmp = 3.0*VPlus.Magnitude();
		}
		break;
	    case 2://Current
		if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		    tmp = 2.0*VPlus.Magnitude()/lineZ0;
		}
		else{
		    tmp = 3.0*VPlus.Magnitude()/lineZ0;
		}
		break;
	    case 3://Power
		if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		    tmp = 2.0*VPlus.Magnitude2()/lineZ0;
		}
		else{
		    tmp = 3.0*VPlus.Magnitude2()/lineZ0;
		}
	}
        */
        switch(i){
	    case 1://Voltage
		//if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		  //  tmp = 2.0*VPlus.Magnitude();
		//}
		//else
                {
		    tmp = PlotTimeScale*VPmax.Magnitude();
		}
		break;
	    case 2://Current
		//if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		//    tmp = 2.0*VPlus.Magnitude()/ZIMP[0];
		//}
		//else
                {
		    tmp = PlotTimeScale*VPmax.Magnitude()/ZIMPmin;
		}
		break;
	    case 3://Power
		//if(!stub[0].isEnable() && !stub[1].isEnable() && !stub[2].isEnable()){
		  //  tmp = 2.0*VPlus.Magnitude2()/ZIMP[0];
		//}
		//else
                {
		    //tmp = PlotTimeScale*VPmax.Magnitude2()/ZIMPmin;
                    //tmp = PlotTimeScale*Pw[0];
                    tmp = PlotTimeScale*0.5*Ptest;
		}
	}
	return tmp;
    }
  
 
    //Sorts stubs and returns the results in an integer array iarray[].
    //The sort is perfrom in increasing stub's distance from the load
    //iarray[0] is first stub (closest to the load)
    //iarray[1] is second stub
    //iarray[3] is third stub	(farthest from the load)

private void sortStubs(){
	double tmp;
	iarray[0]=-1;
	iarray[1]=-1;
	iarray[2]=-1;
	
	iarray[0]=0;
	tmp=stub[iarray[0]].getPosition();
	for(int j=0;j<3;j++){
		if(tmp > stub[j].getPosition()){
			tmp=stub[j].getPosition();
			iarray[0]=j;
		}
	}

	if(iarray[0]!=0) {iarray[1]=0;}
	else{iarray[1]=1;}
	tmp=stub[iarray[1]].getPosition();
	for(int j=0;j<3;j++){
		if(j != iarray[0] && tmp > stub[j].getPosition()){
			tmp=stub[j].getPosition();
			iarray[1]=j;
		}
	}

	if(iarray[0] !=0 && iarray[1] !=0) { iarray[2]=0; }
	else if(iarray[0] !=1 && iarray[1] !=1) { iarray[2]=1; }
	else {iarray[2]=2;}
	/*
	tmp=stub[iarray[2]].getPosition();
	for(int j=0;j<3;j++){
		if(j != iarray[0] && j !=iarray[1] && tmp > stub[j].getPosition()){
			tmp=stub[j].getPosition();
			iarray[2]=j;
		}
	}
	*/
	
}



}/*State.java*/
