/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // LaplaceSOR.java: Solves Laplace eqn using SOR for convergence import java.io.*; public class LaplaceSOR { static int max = 40; // Number of grid points public static void main(String[] argv) throws IOException, FileNotFoundException { double x, tol, aux, omega, r, pi = 3.1415926535; double p[][] = new double[max][max]; int i, j, iter; PrintWriter w =new PrintWriter( new FileOutputStream("laplR.dat"), true); omega = 1.8; // SOR parameter long timeStart=System.nanoTime(); System.out.println(""+timeStart+""); // Init for (i=0; i 0.000001) && (iter <= 140) ) { tol = 0.0; // x, then y directions for (i = 1; i<(max-1); i++) { for (j = 1; j<(max-1); j++) { // SOR ALGORITHM r = omega * ( p[i][j+1] + p[i][j-1] + p[i+1][j] + p[i-1][j] - 4.0 * p[i][j] ) / 4.0; p[i][j] += r; if ( Math.abs(r) > tol ) tol = Math.abs(r); } iter++; } } long timeFinal=System.nanoTime(); System.out.println(""+timeFinal+""); System.out.println("Nanoseconds="+(timeFinal-timeStart)); for (i = 0; i