aboutsummaryrefslogtreecommitdiff
path: root/mg2d_test.c
blob: 9b559635c0734bf6737c5b459846d7c869cf9d3c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "mg2d.h"

#define MAXITER 64
#define TOL 1e-9

#if 1
static double sol(double x, double y)
{
    return sin(M_PI * x) * sin(M_PI * y);
}
static double sol_dx(double x, double y)
{
    return M_PI * cos(M_PI * x) * sin(M_PI * y);
}
static double sol_dy(double x, double y)
{
    return M_PI * sin(M_PI * x) * cos(M_PI * y);
}
static double sol_dxx(double x, double y)
{
    return -M_PI * M_PI * sol(x, y);
}
static double sol_dyy(double x, double y)
{
    return -M_PI * M_PI * sol(x, y);
}
static double sol_dxy(double x, double y)
{
    return M_PI * M_PI * cos(M_PI * x) * cos(M_PI * y);
}
#endif

int main(int argc, char **argv)
{
    MG2DContext *ctx;
    long int gridsize;
    int ret = 0;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s <N>\n", argv[0]);
        return 1;
    }
    gridsize = strtol(argv[1], NULL, 0);
    if (gridsize <= 0) {
        fprintf(stderr, "Invalid parameters: %ld\n", gridsize);
        return 1;
    }

    ctx = mg2d_solver_alloc(gridsize);
    if (!ctx) {
        fprintf(stderr, "Error allocating the solver context\n");
        return 1;
    }

    ctx->step[0] = 1.0 / (gridsize - 1);
    ctx->step[1] = 1.0 / (gridsize - 1);

    ctx->fd_stencil = 2;

    ctx->maxiter = MAXITER;
    ctx->nb_relax_pre  = 2;
    ctx->nb_cycles     = 1;
    ctx->nb_relax_post = 2;
    ctx->tol = TOL;
    ctx->nb_threads = 1;

    {
        MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0L];

        bnd->type = MG2D_BC_TYPE_FIXVAL;

        memset(bnd->val, 0, gridsize * sizeof(*bnd->val));

        for (int j = 1; j < ctx->fd_stencil; j++) {
            double *dst = bnd->val + j * bnd->val_stride;

            for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
                dst[k] = sol(-j * ctx->step[0], k * ctx->step[1]);
        }
    }
    {
        MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_0U];

        bnd->type = MG2D_BC_TYPE_FIXVAL;

        memset(bnd->val, 0, gridsize * sizeof(*bnd->val));

        for (int j = 1; j < ctx->fd_stencil; j++) {
            double *dst = bnd->val + j * bnd->val_stride;

            for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
                dst[k] = sol((gridsize - 1 + j) * ctx->step[0], k * ctx->step[1]);
        }
    }
    {
        MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1L];

        bnd->type = MG2D_BC_TYPE_FIXVAL;

        memset(bnd->val, 0, gridsize * sizeof(*bnd->val));

        for (int j = 1; j < ctx->fd_stencil; j++) {
            double *dst = bnd->val + j * bnd->val_stride;

            for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
                dst[k] = sol(k * ctx->step[0], -j * ctx->step[1]);
        }
    }
    {
        MG2DBoundary *bnd = ctx->boundaries[MG2D_BOUNDARY_1U];

        bnd->type = MG2D_BC_TYPE_FIXVAL;

        memset(bnd->val, 0, gridsize * sizeof(*bnd->val));

        for (int j = 1; j < ctx->fd_stencil; j++) {
            double *dst = bnd->val + j * bnd->val_stride;

            for (ptrdiff_t k = -j; k < (ptrdiff_t)ctx->domain_size + j; k++)
                dst[k] = sol(k * ctx->step[0], (gridsize - 1 + j) * ctx->step[1]);
        }
    }


    for (size_t y = 0; y < ctx->domain_size; y++) {
        const double y_coord = y * ctx->step[1];

        memset(ctx->u + y * ctx->u_stride, 0, sizeof(*ctx->u) * ctx->domain_size);
        //memset(ctx->rhs + y * ctx->rhs_stride, 0, sizeof(*ctx->rhs) * ctx->domain_size[0]);

        for (size_t x = 0; x < ctx->domain_size; x++) {
            const double x_coord = x * ctx->step[0];

            ctx->diff_coeffs[MG2D_DIFF_COEFF_02][ctx->diff_coeffs_stride * y + x] = 1.0;
            ctx->diff_coeffs[MG2D_DIFF_COEFF_20][ctx->diff_coeffs_stride * y + x] = 1.0;
            ctx->diff_coeffs[MG2D_DIFF_COEFF_11][ctx->diff_coeffs_stride * y + x] = 1.0;
            //ctx->diff_coeffs[ELL_RELAX_DIFF_COEFF_00][ctx->diff_coeffs_stride * y + x] = 2.0 * M_PI;

            ctx->rhs[y * ctx->rhs_stride + x] = sol_dxx(x_coord, y_coord) + sol_dyy(x_coord, y_coord) + sol_dxy(x_coord, y_coord);
        }
        memset(ctx->diff_coeffs[MG2D_DIFF_COEFF_00] + y * ctx->diff_coeffs_stride, 0,
               sizeof(*ctx->diff_coeffs[0]) * ctx->domain_size);
        memset(ctx->diff_coeffs[MG2D_DIFF_COEFF_01] + y * ctx->diff_coeffs_stride, 0,
               sizeof(*ctx->diff_coeffs[0]) * ctx->domain_size);
        memset(ctx->diff_coeffs[MG2D_DIFF_COEFF_10] + y * ctx->diff_coeffs_stride, 0,
               sizeof(*ctx->diff_coeffs[0]) * ctx->domain_size);
    }

    ret = mg2d_solve(ctx);
    if (ret < 0) {
        fprintf(stderr, "Error solving the equation\n");
        ret = 1;
        goto fail;
    }

    mg2d_print_stats(ctx, NULL);

    {
        double max_err = 0.0;

        for (size_t y = 0; y < ctx->domain_size; y++) {
            const double y_coord = y * ctx->step[1];

            for (size_t x = 0; x < ctx->domain_size; x++) {
                const double x_coord = x * ctx->step[0];
                double err = fabs(ctx->u[y * ctx->u_stride + x] - sol(x_coord, y_coord));
                if (err > max_err)
                    max_err = err;
            }
        }
        fprintf(stderr, "max(|solution - exact|): %g\n", max_err);
        fprintf(stdout, "%ld %g\n", gridsize, max_err);
    }

fail:
    mg2d_solver_free(&ctx);
    return ret;
}