import java.util.*;

public class Trans_State{

    // Grid points
    public int Nx = 300;
    public int Nxcenter = 150;
    public int Ny = 300;
    public int Nycenter = 150; 
    
    public int Nx2 = 400;
    public int Nxcenter2 = 200;
    public int Ny2 = 400;
    public int Nycenter2 = 200; 
    
    // Time step - corresponds to 20 ps for default Dt
    public int Nt = 6000;
    // x-location of a plane wave source - vertical line, which generates a
    // plane wave with a k-vector parallel to x-axis
    //public int is=19;
    public int is=59;
    public int integer_time;
    // coordinates of barrier
    public int xA, xB, yA, yB;
    
    public int SleepTime = 20;
        
    // simulation parameters
    public int ic1= 134; public int ic2 = 206;
    //public int ic1= 66; public int ic2 = 276;
    
    // EM energy plot indices
    public int iw1=36; public int iw2=304; // x is [-201 nm; 201 nm]
    public int jw1=66; public int jw2=600; // y is [-399.75 nm; 399.75 nm]
  //------------------------------------------------------------------------
    // min/max values of x and y
    double xM=255.0E-9;
    double x0=-255.0E-9;
    double yM=500.25E-9; 
    double y0=-500.25E-9;
  //-------------------------------------------------------------------------
    private static final double eps0 = 8.8541878176E-12; // permittivity - Units: F/m
    private static final double mu0 = 1.25663706144E-6; //  permeability - Units H/m
    private static final double c = Math.sqrt(1.0/(eps0*mu0)); // velocity of light - Units m/s
  //-------------------------------------------------------------------------
    public double dx=(xM-x0)/Nx;  // mesh size x-direction
    public double dy=(yM-y0)/Ny;  // mesh size y-direction
    public double alpha,beta; // parameters of the Drude model for silver
    public double alpha_M,alpha_P,a,b;
    public double eps_r=8.926E0;
    //public double eps_r=1.0E200;
    
    public double omega_p=1.7601E16;
    public double GammaD=3.0841E14;
    public double tau=15.0E-15, E0y=1.0E0; //laser pulse duration & peak amplitude
  //-------------------------------------------------------------------------
    private double PI = Math.PI;
    public int palettereference = 382;
    public int palettereferenceB = 127;
    public int palettereferenceC = 127;
    
  //-------------------------------------------------------------------------  
    //  FDTD grid
    public double x[], xM2[], y[], yM2[];
    // laser pulse parameters
    public double lambda,omega,pulse[];
    // delta_x is a width of a silver film
    // delta_y is a slit size
    // length is a total length of the structure along y-axis
    public double delta_x,delta_y,length;    // structure parameters
    // absorption vectors: below is the set of necessary variables for PML absorption boundaries
    public int mUPML = 2;
    public double delta=25.0E-9,sigma0=1.0E8;
    public double sigmaX,sigmaX2,sigmaY,sigmaY2,temp;
    public double Ax1[], Ax2[], Ay1[],Ay2[];
    public double Azx1[], Azx2[], Azy1[], Azy2[];
    
    public double t, dt, dt_eps0, dt_mu0, dt_epsrJ, dt_epsr;
    public double iFDTD, jFDTD, nFDTD, mFDTD;
    public double Ex[][]; // theta-components of electric field - far field
    public double Ey[][]; // theta-components of electric field  - near field
    public double Er[][]; // r-components of electric field  - near field
    
    public double Dipole_Current = 1.0;
    public double Dipole_Charge = 0.0;
    
    // LARGE PLOTS
    public double Ex2[][]; // used for all possibilities
    
    public double Hzx[][], Hzy[][]; // z-components of magnetic field
    public double Theta[][], SinTheta[][], CosTheta[][], distance[][], one_over_distance[][], one_over_distance2[][], one_over_distance3[][];
    public double Theta2[][], SinTheta2[][], CosTheta2[][], distance2[][], one_over_distance_2[][], one_over_distance22[][], one_over_distance32[][];
    
    public double delta_lambda = 0.000666666 * 20;
    public int x_middle, y_middle;
        
    public double tmp1,tmp2,av_x1,av_x2,av_y,Ex_w,Ey_w,Hz_w; // some numerical variables
    public double w[][]; // EM energy as a function of x and y 
    public double w_plot[][]; // EM energy as a function of x and y - Window with solution results
    public double Ex_plot[][], Ey_plot[][];  //E-field components as a function of x and y - Window with solution results
    
