diff --git a/exercise4/wave_2d_parallel.c b/exercise4/wave_2d_parallel.c index 0b14a81..6e3442e 100644 --- a/exercise4/wave_2d_parallel.c +++ b/exercise4/wave_2d_parallel.c @@ -114,23 +114,28 @@ void domain_save(int_t step) { // Neumann (reflective) boundary condition void boundary_condition(void) { // BEGIN: T7 - // Vertical ghost layers (bottom and top) - for (int_t i = 0; i < M; i++) { - U(i, -1) = U(i, 1); // bottom ghost row <- mirror of row 1 - U(i, N) = U(i, N - 2); // top ghost row <- mirror of row N-2 - } - // Horizontal ghost layers (left and right) - for (int_t j = 0; j < N; j++) { - U(-1, j) = U(1, j); // left ghost col <- mirror of col 1 - U(M, j) = U(M - 2, j); // right ghost col <- mirror of col M-2 + if (up == MPI_PROC_NULL) { + // note: -1 to handle corners automatically + for (int_t j = -1; j < local_N + 1; j++) { + U(-1, j) = U(1, j); + } + } + if (down == MPI_PROC_NULL) { + for (int_t j = -1; j < local_N + 1; j++) { + U(local_M, j) = U(local_M - 2, j); + } + } + if (left == MPI_PROC_NULL) { + for (int_t i = -1; i < local_M + 1; i++) { + U(i, -1) = U(i, 1); + } + } + if (right == MPI_PROC_NULL) { + for (int_t i = -1; i < local_M + 1; i++) { + U(i, local_N) = U(i, local_N - 2); + } } - - // Corner ghost cells (use ghost indices) - U(-1, -1) = U(1, 1); // bottom-left - U(-1, N) = U(1, N - 2); // top-left - U(M, -1) = U(M - 2, 1); // bottom-right - U(M, N) = U(M - 2, N - 2); // top-right // END: T7 }