KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Quasicrystal


1 import JSci.maths.*;
2 import JSci.maths.matrices.DoubleTridiagonalMatrix;
3 import JSci.io.*;
4 import java.io.*;
5
6 /**
7 * Phonons in a Quasicrystal
8 * @author Mark Hale
9 */

10 public final class Quasicrystal {
11     private int N;
12         private double mass_m,mass_M;
13     private double eigenvalue[];
14     /**
15     * Instantiate class.
16     */

17     public static void main(String JavaDoc 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     /**
29     * Constructor.
30     * @param n order of matrix
31     * @param m1 mass
32     * @param m2 mass
33     */

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     /**
42     * Calculate the vibrational frequencies.
43     */

44     private void compute() {
45         DoubleTridiagonalMatrix matrix=new DoubleTridiagonalMatrix(N);
46         eigenvalue=new double[N];
47 // Generate sequence of masses m and M
48
double mass[]=fibonacci();
49 // Create matrix
50
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 // Solve eigenproblem
60
try {
61                         eigenvalue=LinearMath.eigenvalueSolveSymmetric(matrix);
62                 } catch (MaximumIterationsExceededException e) {
63                         System.out.println("Too many iterations.");
64                 }
65     }
66     /**
67     * Creates a Fibonacci sequence.
68     * @return an array containing the sequence
69     */

70     private double[] fibonacci() {
71         double array[];
72         java.util.Vector JavaDoc seq=new java.util.Vector JavaDoc(N);
73         Double JavaDoc adult=new Double JavaDoc(mass_M);
74         Double JavaDoc child=new Double JavaDoc(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 JavaDoc)seq.elementAt(i)).doubleValue();
87         }
88         return array;
89     }
90     /**
91     * Log results to disk.
92     */

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 // Read in existing data
102
try {
103                         FileReader in=new FileReader(file);
104                         filebuf=new char[(int)file.length()];
105                         in.read(filebuf);
106                         in.close();
107                 } catch(Exception JavaDoc e) {
108                         System.out.println("No previous data - new file.");
109                 }
110 // Save all to file
111
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 JavaDoc e) {
118                         System.out.println("Failed to save.");
119                 }
120     }
121 }
122
123
Popular Tags