1 import JSci.maths.*; 2 import JSci.maths.matrices.DoubleTridiagonalMatrix; 3 import JSci.io.*; 4 import java.io.*; 5 6 10 public final class Quasicrystal { 11 private int N; 12 private double mass_m,mass_M; 13 private double eigenvalue[]; 14 17 public static void main(String arg[]) { 18 if(arg.length==3) { 19 int n=Integer.valueOf(arg[0]).intValue(); 20 double m1=Double.valueOf(arg[1]).doubleValue(); 21 double m2=Double.valueOf(arg[2]).doubleValue(); 22 new Quasicrystal(n,m1,m2); 23 } else { 24 System.out.println("Need to specify command line arguments:"); 25 System.out.println("<length of chain> <mass of atom 1> <mass of atom 2>"); 26 } 27 } 28 34 public Quasicrystal(int n,double m1,double m2) { 35 N=n; 36 mass_M=m1; 37 mass_m=m2; 38 compute(); 39 saveResults(); 40 } 41 44 private void compute() { 45 DoubleTridiagonalMatrix matrix=new DoubleTridiagonalMatrix(N); 46 eigenvalue=new double[N]; 47 double mass[]=fibonacci(); 49 matrix.setElement(0,0,2.0/mass[0]); 51 matrix.setElement(0,1,-1.0/Math.sqrt(mass[0]*mass[1])); 52 for(int i=1;i<N-1;i++) { 53 matrix.setElement(i,i-1,-1.0/Math.sqrt(mass[i]*mass[i-1])); 54 matrix.setElement(i,i,2.0/mass[i]); 55 matrix.setElement(i,i+1,-1.0/Math.sqrt(mass[i]*mass[i+1])); 56 } 57 matrix.setElement(N-1,N-1,2.0/mass[N-1]); 58 matrix.setElement(N-1,N-2,-1.0/Math.sqrt(mass[N-1]*mass[N-2])); 59 try { 61 eigenvalue=LinearMath.eigenvalueSolveSymmetric(matrix); 62 } catch (MaximumIterationsExceededException e) { 63 System.out.println("Too many iterations."); 64 } 65 } 66 70 private double[] fibonacci() { 71 double array[]; 72 java.util.Vector seq=new java.util.Vector (N); 73 Double adult=new Double (mass_M); 74 Double child=new Double (mass_m); 75 seq.addElement(child); 76 while(seq.size()<N) { 77 for(int i=0;i<seq.size();i++) { 78 if(seq.elementAt(i).equals(adult)) 79 seq.insertElementAt(child,++i); 80 else if(seq.elementAt(i).equals(child)) 81 seq.setElementAt(adult,i); 82 } 83 } 84 array=new double[N]; 85 for(int i=0;i<array.length;i++) { 86 array[i]=((Double )seq.elementAt(i)).doubleValue(); 87 } 88 return array; 89 } 90 93 private void saveResults() { 94 File file=new File("spectrum.txt"); 95 char filebuf[]=null; 96 double data[][]=new double[1][N+1]; 97 data[0][0]=mass_m/mass_M; 98 for(int i=1;i<data[0].length;i++) { 99 data[0][i]=Math.sqrt(eigenvalue[i-1]); 100 } 101 try { 103 FileReader in=new FileReader(file); 104 filebuf=new char[(int)file.length()]; 105 in.read(filebuf); 106 in.close(); 107 } catch(Exception e) { 108 System.out.println("No previous data - new file."); 109 } 110 try { 112 TextWriter out=new TextWriter(file,','); 113 if(filebuf!=null) 114 out.write(filebuf); 115 out.write(data); 116 out.close(); 117 } catch(Exception e) { 118 System.out.println("Failed to save."); 119 } 120 } 121 } 122 123 | Popular Tags |