/* * Residual calculation template * Copyright 2019 Anton Khirnov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #if ADD # define ACT add #else #define ACT calc #endif #define JOIN3(a, b, c) a ## _ ## b ## _ ## c #define FUNC3(name, act, stencil) JOIN3(name, act, stencil) #define FUNC(name) FUNC3(name, ACT, STENCIL) static void FUNC(derivatives_calc)(double *dst, const double *u, ptrdiff_t stride) { dst[MG2D_DIFF_COEFF_00] = u[0]; { double val = 0.0; for (int i = -STENCIL; i <= STENCIL; i++) val += fd_coeffs[STENCIL - 1][0][i + STENCIL] * u[i]; dst[MG2D_DIFF_COEFF_10] = val; } { double val = 0.0; for (int i = -STENCIL; i <= STENCIL; i++) val += fd_coeffs[STENCIL - 1][0][i + STENCIL] * u[i * stride]; dst[MG2D_DIFF_COEFF_01] = val; } { double val = 0.0; for (int i = -STENCIL; i <= STENCIL; i++) val += fd_coeffs[STENCIL - 1][1][i + STENCIL] * u[i]; dst[MG2D_DIFF_COEFF_20] = val; } { double val = 0.0; for (int i = -STENCIL; i <= STENCIL; i++) val += fd_coeffs[STENCIL - 1][1][i + STENCIL] * u[i * stride]; dst[MG2D_DIFF_COEFF_02] = val; } { double val = 0.0; for (int j = -STENCIL; j <= STENCIL; j++) { double tmp = 0.0; for (int i = -STENCIL; i <= STENCIL; i++) tmp += fd_coeffs[STENCIL - 1][0][i + STENCIL] * u[j * stride + i]; val += fd_coeffs[STENCIL - 1][0][j + STENCIL] * tmp; } dst[MG2D_DIFF_COEFF_11] = val; } } static void FUNC(residual_line)(size_t linesize, double *dst, double *dst_max, ptrdiff_t stride, const double *u, const double *rhs, const double * const diff_coeffs[MG2D_DIFF_COEFF_NB], double res_mult #if ADD , double u_mult #endif ) { double res_max = 0.0, res_abs; for (size_t i = 0; i < linesize; i++) { double u_vals[MG2D_DIFF_COEFF_NB]; double res; FUNC(derivatives_calc)(u_vals, u + i, stride); res = -rhs[i]; for (int j = 0; j < ARRAY_ELEMS(u_vals); j++) res += u_vals[j] * diff_coeffs[j][i]; dst[i] = res_mult * res #if ADD + u_mult * u[i] #endif ; res_abs = fabs(res); res_max = MAX(res_max, res_abs); } *dst_max = MAX(*dst_max, res_max); } #undef FUNC #undef FUNC2 #undef JOIN2 #undef ACT