// this JAVA code is copyright D. Farkas and U. K. Krieger 1998
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Refra extends Applet implements ActionListener{
	Label prompt1, prompt2, prompt3, prompt4;
	TextField input1, input2, input3, input4;
	double xl, wts, wtn, T;
	double rho,ahno3,asul,ah2o; 
	double refra,result,val;
	double alpha;	
	int x,y,z;
	double d;
	int pointAt, length;

public void init()
{
	prompt1=new Label ("Enter the wavelength in micrometers (0.25-2):");
	add(prompt1);
	input1=new TextField(10);
	input1.addActionListener(this);
	add(input1);

	prompt2=new Label ("Enter the sulfuric acid weight percent(0.05-0.7):");
	add(prompt2);
	input2=new TextField(10);
	input2.addActionListener(this);
	add(input2);

	prompt3=new Label ("Enter the nitric acid weight percent(0.05-0.7):");
	add(prompt3);
	input3=new TextField(10);
	input3.addActionListener(this);
	add(input3);

	prompt4=new Label ("Enter the temperature in Kelvin (185-370)");
	add(prompt4);
	input4=new TextField(10);
	input4.addActionListener(this);
	add(input4);
	

}//end init

public void paint(Graphics g)
{
	
	g.drawString("Now please press ENTER", 25,240);
	g.drawString(Index(xl,wts,wtn,T)+" is the refractive index        ",25,260);
	

}//end method paint

public double getDouble(String s){

	
	pointAt=s.indexOf((int)'.');
	length=s.length();
 	x=Integer.parseInt(s.substring(0,pointAt));
	y=Integer.parseInt(s.substring((pointAt+1),length));
	z=(length-pointAt)-1;

 	d=x+(y* (Math.pow(10,-z)));
	
	return d;
}

public void actionPerformed(ActionEvent e)
{
	//Double val=new Double(e.getActionCommand());
	xl=getDouble(input1.getText()); 
	wts=getDouble(input2.getText()); 
	wtn=getDouble(input3.getText());
	T=getDouble(input4.getText());
	
	repaint();

}//end actionPerformed

public double Index(double xl, double wts, double wtn, double T)
{
double refra;
double refra2=(1+2*Alpha(xl,wts,wtn,T))/(1-Alpha(xl,wts,wtn,T));
if (refra2>=0)
refra=Math.sqrt(refra2);
else
{
refra=0;
//System.out.println("Error in input;please check your numbers.");
}

return refra;
}

public double Alpha(double xl, double wts, double wtn, double T)
{
double wt =wts+wtn;
double a = (1-wt)*Rho(xl,wts,wtn,T)/18.016*Ah2o(xl,wtn,wts,T)+wts*Rho(xl,wts,wtn,T)/98.08*Asul(xl,wts,wtn,T)+wtn*Rho(xl,wts,wtn,T)/63.016*Ahno3(xl,wts,wtn,T);
return a;
}

public double Ahno3(double xl, double wts, double wtn, double T)
{
	double a0,a1,a2,wt;
	wt=wtn+wts;
	double[] x = {9.9013,  -1.4456,   1.7326,  0.38526, -0.63329,  
	0.20787, 3.61314E-3,  0.44435, -0.32184,  0.19673, -0.63184, 
	1.23058E-2};
	a0=x[0]+x[1]*wt+x[2]*Math.pow(wt,2)+x[9]*Math.pow(wt,3);
	a1=x[3]+x[4]*wt+x[5]*wt*wt+x[10]*wt*wt*wt;
	a2=x[6]+x[7]*wt+x[8]*wt*wt+x[11]*wt*wt*wt;
	ahno3=a0+a1/xl+a2/(xl*xl);
	return ahno3;
}

public double Asul(double xl, double wts, double wtn, double T)
{
double[] x = {12.4714,  0.45272,  0.208, -0.1258,  1.8034E-002,  -4.6424,
     5.6781,  -2.6354,  0.43043, -1.5631E-002 , 10.149,  -7.6642,
     2.9535, -0.586,   6.0278E-002,  -22.475,    71.4748, 
	-53.5743,   16.294,  -1.73956};
	double xmu,wt;
	double a0,a1,a2,a3,a4;
	double ww;
	double asul;
	
	wt=wtn+wts;
	xmu=1/xl;
	ww=(wt-0.5);

	a0=x[0]+x[5]*ww+x[10]*ww*ww+x[15]*ww*ww*ww;
	a1=x[1]+x[6]*ww+x[11]*ww*ww+x[16]*ww*ww*ww;
	a2=x[2]+x[7]*ww+x[12]*ww*ww+x[17]*ww*ww*ww;
	a3=x[3]+x[8]*ww+x[13]*ww*ww+x[18]*ww*ww*ww;
	a4=x[4]+x[9]*ww+x[14]*ww*ww+x[19]*ww*ww*ww;

	asul=a0+a1*xmu+a2*xmu*xmu+a3*xmu*xmu*xmu+a4*xmu*xmu*xmu*xmu;
	return asul;
	
}


public double Ah2o(double xl, double wts, double wtn, double T)
{
	double a0,a1,a2;

	double x;
	
	double[] b1 = {1.4211,7.540441,-9.81512155,5.7218513,-1.237611};
	double[] b2 = {3.5740152,5.820734E-2,1.35319E-2,-1.5603E-3,8.85744E-4};

	x=1/xl;
	if(x<=1.23) 
	ah2o=b1[0]+b1[1]*x+b1[2]*x*x+b1[3]*x*x*x+b1[4]*x*x*x*x;
	else
	ah2o=b2[0]+b2[1]*x+b2[2]*x*x+b2[3]*x*x*x+b2[4]*x*x*x*x;
	return ah2o;

}
public double Rho(double xl, double wts, double wtn, double T)
{
double v1,vs,vn,w,vmcal,wth;
double rho;	
	double[] x = {2.393284E-02, -4.359335E-05, 7.961181E-08, 0.0,
     -0.198716351, 1.39564574E-03, -2.020633E-06,
     0.51684706, -3.0539E-03, 4.505475E-06, -0.30119511,
     1.840408E-03, -2.7221253742E-06, -0.113316741169,
      8.47763E-04, -1.22336185E-06, 0.3455282, -2.2111E-03,
      3.503768245E-06, -0.2315332, 1.60074E-03, -2.5827835E-06};


	w=wts+wtn;
	wth=1-w;
	
	v1=x[0]+x[1]*T+x[2]*T*T+x[3]*T*T*T;
	vs=x[4]+x[5]*T+x[6]*T*T+(x[7]+x[8]*T+x[9]*T*T)*w+
	(x[10]+x[11]*T+x[12]*T*T)*w*w;
	vn=x[13]+x[14]*T+x[15]*T*T+(x[16]+x[17]*T+x[18]*T*T)*w+
	(x[19]+x[20]*T+x[21]*T*T)*w*w;
	vmcal=wth/18.016*v1+vs*wts/98.08+vn*wtn/63.016;
	rho=1/(vmcal*1000);
	return rho;
}

}
