aboutsummaryrefslogtreecommitdiff
path: root/residual_calc_template.c
blob: 779ac24e04369393d1161e06681f01027fe838a2 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * 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/>.
 */

#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