// pde23a_eq.java 3D uniform grid boundary value PDE // known solution to test method // U(x,y,z) = x*x*x+y*y*y+z*z*z // // differential equation to solve // Uxx + Uyy + Uyzz = c(x,y,z) // Ux(x,y,z) = 3 x*x // Uy(x,y,z) = 3 y*y // Uz(x,y,z) = 3 z*z // Uxx(x,y,z) = 6 x // Uyy(x,y,z) = 6 y // Uzz(x,y,z) = 6 z // c(x,y,z) = 6 x + 6 y + 6 z // // uniform grid on cube -1 to 1, -1 to 1, -1 to 1 // set up and solve system of (nx-2)*(ny-2)*(nz-2) linear equations // // uses nuderiv.java // uses simeq.java import java.text.*; import java.io.*; class pde23a_eq { int nx = 5; // includes boundary int ny = 5; // includes boundary int nz = 5; // includes boundary double xg[] = new double[nx]; double yg[] = new double[ny]; double zg[] = new double[nz]; double cxx[] = new double[nx]; double cyy[] = new double[ny]; double czz[] = new double[nz]; // nx-2 by ny-2 by n-2 internal, non boundary cells int neqn = (nx-2)*(ny-2)*(nz-2); int cs = neqn; double us[] = new double[neqn]; // solution being computed double ut[][] = new double[neqn][neqn+1]; // equations incl RHS double xmin = -1.0; double xmax = 1.0; double ymin = -1.0; double ymax = 1.0; double zmin = -1.0; double zmax = 1.0; double hx, hy, hz; 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"); pde23a_eq() { System.out.println("pde23a_eq.java running"); System.out.println("differential equation to solve"); System.out.println("d^2U/dx^2 + d^2U/dy^2 + d^2U/dz^2 = c(x,y,z)"); System.out.println("Ux(x,y,z) = 3 x*x for testing"); System.out.println("Uy(x,y,z) = 3 y*y"); System.out.println("Uz(x,y,z) = 3 z*z"); System.out.println("Uxx(x,y,z) = 6 x"); System.out.println("Uyy(x,y,z) = 6 y"); System.out.println("Uzz(x,y,z) = 6 z"); System.out.println("c(x,y,z) = 6 x + 6 y + 6 z given "); System.out.println("uniform grid on cube -1,1 to -1,1 to -1,1"); System.out.println("known Solution, for testing method"); System.out.println("U(x,y,z) = x*x*x + y*y*y + z*z*z"); System.out.println(" and U(x,y,z) in program is for boundary values "); 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); hz = (zmax-zmin)/(double)(nz-1); for(int i=0; imaxerror) maxerror=err; } // end k } // end j } // end i System.out.println("check against known solution, maxerror="+maxerror); System.out.println(" "); } // end check void check_rows() // only for(checking { // row is in ut solution coordinates int row, col; double sum; System.out.println("check_rows"); for(int i=1; i0.001) { System.out.println("row i="+i+", j="+j+", is bad err="+sum); } } // end k } // end j } // end 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])+ ", zg["+k+"]="+f6.format(zg[k])+ ", u="+f6.format(u(xg[i],yg[j],zg[k]))+ ", us="+f6.format(us[S(i,j,k)])+ ", err="+fe.format(error)); } // end k } // end j } // end i 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); } // k z } // j y } // i x System.out.println("check soln against PDE max error="+smaxerr); } // end Check_Soln // write_soln3 for gnuplot void write_soln3(double Ux[]) { try { FileWriter fout = new FileWriter("pde23a_eq_java.dat"); BufferedWriter fileout = new BufferedWriter(fout); System.out.println("writing pde23a_eq_java.dat"); for(int i=0; i