import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.io.File;
import static java.lang.Math.*;
public class mixing_up
{
public static void main(String[] args)
{
DecimalFormat fmt = new DecimalFormat("0.###E0"); //number of decimal places 3 and in scientific format
JOptionPane.showMessageDialog (null, "<html> This program calculates the mixing ratio using the chi squared test");
JOptionPane.showMessageDialog (null, "Written by me");
StringBuilder results = new StringBuilder();
String s,t,u,v,w,x,y,z,m,j,l,o,ap,bb,lo,q;
s = JOptionPane.showInputDialog("Enter the value of alpha2: ");
double ALPHA2 = Double.parseDouble(s);
t = JOptionPane.showInputDialog("Enter the value of alpha2 error: ");
double DALPHA2 = Double.parseDouble(t);
u= JOptionPane.showInputDialog(null,"Enter the value of alpha4: ");
double ALPHA4 = Double.parseDouble(u);
v= JOptionPane.showInputDialog("Enter the value of alpha4 error: ");
double DALPHA4 = Double.parseDouble(v);
w = JOptionPane.showInputDialog("Enter the value of A2/A0:");
double A2_EXP = Double.parseDouble(w);
x = JOptionPane.showInputDialog("Enter the value of A2/A0 error: ");
double DA2 = Double.parseDouble(x);
y = JOptionPane.showInputDialog("Enter the value of A4/A0: ");
double A4_EXP = Double.parseDouble(y);
z= JOptionPane.showInputDialog("Enter the value of A4/A0 error: ");
double DA4 = Double.parseDouble(z);
m =JOptionPane.showInputDialog("Enter the value of B2F2 (DD, 11): ");
double B2F21 = Double.parseDouble(m);
j =JOptionPane.showInputDialog("Enter the value of B2F2 (DQ, 12): ");
double B2F22 = Double.parseDouble(j);
l =JOptionPane.showInputDialog("Enter the value of B2F2 (QQ, 22): ");
double B2F23 = Double.parseDouble(l);
o =JOptionPane.showInputDialog("Enter the value of B4F4 (QQ): ");
double B4F4 = Double.parseDouble(o);
ap =JOptionPane.showInputDialog("Enter the lower limit for arctan(delta) (-90.lt.delta): ");
double DL = Double.parseDouble(ap);
bb =JOptionPane.showInputDialog("Enter the upper limit for arctan(delta) (delta.lt+90): ");
double DU = Double.parseDouble(bb);
lo =JOptionPane.showInputDialog("Enter the number of steps for arctan(delta) max 1000: ");
double N = Double.parseDouble(lo);
q =JOptionPane.showInputDialog("Please select from the following: To fit A2/A0 press 1. To fit A4/A0 press 2. To fit A2/A0 and A4/A0 press 3. ");
double r = Double.parseDouble(q);
double CHI_UP = 1000000000;
double CHI_LOW = 1000000000;
double CHI_CENTER = 1000000000;
double Lun = 6;
double ALPHA2_1 = ALPHA2-DALPHA2;
double ALPHA2_3 = ALPHA2+DALPHA2;
double ALPHA4_1 = ALPHA4-DALPHA4;
double ALPHA4_3 = ALPHA4+DALPHA4;
double [] A2_CAL;
A2_CAL = new double[3];
double [] DELTA;
DELTA = new double[1001];
double [] ANGLE;
ANGLE = new double[1001];
double [] ANGLE2;
ANGLE2 = new double[1001];
double []A4_CAL;
A4_CAL = new double[3];
double [] X2;
X2 = new double[3];
double[] LOGX2;
LOGX2 = new double[1001];
double[] CHI_2;
CHI_2 = new double[1001];
double DELTA_LOW;
double DELTA_UP;
double ANGLE_LOW;
double ANGLE_UP;
double DELTA_E_LOW;
double DELTA_E_UP;
double ANGLE_E_LOW;
double ANGLE_E_UP;
double CHI_E_LOW;
double CHI_E_UP;
double CHI_ERROR;
double DELTA_CENTER;
double ANGLE_CENTER;
double J_LOW;
double J_UP;
double J_CENTER;
if (r == 1) {
// put A2 only code here
for(int J=1; J< N; J++)
{
ANGLE[J]= DL + (J-1)*((DU - DL)/N);
ANGLE2[J] = ANGLE[J]/57.297;
DELTA[J] = Math.tan(ANGLE2[J]);
for(int I=1; I < 4; I++)
{
A2_CAL[I] = ALPHA2[I] / (1 + DELTA[J]*DELTA[J])*(B2F21 + 2*DELTA[J]*B2F22 + B2F23*Math.pow(DELTA[J],2));
X2[I] = Math.pow(A2_CAL[I] - A2_EXP,2) / Math.pow(DA2,2);
}
CHI_2[J] = X2[2];
LOGX2[J] = Math.log10(X2[2]);
if (CHI_2[J] < CHI_CENTER)
{
CHI_CENTER = CHI_2[J];
ANGLE_CENTER = ANGLE[J];
DELTA_CENTER = DELTA[J];
}
if (X2[1] < CHI_LOW)
{
CHI_LOW = X2[1];
ANGLE_UP = ANGLE[J];
DELTA_UP = DELTA[J];
}
if (X2[3] < CHI_UP)
{
CHI_UP = X2[3];
ANGLE_LOW = ANGLE[J];
DELTA_LOW = DELTA[J];
}
}
results.append("The values of X2(2) chi central i.e chi squared minimum " + fmt.format(CHI_CENTER) + "");
results.append('\n');
results.append("The value of X2(1) chi squared lower limit is " + fmt.format(CHI_LOW) + "");
results.append('\n');
results.append("The value of X2(3) chi squared upper limit is" + fmt.format(CHI_UP) + "");
results.append('\n');
results.append("The value of Delta for chi central is " + fmt.format(DELTA_CENTER) + "");
results.append('\n');
results.append("The value of Angle for chi central is " + fmt.format(ANGLE_CENTER) + "");
FileWriter resultFile = null;
try {
resultFile = new FileWriter("mixing_ratio.dat");
resultFile.write(results.toString());
resultFile.close();
} catch (IOException ex) {
Logger.getLogger(mixing_up.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog (null, "The values of X2(2) chi central i.e chi squared minimum " + fmt.format(CHI_CENTER) + "");
JOptionPane.showMessageDialog (null, "The value of X2(1) chi squared lower limit is " + fmt.format(CHI_LOW) + "");
JOptionPane.showMessageDialog (null, "The value of X2(3) chi squared upper limit is " + fmt.format(CHI_UP) + "");
JOptionPane.showMessageDialog (null, "The value of Delta for chi central is " + fmt.format(DELTA_CENTER) + "" );
JOptionPane.showMessageDialog (null, "The value of Angle for chi central is " + fmt.format(ANGLE_CENTER) + "" );
JOptionPane.showMessageDialog (null, "The value of chi squared central for a given value of delta is" + fmt.format(DELTA) + "" );
JOptionPane.showMessageDialog (null, "Thank you for using the program the results are saved in mixing_ratio.dat" );
JOptionPane.showMessageDialog (null, "Have a Nice Day" );
}
}
}