import java.io.*; public class Driver { /* Driver routine -- test out the code on a sample problem */ public static void driver(int n, int maxiter, Sparse aMat, Preconditioner pMat) { try { double [1d] b = new double [0:n-1]; double [1d] x = new double [0:n-1]; double [1d] rhist = new double [0:maxiter-1]; double rtol = 1e-3; int i, retval; DataOutputStream rhistOutFile = new DataOutputStream(new FileOutputStream("rhist.out")); DataOutputStream xOutFile = new DataOutputStream(new FileOutputStream("x.out")); b.set(1); retval = CG.precondCG(aMat, pMat, b, x, rtol, n, rhist, maxiter); for (i = 0; i < n; i++) { xOutFile.writeBytes(Double.toString(x[i]) + "\n"); } int lastIter; if (retval < 0) { lastIter = maxiter; System.out.println("Iteration failed to converge!\n"); } else { lastIter = retval; System.out.println("Converged after " + retval + " iterations."); } for (i = 0; i < lastIter; ++i) { rhistOutFile.writeBytes(Double.toString(rhist[i])+"\n"); } rhistOutFile.close(); xOutFile.close(); } catch (IOException e) { System.out.println("Unable to open or use output files: " + e); return; } } public static void main(String [] args) { int maxIter = 500; if (args.length <= 0 || Character.isDigit(args[0].charAt(0))) { int n; if (args.length == 0) { n = 10; } else { n = Integer.parseInt(args[0]); } System.out.println("Using default 1-d Poisson on a " + n + " point mesh.\n"); driver(n, maxIter, new Poisson1D(n), new IdentityPrecond()); System.out.println("Using 1-d Poisson on " + n + " point mesh with precond\n"); driver(n, maxIter, new Poisson1D(n), new Poisson1DBlockJacobiPrecond(n)); } else { CSR aMat; try { aMat = MMIO.CSRLoad(args[0]); } catch (IOException e) { System.out.println(e); return; } System.out.println("Using problem " + args[0]); System.out.println("Vanilla CG: "); driver(aMat.dim(), maxIter, aMat, new IdentityPrecond()); } } }