aboutsummaryrefslogtreecommitdiff
path: root/init.c
blob: 6ea45debb3e6e3bbfd5dcc024ade1abce9551f10 (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
/*
 * Copyright 2014-2015 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 <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "brill_data.h"
#include "internal.h"

static int brill_init_check_options(BDContext *bd)
{
    BDPriv *s = bd->priv;

    switch (bd->q_func_type) {
    case BD_Q_FUNC_GUNDLACH:
        s->q_func   = &bdi_q_func_gundlach;
        break;
    case BD_Q_FUNC_EPPLEY:
        if (bd->eppley_n < 4) {
            bdi_log(bd, 0, "Invalid n: %d < 4\n", bd->eppley_n);
            return -EINVAL;
        }

        s->q_func   = &bdi_q_func_eppley;
        break;
    default:
        bdi_log(bd, 0, "Unknown q function type: %d\n", bd->q_func_type);
        return -EINVAL;
    }

    s->basis[0] = &bdi_sb_even_basis;
    s->basis[1] = &bdi_sb_even_basis;

    s->nb_colloc_points[0] = bd->nb_coeffs[0];
    s->nb_colloc_points[1] = bd->nb_coeffs[1];
    s->nb_coeffs[0]        = bd->nb_coeffs[0];
    s->nb_coeffs[1]        = bd->nb_coeffs[1];

    return 0;
}

int bd_solve(BDContext *bd)
{
    BDPriv *s = bd->priv;
    int ret;

    if (bd->psi_minus1_coeffs) {
        bdi_log(bd, 0, "Solve called multiple times on the same context.\n");
        return -EINVAL;
    }

    ret = brill_init_check_options(bd);
    if (ret < 0)
        return ret;

    ret = bdi_solve(bd);
    if (ret < 0)
        return ret;

    bd->psi_minus1_coeffs = s->coeffs;
    bd->stride            = s->nb_coeffs[0];

    return 0;
}

BDContext *bd_context_alloc(void)
{
    BDContext *bd = calloc(1, sizeof(*bd));

    if (!bd)
        return NULL;

    bd->q_func_type = BD_Q_FUNC_GUNDLACH;
    bd->amplitude   = 1.0;
    bd->eppley_n    = 5;

    bd->nb_coeffs[0] = 80;
    bd->nb_coeffs[1] = 80;

    bd->basis_scale_factor[0] = 3.0;
    bd->basis_scale_factor[1] = 3.0;

    bd->log_callback = bdi_log_default_callback;

    bd->priv = calloc(1, sizeof(BDPriv));
    if (!bd->priv) {
        free(bd);
        return NULL;
    }

    return bd;
}

void bd_context_free(BDContext **pbd)
{
    BDContext *bd = *pbd;
    BDPriv *s;

    if (!bd)
        return;

    s = bd->priv;

    free(s->coeffs);

    free(bd->priv);
    free(bd);
    *pbd = NULL;
}