/* * simpani.java - revised 22 Apr 05 - width 601, height 401 * @author jackord@kw.igs.net * the algorithm optimizes the fit of P-A data to the growth curve for a * transparent anisotropic film * NOTES: (1) THE ALGORITHM DOES ARITHMETIC BUT MAKES NO CLAIM ABOUT * THE SIGNIFICANCE OF THE PARAMETER VALUES IT DETERMINES * (2) "Data2" REQUIRES THE FILE "Va3o.txt" IN THE SAME DIRECTORY * (3) "Print" LISTS ONLY EVERY THIRD POINT IN "Data2" */ import java.awt.*; import java.net.*; import java.io.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class simpani extends java.applet.Applet implements ActionListener { int x1, y1, x2, y2, xorg, yorg; // Declarations int kk=0; int np=0; int ndata=0; int w=0; int sn=5; String b1s="Data1"; Button b1=new Button(b1s); String b2s="Data2"; Button b2=new Button(b2s); String b3s="Tplot"; Button b3=new Button(b3s); String b4s="Smplx"; Button b4=new Button(b4s); String b5s="Print"; Button b5=new Button(b5s); String zz; Checkbox ch1=new Checkbox("Ns"); Checkbox ch2=new Checkbox("Ks"); Checkbox ch3=new Checkbox("Nx"); Checkbox ch4=new Checkbox("Nz"); TextField tx[]=new TextField[5]; int[] ip=new int[6]; double[] db=new double[13]; double[] b=new double[2]; double[][] q=new double[9][6]; double[][] c=new double[3][2]; double[][] d=new double[3][2]; double[][] da=new double[70][4]; double theta, namb, lmda, bb, pp, aa, df; double pi=Math.PI; int nvr, nvx, test; String fs; char URLSeparator='/'; public void init() { setBackground(new Color(211, 211, 211)); for (int i=1; i<=4; i=i+1) { tx[i]= new TextField(10); add(tx[i]); tx[i].setBackground(getBackground()); } add(b1); add(b2); add(b3); add(b4); add(b5); add(ch1); add(ch2); add(ch3); add(ch4); ch1.setBackground(getBackground()); ch2.setBackground(getBackground()); ch3.setBackground(getBackground()); ch4.setBackground(getBackground()); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); } public void getdata() { // Get data from server int k; String txt; String ds=new String(); String urlString=getCodeBase().toString(); urlString=urlString.substring(0, urlString.lastIndexOf(URLSeparator)) +URLSeparator+fs; try { URL url=new URL(urlString); InputStream inStream=url.openStream(); BufferedReader dts=new BufferedReader(new InputStreamReader(inStream)); String inLine=null; while ((inLine=dts.readLine())!=null) { ds=ds+inLine; // Data string ds } } catch(MalformedURLException e) { showStatus("Invalid URL: "+urlString); } catch(IOException e) { showStatus("Error "+e); } for (int i=1; i<=ndata; i=i+1) { // Unpack ds into... for (int j=0; j<=1; j=j+1) { k=12*(i-1)+6*j; txt=ds.substring(k, k+6); try { da[i][j]=Double.valueOf(txt).doubleValue(); // ...da[i][j] } catch(NumberFormatException e) { showStatus("Error "+e); } } } } public void pin(Graphics g) { if (kk==0) { // Screen layout tx[1].setBounds(50, 3, 100, 20); tx[2].setBounds(50, 27, 100, 20); tx[3].setBounds(240, 3, 100, 20); tx[4].setBounds(240, 27, 100, 20); ch1.setBounds(0, 3, 50, 20); ch2.setBounds(0, 27, 50, 20); ch3.setBounds(190, 3, 50, 20); ch4.setBounds(190, 27, 50, 20); b1.setBounds(0, 68, 50, 20); b2.setBounds(0, 92, 50, 20); b3.setBounds(0, 116, 50, 20); b4.setBounds(0, 140, 50, 20); b5.setBounds(0, 164, 50, 20); g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); xorg=80; yorg=360; g.setColor(Color.black); g.drawRect(80, 60, 500, 300); } if ((kk>0)&&(kk<4)) { g.setColor(Color.black); // P-A plot grid for (int i=0; i<=5; i=i+1) { x1=80+i*100; g.drawLine(x1, 60, x1, 360); } for (int i=0; i<=3; i=i+1) { y1=60+i*100; g.drawLine(80, y1, 580, y1); } g.drawRect(380, 0, 200, 50); // Simplex strip chart for (int i=1; i<=3; i=i+1) { x1=380+50*i; g.drawLine(x1, 0, x1, 50); } g.drawString(Double.toString(db[7]), xorg-10, yorg+20); g.drawString(Double.toString(db[8]), 580-18, yorg+20); g.drawString("P deg", 310, yorg+20); g.drawString(Double.toString(db[9]), xorg-28, yorg+6); g.drawString(Double.toString(db[10]), xorg-28, 66); g.drawString("A", 50, 208); g.drawString("deg", 44, 228); for (int i=1; i<=ndata; i=i+1) { g.setColor(Color.red); g.fillArc(xs(da[i][0])-5, ys(da[i][1])-5, 10, 10, 0, 360); g.setColor(Color.black); g.drawArc(xs(da[i][0])-5, ys(da[i][1])-5, 10, 10, 0, 360); } } } int xs(double x) { // Scale x return xorg+(int)((x-db[7])*500/(db[8]-db[7])); } int ys(double y) { // Scale y return yorg-(int)((y-db[9])*300/(db[10]-db[9])); } public void pst(double x, double y, Graphics g) { x1=xs(x); y1=ys(y); // Start line } public void plt(double x, double y, Graphics g) { // Draw line x2=xs(x); y2=ys(y); if (Math.abs(x2-x1)>90) { x1=x2; y1=y2; } g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } public void tplot(Graphics g) { // P-A curve double l, pold; pold=0; pq(1); pa(db[5]); pst(pp, aa, g); for (int i=1; i<=np; i=i+1) { l=db[5]+i*(db[6]-db[5])/np; pa(l); if (Math.abs(pp-pold)>120) { pst(pp, aa, g); } else { plt(pp, aa, g); } pold=pp; } } public void paint(Graphics g) { // Main program pin(g); if (kk==2) { g.setColor(Color.magenta); tplot(g); } if (kk==3) { g.setColor(Color.green); tplot(g); sim(g); } if (kk==4) { int r=60; int c=90; int ii=1; if (ndata>20) { ii=3; } g.setColor(Color.black); g.drawString("P deg", c, r); c=c+120; g.drawString("A deg", c, r); c=c+120; g.drawString("Thick nm", c, r); c=c+120; g.drawString("Dev deg", c, r); c=90; r=r+5; for (int i=1; i<=ndata; i=i+ii) { r=r+15; for (int j=0; j<=3; j=j+1) { g.drawString(Float.toString((float)da[i][j]), c+j*120, r); } } r=r+15; g.drawString("rms dev", 390, r); g.drawString(Float.toString((float)q[1][sn]), 450, r); kk=0; } } public void sim(Graphics g) { // Simplex algorithm int ii=0; int iv=0; int jlof=0; int xflag; double dni, dnf, ave, nexp; long tt=System.currentTimeMillis(); dni=0.02; dnf=0.0000001; if (w==0) { zz="Weight Off"; } else { zz="Weight On"; } if (ch1.getState()==true) { ii=ii+1; ip[ii]=1; } if (ch2.getState()==true) { ii=ii+1; ip[ii]=2; } if (ch3.getState()==true) { ii=ii+1; ip[ii]=3; } if (ch4.getState()==true) { ii=ii+1; ip[ii]=4; } nvr=ii; nvx=ii+1; ip[nvx]=sn; if (nvr==0) { df=0; lfit(1); } else { for (int i=2; i<=(nvx+3); i=i+1) { for (int j=1; j<=4; j=j+1) { q[i][j]=q[1][j]; } } for (int i=1; i<=nvr; i=i+1) { q[i][ip[i]] = q[i][ip[i]]+dni; } for (int i=1; i<=nvx; i=i+1) { lfit(i); } sort(); g.setColor(Color.red); // Plot stripchart x1=380+(int)(15*Math.log(df/dnf)); y1=1; if (x1>570) { x1=570; } } while (df>dnf) { x2=380+(int)(15*Math.log(df/dnf)); y2=y1+1; if (x2>570) { x2=570; } g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; if (y1==49) { y1=1; } xflag=0; for (int i=1; i<=nvr; i=i+1) { ave=0; for (int j=1; j<=nvr; j=j+1) { ave=ave+q[j][ip[i]]; } q[nvx+1][ip[i]]=ave/nvr; } if ((jlof==1)&&(q[nvx+2][sn]==q[nvx][sn])) { jlof=0; cns(); } else { jlof=0; iv=nvx+2; for (int i=1; i<=nvr; i=i+1) { q[iv][ip[i]]=2*q[nvx+1][ip[i]]-q[nvx][ip[i]]; } lfit(iv); if (q[iv][sn]>q[nvx][sn]) { cns(); } else { if (q[iv][sn]q[i+1][sn]) { s=s+1; for (int j=1; j<=nvx; j=j+1) { temp=q[i][ip[j]]; q[i][ip[j]]=q[i+1][ip[j]]; q[i+1][ip[j]]=temp; } } } } while (s>0); for (int i=1; i<=nvr; i=i+1) { max=q[1][ip[i]]; min=max; for (int j=2; j<=nvx; j=j+1) { if (q[j][ip[i]]>max) { max=q[j][ip[i]]; } if (q[j][ip[i]]df) { df=max-min; } } } } public void cns() { // Shrink simplex int iv=nvx+2; for (int i=1; i<=nvr; i=i+1) { q[iv][ip[i]] = (q[nvx+1][ip[i]]+q[nvx][ip[i]])/2; } lfit(iv); if (q[iv][sn]90) { pp=pp-180; } if ((da[j][0]-pp)>90) { pp=pp+180; } pd=da[j][0]-pp; if (w>0) { pd=pd*Math.sin(aa*pi/90); } ad=da[j][1]-aa; return pd*pd+ad*ad; } public void lfit(int i) { // Find thickness double fa, fb, fs, fx, la, lb, lx, cf, dl, ddl, ddlf; dl=1; fs=0; pq(i); for (int j=1; j<=ndata; j=j+1) { la=da[j][2]; if ((la==0)&&(j>1)) { la=da[j-1][2]; } fa=ds(la, j); lb=la+dl; fb=ds(lb, j); if (fb>fa) { lx=la; la=lb; lb=lx; fx=fa; fa=fb; fb=fx; dl=-dl; } lx=lb+dl; fx=ds(lx, j); while (fxddlf); da[j][2]=lb; da[j][3]=Math.sqrt(fb); fs=fs+fb; } q[i][sn]=Math.sqrt(fs/ndata); } public void pq(int iv) { // Calculate p and q double n, k, nx, nz, e2, e3, e4, e5, e6; n=q[iv][1]; k=q[iv][2]; e2=n*n; e3=k*k; e4=e2-e3-bb; e5=Math.sqrt(e4*e4+4*e2*e3); c[0][0]=Math.sqrt((e4+e5)/2); d[0][0]=-n*k/c[0][0]; e6=1/(e2+e3)/(e2+e3); c[0][1]=e6*(c[0][0]*(e2-e3)-2*d[0][0]*n*k); d[0][1]=e6*(d[0][0]*(e2-e3)+2*c[0][0]*n*k); nx=q[iv][3]; nz=q[iv][4]; c[1][0]=Math.sqrt(nx*nx-bb); d[1][0]=c[1][0]; c[1][1]=nx*Math.sqrt(1-bb/nz/nz); d[1][1]=c[1][1]/nx/nx; } public void pa(double l) { // Calculate P and A double[] u=new double[2]; double[] v=new double[2]; double[] e=new double[4]; double[] f=new double[4]; double f1, f2, f3; double g1, g2, g3, g4, g5, g6, g7, g8; for (int i=0; i<=1; i=i+1) { f1=l*2*pi/lmda*c[1][i]; f2=Math.sin(f1); f3=Math.cos(f1); e[0]=f3; e[1]=-f2/d[1][i]; e[2]=f2*d[1][i]; e[3]=e[0]; g1=(e[0]+e[1]*d[0][i])*b[i]; g2=(-e[1]*c[0][i])*b[i]; g3=e[3]*c[0][i]; g4=e[2]+e[3]*d[0][i]; g5=(g1+g3)*(g1+g3)+(g2+g4)*(g2+g4); u[i]=(g1*g1+g2*g2-g3*g3-g4*g4)/g5; v[i]=2*(g2*g3-g1*g4)/g5; } g6=u[0]*u[0]+v[0]*v[0]; g7=(u[0]*u[1]+v[0]*v[1])/g6; g8=(u[0]*v[1]-u[1]*v[0])/g6; aa=Math.atan(Math.sqrt(g7*g7+g8*g8))*180/pi; pp=Math.atan2(g8, g7)*90/pi-45; if (pp0) { b2=(2*db[1]+90)*pi/180; b3=Math.tan(db[2]*pi/180); c1=bb/b[0]/(b3*b3+2*b3*Math.cos(b2)+1); c2=c1*(1-b3*b3); c3=2*b3*c1*Math.sin(b2); c4=c2*c2-c3*c3+bb; c5=2*c2*c3; c6=Math.sqrt(c4*c4+c5*c5); db[1]=Math.sqrt((c4+c6)/2); db[2]=c5/2/db[1]; } for (int i=1; i<=4; i=i+1) { tx[i].setText(Float.toString((float)db[i])); } } public void getnums() { // Keyboard input String txt; test=0; for (int i=1; i<=4; i=i+1) { txt=tx[i].getText(); try { db[i]=Double.valueOf(txt).doubleValue(); } catch(NumberFormatException e) { test=1; tx[i].selectAll(); } q[1][i]=db[i]; } for (int i=1; i<=ndata; i=i+1) { da[i][2]=0; } } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { // Data1 parameters theta=60; namb=1.372; db[0]=0; db[1]=4.07996; db[2]=3.20502; db[3]=2.1692; db[4]=2.1692; db[5]=0; db[6]=160; db[7]=0; db[8]=80; db[9]=25; db[10]=75; ndata=16; np=500; lmda=632.99; da[1][0]=24.708; da[1][1]=29.561; // P-A data da[2][0]=19.052; da[2][1]=30.594; da[3][0]=14.164; da[3][1]=32.142; da[4][0]=10.341; da[4][1]=34.574; da[5][0]=8.148; da[5][1]=38.538; da[6][0]=8.608; da[6][1]=45.327; da[7][0]=14.639; da[7][1]=56.898; da[8][0]=37.582; da[8][1]=68.422; da[9][0]=65.106; da[9][1]=59.783; da[10][0]=72.847; da[10][1]=47.007; da[11][0]=73.697; da[11][1]=39.070; da[12][0]=71.540; da[12][1]=34.620; da[13][0]=67.584; da[13][1]=31.931; da[14][0]=62.531; da[14][1]=30.274; da[15][0]=56.571; da[15][1]=29.214; da[16][0]=49.834; da[16][1]=28.604; setnums(); kk=1; repaint(); } if (b2s.equals(tst)) { // Data2 parameters theta=60; namb=1.372; db[0]=0; db[1]=3.637; db[2]=3.334; db[3]=2.2902; db[4]=2.2902; db[5]=0; db[6]=120; db[7]=0; db[8]=80; db[9]=25; db[10]=75; ndata=63; np=500; lmda=632.99; fs="Va3o.txt"; getdata(); setnums(); kk=1; repaint(); } if (b3s.equals(tst)) { // Plot curve if ((kk>0)&&(kk<4)) { getnums(); if (test==0) { kk=2; repaint(); } } } if (b4s.equals(tst)) { // Simplex fit if ((kk>0)&&(kk<4)) { getnums(); if (test==0) { kk=3; repaint(); } } } if (b5s.equals(tst)) { // Print results if (kk==3) { kk=4; repaint(); } } } }