summaryrefslogtreecommitdiff
path: root/residual_calc_template.c
blob: dcf7eb4fe31add671eacc225185e81da5614b4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Residual calculation template
 * Copyright 2019 Anton Khirnov <anton@khirnov.net>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#define JOIN2(a, b) a ## _ ## b
#define FUNC2(name, stencil) JOIN2(name, stencil)
#define FUNC(name) FUNC2(name, STENCIL)

static void
FUNC(derivatives_calc)(double *dst, const double *u, const double *fd_factors, 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 * fd_factors[MG2D_DIFF_COEFF_10];
    }
    {
        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 * fd_factors[MG2D_DIFF_COEFF_01];
    }
    {
        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 * fd_factors[MG2D_DIFF_COEFF_20];
    }
    {
        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 * fd_factors[MG2D_DIFF_COEFF_02];
    }
    {
        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 * fd_factors[MG2D_DIFF_COEFF_11];
    }
}
static void
FUNC(residual_calc_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],
                         const double *fd_factors)
{
    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, fd_factors, 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;

        res_abs = fabs(res);
        res_max = MAX(res_max, res_abs);
    }

    *dst_max = MAX(*dst_max, res_max);
}

#undef FUNC
#undef FUNC2
#undef JOIN2