/* pde2sin_sparse.c 2d uniform grid boundary value PDE */ /* known solution for testing method */ /* u(x,y) = sin(x-y) */ /* */ /* differential equation to solve */ /* du/dx + du/dy=0 */ /* uniform grid on rectangle 0 to 2Pi, 0 to 2Pi */ /* set up and solve system of linear equations */ #include #include #include #include "deriv.h" #include "sparse.h" #undef abs #define abs(a) ((a)<0?(-(a)):(a)) /* do NOT make nx=ny, this creates a singular matrix */ /* including boundary at 0 and nx-1, 0 and ny-1 */ #define nx 13 #define ny 12 #define neqn ((nx-2)*(ny-2)) #define cs neqn static double pi = 3.1415926535897932384626433832795028841971; static double xmin, xmax, ymin, ymax, hx, hy; static double xg[nx]; static double yg[ny]; /* nx-2 by ny-2 internal, boundary cells */ static double cx[nx]; static double cy[ny]; static double coefdx, coefdy; static double us[neqn]; /* solution vector */ /* static double ut[neqn][neqn+1]; includes RHS, now B,eqn,col */ static struct sparse B; static double u(double x, double y) { /* solution for boundary and optionalcheck */ return sin(x-y); } /* end u */ static double c(double x, double y) { /* RHS of differential equation to solve */ return 0.0; } /* end c */ static int S(int i, int j) { if(i<1 || i>nx-1 || j<1 || j>ny-1) printf("BAD S i=%2d, j=%2d\n", i, j); return (i-1)*(ny-2)+(j-1); /* equation index into us and B */ } /* end S */ static void print() { int i, j; double error = 0.0; double max_error = 0.0; double avg_error = 0.0; double val; printf("\n"); printf("exact solution u, computed us, error\n"); for(i=0; i max_error) max_error = error; printf("us=%g, ", val); printf("err=%g\n", error); } } printf("avg_error=%g, max_error=%g\n", avg_error/(double)(neqn), max_error); } /* end print */ static void check_row(int row) /* only for checking */ { /* row is in B solution coordinates */ int i, j, ii; double sum; sum = 0.0; for(i=1; i0.001) printf("BAD row=%4d, err=%g\n", row, sum); } /* end check_row */ static void init_matrix() { int i, j, ii, jj; int k, kj, ik; /* cs is RHS, constant column subscript */ /* zero internal cells, not with sparse */ for(i=1; i smaxerr) smaxerr = abs(err); } /* ii Y */ } /* i X */ printf("check_soln max error=%g\n", smaxerr); } /* end Check_Soln */ int main(int argc, char *argv[]) { int i, j; /* pde2sin_sparse */ printf("pde2sin_sparse.c running\n"); printf("differential equation to solve\n"); printf("du/dx + du/dy = c(x,y)\n"); printf("c(x,y)=0\n"); printf("uniform grid on rectangle 0,2Pi to 0,2Pi\n"); printf("known Solution, for testing method\n"); printf("u(x,y) = sin(x-y)\n"); printf("do not make nx=ny, this creates a singular matrix\n"); xmin = 0.0; xmax = 2.0*pi; ymin = 0.0; ymax = 2.0*pi; hx = (xmax-xmin)/(double)(nx-1); hy = (ymax-ymin)/(double)(ny-1); for(i=0; i