/* *********************************************************} * Tune.java: a matrix algebra program to be tuned for performace c c converted from tune.f in "Computational Physics" by Landau and Paez c mod to start indices at 0 not 1 c c written by: Paul J Fink and Rubin Landau c code copyrighted by RH Landau ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */ public class Tune_Slow { public static void main(String[] argv) { final int Ldim = 100; int i, j, iter = 0; double [][] ham = new double [Ldim] [Ldim]; double [] coef = new double [Ldim]; double [] sigma = new double [Ldim]; double err, ener, ovlp, step=0.,time, dummy=2.; // Store initial time time = System.currentTimeMillis(); // // set up Hamiltonian & starting vector for ( i = 0; i <= Ldim-1; i++) { for ( j=0; j <= Ldim-1; j++) { if (Math.abs(j-i) >10) {ham[j][i] = 0.0;} else ham[j][i] = Math.pow(0.3, Math.abs(j-i)); } ham[i][i] = i+1 ; // Diagnol H elements coef[i] = 0.0 ; //Eigenvector elements } // // start iterating towards the solution coef[1] = 1.0 ; // Guess 1st eigenvector = 1, 0, ...0 err = 1.0 ; // Arbitrary err to begin System.out.println ("Iteration #\tEnergy\t\tERR\t\tTotal Time "); // iterate until converge or 15 tries while (iter <15 && err > 1.0e-6) {iter = iter + 1 ; // // compute current energy \& norm, \& normalize ener = 0.0 ; ovlp = 0.0 ; for ( i= 0; i <= Ldim-1; i++) // outter loop { ovlp = ovlp + coef[i]*coef[i] ; sigma[i] = 0.0; for ( j= 0; j <= Ldim-1; j++) // inner loop { sigma[i] = sigma[i] + coef[j]*ham[j][i];} ener = ener + coef[i]*sigma[i] ; for ( int k= 0; k <= Ldim*Ldim; k++) // do nothing loop {dummy = Math.pow(k,Math.pow(1,j));} } ener = ener/ovlp ; for ( i = 1; i<= Ldim-1; i++) //Normalize, this starts at 1 { coef[i] = coef[i]/Math.sqrt(ovlp) ; sigma[i] = sigma[i]/Math.sqrt(ovlp) ; } // compute update and error // err = 0.0 ; for ( i = 2; i <= Ldim-1; i++) { step = (sigma[i] - ener*coef[i])/(ener-ham[i][i]) ; coef[i] = coef[i] + step ; err = err + Math.pow(step,2) ; } err = Math.sqrt(err) ; System.out.println ("\t#"+iter+"\t"+ener+ "\t" +err+ "\t"+(System.currentTimeMillis() -time)/1000); } // output elapsed time time = (System.currentTimeMillis() - time)/1000; System.out.println("time= "+ time + "s"); } }