// pde22_eq.java 2D uniform grid boundary value PDE // known solution to test method // u(x,y) = 2^16 *x^4 * (1-x)^4 * y^4 * (1-y)^4; zero on boundary // hump, max 1.0 at (0.5,0.5) // differential equation to solve // d^2u/dx^2 + d^2u/dy^2 = c(x,y) // Uxx(x,y) = 2^16 * ( // 12*x^2 * (1-x)^4 * y^4 * (1-y)^4 - // 32*x^3 * (1-x)^3 * y^4 * (1-y)^4 + // 12*x^4 * (1-x)^2 * y^4 * (1-y)^4) // Uyy(x,y) = 2^16 * ( // 12*x^4 * (1-x)^4 * y^2 * (1-y)^4 - // 32*x^4 * (1-x)^4 * y^3 * (1-y)^3 + // 12*x^4 * (1-x)^4 * y^4 * (1-y)^2) // c(x,y) = Uxx(x,y) + Uyy(x,y); // // uniform grid on rectangle 0 to 1, 0 to 1 // set up and solve system of (nx-2)*(ny-2) linear equations // // uses nuderiv.java // uses simeq.java import java.text.*; import java.io.*; class pde22_eq { int nx = 10; // includes boundary int ny = 10; // includes boundary double xg[] = new double[nx]; double yg[] = new double[ny]; double cxx[] = new double[nx]; // nuderiv coefficients double cyy[] = new double[ny]; // nx-2 by ny-2 internal, non boundary cells int neqn = (nx-2)*(ny-2); int cs = neqn; double us[] = new double[neqn]; // solution being computed double ut[][] = new double[neqn][neqn+1]; // equations incl RHS double xmin = 0.0; double xmax = 1.0; double ymin = 0.0; double ymax = 1.0; double hx, hy; double error; // sum of absolute exact-computed double avgerror; // average error double maxerror; // maximum cell error double time_start; double time_now; double time_last; DecimalFormat fe = new DecimalFormat("0.00E00"); DecimalFormat f6 = new DecimalFormat("0.00000"); pde22_eq() { System.out.println("pde22_eq.java running"); System.out.println("differential equation to solve"); System.out.println("d^2u/dx^2 + d^2u/dy^2 = c(x,y)"); System.out.println("Uxx(x,y) = 2^16 * ("); System.out.println(" 12*x^2 * (1-x)^4 * y^4 * (1-y)^4 -"); System.out.println(" 32*x^3 * (1-x)^3 * y^4 * (1-y)^4 +"); System.out.println(" 12*x^4 * (1-x)^2 * y^4 * (1-y)^4 )"); System.out.println("Uyy(x,y) = 2^16 * ("); System.out.println(" 12*x^4 * (1-x)^4 * y^2 * (1-y)^4 -"); System.out.println(" 32*x^4 * (1-x)^4 * y^3 * (1-y)^3 +"); System.out.println(" 12*x^4 * (1-x)^4 * y^4 * (1-y)^2 )"); System.out.println("c(x,y) = Uxx(x,y) + Uyy(x,y);"); System.out.println("uniform grid on rectangle 0,1 to 0,1"); System.out.println("known Solution, for testing method"); System.out.println("u(x,y) = 2^16*x^4*(1-x)^4*y^4*(1-y)4"); System.out.println("zero on boundary, hump 1.0 at x=0.5, y=0.5 "); System.out.println(" "); time_start = (double)System.currentTimeMillis()/1000.0; time_last = time_start; hx = (xmax-xmin)/(double)(nx-1); hy = (ymax-ymin)/(double)(ny-1); for(int i=0; imaxerror) maxerror=err; } } } // end check void check_rows() // only for(checking { // row is in ut solution coordinates int row, col; double sum; for(int i=1; i0.001) { System.out.println("row i="+i+", j="+j+", is bad err="+sum); } } // j } //i } // end check_rows void print() // typically not known { double error = 0.0; double max_error = 0.0; double avg_error = 0.0; System.out.println("exact solution u, computed us, error"); for(int i=1; imax_error) max_error = Math.abs(error); System.out.println("xg["+i+"]="+f6.format(xg[i])+ ", yg["+j+"]="+f6.format(yg[j])+ ", u="+f6.format(u(xg[i],yg[j]))+ ", us="+f6.format(us[S(i,j)])+ ", err="+fe.format(error)); } } System.out.println("max_error="+max_error+", avg_error="+ avg_error/(double)neqn); System.out.println(" "); } // end print void printB() { System.out.println("matrix B includes RHS"); for(int i=1; i smaxerr) smaxerr = Math.abs(err); } // ii y } // i x System.out.println("check soln against PDE max error="+smaxerr); } // end Check_Soln // write_soln2 for gnuplot void write_soln2(double Ux[]) { try { FileWriter fout = new FileWriter("pde22_eq_java.dat"); BufferedWriter fileout = new BufferedWriter(fout); System.out.println("writing pde22_eq_java.dat"); for(int i=0; i