aboutsummaryrefslogtreecommitdiff
path: root/basis.c
blob: 89437c6d1314ee9a098da57d75addafcd5a3fa9d (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Basis sets for pseudospectral methods
 * Copyright (C) 2016 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/>.
 */

#include "config.h"

#include <errno.h>
#include <math.h>

#include "basis.h"
#include "common.h"

typedef struct BasisSet {
    /* evaluate the idx-th basis function at the specified point*/
    double (*eval)      (const BasisSetContext *s, double coord, unsigned int idx);
    /* evaluate the first derivative of the idx-th basis function at the specified point*/
    double (*eval_diff1)(const BasisSetContext *s, double coord, unsigned int idx);
    /* evaluate the second derivative of the idx-th basis function at the specified point*/
    double (*eval_diff2)(const BasisSetContext *s, double coord, unsigned int idx);
    /**
     * Get the idx-th collocation point for the specified order.
     * idx runs from 0 to order - 1 (inclusive)
     */
    double (*colloc_point)(const BasisSetContext *s, unsigned int order, unsigned int idx);
} BasisSet;

struct BasisSetContext {
    const BasisSet *bs;
    double sf;
};

/*
 * The basis of even (n = 2 * idx) SB functions (Boyd 2000, Ch 17.9)
 * SB(x, n) = sin((n + 1) arccot(|x| / L))
 * They are symmetric wrt origin and decay as 1/x in infinity.
 */
static double sb_even_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = atan2(s->sf, coord);

    idx *= 2;   // even only

    return sin((idx + 1) * val);
}

static double sb_even_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = atan2(s->sf, coord);

    idx *= 2;   // even only

    return -s->sf * (idx + 1) * cos((idx + 1) * val) / (SQR(s->sf) + SQR(coord));
}

static double sb_even_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    const double sf = s->sf;
    double val = atan2(sf, coord);

    idx *= 2;   // even only

    return sf * (idx + 1) * (2 * coord * cos((idx + 1) * val) - sf * (idx + 1) * sin((idx + 1) * val)) / SQR(SQR(sf) + SQR(coord));
}

static double sb_even_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    double t;

    idx = order - idx - 1;
    //order *= 2;

    //t = (idx + 2) * M_PI / (order + 4);
#if TD_POLAR
    t = (idx + 2) * M_PI / (2 * order + 3);
    //t = (2 * idx + 1) * M_PI / (4 * order + 2);
#else
    t = (idx + 2) * M_PI / (2 * order + 2);
#endif
    return s->sf / tan(t);
}

static const BasisSet sb_even_basis = {
    .eval         = sb_even_eval,
    .eval_diff1   = sb_even_eval_diff1,
    .eval_diff2   = sb_even_eval_diff2,
    .colloc_point = sb_even_colloc_point,
};

static double sb_odd_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = atan2(s->sf, coord);

    idx = 2 * idx + 2;   // odd only

    return sin((idx) * val);
}

static double sb_odd_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = atan2(s->sf, coord);

    idx = 2 * idx + 2;   // odd only

    return -s->sf * (idx) * cos((idx) * val) / (SQR(s->sf) + SQR(coord));
}

static double sb_odd_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    const double sf = s->sf;
    double val = atan2(sf, coord);

    idx = 2 * idx + 2;   // odd only

    return sf * (idx) * (2 * coord * cos((idx) * val) - sf * (idx) * sin((idx) * val)) / SQR(SQR(sf) + SQR(coord));
}

static double sb_odd_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    double t;

    idx = order - idx - 1;
    //order *= 2;

    //t = (idx + 2) * M_PI / (order + 4);
#if TD_POLAR
    t = (idx + 2) * M_PI / (2 * order + 3);
#else
    t = (idx + 2) * M_PI / (2 * order + 3);
#endif
    return s->sf / tan(t);
}

static const BasisSet sb_odd_basis = {
    .eval         = sb_odd_eval,
    .eval_diff1   = sb_odd_eval_diff1,
    .eval_diff2   = sb_odd_eval_diff2,
    .colloc_point = sb_odd_colloc_point,
};

static double tb_even_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = (coord == 0.0) ? M_PI_2 : atan(s->sf / fabs(coord));

    idx++;
    idx *= 2;   // even only

    return cos(idx * val) - 1.0;
}

static double tb_even_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    double val = (coord == 0.0) ? M_PI_2 : atan(s->sf / fabs(coord));

    idx++;
    idx *= 2;   // even only

    return s->sf * idx * SGN(coord) * sin(idx * val) / (SQR(s->sf) + SQR(coord));
}

