#include #include #include #include #define M_PI 3.14159265358979323846264338327 #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) #define KEISI 0 double u0(double x, double y) { double u=0.0; switch ( KEISI ) { case 1: if ( x >= 0.25 && x <=0.5 && y>=0.5 && y<=0.75 ) u=1.0; break; case 2: u=x*(1-x)*y*(1-y); break; default: u=sin(M_PI*x)*sin(M_PI*y); } return u; } // // exact solution of the heat equation (if you happen to know it // for the given u0): double uexact(double x, double y, double t) { return exp(-2*M_PI*M_PI*t)*sin(M_PI*x)*sin(M_PI*y); } void output_solution(int n, double u[n][n]) { int i, j; double h; FILE *fp; h=1.0/(n-1); fp=fopen("u.dat","w"); // for plotting with gnuplot for ( i=0; i 0.25*h*h ) { if ( me==0 ) printf("Scheme is not stable. dt=%.3le, should be <= %.3le\n",dt,0.25*h*h); MPI_Finalize(); exit(1); } dth2=dt/(h*h); c1 = 1-4*dth2; c2 = dth2; // if (me==0) t = MPI_Wtime(); // // Computational domain is divided between processors using 2D domain // decomposition. // // Create virtual p x p Cartesian processor grid: // dims[0]=p; dims[1]=p; MPI_Cart_create(MPI_COMM_WORLD,2,dims,periods,reorder,&comm_2d); MPI_Cart_coords(comm_2d,me,2,coords); nloc = N/p; is = coords[0]*nloc; ie = (coords[0]+1)*nloc-1; js = coords[1]*nloc; je = (coords[1]+1)*nloc-1; if ( debug ) printf(" me=%d would take care of nodes (%d,%d)...(%d,%d)\n",me,is,js,ie,je); double sbuf[nloc], rbuf[nloc]; double u[nloc+2][nloc+2], unew[nloc][nloc], buf2[nloc][nloc]; // init_zero2d(nloc,unew); for ( int i=0; i= 0 ) { // exchange data with neighbour above for ( int j=0; j= 0 ) { // exchange data with neighbour below for ( int j=0; j= 0 ) { // exchange data with neighbour left for ( int i=0; i= 0 ) { // exchange data with neighbour right for ( int i=0; i