/* pde3b_eq.c 3D non equal h boundary value PDE non iterative */ /* u(x,y,z) = x^3 + y^3 +z^3 + 2x^2y + 3xy^2 + 4z^2x + 5y + 6 */ /* du/dx = 3x^2 + 0 + 0 + 4xy + 3y^2 + 4z^2 + 0 + 0 */ /* d^2u/dx^2 = 6x + 0 + 0 + 4y + 0 + 0 + 0 + 0 */ /* du/dy = 0 + 3y^2 + 0 + 2x^2 + 6xy + 0 + 5 + 0 */ /* d^2u/dy^2 = 0 + 6y + 0 + 0 + 6x + 0 + 0 + 0 */ /* du/dz = 0 + 0 +3z^2 + 0 + 0 + 8zx + 0 + 0 */ /* d^2u/dz^2 = 0 + 0 +6z + 0 + 0 + 8x + 0 + 0 */ /* */ /* solve d^2u/dx^2 + d^2u/dy^2 + d^2u/dz^2 = c(x,y,z) */ /* by second order method on cube -1,-1,-1 to 1.2,1,1 */ /* second order numerical difference */ /* d^2u/dx^2 + d^2u/dy^2 + d^2u/dz^2 = */ /* 1/hx^2(u(x-hx,y,z)+u(x+hx,y,z)-2u(x,y,z)) + */ /* 1/hy^2(u(x,y-hy,z)+u(x,y+hy,z)-2u(x,y,z)) + */ /* 1/hz^2(u(x,y,z-hz)+u(x,y,z+hz)-2u(x,y,z)) + O(h^2) */ /* = c(x,y,z) = 12x + 10y - 8z */ /* rewrite to collect u(x,y,z) term */ /* u(x-hx,y,z)/hx^2 + u(x+hx,y,z)/hx^2 + */ /* u(x,y-hy,z)/hy^2 + u(x,y+hy,z)/hy^2 + */ /* u(x,y,z-hz)/hz^2 + u(x,y,z+hz)/hz^2 - */ /* u(x,y,z)/(2/hx^2 + 2/hy^2 + 2/hz^2) = c(x,y,z) */ /* solve system of linear equations for */ /* u(i-1,j,k)/hx^2 + u(i+1,j,k)/hx^2 + */ /* u(i,j-1,k)/hy^2 + u(i,j+1,k)/hy^2 + */ /* u(i,j,k-1)/hz^2 + u(i,j,k+1)/hz^2 - */ /* u(i,j,k)/(2/hx^2 + 2/hy^2 + 2/hz^2)= c(x,y,z) */ /* for i=1,nx-2 j=1,ny-2 k=1,nz-2 while substituting */ /* u(i,j,k) boundary values i=0 or nx-1, j=0 or ny-1, k=0 or nz-1 */ /* using x=xmin+i*hx, y=ymin+j*hz, z=zmin+k*hz */ /* create two dimensional array with */ /* (nx-2)*(ny-2)*(nz-2) rows, one for each equation */ /* and (nx-2)*(ny-2)*(nz-2)+1 columns for variables plus constant */ /* Initialize the matrix, see init_matrix in the source code */ /* Then solve the system of linear equations to get the solution. */ /* using linear subscripts (i-1)+(nx-2)*(j-1)+(nx-2)*(ny-2)*(k-1) */ #include #include #undef abs #define abs(a) ((a)<0.0?(-(a)):(a)) #undef min #define min(a,b) ((a)<(b)?(a):(b)) #undef max #define max(a,b) ((a)>(b)?(a):(b)) static int nx = 11; static int ny = 11; static int nz = 9; static double us[567]; /* solution being computed 9*9*7 */ static double ut[567][568]; /* temporary equations */ /* nx-2 by ny-2 by nz-2 internal, non boundary cells */ static const double xmin = -1.0; static const double ymin = -1.0; static const double zmin = -1.0; static const double xmax = 1.2; static const double ymax = 1.0; static const double zmax = 1.0; static double hx; /* x step = (xmax-xmin)/(nx-1) */ static double hy; /* y step = (ymax-ymin)/(ny-1) */ static double hz; /* z step = (zmax-zmin)/(nz-1) */ static double error; /* sum of absolute exact-computed */ static double avgerror; /* average error */ static double maxerror; /* maximum cell error */ static double u(double x, double y, double z) /* for boundary and check */ { return x*x*x + y*y*y + z*z*z + 2.0*x*x*y + 3.0*x*y*y + 4.0*z*z*x + 5.0*y +6.0; } static double c(double x, double y, double z) /* from solve */ { return 20.0*x + 10.0*y + 6.0*z; } static void check() /* typically not known, instrumentation */ { int i, j, k; double uexact, err; error = 0.0; maxerror = 0.0; for(i=1; imaxerror) maxerror=err; } } } } /* end check */ static void printus() { int i, j, k; printf("\n computed solution\n"); for(k=1; k abs_pivot) { i_pivot = i; pivot = B[row[i]][k]; abs_pivot = abs ( pivot ); } } /* have pivot, interchange row pointers */ hold = row[k]; row[k] = row[i_pivot]; row[i_pivot] = hold; /* check for near singular */ if( abs_pivot < 1.0E-10 ) { for(j=k+1; j