static double tb_even_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    const double sf = s->sf;
    double val = (coord == 0.0) ? M_PI_2 : atan(sf / fabs(coord));

    idx++;
    idx *= 2;   // even only

    return -sf * idx * SGN(coord) * (2 * fabs(coord) * sin(idx * val) + sf * idx * cos(idx * val)) / SQR(SQR(sf) + SQR(coord));
}

static double tb_even_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    double t;

    idx = order - idx - 1;
    //order *= 2;

    //t = (idx + 2) * M_PI / (order + 4);
    t = (idx + 2) * M_PI / (2 * order + 4);
    return s->sf / tan(t);
}

static const BasisSet tb_even_basis = {
    .eval         = tb_even_eval,
    .eval_diff1   = tb_even_eval_diff1,
    .eval_diff2   = tb_even_eval_diff2,
    .colloc_point = tb_even_colloc_point,
};

static double cos_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    return cos(idx * coord);
}

static double cos_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -1.0 * idx * sin(idx * coord);
}

static double cos_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -1.0 * SQR(idx) * cos(idx * coord);
}

static double cos_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    return M_PI * (idx + 1) / (order + 2);
}

static const BasisSet cos_basis = {
    .eval         = cos_eval,
    .eval_diff1   = cos_eval_diff1,
    .eval_diff2   = cos_eval_diff2,
    .colloc_point = cos_colloc_point,
};

static double cos_even_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    return cos(2 * idx * coord);
}

static double cos_even_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -2.0 * idx * sin(2.0 * idx * coord);
}

static double cos_even_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -4.0 * SQR(idx) * cos(2.0 * idx * coord);
}

static double cos_even_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    return M_PI * (idx + 1) / (2 * order + 2);
}

static const BasisSet cos_even_basis = {
    .eval         = cos_even_eval,
    .eval_diff1   = cos_even_eval_diff1,
    .eval_diff2   = cos_even_eval_diff2,
    .colloc_point = cos_even_colloc_point,
};

static double cos_4_eval(const BasisSetContext *s, double coord, unsigned int idx)
{
    return cos(4 * idx * coord);
}

static double cos_4_eval_diff1(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -4.0 * idx * sin(4.0 * idx * coord);
}

static double cos_4_eval_diff2(const BasisSetContext *s, double coord, unsigned int idx)
{
    return -16.0 * SQR(idx) * cos(4.0 * idx * coord);
}

static double cos_4_colloc_point(const BasisSetContext *s, unsigned int order, unsigned int idx)
{
    return M_PI * (idx + 1) / (4 * order + 4);
}

static const BasisSet cos_4_basis = {
    .eval         = cos_4_eval,
    .eval_diff1   = cos_4_eval_diff1,
    .eval_diff2   = cos_4_eval_diff2,
    .colloc_point = cos_4_colloc_point,
};

double tdi_basis_eval(const BasisSetContext *s, enum BSEvalType type,
                      double coord, unsigned int order)
{
    double (*eval)(const BasisSetContext *, double, unsigned int) = NULL;

    switch (type) {
    case BS_EVAL_TYPE_VALUE: eval = s->bs->eval;       break;
    case BS_EVAL_TYPE_DIFF1: eval = s->bs->eval_diff1; break;
    case BS_EVAL_TYPE_DIFF2: eval = s->bs->eval_diff2; break;
    }

    return eval(s, coord, order);
}

double tdi_basis_colloc_point(const BasisSetContext *s, unsigned int order,
                              unsigned int idx)
{
    return s->bs->colloc_point(s, order, idx);
}

void tdi_basis_free(BasisSetContext **pctx)
{
    BasisSetContext *ctx = *pctx;

    if (!ctx)
        return;

    free(ctx);
    *pctx = NULL;
}

int tdi_basis_init(BasisSetContext **pctx, enum BasisFamily family, double sf)
{
    BasisSetContext *ctx;

    ctx = calloc(1, sizeof(*ctx));
    if (!ctx)
        return -ENOMEM;

    switch (family) {
    case BASIS_FAMILY_TB_EVEN:   ctx->bs = &tb_even_basis;   break;
    case BASIS_FAMILY_SB_EVEN:   ctx->bs = &sb_even_basis;   break;
    case BASIS_FAMILY_SB_ODD:    ctx->bs = &sb_odd_basis;    break;
    case BASIS_FAMILY_COS:       ctx->bs = &cos_basis;       break;
    case BASIS_FAMILY_COS_EVEN:  ctx->bs = &cos_even_basis;  break;
    case BASIS_FAMILY_COS_4:     ctx->bs = &cos_4_basis;     break;
    default:
        free(ctx);
        return -EINVAL;
    }

    ctx->sf = sf;

    *pctx = ctx;
    return 0;
}