    public int slit_index[][]; //array for slit region
    
    public double wmax_plot, Exmax_plot, Eymax_plot;
  //-------------------------------------------------------------------------
    
    public double xpos;
    public double lineLength, lineLength2, xmesh, xmesh2, x_discontinuity;
    public double frequency, Energy_maximum;
    public double epsilon_r,mu_r,epsilon_r2;
    public double lineZ0, lineZ02, Vg, Rg, VScale, IScale, PScale, VScaleFlag, IScaleFlag, PScaleFlag;
    
    public double Pw[],ReflectionGenerator, TransmissionGenerator, T_period, velocity_phase, velocity_phase2, DeltaT;
    public double T_period1, T_period2, Q_c, RampFactor1, RampFactor2;
   
    public boolean is_Load_Ztype, IsStepOn, Is_Millimeter, IsRampOn;
    //Plot Parameters
    Complex Voltage[], Current[], Impedance[], Admittance[];
    double GammaLoad, GammaInput, Vdivider, Idivider, Vold, Iold, Pold, Vnew, Inew, Vold2, Vnew2, Iold2, Inew2;
    public double Time_Voltage[], Time_Current[], Time_Power[], Vinfinity, Iinfinity, Pinfinity;
    //double x[];
    double xnew[];
    double y1[];
    public int zoomvalue;
    public double y1new[];
    public double PSInewReal[];
    public double PSInewImaginary[];
    public boolean PlotPsiSquare, PlotPsiReal, IsA_absorbing, IsB_absorbing, ShowCurrent,
                   IsTraceOn, RestartPlot, Plot_Energy, Plot_Ex, Plot_Ey, IsPEC;
    double y2[];
    public double yclean[];
    public double ycleannew[];
    public double SUM1, SUM2, SUM3, SUM_TOTAL, SUM_LEFT, SUM_BARRIER, SUM_RIGHT;
    
    public double barrier_location, barrier_width, centroid_location, spread;
    public static final int NPoints=1001;
    public int NTime, NTime2, ctime, dtime, ilocation, iload, Nmesh, Nmesh2;
    //Complex timeFactor; //jwt is the time factor.
    
    private static final Complex one = new Complex(1.0,0.0);
    public double range, timefactor;
    private static final double epsilon0 = 8.8541878176E-12; // Units: F/m
    public static final double mil_factor = 0.0254; // 1 mil = 0.0254 mm

	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 Complex PSI[], PSInew[], C2, UIM, A[], B[], C[], RHS[], W[], G[], PTEMP[];
        
        public int NPlus = 1500;
        public int NMinus = 1500;
        public int NQuantum = 1000+NPlus+NMinus;
        
        public double V[], VPRINT[], C1, C3, PACK[], EEE[], EMINV1[], EMINV2[], PRE[], PIM[];
        public double B1[], B2[], EMST[];
        public double K0, SIG;
        
      
        public int NITER;
        public double DX, DX_maximum, DX_minimum, DT, DT_maximum, DT_minimum, 
                      SIGFACT, VET, VET_eV, VVT, VVT_eV, VLEFT, TIME;        
        
        public int M, M_maximum, M_minimum, MOK, MM, MM_maximum, MM_minimum, 
                   ICENT, LIM, LIM2;
        public double HBAR, EMS1, EMS2, ELMASS, QELECTRON;
        
        public boolean IsThreshold = false;
        public boolean IsCoarser = false;
        public boolean Select_far = false;
        public boolean Select_r = false;
        public boolean Select_near = true;
        public boolean Select_dif = false;
        public boolean Select_wide = false;
        public boolean IsHorizon = false;
        public boolean IsMagnetic = true;
        public boolean IsFormula = false;
                
