summaryrefslogtreecommitdiff
path: root/mg2d.h
blob: 0c12f22aa6201606d90cd440003fa4fd6c9ffc5f (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Multigrid solver for a 2nd order 2D linear PDE.
 * Copyright 2018 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/>.
 */

#ifndef MG2D_H
#define MG2D_H

#include <stddef.h>
#include <stdint.h>

#include <mpi.h>

#include "mg2d_boundary.h"
#include "mg2d_constants.h"

typedef struct MG2DInternal MG2DInternal;

/**
 * Flags for diff coeffs boundary specifications.
 */
enum MG2DDCFlag {
    /**
     * The coefficients have a discontinuity at the boundary, i.e.
     * C(x, y) = C0(x, y) + D(x, y),
     * where C is the resulting value used for the solve, C0 is continuous
     * everywhere, and D is zero everywhere except along the boundary. D is
     * assumed to be continuous along the boundary.
     *
     * The values of D should be stored in MG2DDCBoundarySpec.val
     */
    MG2D_DC_FLAG_DISCONT = (1 << 0),
    /**
     * The coefficients have a first order pole at the boundary, i.e.
     * C(x, y) ยท (x_i - x_i^0)                  (1)
     * is continuous, where C is the actual value of the coefficient, x_i is the
     * coordinate which is constant along the boundary and x_i^0 is the value of
     * that coordinate along the boundary.
     *
     * The value of (1) along the boundary should be stored in
     * MG2DDCBoundarySpec.val.
     */
    MG2D_DC_FLAG_POLE    = (1 << 1),
};

/**
 * Specification of the behaviour of differential equation coefficients on a
 * boundary.
 */
typedef struct MG2DDCBoundarySpec {
    /**
     * A combination of MG2DDCFlag
     */
    int flags;
    /**
     * Value determined by flags. Need not be filled if the flags are zero.
     */
    double *val;
} MG2DDCBoundarySpec;

/**
 * Specification of a single coefficient of the PDE.
 */
typedef struct MG2DDiffCoeffs {
    /**
     * Values of the coefficient at the grid points. Values corresponding to
     * successive x locations are contiguous in memory.
     */
    double         *data;
    /**
     * Number of elements between successive y locations in data.
     */
    ptrdiff_t       stride;

    /**
     * Behaviour of the coefficient on the boundaries.
     */
    MG2DDCBoundarySpec boundaries[4];
} MG2DDiffCoeffs;

typedef struct MG2DContext {
    /**
     * Solver private data, not to be accessed in any way by the caller.
     */
    MG2DInternal *priv;

    /**
     * A callback that will be used to print diagnostic messages.
     *
     * The default implementation prints to stderr.
     *
     * May be set to NULL to disable logging.
     */
    void (*log_callback)(const struct MG2DContext *ctx, int level,
                         const char *fmt, va_list);

    /**
     * Arbitrary user data, e.g. to be used by the log callback.
     */
    void *opaque;

    /**
     * Number of grid points in each direction, set by mg2d_solver_alloc().
     * Read-only for the caller.
     */
    size_t domain_size;

    /**
     * Distance between the neighbouring grid points.
     * Must be set by the caller.
     */
    double step[2];

    /**
     * Order of the finite difference operators used for approximating derivatives.
     * Must be set by the caller.
     */
    size_t fd_stencil;

    /**
     * Boundary specification, indexed by MG2DBoundaryLoc.
     * To be filled by the caller.
     */
    MG2DBoundary *boundaries[4];

    /**
     * Maximum number of solver iterations.
     */
    unsigned int maxiter;

    /**
     * Value of the residual at which to consider the equation solved.
     */
    double tol;

    /**
     * Number of cycles on each subgrid.
     */
    unsigned int nb_cycles;

    /**
     * Number of relaxation steps before and after each coarser-grid solve.
     */
    unsigned int nb_relax_pre;
    unsigned int nb_relax_post;

    /**
     * Values of the unknown function.
     *
     * Allocated and initialized to zero by the solver in mg2d_solver_alloc(),
     * owned by the solver.
     * May be filled by the caller before solving to set the initial guess.
     * Afterwards updated in mg2d_solve().
     */
    double   *u;
    /**
     * Distance between neighbouring gridpoints along coord1.
     */
    ptrdiff_t u_stride;

    /**
     * Values of the right-hand side.
     *
     * Allocated by the solver in mg2d_solver_alloc(), owned by the solver.
     * Must be filled by the caller before solving.
     */
    double    *rhs;
    /**
     * Distance between neighbouring gridpoints along coord1.
     */
    ptrdiff_t  rhs_stride;

    /**
     * Coefficients C_{*} that define the differential equation.
     *
     * Allocated by the solver in mg2d_solver_alloc(), owned by the solver.
     * Must be filled by the caller before solving.
     */
    MG2DDiffCoeffs *diff_coeffs[MG2D_DIFF_COEFF_NB];

    /**
     * Number of threads to use for processing.
     *
     * Set to 0 to autodetect. Defaults to 1.
     */
    unsigned int nb_threads;

    /**
     * Time-stepping factor to use for relaxation.
     */
    double cfl_factor;

    /**
     * Maximum level of messages printed by the default logging callback. Has no
     * effect when log_callback is overridden.
     */
    enum MG2DLogLevel log_level;

    /**
     * Maximum number of refinement levels. May only be modified by the caller
     * before the first call to mg2d_solve().
     */
    unsigned int max_levels;

    /**
     * Maximum size (along one dimensions) of a refinement level on which an
     * exact solve is performed.
     */
    size_t max_exact_size;

    /**
     * Indices of the lower left corner of the local domain in the full
     * computational grid, for distributed solvers. Always { 0, 0 } for
     * single-component solvers.
     *
     * Set by mg2d_solver_alloc[_mpi]().
     */
    ptrdiff_t local_start[2];

    /**
     * Number of points in the local component in each direction. Equal to
     * { domain_size, domain_size } for single-component solvers.
     *
     * Set by mg2d_solver_alloc[_mpi]().
     */
    size_t    local_size[2];

    double residual_max;

    /**
     * Use adaptive stepping for relaxation.
     */
    int adaptive_step;
} MG2DContext;

/**
 * Allocate the solver for the given domain size.
 *
 * @return The solver context on success, NULL on failure.
 */
MG2DContext *mg2d_solver_alloc(size_t domain_size);

/**
 * Allocate a solver component in a multi-component MPI-based solve.
 *
 * @param comm The MPI communicator used to communicate with the other
 *             components.
 * @param local_start Indices of this component's lower left corner in the full
 *                    computational domain.
 * @param local_size Size of this component in each direction.
 *
 * @return The solver context on success, NULL on failure.
 */
MG2DContext *mg2d_solver_alloc_mpi(MPI_Comm comm, const size_t local_start[2],
                                   const size_t local_size[2]);
/**
 * Solve the equation, after all the required fields have been filled by the
 * caller.
 *
 * This function may be called more than once.
 *
 * @return 0 on success, a negative error code on failure.
 */
int mg2d_solve(MG2DContext *ctx);
/**
 * Free the solver and write NULL to the provided pointer.
 */
void mg2d_solver_free(MG2DContext **ctx);

/**
 * Print the solver timing statistics using the logging callback.
 *
 * @param prefix the string prepended to each output line, may be NULL to mean
 *               none
 */
void mg2d_print_stats(MG2DContext *ctx, const char *prefix);

/**
 * @return maximum supported value of fd_stencil.
 */
unsigned int mg2d_max_fd_stencil(void);

int mg2d_init_guess(MG2DContext *ctx, const double *src,
                    ptrdiff_t src_stride,
                    const ptrdiff_t src_start[2],
                    const size_t src_size[2],
                    const double src_step[2]);

int mg2d_interp_solution(MG2DContext *ctx, double *dst,
                         ptrdiff_t dst_stride,
                         const ptrdiff_t dst_start[2],
                         const size_t    dst_size[2],
                         const double    dst_step[2]);

int mg2d_interp(MG2DContext *ctx,
                double *dst,
                ptrdiff_t dst_stride,
                const ptrdiff_t dst_start[2],
                const size_t    dst_size[2],
                const double    dst_step[2],
                double *src,
                ptrdiff_t src_stride,
                const ptrdiff_t src_start[2],
                const size_t    src_size[2],
                const double    src_step[2]);

#endif /* MG2D_H */