    public Trans_State(){ //main constructor
        integer_time = 0;
        //---------------------------------------
        alpha = -GammaD;
        beta = eps0*omega_p*omega_p;
        
        PlotPsiSquare = true;
        
        Plot_Energy = true;
        Plot_Ex = false;
        Plot_Ey = false;
        IsPEC = false;
        
        ShowCurrent = false;
        
        // parameters 
        delta_x = 50.0E-9;
        delta_y = 100.0E-9; // this parameter [delta_y] can be changed by a user ranging from 0 nm upto 700 nm
        length = 500.25E-9;
                
        xA = Nxcenter - (int)(delta_x/dx);
        xB = Nxcenter + (int)(delta_x/dx);
        
        yA = Nycenter - (int)(delta_y/dy);
        yB = Nycenter + (int)(delta_y/dy);
        //System.out.println("xA = "+xA+" xB = "+xB+"  yA = "+yA+"  yB = "+yB);
        
        dt = dx/(1.5 * c);
        dt_eps0 = dt/(eps0*dx);
        dt_epsr = dt/(eps0*eps_r*dx);
        dt_epsrJ = dt/(eps0*eps_r);
        dt_mu0 = dt/(mu0*dx);

        alpha_P = 1.0 + alpha * dt/2.0;
        alpha_M = 1.0 - alpha * dt/2.0;
        a = alpha_P/alpha_M;
        b = beta * dt/alpha_M;
        
        wmax_plot = 0.0;
        Exmax_plot = 0.0;
        Eymax_plot = 0.0;
        
        timefactor = 1.0/360.0;
        
        //System.out.println("mesh = "+dx+"   "+dy);
        
        lambda = 450.0E-9; // wavelength of a laser pulse can also be changed between 300 nm and 700 nm
        omega = 2.0 * PI * c /lambda;
        //----------------------------------------
	
            x = new double[Nx+1];
            xM2 = new double[Nx];
            y = new double[Ny+1];
            yM2 = new double[Ny];
            
            Theta = new double[Nx+1][Ny+1];
            CosTheta = new double[Nx+1][Ny+1];
            SinTheta = new double[Nx+1][Ny+1];
            distance = new double[Nx+1][Ny+1];
            one_over_distance = new double[Nx+1][Ny+1];
            one_over_distance2 = new double[Nx+1][Ny+1];
            one_over_distance3 = new double[Nx+1][Ny+1];
            
            Theta2 = new double[Nx2+1][Ny2+1];
            CosTheta2 = new double[Nx2+1][Ny2+1];
            SinTheta2 = new double[Nx2+1][Ny2+1];
            distance2 = new double[Nx2+1][Ny2+1];
            one_over_distance_2 = new double[Nx2+1][Ny2+1];
            one_over_distance22 = new double[Nx2+1][Ny2+1];
            one_over_distance32 = new double[Nx2+1][Ny2+1];
            
            
            Ex = new double[Nx+1][Ny+1];
            Ey = new double[Nx+1][Ny+1];
            Er = new double[Nx+1][Ny+1];
            
            Hzx = new double[Nx+1][Ny+1];
            Hzy = new double[Nx+1][Ny+1];
            
            w_plot = new double[iw2-iw1+1][jw2-jw1+1];
            Ex_plot = new double[iw2-iw1+1][jw2-jw1+1];
            Ey_plot = new double[iw2-iw1+1][jw2-jw1+1];
            
            w = new double[Nx+1][Ny+1];
            slit_index = new int[Nx+1][Ny+1];
        //------------------------------------------
        
            
            //--- grid ---
            for(int i2 = 0; i2 < Nx+1; i2++){
                x[i2] = x0 + dx*i2;
            }
            for(int j2 = 0; j2 < Ny+1; j2++){
                y[j2] = y0 + dy*j2;
            }
            for(int i2 = 0; i2 < Nx; i2++){
                xM2[i2] = x0 + dx*i2 + dx/2.0;
            }
            for(int j2 = 0; j2 < Ny; j2++){
                yM2[j2] = y0 + dy*j2 + dy/2.0;
            }   

            //-----------------------------------------------------
            //   Array dimension and initialization to zero
            //-----------------------------------------------------

            Ex = new double[Nx+1][Ny+1];
            Ey = new double[Nx+1][Ny+1];
            Er = new double[Nx+1][Ny+1];
            Hzx = new double[Nx+1][Ny+1];
            Hzy = new double[Nx+1][Ny+1];
            Ex2 = new double[Nx2+1][Ny2+1];
            
            for(int jj = 0; jj < Ny+1; jj++){
                for(int ii = 0; ii < Nx+1; ii++){
                    Ex[ii][jj] =0.0;
                    Ey[ii][jj] =0.0;
                    Er[ii][jj] = 0.0;
                    Hzx[ii][jj] =0.0;
                    Hzy[ii][jj] =0.0;
                }
            }
            
            for(int jj = 0; jj < Ny2+1; jj++){
                for(int ii = 0; ii < Nx2+1; ii++){
                    Ex2[ii][jj] =0.0;
                }
            }
            
            double x_point, y_point;
            
            //for(int jj = 0; jj < Ny+1; jj++){
              //  Theta[Nxcenter][jj] = 0.0;
                //    distance[Nxcenter][jj] = (double)(jj-Nycenter)*delta_lambda;
                //    one_over_distance[Nxcenter][jj] = 1.0/distance[Nxcenter][jj];
                //    one_over_distance2[Nxcenter][jj] = 1.0/(distance[Nxcenter][jj]*distance[Nxcenter][jj]);
                //    one_over_distance3[Nxcenter][jj] = 1.0/(Math.pow(distance[Nxcenter][jj],3));
            //}
            
            for(int jj = 0; jj < Ny+1; jj++){
                
                for(int ii = 0; ii <= Nxcenter; ii++){
                    x_point = (double)(Nxcenter-ii)*delta_lambda;
                    y_point = (double)(jj-Nycenter)*delta_lambda;
                    distance[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance[ii][jj] = 1.0/distance[ii][jj];
                    one_over_distance2[ii][jj] = 1.0/(distance[ii][jj]*distance[ii][jj]);
                    one_over_distance3[ii][jj] = 1.0/(Math.pow(distance[ii][jj],3));
                    
                }
                for(int ii = Nxcenter+1; ii < Nx+1; ii++){
                    x_point = (double)(ii-Nxcenter)*delta_lambda;
                    y_point = (double)(jj-Nycenter)*delta_lambda;
                    distance[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance[ii][jj] = 1.0/distance[ii][jj];
                    one_over_distance2[ii][jj] = 1.0/(distance[ii][jj]*distance[ii][jj]);
                    one_over_distance3[ii][jj] = 1.0/(Math.pow(distance[ii][jj],3));
                }
            }

            for(int jj = 0; jj < Ny2+1; jj++){
                
                for(int ii = 0; ii <= Nxcenter2; ii++){
                    x_point = (double)(Nxcenter2-ii)*delta_lambda/2;
                    y_point = (double)(jj-Nycenter2)*delta_lambda/2;
                    distance2[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta2[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance_2[ii][jj] = 1.0/distance2[ii][jj];
                    one_over_distance22[ii][jj] = 1.0/(distance2[ii][jj]*distance2[ii][jj]);
                    one_over_distance32[ii][jj] = 1.0/(Math.pow(distance2[ii][jj],3));
                    
                }
                for(int ii = Nxcenter2+1; ii < Nx2+1; ii++){
                    x_point = (double)(ii-Nxcenter2)*delta_lambda/2;
                    y_point = (double)(jj-Nycenter2)*delta_lambda/2;
                    distance2[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta2[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance_2[ii][jj] = 1.0/distance2[ii][jj];
                    one_over_distance22[ii][jj] = 1.0/(distance2[ii][jj]*distance2[ii][jj]);
                    one_over_distance32[ii][jj] = 1.0/(Math.pow(distance2[ii][jj],3));
                }
            }
            
        DT = dx/(1.5 * c);
        
	NTime = 360;
	NTime2 = 360;
	
	ctime=0;
	DeltaT = T_period/NTime2;
	dtime=2;
	
	//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);

	ignition();
    }
    
    public synchronized void ignition(){
                
        if(IsPEC){
            eps_r=1.0E200;
        }
        else{
            eps_r=8.926E0;
        }
        
        Dipole_Current = 1.0;
        Dipole_Charge = 0.0;
        
         integer_time = 0;
	//double tolerance = 0.0001;
        omega = 2.0 * PI * c /lambda;
        frequency = omega/(2.0*Math.PI);
        alpha = -GammaD;
        beta = eps0*omega_p*omega_p;
       
        alpha_P = 1.0 + alpha * dt/2.0;
        alpha_M = 1.0 - alpha * dt/2.0;
        a = alpha_P/alpha_M;
        b = beta * dt/alpha_M;

        //System.out.println("delta_lambda = "+delta_lambda);
        //lambda = 450.0E-9; // wavelength of a laser pulse can also be changed between 300 nm and 700 nm
        //omega = 2.0 * PI * c /lambda;
        //----------------------------------------
            double x_point, y_point;
                    
            for(int jj = 0; jj < Ny+1; jj++){
                
                for(int ii = 0; ii <= Nxcenter; ii++){
                    x_point = (double)(Nxcenter-ii)*delta_lambda;
                    y_point = (double)(jj-Nycenter)*delta_lambda;
                    distance[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance[ii][jj] = 1.0/distance[ii][jj];
                    one_over_distance2[ii][jj] = 1.0/(distance[ii][jj]*distance[ii][jj]);
                    one_over_distance3[ii][jj] = 1.0/(Math.pow(distance[ii][jj],3));
                    
                }
                for(int ii = Nxcenter+1; ii < Nx+1; ii++){
                    x_point = (double)(ii-Nxcenter)*delta_lambda;
                    y_point = (double)(jj-Nycenter)*delta_lambda;
                    distance[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance[ii][jj] = 1.0/distance[ii][jj];
                    one_over_distance2[ii][jj] = 1.0/(distance[ii][jj]*distance[ii][jj]);
                    one_over_distance3[ii][jj] = 1.0/(Math.pow(distance[ii][jj],3));
                }
            }
            
            for(int jj = 0; jj < Ny2+1; jj++){
                
                for(int ii = 0; ii <= Nxcenter2; ii++){
                    x_point = (double)(Nxcenter2-ii)*3*delta_lambda/4;
                    y_point = (double)(jj-Nycenter2)*3*delta_lambda/4;
                    distance2[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta2[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance_2[ii][jj] = 1.0/distance2[ii][jj];
                    one_over_distance22[ii][jj] = 1.0/(distance2[ii][jj]*distance2[ii][jj]);
                    one_over_distance32[ii][jj] = 1.0/(Math.pow(distance2[ii][jj],3));
                    
                }
                for(int ii = Nxcenter2+1; ii < Nx2+1; ii++){
                    x_point = (double)(ii-Nxcenter2)*3*delta_lambda/4;
                    y_point = (double)(jj-Nycenter2)*3*delta_lambda/4;
                    distance2[ii][jj] = Math.sqrt(Math.pow(x_point,2) + 
                                                 Math.pow(y_point,2)) ;
                    Theta2[ii][jj] = Math.PI - Math.atan2(x_point,y_point);
                    one_over_distance_2[ii][jj] = 1.0/distance2[ii][jj];
                    one_over_distance22[ii][jj] = 1.0/(distance2[ii][jj]*distance2[ii][jj]);
                    one_over_distance32[ii][jj] = 1.0/(Math.pow(distance2[ii][jj],3));
                }
            }
        
            x = new double[Nx+1];
            xM2 = new double[Nx+1];
            y = new double[Ny+1];
            yM2 = new double[Ny+1];
 
            pulse = new double[Nt+1];
            Ax1 = new double[Ny+1];
            Ax2 = new double[Ny+1];
            Ay1 = new double[Nx+1];
            Ay2 = new double[Nx+1];
            Azx1 = new double[Nx];
            Azx2 = new double[Nx];
            Azy1 = new double[Ny];
            Azy2 = new double[Ny];

            w_plot = new double[iw2-iw1+1][jw2-jw1+1];
            w = new double[Nx+1][Ny+1];
            slit_index = new int[Nx+1][Ny+1];
        //------------------------------------------
        
            //delta_x = 50.0E-9; delta_y = 100.0E-9;
            //length = 500.25E-9;
            //System.out.println(delta_x);
            dt = dx/(1.5 * c);
            dt_eps0 = dt/(eps0*dx);
            dt_epsr = dt/(eps0*eps_r*dx);
            dt_epsrJ = dt/(eps0*eps_r);
            dt_mu0 = dt/(mu0*dx);

            alpha_P = 1.0 + alpha * dt/2.0;
            alpha_M = 1.0 - alpha * dt/2.0;
            a = alpha_P/alpha_M;
            b = beta * dt/alpha_M;
            
            xA = Nxcenter - (int)(delta_x/dx);
            xB = Nxcenter + (int)(delta_x/dx);
        
            yA = Nycenter - (int)(delta_y/dy);
            yB = Nycenter + (int)(delta_y/dy);
            
            ic1 = xA - 4;
            ic2 = xB + 4;
            if(is >= ic1){is = ic1 -1;}
            
        
            //--- grid ---
            for(int i2 = 0; i2 < Nx+1; i2++){
                x[i2] = x0 + dx*i2;
            }
            for(int j2 = 0; j2 < Ny+1; j2++){
                y[j2] = y0 + dy*j2;
            }
            for(int i2 = 0; i2 < Nx; i2++){
                xM2[i2] = x0 + dx*i2 + dx/2.0;
            }
            for(int j2 = 0; j2 < Ny; j2++){
                yM2[j2] = y0 + dy*j2 + dy/2.0;
            }
            
            double betafac, betafac2, arg;
            betafac = 2.0*Math.PI;
            betafac2 = betafac*betafac; 
            arg = 0.0;
        
            //ctime=0;
            
            if(!IsHorizon){
              if(IsMagnetic){
                  for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Theta[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Theta[ii][jj])*(
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                            - Math.sin(arg) * betafac2 * one_over_distance[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 1; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 0.0;
                        }
                    }


                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0;
                    
              }
              else{
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Theta[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Theta[ii][jj])*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj] - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 1; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 2.0*Math.cos(Theta[ii][jj])*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }


                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0;
                    Er[Nxcenter][Nycenter]=0.0;
              }
            }
            else{
                 if(IsMagnetic){
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Math.PI/2)*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Math.PI/2)*(
                                            Math.sin(arg)*( - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] =  0.0;
                        }
                    }
                
                    Ex[Nxcenter][Nycenter]=0.0;
                    Ey[Nxcenter][Nycenter]=0.0; 
                   
                 }
                 else{
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Math.PI/2)*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Math.PI/2)*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj] - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] =  2.0*Math.cos(Math.PI/2)*(
                                          Math.sin(arg)*(one_over_distance3[ii][jj])
                                          + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                
                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0; 
                    Er[Nxcenter][Nycenter]=0.0;
                 }
            }
            
            //------------------------------------------------------------------------------------
            // wide plots
            
            if(!IsHorizon){  // Vertical plane
                if(IsMagnetic){
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*( - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 0.0;
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // difference theta-component for near and far 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                               betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    Ex2[Nxcenter2][Nycenter2]=0.0;
                }
                else{
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj] - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 2.0*Math.cos(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // difference theta-component for near and far 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    Ex2[Nxcenter2][Nycenter2]=0.0; 
                }
            }
            else{// Horizontal plane
                if(IsMagnetic){// Magnetic field
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*(
                                                Math.sin(arg)*( - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 0.0;
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                //  
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*(
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }

                   Ex2[Nxcenter2][Nycenter2]=0.0;
                }
                else{
                   if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj] - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 2.0*Math.cos(Math.PI/2)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                //  
                                Ex2[ii][jj] = Math.sin(Math.PI/2)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }

                   Ex2[Nxcenter2][Nycenter2]=0.0; 
                }
            }
            
            
            //------------------------------------------------------------------------------------
    }
    
    public synchronized void increment(){
	ctime+=dtime;
        if(ctime >= 360){ctime = 0;}
        
        integer_time+=1;
        //System.out.println("in state  integer_time"+integer_time);
	//if(ctime>NTime){ ctime -= NTime; }
    }
    
    public synchronized void reset(){
	ctime=0;
        integer_time = 0;
    }
    
    
    public void ScanTimeVoltage(){

    }
    
    public void ScanTimeCurrent(){ 
	
    }
    
    public void ScanTimePower(){ 
    
    }
    
    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();
	}
    }
    
    
    //
    //  TRANSIENTS
    //
    
    
    public void ScanTransientVoltage(){
        
        int nFDTD; double pulse_parameter = 0.0;
        //---------------------------------------------------------------------
        //                                                    UPDATE TIME CLOCK
        //---------------------------------------------------------------------
        
        TIME = (ctime)*DT;
        nFDTD = ctime;
        double betafac, betafac2, arg;
        betafac = 2.0*Math.PI;
        betafac2 = betafac*betafac; 
        arg = 0.0;
        
        Dipole_Current = Math.cos(Math.PI*ctime/180.0);
        Dipole_Charge = Math.sin(Math.PI*ctime/180.0);
        //System.out.println((Math.PI*ctime/180.0)+"   "+Dipole_Current);
        
        if(ctime!=0){
        if(Select_wide){
            if(!IsHorizon){
                if(IsMagnetic){
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Theta[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Theta[ii][jj])*(
                                            Math.sin(arg)*( - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 0.0;
                        }
                    }
                    
                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0;
                    
                }
                else{
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Theta[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Theta[ii][jj])*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj] - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 2.0 * Math.cos(Theta[ii][jj])*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    
                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0;
                    Er[Nxcenter][Nycenter]=0.0;
                }
            }
            else{
                if(IsMagnetic){
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Math.PI/2.0)*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Math.PI/2.0)*(
                                            Math.sin(arg)*( - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 0.0;
                        }
                    }
                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0; 
                    
                }
                else{
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // far field 
                            Ex[ii][jj] = Math.sin(Math.PI/2.0)*Math.sin(arg)*(- betafac2*one_over_distance[ii][jj]);
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // E_theta - near field
                            Ey[ii][jj] = Math.sin(Math.PI/2.0)*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj] - betafac2*one_over_distance[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    for(int jj = 0; jj < Ny+1; jj++){
                        for(int ii = 0; ii < Nx; ii++){
                            arg = betafac*(timefactor*ctime - distance[ii][jj]);
                            // near field - radial component 
                            Er[ii][jj] = 2.0 * Math.cos(Math.PI/2.0)*(
                                            Math.sin(arg)*(one_over_distance3[ii][jj])
                                            + betafac * Math.cos(arg) * one_over_distance2[ii][jj]
                                         );
                        }
                    }
                    Ex[Nxcenter][Nycenter]=0.0; 
                    Ey[Nxcenter][Nycenter]=0.0; 
                    Er[Nxcenter][Nycenter]=0.0;
                }
            }
        }
            //------------------------------------------------------------------------------
        else{    
            if(!IsHorizon){
                if(IsMagnetic){
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*( - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 0.0;
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near - far difference theta component  
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    Ex2[Nxcenter2][Nycenter2]=0.0;
                }
                else{
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj] - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 2.0 * Math.cos(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near - far difference theta component  
                                Ex2[ii][jj] = Math.sin(Theta2[ii][jj])*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    Ex2[Nxcenter2][Nycenter2]=0.0; 
                }
            }
            else{
                if(IsMagnetic){
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*(
                                                Math.sin(arg)*( - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 0.0;
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near - far difference theta component 
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*(
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }

                    Ex2[Nxcenter2][Nycenter2]=0.0;
                }
                else{
                    if(Select_far){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // far field 
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*Math.sin(arg)*(- betafac2*one_over_distance_2[ii][jj]);
                            }
                        }
                    }
                    else if(Select_near){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // E_theta - near field
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj] - betafac2*one_over_distance_2[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_r){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near field - radial component 
                                Ex2[ii][jj] = 2.0 * Math.cos(Math.PI/2.0)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }
                    else if(Select_dif){
                        for(int jj = 0; jj < Ny2+1; jj++){
                            for(int ii = 0; ii < Nx2; ii++){
                                arg = betafac*(timefactor*ctime - distance2[ii][jj]);
                                // near - far difference theta component 
                                Ex2[ii][jj] = Math.sin(Math.PI/2.0)*(
                                                Math.sin(arg)*(one_over_distance32[ii][jj])
                                                + betafac * Math.cos(arg) * one_over_distance22[ii][jj]
                                             );
                            }
                        }
                    }

                    Ex2[Nxcenter2][Nycenter2]=0.0;
                }
            }
            //------------------------------------------------------------------------------
        }
      }
    }
    
    public void ScanTransientCurrent(){
        int nFDTD; double pulse_parameter = 0.0;
        //---------------------------------------------------------------------
        //                                                    UPDATE TIME CLOCK
        //---------------------------------------------------------------------
        TIME = (ctime)*DT;
        nFDTD = ctime;
        
        if(ctime!=0){
            
            for(int jFDTD = 0; jFDTD < Ny; jFDTD++){
                for(int iFDTD = 0; iFDTD < Nx; iFDTD++){
                    Hzx[iFDTD][jFDTD] = 0.0;
                    Ex[iFDTD][jFDTD] = 0.0;
                    Ey[iFDTD][jFDTD] = 0.0;
                }
            }
            
        }   
    }
    
    public void ScanTransientPower(){

    }
        
    public double getYRangeMax(int i){
	double tmp=0.0;
	
	//ignition();
	
	
	switch(i){
	    case 1://Voltage
		    tmp =  50.0 * (double)(zoomvalue+1); 
                    range = tmp;
		    
		break;
		
	    case 2://Current
                    tmp = 5.0 * (double)(zoomvalue+1);
                    range = tmp;
		break;
		
	    case 3://Power
	    
		    
		break;
	}
	return tmp;
    }
  
}

